Skip to content

Commit

Permalink
Adds parameter recompile to Bank::load_program().
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Jun 16, 2023
1 parent 7b284ae commit a2adeb5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
7 changes: 5 additions & 2 deletions ledger-tool/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use {
account::AccountSharedData,
account_utils::StateMut,
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
feature_set,
pubkey::Pubkey,
slot_history::Slot,
transaction_context::{IndexOfAccount, InstructionAccount},
Expand Down Expand Up @@ -357,7 +358,9 @@ fn load_program<'a>(
#[allow(unused_mut)]
let mut verified_executable = if is_elf {
let result = load_program_from_bytes(
&invoke_context.feature_set,
invoke_context
.feature_set
.is_active(&feature_set::delay_visibility_of_program_deployment::id()),
log_collector,
&mut load_program_metrics,
&contents,
Expand Down Expand Up @@ -538,7 +541,7 @@ pub fn program(ledger_path: &Path, matches: &ArgMatches<'_>) {
let mut loaded_programs =
LoadedProgramsForTxBatch::new(bank.slot() + DELAY_VISIBILITY_SLOT_OFFSET);
for key in cached_account_keys {
loaded_programs.replenish(key, bank.load_program(&key));
loaded_programs.replenish(key, bank.load_program(&key, None));
debug!("Loaded program {}", key);
}
invoke_context.programs_loaded_for_tx_batch = &loaded_programs;
Expand Down
20 changes: 10 additions & 10 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use {
cap_bpf_program_instruction_accounts, delay_visibility_of_program_deployment,
enable_bpf_loader_extend_program_ix, enable_bpf_loader_set_authority_checked_ix,
enable_program_redeployment_cooldown, limit_max_instruction_trace_length,
native_programs_consume_cu, remove_bpf_loader_incorrect_program_id, FeatureSet,
native_programs_consume_cu, remove_bpf_loader_incorrect_program_id,
},
instruction::{AccountMeta, InstructionError},
loader_instruction::LoaderInstruction,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub const UPGRADEABLE_LOADER_COMPUTE_UNITS: u64 = 2_370;

#[allow(clippy::too_many_arguments)]
pub fn load_program_from_bytes(
feature_set: &FeatureSet,
delay_visibility_of_program_deployment: bool,
log_collector: Option<Rc<RefCell<LogCollector>>>,
load_program_metrics: &mut LoadProgramMetrics,
programdata: &[u8],
Expand All @@ -77,7 +77,7 @@ pub fn load_program_from_bytes(
deployment_slot: Slot,
program_runtime_environment: Arc<BuiltinProgram<InvokeContext<'static>>>,
) -> Result<LoadedProgram, InstructionError> {
let effective_slot = if feature_set.is_active(&delay_visibility_of_program_deployment::id()) {
let effective_slot = if delay_visibility_of_program_deployment {
deployment_slot.saturating_add(DELAY_VISIBILITY_SLOT_OFFSET)
} else {
deployment_slot
Expand All @@ -100,12 +100,12 @@ pub fn load_program_from_bytes(
}

pub fn load_program_from_account(
feature_set: &FeatureSet,
delay_visibility_of_program_deployment: bool,
log_collector: Option<Rc<RefCell<LogCollector>>>,
program: &BorrowedAccount,
programdata: &BorrowedAccount,
program_runtime_environment: Arc<BuiltinProgram<InvokeContext<'static>>>,
) -> Result<(Arc<LoadedProgram>, LoadProgramMetrics), InstructionError> {
) -> Result<(LoadedProgram, LoadProgramMetrics), InstructionError> {
if !check_loader_id(program.get_owner()) {
ic_logger_msg!(
log_collector,
Expand Down Expand Up @@ -149,8 +149,8 @@ pub fn load_program_from_account(
..LoadProgramMetrics::default()
};

let loaded_program = Arc::new(load_program_from_bytes(
feature_set,
let loaded_program = load_program_from_bytes(
delay_visibility_of_program_deployment,
log_collector,
&mut load_program_metrics,
programdata
Expand All @@ -161,7 +161,7 @@ pub fn load_program_from_account(
program.get_data().len().saturating_add(programdata_size),
deployment_slot,
program_runtime_environment,
)?);
)?;

Ok((loaded_program, load_program_metrics))
}
Expand Down Expand Up @@ -195,7 +195,7 @@ macro_rules! deploy_program {
register_syscalls_time.stop();
load_program_metrics.register_syscalls_us = register_syscalls_time.as_us();
let executor = load_program_from_bytes(
&$invoke_context.feature_set,
$invoke_context.feature_set.is_active(&delay_visibility_of_program_deployment::id()),
$invoke_context.get_log_collector(),
&mut load_program_metrics,
$new_programdata,
Expand Down Expand Up @@ -1731,7 +1731,7 @@ pub mod test_utils {
.expect("Failed to get account key");

if let Ok(loaded_program) = load_program_from_bytes(
&FeatureSet::all_enabled(),
true,
None,
&mut load_program_metrics,
account.data(),
Expand Down
35 changes: 27 additions & 8 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,12 @@ impl Bank {
if let Some((_env, ref mut programs_to_recompile)) =
&mut loaded_programs_cache.upcoming_program_runtime_environment_v1
{

if let Some((key, program_to_recompile)) = programs_to_recompile.pop() {
drop(loaded_programs_cache);
let recompiled = new.load_program(&key, Some(program_to_recompile));
let mut loaded_programs_cache = new.loaded_programs_cache.write().unwrap();
loaded_programs_cache.replenish(key, recompiled);
}
} else if slot_index.saturating_add(
solana_program_runtime::loaded_programs::MAX_LOADED_ENTRY_COUNT
.checked_div(2)
Expand Down Expand Up @@ -5138,7 +5143,11 @@ impl Bank {
}
}

pub fn load_program(&self, pubkey: &Pubkey) -> Arc<LoadedProgram> {
pub fn load_program(
&self,
pubkey: &Pubkey,
recompile: Option<Arc<LoadedProgram>>,
) -> Arc<LoadedProgram> {
let program = if let Some(program) = self.get_account_with_fixed_root(pubkey) {
program
} else {
Expand Down Expand Up @@ -5207,21 +5216,31 @@ impl Bank {
&loaded_programs_cache.program_runtime_environment_v1
};
solana_bpf_loader_program::load_program_from_account(
&self.feature_set,
self.feature_set
.is_active(&feature_set::delay_visibility_of_program_deployment::id()),
None, // log_collector
&program,
programdata.as_ref().unwrap_or(&program),
program_runtime_environment_v1.clone(),
program_runtime_environment.clone(),
)
.map(|(loaded_program, metrics)| {
.map(|(mut loaded_program, metrics)| {
let mut timings = ExecuteDetailsTimings::default();
metrics.submit_datapoint(&mut timings);
loaded_program
if let Some(recompile) = recompile {
loaded_program.effective_slot = self
.epoch_schedule()
.get_first_slot_in_epoch(self.epoch.saturating_add(1));
loaded_program.tx_usage_counter =
AtomicU64::new(recompile.tx_usage_counter.load(Ordering::Relaxed));
loaded_program.ix_usage_counter =
AtomicU64::new(recompile.ix_usage_counter.load(Ordering::Relaxed));
}
Arc::new(loaded_program)
})
.unwrap_or_else(|_| {
Arc::new(LoadedProgram::new_tombstone(
self.slot,
LoadedProgramType::FailedVerification(program_runtime_environment_v1),
LoadedProgramType::FailedVerification(program_runtime_environment.clone()),
))
})
}
Expand Down Expand Up @@ -5469,7 +5488,7 @@ impl Bank {
let missing_programs: Vec<(Pubkey, Arc<LoadedProgram>)> = missing_programs
.iter()
.map(|(key, count)| {
let program = self.load_program(key);
let program = self.load_program(key, None);
program.tx_usage_counter.store(*count, Ordering::Relaxed);
(*key, program)
})
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7416,7 +7416,7 @@ fn test_bank_load_program() {
programdata_account.set_rent_epoch(1);
bank.store_account_and_update_capitalization(&key1, &program_account);
bank.store_account_and_update_capitalization(&programdata_key, &programdata_account);
let program = bank.load_program(&key1);
let program = bank.load_program(&key1, None);
assert!(matches!(program.program, LoadedProgramType::LegacyV1(_)));
assert_eq!(
program.account_size,
Expand Down Expand Up @@ -7571,7 +7571,7 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
assert_eq!(*elf.get(i).unwrap(), *byte);
}

let loaded_program = bank.load_program(&program_keypair.pubkey());
let loaded_program = bank.load_program(&program_keypair.pubkey(), None);

// Invoke deployed program
mock_process_instruction(
Expand Down

0 comments on commit a2adeb5

Please sign in to comment.