pass expected capitalization to hash calculation to improve assert msg (#15191)

* cleanup if

* pass expected capitalization to hash calculation to improve assert message

* fix bank function

* one more level

* calculate_accounts_hash_helper

* add slot to error message

* success
This commit is contained in:
Jeff Washington (jwash) 2021-02-10 14:38:00 -06:00 committed by GitHub
parent c1e93b3ef9
commit e59a24d9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 18 deletions

View File

@ -108,11 +108,12 @@ fn main() {
time.stop();
let mut time_store = Measure::start("hash using store");
let results_store = accounts.accounts_db.update_accounts_hash_with_index_option(
true,
false,
false,
solana_sdk::clock::Slot::default(),
&ancestors,
true,
None,
);
time_store.stop();
if results != results_store {

View File

@ -121,13 +121,14 @@ impl SnapshotRequestHandler {
flush_accounts_cache_time.stop();
let mut hash_time = Measure::start("hash_time");
let mut hash_for_testing = None;
const USE_INDEX: bool = true;
snapshot_root_bank
.update_accounts_hash_with_index_option(!USE_INDEX, test_hash_calculation);
if test_hash_calculation {
hash_for_testing = Some(snapshot_root_bank.get_accounts_hash());
}
.update_accounts_hash_with_index_option(USE_INDEX, test_hash_calculation);
let hash_for_testing = if test_hash_calculation {
Some(snapshot_root_bank.get_accounts_hash())
} else {
None
};
hash_time.stop();
let mut clean_time = Measure::start("clean_time");

View File

@ -3624,11 +3624,12 @@ impl AccountsDB {
simple_capitalization_enabled: bool,
) -> (Hash, u64) {
self.update_accounts_hash_with_index_option(
false,
true,
false,
slot,
ancestors,
simple_capitalization_enabled,
None,
)
}
@ -3639,11 +3640,12 @@ impl AccountsDB {
simple_capitalization_enabled: bool,
) -> (Hash, u64) {
self.update_accounts_hash_with_index_option(
false,
true,
true,
slot,
ancestors,
simple_capitalization_enabled,
None,
)
}
@ -3866,12 +3868,12 @@ impl AccountsDB {
fn calculate_accounts_hash_helper(
&self,
do_not_use_index: bool,
use_index: bool,
slot: Slot,
ancestors: &Ancestors,
simple_capitalization_enabled: bool,
) -> (Hash, u64) {
if do_not_use_index {
if !use_index {
let combined_maps = self.get_snapshot_storages(slot);
Self::calculate_accounts_hash_without_index(
@ -3887,14 +3889,15 @@ impl AccountsDB {
pub fn update_accounts_hash_with_index_option(
&self,
do_not_use_index: bool,
use_index: bool,
debug_verify: bool,
slot: Slot,
ancestors: &Ancestors,
simple_capitalization_enabled: bool,
expected_capitalization: Option<u64>,
) -> (Hash, u64) {
let (hash, total_lamports) = self.calculate_accounts_hash_helper(
do_not_use_index,
use_index,
slot,
ancestors,
simple_capitalization_enabled,
@ -3902,14 +3905,16 @@ impl AccountsDB {
if debug_verify {
// calculate the other way (store or non-store) and verify results match.
let (hash_other, total_lamports_other) = self.calculate_accounts_hash_helper(
!do_not_use_index,
!use_index,
slot,
ancestors,
simple_capitalization_enabled,
);
assert_eq!(hash, hash_other);
assert_eq!(total_lamports, total_lamports_other);
let success = hash == hash_other
&& total_lamports == total_lamports_other
&& total_lamports == expected_capitalization.unwrap_or(total_lamports);
assert!(success, "update_accounts_hash_with_index_option mismatch. hashes: {}, {}; lamports: {}, {}; expected lamports: {:?}, using index: {}, slot: {}", hash, hash_other, total_lamports, total_lamports_other, expected_capitalization, use_index, slot);
}
let mut bank_hashes = self.bank_hashes.write().unwrap();
let mut bank_hash_info = bank_hashes.get_mut(&slot).unwrap();

View File

@ -4280,7 +4280,7 @@ impl Bank {
pub fn update_accounts_hash_with_index_option(
&self,
do_not_use_index: bool,
use_index: bool,
debug_verify: bool,
) -> Hash {
let (hash, total_lamports) = self
@ -4288,18 +4288,19 @@ impl Bank {
.accounts
.accounts_db
.update_accounts_hash_with_index_option(
do_not_use_index,
use_index,
debug_verify,
self.slot(),
&self.ancestors,
self.simple_capitalization_enabled(),
Some(self.capitalization()),
);
assert_eq!(total_lamports, self.capitalization());
hash
}
pub fn update_accounts_hash(&self) -> Hash {
self.update_accounts_hash_with_index_option(false, false)
self.update_accounts_hash_with_index_option(true, false)
}
/// A snapshot bank should be purged of 0 lamport accounts which are not part of the hash