Skip to content

Commit

Permalink
invoke context: unify local program cache instances
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jun 13, 2024
1 parent 1476546 commit 7a2a9af
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 62 deletions.
11 changes: 1 addition & 10 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ pub struct InvokeContext<'a> {
/// the designated compute budget during program execution.
compute_meter: RefCell<u64>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
pub programs_modified_by_tx: &'a mut ProgramCacheForTxBatch,
/// Latest measurement not yet accumulated in [ExecuteDetailsTimings::execute_us]
pub execute_time: Option<Measure>,
pub timings: ExecuteDetailsTimings,
Expand All @@ -218,7 +217,6 @@ impl<'a> InvokeContext<'a> {
environment_config: EnvironmentConfig<'a>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
programs_modified_by_tx: &'a mut ProgramCacheForTxBatch,
) -> Self {
Self {
transaction_context,
Expand All @@ -227,7 +225,6 @@ impl<'a> InvokeContext<'a> {
log_collector,
compute_budget,
compute_meter: RefCell::new(compute_budget.compute_unit_limit),
programs_modified_by_tx,
execute_time: None,
timings: ExecuteDetailsTimings::default(),
syscall_context: Vec::new(),
Expand All @@ -236,11 +233,7 @@ impl<'a> InvokeContext<'a> {
}

pub fn find_program_in_cache(&self, pubkey: &Pubkey) -> Option<Arc<ProgramCacheEntry>> {
// First lookup the cache of the programs modified by the current transaction. If not found, lookup
// the cache of the cache of the programs that are loaded for the transaction batch.
self.programs_modified_by_tx
.find(pubkey)
.or_else(|| self.program_cache_for_tx_batch.find(pubkey))
self.program_cache_for_tx_batch.find(pubkey)
}

pub fn get_environments_for_slot(
Expand Down Expand Up @@ -734,14 +727,12 @@ macro_rules! with_mock_invoke_context {
&sysvar_cache,
);
let mut program_cache_for_tx_batch = ProgramCacheForTxBatch::default();
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let mut $invoke_context = InvokeContext::new(
&mut $transaction_context,
&mut program_cache_for_tx_batch,
environment_config,
Some(LogCollector::new_ref()),
compute_budget,
&mut programs_modified_by_tx,
);
};
}
Expand Down
32 changes: 17 additions & 15 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ macro_rules! deploy_program {
$drop
load_program_metrics.program_id = $program_id.to_string();
load_program_metrics.submit_datapoint(&mut $invoke_context.timings);
$invoke_context.programs_modified_by_tx.store_modified_entry($program_id, Arc::new(executor));
$invoke_context.program_cache_for_tx_batch.store_modified_entry($program_id, Arc::new(executor));
}};
}

Expand Down Expand Up @@ -1109,14 +1109,16 @@ fn process_loader_upgradeable_instruction(
&log_collector,
)?;
let clock = invoke_context.get_sysvar_cache().get_clock()?;
invoke_context.programs_modified_by_tx.store_modified_entry(
program_key,
Arc::new(ProgramCacheEntry::new_tombstone(
clock.slot,
ProgramCacheEntryOwner::LoaderV3,
ProgramCacheEntryType::Closed,
)),
);
invoke_context
.program_cache_for_tx_batch
.store_modified_entry(
program_key,
Arc::new(ProgramCacheEntry::new_tombstone(
clock.slot,
ProgramCacheEntryOwner::LoaderV3,
ProgramCacheEntryType::Closed,
)),
);
}
_ => {
ic_logger_msg!(log_collector, "Invalid Program account");
Expand Down Expand Up @@ -1543,10 +1545,10 @@ pub mod test_utils {
false,
) {
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.set_slot_for_tests(DELAY_VISIBILITY_SLOT_OFFSET);
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.store_modified_entry(*pubkey, Arc::new(loaded_program));
}
}
Expand Down Expand Up @@ -3763,7 +3765,7 @@ mod tests {
latest_access_slot: AtomicU64::new(0),
};
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.replenish(program_id, Arc::new(program));

assert_matches!(
Expand All @@ -3772,7 +3774,7 @@ mod tests {
);

let updated_program = invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.find(&program_id)
.expect("Didn't find upgraded program in the cache");

Expand Down Expand Up @@ -3807,7 +3809,7 @@ mod tests {
latest_access_slot: AtomicU64::new(0),
};
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.replenish(program_id, Arc::new(program));

let program_id2 = Pubkey::new_unique();
Expand All @@ -3817,7 +3819,7 @@ mod tests {
);

let program2 = invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.find(&program_id2)
.expect("Didn't find upgraded program in the cache");

Expand Down
12 changes: 7 additions & 5 deletions programs/loader-v4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub fn process_instruction_deploy(
);
}
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.store_modified_entry(*program.get_key(), Arc::new(executor));
Ok(())
}
Expand Down Expand Up @@ -661,7 +661,7 @@ mod tests {
if let Ok(loaded_program) = ProgramCacheEntry::new(
&loader_v4::id(),
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.environments
.program_runtime_v2
.clone(),
Expand All @@ -671,9 +671,11 @@ mod tests {
account.data().len(),
&mut load_program_metrics,
) {
invoke_context.programs_modified_by_tx.set_slot_for_tests(0);
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.set_slot_for_tests(0);
invoke_context
.program_cache_for_tx_batch
.store_modified_entry(*pubkey, Arc::new(loaded_program));
}
}
Expand Down Expand Up @@ -708,7 +710,7 @@ mod tests {
Entrypoint::vm,
|invoke_context| {
invoke_context
.programs_modified_by_tx
.program_cache_for_tx_batch
.environments
.program_runtime_v2 = Arc::new(create_program_runtime_environment_v2(
&ComputeBudget::default(),
Expand Down
9 changes: 1 addition & 8 deletions runtime/src/bank/builtins/core_bpf_migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ impl Bank {
self.epoch,
&self.transaction_processor.program_cache.read().unwrap(),
);
let mut programs_modified = ProgramCacheForTxBatch::new(
self.slot,
program_cache_for_tx_batch.environments.clone(),
program_cache_for_tx_batch.upcoming_environments.clone(),
program_cache_for_tx_batch.latest_root_epoch,
);

// Configure a dummy `InvokeContext` from the runtime's current
// environment, as well as the two `ProgramCacheForTxBatch`
Expand Down Expand Up @@ -213,7 +207,6 @@ impl Bank {
),
None,
compute_budget,
&mut programs_modified,
);

solana_bpf_loader_program::direct_deploy_program(
Expand All @@ -232,7 +225,7 @@ impl Bank {
.program_cache
.write()
.unwrap()
.merge(&programs_modified.drain_modified_entries());
.merge(&program_cache_for_tx_batch.drain_modified_entries());

Ok(())
}
Expand Down
14 changes: 0 additions & 14 deletions svm/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ mod tests {
]),
));
let sysvar_cache = SysvarCache::default();
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -290,7 +289,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand Down Expand Up @@ -331,7 +329,6 @@ mod tests {
),
]),
));
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -346,7 +343,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand Down Expand Up @@ -377,7 +373,6 @@ mod tests {
),
]),
));
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -392,7 +387,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand Down Expand Up @@ -514,7 +508,6 @@ mod tests {
Some(transaction_context.get_key_of_account_at_index(0).unwrap()),
));
let sysvar_cache = SysvarCache::default();
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -529,7 +522,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand All @@ -555,7 +547,6 @@ mod tests {
)],
Some(transaction_context.get_key_of_account_at_index(0).unwrap()),
));
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -570,7 +561,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand All @@ -593,7 +583,6 @@ mod tests {
)],
Some(transaction_context.get_key_of_account_at_index(0).unwrap()),
));
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -608,7 +597,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand Down Expand Up @@ -692,7 +680,6 @@ mod tests {
mock_program_id,
Arc::new(ProgramCacheEntry::new_builtin(0, 0, MockBuiltin::vm)),
);
let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let environment_config = EnvironmentConfig::new(
Hash::default(),
None,
Expand All @@ -707,7 +694,6 @@ mod tests {
environment_config,
None,
ComputeBudget::default(),
&mut programs_modified_by_tx,
);
let result = MessageProcessor::process_message(
&message,
Expand Down
9 changes: 1 addition & 8 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,6 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
callback.get_last_blockhash_and_lamports_per_signature();

let mut executed_units = 0u64;
let mut programs_modified_by_tx = ProgramCacheForTxBatch::new(
self.slot,
program_cache_for_tx_batch.environments.clone(),
program_cache_for_tx_batch.upcoming_environments.clone(),
program_cache_for_tx_batch.latest_root_epoch,
);
let sysvar_cache = &self.sysvar_cache.read().unwrap();

let mut invoke_context = InvokeContext::new(
Expand All @@ -758,7 +752,6 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
),
log_collector.clone(),
compute_budget,
&mut programs_modified_by_tx,
);

let mut process_message_time = Measure::start("process_message_time");
Expand Down Expand Up @@ -868,7 +861,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
executed_units,
accounts_data_len_delta,
},
programs_modified_by_tx: programs_modified_by_tx.drain_modified_entries(),
programs_modified_by_tx: program_cache_for_tx_batch.drain_modified_entries(),
}
}

Expand Down
2 changes: 0 additions & 2 deletions svm/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ fn execute_fixture_as_instr(
)),
);

let mut programs_modified_by_tx = ProgramCacheForTxBatch::default();
let log_collector = LogCollector::new_ref();

let sysvar_cache = &batch_processor.sysvar_cache.read().unwrap();
Expand All @@ -476,7 +475,6 @@ fn execute_fixture_as_instr(
env_config,
Some(log_collector.clone()),
compute_budget,
&mut programs_modified_by_tx,
);

let mut instruction_accounts: Vec<InstructionAccount> =
Expand Down

0 comments on commit 7a2a9af

Please sign in to comment.