Skip to content

Commit

Permalink
add AccountsHashConfig to manage parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Mar 22, 2022
1 parent 359e2de commit 8132317
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 61 deletions.
18 changes: 10 additions & 8 deletions core/src/accounts_hash_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ impl AccountsHashVerifier {
if let Some(expected_hash) = accounts_package.hash_for_testing {
let sorted_storages = SortedStorages::new(&accounts_package.snapshot_storages);
let (hash, lamports) = AccountsDb::calculate_accounts_hash_without_index(
ledger_path,
&sorted_storages,
thread_pool,
HashStats::default(),
false,
None,
None, // this will fail with filler accounts
None, // this code path is only for testing, so use default # passes here
&mut AccountsHash::AccountsHashConfig {
accounts_hash_cache_path: ledger_path,
storages: &sorted_storages,
thread_pool,
stats: HashStats::default(),
check_hash: false,
accounts_cache_and_ancestors: None,
filler_account_suffix: None, // this will fail with filler accounts
num_hash_scan_passes: None, // this code path is only for testing, so use default # passes here
},
)
.unwrap();

Expand Down
95 changes: 44 additions & 51 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use {
account_info::{AccountInfo, Offset, StorageLocation, StoredSize},
accounts_background_service::{DroppedSlotsSender, SendDroppedBankCallback},
accounts_cache::{AccountsCache, CachedAccount, SlotCache},
accounts_hash::{AccountsHash, CalculateHashIntermediate, HashStats, PreviousPass},
accounts_hash::{
AccountsHash, AccountsHashConfig, CalculateHashIntermediate, HashStats, PreviousPass,
},
accounts_index::{
AccountIndexGetResult, AccountSecondaryIndexes, AccountsIndex, AccountsIndexConfig,
AccountsIndexRootsStats, IndexKey, IndexValue, IsCached, RefCount, ScanConfig,
Expand Down Expand Up @@ -979,7 +981,7 @@ struct RemoveUnrootedSlotsSynchronization {
signal: Condvar,
}

type AccountInfoAccountsIndex = AccountsIndex<AccountInfo>;
pub type AccountInfoAccountsIndex = AccountsIndex<AccountInfo>;

// This structure handles the load/store of the accounts
#[derive(Debug)]
Expand Down Expand Up @@ -5545,20 +5547,20 @@ impl AccountsDb {
} else {
Some(&self.thread_pool_clean)
};
Self::calculate_accounts_hash_without_index(
&self.accounts_hash_cache_path,
&storages,
Self::calculate_accounts_hash_without_index(&mut AccountsHashConfig {
accounts_hash_cache_path: &self.accounts_hash_cache_path,
storages: &storages,
thread_pool,
timings,
stats: timings,
check_hash,
accounts_cache_and_ancestors,
if self.filler_account_count > 0 {
filler_account_suffix: if self.filler_account_count > 0 {
self.filler_account_suffix.as_ref()
} else {
None
},
self.num_hash_scan_passes,
)
num_hash_scan_passes: self.num_hash_scan_passes,
})
} else {
self.calculate_accounts_hash(slot, ancestors, check_hash)
}
Expand Down Expand Up @@ -5749,25 +5751,16 @@ impl AccountsDb {
// modeled after get_accounts_delta_hash
// intended to be faster than calculate_accounts_hash
pub fn calculate_accounts_hash_without_index(
accounts_hash_cache_path: &Path,
storages: &SortedStorages,
thread_pool: Option<&ThreadPool>,
mut stats: HashStats,
check_hash: bool,
accounts_cache_and_ancestors: Option<(
&AccountsCache,
&Ancestors,
&AccountInfoAccountsIndex,
)>,
filler_account_suffix: Option<&Pubkey>,
num_hash_scan_passes: Option<usize>,
config: &mut AccountsHashConfig<'_>,
) -> Result<(Hash, u64), BankHashVerificationError> {
let (num_hash_scan_passes, bins_per_pass) = Self::bins_per_pass(num_hash_scan_passes);
let (num_hash_scan_passes, bins_per_pass) =
Self::bins_per_pass(config.num_hash_scan_passes);
let thread_pool = config.thread_pool.clone();
let mut scan_and_hash = move || {
let mut previous_pass = PreviousPass::default();
let mut final_result = (Hash::default(), 0);

let cache_hash_data = CacheHashData::new(&accounts_hash_cache_path);
let cache_hash_data = CacheHashData::new(&config.accounts_hash_cache_path);

for pass in 0..num_hash_scan_passes {
let bounds = Range {
Expand All @@ -5777,21 +5770,21 @@ impl AccountsDb {

let result = Self::scan_snapshot_stores_with_cache(
&cache_hash_data,
storages,
&mut stats,
config.storages,
&mut config.stats,
PUBKEY_BINS_FOR_CALCULATING_HASHES,
&bounds,
check_hash,
accounts_cache_and_ancestors,
filler_account_suffix,
config.check_hash,
config.accounts_cache_and_ancestors,
config.filler_account_suffix,
)?;

let hash = AccountsHash {
filler_account_suffix: filler_account_suffix.cloned(),
filler_account_suffix: config.filler_account_suffix.cloned(),
};
let (hash, lamports, for_next_pass) = hash.rest_of_hash_calculation(
result,
&mut stats,
&mut config.stats,
pass == num_hash_scan_passes - 1,
previous_pass,
bins_per_pass,
Expand All @@ -5802,7 +5795,7 @@ impl AccountsDb {

info!(
"calculate_accounts_hash_without_index: slot (exclusive): {} {:?}",
storages.range().end,
config.storages.range().end,
final_result
);
Ok(final_result)
Expand Down Expand Up @@ -7930,16 +7923,16 @@ pub mod tests {
solana_logger::setup();

let (storages, _size, _slot_expected) = sample_storage();
let result = AccountsDb::calculate_accounts_hash_without_index(
TempDir::new().unwrap().path(),
&get_storage_refs(&storages),
None,
HashStats::default(),
false,
None,
None,
None,
)
let result = AccountsDb::calculate_accounts_hash_without_index(&mut AccountsHashConfig {
accounts_hash_cache_path: TempDir::new().unwrap().path(),
storages: &get_storage_refs(&storages),
thread_pool: None,
stats: HashStats::default(),
check_hash: false,
accounts_cache_and_ancestors: None,
filler_account_suffix: None,
num_hash_scan_passes: None,
})
.unwrap();
let expected_hash = Hash::from_str("GKot5hBsd81kMupNCXHaqbhv3huEbxAFMLnpcX2hniwn").unwrap();
assert_eq!(result, (expected_hash, 0));
Expand All @@ -7955,16 +7948,16 @@ pub mod tests {
item.hash
});
let sum = raw_expected.iter().map(|item| item.lamports).sum();
let result = AccountsDb::calculate_accounts_hash_without_index(
TempDir::new().unwrap().path(),
&get_storage_refs(&storages),
None,
HashStats::default(),
false,
None,
None,
None,
)
let result = AccountsDb::calculate_accounts_hash_without_index(&mut AccountsHashConfig {
accounts_hash_cache_path: TempDir::new().unwrap().path(),
storages: &get_storage_refs(&storages),
thread_pool: None,
stats: HashStats::default(),
check_hash: false,
accounts_cache_and_ancestors: None,
filler_account_suffix: None,
num_hash_scan_passes: None,
})
.unwrap();

assert_eq!(result, (expected_hash, sum));
Expand Down
30 changes: 28 additions & 2 deletions runtime/src/accounts_hash.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use {
crate::{
accounts_cache::AccountsCache, accounts_db::AccountInfoAccountsIndex, ancestors::Ancestors,
sorted_storages::SortedStorages,
},
log::*,
rayon::prelude::*,
rayon::{prelude::*, ThreadPool},
solana_measure::measure::Measure,
solana_sdk::{
hash::{Hash, Hasher},
pubkey::Pubkey,
},
std::{borrow::Borrow, convert::TryInto, sync::Mutex},
std::{borrow::Borrow, convert::TryInto, path::Path, sync::Mutex},
};
pub const ZERO_RAW_LAMPORTS_SENTINEL: u64 = std::u64::MAX;
pub const MERKLE_FANOUT: usize = 16;
Expand All @@ -18,6 +22,28 @@ pub struct PreviousPass {
pub lamports: u64,
}

/// parameters to calculate accounts hash
pub struct AccountsHashConfig<'a> {
pub accounts_hash_cache_path: &'a Path,
pub storages: &'a SortedStorages<'a>,
pub thread_pool: Option<&'a ThreadPool>,
pub stats: HashStats,
pub check_hash: bool,
pub accounts_cache_and_ancestors: Option<(
&'a AccountsCache,
&'a Ancestors,
&'a AccountInfoAccountsIndex,
)>,
// these should be gone soon as we get an AccountsDb '&self'
pub filler_account_suffix: Option<&'a Pubkey>,
pub num_hash_scan_passes: Option<usize>,
// to come soon
/*
pub rent_collector: RentCollector,
pub epoch_schedule: EpochSchedule,
*/
}

#[derive(Debug, Default)]
pub struct HashStats {
pub scan_time_total_us: u64,
Expand Down

0 comments on commit 8132317

Please sign in to comment.