diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index e273ea4bf..dc8c68293 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -1987,6 +1987,14 @@ dependencies = [ "solana-sdk-bpf-test 0.23.0", ] +[[package]] +name = "solana-bpf-rust-dup-accounts" +version = "0.23.0" +dependencies = [ + "solana-sdk 0.23.0", + "solana-sdk-bpf-test 0.23.0", +] + [[package]] name = "solana-bpf-rust-external-spend" version = "0.23.0" diff --git a/programs/bpf/Cargo.toml b/programs/bpf/Cargo.toml index 0e9e942b7..0c0fed00f 100644 --- a/programs/bpf/Cargo.toml +++ b/programs/bpf/Cargo.toml @@ -37,6 +37,7 @@ members = [ "rust/128bit_dep", "rust/alloc", "rust/dep_crate", + "rust/dup_accounts", "rust/external_spend", "rust/iter", "rust/many_args", diff --git a/programs/bpf/build.rs b/programs/bpf/build.rs index 4cd52727a..6ce572d7a 100644 --- a/programs/bpf/build.rs +++ b/programs/bpf/build.rs @@ -70,6 +70,7 @@ fn main() { "128bit", "alloc", "dep_crate", + "dup_accounts", "iter", "many_args", "external_spend", diff --git a/programs/bpf/rust/dup_accounts/Cargo.toml b/programs/bpf/rust/dup_accounts/Cargo.toml new file mode 100644 index 000000000..65cbb9c67 --- /dev/null +++ b/programs/bpf/rust/dup_accounts/Cargo.toml @@ -0,0 +1,26 @@ + +# Note: This crate must be built using do.sh + +[package] +name = "solana-bpf-rust-dup-accounts" +version = "0.23.0" +description = "Solana BPF test program written in Rust" +authors = ["Solana Maintainers "] +repository = "https://github.com/solana-labs/solana" +license = "Apache-2.0" +homepage = "https://solana.com/" +edition = "2018" + +[dependencies] +solana-sdk = { path = "../../../../sdk/", version = "0.23.0", default-features = false } + +[dev_dependencies] +solana-sdk-bpf-test = { path = "../../../../sdk/bpf/rust/test", version = "0.23.0" } + +[features] +program = ["solana-sdk/program"] +default = ["program"] + +[lib] +name = "solana_bpf_rust_dup_accounts" +crate-type = ["cdylib"] diff --git a/programs/bpf/rust/dup_accounts/Xargo.toml b/programs/bpf/rust/dup_accounts/Xargo.toml new file mode 100644 index 000000000..1744f098a --- /dev/null +++ b/programs/bpf/rust/dup_accounts/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] \ No newline at end of file diff --git a/programs/bpf/rust/dup_accounts/src/lib.rs b/programs/bpf/rust/dup_accounts/src/lib.rs new file mode 100644 index 000000000..f0d621ecc --- /dev/null +++ b/programs/bpf/rust/dup_accounts/src/lib.rs @@ -0,0 +1,48 @@ +//! @brief Example Rust-based BPF program that tests duplicate accounts passed via accounts + +extern crate solana_sdk; +use solana_sdk::{ + account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, info, pubkey::Pubkey, +}; + +entrypoint!(process_instruction); +fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32 { + const FAILURE: u32 = 1; + + match data[0] { + 1 => { + info!("modify first account data"); + accounts[2].data[0] = 1; + } + 2 => { + info!("modify first account data"); + accounts[3].data[0] = 2; + } + 3 => { + info!("modify both account data, should fail"); + accounts[2].data[0] = 1; + accounts[3].data[0] = 2; + } + 4 => { + info!("modify first account lamports"); + *accounts[1].lamports -= 1; + *accounts[2].lamports += 1; + } + 5 => { + info!("modify first account lamports"); + *accounts[1].lamports -= 2; + *accounts[3].lamports += 2; + } + 6 => { + info!("modify both account lamports, should fail"); + *accounts[1].lamports -= 1; + *accounts[2].lamports += 1; + *accounts[3].lamports += 2; + } + _ => { + info!("Unrecognized command"); + return FAILURE; + } + } + SUCCESS +}