From d7eb95368aa47255f19101e788301daa93d7cc6b Mon Sep 17 00:00:00 2001 From: brooks Date: Fri, 18 Nov 2022 11:35:17 -0500 Subject: [PATCH] Promotes accounts hash to a strong type --- accounts-bench/src/main.rs | 2 +- core/src/accounts_hash_verifier.rs | 18 +++--- core/tests/epoch_accounts_hash.rs | 2 +- core/tests/snapshots.rs | 12 ++-- runtime/src/accounts_db.rs | 96 +++++++++++++++-------------- runtime/src/accounts_hash.rs | 4 ++ runtime/src/bank.rs | 13 ++-- runtime/src/epoch_accounts_hash.rs | 8 ++- runtime/src/serde_snapshot.rs | 9 +-- runtime/src/serde_snapshot/newer.rs | 7 ++- runtime/src/serde_snapshot/tests.rs | 9 +-- runtime/src/snapshot_hash.rs | 11 ++-- runtime/src/snapshot_package.rs | 13 ++-- runtime/src/snapshot_utils.rs | 10 +-- 14 files changed, 119 insertions(+), 95 deletions(-) diff --git a/accounts-bench/src/main.rs b/accounts-bench/src/main.rs index b88737cef37821..8f0201fdfd47a3 100644 --- a/accounts-bench/src/main.rs +++ b/accounts-bench/src/main.rs @@ -141,7 +141,7 @@ fn main() { } println!( "hash,{},{},{},{}%", - results.0, + results.0 .0, time, time_store, (time_store.as_us() as f64 / time.as_us() as f64 * 100.0f64) as u32 diff --git a/core/src/accounts_hash_verifier.rs b/core/src/accounts_hash_verifier.rs index 6f60e0ba710341..fd0743ba565b68 100644 --- a/core/src/accounts_hash_verifier.rs +++ b/core/src/accounts_hash_verifier.rs @@ -9,7 +9,7 @@ use { solana_gossip::cluster_info::{ClusterInfo, MAX_SNAPSHOT_HASHES}, solana_measure::{measure, measure::Measure}, solana_runtime::{ - accounts_hash::{CalcAccountsHashConfig, HashStats}, + accounts_hash::{AccountsHash, CalcAccountsHashConfig, HashStats}, epoch_accounts_hash::EpochAccountsHash, snapshot_config::SnapshotConfig, snapshot_package::{ @@ -210,7 +210,7 @@ impl AccountsHashVerifier { } /// returns calculated accounts hash - fn calculate_and_verify_accounts_hash(accounts_package: &AccountsPackage) -> Hash { + fn calculate_and_verify_accounts_hash(accounts_package: &AccountsPackage) -> AccountsHash { let mut measure_hash = Measure::start("hash"); let mut sort_time = Measure::start("sort_storages"); let sorted_storages = SortedStorages::new(&accounts_package.snapshot_storages); @@ -313,13 +313,13 @@ impl AccountsHashVerifier { accounts_hash } - fn save_epoch_accounts_hash(accounts_package: &AccountsPackage, accounts_hash: Hash) { + fn save_epoch_accounts_hash(accounts_package: &AccountsPackage, accounts_hash: AccountsHash) { if accounts_package.package_type == AccountsPackageType::EpochAccountsHash { info!( "saving epoch accounts hash, slot: {}, hash: {}", - accounts_package.slot, accounts_hash + accounts_package.slot, accounts_hash.0, ); - let epoch_accounts_hash = EpochAccountsHash::new(accounts_hash); + let epoch_accounts_hash = EpochAccountsHash::from(accounts_hash); accounts_package .accounts .accounts_db @@ -346,17 +346,17 @@ impl AccountsHashVerifier { hashes: &mut Vec<(Slot, Hash)>, exit: &Arc, fault_injection_rate_slots: u64, - accounts_hash: Hash, + accounts_hash: AccountsHash, ) { if fault_injection_rate_slots != 0 && accounts_package.slot % fault_injection_rate_slots == 0 { // For testing, publish an invalid hash to gossip. - let fault_hash = Self::generate_fault_hash(&accounts_hash); + let fault_hash = Self::generate_fault_hash(&accounts_hash.0); warn!("inserting fault at slot: {}", accounts_package.slot); hashes.push((accounts_package.slot, fault_hash)); } else { - hashes.push((accounts_package.slot, accounts_hash)); + hashes.push((accounts_package.slot, accounts_hash.0)); } retain_max_n_elements(hashes, MAX_SNAPSHOT_HASHES); @@ -378,7 +378,7 @@ impl AccountsHashVerifier { accounts_package: AccountsPackage, pending_snapshot_package: Option<&PendingSnapshotPackage>, snapshot_config: Option<&SnapshotConfig>, - accounts_hash: Hash, + accounts_hash: AccountsHash, ) { if pending_snapshot_package.is_none() || !snapshot_config diff --git a/core/tests/epoch_accounts_hash.rs b/core/tests/epoch_accounts_hash.rs index 75cae95492a82b..797f6fa0719972 100755 --- a/core/tests/epoch_accounts_hash.rs +++ b/core/tests/epoch_accounts_hash.rs @@ -326,7 +326,7 @@ fn test_epoch_accounts_hash_basic(test_environment: TestEnvironment) { }, ) .unwrap(); - expected_epoch_accounts_hash = Some(EpochAccountsHash::new(accounts_hash)); + expected_epoch_accounts_hash = Some(EpochAccountsHash::from(accounts_hash)); debug!( "slot {}, expected epoch accounts hash: {:?}", bank.slot(), diff --git a/core/tests/snapshots.rs b/core/tests/snapshots.rs index f0456302026f1f..cfc820f7f80210 100644 --- a/core/tests/snapshots.rs +++ b/core/tests/snapshots.rs @@ -17,6 +17,7 @@ use { PrunedBanksRequestHandler, SnapshotRequestHandler, }, accounts_db::{self, ACCOUNTS_DB_CONFIG_FOR_TESTING}, + accounts_hash::AccountsHash, accounts_index::AccountSecondaryIndexes, bank::{Bank, BankSlotDelta}, bank_forks::BankForks, @@ -277,13 +278,14 @@ fn run_bank_forks_snapshot_n( None, ) .unwrap(); + let accounts_hash = last_bank.get_accounts_hash(); solana_runtime::serde_snapshot::reserialize_bank_with_new_accounts_hash( accounts_package.snapshot_links_dir(), accounts_package.slot, - &last_bank.get_accounts_hash(), + &accounts_hash, None, ); - let snapshot_package = SnapshotPackage::new(accounts_package, last_bank.get_accounts_hash()); + let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash); snapshot_utils::archive_snapshot_package( &snapshot_package, &snapshot_config.full_snapshot_archives_dir, @@ -527,10 +529,10 @@ fn test_concurrent_snapshot_packaging( solana_runtime::serde_snapshot::reserialize_bank_with_new_accounts_hash( accounts_package.snapshot_links_dir(), accounts_package.slot, - &Hash::default(), + &AccountsHash::default(), None, ); - let snapshot_package = SnapshotPackage::new(accounts_package, Hash::default()); + let snapshot_package = SnapshotPackage::new(accounts_package, AccountsHash::default()); pending_snapshot_package .lock() .unwrap() @@ -571,7 +573,7 @@ fn test_concurrent_snapshot_packaging( solana_runtime::serde_snapshot::reserialize_bank_with_new_accounts_hash( saved_snapshots_dir.path(), saved_slot, - &Hash::default(), + &AccountsHash::default(), None, ); diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 9e28747b8601e2..c105884d046862 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -24,8 +24,8 @@ use { accounts_background_service::{DroppedSlotsSender, SendDroppedBankCallback}, accounts_cache::{AccountsCache, CachedAccount, SlotCache}, accounts_hash::{ - AccountsHasher, CalcAccountsHashConfig, CalculateHashIntermediate, HashStats, - PreviousPass, + AccountsHash, AccountsHasher, CalcAccountsHashConfig, CalculateHashIntermediate, + HashStats, PreviousPass, }, accounts_index::{ AccountIndexGetResult, AccountSecondaryIndexes, AccountsIndex, AccountsIndexConfig, @@ -1155,7 +1155,7 @@ impl BankHashStats { #[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq, AbiExample)] pub struct BankHashInfo { pub accounts_delta_hash: Hash, - pub accounts_hash: Hash, + pub accounts_hash: AccountsHash, pub stats: BankHashStats, } @@ -4987,11 +4987,7 @@ impl AccountsDb { return; } - let new_hash_info = BankHashInfo { - accounts_delta_hash: Hash::default(), - accounts_hash: Hash::default(), - stats: BankHashStats::default(), - }; + let new_hash_info = BankHashInfo::default(); bank_hashes.insert(slot, new_hash_info); } @@ -6931,7 +6927,7 @@ impl AccountsDb { &self, max_slot: Slot, config: &CalcAccountsHashConfig<'_>, - ) -> Result<(Hash, u64), BankHashVerificationError> { + ) -> Result<(AccountsHash, u64), BankHashVerificationError> { use BankHashVerificationError::*; let mut collect = Measure::start("collect"); let keys: Vec<_> = self @@ -7056,10 +7052,11 @@ impl AccountsDb { ); self.assert_safe_squashing_accounts_hash(max_slot, config.epoch_schedule); - Ok((accumulated_hash, total_lamports)) + let accounts_hash = AccountsHash(accumulated_hash); + Ok((accounts_hash, total_lamports)) } - pub fn get_accounts_hash(&self, slot: Slot) -> Hash { + pub fn get_accounts_hash(&self, slot: Slot) -> AccountsHash { let bank_hashes = self.bank_hashes.read().unwrap(); let bank_hash_info = bank_hashes.get(&slot).unwrap(); bank_hash_info.accounts_hash @@ -7071,7 +7068,7 @@ impl AccountsDb { ancestors: &Ancestors, debug_verify: bool, is_startup: bool, - ) -> (Hash, u64) { + ) -> (AccountsHash, u64) { self.update_accounts_hash( CalcAccountsHashDataSource::IndexForTests, debug_verify, @@ -7403,7 +7400,7 @@ impl AccountsDb { data_source: CalcAccountsHashDataSource, slot: Slot, config: &CalcAccountsHashConfig<'_>, - ) -> Result<(Hash, u64), BankHashVerificationError> { + ) -> Result<(AccountsHash, u64), BankHashVerificationError> { match data_source { CalcAccountsHashDataSource::Storages => { if self.accounts_cache.contains_any_slots(slot) { @@ -7448,23 +7445,24 @@ impl AccountsDb { slot: Slot, config: CalcAccountsHashConfig<'_>, expected_capitalization: Option, - ) -> Result<(Hash, u64), BankHashVerificationError> { - let (hash, total_lamports) = self.calculate_accounts_hash(data_source, slot, &config)?; + ) -> Result<(AccountsHash, u64), BankHashVerificationError> { + let (accounts_hash, total_lamports) = + self.calculate_accounts_hash(data_source, slot, &config)?; if debug_verify { // calculate the other way (store or non-store) and verify results match. let data_source_other = match data_source { CalcAccountsHashDataSource::IndexForTests => CalcAccountsHashDataSource::Storages, CalcAccountsHashDataSource::Storages => CalcAccountsHashDataSource::IndexForTests, }; - let (hash_other, total_lamports_other) = + let (accounts_hash_other, total_lamports_other) = self.calculate_accounts_hash(data_source_other, slot, &config)?; - let success = hash == hash_other + let success = accounts_hash == accounts_hash_other && total_lamports == total_lamports_other && total_lamports == expected_capitalization.unwrap_or(total_lamports); - assert!(success, "calculate_accounts_hash_with_verify mismatch. hashes: {}, {}; lamports: {}, {}; expected lamports: {:?}, data source: {:?}, slot: {}", hash, hash_other, total_lamports, total_lamports_other, expected_capitalization, data_source, slot); + assert!(success, "calculate_accounts_hash_with_verify mismatch. hashes: {}, {}; lamports: {}, {}; expected lamports: {:?}, data source: {:?}, slot: {}", accounts_hash.0, accounts_hash_other.0, total_lamports, total_lamports_other, expected_capitalization, data_source, slot); } - Ok((hash, total_lamports)) + Ok((accounts_hash, total_lamports)) } #[allow(clippy::too_many_arguments)] @@ -7478,9 +7476,9 @@ impl AccountsDb { epoch_schedule: &EpochSchedule, rent_collector: &RentCollector, is_startup: bool, - ) -> (Hash, u64) { + ) -> (AccountsHash, u64) { let check_hash = false; - let (hash, total_lamports) = self + let (accounts_hash, total_lamports) = self .calculate_accounts_hash_with_verify( data_source, debug_verify, @@ -7497,15 +7495,15 @@ impl AccountsDb { expected_capitalization, ) .unwrap(); // unwrap here will never fail since check_hash = false - self.set_accounts_hash(slot, hash); - (hash, total_lamports) + self.set_accounts_hash(slot, accounts_hash); + (accounts_hash, total_lamports) } /// update hash for this slot in the 'bank_hashes' map - pub(crate) fn set_accounts_hash(&self, slot: Slot, hash: Hash) { + pub(crate) fn set_accounts_hash(&self, slot: Slot, accounts_hash: AccountsHash) { let mut bank_hashes = self.bank_hashes.write().unwrap(); let mut bank_hash_info = bank_hashes.get_mut(&slot).unwrap(); - bank_hash_info.accounts_hash = hash; + bank_hash_info.accounts_hash = accounts_hash; } /// scan 'storage', return a vec of 'CacheHashDataFile', one per pass @@ -7625,7 +7623,7 @@ impl AccountsDb { config: &CalcAccountsHashConfig<'_>, storages: &SortedStorages<'_>, mut stats: HashStats, - ) -> Result<(Hash, u64), BankHashVerificationError> { + ) -> Result<(AccountsHash, u64), BankHashVerificationError> { let _guard = self.active_stats.activate(ActiveStatItem::Hash); stats.oldest_root = storages.range().start; @@ -7694,6 +7692,7 @@ impl AccountsDb { storages.max_slot_inclusive(), final_result ); + let final_result = (AccountsHash(final_result.0), final_result.1); Ok(final_result) }; @@ -7777,21 +7776,22 @@ impl AccountsDb { use BankHashVerificationError::*; let check_hash = false; // this will not be supported anymore - let (calculated_hash, calculated_lamports) = self.calculate_accounts_hash_with_verify( - CalcAccountsHashDataSource::Storages, - test_hash_calculation, - slot, - CalcAccountsHashConfig { - use_bg_thread_pool, - check_hash, - ancestors: Some(ancestors), - epoch_schedule, - rent_collector, - store_detailed_debug_info_on_failure: store_hash_raw_data_for_debug, - full_snapshot: None, - }, - None, - )?; + let (calculated_accounts_hash, calculated_lamports) = self + .calculate_accounts_hash_with_verify( + CalcAccountsHashDataSource::Storages, + test_hash_calculation, + slot, + CalcAccountsHashConfig { + use_bg_thread_pool, + check_hash, + ancestors: Some(ancestors), + epoch_schedule, + rent_collector, + store_detailed_debug_info_on_failure: store_hash_raw_data_for_debug, + full_snapshot: None, + }, + None, + )?; if calculated_lamports != total_lamports { warn!( @@ -7806,12 +7806,12 @@ impl AccountsDb { } else { let bank_hashes = self.bank_hashes.read().unwrap(); if let Some(found_hash_info) = bank_hashes.get(&slot) { - if calculated_hash == found_hash_info.accounts_hash { + if calculated_accounts_hash == found_hash_info.accounts_hash { Ok(()) } else { warn!( - "mismatched bank hash for slot {}: {} (calculated) != {} (expected)", - slot, calculated_hash, found_hash_info.accounts_hash + "mismatched bank hash for slot {}: {:?} (calculated) != {:?} (expected)", + slot, calculated_accounts_hash, found_hash_info.accounts_hash, ); Err(MismatchedBankHash) } @@ -10624,7 +10624,8 @@ pub mod tests { ) .unwrap(); let expected_hash = Hash::from_str("GKot5hBsd81kMupNCXHaqbhv3huEbxAFMLnpcX2hniwn").unwrap(); - assert_eq!(result, (expected_hash, 0)); + let expected_accounts_hash = AccountsHash(expected_hash); + assert_eq!(result, (expected_accounts_hash, 0)); } #[test] @@ -10646,7 +10647,8 @@ pub mod tests { ) .unwrap(); - assert_eq!(result, (expected_hash, sum)); + let expected_accounts_hash = AccountsHash(expected_hash); + assert_eq!(result, (expected_accounts_hash, sum)); } fn sample_storage() -> (SnapshotStorages, usize, Slot) { @@ -12934,7 +12936,7 @@ pub mod tests { let some_bank_hash = Hash::new(&[0xca; HASH_BYTES]); let bank_hash_info = BankHashInfo { accounts_delta_hash: some_bank_hash, - accounts_hash: Hash::new(&[0xca; HASH_BYTES]), + accounts_hash: AccountsHash(Hash::new(&[0xca; HASH_BYTES])), stats: BankHashStats::default(), }; db.bank_hashes diff --git a/runtime/src/accounts_hash.rs b/runtime/src/accounts_hash.rs index 99b58318d791cb..875d4a58a04bc9 100644 --- a/runtime/src/accounts_hash.rs +++ b/runtime/src/accounts_hash.rs @@ -1079,6 +1079,10 @@ impl AccountsHasher { } } +/// Hash of all the accounts +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Serialize, Deserialize, AbiExample)] +pub struct AccountsHash(pub Hash); + #[cfg(test)] pub mod tests { use {super::*, std::str::FromStr}; diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index f7dad2e36ffab1..5ba5ab3b5eb5b2 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -48,6 +48,7 @@ use { IncludeSlotInHash, SnapshotStorages, ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS, ACCOUNTS_DB_CONFIG_FOR_TESTING, }, + accounts_hash::AccountsHash, accounts_index::{AccountSecondaryIndexes, IndexKey, ScanConfig, ScanResult, ZeroLamport}, accounts_update_notifier_interface::AccountsUpdateNotifier, ancestors::{Ancestors, AncestorsForSerialization}, @@ -1899,7 +1900,7 @@ impl Bank { parent.force_flush_accounts_cache(); let accounts_hash = parent.update_accounts_hash(CalcAccountsHashDataSource::Storages, false, true); - let epoch_accounts_hash = EpochAccountsHash::new(accounts_hash); + let epoch_accounts_hash = accounts_hash.into(); parent .rc .accounts @@ -7043,7 +7044,7 @@ impl Bank { old } - pub fn get_accounts_hash(&self) -> Hash { + pub fn get_accounts_hash(&self) -> AccountsHash { self.rc.accounts.accounts_db.get_accounts_hash(self.slot) } @@ -7069,8 +7070,8 @@ impl Bank { data_source: CalcAccountsHashDataSource, mut debug_verify: bool, is_startup: bool, - ) -> Hash { - let (hash, total_lamports) = self.rc.accounts.accounts_db.update_accounts_hash( + ) -> AccountsHash { + let (accounts_hash, total_lamports) = self.rc.accounts.accounts_db.update_accounts_hash( data_source, debug_verify, self.slot(), @@ -7111,10 +7112,10 @@ impl Bank { self.capitalization() ); } - hash + accounts_hash } - pub fn update_accounts_hash_for_tests(&self) -> Hash { + pub fn update_accounts_hash_for_tests(&self) -> AccountsHash { self.update_accounts_hash(CalcAccountsHashDataSource::IndexForTests, false, false) } diff --git a/runtime/src/epoch_accounts_hash.rs b/runtime/src/epoch_accounts_hash.rs index db6df74209bd2e..c6d1b820c2ced8 100644 --- a/runtime/src/epoch_accounts_hash.rs +++ b/runtime/src/epoch_accounts_hash.rs @@ -7,7 +7,7 @@ //! //! This results in all nodes effectively voting on the accounts state (at least) once per epoch. -use solana_sdk::hash::Hash; +use {crate::accounts_hash::AccountsHash, solana_sdk::hash::Hash}; mod utils; pub use utils::*; @@ -32,3 +32,9 @@ impl EpochAccountsHash { Self(accounts_hash) } } + +impl From for EpochAccountsHash { + fn from(accounts_hash: AccountsHash) -> EpochAccountsHash { + Self::new(accounts_hash.0) + } +} diff --git a/runtime/src/serde_snapshot.rs b/runtime/src/serde_snapshot.rs index b2e1062b11e523..d305c535e7b228 100644 --- a/runtime/src/serde_snapshot.rs +++ b/runtime/src/serde_snapshot.rs @@ -5,6 +5,7 @@ use { AccountShrinkThreshold, AccountStorageEntry, AccountsDb, AccountsDbConfig, AppendVecId, AtomicAppendVecId, BankHashInfo, IndexGenerationInfo, SnapshotStorage, }, + accounts_hash::AccountsHash, accounts_index::AccountSecondaryIndexes, accounts_update_notifier_interface::AccountsUpdateNotifier, append_vec::{AppendVec, StoredMetaWriteVersion}, @@ -65,7 +66,7 @@ pub(crate) enum SerdeStyle { const MAX_STREAM_SIZE: u64 = 32 * 1024 * 1024 * 1024; -#[derive(Clone, Debug, Default, Deserialize, Serialize, AbiExample, PartialEq, Eq)] +#[derive(Clone, Debug, Deserialize, Serialize, AbiExample, PartialEq, Eq)] pub struct AccountsDbFields( HashMap>, StoredMetaWriteVersion, @@ -192,7 +193,7 @@ trait TypeContext<'a>: PartialEq { fn reserialize_bank_fields_with_hash( stream_reader: &mut BufReader, stream_writer: &mut BufWriter, - accounts_hash: &Hash, + accounts_hash: &AccountsHash, incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>, ) -> std::result::Result<(), Box> where @@ -391,7 +392,7 @@ where fn reserialize_bank_fields_with_new_hash( stream_reader: &mut BufReader, stream_writer: &mut BufWriter, - accounts_hash: &Hash, + accounts_hash: &AccountsHash, incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>, ) -> Result<(), Error> where @@ -414,7 +415,7 @@ where pub fn reserialize_bank_with_new_accounts_hash( bank_snapshots_dir: impl AsRef, slot: Slot, - accounts_hash: &Hash, + accounts_hash: &AccountsHash, incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>, ) -> bool { let bank_post = snapshot_utils::get_bank_snapshots_dir(bank_snapshots_dir, slot); diff --git a/runtime/src/serde_snapshot/newer.rs b/runtime/src/serde_snapshot/newer.rs index 7558d8c41ae2ca..a052d74c034b7e 100644 --- a/runtime/src/serde_snapshot/newer.rs +++ b/runtime/src/serde_snapshot/newer.rs @@ -5,6 +5,7 @@ use { *, }, crate::{ + accounts_hash::AccountsHash, ancestors::AncestorsForSerialization, stakes::{serde_stakes_enum_compat, StakesEnum}, }, @@ -269,7 +270,7 @@ impl<'a> TypeContext<'a> for Context { ) })); let slot = serializable_db.slot; - let hash: BankHashInfo = serializable_db + let bank_hash_info: BankHashInfo = serializable_db .accounts_db .bank_hashes .read() @@ -293,7 +294,7 @@ impl<'a> TypeContext<'a> for Context { entries, version, slot, - hash, + bank_hash_info, historical_roots, historical_roots_with_hash, ) @@ -346,7 +347,7 @@ impl<'a> TypeContext<'a> for Context { fn reserialize_bank_fields_with_hash( stream_reader: &mut BufReader, stream_writer: &mut BufWriter, - accounts_hash: &Hash, + accounts_hash: &AccountsHash, incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>, ) -> std::result::Result<(), Box> where diff --git a/runtime/src/serde_snapshot/tests.rs b/runtime/src/serde_snapshot/tests.rs index 7c29430ccf2f03..06d863d267419c 100644 --- a/runtime/src/serde_snapshot/tests.rs +++ b/runtime/src/serde_snapshot/tests.rs @@ -5,6 +5,7 @@ use { crate::{ accounts::{test_utils::create_test_accounts, Accounts}, accounts_db::{get_temp_accounts_paths, AccountShrinkThreshold, AccountStorageMap}, + accounts_hash::AccountsHash, append_vec::AppendVec, bank::{Bank, Rewrites}, epoch_accounts_hash, @@ -289,12 +290,12 @@ fn test_bank_serialize_style( .unwrap(); let accounts_hash = if update_accounts_hash { - let hash = Hash::new(&[1; 32]); + let accounts_hash = AccountsHash(Hash::new(&[1; 32])); bank2 .accounts() .accounts_db - .set_accounts_hash(bank2.slot(), hash); - hash + .set_accounts_hash(bank2.slot(), accounts_hash); + accounts_hash } else { bank2.get_accounts_hash() }; @@ -685,7 +686,7 @@ mod test_bank_serialize { // This some what long test harness is required to freeze the ABI of // Bank's serialization due to versioned nature - #[frozen_abi(digest = "B9ui5cFeJ5NGtXAVFXRCSX4GJ77yLc3izv1E8QE34TdQ")] + #[frozen_abi(digest = "464v92sAyLDZWCXjH6cuQfgrSNuB3PY8cmoVu2dciXvV")] #[derive(Serialize, AbiExample)] pub struct BankAbiTestWrapperNewer { #[serde(serialize_with = "wrapper_newer")] diff --git a/runtime/src/snapshot_hash.rs b/runtime/src/snapshot_hash.rs index a23634e29643ca..5a6dc6cc88488e 100644 --- a/runtime/src/snapshot_hash.rs +++ b/runtime/src/snapshot_hash.rs @@ -1,6 +1,6 @@ //! Helper types and functions for handling and dealing with snapshot hashes. use { - crate::epoch_accounts_hash::EpochAccountsHash, + crate::{accounts_hash::AccountsHash, epoch_accounts_hash::EpochAccountsHash}, solana_sdk::{ clock::Slot, hash::{Hash, Hasher}, @@ -56,12 +56,15 @@ pub struct SnapshotHash(pub Hash); impl SnapshotHash { /// Make a snapshot hash from an accounts hash and epoch accounts hash #[must_use] - pub fn new(accounts_hash: &Hash, epoch_accounts_hash: Option<&EpochAccountsHash>) -> Self { + pub fn new( + accounts_hash: &AccountsHash, + epoch_accounts_hash: Option<&EpochAccountsHash>, + ) -> Self { let snapshot_hash = match epoch_accounts_hash { - None => *accounts_hash, + None => accounts_hash.0, Some(epoch_accounts_hash) => { let mut hasher = Hasher::default(); - hasher.hash(accounts_hash.as_ref()); + hasher.hash(accounts_hash.0.as_ref()); hasher.hash(epoch_accounts_hash.as_ref().as_ref()); hasher.result() } diff --git a/runtime/src/snapshot_package.rs b/runtime/src/snapshot_package.rs index a496b47ff0772e..c556dad4f64dde 100644 --- a/runtime/src/snapshot_package.rs +++ b/runtime/src/snapshot_package.rs @@ -2,6 +2,7 @@ use { crate::{ accounts::Accounts, accounts_db::SnapshotStorages, + accounts_hash::AccountsHash, bank::{Bank, BankSlotDelta}, epoch_accounts_hash::EpochAccountsHash, rent_collector::RentCollector, @@ -13,7 +14,7 @@ use { }, }, log::*, - solana_sdk::{clock::Slot, hash::Hash, sysvar::epoch_schedule::EpochSchedule}, + solana_sdk::{clock::Slot, sysvar::epoch_schedule::EpochSchedule}, std::{ fs, path::{Path, PathBuf}, @@ -37,7 +38,7 @@ pub struct AccountsPackage { pub block_height: Slot, pub snapshot_storages: SnapshotStorages, pub expected_capitalization: u64, - pub accounts_hash_for_testing: Option, + pub accounts_hash_for_testing: Option, pub accounts: Arc, pub epoch_schedule: EpochSchedule, pub rent_collector: RentCollector, @@ -64,7 +65,7 @@ impl AccountsPackage { snapshot_storages: SnapshotStorages, archive_format: ArchiveFormat, snapshot_version: SnapshotVersion, - accounts_hash_for_testing: Option, + accounts_hash_for_testing: Option, ) -> Result { if let AccountsPackageType::Snapshot(snapshot_type) = package_type { info!( @@ -125,7 +126,7 @@ impl AccountsPackage { package_type: AccountsPackageType, bank: &Bank, snapshot_storages: SnapshotStorages, - accounts_hash_for_testing: Option, + accounts_hash_for_testing: Option, ) -> Self { assert_eq!(package_type, AccountsPackageType::EpochAccountsHash); Self::_new( @@ -141,7 +142,7 @@ impl AccountsPackage { package_type: AccountsPackageType, bank: &Bank, snapshot_storages: SnapshotStorages, - accounts_hash_for_testing: Option, + accounts_hash_for_testing: Option, snapshot_info: Option, ) -> Self { Self { @@ -244,7 +245,7 @@ pub struct SnapshotPackage { } impl SnapshotPackage { - pub fn new(accounts_package: AccountsPackage, accounts_hash: Hash) -> Self { + pub fn new(accounts_package: AccountsPackage, accounts_hash: AccountsHash) -> Self { let AccountsPackageType::Snapshot(snapshot_type) = accounts_package.package_type else { panic!("The AccountsPackage must be of type Snapshot in order to make a SnapshotPackage!"); }; diff --git a/runtime/src/snapshot_utils.rs b/runtime/src/snapshot_utils.rs index c2a961955143b6..d148ec8b9452db 100644 --- a/runtime/src/snapshot_utils.rs +++ b/runtime/src/snapshot_utils.rs @@ -2221,14 +2221,15 @@ pub fn package_and_archive_full_snapshot( None, )?; + let accounts_hash = bank.get_accounts_hash(); crate::serde_snapshot::reserialize_bank_with_new_accounts_hash( accounts_package.snapshot_links_dir(), accounts_package.slot, - &bank.get_accounts_hash(), + &accounts_hash, None, ); - let snapshot_package = SnapshotPackage::new(accounts_package, bank.get_accounts_hash()); + let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash); archive_snapshot_package( &snapshot_package, full_snapshot_archives_dir, @@ -2274,14 +2275,15 @@ pub fn package_and_archive_incremental_snapshot( None, )?; + let accounts_hash = bank.get_accounts_hash(); crate::serde_snapshot::reserialize_bank_with_new_accounts_hash( accounts_package.snapshot_links_dir(), accounts_package.slot, - &bank.get_accounts_hash(), + &accounts_hash, None, ); - let snapshot_package = SnapshotPackage::new(accounts_package, bank.get_accounts_hash()); + let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash); archive_snapshot_package( &snapshot_package, full_snapshot_archives_dir,