From 9ad1712561851241f71efc929943967ef42b7f22 Mon Sep 17 00:00:00 2001 From: Lucas Ste <38472950+LucasSte@users.noreply.github.com> Date: Tue, 25 Jun 2024 15:41:42 -0300 Subject: [PATCH] Allow the registration of genesis hash and recent block hash for fuzzing (#1847) --- ledger/src/blockstore_processor.rs | 1 + program-test/src/lib.rs | 1 + runtime/src/bank.rs | 24 +++++++++++++++---- .../src/bank/partitioned_epoch_rewards/mod.rs | 2 ++ runtime/src/bank/tests.rs | 5 ++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/ledger/src/blockstore_processor.rs b/ledger/src/blockstore_processor.rs index 84deb781806768..2b95d40f9a8c78 100644 --- a/ledger/src/blockstore_processor.rs +++ b/ledger/src/blockstore_processor.rs @@ -843,6 +843,7 @@ pub(crate) fn process_blockstore_for_bank_0( accounts_update_notifier, None, exit, + None, ); let bank0_slot = bank0.slot(); let bank_forks = BankForks::new_rw_arc(bank0); diff --git a/program-test/src/lib.rs b/program-test/src/lib.rs index 37453f986b9519..58492d893aea80 100644 --- a/program-test/src/lib.rs +++ b/program-test/src/lib.rs @@ -863,6 +863,7 @@ impl ProgramTest { None, None, Arc::default(), + None, ); // Add commonly-used SPL programs as a convenience to the user diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 7f87dd5466e0c9..6a5dfd85d83c19 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -1029,6 +1029,7 @@ impl Bank { accounts_update_notifier: Option, #[allow(unused)] collector_id_for_tests: Option, exit: Arc, + #[allow(unused)] genesis_hash: Option, ) -> Self { let accounts_db = AccountsDb::new_with_config( paths, @@ -1050,7 +1051,7 @@ impl Bank { #[cfg(not(feature = "dev-context-only-utils"))] bank.process_genesis_config(genesis_config); #[cfg(feature = "dev-context-only-utils")] - bank.process_genesis_config(genesis_config, collector_id_for_tests); + bank.process_genesis_config(genesis_config, collector_id_for_tests, genesis_hash); bank.finish_init( genesis_config, @@ -2921,6 +2922,7 @@ impl Bank { &mut self, genesis_config: &GenesisConfig, #[cfg(feature = "dev-context-only-utils")] collector_id_for_tests: Option, + #[cfg(feature = "dev-context-only-utils")] genesis_hash: Option, ) { // Bootstrap validator collects fees until `new_from_parent` is called. self.fee_rate_governor = genesis_config.fee_rate_governor.clone(); @@ -2957,10 +2959,15 @@ impl Bank { self.collector_id = collector_id.expect("genesis processing failed because no staked nodes exist"); - self.blockhash_queue.write().unwrap().genesis_hash( - &genesis_config.hash(), - self.fee_rate_governor.lamports_per_signature, - ); + #[cfg(not(feature = "dev-context-only-utils"))] + let genesis_hash = genesis_config.hash(); + #[cfg(feature = "dev-context-only-utils")] + let genesis_hash = genesis_hash.unwrap_or(genesis_config.hash()); + + self.blockhash_queue + .write() + .unwrap() + .genesis_hash(&genesis_hash, self.fee_rate_governor.lamports_per_signature); self.hashes_per_tick = genesis_config.hashes_per_tick(); self.ticks_per_slot = genesis_config.ticks_per_slot(); @@ -3228,6 +3235,11 @@ impl Bank { ) } + #[cfg(feature = "dev-context-only-utils")] + pub fn register_recent_blockhash_for_test(&self, hash: &Hash) { + self.register_recent_blockhash(hash, &BankWithScheduler::no_scheduler_available()); + } + /// Tell the bank which Entry IDs exist on the ledger. This function assumes subsequent calls /// correspond to later entries, and will boot the oldest ones once its internal cache is full. /// Once boot, the bank will reject transactions using that `hash`. @@ -6897,6 +6909,7 @@ impl Bank { None, Some(Pubkey::new_unique()), Arc::default(), + None, ) } @@ -6920,6 +6933,7 @@ impl Bank { None, Some(Pubkey::new_unique()), Arc::default(), + None, ) } diff --git a/runtime/src/bank/partitioned_epoch_rewards/mod.rs b/runtime/src/bank/partitioned_epoch_rewards/mod.rs index 99323c990a0c12..ea405dce8e1316 100644 --- a/runtime/src/bank/partitioned_epoch_rewards/mod.rs +++ b/runtime/src/bank/partitioned_epoch_rewards/mod.rs @@ -379,6 +379,7 @@ mod tests { None, Some(Pubkey::new_unique()), Arc::default(), + None, ); // Fill bank_forks with banks with votes landing in the next slot @@ -486,6 +487,7 @@ mod tests { None, Some(Pubkey::new_unique()), Arc::default(), + None, ); let stake_account_stores_per_block = diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index 264485123da3ab..6cf2bded8de9ee 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -9072,6 +9072,7 @@ fn test_epoch_schedule_from_genesis_config() { None, None, Arc::default(), + None, )); assert_eq!(bank.epoch_schedule(), &genesis_config.epoch_schedule); @@ -9102,6 +9103,7 @@ where None, None, Arc::default(), + None, )); let vote_and_stake_accounts = load_vote_and_stake_accounts(&bank).vote_with_stake_delegations_map; @@ -12645,6 +12647,7 @@ fn test_rehash_with_skipped_rewrites() { None, Some(Pubkey::new_unique()), Arc::new(AtomicBool::new(false)), + None, )); // This test is only meaningful while the bank hash contains rewrites. // Once this feature is enabled, it may be possible to remove this test entirely. @@ -12706,6 +12709,7 @@ fn test_rebuild_skipped_rewrites() { None, Some(Pubkey::new_unique()), Arc::new(AtomicBool::new(false)), + None, )); // This test is only meaningful while the bank hash contains rewrites. // Once this feature is enabled, it may be possible to remove this test entirely. @@ -12816,6 +12820,7 @@ fn test_get_accounts_for_bank_hash_details(skip_rewrites: bool) { None, Some(Pubkey::new_unique()), Arc::new(AtomicBool::new(false)), + None, )); // This test is only meaningful while the bank hash contains rewrites. // Once this feature is enabled, it may be possible to remove this test entirely.