Rpc: add getLargestAccounts endpoint (#9869) (#9876)

automerge
This commit is contained in:
mergify[bot]
2020-05-04 19:12:18 -07:00
committed by GitHub
parent f951d7d33f
commit 2acf4d874d
5 changed files with 174 additions and 4 deletions

View File

@@ -58,6 +58,11 @@ pub type TransactionLoaders = Vec<Vec<(Pubkey, Account)>>;
pub type TransactionLoadResult = (TransactionAccounts, TransactionLoaders, TransactionRent);
pub enum AccountAddressFilter {
Exclude, // exclude all addresses matching the fiter
Include, // only include addresses matching the filter
}
impl Accounts {
pub fn new(paths: Vec<PathBuf>) -> Self {
Self::new_with_frozen_accounts(paths, &HashMap::default(), &[])
@@ -414,6 +419,39 @@ impl Accounts {
})
}
pub fn load_largest_accounts(
&self,
ancestors: &HashMap<Slot, usize>,
num: usize,
filter_by_address: &HashSet<Pubkey>,
filter: AccountAddressFilter,
) -> Vec<(Pubkey, u64)> {
let mut accounts_balances = self.accounts_db.scan_accounts(
ancestors,
|collector: &mut Vec<(Pubkey, u64)>, option| {
if let Some(data) = option
.filter(|(pubkey, account, _)| {
let should_include_pubkey = match filter {
AccountAddressFilter::Exclude => !filter_by_address.contains(&pubkey),
AccountAddressFilter::Include => filter_by_address.contains(&pubkey),
};
should_include_pubkey
&& account.lamports != 0
&& !(account.lamports == std::u64::MAX
&& account.owner == solana_storage_program::id())
})
.map(|(pubkey, account, _slot)| (*pubkey, account.lamports))
{
collector.push(data)
}
},
);
accounts_balances.sort_by(|a, b| a.1.cmp(&b.1).reverse());
accounts_balances.truncate(num);
accounts_balances
}
#[must_use]
pub fn verify_bank_hash(&self, slot: Slot, ancestors: &HashMap<Slot, usize>) -> bool {
if let Err(err) = self.accounts_db.verify_bank_hash(slot, ancestors) {

View File

@@ -3,7 +3,10 @@
//! on behalf of the caller, and a low-level API for when they have
//! already been signed and verified.
use crate::{
accounts::{Accounts, TransactionAccounts, TransactionLoadResult, TransactionLoaders},
accounts::{
AccountAddressFilter, Accounts, TransactionAccounts, TransactionLoadResult,
TransactionLoaders,
},
accounts_db::{AccountsDBSerialize, ErrorCounters, SnapshotStorage, SnapshotStorages},
blockhash_queue::BlockhashQueue,
epoch_stakes::{EpochStakes, NodeVoteAccounts},
@@ -54,7 +57,7 @@ use solana_stake_program::stake_state::{self, Delegation};
use solana_vote_program::vote_state::VoteState;
use std::{
cell::RefCell,
collections::HashMap,
collections::{HashMap, HashSet},
io::{BufReader, Cursor, Error as IOError, Read},
path::{Path, PathBuf},
rc::Rc,
@@ -1813,6 +1816,17 @@ impl Bank {
None
}
pub fn get_largest_accounts(
&self,
num: usize,
filter_by_address: &HashSet<Pubkey>,
filter: AccountAddressFilter,
) -> Vec<(Pubkey, u64)> {
self.rc
.accounts
.load_largest_accounts(&self.ancestors, num, filter_by_address, filter)
}
pub fn transaction_count(&self) -> u64 {
self.transaction_count.load(Ordering::Relaxed)
}