Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
update slot in cache in the test
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed May 5, 2023
1 parent d9c9bb2 commit 1c14a1d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
6 changes: 3 additions & 3 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub struct InvokeContext<'a> {
current_compute_budget: ComputeBudget,
compute_meter: RefCell<u64>,
accounts_data_meter: AccountsDataMeter,
pub programs_loaded_for_tx_batch: Rc<RefCell<LoadedProgramsForTxBatch>>,
pub programs_loaded_for_tx_batch: Rc<LoadedProgramsForTxBatch>,
pub programs_modified_by_tx: Rc<RefCell<LoadedProgramsForTxBatch>>,
pub feature_set: Arc<FeatureSet>,
pub timings: ExecuteDetailsTimings,
Expand All @@ -178,7 +178,7 @@ impl<'a> InvokeContext<'a> {
sysvar_cache: &'a SysvarCache,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
programs_loaded_for_tx_batch: Rc<RefCell<LoadedProgramsForTxBatch>>,
programs_loaded_for_tx_batch: Rc<LoadedProgramsForTxBatch>,
programs_modified_by_tx: Rc<RefCell<LoadedProgramsForTxBatch>>,
feature_set: Arc<FeatureSet>,
blockhash: Hash,
Expand Down Expand Up @@ -942,7 +942,7 @@ macro_rules! with_mock_invoke_context_and_builtin_programs {
&sysvar_cache,
Some(LogCollector::new_ref()),
compute_budget,
Rc::new(RefCell::new(LoadedProgramsForTxBatch::default())),
Rc::new(LoadedProgramsForTxBatch::default()),
Rc::new(RefCell::new(LoadedProgramsForTxBatch::default())),
Arc::new(FeatureSet::all_enabled()),
Hash::default(),
Expand Down
4 changes: 4 additions & 0 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ impl LoadedProgramsForTxBatch {
pub fn slot(&self) -> Slot {
self.slot
}

pub fn set_slot(&mut self, slot: Slot) {
self.slot = slot;
}
}

pub enum LoadedProgramMatchCriteria {
Expand Down
35 changes: 19 additions & 16 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ pub fn load_program_from_account(
Ok((loaded_program, Some(load_program_metrics)))
}

fn find_program_in_cache(
invoke_context: &InvokeContext,
pubkey: &Pubkey,
) -> Option<Arc<LoadedProgram>> {
// 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.
invoke_context
.programs_modified_by_tx
.borrow()
.find(pubkey)
.or_else(|| invoke_context.programs_loaded_for_tx_batch.find(pubkey))
}

macro_rules! deploy_program {
($invoke_context:expr, $program_id:expr, $loader_key:expr,
$account_size:expr, $slot:expr, $drop:expr, $new_programdata:expr $(,)?) => {{
Expand All @@ -206,7 +219,7 @@ macro_rules! deploy_program {
true, /* reject_deployment_of_broken_elfs */
false, /* debugging_features */
)?;
if let Some(old_entry) = $invoke_context.programs_loaded_for_tx_batch.borrow().find(&$program_id) {
if let Some(old_entry) = find_program_in_cache($invoke_context, &$program_id) {
let usage_counter = old_entry.usage_counter.load(Ordering::Relaxed);
executor.usage_counter.store(usage_counter, Ordering::Relaxed);
}
Expand Down Expand Up @@ -553,18 +566,7 @@ fn process_instruction_inner(
}

let mut get_or_create_executor_time = Measure::start("get_or_create_executor_time");
// 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.
let executor = invoke_context
.programs_modified_by_tx
.borrow()
.find(program_account.get_key())
.or_else(|| {
invoke_context
.programs_loaded_for_tx_batch
.borrow()
.find(program_account.get_key())
})
let executor = find_program_in_cache(invoke_context, program_account.get_key())
.ok_or(InstructionError::InvalidAccountData)?;

if executor.is_tombstone() {
Expand Down Expand Up @@ -1693,7 +1695,8 @@ pub mod test_utils {
true,
false,
) {
let mut cache = invoke_context.programs_loaded_for_tx_batch.borrow_mut();
let mut cache = invoke_context.programs_modified_by_tx.borrow_mut();
cache.set_slot(1);
cache.replenish(*pubkey, Arc::new(loaded_program));
}
}
Expand Down Expand Up @@ -4097,7 +4100,7 @@ mod tests {
usage_counter: AtomicU64::new(100),
};
invoke_context
.programs_loaded_for_tx_batch
.programs_modified_by_tx
.borrow_mut()
.replenish(program_id, Arc::new(program));

Expand Down Expand Up @@ -4130,7 +4133,7 @@ mod tests {
usage_counter: AtomicU64::new(100),
};
invoke_context
.programs_loaded_for_tx_batch
.programs_modified_by_tx
.borrow_mut()
.replenish(program_id, Arc::new(program));

Expand Down
10 changes: 8 additions & 2 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,10 @@ fn test_program_sbf_loader_deprecated() {
let bank = Bank::new_for_tests(&genesis_config);
let program_id = create_program(&bank, &bpf_loader_deprecated::id(), program);

let bank_client = BankClient::new(bank);
let mut bank_client = BankClient::new(bank);
bank_client
.advance_slot(1, &Pubkey::default())
.expect("Failed to advance the slot");
let account_metas = vec![AccountMeta::new(mint_keypair.pubkey(), true)];
let instruction = Instruction::new_with_bytes(program_id, &[1], account_metas);
let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction);
Expand Down Expand Up @@ -3908,7 +3911,10 @@ fn test_program_sbf_inner_instruction_alignment_checks() {

// invoke unaligned program, which will call aligned program twice,
// unaligned should be allowed once invoke completes
let bank_client = BankClient::new(bank);
let mut bank_client = BankClient::new(bank);
bank_client
.advance_slot(1, &Pubkey::default())
.expect("Failed to advance the slot");
let mut instruction = Instruction::new_with_bytes(
inner_instruction_alignment_check,
&[0],
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4264,7 +4264,7 @@ impl Bank {
timings: &mut ExecuteTimings,
error_counters: &mut TransactionErrorMetrics,
log_messages_bytes_limit: Option<usize>,
programs_loaded_for_tx_batch: Rc<RefCell<LoadedProgramsForTxBatch>>,
programs_loaded_for_tx_batch: Rc<LoadedProgramsForTxBatch>,
) -> TransactionExecutionResult {
let prev_accounts_data_len = self.load_accounts_data_size();
let transaction_accounts = std::mem::take(&mut loaded_transaction.accounts);
Expand Down Expand Up @@ -4620,7 +4620,8 @@ impl Bank {
&self.blockhash_queue.read().unwrap(),
);

let programs_loaded_for_tx_batch = self.replenish_program_cache(&program_accounts_map);
let programs_loaded_for_tx_batch =
Rc::new(self.replenish_program_cache(&program_accounts_map));

let mut load_time = Measure::start("accounts_load");
let mut loaded_transactions = self.rc.accounts.load_accounts(
Expand All @@ -4638,7 +4639,6 @@ impl Bank {
);
load_time.stop();

let programs_loaded_for_tx_batch = Rc::new(RefCell::new(programs_loaded_for_tx_batch));
let mut execution_time = Measure::start("execution_time");
let mut signature_count: u64 = 0;

Expand Down
3 changes: 2 additions & 1 deletion runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7771,7 +7771,8 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
Ok(()),
solana_bpf_loader_program::process_instruction,
|invoke_context| {
let mut cache = invoke_context.programs_loaded_for_tx_batch.borrow_mut();
let mut cache = invoke_context.programs_modified_by_tx.borrow_mut();
cache.set_slot(bank.slot() + 1);
cache.replenish(program_keypair.pubkey(), loaded_program.clone());
},
|_invoke_context| {},
Expand Down
10 changes: 4 additions & 6 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl MessageProcessor {
transaction_context: &mut TransactionContext,
rent: Rent,
log_collector: Option<Rc<RefCell<LogCollector>>>,
programs_loaded_for_tx_batch: Rc<RefCell<LoadedProgramsForTxBatch>>,
programs_loaded_for_tx_batch: Rc<LoadedProgramsForTxBatch>,
programs_modified_by_tx: Rc<RefCell<LoadedProgramsForTxBatch>>,
feature_set: Arc<FeatureSet>,
compute_budget: ComputeBudget,
Expand Down Expand Up @@ -274,8 +274,7 @@ mod tests {
let mut transaction_context =
TransactionContext::new(accounts, Some(Rent::default()), 1, 3);
let program_indices = vec![vec![2]];
let programs_loaded_for_tx_batch =
Rc::new(RefCell::new(LoadedProgramsForTxBatch::default()));
let programs_loaded_for_tx_batch = Rc::new(LoadedProgramsForTxBatch::default());
let account_keys = (0..transaction_context.get_number_of_accounts())
.map(|index| {
*transaction_context
Expand Down Expand Up @@ -502,8 +501,7 @@ mod tests {
let mut transaction_context =
TransactionContext::new(accounts, Some(Rent::default()), 1, 3);
let program_indices = vec![vec![2]];
let programs_loaded_for_tx_batch =
Rc::new(RefCell::new(LoadedProgramsForTxBatch::default()));
let programs_loaded_for_tx_batch = Rc::new(LoadedProgramsForTxBatch::default());
let account_metas = vec![
AccountMeta::new(
*transaction_context.get_key_of_account_at_index(0).unwrap(),
Expand Down Expand Up @@ -678,7 +676,7 @@ mod tests {
&mut transaction_context,
RentCollector::default().rent,
None,
Rc::new(RefCell::new(LoadedProgramsForTxBatch::default())),
Rc::new(LoadedProgramsForTxBatch::default()),
Rc::new(RefCell::new(LoadedProgramsForTxBatch::default())),
Arc::new(FeatureSet::all_enabled()),
ComputeBudget::default(),
Expand Down

0 comments on commit 1c14a1d

Please sign in to comment.