Skip to content

Commit

Permalink
Promotes accounts hash to a strong type
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo committed Nov 22, 2022
1 parent ed2c59d commit 1da7740
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 116 deletions.
2 changes: 1 addition & 1 deletion accounts-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions core/src/accounts_hash_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -346,17 +346,17 @@ impl AccountsHashVerifier {
hashes: &mut Vec<(Slot, Hash)>,
exit: &Arc<AtomicBool>,
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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/tests/epoch_accounts_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 7 additions & 5 deletions core/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -277,13 +278,14 @@ fn run_bank_forks_snapshot_n<F>(
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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
);

Expand Down
36 changes: 15 additions & 21 deletions runtime/src/accounts_background_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use {
log::*,
rand::{thread_rng, Rng},
solana_measure::measure::Measure,
solana_sdk::{
clock::{BankId, Slot},
hash::Hash,
},
solana_sdk::clock::{BankId, Slot},
stats::StatsManager,
std::{
boxed::Box,
Expand Down Expand Up @@ -294,17 +291,15 @@ impl SnapshotRequestHandler {
*last_full_snapshot_slot = Some(snapshot_root_bank.slot());
}

let previous_hash = if test_hash_calculation {
let previous_accounts_hash = test_hash_calculation.then(|| {
// We have to use the index version here.
// We cannot calculate the non-index way because cache has not been flushed and stores don't match reality.
snapshot_root_bank.update_accounts_hash(
CalcAccountsHashDataSource::IndexForTests,
false,
false,
)
} else {
Hash::default()
};
});

let mut shrink_time = Measure::start("shrink_time");
if !accounts_db_caching_enabled {
Expand Down Expand Up @@ -334,10 +329,10 @@ impl SnapshotRequestHandler {
}
flush_accounts_cache_time.stop();

let hash_for_testing = if test_hash_calculation {
let accounts_hash_for_testing = previous_accounts_hash.map(|previous_accounts_hash| {
let check_hash = false;

let (this_hash, capitalization) = snapshot_root_bank
let (this_accounts_hash, capitalization) = snapshot_root_bank
.accounts()
.accounts_db
.calculate_accounts_hash(
Expand All @@ -354,12 +349,10 @@ impl SnapshotRequestHandler {
},
)
.unwrap();
assert_eq!(previous_hash, this_hash);
assert_eq!(previous_accounts_hash, this_accounts_hash);
assert_eq!(capitalization, snapshot_root_bank.capitalization());
Some(this_hash)
} else {
None
};
this_accounts_hash
});

let mut clean_time = Measure::start("clean_time");
snapshot_root_bank.clean_accounts(*last_full_snapshot_slot);
Expand Down Expand Up @@ -394,7 +387,7 @@ impl SnapshotRequestHandler {
snapshot_storages,
self.snapshot_config.archive_format,
self.snapshot_config.snapshot_version,
hash_for_testing,
accounts_hash_for_testing,
)
.expect("new accounts package for snapshot")
}
Expand All @@ -404,7 +397,7 @@ impl SnapshotRequestHandler {
accounts_package_type,
&snapshot_root_bank,
snapshot_storages,
hash_for_testing,
accounts_hash_for_testing,
)
}
};
Expand All @@ -413,12 +406,11 @@ impl SnapshotRequestHandler {
.expect("send accounts package");
snapshot_time.stop();
info!(
"Took bank snapshot. accounts package type: {:?}, slot: {}, accounts hash: {}, bank hash: {}",
"Took bank snapshot. accounts package type: {:?}, slot: {}, bank hash: {}",
accounts_package_type,
snapshot_root_bank.slot(),
snapshot_root_bank.get_accounts_hash(),
snapshot_root_bank.hash(),
);
);

// Cleanup outdated snapshots
let mut purge_old_snapshots_time = Measure::start("purge_old_snapshots_time");
Expand Down Expand Up @@ -785,7 +777,9 @@ mod test {
genesis_utils::create_genesis_config,
},
crossbeam_channel::unbounded,
solana_sdk::{account::AccountSharedData, epoch_schedule::EpochSchedule, pubkey::Pubkey},
solana_sdk::{
account::AccountSharedData, epoch_schedule::EpochSchedule, hash::Hash, pubkey::Pubkey,
},
};

#[test]
Expand Down
Loading

0 comments on commit 1da7740

Please sign in to comment.