Skip to content

Commit

Permalink
add add_program_from_file (#53)
Browse files Browse the repository at this point in the history
* add add_program_from_file

* update tests to use add_program_from_file
  • Loading branch information
kevinheavey authored Apr 2, 2024
1 parent eec2c13 commit 8e8c7e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
11 changes: 3 additions & 8 deletions benches/max_perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ use solana_sdk::{
transaction::Transaction,
};

fn read_counter_program() -> Vec<u8> {
let mut so_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
so_path.push("test_programs/target/deploy/counter.so");
std::fs::read(so_path).unwrap()
}

const NUM_GREETINGS: u8 = 255;

fn make_tx(
Expand Down Expand Up @@ -45,8 +39,9 @@ fn criterion_benchmark(c: &mut Criterion) {
let payer_kp = Keypair::new();
let payer_pk = payer_kp.pubkey();
let program_id = Pubkey::new_unique();

svm.add_program(program_id, &read_counter_program());
let mut so_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
so_path.push("test_programs/target/deploy/counter.so");
svm.add_program_from_file(program_id, &so_path).unwrap();
svm.airdrop(&payer_pk, 1000000000).unwrap();
let counter_address = Pubkey::new_unique();
let latest_blockhash = svm.latest_blockhash();
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use solana_sdk::{
transaction_context::{ExecutionRecord, IndexOfAccount, TransactionContext},
};
use solana_system_program::{get_system_account_kind, SystemAccountKind};
use std::{cell::RefCell, rc::Rc, sync::Arc};
use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
use utils::construct_instructions_account;

use crate::{
Expand Down Expand Up @@ -274,6 +274,16 @@ impl LiteSVM {
.unwrap();
}

pub fn add_program_from_file(
&mut self,
program_id: Pubkey,
path: impl AsRef<Path>,
) -> Result<(), std::io::Error> {
let bytes = std::fs::read(path)?;
self.add_program(program_id, &bytes);
Ok(())
}

pub fn add_program(&mut self, program_id: Pubkey, program_bytes: &[u8]) {
let program_len = program_bytes.len();
let lamports = self.minimum_balance_for_rent_exemption(program_len);
Expand Down
10 changes: 3 additions & 7 deletions tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@ fn test_insufficient_funds_for_rent() {
assert!(svm.get_transaction(&signature).is_none());
}

fn read_failure_program() -> Vec<u8> {
let mut so_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
so_path.push("test_programs/target/deploy/failure.so");
std::fs::read(so_path).unwrap()
}

#[test_log::test]
fn test_fees_failed_transaction() {
let from_keypair = Keypair::new();
let from = from_keypair.pubkey();

let mut svm = LiteSVM::new();
let program_id = pubkey!("HvrRMSshMx3itvsyWDnWg2E3cy5h57iMaR7oVxSZJDSA");
svm.add_program(program_id, &read_failure_program());
let mut so_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
so_path.push("test_programs/target/deploy/failure.so");
svm.add_program_from_file(program_id, &so_path).unwrap();
let initial_balance = 1_000_000_000;
svm.airdrop(&from, initial_balance).unwrap();
let instruction = Instruction {
Expand Down

0 comments on commit 8e8c7e9

Please sign in to comment.