Skip to content

Commit

Permalink
introduce CPI target program
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jun 23, 2024
1 parent 155c473 commit 40747e6
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 17 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ jobs:
if: steps.cache-solana-toolchain.outputs.cache-hit != 'true'
with:
version: ${{ env.SOLANA_VERSION }}
- name: Build test program
run: cargo build-sbf --manifest-path test-program/Cargo.toml
- name: Build test programs
run: |
cargo build-sbf --manifest-path test-programs/cpi-target/Cargo.toml
cargo build-sbf --manifest-path test-programs/primary/Cargo.toml
- name: Test
run: cargo test
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
members = [
"bencher",
"harness",
"test-program",
"test-programs/cpi-target",
"test-programs/primary",
]
resolver = "2"

Expand Down
2 changes: 1 addition & 1 deletion bencher/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn test_markdown() {
let instruction = Instruction::new_with_bytes(program_id, &[0], vec![]);
let accounts = vec![];

let mollusk = Mollusk::new(&program_id, "test_program");
let mollusk = Mollusk::new(&program_id, "test_program_primary");

MolluskComputeUnitBencher::new(mollusk)
.bench((String::from("bench0"), instruction.clone(), &accounts))
Expand Down
22 changes: 11 additions & 11 deletions harness/tests/bpf_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_write_data() {

let program_id = Pubkey::new_unique();

let mollusk = Mollusk::new(&program_id, "test_program");
let mollusk = Mollusk::new(&program_id, "test_program_primary");

let data = &[1, 2, 3, 4, 5];
let space = data.len();
Expand Down Expand Up @@ -46,7 +46,7 @@ fn test_write_data() {
&[(key, account.clone())],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(272),
Check::compute_units(279),
],
);
}
Expand All @@ -61,7 +61,7 @@ fn test_write_data() {
&[(key, account.clone())],
&[
Check::err(ProgramError::AccountDataTooSmall),
Check::compute_units(281),
Check::compute_units(290),
],
);
}
Expand All @@ -72,7 +72,7 @@ fn test_write_data() {
&[(key, account.clone())],
&[
Check::success(),
Check::compute_units(350),
Check::compute_units(358),
Check::account(&key)
.data(data)
.lamports(lamports)
Expand All @@ -88,7 +88,7 @@ fn test_transfer() {

let program_id = Pubkey::new_unique();

let mollusk = Mollusk::new(&program_id, "test_program");
let mollusk = Mollusk::new(&program_id, "test_program_primary");

let payer = Pubkey::new_unique();
let payer_lamports = 100_000_000;
Expand Down Expand Up @@ -128,7 +128,7 @@ fn test_transfer() {
],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(598),
Check::compute_units(605),
],
);
}
Expand All @@ -146,7 +146,7 @@ fn test_transfer() {
Check::err(ProgramError::Custom(
SystemError::ResultWithNegativeLamports as u32,
)),
Check::compute_units(2256),
Check::compute_units(2261),
],
);
}
Expand All @@ -161,7 +161,7 @@ fn test_transfer() {
],
&[
Check::success(),
Check::compute_units(2366),
Check::compute_units(2371),
Check::account(&payer)
.lamports(payer_lamports - transfer_amount)
.build(),
Expand All @@ -178,7 +178,7 @@ fn test_close_account() {

let program_id = Pubkey::new_unique();

let mollusk = Mollusk::new(&program_id, "test_program");
let mollusk = Mollusk::new(&program_id, "test_program_primary");

let key = Pubkey::new_unique();
let account = AccountSharedData::new(50_000_000, 50, &program_id);
Expand Down Expand Up @@ -207,7 +207,7 @@ fn test_close_account() {
],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(598),
Check::compute_units(605),
],
);
}
Expand All @@ -222,7 +222,7 @@ fn test_close_account() {
],
&[
Check::success(),
Check::compute_units(2558),
Check::compute_units(2563),
Check::account(&key)
.data(&[])
.lamports(0)
Expand Down
10 changes: 10 additions & 0 deletions test-programs/cpi-target/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "test-program-cpi-target"
version = "0.1.0"
edition = "2021"

[dependencies]
solana-program = { workspace = true }

[lib]
crate-type = ["cdylib", "lib"]
37 changes: 37 additions & 0 deletions test-programs/cpi-target/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
program_error::ProgramError,
pubkey::Pubkey,
};

solana_program::declare_id!("MD24T7azhc2q9ZXaeskbLpmVA41k7StzTGgcfvGcpHj");

solana_program::entrypoint!(process_instruction);

fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
// Simply write the input data to the first account.
let accounts_iter = &mut accounts.iter();

let account_info = next_account_info(accounts_iter)?;

if !account_info.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}

if account_info.owner != program_id {
return Err(ProgramError::IncorrectProgramId);
}

if input.len() > account_info.data_len() {
return Err(ProgramError::AccountDataTooSmall);
}

account_info.try_borrow_mut_data()?[..].copy_from_slice(input);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "test-program"
name = "test-program-primary"
version = "0.1.0"
edition = "2021"

Expand Down
20 changes: 19 additions & 1 deletion test-program/src/lib.rs → test-programs/primary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
incinerator,
instruction::{AccountMeta, Instruction},
program::invoke,
program_error::ProgramError,
pubkey::Pubkey,
pubkey::{Pubkey, PUBKEY_BYTES},
system_instruction, system_program,
};

Expand Down Expand Up @@ -74,6 +75,23 @@ fn process_instruction(
&[account_info.clone(), incinerator_info.clone()],
)?;
}
Some((4, rest)) if rest.len() >= PUBKEY_BYTES => {
// Invoke the "CPI Target" test program, which will write the rest
// of the input data to the first account (after the provided
// program ID).
let account_info = next_account_info(accounts_iter)?;

let (program_id_bytes, data) = rest.split_at(PUBKEY_BYTES);

let program_id = Pubkey::new_from_array(program_id_bytes.try_into().unwrap());
let instruction = Instruction::new_with_bytes(
program_id,
data,
vec![AccountMeta::new(*account_info.key, true)],
);

invoke(&instruction, &[account_info.clone()])?;
}
_ => return Err(ProgramError::InvalidInstructionData),
}

Expand Down

0 comments on commit 40747e6

Please sign in to comment.