Skip to content

Commit

Permalink
add integration tests from old branch
Browse files Browse the repository at this point in the history
  • Loading branch information
2501babe committed Oct 11, 2024
1 parent 5456754 commit beb17b3
Show file tree
Hide file tree
Showing 5 changed files with 1,346 additions and 85 deletions.
12 changes: 12 additions & 0 deletions svm/tests/example-programs/write-to-account/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "write-to-account"
version = "2.1.0"
edition = "2021"

[dependencies]
solana-program = { path = "../../../../sdk/program", version = "=2.1.0" }

[lib]
crate-type = ["cdylib", "rlib"]

[workspace]
62 changes: 62 additions & 0 deletions svm/tests/example-programs/write-to-account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
incinerator, msg,
program_error::ProgramError,
pubkey::Pubkey,
};

entrypoint!(process_instruction);

fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
data: &[u8],
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();
let target_account_info = next_account_info(accounts_iter)?;
match data[0] {
// print account size
0 => {
msg!(
"account size {}",
target_account_info.try_borrow_data()?.len()
);
}
// set account data
1 => {
let mut account_data = target_account_info.try_borrow_mut_data()?;
account_data[0] = 100;
}
// deallocate account
2 => {
let incinerator_info = next_account_info(accounts_iter)?;
if !incinerator::check_id(incinerator_info.key) {
return Err(ProgramError::InvalidAccountData);
}

let mut target_lamports = target_account_info.try_borrow_mut_lamports()?;
let mut incinerator_lamports = incinerator_info.try_borrow_mut_lamports()?;

**incinerator_lamports = incinerator_lamports
.checked_add(**target_lamports)
.ok_or(ProgramError::ArithmeticOverflow)?;

**target_lamports = target_lamports
.checked_sub(**target_lamports)
.ok_or(ProgramError::InsufficientFunds)?;
}
// reallocate account
3 => {
let new_size = usize::from_le_bytes(data[1..9].try_into().unwrap());
target_account_info.realloc(new_size, false)?;
}
// bad ixn
_ => {
return Err(ProgramError::InvalidArgument);
}
}

Ok(())
}
Binary file not shown.
Loading

0 comments on commit beb17b3

Please sign in to comment.