Skip to content

Commit

Permalink
feat: make loader generic over hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Oct 20, 2024
1 parent 38fe2a7 commit 2162342
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions crates/svm-test/src/svm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasherDefault, DefaultHasher};

use litesvm::types::SimulatedTransactionInfo;
pub use litesvm::types::{FailedTransactionMetadata, TransactionMetadata, TransactionResult};
Expand All @@ -16,10 +17,12 @@ use solana_sdk::{
use crate::spl::SplProgram;
use crate::AccountLoader;

pub type DefaultLoader = HashMap<Pubkey, Account, BuildHasherDefault<DefaultHasher>>;

const PRE_LOADED: &[Pubkey] =
&[ed25519_program::ID, secp256k1_program::ID, sysvar::instructions::ID];

pub struct Svm<L> {
pub struct Svm<L = DefaultLoader> {
inner: litesvm::LiteSVM,
pub loader: L,
reserved_account_keys: ReservedAccountKeys,
Expand Down
5 changes: 4 additions & 1 deletion crates/svm-test/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub trait AccountLoader {
}
}

impl AccountLoader for std::collections::HashMap<Pubkey, Account> {
impl<S> AccountLoader for std::collections::HashMap<Pubkey, Account, S>
where
S: std::hash::BuildHasher,
{
fn load(&self, key: &Pubkey) -> Account {
self.get(key).cloned().unwrap_or_default()
}
Expand Down
5 changes: 2 additions & 3 deletions crates/svm-test/tests/program_unit_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! These examples do not make use of `Harness` and only include local accounts
//! & programs.
use std::collections::HashMap;

use expect_test::expect;
use litesvm::types::SimulatedTransactionInfo;
use solana_sdk::account::Account;
use solana_sdk::instruction::Instruction;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction::Transaction;
use svm_test::svm::DefaultLoader;
use svm_test::utils::{test_payer_keypair, TEST_PAYER};
use svm_test::Svm;

Expand All @@ -16,7 +15,7 @@ const MEMO_ID: Pubkey = Pubkey::new_from_array([1; 32]);
#[test]
fn memo() {
// Load our program & give our test payer some funds.
let mut svm = Svm::new(HashMap::default());
let mut svm: Svm<DefaultLoader> = Svm::default();
svm.load_program(MEMO_ID, "memo");
svm.set(TEST_PAYER, Account { lamports: 10u64.pow(9), ..Default::default() });

Expand Down

0 comments on commit 2162342

Please sign in to comment.