Skip to content

Commit

Permalink
Adds LoadedPrograms::get_environments_for_epoch().
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Oct 13, 2023
1 parent c193ca2 commit 64038d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
24 changes: 20 additions & 4 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ pub trait ForkGraph {

/// Provides information about current working slot, and its ancestors
pub trait WorkingSlot {
/// Returns the current slot value
/// Returns the current slot
fn current_slot(&self) -> Slot;

/// Returns the epoch of the current slot
fn current_epoch(&self) -> Epoch;

/// Returns true if the `other` slot is an ancestor of self, false otherwise
fn is_ancestor(&self, other: Slot) -> bool;
}
Expand Down Expand Up @@ -529,6 +532,11 @@ pub enum LoadedProgramMatchCriteria {
}

impl LoadedPrograms {
/// Returns the current environments depending on the given epoch
pub fn get_environments_for_epoch(&self, _epoch: Epoch) -> &ProgramRuntimeEnvironments {
&self.environments
}

/// Refill the cache with a single entry. It's typically called during transaction loading,
/// when the cache doesn't contain the entry corresponding to program `key`.
/// The function dedupes the cache, in case some other thread replenished the entry in parallel.
Expand Down Expand Up @@ -716,6 +724,7 @@ impl LoadedPrograms {
working_slot: &S,
keys: impl Iterator<Item = (Pubkey, (LoadedProgramMatchCriteria, u64))>,
) -> ExtractedPrograms {
let environments = self.get_environments_for_epoch(working_slot.current_epoch());
let mut missing = Vec::new();
let mut unloaded = Vec::new();
let found = keys
Expand All @@ -733,7 +742,7 @@ impl LoadedPrograms {
return None;
}

if !Self::matches_environment(entry, &self.environments) {
if !Self::matches_environment(entry, environments) {
missing.push((key, count));
return None;
}
Expand Down Expand Up @@ -778,7 +787,7 @@ impl LoadedPrograms {
loaded: LoadedProgramsForTxBatch {
entries: found,
slot: working_slot.current_slot(),
environments: self.environments.clone(),
environments: environments.clone(),
},
missing,
unloaded,
Expand Down Expand Up @@ -920,7 +929,10 @@ mod tests {
assert_matches::assert_matches,
percentage::Percentage,
solana_rbpf::vm::BuiltinProgram,
solana_sdk::{clock::Slot, pubkey::Pubkey},
solana_sdk::{
clock::{Epoch, Slot},
pubkey::Pubkey,
},
std::{
ops::ControlFlow,
sync::{
Expand Down Expand Up @@ -1472,6 +1484,10 @@ mod tests {
self.slot
}

fn current_epoch(&self) -> Epoch {
0
}

fn is_ancestor(&self, other: Slot) -> bool {
self.fork
.iter()
Expand Down
20 changes: 11 additions & 9 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,10 @@ impl WorkingSlot for Bank {
self.slot
}

fn current_epoch(&self) -> Epoch {
self.epoch
}

fn is_ancestor(&self, other: Slot) -> bool {
self.ancestors.contains_key(&other)
}
Expand Down Expand Up @@ -4675,12 +4679,8 @@ impl Bank {
}

pub fn load_program(&self, pubkey: &Pubkey, reload: bool) -> Arc<LoadedProgram> {
let environments = self
.loaded_programs_cache
.read()
.unwrap()
.environments
.clone();
let loaded_programs_cache = self.loaded_programs_cache.read().unwrap();
let environments = loaded_programs_cache.get_environments_for_epoch(self.epoch);

let mut load_program_metrics = LoadProgramMetrics {
program_id: pubkey.to_string(),
Expand Down Expand Up @@ -4773,20 +4773,22 @@ impl Bank {
})
.unwrap_or(LoadedProgram::new_tombstone(
self.slot,
LoadedProgramType::FailedVerification(environments.program_runtime_v2),
LoadedProgramType::FailedVerification(
environments.program_runtime_v2.clone(),
),
));
Ok(loaded_program)
}

ProgramAccountLoadResult::InvalidV4Program => Ok(LoadedProgram::new_tombstone(
self.slot,
LoadedProgramType::FailedVerification(environments.program_runtime_v2),
LoadedProgramType::FailedVerification(environments.program_runtime_v2.clone()),
)),
}
.unwrap_or_else(|_| {
LoadedProgram::new_tombstone(
self.slot,
LoadedProgramType::FailedVerification(environments.program_runtime_v1),
LoadedProgramType::FailedVerification(environments.program_runtime_v1.clone()),
)
});

Expand Down

0 comments on commit 64038d2

Please sign in to comment.