Skip to content

Commit

Permalink
Add initial signed-memo program (#1135)
Browse files Browse the repository at this point in the history
* Initial s-memo

* Populate readme

* Add signed-memo to spl docs

* Log less, fail faster

* Replace and bump memo

* Update memo id

* Add memo prefix and len

* Add test that demonstrates compute bounds

* Add logging and compute to memo docs
  • Loading branch information
CriesofCarrots authored Jan 28, 2021
1 parent 1c4753e commit 190e664
Show file tree
Hide file tree
Showing 10 changed files with 439 additions and 53 deletions.
13 changes: 8 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 55 additions & 7 deletions docs/src/memo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
title: Memo Program
---

A simple program that validates a string of UTF-8 encoded characters. It can be
used to record a string on-chain, stored in the instruction data of a successful
transaction.
The Memo program is a simple program that validates a string of UTF-8 encoded
characters and verifies that any accounts provided are signers of the
transaction. The program also logs the memo, as well as any verified signer
addresses, to the transaction log, so that anyone can easily observe memos and
know they were approved by zero or more addresses by inspecting the transaction
log from a trusted provider.

## Background

Expand All @@ -22,9 +25,54 @@ The Memo Program's source is available on
## Interface

The on-chain Memo Program is written in Rust and available on crates.io as
[spl-memo](https://crates.io/crates/spl-memo).
[spl-memo](https://crates.io/crates/spl-memo) and
[docs.rs](https://docs.rs/spl-memo).

## Operational overview
The crate provides a `build_memo()` method to easily create a properly
constructed Instruction.

The Memo program attempts to UTF-8 decode the instruction data; if successfully
decoded, the instruction is successful.
## Operational Notes

If zero accounts are provided to the signed-memo instruction, the program
succeeds when the memo is valid UTF-8, and logs the memo to the transaction log.

If one or more accounts are provided to the signed-memo instruction, all must be
valid signers of the transaction for the instruction to succeed.

### Logs

This section details expected log output for memo instructions.

Logging begins with entry into the program:
`Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr invoke [1]`

The program will include a separate log for each verified signer:
`Program log: Signed by <BASE_58_ADDRESS>`

Then the program logs the memo length and UTF-8 text:
`Program log: Memo (len 4): "🐆"`

If UTF-8 parsing fails, the program will log the failure point:
`Program log: Invalid UTF-8, from byte 4`

Logging ends with the status of the instruction, one of:
`Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr success`
`Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr failed: missing required signature for instruction`
`Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr failed: invalid instruction data`

For more information about exposing program logs on a node, head to the
[developer
docs](https://docs.solana.com/developing/deployed-programs/debugging#logging)

### Compute Limits

Like all programs, the Memo Program is subject to the cluster's [compute
budget](https://docs.solana.com/developing/programming-model/runtime#compute-budget).
In Memo, compute is used for parsing UTF-8, verifying signers, and logging,
limiting the memo length and number of signers that can be processed
successfully in a single instruction. The longer or more complex the UTF-8 memo,
the fewer signers can be supported, and vice versa.

As of v1.5.1, an unsigned instruction can support single-byte UTF-8 of up to 566
bytes. An instruction with a simple memo of 32 bytes can support up to 12
signers.
8 changes: 5 additions & 3 deletions memo/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Memo Program

A simple program that validates a string of UTF-8 encoded characters. It can be
used to record a string on-chain, stored in the instruction data of a successful
transaction.
A simple program that validates a string of UTF-8 encoded characters and logs it
in the transaction log. The program also verifies that any accounts provided are
signers of the transaction, and if so, logs their addresses. It can be used to
record a string on-chain, stored in the instruction data of a successful
transaction, and optionally verify the originator.

Full documentation is available at https://spl.solana.com/memo
8 changes: 7 additions & 1 deletion memo/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spl-memo"
version = "2.0.1"
version = "3.0.0"
description = "Solana Program Library Memo"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
repository = "https://github.com/solana-labs/solana-program-library"
Expand All @@ -9,10 +9,16 @@ edition = "2018"

[features]
no-entrypoint = []
test-bpf = []

[dependencies]
solana-program = "1.5.1"

[dev-dependencies]
solana-program-test = "1.5.1"
solana-sdk = "1.5.1"
tokio = { version = "0.3", features = ["macros"]}

[lib]
crate-type = ["cdylib", "lib"]

Expand Down
2 changes: 1 addition & 1 deletion memo/program/program-id.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo
MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr
15 changes: 15 additions & 0 deletions memo/program/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -ex
cd "$(dirname "$0")"
cargo fmt -- --check
cargo clippy
cargo build
cargo build-bpf

if [[ $1 = -v ]]; then
export RUST_LOG=solana=debug
fi

cargo test
cargo test-bpf
39 changes: 6 additions & 33 deletions memo/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
//! Program entrypoint

#![cfg(not(feature = "no-entrypoint"))]

use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey,
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, pubkey::Pubkey,
};
use std::str::from_utf8;

entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
from_utf8(instruction_data).map_err(|_| ProgramError::InvalidInstructionData)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use solana_program::{program_error::ProgramError, pubkey::Pubkey};

#[test]
fn test_utf8_memo() {
let program_id = Pubkey::new(&[0; 32]);

let string = b"letters and such";
assert_eq!(Ok(()), process_instruction(&program_id, &[], string));

let emoji = "🐆".as_bytes();
let bytes = [0xF0, 0x9F, 0x90, 0x86];
assert_eq!(emoji, bytes);
assert_eq!(Ok(()), process_instruction(&program_id, &[], &emoji));

let mut bad_utf8 = bytes;
bad_utf8[3] = 0xFF; // Invalid UTF-8 byte
assert_eq!(
Err(ProgramError::InvalidInstructionData),
process_instruction(&program_id, &[], &bad_utf8)
);
}
crate::processor::process_instruction(program_id, accounts, instruction_data)
}
29 changes: 26 additions & 3 deletions memo/program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
#![deny(missing_docs)]

//! A simple program that accepts a string of encoded characters and verifies that it parses. Currently handles UTF-8.
//! A program that accepts a string of encoded characters and verifies that it parses,
//! while verifying and logging signers. Currently handles UTF-8 characters.

#[cfg(not(feature = "no-entrypoint"))]
mod entrypoint;
pub mod processor;

// Export current sdk types for downstream users building with a different sdk version
pub use solana_program;
use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};

solana_program::declare_id!("Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo");
solana_program::declare_id!("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");

/// Build a memo instruction, possibly signed
///
/// Accounts expected by this instruction:
///
/// 0. ..0+N. `[signer]` Expected signers; if zero provided, instruction will be processed as a
/// normal, unsigned spl-memo
///
pub fn build_memo(memo: &[u8], signer_pubkeys: &[&Pubkey]) -> Instruction {
Instruction {
program_id: id(),
accounts: signer_pubkeys
.iter()
.map(|&pubkey| AccountMeta::new_readonly(*pubkey, true))
.collect(),
data: memo.to_vec(),
}
}
109 changes: 109 additions & 0 deletions memo/program/src/processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! Program state processor

use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, msg, program_error::ProgramError,
pubkey::Pubkey,
};
use std::str::from_utf8;

/// Instruction processor
pub fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let mut missing_required_signature = false;
for account_info in account_info_iter {
if let Some(address) = account_info.signer_key() {
msg!("Signed by {:?}", address);
} else {
missing_required_signature = true;
}
}
if missing_required_signature {
return Err(ProgramError::MissingRequiredSignature);
}

let memo = from_utf8(input).map_err(|err| {
msg!("Invalid UTF-8, from byte {}", err.valid_up_to());
ProgramError::InvalidInstructionData
})?;
msg!("Memo (len {}): {:?}", memo.len(), memo);

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use solana_program::{
account_info::IntoAccountInfo, program_error::ProgramError, pubkey::Pubkey,
};
use solana_sdk::account::Account;

#[test]
fn test_utf8_memo() {
let program_id = Pubkey::new(&[0; 32]);

let string = b"letters and such";
assert_eq!(Ok(()), process_instruction(&program_id, &[], string));

let emoji = "🐆".as_bytes();
let bytes = [0xF0, 0x9F, 0x90, 0x86];
assert_eq!(emoji, bytes);
assert_eq!(Ok(()), process_instruction(&program_id, &[], &emoji));

let mut bad_utf8 = bytes;
bad_utf8[3] = 0xFF; // Invalid UTF-8 byte
assert_eq!(
Err(ProgramError::InvalidInstructionData),
process_instruction(&program_id, &[], &bad_utf8)
);
}

#[test]
fn test_signers() {
let program_id = Pubkey::new(&[0; 32]);
let memo = "🐆".as_bytes();

let pubkey0 = Pubkey::new_unique();
let pubkey1 = Pubkey::new_unique();
let pubkey2 = Pubkey::new_unique();
let mut account0 = Account::default();
let mut account1 = Account::default();
let mut account2 = Account::default();

let signed_account_infos = vec![
(&pubkey0, true, &mut account0).into_account_info(),
(&pubkey1, true, &mut account1).into_account_info(),
(&pubkey2, true, &mut account2).into_account_info(),
];
assert_eq!(
Ok(()),
process_instruction(&program_id, &signed_account_infos, memo)
);

assert_eq!(Ok(()), process_instruction(&program_id, &[], memo));

let unsigned_account_infos = vec![
(&pubkey0, false, &mut account0).into_account_info(),
(&pubkey1, false, &mut account1).into_account_info(),
(&pubkey2, false, &mut account2).into_account_info(),
];
assert_eq!(
Err(ProgramError::MissingRequiredSignature),
process_instruction(&program_id, &unsigned_account_infos, memo)
);

let partially_signed_account_infos = vec![
(&pubkey0, true, &mut account0).into_account_info(),
(&pubkey1, false, &mut account1).into_account_info(),
(&pubkey2, true, &mut account2).into_account_info(),
];
assert_eq!(
Err(ProgramError::MissingRequiredSignature),
process_instruction(&program_id, &partially_signed_account_infos, memo)
);
}
}
Loading

0 comments on commit 190e664

Please sign in to comment.