Files
solana/programs/bpf/rust/invoked/src/processor.rs

237 lines
9.0 KiB
Rust
Raw Normal View History

2020-10-06 11:03:51 -07:00
//! @brief Example Rust-based BPF program that issues a cross-program-invocation
Separate the "program" feature of `solana-sdk` into a new crate called `solana-program` (bp #12989) (#13131) * Add solana-program-sdk boilerplate (cherry picked from commit 3718771ffb9caad0bc1c180e488f699ce895a10e) # Conflicts: # sdk/Cargo.toml * Initial population of solana-program-sdk (cherry picked from commit 63db3242043602c7b9765a066b6f8d01ef20454e) # Conflicts: # Cargo.lock * Port programs to solana-program-sdk (cherry picked from commit fe68f7f786acf13e5b1f1fe4c54eb7af7b549391) # Conflicts: # programs/bpf/Cargo.lock # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Only activate legacy program feature for the solana-sdk crate (cherry picked from commit 85c51f5787895c97220d3a2f8ce9be6b9cbdd6c6) * Run serum-dex unit tests (cherry picked from commit 92ce381d60d65a73973fc20cdd4772334f973b96) * Rename solana-program-sdk to solana-program (cherry picked from commit dd711ab5fbf96c1efcfdf9488397e8af2568182a) # Conflicts: # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Update frozen_abi hashes The movement of files in sdk/ caused ABI hashes to change (cherry picked from commit a4956844bdd081e7b90508066c579f29be306ce7) * Resolve merge conflicts Co-authored-by: Michael Vines <mvines@gmail.com>
2020-10-24 17:25:22 +00:00
#![cfg(feature = "program")]
2020-10-06 11:03:51 -07:00
use crate::instruction::*;
Separate the "program" feature of `solana-sdk` into a new crate called `solana-program` (bp #12989) (#13131) * Add solana-program-sdk boilerplate (cherry picked from commit 3718771ffb9caad0bc1c180e488f699ce895a10e) # Conflicts: # sdk/Cargo.toml * Initial population of solana-program-sdk (cherry picked from commit 63db3242043602c7b9765a066b6f8d01ef20454e) # Conflicts: # Cargo.lock * Port programs to solana-program-sdk (cherry picked from commit fe68f7f786acf13e5b1f1fe4c54eb7af7b549391) # Conflicts: # programs/bpf/Cargo.lock # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Only activate legacy program feature for the solana-sdk crate (cherry picked from commit 85c51f5787895c97220d3a2f8ce9be6b9cbdd6c6) * Run serum-dex unit tests (cherry picked from commit 92ce381d60d65a73973fc20cdd4772334f973b96) * Rename solana-program-sdk to solana-program (cherry picked from commit dd711ab5fbf96c1efcfdf9488397e8af2568182a) # Conflicts: # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Update frozen_abi hashes The movement of files in sdk/ caused ABI hashes to change (cherry picked from commit a4956844bdd081e7b90508066c579f29be306ce7) * Resolve merge conflicts Co-authored-by: Michael Vines <mvines@gmail.com>
2020-10-24 17:25:22 +00:00
use solana_program::{
account_info::AccountInfo,
bpf_loader, entrypoint,
entrypoint::ProgramResult,
msg,
program::{invoke, invoke_signed},
program_error::ProgramError,
pubkey::Pubkey,
2020-10-06 11:03:51 -07:00
};
entrypoint!(process_instruction);
#[allow(clippy::cognitive_complexity)]
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
msg!("Invoked program");
2020-10-06 11:03:51 -07:00
if instruction_data.is_empty() {
return Ok(());
}
match instruction_data[0] {
VERIFY_TRANSLATIONS => {
msg!("verify data translations");
2020-10-06 11:03:51 -07:00
const ARGUMENT_INDEX: usize = 0;
const INVOKED_ARGUMENT_INDEX: usize = 1;
const INVOKED_PROGRAM_INDEX: usize = 2;
const INVOKED_PROGRAM_DUP_INDEX: usize = 3;
assert_eq!(&instruction_data[1..], &[1, 2, 3, 4, 5]);
assert_eq!(accounts.len(), 4);
assert_eq!(accounts[ARGUMENT_INDEX].lamports(), 42);
assert_eq!(accounts[ARGUMENT_INDEX].data_len(), 100);
assert!(accounts[ARGUMENT_INDEX].is_signer);
assert!(accounts[ARGUMENT_INDEX].is_writable);
assert_eq!(accounts[ARGUMENT_INDEX].rent_epoch, 0);
assert!(!accounts[ARGUMENT_INDEX].executable);
{
let data = accounts[ARGUMENT_INDEX].try_borrow_data()?;
for i in 0..100 {
assert_eq!(data[i as usize], i);
}
}
assert_eq!(
accounts[INVOKED_ARGUMENT_INDEX].owner,
accounts[INVOKED_PROGRAM_INDEX].key
);
assert_eq!(accounts[INVOKED_ARGUMENT_INDEX].lamports(), 10);
assert_eq!(accounts[INVOKED_ARGUMENT_INDEX].data_len(), 10);
assert!(accounts[INVOKED_ARGUMENT_INDEX].is_signer);
assert!(accounts[INVOKED_ARGUMENT_INDEX].is_writable);
assert_eq!(accounts[INVOKED_ARGUMENT_INDEX].rent_epoch, 0);
assert!(!accounts[INVOKED_ARGUMENT_INDEX].executable);
assert_eq!(accounts[INVOKED_PROGRAM_INDEX].key, program_id);
assert_eq!(accounts[INVOKED_PROGRAM_INDEX].owner, &bpf_loader::id());
assert!(!accounts[INVOKED_PROGRAM_INDEX].is_signer);
assert!(!accounts[INVOKED_PROGRAM_INDEX].is_writable);
assert_eq!(accounts[INVOKED_PROGRAM_INDEX].rent_epoch, 0);
assert!(accounts[INVOKED_PROGRAM_INDEX].executable);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].key,
accounts[INVOKED_PROGRAM_DUP_INDEX].key
);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].owner,
accounts[INVOKED_PROGRAM_DUP_INDEX].owner
);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].lamports,
accounts[INVOKED_PROGRAM_DUP_INDEX].lamports
);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].is_signer,
accounts[INVOKED_PROGRAM_DUP_INDEX].is_signer
);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].is_writable,
accounts[INVOKED_PROGRAM_DUP_INDEX].is_writable
);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].rent_epoch,
accounts[INVOKED_PROGRAM_DUP_INDEX].rent_epoch
);
assert_eq!(
accounts[INVOKED_PROGRAM_INDEX].executable,
accounts[INVOKED_PROGRAM_DUP_INDEX].executable
);
{
let data = accounts[INVOKED_PROGRAM_INDEX].try_borrow_data()?;
assert!(accounts[INVOKED_PROGRAM_DUP_INDEX]
.try_borrow_mut_data()
.is_err());
msg!(data[0], 0, 0, 0, 0);
2020-10-06 11:03:51 -07:00
}
}
RETURN_OK => {
msg!("Ok");
return Ok(());
}
RETURN_ERROR => {
msg!("return error");
2020-10-06 11:03:51 -07:00
return Err(ProgramError::Custom(42));
}
DERIVED_SIGNERS => {
msg!("verify derived signers");
const INVOKED_PROGRAM_INDEX: usize = 0;
2020-10-06 11:03:51 -07:00
const DERIVED_KEY1_INDEX: usize = 1;
const DERIVED_KEY2_INDEX: usize = 2;
const DERIVED_KEY3_INDEX: usize = 3;
assert!(accounts[DERIVED_KEY1_INDEX].is_signer);
assert!(!accounts[DERIVED_KEY2_INDEX].is_signer);
assert!(!accounts[DERIVED_KEY3_INDEX].is_signer);
let bump_seed2 = instruction_data[1];
let bump_seed3 = instruction_data[2];
let invoked_instruction = create_instruction(
*accounts[INVOKED_PROGRAM_INDEX].key,
&[
(accounts[DERIVED_KEY1_INDEX].key, true, false),
(accounts[DERIVED_KEY2_INDEX].key, true, true),
(accounts[DERIVED_KEY3_INDEX].key, false, true),
],
vec![VERIFY_NESTED_SIGNERS],
);
invoke_signed(
&invoked_instruction,
accounts,
&[
&[b"Lil'", b"Bits", &[bump_seed2]],
&[accounts[DERIVED_KEY2_INDEX].key.as_ref(), &[bump_seed3]],
],
)?;
2020-10-06 11:03:51 -07:00
}
VERIFY_NESTED_SIGNERS => {
msg!("verify nested derived signers");
2020-10-06 11:03:51 -07:00
const DERIVED_KEY1_INDEX: usize = 0;
const DERIVED_KEY2_INDEX: usize = 1;
const DERIVED_KEY3_INDEX: usize = 2;
assert!(!accounts[DERIVED_KEY1_INDEX].is_signer);
assert!(accounts[DERIVED_KEY2_INDEX].is_signer);
assert!(accounts[DERIVED_KEY3_INDEX].is_signer);
}
VERIFY_WRITER => {
msg!("verify writable");
2020-10-06 11:03:51 -07:00
const ARGUMENT_INDEX: usize = 0;
assert!(!accounts[ARGUMENT_INDEX].is_writable);
}
VERIFY_PRIVILEGE_ESCALATION => {
msg!("Verify privilege escalation");
}
VERIFY_PRIVILEGE_DEESCALATION => {
msg!("verify privilege deescalation");
const INVOKED_ARGUMENT_INDEX: usize = 0;
assert!(!accounts[INVOKED_ARGUMENT_INDEX].is_signer);
assert!(!accounts[INVOKED_ARGUMENT_INDEX].is_writable);
}
VERIFY_PRIVILEGE_DEESCALATION_ESCALATION_SIGNER => {
msg!("verify privilege deescalation escalation signer");
const INVOKED_PROGRAM_INDEX: usize = 0;
const INVOKED_ARGUMENT_INDEX: usize = 1;
assert!(!accounts[INVOKED_ARGUMENT_INDEX].is_signer);
assert!(!accounts[INVOKED_ARGUMENT_INDEX].is_writable);
let invoked_instruction = create_instruction(
*accounts[INVOKED_PROGRAM_INDEX].key,
&[(accounts[INVOKED_ARGUMENT_INDEX].key, true, false)],
vec![VERIFY_PRIVILEGE_ESCALATION],
);
invoke(&invoked_instruction, accounts)?;
}
VERIFY_PRIVILEGE_DEESCALATION_ESCALATION_WRITABLE => {
msg!("verify privilege deescalation escalation writable");
const INVOKED_PROGRAM_INDEX: usize = 0;
const INVOKED_ARGUMENT_INDEX: usize = 1;
assert!(!accounts[INVOKED_ARGUMENT_INDEX].is_signer);
assert!(!accounts[INVOKED_ARGUMENT_INDEX].is_writable);
let invoked_instruction = create_instruction(
*accounts[INVOKED_PROGRAM_INDEX].key,
&[(accounts[INVOKED_ARGUMENT_INDEX].key, false, true)],
vec![VERIFY_PRIVILEGE_ESCALATION],
);
invoke(&invoked_instruction, accounts)?;
2020-10-06 11:03:51 -07:00
}
NESTED_INVOKE => {
msg!("nested invoke");
2020-10-06 11:03:51 -07:00
const ARGUMENT_INDEX: usize = 0;
const INVOKED_ARGUMENT_INDEX: usize = 1;
const INVOKED_PROGRAM_INDEX: usize = 3;
assert!(accounts[INVOKED_ARGUMENT_INDEX].is_signer);
**accounts[INVOKED_ARGUMENT_INDEX].lamports.borrow_mut() -= 1;
**accounts[ARGUMENT_INDEX].lamports.borrow_mut() += 1;
if accounts.len() > 2 {
msg!("Invoke again");
2020-10-06 11:03:51 -07:00
let invoked_instruction = create_instruction(
*accounts[INVOKED_PROGRAM_INDEX].key,
&[
(accounts[ARGUMENT_INDEX].key, true, true),
(accounts[INVOKED_ARGUMENT_INDEX].key, true, true),
],
vec![NESTED_INVOKE],
2020-10-06 11:03:51 -07:00
);
invoke(&invoked_instruction, accounts)?;
} else {
msg!("Last invoked");
2020-10-06 11:03:51 -07:00
{
let mut data = accounts[INVOKED_ARGUMENT_INDEX].try_borrow_mut_data()?;
for i in 0..10 {
data[i as usize] = i;
}
}
}
}
_ => panic!(),
}
Ok(())
}