Refactor bank get vote accounts (#3052)
This commit is contained in:
committed by
Michael Vines
parent
f4c5b9ccb0
commit
d22a13257e
@ -906,12 +906,11 @@ impl Accounts {
|
||||
self.accounts_db.squash(fork);
|
||||
}
|
||||
|
||||
pub fn get_vote_accounts(&self, fork: Fork) -> HashMap<Pubkey, Account> {
|
||||
pub fn get_vote_accounts(&self, fork: Fork) -> impl Iterator<Item = (Pubkey, Account)> {
|
||||
self.accounts_db
|
||||
.get_vote_accounts(fork)
|
||||
.into_iter()
|
||||
.filter(|(_, acc)| acc.tokens != 0)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1600,26 +1599,31 @@ mod tests {
|
||||
|
||||
accounts.new_from_parent(1, 0);
|
||||
|
||||
assert_eq!(accounts.get_vote_accounts(1).len(), 1);
|
||||
let mut vote_accounts: Vec<_> = accounts.get_vote_accounts(1).collect();
|
||||
assert_eq!(vote_accounts.len(), 1);
|
||||
|
||||
vote_account.tokens = 0;
|
||||
accounts.store_slow(1, &key, &vote_account);
|
||||
|
||||
assert_eq!(accounts.get_vote_accounts(1).len(), 0);
|
||||
vote_accounts = accounts.get_vote_accounts(1).collect();
|
||||
assert_eq!(vote_accounts.len(), 0);
|
||||
|
||||
let mut vote_account1 = Account::new(2, 0, vote_program::id());
|
||||
let key1 = Keypair::new().pubkey();
|
||||
accounts.store_slow(1, &key1, &vote_account1);
|
||||
|
||||
accounts.squash(1);
|
||||
assert_eq!(accounts.get_vote_accounts(0).len(), 1);
|
||||
assert_eq!(accounts.get_vote_accounts(1).len(), 1);
|
||||
vote_accounts = accounts.get_vote_accounts(0).collect();
|
||||
assert_eq!(vote_accounts.len(), 1);
|
||||
vote_accounts = accounts.get_vote_accounts(1).collect();
|
||||
assert_eq!(vote_accounts.len(), 1);
|
||||
|
||||
vote_account1.tokens = 0;
|
||||
accounts.store_slow(1, &key1, &vote_account1);
|
||||
accounts.store_slow(0, &key, &vote_account);
|
||||
|
||||
assert_eq!(accounts.get_vote_accounts(1).len(), 0);
|
||||
vote_accounts = accounts.get_vote_accounts(1).collect();
|
||||
assert_eq!(vote_accounts.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -142,7 +142,7 @@ impl Bank {
|
||||
|
||||
// genesis needs stakes for all epochs up to the epoch implied by
|
||||
// slot = 0 and genesis configuration
|
||||
let vote_accounts = bank.vote_accounts(|k, v| Some((*k, v.clone())));
|
||||
let vote_accounts: HashMap<_, _> = bank.vote_accounts().collect();
|
||||
for i in 0..=bank.epoch_from_stakers_slot_offset() {
|
||||
bank.epoch_vote_accounts.insert(i, vote_accounts.clone());
|
||||
}
|
||||
@ -182,7 +182,7 @@ impl Bank {
|
||||
// if my parent didn't populate for this epoch, we've
|
||||
// crossed a boundary
|
||||
if epoch_vote_accounts.get(&epoch).is_none() {
|
||||
epoch_vote_accounts.insert(epoch, bank.vote_accounts(|k, v| Some((*k, v.clone()))));
|
||||
epoch_vote_accounts.insert(epoch, bank.vote_accounts().collect());
|
||||
}
|
||||
epoch_vote_accounts
|
||||
};
|
||||
@ -787,15 +787,8 @@ impl Bank {
|
||||
}
|
||||
|
||||
/// current vote accounts for this bank
|
||||
pub fn vote_accounts<F, T>(&self, filter: F) -> HashMap<Pubkey, T>
|
||||
where
|
||||
F: Fn(&Pubkey, &Account) -> Option<(Pubkey, T)>,
|
||||
{
|
||||
self.accounts()
|
||||
.get_vote_accounts(self.accounts_id)
|
||||
.iter()
|
||||
.filter_map(|(pubkey, account)| filter(pubkey, account))
|
||||
.collect()
|
||||
pub fn vote_accounts(&self) -> impl Iterator<Item = (Pubkey, Account)> {
|
||||
self.accounts().get_vote_accounts(self.accounts_id)
|
||||
}
|
||||
|
||||
/// vote accounts for the specific epoch
|
||||
@ -817,11 +810,10 @@ impl Bank {
|
||||
{
|
||||
self.accounts()
|
||||
.get_vote_accounts(self.accounts_id)
|
||||
.iter()
|
||||
.filter_map(|(p, account)| {
|
||||
if let Ok(vote_state) = VoteState::deserialize(&account.userdata) {
|
||||
if cond(&p, &vote_state) {
|
||||
return Some((*p, vote_state));
|
||||
return Some((p, vote_state));
|
||||
}
|
||||
}
|
||||
None
|
||||
|
Reference in New Issue
Block a user