diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index 75b0dcdf44..1806d0366c 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -87,7 +87,7 @@ impl AccountLocks { } /// This structure handles synchronization for db -#[derive(Default, Debug, AbiExample)] +#[derive(Debug, AbiExample)] pub struct Accounts { /// Single global AccountsDb pub accounts_db: Arc, @@ -97,6 +97,15 @@ pub struct Accounts { pub(crate) account_locks: Mutex, } +impl Default for Accounts { + fn default() -> Self { + Self { + accounts_db: Arc::new(AccountsDb::default()), + account_locks: Mutex::default(), + } + } +} + // for the load instructions pub type TransactionAccounts = Vec<(Pubkey, AccountSharedData)>; pub type TransactionRent = u64; @@ -117,6 +126,13 @@ pub enum AccountAddressFilter { } impl Accounts { + pub fn default_for_tests() -> Self { + Self { + accounts_db: Arc::new(AccountsDb::default_for_tests()), + account_locks: Mutex::default(), + } + } + pub fn new( paths: Vec, cluster_type: &ClusterType, diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 9ee804c97c..9b399e5758 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -24,7 +24,7 @@ use crate::{ accounts_hash::{AccountsHash, CalculateHashIntermediate, HashStats, PreviousPass}, accounts_index::{ AccountIndexGetResult, AccountSecondaryIndexes, AccountsIndex, AccountsIndexRootsStats, - IndexKey, IsCached, ScanResult, SlotList, SlotSlice, ZeroLamport, + IndexKey, IsCached, ScanResult, SlotList, SlotSlice, ZeroLamport, BINS_DEFAULT, }, ancestors::Ancestors, append_vec::{AppendVec, StoredAccountMeta, StoredMeta, StoredMetaWriteVersion}, @@ -1346,13 +1346,26 @@ impl<'a> ReadableAccount for StoredAccountMeta<'a> { impl Default for AccountsDb { fn default() -> Self { + Self::default_with_accounts_index(AccountInfoAccountsIndex::new(BINS_DEFAULT)) + } +} + +type GenerateIndexAccountsMap<'a> = + HashMap)>; + +impl AccountsDb { + pub fn default_for_tests() -> Self { + Self::default_with_accounts_index(AccountInfoAccountsIndex::default_for_tests()) + } + + fn default_with_accounts_index(accounts_index: AccountInfoAccountsIndex) -> Self { let num_threads = get_thread_count(); const MAX_READ_ONLY_CACHE_DATA_SIZE: usize = 200_000_000; let mut bank_hashes = HashMap::new(); bank_hashes.insert(0, BankHashInfo::default()); AccountsDb { - accounts_index: AccountsIndex::new(crate::accounts_index::BINS_DEFAULT), + accounts_index, storage: AccountStorage::default(), accounts_cache: AccountsCache::default(), sender_bg_hasher: None, @@ -1393,12 +1406,7 @@ impl Default for AccountsDb { dirty_stores: DashMap::default(), } } -} -type GenerateIndexAccountsMap<'a> = - HashMap)>; - -impl AccountsDb { pub fn new(paths: Vec, cluster_type: &ClusterType) -> Self { AccountsDb::new_with_config( paths, @@ -1409,6 +1417,11 @@ impl AccountsDb { ) } + pub fn new_for_tests(paths: Vec, cluster_type: &ClusterType) -> Self { + // will diverge + Self::new(paths, cluster_type) + } + pub fn new_with_config( paths: Vec, cluster_type: &ClusterType, @@ -1416,6 +1429,7 @@ impl AccountsDb { caching_enabled: bool, shrink_ratio: AccountShrinkThreshold, ) -> Self { + let accounts_index = AccountsIndex::new(BINS_DEFAULT); let mut new = if !paths.is_empty() { Self { paths, @@ -1424,7 +1438,7 @@ impl AccountsDb { account_indexes, caching_enabled, shrink_ratio, - ..Self::default() + ..Self::default_with_accounts_index(accounts_index) } } else { // Create a temporary set of accounts directories, used primarily @@ -1437,7 +1451,7 @@ impl AccountsDb { account_indexes, caching_enabled, shrink_ratio, - ..Self::default() + ..Self::default_with_accounts_index(accounts_index) } }; @@ -1466,7 +1480,7 @@ impl AccountsDb { pub fn new_single_for_tests() -> Self { AccountsDb { min_num_stores: 0, - ..AccountsDb::new(Vec::new(), &ClusterType::Development) + ..AccountsDb::new_for_tests(Vec::new(), &ClusterType::Development) } } diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 594f026a42..08c1a48ff0 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -438,7 +438,7 @@ impl ComputeMeter for TransactionComputeMeter { } } -#[derive(Default, Debug)] +#[derive(Debug)] pub struct BankRc { /// where all the Accounts are stored pub accounts: Arc, @@ -884,7 +884,7 @@ impl AbiExample for OptionalDropCallback { /// Manager for the state of all accounts and programs after processing its entries. /// AbiExample is needed even without Serialize/Deserialize; actual (de-)serialization /// are implemented elsewhere for versioning -#[derive(AbiExample, Debug, Default)] +#[derive(AbiExample, Debug)] pub struct Bank { /// References to accounts, parent and signature status pub rc: BankRc, @@ -1040,6 +1040,12 @@ pub struct Bank { pub freeze_started: AtomicBool, } +impl Default for Bank { + fn default() -> Self { + Self::default_with_accounts(Accounts::default()) + } +} + impl Default for BlockhashQueue { fn default() -> Self { Self::new(MAX_RECENT_BLOCKHASHES) @@ -1109,6 +1115,65 @@ impl Bank { ) } + fn default_with_accounts(accounts: Accounts) -> Self { + Self { + rc: BankRc::new(accounts, Slot::default()), + src: StatusCacheRc::default(), + blockhash_queue: RwLock::::default(), + ancestors: Ancestors::default(), + hash: RwLock::::default(), + parent_hash: Hash::default(), + parent_slot: Slot::default(), + hard_forks: Arc::>::default(), + transaction_count: AtomicU64::default(), + transaction_error_count: AtomicU64::default(), + transaction_entries_count: AtomicU64::default(), + transactions_per_entry_max: AtomicU64::default(), + tick_height: AtomicU64::default(), + signature_count: AtomicU64::default(), + capitalization: AtomicU64::default(), + max_tick_height: u64::default(), + hashes_per_tick: Option::::default(), + ticks_per_slot: u64::default(), + ns_per_slot: u128::default(), + genesis_creation_time: UnixTimestamp::default(), + slots_per_year: f64::default(), + unused: u64::default(), + slot: Slot::default(), + bank_id: BankId::default(), + epoch: Epoch::default(), + block_height: u64::default(), + collector_id: Pubkey::default(), + collector_fees: AtomicU64::default(), + fee_calculator: FeeCalculator::default(), + fee_rate_governor: FeeRateGovernor::default(), + collected_rent: AtomicU64::default(), + rent_collector: RentCollector::default(), + epoch_schedule: EpochSchedule::default(), + inflation: Arc::>::default(), + stakes: RwLock::::default(), + epoch_stakes: HashMap::::default(), + is_delta: AtomicBool::default(), + message_processor: MessageProcessor::default(), + compute_budget: Option::::default(), + feature_builtins: Arc::>::default(), + last_vote_sync: AtomicU64::default(), + rewards: RwLock::>::default(), + cluster_type: Option::::default(), + lazy_rent_collection: AtomicBool::default(), + no_stake_rewrite: AtomicBool::default(), + rewards_pool_pubkeys: Arc::>::default(), + cached_executors: RwLock::::default(), + transaction_debug_keys: Option::>>::default(), + transaction_log_collector_config: Arc::>::default( + ), + transaction_log_collector: Arc::>::default(), + feature_set: Arc::::default(), + drop_callback: RwLock::::default(), + freeze_started: AtomicBool::default(), + } + } + pub fn new_with_paths( genesis_config: &GenesisConfig, paths: Vec, @@ -1120,18 +1185,18 @@ impl Bank { shrink_ratio: AccountShrinkThreshold, debug_do_not_add_builtins: bool, ) -> Self { - let mut bank = Self::default(); - bank.ancestors = Ancestors::from(vec![bank.slot()]); - bank.transaction_debug_keys = debug_keys; - bank.cluster_type = Some(genesis_config.cluster_type); - - bank.rc.accounts = Arc::new(Accounts::new_with_config( + let accounts = Accounts::new_with_config( paths, &genesis_config.cluster_type, account_indexes, accounts_db_caching_enabled, shrink_ratio, - )); + ); + let mut bank = Self::default_with_accounts(accounts); + bank.ancestors = Ancestors::from(vec![bank.slot()]); + bank.transaction_debug_keys = debug_keys; + bank.cluster_type = Some(genesis_config.cluster_type); + bank.process_genesis_config(genesis_config); bank.finish_init( genesis_config,