Skip to content

Commit

Permalink
Bpf cpi (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Jun 23, 2024
1 parent 155c473 commit 06ed0b4
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 39 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
16 changes: 15 additions & 1 deletion harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Mollusk {

let mut mollusk = Self {
program_id: *program_id,
program_account: program::program_account(program_id),
program_account: program::create_program_account(program_id),
..Default::default()
};

Expand All @@ -115,6 +115,20 @@ impl Mollusk {
mollusk
}

/// Add a program to the test environment.
///
/// If you intend to CPI to a program, this is likely what you want to use.
pub fn add_program(&mut self, program_id: &Pubkey, program_name: &'static str) {
let elf = file::load_program_elf(program_name);
program::add_program_to_cache(
&mut self.program_cache,
program_id,
&elf,
&self.compute_budget,
&self.feature_set,
);
}

/// Get the current rent.
pub fn get_rent(&self) -> Arc<Rent> {
self.sysvar_cache.get_rent().unwrap_or_default()
Expand Down
19 changes: 0 additions & 19 deletions harness/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,6 @@ pub fn create_program_accounts(
)
}

pub fn program_account(program_id: &Pubkey) -> AccountSharedData {
let data = bincode::serialize(&UpgradeableLoaderState::Program {
programdata_address: Pubkey::find_program_address(
&[program_id.as_ref()],
&bpf_loader_upgradeable::id(),
)
.0,
})
.unwrap();
let lamports = Rent::default().minimum_balance(data.len());
AccountSharedData::from(Account {
lamports,
data,
owner: bpf_loader_upgradeable::id(),
executable: true,
rent_epoch: 0,
})
}

/// Create a default program cache instance.
pub fn default_program_cache() -> LoadedProgramsForTxBatch {
let mut cache = LoadedProgramsForTxBatch::default();
Expand Down
161 changes: 148 additions & 13 deletions harness/tests/bpf_program.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use {
mollusk::{program::system_program_account, result::Check, Mollusk},
mollusk::{
program::{create_program_account, system_program_account},
result::Check,
Mollusk,
},
solana_sdk::{
account::AccountSharedData,
incinerator,
instruction::{AccountMeta, Instruction},
instruction::{AccountMeta, Instruction, InstructionError},
program_error::ProgramError,
pubkey::Pubkey,
system_instruction::SystemError,
Expand All @@ -17,7 +21,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 +50,7 @@ fn test_write_data() {
&[(key, account.clone())],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(272),
Check::compute_units(279),
],
);
}
Expand All @@ -61,7 +65,7 @@ fn test_write_data() {
&[(key, account.clone())],
&[
Check::err(ProgramError::AccountDataTooSmall),
Check::compute_units(281),
Check::compute_units(290),
],
);
}
Expand All @@ -72,7 +76,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 +92,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 +132,7 @@ fn test_transfer() {
],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(598),
Check::compute_units(605),
],
);
}
Expand All @@ -146,7 +150,7 @@ fn test_transfer() {
Check::err(ProgramError::Custom(
SystemError::ResultWithNegativeLamports as u32,
)),
Check::compute_units(2256),
Check::compute_units(2261),
],
);
}
Expand All @@ -161,7 +165,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 +182,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 +211,7 @@ fn test_close_account() {
],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(598),
Check::compute_units(605),
],
);
}
Expand All @@ -222,7 +226,7 @@ fn test_close_account() {
],
&[
Check::success(),
Check::compute_units(2558),
Check::compute_units(2563),
Check::account(&key)
.data(&[])
.lamports(0)
Expand All @@ -232,3 +236,134 @@ fn test_close_account() {
],
);
}

#[test]
fn test_cpi() {
std::env::set_var("SBF_OUT_DIR", "../target/deploy");

let program_id = Pubkey::new_unique();
let cpi_target_program_id = Pubkey::new_unique();

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

let data = &[1, 2, 3, 4, 5];
let space = data.len();
let lamports = mollusk.get_rent().minimum_balance(space);

let key = Pubkey::new_unique();
let account = AccountSharedData::new(lamports, space, &cpi_target_program_id);

let instruction = {
let mut instruction_data = vec![4];
instruction_data.extend_from_slice(cpi_target_program_id.as_ref());
instruction_data.extend_from_slice(data);
Instruction::new_with_bytes(
program_id,
&instruction_data,
vec![
AccountMeta::new(key, true),
AccountMeta::new_readonly(cpi_target_program_id, false),
],
)
};

// Fail CPI target program account not provided.
{
mollusk.process_and_validate_instruction(
&instruction,
&[(key, account.clone())],
&[
Check::err(ProgramError::NotEnoughAccountKeys),
Check::compute_units(0),
],
);
}

// Fail CPI target program not added to test environment.
{
mollusk.process_and_validate_instruction(
&instruction,
&[
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
),
],
&[
// This is the error thrown by SVM. It also emits the message
// "Program is not cached".
Check::err(ProgramError::InvalidAccountData),
Check::compute_units(1840),
],
);
}

mollusk.add_program(&cpi_target_program_id, "test_program_cpi_target");

// Fail account not signer.
{
let mut account_not_signer_ix = instruction.clone();
account_not_signer_ix.accounts[0].is_signer = false;

mollusk.process_and_validate_instruction(
&account_not_signer_ix,
&[
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
),
],
&[
Check::instruction_err(InstructionError::PrivilegeEscalation), // CPI
Check::compute_units(1841),
],
);
}

// Fail data too large.
{
let mut data_too_large_ix = instruction.clone();
let mut too_large_data = vec![4];
too_large_data.extend_from_slice(cpi_target_program_id.as_ref());
too_large_data.extend_from_slice(&vec![1; space + 2]);
data_too_large_ix.data = too_large_data;

mollusk.process_and_validate_instruction(
&data_too_large_ix,
&[
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
),
],
&[
Check::err(ProgramError::AccountDataTooSmall),
Check::compute_units(2162),
],
);
}

// Success.
mollusk.process_and_validate_instruction(
&instruction,
&[
(key, account.clone()),
(
cpi_target_program_id,
create_program_account(&cpi_target_program_id),
),
],
&[
Check::success(),
Check::compute_units(2279),
Check::account(&key)
.data(data)
.lamports(lamports)
.owner(cpi_target_program_id)
.build(),
],
);
}
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
Loading

0 comments on commit 06ed0b4

Please sign in to comment.