Refactor Vote Program Account setup (#2992)
This commit is contained in:
@ -246,13 +246,19 @@ impl AccountsDB {
|
||||
)))
|
||||
}
|
||||
|
||||
fn get_vote_accounts(&self, fork: Fork) -> Vec<Account> {
|
||||
fn get_vote_accounts(&self, fork: Fork) -> HashMap<Pubkey, Account> {
|
||||
self.index_info
|
||||
.vote_index
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|pubkey| self.load(fork, pubkey, true))
|
||||
.filter_map(|pubkey| {
|
||||
if let Some(account) = self.load(fork, pubkey, true) {
|
||||
Some((*pubkey, account))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@ -886,11 +892,11 @@ impl Accounts {
|
||||
self.accounts_db.squash(fork);
|
||||
}
|
||||
|
||||
pub fn get_vote_accounts(&self, fork: Fork) -> Vec<Account> {
|
||||
pub fn get_vote_accounts(&self, fork: Fork) -> HashMap<Pubkey, Account> {
|
||||
self.accounts_db
|
||||
.get_vote_accounts(fork)
|
||||
.into_iter()
|
||||
.filter(|acc| acc.tokens != 0)
|
||||
.filter(|(_, acc)| acc.tokens != 0)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
@ -1605,7 +1611,7 @@ mod tests {
|
||||
create_account(&accounts_db, &mut pubkeys, 100, 6);
|
||||
let accounts = accounts_db.get_vote_accounts(0);
|
||||
assert_eq!(accounts.len(), 6);
|
||||
accounts.iter().for_each(|account| {
|
||||
accounts.iter().for_each(|(_, account)| {
|
||||
assert_eq!(account.owner, vote_program::id());
|
||||
});
|
||||
let lastkey = Keypair::new().pubkey();
|
||||
|
@ -8,6 +8,7 @@ use crate::last_id_queue::LastIdQueue;
|
||||
use crate::runtime::{self, RuntimeError};
|
||||
use crate::status_cache::StatusCache;
|
||||
use bincode::serialize;
|
||||
use hashbrown::HashMap;
|
||||
use log::*;
|
||||
use solana_metrics::counter::Counter;
|
||||
use solana_sdk::account::Account;
|
||||
@ -225,10 +226,7 @@ impl Bank {
|
||||
executable: false,
|
||||
};
|
||||
|
||||
let mut vote_state = VoteState::new(
|
||||
genesis_block.bootstrap_leader_id,
|
||||
genesis_block.bootstrap_leader_id,
|
||||
);
|
||||
let mut vote_state = VoteState::new(genesis_block.bootstrap_leader_id);
|
||||
vote_state
|
||||
.votes
|
||||
.push_back(vote_program::Lockout::new(&vote_program::Vote::new(0)));
|
||||
@ -717,17 +715,17 @@ impl Bank {
|
||||
self.slots_per_epoch
|
||||
}
|
||||
|
||||
pub fn vote_states<F>(&self, cond: F) -> Vec<VoteState>
|
||||
pub fn vote_states<F>(&self, cond: F) -> HashMap<Pubkey, VoteState>
|
||||
where
|
||||
F: Fn(&VoteState) -> bool,
|
||||
F: Fn(&Pubkey, &VoteState) -> bool,
|
||||
{
|
||||
self.accounts()
|
||||
.get_vote_accounts(self.id)
|
||||
.iter()
|
||||
.filter_map(|account| {
|
||||
.filter_map(|(p, account)| {
|
||||
if let Ok(vote_state) = VoteState::deserialize(&account.userdata) {
|
||||
if cond(&vote_state) {
|
||||
return Some(vote_state);
|
||||
if cond(&p, &vote_state) {
|
||||
return Some((*p, vote_state));
|
||||
}
|
||||
}
|
||||
None
|
||||
|
Reference in New Issue
Block a user