solana_program_runtime/program_cache/ebpp.rs
1//! The Epoch Boundary Preparation Phase (EBPP) is an optimization offered by
2//! the program cache in order to preemptively reload programs for the
3//! anticipated upcoming environment based on feature activations.
4//!
5//! Without such a phase, pending features would activate at the epoch boundary
6//! and change the [`ProgramRuntimeEnvironment`], leaving no cached program
7//! usable under the new environment. Each program would then have to be
8//! reloaded, verified, and compiled on first use, slowing transaction execution
9//! at the start of the epoch.
10//!
11//! Instead, if the anticipated environment differs from the current one, one
12//! program per slot is recompiled against it during the final slots of the
13//! epoch, starting with the highest retention score. This is best-effort: any
14//! program not recompiled in time is loaded on demand after the boundary.
15
16use {
17 crate::program_cache::{entry::ProgramCacheEntry, env::ProgramRuntimeEnvironment},
18 solana_clock::Epoch,
19 solana_pubkey::Pubkey,
20 solana_svm_type_overrides::sync::Arc,
21};
22
23/// Globally manages the transition between environments at the epoch boundary
24#[derive(Debug, Default)]
25pub struct EpochBoundaryPreparation {
26 /// The epoch of the upcoming_environment
27 pub upcoming_epoch: Epoch,
28 /// Anticipated replacement for `environments` at the next epoch
29 ///
30 /// This is only `Some` around the boundaries when a changed environment is
31 /// actually coming. It starts with the cache preparation phase a few
32 /// hundred slots before the epoch boundary, and it ends with the first
33 /// rerooting after the epoch boundary.
34 pub upcoming_environment: Option<ProgramRuntimeEnvironment>,
35 /// List of loaded programs which should be recompiled before the next epoch (but don't have to).
36 pub programs_to_recompile: Vec<(Pubkey, Arc<ProgramCacheEntry>)>,
37}
38
39impl EpochBoundaryPreparation {
40 pub fn new(epoch: Epoch) -> Self {
41 Self {
42 upcoming_epoch: epoch,
43 upcoming_environment: None,
44 programs_to_recompile: Vec::default(),
45 }
46 }
47
48 /// Returns the upcoming environments depending on the given epoch
49 pub fn get_upcoming_environment_for_epoch(
50 &self,
51 epoch: Epoch,
52 ) -> Option<ProgramRuntimeEnvironment> {
53 if epoch == self.upcoming_epoch {
54 return self.upcoming_environment.clone();
55 }
56 None
57 }
58
59 /// Before rerooting the blockstore this concludes the epoch boundary
60 /// preparation. Returns whether there was one to conclude.
61 pub fn reroot(&mut self, epoch: Epoch) -> bool {
62 if epoch == self.upcoming_epoch && self.upcoming_environment.take().is_some() {
63 self.programs_to_recompile.clear();
64 return true;
65 }
66
67 false
68 }
69}