Makes AccountsHash an enum (#30416)
This commit is contained in:
parent
c5a24e11ba
commit
35328ca63d
|
@ -387,7 +387,7 @@ impl AccountsHashVerifier {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash);
|
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash.into());
|
||||||
let pending_snapshot_package = pending_snapshot_package.unwrap();
|
let pending_snapshot_package = pending_snapshot_package.unwrap();
|
||||||
|
|
||||||
// If the snapshot package is an Incremental Snapshot, do not submit it if there's already
|
// If the snapshot package is an Incremental Snapshot, do not submit it if there's already
|
||||||
|
|
|
@ -284,7 +284,7 @@ fn run_bank_forks_snapshot_n<F>(
|
||||||
&accounts_hash,
|
&accounts_hash,
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash);
|
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash.into());
|
||||||
snapshot_utils::archive_snapshot_package(
|
snapshot_utils::archive_snapshot_package(
|
||||||
&snapshot_package,
|
&snapshot_package,
|
||||||
&snapshot_config.full_snapshot_archives_dir,
|
&snapshot_config.full_snapshot_archives_dir,
|
||||||
|
@ -544,7 +544,7 @@ fn test_concurrent_snapshot_packaging(
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
let snapshot_package =
|
let snapshot_package =
|
||||||
SnapshotPackage::new(accounts_package, AccountsHash(Hash::default()));
|
SnapshotPackage::new(accounts_package, AccountsHash(Hash::default()).into());
|
||||||
pending_snapshot_package
|
pending_snapshot_package
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -26,8 +26,9 @@ use {
|
||||||
accounts_cache::{AccountsCache, CachedAccount, SlotCache},
|
accounts_cache::{AccountsCache, CachedAccount, SlotCache},
|
||||||
accounts_file::AccountsFile,
|
accounts_file::AccountsFile,
|
||||||
accounts_hash::{
|
accounts_hash::{
|
||||||
AccountsDeltaHash, AccountsHash, AccountsHasher, CalcAccountsHashConfig,
|
AccountsDeltaHash, AccountsHash, AccountsHashEnum, AccountsHasher,
|
||||||
CalculateHashIntermediate, HashStats, ZeroLamportAccounts,
|
CalcAccountsHashConfig, CalculateHashIntermediate, HashStats, IncrementalAccountsHash,
|
||||||
|
ZeroLamportAccounts,
|
||||||
},
|
},
|
||||||
accounts_index::{
|
accounts_index::{
|
||||||
AccountIndexGetResult, AccountSecondaryIndexes, AccountsIndex, AccountsIndexConfig,
|
AccountIndexGetResult, AccountSecondaryIndexes, AccountsIndex, AccountsIndexConfig,
|
||||||
|
@ -7453,13 +7454,17 @@ impl AccountsDb {
|
||||||
storages: &SortedStorages<'_>,
|
storages: &SortedStorages<'_>,
|
||||||
stats: HashStats,
|
stats: HashStats,
|
||||||
) -> Result<(AccountsHash, u64), BankHashVerificationError> {
|
) -> Result<(AccountsHash, u64), BankHashVerificationError> {
|
||||||
self._calculate_accounts_hash_from_storages(
|
let (accounts_hash, capitalization) = self._calculate_accounts_hash_from_storages(
|
||||||
config,
|
config,
|
||||||
storages,
|
storages,
|
||||||
stats,
|
stats,
|
||||||
CalcAccountsHashFlavor::Full,
|
CalcAccountsHashFlavor::Full,
|
||||||
self.full_accounts_hash_cache_path.clone(),
|
self.full_accounts_hash_cache_path.clone(),
|
||||||
)
|
)?;
|
||||||
|
let AccountsHashEnum::Full(accounts_hash) = accounts_hash else {
|
||||||
|
panic!("calculate_accounts_hash_from_storages must return a FullAccountsHash");
|
||||||
|
};
|
||||||
|
Ok((accounts_hash, capitalization))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate the incremental accounts hash
|
/// Calculate the incremental accounts hash
|
||||||
|
@ -7477,15 +7482,20 @@ impl AccountsDb {
|
||||||
storages: &SortedStorages<'_>,
|
storages: &SortedStorages<'_>,
|
||||||
base_slot: Slot,
|
base_slot: Slot,
|
||||||
stats: HashStats,
|
stats: HashStats,
|
||||||
) -> Result<(AccountsHash, /* capitalization */ u64), BankHashVerificationError> {
|
) -> Result<(IncrementalAccountsHash, /* capitalization */ u64), BankHashVerificationError>
|
||||||
|
{
|
||||||
assert!(storages.range().start > base_slot, "The storages for calculating an incremental accounts hash must all be higher than the base slot");
|
assert!(storages.range().start > base_slot, "The storages for calculating an incremental accounts hash must all be higher than the base slot");
|
||||||
self._calculate_accounts_hash_from_storages(
|
let (accounts_hash, capitalization) = self._calculate_accounts_hash_from_storages(
|
||||||
config,
|
config,
|
||||||
storages,
|
storages,
|
||||||
stats,
|
stats,
|
||||||
CalcAccountsHashFlavor::Incremental,
|
CalcAccountsHashFlavor::Incremental,
|
||||||
self.incremental_accounts_hash_cache_path.clone(),
|
self.incremental_accounts_hash_cache_path.clone(),
|
||||||
)
|
)?;
|
||||||
|
let AccountsHashEnum::Incremental(incremental_accounts_hash) = accounts_hash else {
|
||||||
|
panic!("calculate_incremental_accounts_hash must return an IncrementalAccountsHash");
|
||||||
|
};
|
||||||
|
Ok((incremental_accounts_hash, capitalization))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _calculate_accounts_hash_from_storages(
|
fn _calculate_accounts_hash_from_storages(
|
||||||
|
@ -7495,19 +7505,16 @@ impl AccountsDb {
|
||||||
mut stats: HashStats,
|
mut stats: HashStats,
|
||||||
flavor: CalcAccountsHashFlavor,
|
flavor: CalcAccountsHashFlavor,
|
||||||
accounts_hash_cache_path: PathBuf,
|
accounts_hash_cache_path: PathBuf,
|
||||||
) -> Result<(AccountsHash, u64), BankHashVerificationError> {
|
) -> Result<(AccountsHashEnum, u64), BankHashVerificationError> {
|
||||||
let _guard = self.active_stats.activate(ActiveStatItem::Hash);
|
let _guard = self.active_stats.activate(ActiveStatItem::Hash);
|
||||||
stats.oldest_root = storages.range().start;
|
stats.oldest_root = storages.range().start;
|
||||||
|
|
||||||
self.mark_old_slots_as_dirty(storages, config.epoch_schedule.slots_per_epoch, &mut stats);
|
self.mark_old_slots_as_dirty(storages, config.epoch_schedule.slots_per_epoch, &mut stats);
|
||||||
|
|
||||||
|
let slot = storages.max_slot_inclusive();
|
||||||
let use_bg_thread_pool = config.use_bg_thread_pool;
|
let use_bg_thread_pool = config.use_bg_thread_pool;
|
||||||
let scan_and_hash = || {
|
let scan_and_hash = || {
|
||||||
let cache_hash_data = Self::get_cache_hash_data(
|
let cache_hash_data = Self::get_cache_hash_data(accounts_hash_cache_path, config, slot);
|
||||||
accounts_hash_cache_path,
|
|
||||||
config,
|
|
||||||
storages.max_slot_inclusive(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let bounds = Range {
|
let bounds = Range {
|
||||||
start: 0,
|
start: 0,
|
||||||
|
@ -7548,14 +7555,16 @@ impl AccountsDb {
|
||||||
);
|
);
|
||||||
|
|
||||||
// turn raw data into merkle tree hashes and sum of lamports
|
// turn raw data into merkle tree hashes and sum of lamports
|
||||||
let final_result = accounts_hasher.rest_of_hash_calculation(result, &mut stats);
|
let (accounts_hash, capitalization) =
|
||||||
info!(
|
accounts_hasher.rest_of_hash_calculation(result, &mut stats);
|
||||||
"calculate_accounts_hash_from_storages: slot: {} {:?}",
|
let accounts_hash = match flavor {
|
||||||
storages.max_slot_inclusive(),
|
CalcAccountsHashFlavor::Full => AccountsHashEnum::Full(AccountsHash(accounts_hash)),
|
||||||
final_result
|
CalcAccountsHashFlavor::Incremental => {
|
||||||
);
|
AccountsHashEnum::Incremental(IncrementalAccountsHash(accounts_hash))
|
||||||
let final_result = (AccountsHash(final_result.0), final_result.1);
|
}
|
||||||
Ok(final_result)
|
};
|
||||||
|
info!("calculate_accounts_hash_from_storages: slot: {slot}, {accounts_hash:?}, capitalization: {capitalization}");
|
||||||
|
Ok((accounts_hash, capitalization))
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = if use_bg_thread_pool {
|
let result = if use_bg_thread_pool {
|
||||||
|
@ -7563,10 +7572,7 @@ impl AccountsDb {
|
||||||
} else {
|
} else {
|
||||||
scan_and_hash()
|
scan_and_hash()
|
||||||
};
|
};
|
||||||
self.assert_safe_squashing_accounts_hash(
|
self.assert_safe_squashing_accounts_hash(slot, config.epoch_schedule);
|
||||||
storages.max_slot_inclusive(),
|
|
||||||
config.epoch_schedule,
|
|
||||||
);
|
|
||||||
stats.log();
|
stats.log();
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
@ -18083,7 +18089,8 @@ pub mod tests {
|
||||||
AccountsDb::hash_account(slot, account, pubkey, INCLUDE_SLOT_IN_HASH_TESTS)
|
AccountsDb::hash_account(slot, account, pubkey, INCLUDE_SLOT_IN_HASH_TESTS)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let expected_accounts_hash = AccountsHash(compute_merkle_root(incremental_account_hashes));
|
let expected_accounts_hash =
|
||||||
|
IncrementalAccountsHash(compute_merkle_root(incremental_account_hashes));
|
||||||
assert_eq!(incremental_accounts_hash.0, expected_accounts_hash);
|
assert_eq!(incremental_accounts_hash.0, expected_accounts_hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1109,9 +1109,38 @@ pub enum ZeroLamportAccounts {
|
||||||
Included,
|
Included,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Hash of accounts
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
pub enum AccountsHashEnum {
|
||||||
|
Full(AccountsHash),
|
||||||
|
Incremental(IncrementalAccountsHash),
|
||||||
|
}
|
||||||
|
impl AccountsHashEnum {
|
||||||
|
pub fn as_hash(&self) -> &Hash {
|
||||||
|
match self {
|
||||||
|
AccountsHashEnum::Full(AccountsHash(hash))
|
||||||
|
| AccountsHashEnum::Incremental(IncrementalAccountsHash(hash)) => hash,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<AccountsHash> for AccountsHashEnum {
|
||||||
|
fn from(accounts_hash: AccountsHash) -> Self {
|
||||||
|
AccountsHashEnum::Full(accounts_hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<IncrementalAccountsHash> for AccountsHashEnum {
|
||||||
|
fn from(incremental_accounts_hash: IncrementalAccountsHash) -> Self {
|
||||||
|
AccountsHashEnum::Incremental(incremental_accounts_hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Hash of accounts
|
/// Hash of accounts
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub struct AccountsHash(pub Hash);
|
pub struct AccountsHash(pub Hash);
|
||||||
|
/// Hash of accounts that includes zero-lamport accounts
|
||||||
|
/// Used with incremental snapshots
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
pub struct IncrementalAccountsHash(pub Hash);
|
||||||
|
|
||||||
/// Hash of accounts written in a single slot
|
/// Hash of accounts written in a single slot
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
|
|
@ -7023,7 +7023,7 @@ impl Bank {
|
||||||
.get_accounts_hash()
|
.get_accounts_hash()
|
||||||
.expect("accounts hash is required to get snapshot hash");
|
.expect("accounts hash is required to get snapshot hash");
|
||||||
let epoch_accounts_hash = self.get_epoch_accounts_hash_to_serialize();
|
let epoch_accounts_hash = self.get_epoch_accounts_hash_to_serialize();
|
||||||
SnapshotHash::new(&accounts_hash, epoch_accounts_hash.as_ref())
|
SnapshotHash::new(&accounts_hash.into(), epoch_accounts_hash.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_thread_pool(&self) -> &ThreadPool {
|
pub fn get_thread_pool(&self) -> &ThreadPool {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! Helper types and functions for handling and dealing with snapshot hashes.
|
//! Helper types and functions for handling and dealing with snapshot hashes.
|
||||||
use {
|
use {
|
||||||
crate::{accounts_hash::AccountsHash, epoch_accounts_hash::EpochAccountsHash},
|
crate::{accounts_hash::AccountsHashEnum, epoch_accounts_hash::EpochAccountsHash},
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
clock::Slot,
|
clock::Slot,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
|
@ -57,14 +57,14 @@ impl SnapshotHash {
|
||||||
/// Make a snapshot hash from an accounts hash and epoch accounts hash
|
/// Make a snapshot hash from an accounts hash and epoch accounts hash
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
accounts_hash: &AccountsHash,
|
accounts_hash: &AccountsHashEnum,
|
||||||
epoch_accounts_hash: Option<&EpochAccountsHash>,
|
epoch_accounts_hash: Option<&EpochAccountsHash>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let snapshot_hash = match epoch_accounts_hash {
|
let snapshot_hash = match epoch_accounts_hash {
|
||||||
None => accounts_hash.0,
|
None => *accounts_hash.as_hash(),
|
||||||
Some(epoch_accounts_hash) => {
|
Some(epoch_accounts_hash) => {
|
||||||
let mut hasher = Hasher::default();
|
let mut hasher = Hasher::default();
|
||||||
hasher.hash(accounts_hash.0.as_ref());
|
hasher.hash(accounts_hash.as_hash().as_ref());
|
||||||
hasher.hash(epoch_accounts_hash.as_ref().as_ref());
|
hasher.hash(epoch_accounts_hash.as_ref().as_ref());
|
||||||
hasher.result()
|
hasher.result()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use {
|
||||||
crate::{
|
crate::{
|
||||||
accounts::Accounts,
|
accounts::Accounts,
|
||||||
accounts_db::AccountStorageEntry,
|
accounts_db::AccountStorageEntry,
|
||||||
accounts_hash::AccountsHash,
|
accounts_hash::{AccountsHash, AccountsHashEnum},
|
||||||
bank::Bank,
|
bank::Bank,
|
||||||
epoch_accounts_hash::EpochAccountsHash,
|
epoch_accounts_hash::EpochAccountsHash,
|
||||||
rent_collector::RentCollector,
|
rent_collector::RentCollector,
|
||||||
|
@ -251,7 +251,7 @@ pub struct SnapshotPackage {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SnapshotPackage {
|
impl SnapshotPackage {
|
||||||
pub fn new(accounts_package: AccountsPackage, accounts_hash: AccountsHash) -> Self {
|
pub fn new(accounts_package: AccountsPackage, accounts_hash: AccountsHashEnum) -> Self {
|
||||||
let AccountsPackageType::Snapshot(snapshot_type) = accounts_package.package_type else {
|
let AccountsPackageType::Snapshot(snapshot_type) = accounts_package.package_type else {
|
||||||
panic!("The AccountsPackage must be of type Snapshot in order to make a SnapshotPackage!");
|
panic!("The AccountsPackage must be of type Snapshot in order to make a SnapshotPackage!");
|
||||||
};
|
};
|
||||||
|
|
|
@ -2571,7 +2571,7 @@ pub fn package_and_archive_full_snapshot(
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash);
|
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash.into());
|
||||||
archive_snapshot_package(
|
archive_snapshot_package(
|
||||||
&snapshot_package,
|
&snapshot_package,
|
||||||
full_snapshot_archives_dir,
|
full_snapshot_archives_dir,
|
||||||
|
@ -2625,7 +2625,7 @@ pub fn package_and_archive_incremental_snapshot(
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash);
|
let snapshot_package = SnapshotPackage::new(accounts_package, accounts_hash.into());
|
||||||
archive_snapshot_package(
|
archive_snapshot_package(
|
||||||
&snapshot_package,
|
&snapshot_package,
|
||||||
full_snapshot_archives_dir,
|
full_snapshot_archives_dir,
|
||||||
|
|
Loading…
Reference in New Issue