Skip to content

Commit

Permalink
SVM: move program cache construction inside SVM (anza-xyz#1565)
Browse files Browse the repository at this point in the history
* SVM: move program cache construction inside SVM

* change slot and epoch for program cache in SVM tests

* move creation of ProgramCache within SVM bounds
  • Loading branch information
pgarg66 authored Jun 3, 2024
1 parent 8a1b2dc commit 6f0afae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
7 changes: 1 addition & 6 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use {
solana_program_runtime::{
invoke_context::BuiltinFunctionWithContext,
loaded_programs::{
ProgramCache, ProgramCacheEntry, ProgramCacheEntryOwner, ProgramCacheEntryType,
ProgramCacheEntry, ProgramCacheEntryOwner, ProgramCacheEntryType,
ProgramCacheMatchCriteria,
},
timings::{ExecuteTimingType, ExecuteTimings},
Expand Down Expand Up @@ -996,10 +996,6 @@ impl Bank {
bank.epoch,
bank.epoch_schedule.clone(),
Arc::new(RuntimeConfig::default()),
Arc::new(RwLock::new(ProgramCache::new(
Slot::default(),
Epoch::default(),
))),
HashSet::default(),
);

Expand Down Expand Up @@ -1630,7 +1626,6 @@ impl Bank {
bank.epoch,
bank.epoch_schedule.clone(),
runtime_config,
Arc::new(RwLock::new(ProgramCache::new(fields.slot, fields.epoch))),
HashSet::default(),
);

Expand Down
11 changes: 3 additions & 8 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
epoch: Epoch,
epoch_schedule: EpochSchedule,
runtime_config: Arc<RuntimeConfig>,
program_cache: Arc<RwLock<ProgramCache<FG>>>,
builtin_program_ids: HashSet<Pubkey>,
) -> Self {
Self {
Expand All @@ -174,7 +173,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
fee_structure: FeeStructure::default(),
runtime_config,
sysvar_cache: RwLock::<SysvarCache>::default(),
program_cache,
program_cache: Arc::new(RwLock::new(ProgramCache::new(slot, epoch))),
builtin_program_ids: RwLock::new(builtin_program_ids),
}
}
Expand Down Expand Up @@ -1711,7 +1710,6 @@ mod tests {
5,
EpochSchedule::default(),
Arc::new(RuntimeConfig::default()),
Arc::new(RwLock::new(ProgramCache::new(0, 0))),
HashSet::new(),
);
batch_processor.program_cache.write().unwrap().fork_graph =
Expand All @@ -1733,13 +1731,10 @@ mod tests {
let ths: Vec<_> = (0..4)
.map(|_| {
let local_bank = mock_bank.clone();
let processor = TransactionBatchProcessor::new(
let processor = TransactionBatchProcessor::new_from(
&batch_processor,
batch_processor.slot,
batch_processor.epoch,
batch_processor.epoch_schedule.clone(),
batch_processor.runtime_config.clone(),
batch_processor.program_cache.clone(),
HashSet::new(),
);
let maps = account_maps.clone();
let programs = programs.clone();
Expand Down
28 changes: 13 additions & 15 deletions svm/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use {
solana_compute_budget::compute_budget::ComputeBudget,
solana_program_runtime::{
invoke_context::{EnvironmentConfig, InvokeContext},
loaded_programs::{
ProgramCache, ProgramCacheEntry, ProgramCacheForTxBatch, ProgramRuntimeEnvironments,
},
loaded_programs::{ProgramCacheEntry, ProgramCacheForTxBatch, ProgramRuntimeEnvironments},
log_collector::LogCollector,
solana_rbpf::{
program::{BuiltinProgram, FunctionRegistry},
Expand Down Expand Up @@ -244,27 +242,27 @@ fn run_fixture(fixture: InstrFixture, filename: OsString, execute_as_instr: bool
let v1_environment =
create_program_runtime_environment_v1(&feature_set, &compute_budget, false, false).unwrap();

let mut program_cache = ProgramCache::<MockForkGraph>::new(0, 20);
program_cache.environments = ProgramRuntimeEnvironments {
program_runtime_v1: Arc::new(v1_environment),
program_runtime_v2: Arc::new(BuiltinProgram::new_loader(
Config::default(),
FunctionRegistry::default(),
)),
};
program_cache.fork_graph = Some(Arc::new(RwLock::new(MockForkGraph {})));

let program_cache = Arc::new(RwLock::new(program_cache));
mock_bank.override_feature_set(feature_set);
let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new(
42,
2,
EpochSchedule::default(),
Arc::new(RuntimeConfig::default()),
program_cache.clone(),
HashSet::new(),
);

{
let mut program_cache = batch_processor.program_cache.write().unwrap();
program_cache.environments = ProgramRuntimeEnvironments {
program_runtime_v1: Arc::new(v1_environment),
program_runtime_v2: Arc::new(BuiltinProgram::new_loader(
Config::default(),
FunctionRegistry::default(),
)),
};
program_cache.fork_graph = Some(Arc::new(RwLock::new(MockForkGraph {})));
}

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

Expand Down
19 changes: 10 additions & 9 deletions svm/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ fn create_custom_environment<'a>() -> BuiltinProgram<InvokeContext<'a>> {
BuiltinProgram::new_loader(vm_config, function_registry)
}

fn create_executable_environment(mock_bank: &mut MockBankCallback) -> ProgramCache<MockForkGraph> {
let mut program_cache = ProgramCache::<MockForkGraph>::new(0, 20);

fn create_executable_environment(
mock_bank: &mut 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
Expand Down Expand Up @@ -165,8 +166,6 @@ fn create_executable_environment(mock_bank: &mut MockBankCallback) -> ProgramCac
.account_shared_data
.borrow_mut()
.insert(Clock::id(), account_data);

program_cache
}

fn load_program(name: String) -> Vec<u8> {
Expand Down Expand Up @@ -445,15 +444,17 @@ fn prepare_transactions(
fn svm_integration() {
let mut mock_bank = MockBankCallback::default();
let (transactions, mut check_results) = prepare_transactions(&mut mock_bank);
let program_cache = create_executable_environment(&mut mock_bank);
let program_cache = Arc::new(RwLock::new(program_cache));
let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new(
EXECUTION_SLOT,
EXECUTION_EPOCH,
EpochSchedule::default(),
Arc::new(RuntimeConfig::default()),
program_cache.clone(),
HashSet::default(),
HashSet::new(),
);

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

// The sysvars must be put in the cache
Expand Down

0 comments on commit 6f0afae

Please sign in to comment.