stake: Remove v2 program references (backport #19308) (#19336)

* stake: Remove v2 program references (#19308)

* stake: Remove v2 program references

* Remove stake v2 feature, along with stake rewrite

(cherry picked from commit 73aa004c59)

# Conflicts:
#	runtime/src/bank.rs

* Fix merge conflict

* Fix uses of old `stake` function

Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
This commit is contained in:
mergify[bot]
2021-08-20 07:32:24 +00:00
committed by GitHub
parent caea9c99cd
commit b5b1ed2a55
12 changed files with 103 additions and 629 deletions

View File

@@ -985,8 +985,6 @@ pub struct Bank {
pub lazy_rent_collection: AtomicBool,
pub no_stake_rewrite: AtomicBool,
// this is temporary field only to remove rewards_pool entirely
pub rewards_pool_pubkeys: Arc<HashSet<Pubkey>>,
@@ -1211,7 +1209,6 @@ impl Bank {
rewards: RwLock::new(vec![]),
cluster_type: parent.cluster_type,
lazy_rent_collection: AtomicBool::new(parent.lazy_rent_collection.load(Relaxed)),
no_stake_rewrite: AtomicBool::new(parent.no_stake_rewrite.load(Relaxed)),
rewards_pool_pubkeys: parent.rewards_pool_pubkeys.clone(),
cached_executors: RwLock::new(
(*parent.cached_executors.read().unwrap()).clone_with_epoch(epoch),
@@ -1252,11 +1249,7 @@ impl Bank {
new.apply_feature_activations(false, false);
}
let cloned = new
.stakes
.read()
.unwrap()
.clone_with_epoch(epoch, new.stake_program_v2_enabled());
let cloned = new.stakes.read().unwrap().clone_with_epoch(epoch);
*new.stakes.write().unwrap() = cloned;
let leader_schedule_epoch = epoch_schedule.get_leader_schedule_epoch(slot);
@@ -1370,7 +1363,6 @@ impl Bank {
rewards: new(),
cluster_type: Some(genesis_config.cluster_type),
lazy_rent_collection: new(),
no_stake_rewrite: new(),
rewards_pool_pubkeys: new(),
cached_executors: RwLock::new(CowCachedExecutors::new(Arc::new(RwLock::new(
CachedExecutors::new(MAX_CACHED_EXECUTORS, fields.epoch),
@@ -1776,41 +1768,6 @@ impl Bank {
self.epoch_schedule.get_slots_in_epoch(prev_epoch) as f64 / self.slots_per_year
}
fn rewrite_stakes(&self) -> (usize, usize) {
let mut examined_count = 0;
let mut rewritten_count = 0;
self.cloned_stake_delegations()
.into_iter()
.for_each(|(stake_pubkey, _delegation)| {
examined_count += 1;
if let Some(mut stake_account) = self.get_account_with_fixed_root(&stake_pubkey) {
if let Ok(result) =
stake_state::rewrite_stakes(&mut stake_account, &self.rent_collector.rent)
{
self.store_account(&stake_pubkey, &stake_account);
let message = format!("rewrote stake: {}, {:?}", stake_pubkey, result);
info!("{}", message);
datapoint_info!("stake_info", ("info", message, String));
rewritten_count += 1;
}
}
});
info!(
"bank (slot: {}): rewrite_stakes: {} accounts rewritten / {} accounts examined",
self.slot(),
rewritten_count,
examined_count,
);
datapoint_info!(
"rewrite-stakes",
("examined_count", examined_count, i64),
("rewritten_count", rewritten_count, i64)
);
(examined_count, rewritten_count)
}
// Calculates the starting-slot for inflation from the activation slot.
// This method assumes that `pico_inflation` will be enabled before `full_inflation`, giving
// precedence to the latter. However, since `pico_inflation` is fixed-rate Inflation, should
@@ -1877,12 +1834,8 @@ impl Bank {
let old_vote_balance_and_staked = self.stakes.read().unwrap().vote_balance_and_staked();
let validator_point_value = self.pay_validator_rewards(
prev_epoch,
validator_rewards,
reward_calc_tracer,
self.stake_program_v2_enabled(),
);
let validator_point_value =
self.pay_validator_rewards(prev_epoch, validator_rewards, reward_calc_tracer);
if !self
.feature_set
@@ -2019,7 +1972,6 @@ impl Bank {
rewarded_epoch: Epoch,
rewards: u64,
reward_calc_tracer: &mut Option<impl FnMut(&RewardCalculationEvent)>,
fix_stake_deactivate: bool,
) -> f64 {
let stake_history = self.stakes.read().unwrap().history().clone();
@@ -2033,13 +1985,8 @@ impl Bank {
.map(move |(_stake_pubkey, stake_account)| (stake_account, vote_account))
})
.map(|(stake_account, vote_account)| {
stake_state::calculate_points(
stake_account,
vote_account,
Some(&stake_history),
fix_stake_deactivate,
)
.unwrap_or(0)
stake_state::calculate_points(stake_account, vote_account, Some(&stake_history))
.unwrap_or(0)
})
.sum();
@@ -2083,7 +2030,6 @@ impl Bank {
&point_value,
Some(&stake_history),
&mut reward_calc_tracer.as_mut(),
fix_stake_deactivate,
);
if let Ok((stakers_reward, _voters_reward)) = redeemed {
self.store_account(stake_pubkey, stake_account);
@@ -3707,7 +3653,6 @@ impl Bank {
#[cfg(test)]
fn restore_old_behavior_for_fragile_tests(&self) {
self.lazy_rent_collection.store(true, Relaxed);
self.no_stake_rewrite.store(true, Relaxed);
}
fn enable_eager_rent_collection(&self) -> bool {
@@ -4221,7 +4166,6 @@ impl Bank {
self.stakes.write().unwrap().store(
pubkey,
account,
self.stake_program_v2_enabled(),
self.check_init_vote_data_enabled(),
);
}
@@ -4887,7 +4831,6 @@ impl Bank {
if let Some(old_vote_account) = self.stakes.write().unwrap().store(
pubkey,
account,
self.stake_program_v2_enabled(),
self.check_init_vote_data_enabled(),
) {
// TODO: one of the indices is redundant.
@@ -5108,11 +5051,6 @@ impl Bank {
.is_active(&feature_set::no_overflow_rent_distribution::id())
}
pub fn stake_program_v2_enabled(&self) -> bool {
self.feature_set
.is_active(&feature_set::stake_program_v2::id())
}
pub fn check_init_vote_data_enabled(&self) -> bool {
self.feature_set
.is_active(&feature_set::check_init_vote_data::id())
@@ -5182,16 +5120,6 @@ impl Bank {
if new_feature_activations.contains(&feature_set::spl_token_v2_set_authority_fix::id()) {
self.apply_spl_token_v2_set_authority_fix();
}
// Remove me after a while around v1.6
if !self.no_stake_rewrite.load(Relaxed)
&& new_feature_activations.contains(&feature_set::rewrite_stake::id())
{
// to avoid any potential risk of wrongly rewriting accounts in the future,
// only do this once, taking small risk of unknown
// bugs which again creates bad stake accounts..
self.rewrite_stakes();
}
if new_feature_activations.contains(&feature_set::rent_for_sysvars::id()) {
// when this feature is activated, immediately all of existing sysvars are susceptible
// to rent collection and account data removal due to insufficient balance due to only
@@ -7477,7 +7405,7 @@ pub(crate) mod tests {
.map(move |(_stake_pubkey, stake_account)| (stake_account, vote_account))
})
.map(|(stake_account, vote_account)| {
stake_state::calculate_points(stake_account, vote_account, None, true).unwrap_or(0)
stake_state::calculate_points(stake_account, vote_account, None).unwrap_or(0)
})
.sum();
@@ -9016,7 +8944,7 @@ pub(crate) mod tests {
// epoch_stakes are a snapshot at the leader_schedule_slot_offset boundary
// in the prior epoch (0 in this case)
assert_eq!(
leader_stake.stake(0, None, true),
leader_stake.stake(0, None),
vote_accounts.unwrap().get(&leader_vote_account).unwrap().0
);
@@ -9032,7 +8960,7 @@ pub(crate) mod tests {
assert!(child.epoch_vote_accounts(epoch).is_some());
assert_eq!(
leader_stake.stake(child.epoch(), None, true),
leader_stake.stake(child.epoch(), None),
child
.epoch_vote_accounts(epoch)
.unwrap()
@@ -9050,7 +8978,7 @@ pub(crate) mod tests {
);
assert!(child.epoch_vote_accounts(epoch).is_some());
assert_eq!(
leader_stake.stake(child.epoch(), None, true),
leader_stake.stake(child.epoch(), None),
child
.epoch_vote_accounts(epoch)
.unwrap()
@@ -12980,26 +12908,6 @@ pub(crate) mod tests {
}
}
#[test]
fn test_stake_rewrite() {
let GenesisConfigInfo { genesis_config, .. } =
create_genesis_config_with_leader(500, &solana_sdk::pubkey::new_rand(), 1);
let bank = Arc::new(Bank::new(&genesis_config));
// quickest way of creting bad stake account
let bootstrap_stake_pubkey = bank
.cloned_stake_delegations()
.keys()
.next()
.copied()
.unwrap();
let mut bootstrap_stake_account = bank.get_account(&bootstrap_stake_pubkey).unwrap();
bootstrap_stake_account.set_lamports(10000000);
bank.store_account(&bootstrap_stake_pubkey, &bootstrap_stake_account);
assert_eq!(bank.rewrite_stakes(), (1, 1));
}
#[test]
fn test_get_inflation_start_slot_devnet_testnet() {
let GenesisConfigInfo {

View File

@@ -37,7 +37,7 @@ impl Stakes {
pub fn history(&self) -> &StakeHistory {
&self.stake_history
}
pub fn clone_with_epoch(&self, next_epoch: Epoch, fix_stake_deactivate: bool) -> Self {
pub fn clone_with_epoch(&self, next_epoch: Epoch) -> Self {
let prev_epoch = self.epoch;
if prev_epoch == next_epoch {
self.clone()
@@ -52,7 +52,6 @@ impl Stakes {
.iter()
.map(|(_pubkey, stake_delegation)| stake_delegation),
Some(&self.stake_history),
fix_stake_deactivate,
),
);
@@ -65,7 +64,6 @@ impl Stakes {
pubkey,
next_epoch,
Some(&stake_history_upto_prev_epoch),
fix_stake_deactivate,
);
(*pubkey, (stake, account.clone()))
})
@@ -87,13 +85,12 @@ impl Stakes {
voter_pubkey: &Pubkey,
epoch: Epoch,
stake_history: Option<&StakeHistory>,
fix_stake_deactivate: bool,
) -> u64 {
self.stake_delegations
.iter()
.map(|(_, stake_delegation)| {
if &stake_delegation.voter_pubkey == voter_pubkey {
stake_delegation.stake(epoch, stake_history, fix_stake_deactivate)
stake_delegation.stake(epoch, stake_history)
} else {
0
}
@@ -123,7 +120,6 @@ impl Stakes {
&mut self,
pubkey: &Pubkey,
account: &AccountSharedData,
fix_stake_deactivate: bool,
check_vote_init: bool,
) -> Option<ArcVoteAccount> {
if solana_vote_program::check_id(account.owner()) {
@@ -137,14 +133,7 @@ impl Stakes {
&& !(check_vote_init && VoteState::is_uninitialized_no_deser(account.data()))
{
let stake = old.as_ref().map_or_else(
|| {
self.calculate_stake(
pubkey,
self.epoch,
Some(&self.stake_history),
fix_stake_deactivate,
)
},
|| self.calculate_stake(pubkey, self.epoch, Some(&self.stake_history)),
|v| v.0,
);
@@ -157,7 +146,7 @@ impl Stakes {
let old_stake = self.stake_delegations.get(pubkey).map(|delegation| {
(
delegation.voter_pubkey,
delegation.stake(self.epoch, Some(&self.stake_history), fix_stake_deactivate),
delegation.stake(self.epoch, Some(&self.stake_history)),
)
});
@@ -167,11 +156,7 @@ impl Stakes {
(
delegation.voter_pubkey,
if account.lamports() != 0 {
delegation.stake(
self.epoch,
Some(&self.stake_history),
fix_stake_deactivate,
)
delegation.stake(self.epoch, Some(&self.stake_history))
} else {
// when account is removed (lamports == 0), this special `else` clause ensures
// resetting cached stake value below, even if the account happens to be
@@ -310,44 +295,44 @@ pub mod tests {
let ((vote_pubkey, vote_account), (stake_pubkey, mut stake_account)) =
create_staked_node_accounts(10);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&stake_pubkey, &stake_account, true);
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
assert_eq!(
vote_accounts.get(&vote_pubkey).unwrap().0,
stake.stake(i, None, true)
stake.stake(i, None)
);
}
stake_account.set_lamports(42);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true);
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
assert_eq!(
vote_accounts.get(&vote_pubkey).unwrap().0,
stake.stake(i, None, true)
stake.stake(i, None)
); // stays old stake, because only 10 is activated
}
// activate more
let (_stake_pubkey, mut stake_account) = create_stake_account(42, &vote_pubkey);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true);
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
assert_eq!(
vote_accounts.get(&vote_pubkey).unwrap().0,
stake.stake(i, None, true)
stake.stake(i, None)
); // now stake of 42 is activated
}
stake_account.set_lamports(0);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true);
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
@@ -365,14 +350,14 @@ pub mod tests {
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
create_staked_node_accounts(10);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&stake_pubkey, &stake_account, true);
let ((vote11_pubkey, vote11_account), (stake11_pubkey, stake11_account)) =
create_staked_node_accounts(20);
stakes.store(&vote11_pubkey, &vote11_account, true, true);
stakes.store(&stake11_pubkey, &stake11_account, true, true);
stakes.store(&vote11_pubkey, &vote11_account, true);
stakes.store(&stake11_pubkey, &stake11_account, true);
let vote11_node_pubkey = VoteState::from(&vote11_account).unwrap().node_pubkey;
@@ -389,8 +374,8 @@ pub mod tests {
let ((vote_pubkey, mut vote_account), (stake_pubkey, stake_account)) =
create_staked_node_accounts(10);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&stake_pubkey, &stake_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -399,7 +384,7 @@ pub mod tests {
}
vote_account.set_lamports(0);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -407,7 +392,7 @@ pub mod tests {
}
vote_account.set_lamports(1);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -420,7 +405,7 @@ pub mod tests {
let mut pushed = vote_account.data().to_vec();
pushed.push(0);
vote_account.set_data(pushed);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -431,7 +416,7 @@ pub mod tests {
let default_vote_state = VoteState::default();
let versioned = VoteStateVersions::new_current(default_vote_state);
VoteState::to(&versioned, &mut vote_account).unwrap();
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -439,7 +424,7 @@ pub mod tests {
}
vote_account.set_data(cache_data);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -461,11 +446,11 @@ pub mod tests {
let ((vote_pubkey2, vote_account2), (_stake_pubkey2, stake_account2)) =
create_staked_node_accounts(10);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey2, &vote_account2, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&vote_pubkey2, &vote_account2, true);
// delegates to vote_pubkey
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true);
let stake = stake_state::stake_from(&stake_account).unwrap();
@@ -474,14 +459,14 @@ pub mod tests {
assert!(vote_accounts.get(&vote_pubkey).is_some());
assert_eq!(
vote_accounts.get(&vote_pubkey).unwrap().0,
stake.stake(stakes.epoch, Some(&stakes.stake_history), true)
stake.stake(stakes.epoch, Some(&stakes.stake_history))
);
assert!(vote_accounts.get(&vote_pubkey2).is_some());
assert_eq!(vote_accounts.get(&vote_pubkey2).unwrap().0, 0);
}
// delegates to vote_pubkey2
stakes.store(&stake_pubkey, &stake_account2, true, true);
stakes.store(&stake_pubkey, &stake_account2, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -490,7 +475,7 @@ pub mod tests {
assert!(vote_accounts.get(&vote_pubkey2).is_some());
assert_eq!(
vote_accounts.get(&vote_pubkey2).unwrap().0,
stake.stake(stakes.epoch, Some(&stakes.stake_history), true)
stake.stake(stakes.epoch, Some(&stakes.stake_history))
);
}
}
@@ -506,11 +491,11 @@ pub mod tests {
let (stake_pubkey2, stake_account2) = create_stake_account(10, &vote_pubkey);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
// delegates to vote_pubkey
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&stake_pubkey2, &stake_account2, true, true);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&stake_pubkey2, &stake_account2, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -525,23 +510,23 @@ pub mod tests {
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
create_staked_node_accounts(10);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&stake_pubkey, &stake_account, true);
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
assert_eq!(
vote_accounts.get(&vote_pubkey).unwrap().0,
stake.stake(stakes.epoch, Some(&stakes.stake_history), true)
stake.stake(stakes.epoch, Some(&stakes.stake_history))
);
}
let stakes = stakes.clone_with_epoch(3, true);
let stakes = stakes.clone_with_epoch(3);
{
let vote_accounts = stakes.vote_accounts();
assert_eq!(
vote_accounts.get(&vote_pubkey).unwrap().0,
stake.stake(stakes.epoch, Some(&stakes.stake_history), true)
stake.stake(stakes.epoch, Some(&stakes.stake_history))
);
}
}
@@ -556,8 +541,8 @@ pub mod tests {
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
create_staked_node_accounts(10);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&stake_pubkey, &stake_account, true);
{
let vote_accounts = stakes.vote_accounts();
@@ -570,7 +555,6 @@ pub mod tests {
&stake_pubkey,
&AccountSharedData::new(1, 0, &stake::program::id()),
true,
true,
);
{
let vote_accounts = stakes.vote_accounts();
@@ -600,14 +584,14 @@ pub mod tests {
let genesis_epoch = 0;
let ((vote_pubkey, vote_account), (stake_pubkey, stake_account)) =
create_warming_staked_node_accounts(10, genesis_epoch);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&stake_pubkey, &stake_account, true);
assert_eq!(stakes.vote_balance_and_staked(), 11);
assert_eq!(stakes.vote_balance_and_warmed_staked(), 1);
for (epoch, expected_warmed_stake) in ((genesis_epoch + 1)..=3).zip(&[2, 3, 4]) {
stakes = stakes.clone_with_epoch(epoch, true);
stakes = stakes.clone_with_epoch(epoch);
// vote_balance_and_staked() always remain to return same lamports
// while vote_balance_and_warmed_staked() gradually increases
assert_eq!(stakes.vote_balance_and_staked(), 11);