From 1663a7091a5d641502e95aedfbf19d5eb8dd67a8 Mon Sep 17 00:00:00 2001 From: Brooks Date: Thu, 13 Jul 2023 18:06:11 -0400 Subject: [PATCH] Uses checked math in EAH tests (#32483) --- core/tests/epoch_accounts_hash.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/core/tests/epoch_accounts_hash.rs b/core/tests/epoch_accounts_hash.rs index d9ed1fa616..36833f5b0d 100755 --- a/core/tests/epoch_accounts_hash.rs +++ b/core/tests/epoch_accounts_hash.rs @@ -1,4 +1,3 @@ -#![allow(clippy::integer_arithmetic)] use { crate::snapshot_utils::create_tmp_accounts_dir_for_tests, log::*, @@ -90,13 +89,15 @@ impl TestEnvironment { #[must_use] fn _new(snapshot_config: SnapshotConfig) -> TestEnvironment { + const MINT_LAMPORTS: u64 = 100_000 * LAMPORTS_PER_SOL; + const STAKE_LAMPORTS: u64 = 100 * LAMPORTS_PER_SOL; let bank_snapshots_dir = TempDir::new().unwrap(); let full_snapshot_archives_dir = TempDir::new().unwrap(); let incremental_snapshot_archives_dir = TempDir::new().unwrap(); let mut genesis_config_info = genesis_utils::create_genesis_config_with_leader( - 100_000 * LAMPORTS_PER_SOL, // mint_lamports - &Pubkey::new_unique(), // validator_pubkey - 100 * LAMPORTS_PER_SOL, // validator_stake_lamports + MINT_LAMPORTS, + &Pubkey::new_unique(), + STAKE_LAMPORTS, ); genesis_config_info.genesis_config.epoch_schedule = EpochSchedule::custom(Self::SLOTS_PER_EPOCH, Self::SLOTS_PER_EPOCH, false); @@ -266,13 +267,13 @@ fn test_epoch_accounts_hash_basic(test_environment: TestEnvironment) { .genesis_config .epoch_schedule .slots_per_epoch; - for _ in 0..slots_per_epoch * NUM_EPOCHS_TO_TEST { + for _ in 0..slots_per_epoch.checked_mul(NUM_EPOCHS_TO_TEST).unwrap() { let bank = { let parent = bank_forks.read().unwrap().working_bank(); let bank = bank_forks.write().unwrap().insert(Bank::new_from_parent( &parent, &Pubkey::default(), - parent.slot() + 1, + parent.slot().checked_add(1).unwrap(), )); let transaction = system_transaction::transfer( @@ -289,7 +290,7 @@ fn test_epoch_accounts_hash_basic(test_environment: TestEnvironment) { trace!("new bank {}", bank.slot()); // Set roots so that ABS requests are sent (this is what requests EAH calculations) - if bank.slot() % SET_ROOT_INTERVAL == 0 { + if bank.slot().checked_rem(SET_ROOT_INTERVAL).unwrap() == 0 { trace!("rooting bank {}", bank.slot()); bank_forks.write().unwrap().set_root( bank.slot(), @@ -378,7 +379,7 @@ fn test_snapshots_have_expected_epoch_accounts_hash() { .genesis_config .epoch_schedule .slots_per_epoch; - for _ in 0..slots_per_epoch * NUM_EPOCHS_TO_TEST { + for _ in 0..slots_per_epoch.checked_mul(NUM_EPOCHS_TO_TEST).unwrap() { let bank = { let parent = bank_forks.read().unwrap().working_bank(); let bank = bank_forks.write().unwrap().insert(Bank::new_from_parent( @@ -494,7 +495,7 @@ fn test_background_services_request_handling_for_epoch_accounts_hash() { .genesis_config .epoch_schedule .slots_per_epoch; - for _ in 0..slots_per_epoch * NUM_EPOCHS_TO_TEST { + for _ in 0..slots_per_epoch.checked_mul(NUM_EPOCHS_TO_TEST).unwrap() { let bank = { let parent = bank_forks.read().unwrap().working_bank(); let bank = bank_forks.write().unwrap().insert(Bank::new_from_parent( @@ -656,6 +657,7 @@ fn test_epoch_accounts_hash_and_warping() { // https://github.com/rust-lang/rust/pull/88582 // https://github.com/jhpratt/rust/blob/727a4fc7e3f836938dfeb4a2ab237cfca612222d/library/core/src/num/uint_macros.rs#L1811-L1837 const fn next_multiple_of(lhs: u64, rhs: u64) -> u64 { + #![allow(clippy::integer_arithmetic)] match lhs % rhs { 0 => lhs, r => lhs + (rhs - r),