From ae750bb16b2591d3fcb1ceaf23b438bedbd0ab9d Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Mon, 11 Mar 2019 17:58:21 -0700 Subject: [PATCH] Filter vote accounts with no delegate from being selected in Rotation (#3224) --- core/src/staking_utils.rs | 23 +++++++++++++++++++---- core/src/voting_keypair.rs | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/core/src/staking_utils.rs b/core/src/staking_utils.rs index bab6474a42..48e894c39e 100644 --- a/core/src/staking_utils.rs +++ b/core/src/staking_utils.rs @@ -63,12 +63,21 @@ fn node_staked_accounts_at_epoch( epoch_height: u64, ) -> Option> { bank.epoch_vote_accounts(epoch_height).map(|epoch_state| { - epoch_state.into_iter().filter_map(|(account_id, account)| { - filter_zero_balances(account).map(|stake| (account_id, stake, account)) - }) + epoch_state + .into_iter() + .filter_map(|(account_id, account)| { + filter_zero_balances(account).map(|stake| (account_id, stake, account)) + }) + .filter(|(account_id, _, account)| filter_no_delegate(account_id, account)) }) } +fn filter_no_delegate(account_id: &Pubkey, account: &Account) -> bool { + VoteState::deserialize(&account.userdata) + .map(|vote_state| vote_state.delegate_id != *account_id) + .unwrap_or(false) +} + fn filter_zero_balances(account: &Account) -> Option { let balance = Bank::read_balance(&account); if balance > 0 { @@ -189,7 +198,13 @@ mod tests { // Make a mint vote account. Because the mint has nonzero stake, this // should show up in the active set - voting_keypair_tests::new_vote_account_with_vote(&mint_keypair, &bank_voter, &bank, 499, 0); + voting_keypair_tests::new_vote_account_with_delegate( + &mint_keypair, + &bank_voter, + &mint_keypair.pubkey(), + &bank, + 499, + ); // soonest slot that could be a new epoch is 1 let mut slot = 1; diff --git a/core/src/voting_keypair.rs b/core/src/voting_keypair.rs index cd97d596f3..95ee51da2b 100644 --- a/core/src/voting_keypair.rs +++ b/core/src/voting_keypair.rs @@ -120,6 +120,25 @@ pub mod tests { bank.process_transaction(&tx).unwrap(); } + pub fn new_vote_account_with_delegate( + from_keypair: &Keypair, + voting_keypair: &Keypair, + delegate: &Pubkey, + bank: &Bank, + lamports: u64, + ) { + let blockhash = bank.last_blockhash(); + let tx = VoteTransaction::new_account_with_delegate( + from_keypair, + voting_keypair, + delegate, + blockhash, + lamports, + 0, + ); + bank.process_transaction(&tx).unwrap(); + } + pub fn push_vote(voting_keypair: &T, bank: &Bank, slot: u64) { let blockhash = bank.last_blockhash(); let tx =