diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 3074bb4f0e..71415bdc2e 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -24,8 +24,9 @@ use crate::{ accounts_hash::{AccountsHash, CalculateHashIntermediate, HashStats, PreviousPass}, accounts_index::{ AccountIndexGetResult, AccountSecondaryIndexes, AccountsIndex, AccountsIndexConfig, - AccountsIndexRootsStats, IndexKey, IsCached, RefCount, ScanResult, SlotList, SlotSlice, - ZeroLamport, ACCOUNTS_INDEX_CONFIG_FOR_BENCHMARKS, ACCOUNTS_INDEX_CONFIG_FOR_TESTING, + AccountsIndexRootsStats, IndexKey, IndexValue, IsCached, RefCount, ScanResult, SlotList, + SlotSlice, ZeroLamport, ACCOUNTS_INDEX_CONFIG_FOR_BENCHMARKS, + ACCOUNTS_INDEX_CONFIG_FOR_TESTING, }, ancestors::Ancestors, append_vec::{AppendVec, StoredAccountMeta, StoredMeta, StoredMetaWriteVersion}, @@ -278,6 +279,8 @@ impl IsCached for AccountInfo { } } +impl IndexValue for AccountInfo {} + impl ZeroLamport for AccountInfo { fn is_zero_lamport(&self) -> bool { self.lamports == 0 diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index cedcdb0e9a..4e8817cbb0 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -54,6 +54,11 @@ pub trait IsCached: fn is_cached(&self) -> bool; } +pub trait IndexValue: + 'static + IsCached + Clone + Debug + PartialEq + ZeroLamport + Copy + Default + Sync + Send +{ +} + #[derive(Error, Debug, PartialEq)] pub enum ScanError { #[error("Node detected it replayed bad version of slot {slot:?} with id {bank_id:?}, thus the scan on said slot was aborted")] @@ -117,7 +122,7 @@ pub struct AccountMapEntryInner { pub slot_list: RwLock>, } -impl AccountMapEntryInner { +impl AccountMapEntryInner { pub fn ref_count(&self) -> RefCount { self.ref_count.load(Ordering::Relaxed) } @@ -131,27 +136,27 @@ impl AccountMapEntryInner { } } -pub enum AccountIndexGetResult<'a, T: IsCached> { +pub enum AccountIndexGetResult<'a, T: IndexValue> { Found(ReadAccountMapEntry, usize), NotFoundOnFork, Missing(AccountMapsReadLock<'a, T>), } #[self_referencing] -pub struct ReadAccountMapEntry { +pub struct ReadAccountMapEntry { owned_entry: AccountMapEntry, #[borrows(owned_entry)] #[covariant] slot_list_guard: RwLockReadGuard<'this, SlotList>, } -impl Debug for ReadAccountMapEntry { +impl Debug for ReadAccountMapEntry { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{:?}", self.borrow_owned_entry()) } } -impl ReadAccountMapEntry { +impl ReadAccountMapEntry { pub fn from_account_map_entry(account_map_entry: AccountMapEntry) -> Self { ReadAccountMapEntryBuilder { owned_entry: account_map_entry, @@ -178,14 +183,14 @@ impl ReadAccountMapEntry { } #[self_referencing] -pub struct WriteAccountMapEntry { +pub struct WriteAccountMapEntry { owned_entry: AccountMapEntry, #[borrows(owned_entry)] #[covariant] slot_list_guard: RwLockWriteGuard<'this, SlotList>, } -impl WriteAccountMapEntry { +impl WriteAccountMapEntry { pub fn from_account_map_entry(account_map_entry: AccountMapEntry) -> Self { WriteAccountMapEntryBuilder { owned_entry: account_map_entry, @@ -522,7 +527,7 @@ pub struct AccountsIndexRootsStats { pub unrooted_cleaned_count: usize, } -pub struct AccountsIndexIterator<'a, T: IsCached> { +pub struct AccountsIndexIterator<'a, T: IndexValue> { account_maps: &'a LockMapTypeSlice, bin_calculator: &'a PubkeyBinCalculator16, start_bound: Bound, @@ -531,7 +536,7 @@ pub struct AccountsIndexIterator<'a, T: IsCached> { collect_all_unsorted: bool, } -impl<'a, T: IsCached> AccountsIndexIterator<'a, T> { +impl<'a, T: IndexValue> AccountsIndexIterator<'a, T> { fn range( map: &AccountMapsReadLock, range: R, @@ -615,7 +620,7 @@ impl<'a, T: IsCached> AccountsIndexIterator<'a, T> { } } -impl<'a, T: IsCached> Iterator for AccountsIndexIterator<'a, T> { +impl<'a, T: IndexValue> Iterator for AccountsIndexIterator<'a, T> { type Item = Vec<(Pubkey, AccountMapEntry)>; fn next(&mut self) -> Option { if self.is_finished { @@ -676,7 +681,7 @@ impl ScanSlotTracker { } #[derive(Debug)] -pub struct AccountsIndex { +pub struct AccountsIndex { pub account_maps: LockMapType, pub bin_calculator: PubkeyBinCalculator16, program_id_index: SecondaryIndex, @@ -699,7 +704,7 @@ pub struct AccountsIndex { storage: AccountsIndexStorage, } -impl AccountsIndex { +impl AccountsIndex { pub fn default_for_tests() -> Self { Self::new(Some(ACCOUNTS_INDEX_CONFIG_FOR_TESTING)) } @@ -1824,7 +1829,7 @@ pub mod tests { } } - impl<'a, T: IsCached> AccountIndexGetResult<'a, T> { + impl<'a, T: IndexValue> AccountIndexGetResult<'a, T> { pub fn unwrap(self) -> (ReadAccountMapEntry, usize) { match self { AccountIndexGetResult::Found(lock, size) => (lock, size), @@ -2614,6 +2619,7 @@ pub mod tests { type AccountInfoTest = f64; + impl IndexValue for AccountInfoTest {} impl IsCached for AccountInfoTest { fn is_cached(&self) -> bool { true @@ -2734,7 +2740,7 @@ pub mod tests { } } - fn test_new_entry_code_paths_helper( + fn test_new_entry_code_paths_helper( account_infos: [T; 2], is_cached: bool, upsert: bool, @@ -3416,7 +3422,7 @@ pub mod tests { assert!(found_key); } - fn account_maps_len_expensive(index: &AccountsIndex) -> usize { + fn account_maps_len_expensive(index: &AccountsIndex) -> usize { index .account_maps .iter() @@ -3944,6 +3950,8 @@ pub mod tests { ); } + impl IndexValue for bool {} + impl IndexValue for u64 {} impl IsCached for bool { fn is_cached(&self) -> bool { false diff --git a/runtime/src/accounts_index_storage.rs b/runtime/src/accounts_index_storage.rs index e1567ed4e0..444c309100 100644 --- a/runtime/src/accounts_index_storage.rs +++ b/runtime/src/accounts_index_storage.rs @@ -1,4 +1,4 @@ -use crate::accounts_index::IsCached; +use crate::accounts_index::IndexValue; use crate::bucket_map_holder::BucketMapHolder; use crate::waitable_condvar::WaitableCondvar; use std::{ @@ -15,7 +15,7 @@ use std::{ // and it will stop all the background threads and join them. #[derive(Debug, Default)] -pub struct AccountsIndexStorage { +pub struct AccountsIndexStorage { // for managing the bg threads exit: Arc, wait: Arc, @@ -25,7 +25,7 @@ pub struct AccountsIndexStorage { storage: Arc>, } -impl Drop for AccountsIndexStorage { +impl Drop for AccountsIndexStorage { fn drop(&mut self) { self.exit.store(true, Ordering::Relaxed); self.wait.notify_all(); @@ -35,7 +35,7 @@ impl Drop for AccountsIndexStorage { } } -impl AccountsIndexStorage { +impl AccountsIndexStorage { pub fn new() -> AccountsIndexStorage { let storage = Arc::new(BucketMapHolder::new()); let storage_ = storage.clone(); diff --git a/runtime/src/bucket_map_holder.rs b/runtime/src/bucket_map_holder.rs index 2709eed1e4..ef020905f4 100644 --- a/runtime/src/bucket_map_holder.rs +++ b/runtime/src/bucket_map_holder.rs @@ -1,4 +1,4 @@ -use crate::accounts_index::IsCached; +use crate::accounts_index::IndexValue; use crate::bucket_map_holder_stats::BucketMapHolderStats; use crate::waitable_condvar::WaitableCondvar; use std::fmt::Debug; @@ -8,12 +8,12 @@ use std::time::Duration; // will eventually hold the bucket map #[derive(Debug, Default)] -pub struct BucketMapHolder { +pub struct BucketMapHolder { pub stats: BucketMapHolderStats, _phantom: std::marker::PhantomData, } -impl BucketMapHolder { +impl BucketMapHolder { pub fn new() -> Self { Self::default() } diff --git a/runtime/src/in_mem_accounts_index.rs b/runtime/src/in_mem_accounts_index.rs index 93511de994..dfd784b8ad 100644 --- a/runtime/src/in_mem_accounts_index.rs +++ b/runtime/src/in_mem_accounts_index.rs @@ -1,5 +1,5 @@ use crate::accounts_index::{ - AccountMapEntry, AccountMapEntryInner, IsCached, SlotList, WriteAccountMapEntry, + AccountMapEntry, AccountMapEntryInner, IndexValue, SlotList, WriteAccountMapEntry, }; use crate::accounts_index_storage::AccountsIndexStorage; use crate::bucket_map_holder::BucketMapHolder; @@ -16,14 +16,14 @@ type K = Pubkey; // one instance of this represents one bin of the accounts index. #[derive(Debug)] -pub struct InMemAccountsIndex { +pub struct InMemAccountsIndex { // backing store map: HashMap>, storage: Arc>, bin: usize, } -impl InMemAccountsIndex { +impl InMemAccountsIndex { pub fn new(storage: &AccountsIndexStorage, bin: usize) -> Self { Self { map: HashMap::new(),