From e8bf812a8a9e1dfbac3aa22c0ae114ba59f0e7fd Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Wed, 4 May 2022 16:27:49 -0500 Subject: [PATCH] allow ledger-tool halt at slot to calc hash using write cache (#24974) --- ledger/src/blockstore_processor.rs | 3 ++- runtime/benches/accounts.rs | 3 ++- runtime/src/accounts.rs | 2 ++ runtime/src/accounts_db.rs | 30 +++++++++++++++++++++--------- runtime/src/bank.rs | 29 +++++++++++++++++------------ 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/ledger/src/blockstore_processor.rs b/ledger/src/blockstore_processor.rs index 4fb29953d..df0b8ec57 100644 --- a/ledger/src/blockstore_processor.rs +++ b/ledger/src/blockstore_processor.rs @@ -1309,7 +1309,8 @@ fn load_frozen_forks( if slot >= halt_at_slot { bank.force_flush_accounts_cache(); - let _ = bank.verify_bank_hash(false); + let can_cached_slot_be_unflushed = true; + let _ = bank.verify_bank_hash(false, can_cached_slot_be_unflushed); break; } } diff --git a/runtime/benches/accounts.rs b/runtime/benches/accounts.rs index 24ad6761d..6166185f0 100644 --- a/runtime/benches/accounts.rs +++ b/runtime/benches/accounts.rs @@ -123,7 +123,8 @@ fn test_accounts_hash_bank_hash(bencher: &mut Bencher) { total_lamports, test_hash_calculation, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false )) }); } diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index 529c4060c..848ac0d86 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -790,6 +790,7 @@ impl Accounts { test_hash_calculation: bool, epoch_schedule: &EpochSchedule, rent_collector: &RentCollector, + can_cached_slot_be_unflushed: bool, ) -> bool { if let Err(err) = self.accounts_db.verify_bank_hash_and_lamports_new( slot, @@ -798,6 +799,7 @@ impl Accounts { test_hash_calculation, epoch_schedule, rent_collector, + can_cached_slot_be_unflushed, ) { warn!("verify_bank_hash failed: {:?}", err); false diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 580163397..2cb7e3e12 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -5958,6 +5958,7 @@ impl AccountsDb { test_hash_calculation: bool, epoch_schedule: &EpochSchedule, rent_collector: &RentCollector, + can_cached_slot_be_unflushed: bool, ) -> Result<(), BankHashVerificationError> { self.verify_bank_hash_and_lamports_new( slot, @@ -5966,6 +5967,7 @@ impl AccountsDb { test_hash_calculation, epoch_schedule, rent_collector, + can_cached_slot_be_unflushed, ) } @@ -5978,13 +5980,13 @@ impl AccountsDb { test_hash_calculation: bool, epoch_schedule: &EpochSchedule, rent_collector: &RentCollector, + can_cached_slot_be_unflushed: bool, ) -> Result<(), BankHashVerificationError> { use BankHashVerificationError::*; let use_index = false; let check_hash = false; // this will not be supported anymore let is_startup = true; - let can_cached_slot_be_unflushed = false; let (calculated_hash, calculated_lamports) = self .calculate_accounts_hash_helper_with_verify( use_index, @@ -9958,6 +9960,7 @@ pub mod tests { true, &EpochSchedule::default(), &RentCollector::default(), + false, ) .unwrap(); } @@ -10343,7 +10346,8 @@ pub mod tests { 1, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false, ), Ok(_) ); @@ -10356,7 +10360,8 @@ pub mod tests { 1, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false, ), Err(MissingBankHash) ); @@ -10378,7 +10383,8 @@ pub mod tests { 1, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false, ), Err(MismatchedBankHash) ); @@ -10406,7 +10412,8 @@ pub mod tests { 1, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false ), Ok(_) ); @@ -10427,13 +10434,14 @@ pub mod tests { 2, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false ), Ok(_) ); assert_matches!( - db.verify_bank_hash_and_lamports(some_slot, &ancestors, 10, true, &EpochSchedule::default(), &RentCollector::default()), + db.verify_bank_hash_and_lamports(some_slot, &ancestors, 10, true, &EpochSchedule::default(), &RentCollector::default(), false), Err(MismatchedTotalLamports(expected, actual)) if expected == 2 && actual == 10 ); } @@ -10459,7 +10467,8 @@ pub mod tests { 0, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false ), Ok(_) ); @@ -10496,7 +10505,8 @@ pub mod tests { 1, true, &EpochSchedule::default(), - &RentCollector::default() + &RentCollector::default(), + false ), Err(MismatchedBankHash) ); @@ -11115,6 +11125,7 @@ pub mod tests { true, &EpochSchedule::default(), &RentCollector::default(), + false, ) .unwrap(); @@ -11127,6 +11138,7 @@ pub mod tests { true, &EpochSchedule::default(), &RentCollector::default(), + false, ) .unwrap(); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index c8093995f..640f0871a 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -6251,7 +6251,11 @@ impl Bank { /// snapshot. /// Only called from startup or test code. #[must_use] - pub fn verify_bank_hash(&self, test_hash_calculation: bool) -> bool { + pub fn verify_bank_hash( + &self, + test_hash_calculation: bool, + can_cached_slot_be_unflushed: bool, + ) -> bool { self.rc.accounts.verify_bank_hash_and_lamports( self.slot(), &self.ancestors, @@ -6259,6 +6263,7 @@ impl Bank { test_hash_calculation, self.epoch_schedule(), &self.rent_collector, + can_cached_slot_be_unflushed, ) } @@ -6450,7 +6455,7 @@ impl Bank { info!("verify_bank_hash.."); let mut verify_time = Measure::start("verify_bank_hash"); - let mut verify = self.verify_bank_hash(test_hash_calculation); + let mut verify = self.verify_bank_hash(test_hash_calculation, false); verify_time.stop(); info!("verify_hash.."); @@ -9540,17 +9545,17 @@ pub(crate) mod tests { assert_eq!(bank0.get_account(&keypair.pubkey()).unwrap().lamports(), 10); assert_eq!(bank1.get_account(&keypair.pubkey()), None); - assert!(bank0.verify_bank_hash(true)); + assert!(bank0.verify_bank_hash(true, false)); // Squash and then verify hash_internal value bank0.freeze(); bank0.squash(); - assert!(bank0.verify_bank_hash(true)); + assert!(bank0.verify_bank_hash(true, false)); bank1.freeze(); bank1.squash(); bank1.update_accounts_hash(); - assert!(bank1.verify_bank_hash(true)); + assert!(bank1.verify_bank_hash(true, false)); // keypair should have 0 tokens on both forks assert_eq!(bank0.get_account(&keypair.pubkey()), None); @@ -9558,7 +9563,7 @@ pub(crate) mod tests { bank1.force_flush_accounts_cache(); bank1.clean_accounts(false, false, None); - assert!(bank1.verify_bank_hash(true)); + assert!(bank1.verify_bank_hash(true, false)); } #[test] @@ -10616,7 +10621,7 @@ pub(crate) mod tests { info!("transfer 2 {}", pubkey2); bank2.transfer(10, &mint_keypair, &pubkey2).unwrap(); bank2.update_accounts_hash(); - assert!(bank2.verify_bank_hash(true)); + assert!(bank2.verify_bank_hash(true, false)); } #[test] @@ -10640,19 +10645,19 @@ pub(crate) mod tests { // Checkpointing should never modify the checkpoint's state once frozen let bank0_state = bank0.hash_internal_state(); bank2.update_accounts_hash(); - assert!(bank2.verify_bank_hash(true)); + assert!(bank2.verify_bank_hash(true, false)); let bank3 = Bank::new_from_parent(&bank0, &solana_sdk::pubkey::new_rand(), 2); assert_eq!(bank0_state, bank0.hash_internal_state()); - assert!(bank2.verify_bank_hash(true)); + assert!(bank2.verify_bank_hash(true, false)); bank3.update_accounts_hash(); - assert!(bank3.verify_bank_hash(true)); + assert!(bank3.verify_bank_hash(true, false)); let pubkey2 = solana_sdk::pubkey::new_rand(); info!("transfer 2 {}", pubkey2); bank2.transfer(10, &mint_keypair, &pubkey2).unwrap(); bank2.update_accounts_hash(); - assert!(bank2.verify_bank_hash(true)); - assert!(bank3.verify_bank_hash(true)); + assert!(bank2.verify_bank_hash(true, false)); + assert!(bank3.verify_bank_hash(true, false)); } #[test]