Skip to content

Commit

Permalink
runtime: remove inactive delegation from stakes cache
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson committed Sep 23, 2021
1 parent 476124d commit 7b365c5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
7 changes: 7 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4449,6 +4449,7 @@ impl Bank {
pubkey,
account,
self.check_init_vote_data_enabled(),
self.stakes_remove_delegation_if_inactive_enabled(),
);
}
}
Expand Down Expand Up @@ -5170,6 +5171,7 @@ impl Bank {
pubkey,
account,
self.check_init_vote_data_enabled(),
self.stakes_remove_delegation_if_inactive_enabled(),
) {
// TODO: one of the indices is redundant.
overwritten_vote_accounts.push(OverwrittenVoteAccount {
Expand Down Expand Up @@ -5412,6 +5414,11 @@ impl Bank {
.is_active(&feature_set::demote_program_write_locks::id())
}

pub fn stakes_remove_delegation_if_inactive_enabled(&self) -> bool {
self.feature_set
.is_active(&feature_set::stakes_remove_delegation_if_inactive::id())
}

// Check if the wallclock time from bank creation to now has exceeded the allotted
// time for transaction processing
pub fn should_bank_still_be_processing_txs(
Expand Down
68 changes: 38 additions & 30 deletions runtime/src/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl Stakes {
pubkey: &Pubkey,
account: &AccountSharedData,
check_vote_init: bool,
remove_delegation_on_inactive: bool,
) -> Option<VoteAccount> {
if solana_vote_program::check_id(account.owner()) {
// unconditionally remove existing at first; there is no dependent calculated state for
Expand Down Expand Up @@ -178,7 +179,13 @@ impl Stakes {
}
}

if account.lamports() == 0 {
let remove_delegation = if remove_delegation_on_inactive {
delegation.is_none()
} else {
account.lamports() == 0

This comment has been minimized.

Copy link
@behzadnouri

behzadnouri Apr 10, 2022

Contributor

@t-nelson Is there any scenario that we don't want to remove the delegation from the stakes-cache when account's lamports == 0?

};

if remove_delegation {
// when account is removed (lamports == 0), remove it from Stakes as well
// so that given `pubkey` can be used for any owner in the future, while not
// affecting Stakes.
Expand Down Expand Up @@ -297,8 +304,8 @@ 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);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -310,7 +317,7 @@ pub mod tests {
}

stake_account.set_lamports(42);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
Expand All @@ -322,7 +329,7 @@ pub mod tests {

// activate more
let (_stake_pubkey, mut stake_account) = create_stake_account(42, &vote_pubkey);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = stake_state::stake_from(&stake_account).unwrap();
{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -334,7 +341,7 @@ pub mod tests {
}

stake_account.set_lamports(0);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_some());
Expand All @@ -352,14 +359,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);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);

let ((vote11_pubkey, vote11_account), (stake11_pubkey, stake11_account)) =
create_staked_node_accounts(20);

stakes.store(&vote11_pubkey, &vote11_account, true);
stakes.store(&stake11_pubkey, &stake11_account, true);
stakes.store(&vote11_pubkey, &vote11_account, true, true);
stakes.store(&stake11_pubkey, &stake11_account, true, true);

let vote11_node_pubkey = VoteState::from(&vote11_account).unwrap().node_pubkey;

Expand All @@ -376,8 +383,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);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -386,15 +393,15 @@ pub mod tests {
}

vote_account.set_lamports(0);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_none());
}

vote_account.set_lamports(1);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -407,7 +414,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);
stakes.store(&vote_pubkey, &vote_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -418,15 +425,15 @@ 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);
stakes.store(&vote_pubkey, &vote_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
assert!(vote_accounts.get(&vote_pubkey).is_none());
}

vote_account.set_data(cache_data);
stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -448,11 +455,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);
stakes.store(&vote_pubkey2, &vote_account2, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&vote_pubkey2, &vote_account2, true, true);

// delegates to vote_pubkey
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&stake_pubkey, &stake_account, true, true);

let stake = stake_state::stake_from(&stake_account).unwrap();

Expand All @@ -468,7 +475,7 @@ pub mod tests {
}

// delegates to vote_pubkey2
stakes.store(&stake_pubkey, &stake_account2, true);
stakes.store(&stake_pubkey, &stake_account2, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -493,11 +500,11 @@ pub mod tests {

let (stake_pubkey2, stake_account2) = create_stake_account(10, &vote_pubkey);

stakes.store(&vote_pubkey, &vote_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);

// delegates to vote_pubkey
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&stake_pubkey2, &stake_account2, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
stakes.store(&stake_pubkey2, &stake_account2, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -512,8 +519,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);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);
let stake = stake_state::stake_from(&stake_account).unwrap();

{
Expand Down Expand Up @@ -543,8 +550,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);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);

{
let vote_accounts = stakes.vote_accounts();
Expand All @@ -557,6 +564,7 @@ pub mod tests {
&stake_pubkey,
&AccountSharedData::new(1, 0, &stake::program::id()),
true,
true,
);
{
let vote_accounts = stakes.vote_accounts();
Expand Down Expand Up @@ -586,8 +594,8 @@ 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);
stakes.store(&stake_pubkey, &stake_account, true);
stakes.store(&vote_pubkey, &vote_account, true, true);
stakes.store(&stake_pubkey, &stake_account, true, true);

assert_eq!(stakes.vote_balance_and_staked(), 11);
assert_eq!(stakes.vote_balance_and_warmed_staked(), 1);
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/feature_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ pub mod sol_log_data_syscall_enabled {
solana_sdk::declare_id!("HYPs7jyJ3KwQFdDpuSzMtVKf1MLJDaZRv3CSWvfUqdFo");
}

pub mod stakes_remove_delegation_if_inactive {
solana_sdk::declare_id!("HFpdDDNQjvcXnXKec697HDDsyk6tFoWS2o8fkxuhQZpL");
}

lazy_static! {
/// Map of feature identifiers to user-visible description
pub static ref FEATURE_NAMES: HashMap<Pubkey, &'static str> = [
Expand Down Expand Up @@ -264,6 +268,7 @@ lazy_static! {
(fix_write_privs::id(), "fix native invoke write privileges"),
(reduce_required_deploy_balance::id(), "reduce required payer balance for program deploys"),
(sol_log_data_syscall_enabled::id(), "enable sol_log_data syscall"),
(stakes_remove_delegation_if_inactive::id(), "remove delegations from stakes cache when inactive"),
/*************** ADD NEW FEATURES HERE ***************/
]
.iter()
Expand Down

0 comments on commit 7b365c5

Please sign in to comment.