Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.17: Fix - Bank::compute_active_feature_set() and Bank::apply_feature_activations() (backport of #34124) #34136

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7892,6 +7892,19 @@ impl Bank {
self.compute_active_feature_set(allow_new_activations);
self.feature_set = Arc::new(feature_set);

// Update activation slot of features in `new_feature_activations`
for feature_id in new_feature_activations.iter() {
if let Some(mut account) = self.get_account_with_fixed_root(feature_id) {
if let Some(mut feature) = feature::from_account(&account) {
feature.activated_at = Some(self.slot());
if feature::to_account(&feature, &mut account).is_some() {
self.store_account(feature_id, &account);
}
info!("Feature {} activated at slot {}", feature_id, self.slot());
}
}
}

if new_feature_activations.contains(&feature_set::pico_inflation::id()) {
*self.inflation.write().unwrap() = Inflation::pico();
self.fee_rate_governor.burn_percent = 50; // 50% fee burn
Expand Down Expand Up @@ -7960,38 +7973,27 @@ impl Bank {

/// Compute the active feature set based on the current bank state,
/// and return it together with the set of newly activated features.
fn compute_active_feature_set(
&mut self,
allow_new_activations: bool,
) -> (FeatureSet, HashSet<Pubkey>) {
fn compute_active_feature_set(&self, include_pending: bool) -> (FeatureSet, HashSet<Pubkey>) {
let mut active = self.feature_set.active.clone();
let mut inactive = HashSet::new();
let mut newly_activated = HashSet::new();
let mut pending = HashSet::new();
let slot = self.slot();

for feature_id in &self.feature_set.inactive {
let mut activated = None;
if let Some(mut account) = self.get_account_with_fixed_root(feature_id) {
if let Some(mut feature) = feature::from_account(&account) {
if let Some(account) = self.get_account_with_fixed_root(feature_id) {
if let Some(feature) = feature::from_account(&account) {
match feature.activated_at {
None => {
if allow_new_activations {
// Feature has been requested, activate it now
feature.activated_at = Some(slot);
if feature::to_account(&feature, &mut account).is_some() {
self.store_account(feature_id, &account);
}
newly_activated.insert(*feature_id);
activated = Some(slot);
info!("Feature {} activated at slot {}", feature_id, slot);
}
None if include_pending => {
// Feature activation is pending
pending.insert(*feature_id);
activated = Some(slot);
}
Some(activation_slot) => {
if slot >= activation_slot {
// Feature is already active
activated = Some(activation_slot);
}
Some(activation_slot) if slot >= activation_slot => {
// Feature has been activated already
activated = Some(activation_slot);
}
_ => {}
}
}
}
Expand All @@ -8002,7 +8004,7 @@ impl Bank {
}
}

(FeatureSet { active, inactive }, newly_activated)
(FeatureSet { active, inactive }, pending)
}

fn apply_builtin_program_feature_transitions(
Expand Down
19 changes: 10 additions & 9 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7740,25 +7740,26 @@ fn test_compute_active_feature_set() {
let feature = Feature::default();
assert_eq!(feature.activated_at, None);
bank.store_account(&test_feature, &feature::create_account(&feature, 42));
let feature = feature::from_account(&bank.get_account(&test_feature).expect("get_account"))
.expect("from_account");
assert_eq!(feature.activated_at, None);

// Run `compute_active_feature_set` disallowing new activations
// Run `compute_active_feature_set` excluding pending activation
let (feature_set, new_activations) = bank.compute_active_feature_set(false);
assert!(new_activations.is_empty());
assert!(!feature_set.is_active(&test_feature));
let feature = feature::from_account(&bank.get_account(&test_feature).expect("get_account"))
.expect("from_account");
assert_eq!(feature.activated_at, None);

// Run `compute_active_feature_set` allowing new activations
let (feature_set, new_activations) = bank.compute_active_feature_set(true);
// Run `compute_active_feature_set` including pending activation
let (_feature_set, new_activations) = bank.compute_active_feature_set(true);
assert_eq!(new_activations.len(), 1);
assert!(feature_set.is_active(&test_feature));
assert!(new_activations.contains(&test_feature));

// Actually activate the pending activation
bank.apply_feature_activations(ApplyFeatureActivationsCaller::NewFromParent, true);
let feature = feature::from_account(&bank.get_account(&test_feature).expect("get_account"))
.expect("from_account");
assert_eq!(feature.activated_at, Some(1));

// Running `compute_active_feature_set` will not cause new activations, but
// `test_feature` is now be active
let (feature_set, new_activations) = bank.compute_active_feature_set(true);
assert!(new_activations.is_empty());
assert!(feature_set.is_active(&test_feature));
Expand Down