Skip to content

Commit

Permalink
SVM: simplify mock bank (anza-xyz#3536)
Browse files Browse the repository at this point in the history
* SVM: mock bank: rename `create_custom_environment` to `create_custom_loader`

* SVM: mock bank: shift sysvar setup to new method

* SVM: mock bank: drop `create_executable_environment`
  • Loading branch information
buffalojoec authored Nov 13, 2024
1 parent ef5c7a3 commit 3fce80f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 94 deletions.
20 changes: 9 additions & 11 deletions svm/tests/concurrent_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use {
crate::{
mock_bank::{
create_executable_environment, deploy_program, register_builtins, MockForkGraph,
},
mock_bank::{create_custom_loader, deploy_program, register_builtins, MockForkGraph},
transaction_builder::SanitizedTransactionBuilder,
},
assert_matches::assert_matches,
Expand Down Expand Up @@ -128,16 +126,16 @@ fn test_program_cache_with_exhaustive_scheduler() {
// correctly.
fn svm_concurrent() {
let mock_bank = Arc::new(MockBankCallback::default());
let batch_processor =
Arc::new(TransactionBatchProcessor::<MockForkGraph>::new_uninitialized(5, 2));
let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
let batch_processor = Arc::new(TransactionBatchProcessor::new(
5,
2,
Arc::downgrade(&fork_graph),
Some(Arc::new(create_custom_loader())),
None, // We are not using program runtime v2.
));

create_executable_environment(
fork_graph.clone(),
&mock_bank,
&mut batch_processor.program_cache.write().unwrap(),
);

mock_bank.configure_sysvars();
batch_processor.fill_missing_sysvar_cache_entries(&*mock_bank);
register_builtins(&mock_bank, &batch_processor);

Expand Down
17 changes: 7 additions & 10 deletions svm/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use {
crate::mock_bank::{
create_executable_environment, deploy_program_with_upgrade_authority, program_address,
create_custom_loader, deploy_program_with_upgrade_authority, program_address,
register_builtins, MockBankCallback, MockForkGraph, EXECUTION_EPOCH, EXECUTION_SLOT,
WALLCLOCK_TIME,
},
Expand Down Expand Up @@ -78,20 +78,17 @@ impl SvmTestEnvironment<'_> {
.insert(*pubkey, account.clone());
}

let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new_uninitialized(
let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
let batch_processor = TransactionBatchProcessor::new(
EXECUTION_SLOT,
EXECUTION_EPOCH,
);

let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));

create_executable_environment(
fork_graph.clone(),
&mock_bank,
&mut batch_processor.program_cache.write().unwrap(),
Arc::downgrade(&fork_graph),
Some(Arc::new(create_custom_loader())),
None, // We are not using program runtime v2.
);

// The sysvars must be put in the cache
mock_bank.configure_sysvars();
batch_processor.fill_missing_sysvar_cache_entries(&mock_bank);
register_builtins(&mock_bank, &batch_processor);

Expand Down
119 changes: 46 additions & 73 deletions svm/tests/mock_bank.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused)]
#[allow(deprecated)]
use solana_sdk::sysvar::recent_blockhashes::{Entry as BlockhashesEntry, RecentBlockhashes};
use {
Expand All @@ -9,9 +10,7 @@ use {
solana_feature_set::FeatureSet,
solana_program_runtime::{
invoke_context::InvokeContext,
loaded_programs::{
BlockRelation, ForkGraph, ProgramCache, ProgramCacheEntry, ProgramRuntimeEnvironments,
},
loaded_programs::{BlockRelation, ForkGraph, ProgramCacheEntry},
solana_rbpf::{
program::{BuiltinFunction, BuiltinProgram, FunctionRegistry},
vm::Config,
Expand Down Expand Up @@ -115,9 +114,51 @@ impl MockBankCallback {
pub fn override_feature_set(&mut self, new_set: FeatureSet) {
self.feature_set = Arc::new(new_set)
}

pub fn configure_sysvars(&self) {
// We must fill in the sysvar cache entries

// clock contents are important because we use them for a sysvar loading test
let clock = Clock {
slot: EXECUTION_SLOT,
epoch_start_timestamp: WALLCLOCK_TIME.saturating_sub(10) as UnixTimestamp,
epoch: EXECUTION_EPOCH,
leader_schedule_epoch: EXECUTION_EPOCH,
unix_timestamp: WALLCLOCK_TIME as UnixTimestamp,
};

let mut account_data = AccountSharedData::default();
account_data.set_data(bincode::serialize(&clock).unwrap());
self.account_shared_data
.write()
.unwrap()
.insert(Clock::id(), account_data);

// default rent is fine
let rent = Rent::default();

let mut account_data = AccountSharedData::default();
account_data.set_data(bincode::serialize(&rent).unwrap());
self.account_shared_data
.write()
.unwrap()
.insert(Rent::id(), account_data);

// SystemInstruction::AdvanceNonceAccount asserts RecentBlockhashes is non-empty
// but then just gets the blockhash from InvokeContext. so the sysvar doesnt need real entries
#[allow(deprecated)]
let recent_blockhashes = vec![BlockhashesEntry::default()];

let mut account_data = AccountSharedData::default();
account_data.set_data(bincode::serialize(&recent_blockhashes).unwrap());
#[allow(deprecated)]
self.account_shared_data
.write()
.unwrap()
.insert(RecentBlockhashes::id(), account_data);
}
}

#[allow(unused)]
fn load_program(name: String) -> Vec<u8> {
// Loading the program file
let mut dir = env::current_dir().unwrap();
Expand All @@ -133,22 +174,18 @@ fn load_program(name: String) -> Vec<u8> {
buffer
}

#[allow(unused)]
pub fn program_address(program_name: &str) -> Pubkey {
Pubkey::create_with_seed(&Pubkey::default(), program_name, &Pubkey::default()).unwrap()
}

#[allow(unused)]
pub fn program_data_size(program_name: &str) -> usize {
load_program(program_name.to_string()).len()
}

#[allow(unused)]
pub fn deploy_program(name: String, deployment_slot: Slot, mock_bank: &MockBankCallback) -> Pubkey {
deploy_program_with_upgrade_authority(name, deployment_slot, mock_bank, None)
}

#[allow(unused)]
pub fn deploy_program_with_upgrade_authority(
name: String,
deployment_slot: Slot,
Expand Down Expand Up @@ -204,69 +241,6 @@ pub fn deploy_program_with_upgrade_authority(
program_account
}

#[allow(unused)]
pub fn create_executable_environment(
fork_graph: Arc<RwLock<MockForkGraph>>,
mock_bank: &MockBankCallback,
program_cache: &mut ProgramCache<MockForkGraph>,
) {
program_cache.environments = ProgramRuntimeEnvironments {
program_runtime_v1: Arc::new(create_custom_environment()),
// We are not using program runtime v2
program_runtime_v2: Arc::new(BuiltinProgram::new_loader(
Config::default(),
FunctionRegistry::default(),
)),
};

program_cache.fork_graph = Some(Arc::downgrade(&fork_graph));

// We must fill in the sysvar cache entries

// clock contents are important because we use them for a sysvar loading test
let clock = Clock {
slot: EXECUTION_SLOT,
epoch_start_timestamp: WALLCLOCK_TIME.saturating_sub(10) as UnixTimestamp,
epoch: EXECUTION_EPOCH,
leader_schedule_epoch: EXECUTION_EPOCH,
unix_timestamp: WALLCLOCK_TIME as UnixTimestamp,
};

let mut account_data = AccountSharedData::default();
account_data.set_data(bincode::serialize(&clock).unwrap());
mock_bank
.account_shared_data
.write()
.unwrap()
.insert(Clock::id(), account_data);

// default rent is fine
let rent = Rent::default();

let mut account_data = AccountSharedData::default();
account_data.set_data(bincode::serialize(&rent).unwrap());
mock_bank
.account_shared_data
.write()
.unwrap()
.insert(Rent::id(), account_data);

// SystemInstruction::AdvanceNonceAccount asserts RecentBlockhashes is non-empty
// but then just gets the blockhash from InvokeContext. so the sysvar doesnt need real entries
#[allow(deprecated)]
let recent_blockhashes = vec![BlockhashesEntry::default()];

let mut account_data = AccountSharedData::default();
account_data.set_data(bincode::serialize(&recent_blockhashes).unwrap());
#[allow(deprecated)]
mock_bank
.account_shared_data
.write()
.unwrap()
.insert(RecentBlockhashes::id(), account_data);
}

#[allow(unused)]
pub fn register_builtins(
mock_bank: &MockBankCallback,
batch_processor: &TransactionBatchProcessor<MockForkGraph>,
Expand Down Expand Up @@ -314,8 +288,7 @@ pub fn register_builtins(
);
}

#[allow(unused)]
fn create_custom_environment<'a>() -> BuiltinProgram<InvokeContext<'a>> {
pub fn create_custom_loader<'a>() -> BuiltinProgram<InvokeContext<'a>> {
let compute_budget = ComputeBudget::default();
let vm_config = Config {
max_call_depth: compute_budget.max_call_depth,
Expand Down

0 comments on commit 3fce80f

Please sign in to comment.