Refactor bank get vote accounts (#3052)

This commit is contained in:
Sagar Dhawan
2019-03-02 13:23:55 -08:00
committed by Michael Vines
parent f4c5b9ccb0
commit d22a13257e
7 changed files with 46 additions and 101 deletions

View File

@@ -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]