Skip to content

Commit 2e3fbd5

Browse files
authored
Refactors Bank::verify_accounts_hash() (#7237)
1 parent a2ce908 commit 2e3fbd5

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

runtime/src/bank.rs

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,11 +4533,10 @@ impl Bank {
45334533
mut config: VerifyAccountsHashConfig,
45344534
duplicates_lt_hash: Option<Box<DuplicatesLtHash>>,
45354535
) -> bool {
4536-
let accounts = &self.rc.accounts;
4536+
let accounts_db = &self.rc.accounts.accounts_db;
45374537
// Wait until initial hash calc is complete before starting a new hash calc.
45384538
// This should only occur when we halt at a slot in ledger-tool.
4539-
accounts
4540-
.accounts_db
4539+
accounts_db
45414540
.verify_accounts_hash_in_bg
45424541
.join_background_thread();
45434542

@@ -4550,7 +4549,7 @@ impl Bank {
45504549
config.run_in_background = false;
45514550
}
45524551

4553-
if config.require_rooted_bank && !accounts.accounts_db.accounts_index.is_alive_root(slot) {
4552+
if config.require_rooted_bank && !accounts_db.accounts_index.is_alive_root(slot) {
45544553
if let Some(parent) = self.parent() {
45554554
info!(
45564555
"slot {slot} is not a root, so verify accounts hash on parent bank at slot {}",
@@ -4568,43 +4567,47 @@ impl Bank {
45684567
}
45694568
}
45704569

4570+
fn check_lt_hash(
4571+
expected_accounts_lt_hash: &AccountsLtHash,
4572+
calculated_accounts_lt_hash: &AccountsLtHash,
4573+
) -> bool {
4574+
let is_ok = calculated_accounts_lt_hash == expected_accounts_lt_hash;
4575+
if !is_ok {
4576+
let expected = expected_accounts_lt_hash.0.checksum();
4577+
let calculated = calculated_accounts_lt_hash.0.checksum();
4578+
error!(
4579+
"Verifying accounts failed: accounts lattice hashes do not match, \
4580+
expected: {expected}, calculated: {calculated}",
4581+
);
4582+
}
4583+
is_ok
4584+
}
4585+
45714586
// The snapshot storages must be captured *before* starting the background verification.
45724587
// Otherwise, it is possible that a delayed call to `get_snapshot_storages()` will *not*
45734588
// get the correct storages required to calculate and verify the accounts hashes.
4574-
let snapshot_storages = self.rc.accounts.accounts_db.get_storages(RangeFull);
4575-
4576-
info!(
4577-
"Verifying accounts, in background? {}",
4578-
config.run_in_background,
4579-
);
4589+
let snapshot_storages = accounts_db.get_storages(RangeFull);
4590+
let expected_accounts_lt_hash = self.accounts_lt_hash.lock().unwrap().clone();
45804591
if config.run_in_background {
4581-
let accounts = Arc::clone(accounts);
4582-
let accounts_ = Arc::clone(&accounts);
4583-
let expected_accounts_lt_hash = self.accounts_lt_hash.lock().unwrap().clone();
4584-
accounts.accounts_db.verify_accounts_hash_in_bg.start(|| {
4592+
let accounts_db_ = Arc::clone(accounts_db);
4593+
accounts_db.verify_accounts_hash_in_bg.start(|| {
45854594
Builder::new()
45864595
.name("solBgHashVerify".into())
45874596
.spawn(move || {
45884597
info!("Initial background accounts hash verification has started");
45894598
let start = Instant::now();
4590-
let accounts_db = &accounts_.accounts_db;
45914599
let (calculated_accounts_lt_hash, lattice_verify_time) =
4592-
meas_dur!(accounts_db.thread_pool_hash.install(|| {
4593-
accounts_db.calculate_accounts_lt_hash_at_startup_from_storages(
4600+
meas_dur!(accounts_db_.thread_pool_hash.install(|| {
4601+
accounts_db_.calculate_accounts_lt_hash_at_startup_from_storages(
45944602
snapshot_storages.0.as_slice(),
45954603
&duplicates_lt_hash.unwrap(),
45964604
)
45974605
}));
4598-
let is_ok = calculated_accounts_lt_hash == expected_accounts_lt_hash;
4599-
if !is_ok {
4600-
let expected = expected_accounts_lt_hash.0.checksum();
4601-
let calculated = calculated_accounts_lt_hash.0.checksum();
4602-
error!(
4603-
"Verifying accounts failed: accounts lattice hashes do not \
4604-
match, expected: {expected}, calculated: {calculated}",
4605-
);
4606-
}
4607-
accounts_db.verify_accounts_hash_in_bg.background_finished();
4606+
let is_ok =
4607+
check_lt_hash(&expected_accounts_lt_hash, &calculated_accounts_lt_hash);
4608+
accounts_db_
4609+
.verify_accounts_hash_in_bg
4610+
.background_finished();
46084611
let total_time = start.elapsed();
46094612
datapoint_info!(
46104613
"startup_verify_accounts",
@@ -4615,35 +4618,22 @@ impl Bank {
46154618
i64
46164619
),
46174620
);
4618-
info!("Initial background accounts hash verification has stopped");
4621+
info!("Initial background accounts hash verification has stopped in {total_time:?}");
46194622
is_ok
46204623
})
46214624
.unwrap()
46224625
});
46234626
true // initial result is true. We haven't failed yet. If verification fails, we'll panic from bg thread.
46244627
} else {
4625-
let expected_accounts_lt_hash = self.accounts_lt_hash.lock().unwrap().clone();
46264628
let calculated_accounts_lt_hash = if let Some(duplicates_lt_hash) = duplicates_lt_hash {
4627-
accounts
4628-
.accounts_db
4629-
.calculate_accounts_lt_hash_at_startup_from_storages(
4630-
snapshot_storages.0.as_slice(),
4631-
&duplicates_lt_hash,
4632-
)
4629+
accounts_db.calculate_accounts_lt_hash_at_startup_from_storages(
4630+
snapshot_storages.0.as_slice(),
4631+
&duplicates_lt_hash,
4632+
)
46334633
} else {
4634-
accounts
4635-
.accounts_db
4636-
.calculate_accounts_lt_hash_at_startup_from_index(&self.ancestors, slot)
4634+
accounts_db.calculate_accounts_lt_hash_at_startup_from_index(&self.ancestors, slot)
46374635
};
4638-
let is_ok = calculated_accounts_lt_hash == expected_accounts_lt_hash;
4639-
if !is_ok {
4640-
let expected = expected_accounts_lt_hash.0.checksum();
4641-
let calculated = calculated_accounts_lt_hash.0.checksum();
4642-
error!(
4643-
"Verifying accounts failed: accounts lattice hashes do not \
4644-
match, expected: {expected}, calculated: {calculated}",
4645-
);
4646-
}
4636+
let is_ok = check_lt_hash(&expected_accounts_lt_hash, &calculated_accounts_lt_hash);
46474637
self.set_initial_accounts_hash_verification_completed();
46484638
is_ok
46494639
}

0 commit comments

Comments
 (0)