Add way to look at tx instructions (#11943)
This commit is contained in:
7
programs/bpf/Cargo.lock
generated
7
programs/bpf/Cargo.lock
generated
@@ -1822,6 +1822,13 @@ dependencies = [
|
||||
"solana-sdk",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-instruction-introspection"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"solana-sdk",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-invoke"
|
||||
version = "1.4.0"
|
||||
|
@@ -41,6 +41,7 @@ members = [
|
||||
"rust/dup_accounts",
|
||||
"rust/error_handling",
|
||||
"rust/external_spend",
|
||||
"rust/instruction_introspection",
|
||||
"rust/invoke",
|
||||
"rust/invoked",
|
||||
"rust/iter",
|
||||
|
@@ -72,6 +72,7 @@ fn main() {
|
||||
"dup_accounts",
|
||||
"error_handling",
|
||||
"external_spend",
|
||||
"instruction_introspection",
|
||||
"invoke",
|
||||
"invoked",
|
||||
"iter",
|
||||
|
26
programs/bpf/rust/instruction_introspection/Cargo.toml
Normal file
26
programs/bpf/rust/instruction_introspection/Cargo.toml
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
# Note: This crate must be built using do.sh
|
||||
|
||||
[package]
|
||||
name = "solana-bpf-rust-instruction-introspection"
|
||||
version = "1.4.0"
|
||||
description = "Solana BPF test program written in Rust"
|
||||
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
solana-sdk = { path = "../../../../sdk/", version = "1.4.0", default-features = false }
|
||||
|
||||
[features]
|
||||
program = ["solana-sdk/program"]
|
||||
default = ["program", "solana-sdk/default"]
|
||||
|
||||
[lib]
|
||||
name = "solana_bpf_rust_instruction_introspection"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
2
programs/bpf/rust/instruction_introspection/Xargo.toml
Normal file
2
programs/bpf/rust/instruction_introspection/Xargo.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
45
programs/bpf/rust/instruction_introspection/src/lib.rs
Normal file
45
programs/bpf/rust/instruction_introspection/src/lib.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! @brief Example Rust-based BPF program that exercises instruction introspection
|
||||
|
||||
extern crate solana_sdk;
|
||||
use solana_sdk::{
|
||||
account_info::next_account_info, account_info::AccountInfo, entrypoint,
|
||||
entrypoint::ProgramResult, info, program_error::ProgramError, pubkey::Pubkey,
|
||||
sysvar::instructions,
|
||||
};
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
instruction_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
if instruction_data.is_empty() {
|
||||
return Err(ProgramError::InvalidAccountData);
|
||||
}
|
||||
|
||||
let secp_instruction_index = instruction_data[0];
|
||||
let account_info_iter = &mut accounts.iter();
|
||||
let instruction_accounts = next_account_info(account_info_iter)?;
|
||||
assert_eq!(*instruction_accounts.key, instructions::id());
|
||||
let data_len = instruction_accounts.try_borrow_data()?.len();
|
||||
if data_len < 2 {
|
||||
return Err(ProgramError::InvalidAccountData);
|
||||
}
|
||||
|
||||
let instruction = instructions::get_instruction(
|
||||
secp_instruction_index as usize,
|
||||
&instruction_accounts.try_borrow_data()?,
|
||||
)
|
||||
.map_err(|_| ProgramError::InvalidAccountData)?;
|
||||
|
||||
let current_instruction =
|
||||
instructions::get_current_instruction(&instruction_accounts.try_borrow_data()?);
|
||||
let my_index = instruction_data[1] as u16;
|
||||
assert_eq!(current_instruction, my_index);
|
||||
|
||||
info!(&format!("id: {}", instruction.program_id));
|
||||
|
||||
info!(&format!("data[0]: {}", instruction.data[0]));
|
||||
info!(&format!("index: {}", current_instruction));
|
||||
Ok(())
|
||||
}
|
@@ -576,7 +576,7 @@ fn assert_instruction_count() {
|
||||
("solana_bpf_rust_128bit", 543),
|
||||
("solana_bpf_rust_alloc", 19082),
|
||||
("solana_bpf_rust_dep_crate", 2),
|
||||
("solana_bpf_rust_external_spend", 473),
|
||||
("solana_bpf_rust_external_spend", 485),
|
||||
("solana_bpf_rust_iter", 723),
|
||||
("solana_bpf_rust_many_args", 231),
|
||||
("solana_bpf_rust_noop", 2217),
|
||||
@@ -669,3 +669,71 @@ impl InstructionMeter for TestInstructionMeter {
|
||||
u64::MAX
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "bpf_rust"))]
|
||||
#[test]
|
||||
fn test_program_bpf_instruction_introspection() {
|
||||
solana_logger::setup();
|
||||
|
||||
let GenesisConfigInfo {
|
||||
genesis_config,
|
||||
mint_keypair,
|
||||
..
|
||||
} = create_genesis_config(50_000);
|
||||
let mut bank = Bank::new(&genesis_config);
|
||||
|
||||
let (name, id, entrypoint) = solana_bpf_loader_program!();
|
||||
bank.add_builtin_loader(&name, id, entrypoint);
|
||||
let bank = Arc::new(bank);
|
||||
let bank_client = BankClient::new_shared(&bank);
|
||||
|
||||
let program_id = load_bpf_program(
|
||||
&bank_client,
|
||||
&bpf_loader::id(),
|
||||
&mint_keypair,
|
||||
"solana_bpf_rust_instruction_introspection",
|
||||
);
|
||||
|
||||
// Passing transaction
|
||||
let account_metas = vec![AccountMeta::new_readonly(
|
||||
solana_sdk::sysvar::instructions::id(),
|
||||
false,
|
||||
)];
|
||||
let instruction0 = Instruction::new(program_id, &[0u8, 0u8], account_metas.clone());
|
||||
let instruction1 = Instruction::new(program_id, &[0u8, 1u8], account_metas.clone());
|
||||
let instruction2 = Instruction::new(program_id, &[0u8, 2u8], account_metas);
|
||||
let message = Message::new(
|
||||
&[instruction0, instruction1, instruction2],
|
||||
Some(&mint_keypair.pubkey()),
|
||||
);
|
||||
let result = bank_client.send_and_confirm_message(&[&mint_keypair], message);
|
||||
println!("result: {:?}", result);
|
||||
assert!(result.is_ok());
|
||||
|
||||
// writable special instructions11111 key, should not be allowed
|
||||
let account_metas = vec![AccountMeta::new(
|
||||
solana_sdk::sysvar::instructions::id(),
|
||||
false,
|
||||
)];
|
||||
let instruction = Instruction::new(program_id, &0u8, account_metas);
|
||||
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
|
||||
assert_eq!(
|
||||
result.unwrap_err().unwrap(),
|
||||
TransactionError::InvalidAccountIndex
|
||||
);
|
||||
|
||||
// No accounts, should error
|
||||
let instruction = Instruction::new(program_id, &0u8, vec![]);
|
||||
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.unwrap_err().unwrap(),
|
||||
TransactionError::InstructionError(
|
||||
0,
|
||||
solana_sdk::instruction::InstructionError::NotEnoughAccountKeys
|
||||
)
|
||||
);
|
||||
assert!(bank
|
||||
.get_account(&solana_sdk::sysvar::instructions::id())
|
||||
.is_none());
|
||||
}
|
||||
|
Reference in New Issue
Block a user