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

Commit

Permalink
runtime: bank: wire in core bpf migration feature activations
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Feb 20, 2024
1 parent ab49495 commit 5221627
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
75 changes: 70 additions & 5 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use solana_sdk::reward_type::RewardType;
use {
crate::{
bank::{
builtins::{BuiltinPrototype, BUILTINS},
builtins::{BuiltinPrototype, BUILTINS, EPHEMERAL_BUILTINS},
metrics::*,
},
bank_forks::BankForks,
Expand Down Expand Up @@ -5953,7 +5953,16 @@ impl Bank {
.iter()
.chain(additional_builtins.unwrap_or(&[]).iter())
{
if builtin.enable_feature_id.is_none() {
let should_add_builtin = builtin.enable_feature_id.is_none() && {
if let Some(core_bpf_migration) = &builtin.core_bpf_migration {
// The built-in should be added if the feature to
// migrate it to Core BPF is not active.
!self.feature_set.is_active(&core_bpf_migration.feature_id)
} else {
true
}
};
if should_add_builtin {
self.add_builtin(
builtin.program_id,
builtin.name.to_string(),
Expand Down Expand Up @@ -6051,6 +6060,10 @@ impl Bank {
self.load_slow(&self.ancestors, pubkey)
}

pub fn get_builtins(&self) -> &HashSet<Pubkey> {
&self.builtin_programs
}

fn load_slow(
&self,
ancestors: &Ancestors,
Expand Down Expand Up @@ -7299,14 +7312,49 @@ impl Bank {
new_feature_activations: &HashSet<Pubkey>,
) {
for builtin in BUILTINS.iter() {
// The `builtin_disabled` flag is used to handle the case where a
// built-in is scheduled to be enabled by one feature gate and
// later migrated to Core BPF by another.
// There should never be a case where a built-in is set to be
// migrated to Core BPF and is also set to be enabled on feature
// activation on the same feature gate. However, the
// `builtin_disabled` flag will handle this case as well, electing
// to first attempt the migration to Core BPF.
// This will fail gracefully, and the built-in will subsequently be
// enabled, but it will never be migrated to Core BPF.
// Using the same feature gate for both enabling and migrating a
// built-in to Core BPF should be strictly avoided.
let mut builtin_disabled = false;
if let Some(core_bpf_migration) = &builtin.core_bpf_migration {
// If the built-in is set to be migrated to Core BPF on feature
// activation, perform the migration and do not add the program
// to the bank's builtins. The migration will remove it from
// the builtins list and the cache.
if new_feature_activations.contains(&core_bpf_migration.feature_id) {
if let Err(e) = builtin.migrate_to_core_bpf(self) {
warn!(
"Failed to migrate built-in {} to Core BPF: {}",
builtin.name, e
);
} else {
builtin_disabled = true;
}
} else {
// If the built-in has already been migrated to Core BPF, do not
// add it to the bank's builtins.
builtin_disabled = self.feature_set.is_active(&core_bpf_migration.feature_id);
}
};

if let Some(feature_id) = builtin.enable_feature_id {
let should_apply_action_for_feature_transition =
if only_apply_transitions_for_new_features {
let should_enable_builtin_on_feature_transition = !builtin_disabled
&& if only_apply_transitions_for_new_features {
new_feature_activations.contains(&feature_id)
} else {
self.feature_set.is_active(&feature_id)
};
if should_apply_action_for_feature_transition {

if should_enable_builtin_on_feature_transition {
self.add_builtin(
builtin.program_id,
builtin.name.to_string(),
Expand All @@ -7319,6 +7367,23 @@ impl Bank {
}
}
}

// Migrate any necessary ephemeral built-ins to core BPF.
// Ephemeral built-ins do not have an `enable_feature_id` since they
// do not exist on-chain.
for ephemeral_builtin in EPHEMERAL_BUILTINS.iter() {
if let Some(core_bpf_migration) = &ephemeral_builtin.core_bpf_migration {
if new_feature_activations.contains(&core_bpf_migration.feature_id) {
if let Err(e) = ephemeral_builtin.migrate_to_core_bpf(self) {
warn!(
"Failed to migrate ephemeral built-in {} to Core BPF: {}",
ephemeral_builtin.name, e
);
}
}
}
}

for precompile in get_precompiles() {
let should_add_precompile = precompile
.feature
Expand Down
1 change: 0 additions & 1 deletion runtime/src/bank/builtins/prototypes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)] // Removed in later commit
use {
super::core_bpf_migration::{
error::CoreBpfMigrationError, CoreBpfMigration, CoreBpfMigrationConfig,
Expand Down
11 changes: 5 additions & 6 deletions runtime/src/snapshot_minimizer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Used to create minimal snapshots - separated here to keep accounts_db simpler
use {
crate::{
bank::{builtins::BUILTINS, Bank},
static_ids,
},
crate::{bank::Bank, static_ids},
dashmap::DashSet,
log::info,
rayon::{
Expand Down Expand Up @@ -116,8 +113,10 @@ impl<'a> SnapshotMinimizer<'a> {

/// Used to get builtin accounts in `minimize`
fn get_builtins(&self) {
BUILTINS.iter().for_each(|e| {
self.minimized_account_set.insert(e.program_id);
// Use the bank's builtins, since some builtins from the static
// `BUILTINS` list may have been migrated to Core BPF.
self.bank.get_builtins().iter().for_each(|program_id| {
self.minimized_account_set.insert(*program_id);
});
}

Expand Down

0 comments on commit 5221627

Please sign in to comment.