Add get_processed_sibling_instruction syscall (#22859)
This commit is contained in:
14
programs/bpf/Cargo.lock
generated
14
programs/bpf/Cargo.lock
generated
@ -3017,6 +3017,20 @@ dependencies = [
|
||||
"solana-program 1.10.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-sibling-instructions"
|
||||
version = "1.10.0"
|
||||
dependencies = [
|
||||
"solana-program 1.10.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-sibling_inner-instructions"
|
||||
version = "1.10.0"
|
||||
dependencies = [
|
||||
"solana-program 1.10.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "solana-bpf-rust-spoof1"
|
||||
version = "1.10.0"
|
||||
|
@ -81,6 +81,8 @@ members = [
|
||||
"rust/sanity",
|
||||
"rust/secp256k1_recover",
|
||||
"rust/sha",
|
||||
"rust/sibling_inner_instruction",
|
||||
"rust/sibling_instruction",
|
||||
"rust/spoof1",
|
||||
"rust/spoof1_system",
|
||||
"rust/sysvar",
|
||||
|
@ -91,6 +91,8 @@ fn main() {
|
||||
"sanity",
|
||||
"secp256k1_recover",
|
||||
"sha",
|
||||
"sibling_inner_instruction",
|
||||
"sibling_instruction",
|
||||
"spoof1",
|
||||
"spoof1_system",
|
||||
"upgradeable",
|
||||
|
23
programs/bpf/rust/sibling_inner_instruction/Cargo.toml
Normal file
23
programs/bpf/rust/sibling_inner_instruction/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "solana-bpf-rust-sibling_inner-instructions"
|
||||
version = "1.10.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/"
|
||||
documentation = "https://docs.rs/solana-bpf-rust-log-data"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
solana-program = { path = "../../../../sdk/program", version = "=1.10.0" }
|
||||
|
||||
[features]
|
||||
default = ["program"]
|
||||
program = []
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib", "cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
69
programs/bpf/rust/sibling_inner_instruction/src/lib.rs
Normal file
69
programs/bpf/rust/sibling_inner_instruction/src/lib.rs
Normal file
@ -0,0 +1,69 @@
|
||||
//! Example Rust-based BPF program that queries sibling instructions
|
||||
|
||||
#![cfg(feature = "program")]
|
||||
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
entrypoint,
|
||||
entrypoint::ProgramResult,
|
||||
instruction::{
|
||||
get_processed_sibling_instruction, get_stack_height, AccountMeta, Instruction,
|
||||
TRANSACTION_LEVEL_STACK_HEIGHT,
|
||||
},
|
||||
msg,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
_instruction_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
msg!("sibling inner");
|
||||
|
||||
// account 0 is mint
|
||||
// account 1 is noop
|
||||
// account 2 is invoke_and_return
|
||||
|
||||
// Check sibling instructions
|
||||
|
||||
let sibling_instruction2 = Instruction::new_with_bytes(
|
||||
*accounts[2].key,
|
||||
&[3],
|
||||
vec![AccountMeta::new_readonly(*accounts[1].key, false)],
|
||||
);
|
||||
let sibling_instruction1 = Instruction::new_with_bytes(
|
||||
*accounts[1].key,
|
||||
&[2],
|
||||
vec![
|
||||
AccountMeta::new_readonly(*accounts[0].key, true),
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
],
|
||||
);
|
||||
let sibling_instruction0 = Instruction::new_with_bytes(
|
||||
*accounts[1].key,
|
||||
&[1],
|
||||
vec![
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
AccountMeta::new_readonly(*accounts[0].key, true),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(TRANSACTION_LEVEL_STACK_HEIGHT + 1, get_stack_height());
|
||||
assert_eq!(
|
||||
get_processed_sibling_instruction(0),
|
||||
Some(sibling_instruction0)
|
||||
);
|
||||
assert_eq!(
|
||||
get_processed_sibling_instruction(1),
|
||||
Some(sibling_instruction1)
|
||||
);
|
||||
assert_eq!(
|
||||
get_processed_sibling_instruction(2),
|
||||
Some(sibling_instruction2)
|
||||
);
|
||||
assert_eq!(get_processed_sibling_instruction(3), None);
|
||||
|
||||
Ok(())
|
||||
}
|
23
programs/bpf/rust/sibling_instruction/Cargo.toml
Normal file
23
programs/bpf/rust/sibling_instruction/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "solana-bpf-rust-sibling-instructions"
|
||||
version = "1.10.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/"
|
||||
documentation = "https://docs.rs/solana-bpf-rust-log-data"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
solana-program = { path = "../../../../sdk/program", version = "=1.10.0" }
|
||||
|
||||
[features]
|
||||
default = ["program"]
|
||||
program = []
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib", "cdylib"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
100
programs/bpf/rust/sibling_instruction/src/lib.rs
Normal file
100
programs/bpf/rust/sibling_instruction/src/lib.rs
Normal file
@ -0,0 +1,100 @@
|
||||
//! Example Rust-based BPF program that queries sibling instructions
|
||||
|
||||
#![cfg(feature = "program")]
|
||||
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
entrypoint,
|
||||
entrypoint::ProgramResult,
|
||||
instruction::{
|
||||
get_processed_sibling_instruction, get_stack_height, AccountMeta, Instruction,
|
||||
TRANSACTION_LEVEL_STACK_HEIGHT,
|
||||
},
|
||||
msg,
|
||||
program::invoke,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
_instruction_data: &[u8],
|
||||
) -> ProgramResult {
|
||||
msg!("sibling");
|
||||
|
||||
// account 0 is mint
|
||||
// account 1 is noop
|
||||
// account 2 is invoke_and_return
|
||||
// account 3 is sibling_inner
|
||||
|
||||
// Invoke child instructions
|
||||
|
||||
let instruction3 = Instruction::new_with_bytes(
|
||||
*accounts[2].key,
|
||||
&[3],
|
||||
vec![AccountMeta::new_readonly(*accounts[1].key, false)],
|
||||
);
|
||||
let instruction2 = Instruction::new_with_bytes(
|
||||
*accounts[1].key,
|
||||
&[2],
|
||||
vec![
|
||||
AccountMeta::new_readonly(*accounts[0].key, true),
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
],
|
||||
);
|
||||
let instruction1 = Instruction::new_with_bytes(
|
||||
*accounts[1].key,
|
||||
&[1],
|
||||
vec![
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
AccountMeta::new_readonly(*accounts[0].key, true),
|
||||
],
|
||||
);
|
||||
let instruction0 = Instruction::new_with_bytes(
|
||||
*accounts[3].key,
|
||||
&[0],
|
||||
vec![
|
||||
AccountMeta::new_readonly(*accounts[0].key, false),
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
AccountMeta::new_readonly(*accounts[2].key, false),
|
||||
AccountMeta::new_readonly(*accounts[3].key, false),
|
||||
],
|
||||
);
|
||||
invoke(&instruction3, accounts)?;
|
||||
invoke(&instruction2, accounts)?;
|
||||
invoke(&instruction1, accounts)?;
|
||||
invoke(&instruction0, accounts)?;
|
||||
|
||||
// Check sibling instructions
|
||||
|
||||
let sibling_instruction1 = Instruction::new_with_bytes(
|
||||
*accounts[1].key,
|
||||
&[43],
|
||||
vec![
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
AccountMeta::new(*accounts[0].key, true),
|
||||
],
|
||||
);
|
||||
let sibling_instruction0 = Instruction::new_with_bytes(
|
||||
*accounts[1].key,
|
||||
&[42],
|
||||
vec![
|
||||
AccountMeta::new(*accounts[0].key, true),
|
||||
AccountMeta::new_readonly(*accounts[1].key, false),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(TRANSACTION_LEVEL_STACK_HEIGHT, get_stack_height());
|
||||
assert_eq!(
|
||||
get_processed_sibling_instruction(0),
|
||||
Some(sibling_instruction0)
|
||||
);
|
||||
assert_eq!(
|
||||
get_processed_sibling_instruction(1),
|
||||
Some(sibling_instruction1)
|
||||
);
|
||||
assert_eq!(get_processed_sibling_instruction(2), None);
|
||||
|
||||
Ok(())
|
||||
}
|
@ -3343,3 +3343,79 @@ fn test_program_bpf_realloc_invoke() {
|
||||
TransactionError::InstructionError(0, InstructionError::InvalidRealloc)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "bpf_rust"))]
|
||||
fn test_program_bpf_processed_inner_instruction() {
|
||||
solana_logger::setup();
|
||||
|
||||
let GenesisConfigInfo {
|
||||
genesis_config,
|
||||
mint_keypair,
|
||||
..
|
||||
} = create_genesis_config(50);
|
||||
let mut bank = Bank::new_for_tests(&genesis_config);
|
||||
let (name, id, entrypoint) = solana_bpf_loader_program!();
|
||||
bank.add_builtin(&name, &id, entrypoint);
|
||||
let bank = Arc::new(bank);
|
||||
let bank_client = BankClient::new_shared(&bank);
|
||||
|
||||
let sibling_program_id = load_bpf_program(
|
||||
&bank_client,
|
||||
&bpf_loader::id(),
|
||||
&mint_keypair,
|
||||
"solana_bpf_rust_sibling_instructions",
|
||||
);
|
||||
let sibling_inner_program_id = load_bpf_program(
|
||||
&bank_client,
|
||||
&bpf_loader::id(),
|
||||
&mint_keypair,
|
||||
"solana_bpf_rust_sibling_inner_instructions",
|
||||
);
|
||||
let noop_program_id = load_bpf_program(
|
||||
&bank_client,
|
||||
&bpf_loader::id(),
|
||||
&mint_keypair,
|
||||
"solana_bpf_rust_noop",
|
||||
);
|
||||
let invoke_and_return_program_id = load_bpf_program(
|
||||
&bank_client,
|
||||
&bpf_loader::id(),
|
||||
&mint_keypair,
|
||||
"solana_bpf_rust_invoke_and_return",
|
||||
);
|
||||
|
||||
let instruction2 = Instruction::new_with_bytes(
|
||||
noop_program_id,
|
||||
&[43],
|
||||
vec![
|
||||
AccountMeta::new_readonly(noop_program_id, false),
|
||||
AccountMeta::new(mint_keypair.pubkey(), true),
|
||||
],
|
||||
);
|
||||
let instruction1 = Instruction::new_with_bytes(
|
||||
noop_program_id,
|
||||
&[42],
|
||||
vec![
|
||||
AccountMeta::new(mint_keypair.pubkey(), true),
|
||||
AccountMeta::new_readonly(noop_program_id, false),
|
||||
],
|
||||
);
|
||||
let instruction0 = Instruction::new_with_bytes(
|
||||
sibling_program_id,
|
||||
&[1, 2, 3, 0, 4, 5, 6],
|
||||
vec![
|
||||
AccountMeta::new(mint_keypair.pubkey(), true),
|
||||
AccountMeta::new_readonly(noop_program_id, false),
|
||||
AccountMeta::new_readonly(invoke_and_return_program_id, false),
|
||||
AccountMeta::new_readonly(sibling_inner_program_id, false),
|
||||
],
|
||||
);
|
||||
let message = Message::new(
|
||||
&[instruction2, instruction1, instruction0],
|
||||
Some(&mint_keypair.pubkey()),
|
||||
);
|
||||
assert!(bank_client
|
||||
.send_and_confirm_message(&[&mint_keypair], message)
|
||||
.is_ok());
|
||||
}
|
||||
|
Reference in New Issue
Block a user