From e26840cb09465352067aa037b2eee7591091529b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 13 May 2020 04:10:18 -0700 Subject: [PATCH] Introduce type alias Ancestors (bp #9699) (#10019) automerge --- runtime/src/accounts.rs | 30 +++++++++++------------------- runtime/src/accounts_db.rs | 28 ++++++++++------------------ runtime/src/accounts_index.rs | 7 ++++--- runtime/src/bank/mod.rs | 7 ++++--- runtime/src/status_cache.rs | 6 ++++-- 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index 8ff410a1ea..c33ca3bcfc 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -2,7 +2,7 @@ use crate::{ accounts_db::{ AccountInfo, AccountStorage, AccountsDB, AppendVecId, BankHashInfo, ErrorCounters, }, - accounts_index::AccountsIndex, + accounts_index::{AccountsIndex, Ancestors}, append_vec::StoredAccount, bank::{HashAgeKind, TransactionProcessResult}, blockhash_queue::BlockhashQueue, @@ -81,7 +81,7 @@ impl Accounts { pub fn new_with_frozen_accounts( paths: Vec, - ancestors: &HashMap, + ancestors: &Ancestors, frozen_account_pubkeys: &[Pubkey], ) -> Self { let mut accounts = Accounts { @@ -94,11 +94,7 @@ impl Accounts { accounts } - pub fn freeze_accounts( - &mut self, - ancestors: &HashMap, - frozen_account_pubkeys: &[Pubkey], - ) { + pub fn freeze_accounts(&mut self, ancestors: &Ancestors, frozen_account_pubkeys: &[Pubkey]) { Arc::get_mut(&mut self.accounts_db) .unwrap() .freeze_accounts(ancestors, frozen_account_pubkeys); @@ -106,7 +102,7 @@ impl Accounts { pub fn from_stream>( account_paths: &[PathBuf], - ancestors: &HashMap, + ancestors: &Ancestors, frozen_account_pubkeys: &[Pubkey], stream: &mut BufReader, stream_append_vecs_path: P, @@ -139,7 +135,7 @@ impl Accounts { fn load_tx_accounts( &self, storage: &AccountStorage, - ancestors: &HashMap, + ancestors: &Ancestors, accounts_index: &AccountsIndex, tx: &Transaction, fee: u64, @@ -221,7 +217,7 @@ impl Accounts { fn load_executable_accounts( storage: &AccountStorage, - ancestors: &HashMap, + ancestors: &Ancestors, accounts_index: &AccountsIndex, program_id: &Pubkey, error_counters: &mut ErrorCounters, @@ -266,7 +262,7 @@ impl Accounts { /// For each program_id in the transaction, load its loaders. fn load_loaders( storage: &AccountStorage, - ancestors: &HashMap, + ancestors: &Ancestors, accounts_index: &AccountsIndex, tx: &Transaction, error_counters: &mut ErrorCounters, @@ -294,7 +290,7 @@ impl Accounts { pub fn load_accounts( &self, - ancestors: &HashMap, + ancestors: &Ancestors, txs: &[Transaction], txs_iteration_order: Option<&[usize]>, lock_results: Vec, @@ -358,11 +354,7 @@ impl Accounts { } /// Slow because lock is held for 1 operation instead of many - pub fn load_slow( - &self, - ancestors: &HashMap, - pubkey: &Pubkey, - ) -> Option<(Account, Slot)> { + pub fn load_slow(&self, ancestors: &Ancestors, pubkey: &Pubkey) -> Option<(Account, Slot)> { let (account, slot) = self .accounts_db .load_slow(ancestors, pubkey) @@ -453,7 +445,7 @@ impl Accounts { } #[must_use] - pub fn verify_bank_hash(&self, slot: Slot, ancestors: &HashMap) -> bool { + pub fn verify_bank_hash(&self, slot: Slot, ancestors: &Ancestors) -> bool { if let Err(err) = self.accounts_db.verify_bank_hash(slot, ancestors) { warn!("verify_bank_hash failed: {:?}", err); false @@ -464,7 +456,7 @@ impl Accounts { pub fn load_by_program( &self, - ancestors: &HashMap, + ancestors: &Ancestors, program_id: Option<&Pubkey>, ) -> Vec<(Pubkey, Account)> { self.accounts_db.scan_accounts( diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 7b5d9d934c..145d28cc3c 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -19,7 +19,7 @@ //! commit for each slot entry would be indexed. use crate::{ - accounts_index::AccountsIndex, + accounts_index::{AccountsIndex, Ancestors}, append_vec::{AppendVec, StoredAccount, StoredMeta}, bank::deserialize_from_snapshot, }; @@ -848,7 +848,7 @@ impl AccountsDB { purge_slots.stop(); } - pub fn scan_accounts(&self, ancestors: &HashMap, scan_func: F) -> A + pub fn scan_accounts(&self, ancestors: &Ancestors, scan_func: F) -> A where F: Fn(&mut A, Option<(&Pubkey, Account, Slot)>) -> (), A: Default, @@ -930,7 +930,7 @@ impl AccountsDB { pub fn load( storage: &AccountStorage, - ancestors: &HashMap, + ancestors: &Ancestors, accounts_index: &AccountsIndex, pubkey: &Pubkey, ) -> Option<(Account, Slot)> { @@ -948,11 +948,7 @@ impl AccountsDB { } } - pub fn load_slow( - &self, - ancestors: &HashMap, - pubkey: &Pubkey, - ) -> Option<(Account, Slot)> { + pub fn load_slow(&self, ancestors: &Ancestors, pubkey: &Pubkey) -> Option<(Account, Slot)> { let accounts_index = self.accounts_index.read().unwrap(); let storage = self.storage.read().unwrap(); Self::load(&storage, ancestors, &accounts_index, pubkey) @@ -1255,7 +1251,7 @@ impl AccountsDB { fn calculate_accounts_hash( &self, - ancestors: &HashMap, + ancestors: &Ancestors, check_hash: bool, ) -> Result { use BankHashVerificationError::*; @@ -1317,7 +1313,7 @@ impl AccountsDB { bank_hash_info.snapshot_hash } - pub fn update_accounts_hash(&self, slot: Slot, ancestors: &HashMap) -> Hash { + pub fn update_accounts_hash(&self, slot: Slot, ancestors: &Ancestors) -> Hash { let hash = self.calculate_accounts_hash(ancestors, false).unwrap(); let mut bank_hashes = self.bank_hashes.write().unwrap(); let mut bank_hash_info = bank_hashes.get_mut(&slot).unwrap(); @@ -1328,7 +1324,7 @@ impl AccountsDB { pub fn verify_bank_hash( &self, slot: Slot, - ancestors: &HashMap, + ancestors: &Ancestors, ) -> Result<(), BankHashVerificationError> { use BankHashVerificationError::*; @@ -1502,11 +1498,7 @@ impl AccountsDB { hashes } - pub fn freeze_accounts( - &mut self, - ancestors: &HashMap, - account_pubkeys: &[Pubkey], - ) { + pub fn freeze_accounts(&mut self, ancestors: &Ancestors, account_pubkeys: &[Pubkey]) { for account_pubkey in account_pubkeys { if let Some((account, _slot)) = self.load_slow(ancestors, &account_pubkey) { let frozen_account_info = FrozenAccountInfo { @@ -1712,8 +1704,8 @@ pub mod tests { use std::{fs, str::FromStr}; use tempfile::TempDir; - fn linear_ancestors(end_slot: u64) -> HashMap { - let mut ancestors: HashMap = vec![(0, 0)].into_iter().collect(); + fn linear_ancestors(end_slot: u64) -> Ancestors { + let mut ancestors: Ancestors = vec![(0, 0)].into_iter().collect(); for i in 1..end_slot { ancestors.insert(i, (i - 1) as usize); } diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index d9d69f0325..fafd1f00b9 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -6,6 +6,7 @@ use std::{ pub type Slot = u64; type SlotList = Vec<(Slot, T)>; +pub type Ancestors = HashMap; pub type RefCount = u64; type AccountMapEntry = (RefCount, SlotList); @@ -19,7 +20,7 @@ pub struct AccountsIndex { impl AccountsIndex { /// call func with every pubkey and index visible from a given set of ancestors - pub fn scan_accounts(&self, ancestors: &HashMap, mut func: F) + pub fn scan_accounts(&self, ancestors: &Ancestors, mut func: F) where F: FnMut(&Pubkey, (&T, Slot)) -> (), { @@ -54,7 +55,7 @@ impl AccountsIndex { // find the latest slot and T in a list for a given ancestor // returns index into 'list' if found, None if not. - fn latest_slot(&self, ancestors: &HashMap, list: &[(Slot, T)]) -> Option { + fn latest_slot(&self, ancestors: &Ancestors, list: &[(Slot, T)]) -> Option { let mut max = 0; let mut rv = None; for (i, (slot, _t)) in list.iter().rev().enumerate() { @@ -71,7 +72,7 @@ impl AccountsIndex { pub fn get( &self, pubkey: &Pubkey, - ancestors: &HashMap, + ancestors: &Ancestors, ) -> Option<(RwLockReadGuard>, usize)> { self.account_maps.get(pubkey).and_then(|list| { let list_r = list.read().unwrap(); diff --git a/runtime/src/bank/mod.rs b/runtime/src/bank/mod.rs index 1624f65107..aff223e449 100644 --- a/runtime/src/bank/mod.rs +++ b/runtime/src/bank/mod.rs @@ -8,6 +8,7 @@ use crate::{ TransactionLoaders, }, accounts_db::{AccountsDBSerialize, ErrorCounters, SnapshotStorage, SnapshotStorages}, + accounts_index::Ancestors, blockhash_queue::BlockhashQueue, epoch_stakes::{EpochStakes, NodeVoteAccounts}, message_processor::{MessageProcessor, ProcessInstruction}, @@ -95,7 +96,7 @@ impl BankRc { pub fn from_stream>( account_paths: &[PathBuf], slot: Slot, - ancestors: &HashMap, + ancestors: &Ancestors, frozen_account_pubkeys: &[Pubkey], mut stream: &mut BufReader, stream_append_vecs_path: P, @@ -232,7 +233,7 @@ pub struct Bank { blockhash_queue: RwLock, /// The set of parents including this bank - pub ancestors: HashMap, + pub ancestors: Ancestors, /// Hash of this Bank's state. Only meaningful after freezing. hash: RwLock, @@ -1841,7 +1842,7 @@ impl Bank { } pub fn get_account_modified_since_parent(&self, pubkey: &Pubkey) -> Option<(Account, Slot)> { - let just_self: HashMap = vec![(self.slot(), 0)].into_iter().collect(); + let just_self: Ancestors = vec![(self.slot(), 0)].into_iter().collect(); if let Some((account, slot)) = self.rc.accounts.load_slow(&just_self, pubkey) { if slot == self.slot() { return Some((account, slot)); diff --git a/runtime/src/status_cache.rs b/runtime/src/status_cache.rs index b6ae088965..4885e5c5c5 100644 --- a/runtime/src/status_cache.rs +++ b/runtime/src/status_cache.rs @@ -1,3 +1,5 @@ +use crate::accounts_index::Ancestors; + use log::*; use rand::{thread_rng, Rng}; use serde::Serialize; @@ -85,7 +87,7 @@ impl StatusCache { &self, sig: &Signature, transaction_blockhash: &Hash, - ancestors: &HashMap, + ancestors: &Ancestors, ) -> Option<(Slot, T)> { let map = self.cache.get(transaction_blockhash)?; let (_, index, sigmap) = map; @@ -106,7 +108,7 @@ impl StatusCache { pub fn get_signature_slot( &self, signature: &Signature, - ancestors: &HashMap, + ancestors: &Ancestors, ) -> Option<(Slot, T)> { let mut keys = vec![]; let mut val: Vec<_> = self.cache.iter().map(|(k, _)| *k).collect();