From 2af59926ed1ddabe46c13a74cc2323fe1e6c0a0c Mon Sep 17 00:00:00 2001 From: ethanoroshiba Date: Wed, 2 Oct 2024 14:51:56 -0500 Subject: [PATCH] Separate test utils and shared utils --- .../src/accounts/state_ext.rs | 2 +- .../src/app/benchmark_and_test_utils.rs | 269 ++++++++++++++ crates/astria-sequencer/src/app/benchmarks.rs | 14 +- crates/astria-sequencer/src/app/mod.rs | 4 +- crates/astria-sequencer/src/app/test_utils.rs | 345 +----------------- .../src/app/tests_app/mempool.rs | 9 +- .../astria-sequencer/src/app/tests_app/mod.rs | 20 +- .../src/app/tests_block_fees.rs | 22 +- .../src/app/tests_breaking_changes.rs | 30 +- .../src/app/tests_execute_transaction.rs | 29 +- .../src/authority/state_ext.rs | 2 +- .../src/benchmark_and_test_utils.rs | 46 +++ .../astria-sequencer/src/benchmark_utils.rs | 2 +- .../src/bridge/bridge_lock_action.rs | 2 +- .../src/bridge/bridge_sudo_change_action.rs | 2 +- .../src/bridge/bridge_unlock_action.rs | 4 +- crates/astria-sequencer/src/bridge/query.rs | 4 +- .../astria-sequencer/src/bridge/state_ext.rs | 2 +- crates/astria-sequencer/src/grpc/sequencer.rs | 6 +- crates/astria-sequencer/src/grpc/state_ext.rs | 2 +- .../src/ibc/ics20_transfer.rs | 14 +- .../src/ibc/ics20_withdrawal.rs | 2 +- crates/astria-sequencer/src/ibc/state_ext.rs | 4 +- crates/astria-sequencer/src/lib.rs | 4 +- .../src/mempool/benchmarks.rs | 2 +- .../src/mempool/mempool_state.rs | 2 +- crates/astria-sequencer/src/mempool/mod.rs | 28 +- .../src/mempool/transactions_container.rs | 38 +- .../src/proposal/commitment.rs | 20 +- .../astria-sequencer/src/service/consensus.rs | 9 +- .../astria-sequencer/src/service/info/mod.rs | 7 +- crates/astria-sequencer/src/test_utils.rs | 43 +-- .../src/transaction/checks.rs | 25 +- 33 files changed, 501 insertions(+), 513 deletions(-) create mode 100644 crates/astria-sequencer/src/app/benchmark_and_test_utils.rs create mode 100644 crates/astria-sequencer/src/benchmark_and_test_utils.rs diff --git a/crates/astria-sequencer/src/accounts/state_ext.rs b/crates/astria-sequencer/src/accounts/state_ext.rs index 1aca12616c..885fe2a010 100644 --- a/crates/astria-sequencer/src/accounts/state_ext.rs +++ b/crates/astria-sequencer/src/accounts/state_ext.rs @@ -346,7 +346,7 @@ mod tests { StateReadExt as _, StateWriteExt as _, }, - test_utils::{ + benchmark_and_test_utils::{ astria_address, nria, }, diff --git a/crates/astria-sequencer/src/app/benchmark_and_test_utils.rs b/crates/astria-sequencer/src/app/benchmark_and_test_utils.rs new file mode 100644 index 0000000000..b14da7d15f --- /dev/null +++ b/crates/astria-sequencer/src/app/benchmark_and_test_utils.rs @@ -0,0 +1,269 @@ +use std::collections::HashMap; + +use astria_core::{ + primitive::v1::asset::{ + Denom, + IbcPrefixed, + }, + protocol::{ + genesis::v1alpha1::{ + Account, + AddressPrefixes, + GenesisAppState, + }, + transaction::v1alpha1::action::ValidatorUpdate, + }, + Protobuf, +}; +use cnidarium::{ + Snapshot, + StateDelta, + Storage, +}; +use telemetry::Metrics as _; + +use crate::{ + accounts::StateWriteExt as _, + app::App, + assets::StateWriteExt as _, + benchmark_and_test_utils::{ + astria_address_from_hex_string, + nria, + }, + bridge::StateWriteExt as _, + ibc::StateWriteExt as _, + mempool::Mempool, + metrics::Metrics, + sequence::StateWriteExt as _, +}; + +pub(crate) const ALICE_ADDRESS: &str = "1c0c490f1b5528d8173c5de46d131160e4b2c0c3"; +pub(crate) const BOB_ADDRESS: &str = "2269aca7b7c03c7d07345f83db4fababd1a05570"; +pub(crate) const CAROL_ADDRESS: &str = "4e8846b82a8f31fd59265a9005959c4a030fc44c"; +pub(crate) const JUDY_ADDRESS: &str = "989a77160cb0e96e2d168083ab72ffe89b41c199"; +pub(crate) const TED_ADDRESS: &str = "4c4f91d8a918357ab5f6f19c1e179968fc39bb44"; + +pub(crate) fn address_prefixes() -> AddressPrefixes { + AddressPrefixes::try_from_raw( + astria_core::generated::protocol::genesis::v1alpha1::AddressPrefixes { + base: crate::benchmark_and_test_utils::ASTRIA_PREFIX.into(), + ibc_compat: crate::benchmark_and_test_utils::ASTRIA_COMPAT_PREFIX.into(), + }, + ) + .unwrap() +} + +pub(crate) fn default_fees() -> astria_core::protocol::genesis::v1alpha1::Fees { + astria_core::protocol::genesis::v1alpha1::Fees { + transfer_base_fee: 12, + sequence_base_fee: 32, + sequence_byte_cost_multiplier: 1, + init_bridge_account_base_fee: 48, + bridge_lock_byte_cost_multiplier: 1, + bridge_sudo_change_fee: 24, + ics20_withdrawal_base_fee: 24, + } +} + +pub(crate) fn proto_genesis_state() +-> astria_core::generated::protocol::genesis::v1alpha1::GenesisAppState { + use astria_core::generated::protocol::genesis::v1alpha1::{ + GenesisAppState, + IbcParameters, + }; + GenesisAppState { + address_prefixes: Some(address_prefixes().to_raw()), + accounts: default_genesis_accounts() + .into_iter() + .map(Protobuf::into_raw) + .collect(), + authority_sudo_address: Some(astria_address_from_hex_string(JUDY_ADDRESS).to_raw()), + chain_id: "test-1".to_string(), + ibc_sudo_address: Some(astria_address_from_hex_string(TED_ADDRESS).to_raw()), + ibc_relayer_addresses: vec![], + native_asset_base_denomination: nria().to_string(), + ibc_parameters: Some(IbcParameters { + ibc_enabled: true, + inbound_ics20_transfers_enabled: true, + outbound_ics20_transfers_enabled: true, + }), + allowed_fee_assets: vec![nria().to_string()], + fees: Some(default_fees().to_raw()), + } +} + +pub(crate) fn genesis_state() -> GenesisAppState { + proto_genesis_state().try_into().unwrap() +} + +pub(crate) async fn initialize_app_with_storage( + genesis_state: Option, + genesis_validators: Vec, +) -> (App, Storage) { + let storage = cnidarium::TempStorage::new() + .await + .expect("failed to create temp storage backing chain state"); + let snapshot = storage.latest_snapshot(); + let mempool = Mempool::new(); + let metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); + let mut app = App::new(snapshot, mempool, metrics).await.unwrap(); + + let genesis_state = genesis_state.unwrap_or_else(self::genesis_state); + + app.init_chain( + storage.clone(), + genesis_state, + genesis_validators, + "test".to_string(), + ) + .await + .unwrap(); + app.commit(storage.clone()).await; + + (app, storage.clone()) +} + +pub(crate) fn default_genesis_accounts() -> Vec { + vec![ + Account { + address: astria_address_from_hex_string(ALICE_ADDRESS), + balance: 10u128.pow(19), + }, + Account { + address: astria_address_from_hex_string(BOB_ADDRESS), + balance: 10u128.pow(19), + }, + Account { + address: astria_address_from_hex_string(CAROL_ADDRESS), + balance: 10u128.pow(19), + }, + ] +} + +pub(crate) fn mock_tx_cost( + denom_0_cost: u128, + denom_1_cost: u128, + denom_2_cost: u128, +) -> HashMap { + let mut costs: HashMap = HashMap::::new(); + costs.insert(denom_0().to_ibc_prefixed(), denom_0_cost); + costs.insert(denom_1().to_ibc_prefixed(), denom_1_cost); + costs.insert(denom_2().to_ibc_prefixed(), denom_2_cost); // not present in balances + + // we don't sanitize the cost inputs + costs.insert(denom_5().to_ibc_prefixed(), 0); // zero in balances also + costs.insert(denom_6().to_ibc_prefixed(), 0); // not present in balances + + costs +} + +pub(crate) const MOCK_SEQUENCE_FEE: u128 = 10; +pub(crate) fn denom_0() -> Denom { + "denom_0".parse().unwrap() +} + +pub(crate) fn denom_1() -> Denom { + "denom_1".parse().unwrap() +} + +pub(crate) fn denom_2() -> Denom { + "denom_2".parse().unwrap() +} + +pub(crate) fn denom_3() -> Denom { + "denom_3".parse().unwrap() +} + +pub(crate) fn denom_4() -> Denom { + "denom_4".parse().unwrap() +} + +pub(crate) fn denom_5() -> Denom { + "denom_5".parse().unwrap() +} + +pub(crate) fn denom_6() -> Denom { + "denom_6".parse().unwrap() +} + +pub(crate) fn mock_balances( + denom_0_balance: u128, + denom_1_balance: u128, +) -> HashMap { + let mut balances = HashMap::::new(); + if denom_0_balance != 0 { + balances.insert(denom_0().to_ibc_prefixed(), denom_0_balance); + } + if denom_1_balance != 0 { + balances.insert(denom_1().to_ibc_prefixed(), denom_1_balance); + } + // we don't sanitize the balance inputs + balances.insert(denom_3().to_ibc_prefixed(), 100); // balance transaction costs won't have entry for + balances.insert(denom_4().to_ibc_prefixed(), 0); // zero balance not in transaction + balances.insert(denom_5().to_ibc_prefixed(), 0); // zero balance with corresponding zero cost + + balances +} + +pub(crate) fn mock_state_put_account_balances( + state: &mut StateDelta, + address: &[u8; 20], + account_balances: HashMap, +) { + for (denom, balance) in account_balances { + state.put_account_balance(address, &denom, balance).unwrap(); + } +} + +pub(crate) fn mock_state_put_account_nonce( + state: &mut StateDelta, + address: &[u8; 20], + nonce: u32, +) { + state.put_account_nonce(address, nonce).unwrap(); +} + +pub(crate) async fn mock_state_getter() -> StateDelta { + let storage = cnidarium::TempStorage::new().await.unwrap(); + let snapshot = storage.latest_snapshot(); + let mut state: StateDelta = StateDelta::new(snapshot); + + // setup denoms + state + .put_ibc_asset(denom_0().unwrap_trace_prefixed()) + .unwrap(); + state + .put_ibc_asset(denom_1().unwrap_trace_prefixed()) + .unwrap(); + state + .put_ibc_asset(denom_2().unwrap_trace_prefixed()) + .unwrap(); + state + .put_ibc_asset(denom_3().unwrap_trace_prefixed()) + .unwrap(); + state + .put_ibc_asset(denom_4().unwrap_trace_prefixed()) + .unwrap(); + state + .put_ibc_asset(denom_5().unwrap_trace_prefixed()) + .unwrap(); + state + .put_ibc_asset(denom_6().unwrap_trace_prefixed()) + .unwrap(); + + // setup tx fees + state + .put_sequence_action_base_fee(MOCK_SEQUENCE_FEE) + .unwrap(); + state.put_sequence_action_byte_cost_multiplier(0).unwrap(); + state.put_transfer_base_fee(0).unwrap(); + state.put_ics20_withdrawal_base_fee(0).unwrap(); + state.put_init_bridge_account_base_fee(0).unwrap(); + state.put_bridge_lock_byte_cost_multiplier(0).unwrap(); + state.put_bridge_sudo_change_base_fee(0).unwrap(); + + // put denoms as allowed fee asset + state.put_allowed_fee_asset(&denom_0()).unwrap(); + + state +} diff --git a/crates/astria-sequencer/src/app/benchmarks.rs b/crates/astria-sequencer/src/app/benchmarks.rs index ec2adade14..ae433cc692 100644 --- a/crates/astria-sequencer/src/app/benchmarks.rs +++ b/crates/astria-sequencer/src/app/benchmarks.rs @@ -16,20 +16,19 @@ use cnidarium::Storage; use crate::{ app::{ - test_utils, - test_utils::{ + benchmark_and_test_utils::{ mock_balances, mock_tx_cost, }, App, }, + benchmark_and_test_utils::astria_address, benchmark_utils::{ self, TxTypes, SIGNER_COUNT, }, proposal::block_size_constraints::BlockSizeConstraints, - test_utils::astria_address, }; /// The max time for any benchmark. @@ -66,13 +65,16 @@ impl Fixture { accounts, authority_sudo_address: first_address.clone(), ibc_sudo_address: first_address.clone(), - ..crate::app::test_utils::proto_genesis_state() + ..crate::app::benchmark_and_test_utils::proto_genesis_state() }, ) .unwrap(); - let (app, storage) = - test_utils::initialize_app_with_storage(Some(genesis_state), vec![]).await; + let (app, storage) = crate::app::benchmark_and_test_utils::initialize_app_with_storage( + Some(genesis_state), + vec![], + ) + .await; let mock_balances = mock_balances(0, 0); let mock_tx_cost = mock_tx_cost(0, 0, 0); diff --git a/crates/astria-sequencer/src/app/mod.rs b/crates/astria-sequencer/src/app/mod.rs index 34a96202f2..7d6930e863 100644 --- a/crates/astria-sequencer/src/app/mod.rs +++ b/crates/astria-sequencer/src/app/mod.rs @@ -1,9 +1,11 @@ mod action_handler; +#[cfg(any(test, feature = "benchmark"))] +pub(crate) mod benchmark_and_test_utils; #[cfg(feature = "benchmark")] mod benchmarks; mod state_ext; pub(crate) mod storage; -#[cfg(any(test, feature = "benchmark"))] +#[cfg(test)] pub(crate) mod test_utils; #[cfg(test)] mod tests_app; diff --git a/crates/astria-sequencer/src/app/test_utils.rs b/crates/astria-sequencer/src/app/test_utils.rs index 38dc2816c9..e6866e9ece 100644 --- a/crates/astria-sequencer/src/app/test_utils.rs +++ b/crates/astria-sequencer/src/app/test_utils.rs @@ -1,23 +1,10 @@ -use std::{ - collections::HashMap, - sync::Arc, -}; +use std::sync::Arc; use astria_core::{ crypto::SigningKey, - primitive::v1::{ - asset::{ - Denom, - IbcPrefixed, - }, - RollupId, - }, + primitive::v1::RollupId, protocol::{ - genesis::v1alpha1::{ - Account, - AddressPrefixes, - GenesisAppState, - }, + genesis::v1alpha1::GenesisAppState, transaction::v1alpha1::{ action::{ SequenceAction, @@ -27,40 +14,17 @@ use astria_core::{ UnsignedTransaction, }, }, - Protobuf, }; use bytes::Bytes; -use cnidarium::{ - Snapshot, - StateDelta, - Storage, -}; -use telemetry::Metrics as _; -use crate::{ - accounts::StateWriteExt, - app::App, - assets::StateWriteExt as AssetStateWriteExt, - bridge::StateWriteExt as BridgeStateWriteExt, - ibc::StateWriteExt as IbcStateWriteExt, - mempool::Mempool, - metrics::Metrics, - sequence::StateWriteExt as SequenceStateWriteExt, - test_utils::astria_address_from_hex_string, +use crate::app::{ + benchmark_and_test_utils::{ + denom_0, + initialize_app_with_storage, + }, + App, }; -pub(crate) const ALICE_ADDRESS: &str = "1c0c490f1b5528d8173c5de46d131160e4b2c0c3"; -pub(crate) const BOB_ADDRESS: &str = "2269aca7b7c03c7d07345f83db4fababd1a05570"; -pub(crate) const CAROL_ADDRESS: &str = "4e8846b82a8f31fd59265a9005959c4a030fc44c"; -pub(crate) const JUDY_ADDRESS: &str = "989a77160cb0e96e2d168083ab72ffe89b41c199"; -pub(crate) const TED_ADDRESS: &str = "4c4f91d8a918357ab5f6f19c1e179968fc39bb44"; - -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) fn get_alice_signing_key() -> SigningKey { // this secret key corresponds to ALICE_ADDRESS let alice_secret_bytes: [u8; 32] = @@ -71,12 +35,6 @@ pub(crate) fn get_alice_signing_key() -> SigningKey { SigningKey::from(alice_secret_bytes) } -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) fn get_bob_signing_key() -> SigningKey { // this secret key corresponds to ALICE_ADDRESS let bob_secret_bytes: [u8; 32] = @@ -87,12 +45,6 @@ pub(crate) fn get_bob_signing_key() -> SigningKey { SigningKey::from(bob_secret_bytes) } -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) fn get_carol_signing_key() -> SigningKey { // this secret key corresponds to ALICE_ADDRESS let carol_secret_bytes: [u8; 32] = @@ -103,12 +55,6 @@ pub(crate) fn get_carol_signing_key() -> SigningKey { SigningKey::from(carol_secret_bytes) } -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) fn get_judy_signing_key() -> SigningKey { // this secret key corresponds to ALICE_ADDRESS let judy_secret_bytes: [u8; 32] = @@ -119,12 +65,6 @@ pub(crate) fn get_judy_signing_key() -> SigningKey { SigningKey::from(judy_secret_bytes) } -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) fn get_bridge_signing_key() -> SigningKey { let bridge_secret_bytes: [u8; 32] = hex::decode("db4982e01f3eba9e74ac35422fcd49aa2b47c3c535345c7e7da5220fe3a0ce79") @@ -134,115 +74,6 @@ pub(crate) fn get_bridge_signing_key() -> SigningKey { SigningKey::from(bridge_secret_bytes) } -pub(crate) fn default_genesis_accounts() -> Vec { - vec![ - Account { - address: astria_address_from_hex_string(ALICE_ADDRESS), - balance: 10u128.pow(19), - }, - Account { - address: astria_address_from_hex_string(BOB_ADDRESS), - balance: 10u128.pow(19), - }, - Account { - address: astria_address_from_hex_string(CAROL_ADDRESS), - balance: 10u128.pow(19), - }, - ] -} - -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] -pub(crate) fn default_fees() -> astria_core::protocol::genesis::v1alpha1::Fees { - astria_core::protocol::genesis::v1alpha1::Fees { - transfer_base_fee: 12, - sequence_base_fee: 32, - sequence_byte_cost_multiplier: 1, - init_bridge_account_base_fee: 48, - bridge_lock_byte_cost_multiplier: 1, - bridge_sudo_change_fee: 24, - ics20_withdrawal_base_fee: 24, - } -} - -pub(crate) fn address_prefixes() -> AddressPrefixes { - AddressPrefixes::try_from_raw( - astria_core::generated::protocol::genesis::v1alpha1::AddressPrefixes { - base: crate::test_utils::ASTRIA_PREFIX.into(), - ibc_compat: crate::test_utils::ASTRIA_COMPAT_PREFIX.into(), - }, - ) - .unwrap() -} - -pub(crate) fn proto_genesis_state() --> astria_core::generated::protocol::genesis::v1alpha1::GenesisAppState { - use astria_core::generated::protocol::genesis::v1alpha1::{ - GenesisAppState, - IbcParameters, - }; - GenesisAppState { - address_prefixes: Some(address_prefixes().to_raw()), - accounts: default_genesis_accounts() - .into_iter() - .map(Protobuf::into_raw) - .collect(), - authority_sudo_address: Some(astria_address_from_hex_string(JUDY_ADDRESS).to_raw()), - chain_id: "test-1".to_string(), - ibc_sudo_address: Some(astria_address_from_hex_string(TED_ADDRESS).to_raw()), - ibc_relayer_addresses: vec![], - native_asset_base_denomination: crate::test_utils::nria().to_string(), - ibc_parameters: Some(IbcParameters { - ibc_enabled: true, - inbound_ics20_transfers_enabled: true, - outbound_ics20_transfers_enabled: true, - }), - allowed_fee_assets: vec![crate::test_utils::nria().to_string()], - fees: Some(default_fees().to_raw()), - } -} - -pub(crate) fn genesis_state() -> GenesisAppState { - proto_genesis_state().try_into().unwrap() -} - -pub(crate) async fn initialize_app_with_storage( - genesis_state: Option, - genesis_validators: Vec, -) -> (App, Storage) { - let storage = cnidarium::TempStorage::new() - .await - .expect("failed to create temp storage backing chain state"); - let snapshot = storage.latest_snapshot(); - let mempool = Mempool::new(); - let metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); - let mut app = App::new(snapshot, mempool, metrics).await.unwrap(); - - let genesis_state = genesis_state.unwrap_or_else(self::genesis_state); - - app.init_chain( - storage.clone(), - genesis_state, - genesis_validators, - "test".to_string(), - ) - .await - .unwrap(); - app.commit(storage.clone()).await; - - (app, storage.clone()) -} - -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) async fn initialize_app( genesis_state: Option, genesis_validators: Vec, @@ -251,24 +82,12 @@ pub(crate) async fn initialize_app( app } -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] pub(crate) struct MockTxBuilder { nonce: u32, signer: SigningKey, chain_id: String, } -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] impl MockTxBuilder { pub(crate) fn new() -> Self { Self { @@ -317,149 +136,3 @@ impl MockTxBuilder { Arc::new(tx.into_signed(&self.signer)) } } - -pub(crate) const MOCK_SEQUENCE_FEE: u128 = 10; -pub(crate) fn denom_0() -> Denom { - "denom_0".parse().unwrap() -} - -pub(crate) fn denom_1() -> Denom { - "denom_1".parse().unwrap() -} - -fn denom_2() -> Denom { - "denom_2".parse().unwrap() -} - -pub(crate) fn denom_3() -> Denom { - "denom_3".parse().unwrap() -} - -fn denom_4() -> Denom { - "denom_4".parse().unwrap() -} - -fn denom_5() -> Denom { - "denom_5".parse().unwrap() -} - -fn denom_6() -> Denom { - "denom_6".parse().unwrap() -} - -pub(crate) fn mock_balances( - denom_0_balance: u128, - denom_1_balance: u128, -) -> HashMap { - let mut balances = HashMap::::new(); - if denom_0_balance != 0 { - balances.insert(denom_0().to_ibc_prefixed(), denom_0_balance); - } - if denom_1_balance != 0 { - balances.insert(denom_1().to_ibc_prefixed(), denom_1_balance); - } - // we don't sanitize the balance inputs - balances.insert(denom_3().to_ibc_prefixed(), 100); // balance transaction costs won't have entry for - balances.insert(denom_4().to_ibc_prefixed(), 0); // zero balance not in transaction - balances.insert(denom_5().to_ibc_prefixed(), 0); // zero balance with corresponding zero cost - - balances -} - -pub(crate) fn mock_tx_cost( - denom_0_cost: u128, - denom_1_cost: u128, - denom_2_cost: u128, -) -> HashMap { - let mut costs: HashMap = HashMap::::new(); - costs.insert(denom_0().to_ibc_prefixed(), denom_0_cost); - costs.insert(denom_1().to_ibc_prefixed(), denom_1_cost); - costs.insert(denom_2().to_ibc_prefixed(), denom_2_cost); // not present in balances - - // we don't sanitize the cost inputs - costs.insert(denom_5().to_ibc_prefixed(), 0); // zero in balances also - costs.insert(denom_6().to_ibc_prefixed(), 0); // not present in balances - - costs -} - -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] -pub(crate) fn mock_state_put_account_balances( - state: &mut StateDelta, - address: &[u8; 20], - account_balances: HashMap, -) { - for (denom, balance) in account_balances { - state.put_account_balance(address, &denom, balance).unwrap(); - } -} - -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] -pub(crate) fn mock_state_put_account_nonce( - state: &mut StateDelta, - address: &[u8; 20], - nonce: u32, -) { - state.put_account_nonce(address, nonce).unwrap(); -} - -#[expect( - clippy::allow_attributes, - clippy::allow_attributes_without_reason, - reason = "allow is only necessary when benchmark isn't enabled" -)] -#[cfg_attr(feature = "benchmark", allow(dead_code))] -pub(crate) async fn mock_state_getter() -> StateDelta { - let storage = cnidarium::TempStorage::new().await.unwrap(); - let snapshot = storage.latest_snapshot(); - let mut state: StateDelta = StateDelta::new(snapshot); - - // setup denoms - state - .put_ibc_asset(denom_0().unwrap_trace_prefixed()) - .unwrap(); - state - .put_ibc_asset(denom_1().unwrap_trace_prefixed()) - .unwrap(); - state - .put_ibc_asset(denom_2().unwrap_trace_prefixed()) - .unwrap(); - state - .put_ibc_asset(denom_3().unwrap_trace_prefixed()) - .unwrap(); - state - .put_ibc_asset(denom_4().unwrap_trace_prefixed()) - .unwrap(); - state - .put_ibc_asset(denom_5().unwrap_trace_prefixed()) - .unwrap(); - state - .put_ibc_asset(denom_6().unwrap_trace_prefixed()) - .unwrap(); - - // setup tx fees - state - .put_sequence_action_base_fee(MOCK_SEQUENCE_FEE) - .unwrap(); - state.put_sequence_action_byte_cost_multiplier(0).unwrap(); - state.put_transfer_base_fee(0).unwrap(); - state.put_ics20_withdrawal_base_fee(0).unwrap(); - state.put_init_bridge_account_base_fee(0).unwrap(); - state.put_bridge_lock_byte_cost_multiplier(0).unwrap(); - state.put_bridge_sudo_change_base_fee(0).unwrap(); - - // put denoms as allowed fee asset - state.put_allowed_fee_asset(&denom_0()).unwrap(); - - state -} diff --git a/crates/astria-sequencer/src/app/tests_app/mempool.rs b/crates/astria-sequencer/src/app/tests_app/mempool.rs index a147e643e0..fdebc29e95 100644 --- a/crates/astria-sequencer/src/app/tests_app/mempool.rs +++ b/crates/astria-sequencer/src/app/tests_app/mempool.rs @@ -14,6 +14,11 @@ use astria_core::{ }, Protobuf, }; +use benchmark_and_test_utils::{ + proto_genesis_state, + ALICE_ADDRESS, + CAROL_ADDRESS, +}; use prost::Message as _; use tendermint::{ abci::{ @@ -33,11 +38,11 @@ use super::*; use crate::{ accounts::StateReadExt as _, app::test_utils::*, - proposal::commitment::generate_rollup_datas_commitment, - test_utils::{ + benchmark_and_test_utils::{ astria_address_from_hex_string, nria, }, + proposal::commitment::generate_rollup_datas_commitment, }; #[tokio::test] diff --git a/crates/astria-sequencer/src/app/tests_app/mod.rs b/crates/astria-sequencer/src/app/tests_app/mod.rs index 6e5ddea619..1a44c6c39a 100644 --- a/crates/astria-sequencer/src/app/tests_app/mod.rs +++ b/crates/astria-sequencer/src/app/tests_app/mod.rs @@ -21,6 +21,14 @@ use astria_core::{ }, sequencerblock::v1alpha1::block::Deposit, }; +use benchmark_and_test_utils::{ + default_genesis_accounts, + initialize_app_with_storage, + mock_balances, + mock_tx_cost, + BOB_ADDRESS, + JUDY_ADDRESS, +}; use cnidarium::StateDelta; use prost::{ bytes::Bytes, @@ -54,14 +62,14 @@ use crate::{ StateWriteExt as _, ValidatorSet, }, - bridge::StateWriteExt as _, - proposal::commitment::generate_rollup_datas_commitment, - test_utils::{ + benchmark_and_test_utils::{ astria_address, astria_address_from_hex_string, nria, verification_key, }, + bridge::StateWriteExt as _, + proposal::commitment::generate_rollup_datas_commitment, }; fn default_tendermint_header() -> Header { @@ -153,7 +161,7 @@ async fn app_begin_block_remove_byzantine_validators() { let misbehavior = types::Misbehavior { kind: types::MisbehaviorKind::Unknown, validator: types::Validator { - address: *crate::test_utils::verification_key(1).address_bytes(), + address: *verification_key(1).address_bytes(), power: 0u32.into(), }, height: Height::default(), @@ -741,11 +749,11 @@ async fn app_end_block_validator_updates() { let initial_validator_set = vec![ ValidatorUpdate { power: 100, - verification_key: crate::test_utils::verification_key(1), + verification_key: verification_key(1), }, ValidatorUpdate { power: 1, - verification_key: crate::test_utils::verification_key(2), + verification_key: verification_key(2), }, ]; diff --git a/crates/astria-sequencer/src/app/tests_block_fees.rs b/crates/astria-sequencer/src/app/tests_block_fees.rs index 6937f481ba..7f168a2a87 100644 --- a/crates/astria-sequencer/src/app/tests_block_fees.rs +++ b/crates/astria-sequencer/src/app/tests_block_fees.rs @@ -22,13 +22,20 @@ use crate::{ StateReadExt as _, StateWriteExt as _, }, - app::test_utils::{ - get_alice_signing_key, - get_bridge_signing_key, - initialize_app, - BOB_ADDRESS, + app::{ + benchmark_and_test_utils::BOB_ADDRESS, + test_utils::{ + get_alice_signing_key, + get_bridge_signing_key, + initialize_app, + }, }, assets::StateReadExt as _, + benchmark_and_test_utils::{ + astria_address, + astria_address_from_hex_string, + nria, + }, bridge::{ calculate_base_deposit_fee, StateWriteExt as _, @@ -37,11 +44,6 @@ use crate::{ calculate_fee_from_state, StateWriteExt as _, }, - test_utils::{ - astria_address, - astria_address_from_hex_string, - nria, - }, }; #[tokio::test] diff --git a/crates/astria-sequencer/src/app/tests_breaking_changes.rs b/crates/astria-sequencer/src/app/tests_breaking_changes.rs index d992372f84..72967e632b 100644 --- a/crates/astria-sequencer/src/app/tests_breaking_changes.rs +++ b/crates/astria-sequencer/src/app/tests_breaking_changes.rs @@ -50,25 +50,29 @@ use tendermint::{ }; use crate::{ - app::test_utils::{ - default_genesis_accounts, - get_alice_signing_key, - get_bridge_signing_key, - initialize_app, - initialize_app_with_storage, - proto_genesis_state, - BOB_ADDRESS, - CAROL_ADDRESS, + app::{ + benchmark_and_test_utils::{ + default_genesis_accounts, + initialize_app_with_storage, + proto_genesis_state, + BOB_ADDRESS, + CAROL_ADDRESS, + }, + test_utils::{ + get_alice_signing_key, + get_bridge_signing_key, + initialize_app, + }, }, authority::StateReadExt as _, - bridge::StateWriteExt as _, - proposal::commitment::generate_rollup_datas_commitment, - test_utils::{ + benchmark_and_test_utils::{ astria_address, astria_address_from_hex_string, nria, ASTRIA_PREFIX, }, + bridge::StateWriteExt as _, + proposal::commitment::generate_rollup_datas_commitment, }; #[tokio::test] @@ -197,7 +201,7 @@ async fn app_execute_transaction_with_every_action_snapshot() { // setup for ValidatorUpdate action let update = ValidatorUpdate { power: 100, - verification_key: crate::test_utils::verification_key(1), + verification_key: crate::benchmark_and_test_utils::verification_key(1), }; let rollup_id = RollupId::from_unhashed_bytes(b"testchainid"); diff --git a/crates/astria-sequencer/src/app/tests_execute_transaction.rs b/crates/astria-sequencer/src/app/tests_execute_transaction.rs index 7baf724a11..5509ab7403 100644 --- a/crates/astria-sequencer/src/app/tests_execute_transaction.rs +++ b/crates/astria-sequencer/src/app/tests_execute_transaction.rs @@ -36,11 +36,13 @@ use super::test_utils::get_alice_signing_key; use crate::{ accounts::StateReadExt as _, app::{ + benchmark_and_test_utils::{ + BOB_ADDRESS, + CAROL_ADDRESS, + }, test_utils::{ get_bridge_signing_key, initialize_app, - BOB_ADDRESS, - CAROL_ADDRESS, }, ActionHandler as _, }, @@ -49,18 +51,19 @@ use crate::{ StateWriteExt as _, }, authority::StateReadExt as _, + benchmark_and_test_utils::{ + astria_address, + astria_address_from_hex_string, + nria, + verification_key, + ASTRIA_PREFIX, + }, bridge::{ StateReadExt as _, StateWriteExt as _, }, ibc::StateReadExt as _, sequence::calculate_fee_from_state, - test_utils::{ - astria_address, - astria_address_from_hex_string, - nria, - ASTRIA_PREFIX, - }, transaction::{ InvalidChainId, InvalidNonce, @@ -82,7 +85,7 @@ fn proto_genesis_state() -> astria_core::generated::protocol::genesis::v1alpha1: .unwrap() .to_raw(), ), - ..crate::app::test_utils::proto_genesis_state() + ..crate::app::benchmark_and_test_utils::proto_genesis_state() } } @@ -108,8 +111,8 @@ async fn app_execute_transaction_transfer() { TransferAction { to: bob_address, amount: value, - asset: crate::test_utils::nria().into(), - fee_asset: crate::test_utils::nria().into(), + asset: nria().into(), + fee_asset: nria().into(), } .into(), ]) @@ -330,7 +333,7 @@ async fn app_execute_transaction_validator_update() { let update = ValidatorUpdate { power: 100, - verification_key: crate::test_utils::verification_key(1), + verification_key: verification_key(1), }; let tx = UnsignedTransaction::builder() @@ -349,7 +352,7 @@ async fn app_execute_transaction_validator_update() { let validator_updates = app.state.get_validator_updates().await.unwrap(); assert_eq!(validator_updates.len(), 1); assert_eq!( - validator_updates.get(crate::test_utils::verification_key(1).address_bytes()), + validator_updates.get(verification_key(1).address_bytes()), Some(&update) ); } diff --git a/crates/astria-sequencer/src/authority/state_ext.rs b/crates/astria-sequencer/src/authority/state_ext.rs index 38df2f7c5b..8aae00a1ba 100644 --- a/crates/astria-sequencer/src/authority/state_ext.rs +++ b/crates/astria-sequencer/src/authority/state_ext.rs @@ -134,7 +134,7 @@ mod tests { use crate::{ address::StateWriteExt as _, authority::ValidatorSet, - test_utils::{ + benchmark_and_test_utils::{ verification_key, ASTRIA_PREFIX, }, diff --git a/crates/astria-sequencer/src/benchmark_and_test_utils.rs b/crates/astria-sequencer/src/benchmark_and_test_utils.rs new file mode 100644 index 0000000000..3b94f6dbbb --- /dev/null +++ b/crates/astria-sequencer/src/benchmark_and_test_utils.rs @@ -0,0 +1,46 @@ +use astria_core::primitive::v1::{ + asset::TracePrefixed, + Address, +}; + +pub(crate) const ASTRIA_PREFIX: &str = "astria"; +pub(crate) const ASTRIA_COMPAT_PREFIX: &str = "astriacompat"; + +pub(crate) fn astria_address_from_hex_string(s: &str) -> Address { + let bytes = hex::decode(s).unwrap(); + Address::builder() + .prefix(ASTRIA_PREFIX) + .slice(bytes) + .try_build() + .unwrap() +} + +pub(crate) fn nria() -> TracePrefixed { + "nria".parse().unwrap() +} + +#[cfg(test)] +pub(crate) fn verification_key(seed: u64) -> astria_core::crypto::VerificationKey { + use rand::SeedableRng as _; + let rng = rand_chacha::ChaChaRng::seed_from_u64(seed); + let signing_key = astria_core::crypto::SigningKey::new(rng); + signing_key.verification_key() +} + +#[cfg(test)] +#[track_caller] +pub(crate) fn assert_eyre_error(error: &astria_eyre::eyre::Error, expected: &'static str) { + let msg = error.to_string(); + assert!( + msg.contains(expected), + "error contained different message\n\texpected: {expected}\n\tfull_error: {msg}", + ); +} + +pub(crate) fn astria_address(bytes: &[u8]) -> Address { + Address::builder() + .prefix(ASTRIA_PREFIX) + .slice(bytes) + .try_build() + .unwrap() +} diff --git a/crates/astria-sequencer/src/benchmark_utils.rs b/crates/astria-sequencer/src/benchmark_utils.rs index 9cc901065f..0ed3ff7975 100644 --- a/crates/astria-sequencer/src/benchmark_utils.rs +++ b/crates/astria-sequencer/src/benchmark_utils.rs @@ -26,7 +26,7 @@ use astria_core::{ }, }; -use crate::test_utils::{ +use crate::benchmark_and_test_utils::{ astria_address, nria, }; diff --git a/crates/astria-sequencer/src/bridge/bridge_lock_action.rs b/crates/astria-sequencer/src/bridge/bridge_lock_action.rs index 20f8764380..3df1e9521e 100644 --- a/crates/astria-sequencer/src/bridge/bridge_lock_action.rs +++ b/crates/astria-sequencer/src/bridge/bridge_lock_action.rs @@ -179,7 +179,7 @@ mod tests { use super::*; use crate::{ address::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ assert_eyre_error, astria_address, ASTRIA_PREFIX, diff --git a/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs b/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs index 4d626ce690..d3289a75a2 100644 --- a/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs +++ b/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs @@ -116,7 +116,7 @@ mod tests { use super::*; use crate::{ address::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ astria_address, ASTRIA_PREFIX, }, diff --git a/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs b/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs index 787000f9a3..f4c7e715ef 100644 --- a/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs +++ b/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs @@ -117,12 +117,12 @@ mod tests { address::StateWriteExt as _, app::ActionHandler as _, assets::StateWriteExt as _, - bridge::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ assert_eyre_error, astria_address, ASTRIA_PREFIX, }, + bridge::StateWriteExt as _, transaction::{ StateWriteExt as _, TransactionContext, diff --git a/crates/astria-sequencer/src/bridge/query.rs b/crates/astria-sequencer/src/bridge/query.rs index 7a2b1d108c..96d14b8cf8 100644 --- a/crates/astria-sequencer/src/bridge/query.rs +++ b/crates/astria-sequencer/src/bridge/query.rs @@ -304,11 +304,11 @@ mod tests { address::StateWriteExt as _, app::StateWriteExt as _, assets::StateWriteExt as _, - bridge::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ astria_address, ASTRIA_PREFIX, }, + bridge::StateWriteExt as _, }; #[tokio::test] diff --git a/crates/astria-sequencer/src/bridge/state_ext.rs b/crates/astria-sequencer/src/bridge/state_ext.rs index 867ac9ecdd..17c6a3b26d 100644 --- a/crates/astria-sequencer/src/bridge/state_ext.rs +++ b/crates/astria-sequencer/src/bridge/state_ext.rs @@ -511,7 +511,7 @@ mod tests { use insta::assert_snapshot; use super::*; - use crate::test_utils::astria_address; + use crate::benchmark_and_test_utils::astria_address; fn asset_0() -> asset::Denom { "asset_0".parse().unwrap() diff --git a/crates/astria-sequencer/src/grpc/sequencer.rs b/crates/astria-sequencer/src/grpc/sequencer.rs index 1a506f4578..60216f6b15 100644 --- a/crates/astria-sequencer/src/grpc/sequencer.rs +++ b/crates/astria-sequencer/src/grpc/sequencer.rs @@ -233,15 +233,15 @@ mod tests { use super::*; use crate::{ app::{ - test_utils::{ - get_alice_signing_key, + benchmark_and_test_utils::{ mock_balances, mock_tx_cost, }, + test_utils::get_alice_signing_key, StateWriteExt as _, }, + benchmark_and_test_utils::astria_address, grpc::StateWriteExt as _, - test_utils::astria_address, }; fn make_test_sequencer_block(height: u32) -> SequencerBlock { diff --git a/crates/astria-sequencer/src/grpc/state_ext.rs b/crates/astria-sequencer/src/grpc/state_ext.rs index fffc906d20..9f9898bb55 100644 --- a/crates/astria-sequencer/src/grpc/state_ext.rs +++ b/crates/astria-sequencer/src/grpc/state_ext.rs @@ -349,7 +349,7 @@ mod tests { use rand::Rng; use super::*; - use crate::test_utils::astria_address; + use crate::benchmark_and_test_utils::astria_address; // creates new sequencer block, optionally shifting all values except the height by 1 fn make_test_sequencer_block(height: u32) -> SequencerBlock { diff --git a/crates/astria-sequencer/src/ibc/ics20_transfer.rs b/crates/astria-sequencer/src/ibc/ics20_transfer.rs index b7191d8238..c298e696c9 100644 --- a/crates/astria-sequencer/src/ibc/ics20_transfer.rs +++ b/crates/astria-sequencer/src/ibc/ics20_transfer.rs @@ -772,6 +772,12 @@ mod tests { accounts::StateReadExt as _, address::StateWriteExt as _, assets::StateReadExt as _, + benchmark_and_test_utils::{ + astria_address, + nria, + ASTRIA_COMPAT_PREFIX, + ASTRIA_PREFIX, + }, bridge::{ StateReadExt as _, StateWriteExt as _, @@ -780,13 +786,7 @@ mod tests { StateReadExt as _, StateWriteExt, }, - test_utils::{ - astria_address, - astria_compat_address, - nria, - ASTRIA_COMPAT_PREFIX, - ASTRIA_PREFIX, - }, + test_utils::astria_compat_address, transaction::{ StateWriteExt as _, TransactionContext, diff --git a/crates/astria-sequencer/src/ibc/ics20_withdrawal.rs b/crates/astria-sequencer/src/ibc/ics20_withdrawal.rs index d861ee52e3..fcd5f2b927 100644 --- a/crates/astria-sequencer/src/ibc/ics20_withdrawal.rs +++ b/crates/astria-sequencer/src/ibc/ics20_withdrawal.rs @@ -291,7 +291,7 @@ mod tests { use super::*; use crate::{ address::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ assert_eyre_error, astria_address, ASTRIA_PREFIX, diff --git a/crates/astria-sequencer/src/ibc/state_ext.rs b/crates/astria-sequencer/src/ibc/state_ext.rs index 4ec443a9c6..3726b15963 100644 --- a/crates/astria-sequencer/src/ibc/state_ext.rs +++ b/crates/astria-sequencer/src/ibc/state_ext.rs @@ -225,11 +225,11 @@ mod tests { }; use crate::{ address::StateWriteExt, - ibc::state_ext::channel_balance_storage_key, - test_utils::{ + benchmark_and_test_utils::{ astria_address, ASTRIA_PREFIX, }, + ibc::state_ext::channel_balance_storage_key, }; fn asset_0() -> asset::Denom { diff --git a/crates/astria-sequencer/src/lib.rs b/crates/astria-sequencer/src/lib.rs index 8322623b57..2d269ece1b 100644 --- a/crates/astria-sequencer/src/lib.rs +++ b/crates/astria-sequencer/src/lib.rs @@ -3,6 +3,8 @@ pub(crate) mod address; pub(crate) mod app; pub(crate) mod assets; pub(crate) mod authority; +#[cfg(any(test, feature = "benchmark"))] +pub(crate) mod benchmark_and_test_utils; #[cfg(feature = "benchmark")] pub(crate) mod benchmark_utils; pub(crate) mod bridge; @@ -20,7 +22,7 @@ mod sequencer; pub(crate) mod service; pub(crate) mod storage; pub(crate) mod storage_keys; -#[cfg(any(test, feature = "benchmark"))] +#[cfg(test)] pub(crate) mod test_utils; pub(crate) mod transaction; mod utils; diff --git a/crates/astria-sequencer/src/mempool/benchmarks.rs b/crates/astria-sequencer/src/mempool/benchmarks.rs index 5b646231ea..2358981f57 100644 --- a/crates/astria-sequencer/src/mempool/benchmarks.rs +++ b/crates/astria-sequencer/src/mempool/benchmarks.rs @@ -19,7 +19,7 @@ use sha2::{ }; use crate::{ - app::test_utils::{ + app::benchmark_and_test_utils::{ mock_balances, mock_state_getter, mock_state_put_account_balances, diff --git a/crates/astria-sequencer/src/mempool/mempool_state.rs b/crates/astria-sequencer/src/mempool/mempool_state.rs index 53f79eee5d..53116f6457 100644 --- a/crates/astria-sequencer/src/mempool/mempool_state.rs +++ b/crates/astria-sequencer/src/mempool/mempool_state.rs @@ -41,7 +41,7 @@ mod tests { StateReadExt as _, StateWriteExt as _, }, - test_utils::{ + benchmark_and_test_utils::{ astria_address, nria, }, diff --git a/crates/astria-sequencer/src/mempool/mod.rs b/crates/astria-sequencer/src/mempool/mod.rs index fde6479b25..a51d4c7cee 100644 --- a/crates/astria-sequencer/src/mempool/mod.rs +++ b/crates/astria-sequencer/src/mempool/mod.rs @@ -412,19 +412,23 @@ impl Mempool { mod tests { use super::*; use crate::{ - app::test_utils::{ - get_bob_signing_key, - mock_balances, - mock_state_getter, - mock_state_put_account_balances, - mock_state_put_account_nonce, - mock_tx_cost, - MockTxBuilder, - ALICE_ADDRESS, - BOB_ADDRESS, - CAROL_ADDRESS, + app::{ + benchmark_and_test_utils::{ + mock_balances, + mock_state_getter, + mock_state_put_account_balances, + mock_state_put_account_nonce, + mock_tx_cost, + ALICE_ADDRESS, + BOB_ADDRESS, + CAROL_ADDRESS, + }, + test_utils::{ + get_bob_signing_key, + MockTxBuilder, + }, }, - test_utils::astria_address_from_hex_string, + benchmark_and_test_utils::astria_address_from_hex_string, }; #[tokio::test] diff --git a/crates/astria-sequencer/src/mempool/transactions_container.rs b/crates/astria-sequencer/src/mempool/transactions_container.rs index 68ceeb9631..c70524df42 100644 --- a/crates/astria-sequencer/src/mempool/transactions_container.rs +++ b/crates/astria-sequencer/src/mempool/transactions_container.rs @@ -783,24 +783,28 @@ mod tests { use super::*; use crate::{ - app::test_utils::{ - denom_0, - denom_1, - denom_3, - get_alice_signing_key, - get_bob_signing_key, - get_carol_signing_key, - mock_balances, - mock_state_getter, - mock_state_put_account_nonce, - mock_tx_cost, - MockTxBuilder, - ALICE_ADDRESS, - BOB_ADDRESS, - CAROL_ADDRESS, - MOCK_SEQUENCE_FEE, + app::{ + benchmark_and_test_utils::{ + denom_0, + denom_1, + denom_3, + mock_balances, + mock_state_getter, + mock_state_put_account_nonce, + mock_tx_cost, + ALICE_ADDRESS, + BOB_ADDRESS, + CAROL_ADDRESS, + MOCK_SEQUENCE_FEE, + }, + test_utils::{ + get_alice_signing_key, + get_bob_signing_key, + get_carol_signing_key, + MockTxBuilder, + }, }, - test_utils::astria_address_from_hex_string, + benchmark_and_test_utils::astria_address_from_hex_string, }; const MAX_PARKED_TXS_PER_ACCOUNT: usize = 15; diff --git a/crates/astria-sequencer/src/proposal/commitment.rs b/crates/astria-sequencer/src/proposal/commitment.rs index 627cb1998f..8d424bb0a4 100644 --- a/crates/astria-sequencer/src/proposal/commitment.rs +++ b/crates/astria-sequencer/src/proposal/commitment.rs @@ -104,19 +104,23 @@ mod tests { use rand::rngs::OsRng; use super::*; + use crate::benchmark_and_test_utils::{ + astria_address, + nria, + }; #[test] fn generate_rollup_datas_commitment_should_ignore_transfers() { let sequence_action = SequenceAction { rollup_id: RollupId::from_unhashed_bytes(b"testchainid"), data: Bytes::from_static(b"hello world"), - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), }; let transfer_action = TransferAction { - to: crate::test_utils::astria_address(&[0u8; 20]), + to: astria_address(&[0u8; 20]), amount: 1, - asset: crate::test_utils::nria().into(), - fee_asset: crate::test_utils::nria().into(), + asset: nria().into(), + fee_asset: nria().into(), }; let signing_key = SigningKey::new(OsRng); @@ -161,13 +165,13 @@ mod tests { let sequence_action = SequenceAction { rollup_id: RollupId::from_unhashed_bytes(b"testchainid"), data: b"helloworld".to_vec().into(), - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), }; let transfer_action = TransferAction { - to: crate::test_utils::astria_address(&[0u8; 20]), + to: astria_address(&[0u8; 20]), amount: 1, - asset: crate::test_utils::nria().into(), - fee_asset: crate::test_utils::nria().into(), + asset: nria().into(), + fee_asset: nria().into(), }; let signing_key = SigningKey::new(OsRng); diff --git a/crates/astria-sequencer/src/service/consensus.rs b/crates/astria-sequencer/src/service/consensus.rs index 95597fbd7c..d8ad8a060c 100644 --- a/crates/astria-sequencer/src/service/consensus.rs +++ b/crates/astria-sequencer/src/service/consensus.rs @@ -226,7 +226,7 @@ mod tests { use super::*; use crate::{ - app::test_utils::{ + app::benchmark_and_test_utils::{ mock_balances, mock_tx_cost, }, @@ -241,7 +241,7 @@ mod tests { SequenceAction { rollup_id: RollupId::from_unhashed_bytes(b"testchainid"), data: Bytes::from_static(b"hello world"), - fee_asset: crate::test_utils::nria().into(), + fee_asset: crate::benchmark_and_test_utils::nria().into(), } .into(), ]) @@ -452,7 +452,8 @@ mod tests { vec![ astria_core::generated::protocol::genesis::v1alpha1::Account { address: Some( - crate::test_utils::astria_address(funded_key.address_bytes()).to_raw(), + crate::benchmark_and_test_utils::astria_address(funded_key.address_bytes()) + .to_raw(), ), balance: Some(10u128.pow(19).into()), }, @@ -461,7 +462,7 @@ mod tests { vec![] }; let genesis_state = { - let mut state = crate::app::test_utils::proto_genesis_state(); + let mut state = crate::app::benchmark_and_test_utils::proto_genesis_state(); state.accounts = accounts; state } diff --git a/crates/astria-sequencer/src/service/info/mod.rs b/crates/astria-sequencer/src/service/info/mod.rs index ba106fdfe4..0076ebddc4 100644 --- a/crates/astria-sequencer/src/service/info/mod.rs +++ b/crates/astria-sequencer/src/service/info/mod.rs @@ -205,6 +205,7 @@ mod tests { StateReadExt as _, StateWriteExt as _, }, + benchmark_and_test_utils::nria, }; #[tokio::test] @@ -225,7 +226,7 @@ mod tests { .unwrap(); state.put_base_prefix("astria".to_string()).unwrap(); - state.put_native_asset(crate::test_utils::nria()).unwrap(); + state.put_native_asset(nria()).unwrap(); let address = state .try_base_prefixed(&hex::decode("a034c743bed8f26cb8ee7b8db2230fd8347ae131").unwrap()) @@ -234,7 +235,7 @@ mod tests { let balance = 1000; state - .put_account_balance(&address, &crate::test_utils::nria(), balance) + .put_account_balance(&address, &nria(), balance) .unwrap(); state.put_block_height(height).unwrap(); storage.commit(state).await.unwrap(); @@ -261,7 +262,7 @@ mod tests { assert!(query_response.code.is_ok()); let expected_balance = AssetBalance { - denom: crate::test_utils::nria().into(), + denom: nria().into(), balance, }; diff --git a/crates/astria-sequencer/src/test_utils.rs b/crates/astria-sequencer/src/test_utils.rs index 2fcf2b9d95..d64009c8f2 100644 --- a/crates/astria-sequencer/src/test_utils.rs +++ b/crates/astria-sequencer/src/test_utils.rs @@ -1,19 +1,9 @@ use astria_core::primitive::v1::{ - asset::TracePrefixed, Address, Bech32, }; -pub(crate) const ASTRIA_PREFIX: &str = "astria"; -pub(crate) const ASTRIA_COMPAT_PREFIX: &str = "astriacompat"; - -pub(crate) fn astria_address(bytes: &[u8]) -> Address { - Address::builder() - .prefix(ASTRIA_PREFIX) - .slice(bytes) - .try_build() - .unwrap() -} +use crate::benchmark_and_test_utils::ASTRIA_COMPAT_PREFIX; #[expect( clippy::allow_attributes, @@ -28,34 +18,3 @@ pub(crate) fn astria_compat_address(bytes: &[u8]) -> Address { .try_build() .unwrap() } - -pub(crate) fn astria_address_from_hex_string(s: &str) -> Address { - let bytes = hex::decode(s).unwrap(); - Address::builder() - .prefix(ASTRIA_PREFIX) - .slice(bytes) - .try_build() - .unwrap() -} - -pub(crate) fn nria() -> TracePrefixed { - "nria".parse().unwrap() -} - -#[cfg(test)] -pub(crate) fn verification_key(seed: u64) -> astria_core::crypto::VerificationKey { - use rand::SeedableRng as _; - let rng = rand_chacha::ChaChaRng::seed_from_u64(seed); - let signing_key = astria_core::crypto::SigningKey::new(rng); - signing_key.verification_key() -} - -#[cfg(test)] -#[track_caller] -pub(crate) fn assert_eyre_error(error: &astria_eyre::eyre::Error, expected: &'static str) { - let msg = error.to_string(); - assert!( - msg.contains(expected), - "error contained different message\n\texpected: {expected}\n\tfull_error: {msg}", - ); -} diff --git a/crates/astria-sequencer/src/transaction/checks.rs b/crates/astria-sequencer/src/transaction/checks.rs index 07858213ea..56aefe52ae 100644 --- a/crates/astria-sequencer/src/transaction/checks.rs +++ b/crates/astria-sequencer/src/transaction/checks.rs @@ -333,10 +333,13 @@ mod tests { }, app::test_utils::*, assets::StateWriteExt as _, + benchmark_and_test_utils::{ + nria, + ASTRIA_PREFIX, + }, bridge::StateWriteExt as _, ibc::StateWriteExt as _, sequence::StateWriteExt as _, - test_utils::ASTRIA_PREFIX, }; #[tokio::test] @@ -346,9 +349,7 @@ mod tests { let mut state_tx = StateDelta::new(snapshot); state_tx.put_base_prefix("astria".to_string()).unwrap(); - state_tx - .put_native_asset(crate::test_utils::nria()) - .unwrap(); + state_tx.put_native_asset(nria()).unwrap(); state_tx.put_transfer_base_fee(12).unwrap(); state_tx.put_sequence_action_base_fee(0).unwrap(); state_tx @@ -371,7 +372,7 @@ mod tests { .try_base_prefixed(&alice.address_bytes()) .await .unwrap(), - &crate::test_utils::nria(), + &nria(), transfer_fee + crate::sequence::calculate_fee_from_state(&data, &state_tx) .await @@ -395,13 +396,13 @@ mod tests { Action::Transfer(TransferAction { asset: other_asset.clone(), amount, - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), to: state_tx.try_base_prefixed(&[0; ADDRESS_LEN]).await.unwrap(), }), Action::Sequence(SequenceAction { rollup_id: RollupId::from_unhashed_bytes([0; 32]), data, - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), }), ]; @@ -424,9 +425,7 @@ mod tests { let mut state_tx = StateDelta::new(snapshot); state_tx.put_base_prefix(ASTRIA_PREFIX.to_string()).unwrap(); - state_tx - .put_native_asset(crate::test_utils::nria()) - .unwrap(); + state_tx.put_native_asset(nria()).unwrap(); state_tx.put_transfer_base_fee(12).unwrap(); state_tx.put_sequence_action_base_fee(0).unwrap(); state_tx @@ -449,7 +448,7 @@ mod tests { .try_base_prefixed(&alice.address_bytes()) .await .unwrap(), - &crate::test_utils::nria(), + &nria(), transfer_fee + crate::sequence::calculate_fee_from_state(&data, &state_tx) .await @@ -462,13 +461,13 @@ mod tests { Action::Transfer(TransferAction { asset: other_asset.clone(), amount, - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), to: state_tx.try_base_prefixed(&[0; ADDRESS_LEN]).await.unwrap(), }), Action::Sequence(SequenceAction { rollup_id: RollupId::from_unhashed_bytes([0; 32]), data, - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), }), ];