diff --git a/crates/astria-sequencer/src/accounts/state_ext.rs b/crates/astria-sequencer/src/accounts/state_ext.rs index 3811dfa54..ff466469d 100644 --- a/crates/astria-sequencer/src/accounts/state_ext.rs +++ b/crates/astria-sequencer/src/accounts/state_ext.rs @@ -282,7 +282,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 000000000..6c46e5c6e --- /dev/null +++ b/crates/astria-sequencer/src/app/benchmark_and_test_utils.rs @@ -0,0 +1,454 @@ +use std::collections::HashMap; + +use astria_core::{ + primitive::v1::asset::{ + Denom, + IbcPrefixed, + }, + protocol::{ + fees::v1::{ + BridgeLockFeeComponents, + BridgeSudoChangeFeeComponents, + BridgeUnlockFeeComponents, + FeeAssetChangeFeeComponents, + FeeChangeFeeComponents, + IbcRelayFeeComponents, + IbcRelayerChangeFeeComponents, + IbcSudoChangeFeeComponents, + Ics20WithdrawalFeeComponents, + InitBridgeAccountFeeComponents, + RollupDataSubmissionFeeComponents, + SudoAddressChangeFeeComponents, + TransferFeeComponents, + ValidatorUpdateFeeComponents, + }, + genesis::v1::{ + Account, + AddressPrefixes, + GenesisAppState, + }, + transaction::v1::action::ValidatorUpdate, + }, + Protobuf, +}; +use astria_eyre::eyre::WrapErr as _; +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, + }, + fees::StateWriteExt as _, + mempool::Mempool, + metrics::Metrics, +}; + +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::v1::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::v1::GenesisFees { + astria_core::protocol::genesis::v1::GenesisFees { + transfer: Some(TransferFeeComponents { + base: 12, + multiplier: 0, + }), + rollup_data_submission: Some(RollupDataSubmissionFeeComponents { + base: 32, + multiplier: 1, + }), + init_bridge_account: Some(InitBridgeAccountFeeComponents { + base: 48, + multiplier: 0, + }), + bridge_lock: Some(BridgeLockFeeComponents { + base: 12, // should reflect transfer fee + multiplier: 1, + }), + bridge_sudo_change: Some(BridgeSudoChangeFeeComponents { + base: 24, + multiplier: 0, + }), + ics20_withdrawal: Some(Ics20WithdrawalFeeComponents { + base: 24, + multiplier: 0, + }), + bridge_unlock: Some(BridgeUnlockFeeComponents { + base: 12, // should reflect transfer fee + multiplier: 0, + }), + ibc_relay: Some(IbcRelayFeeComponents { + base: 0, + multiplier: 0, + }), + validator_update: Some(ValidatorUpdateFeeComponents { + base: 0, + multiplier: 0, + }), + fee_asset_change: Some(FeeAssetChangeFeeComponents { + base: 0, + multiplier: 0, + }), + fee_change: FeeChangeFeeComponents { + base: 0, + multiplier: 0, + }, + ibc_relayer_change: Some(IbcRelayerChangeFeeComponents { + base: 0, + multiplier: 0, + }), + sudo_address_change: Some(SudoAddressChangeFeeComponents { + base: 0, + multiplier: 0, + }), + ibc_sudo_change: Some(IbcSudoChangeFeeComponents { + base: 0, + multiplier: 0, + }), + } +} + +pub(crate) fn proto_genesis_state() -> astria_core::generated::protocol::genesis::v1::GenesisAppState +{ + use astria_core::generated::protocol::genesis::v1::{ + 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 metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); + let mempool = Mempool::new(metrics, 100); + 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 { + nria().into() +} + +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(); +} + +#[expect( + clippy::too_many_lines, + reason = "lines come from necessary fees setup" +)] +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 + // setup tx fees + let transfer_fees = TransferFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_transfer_fees(transfer_fees) + .wrap_err("failed to initiate transfer fee components") + .unwrap(); + + let rollup_data_submission_fees = RollupDataSubmissionFeeComponents { + base: MOCK_SEQUENCE_FEE, + multiplier: 0, + }; + state + .put_rollup_data_submission_fees(rollup_data_submission_fees) + .wrap_err("failed to initiate sequence action fee components") + .unwrap(); + + let ics20_withdrawal_fees = Ics20WithdrawalFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_ics20_withdrawal_fees(ics20_withdrawal_fees) + .wrap_err("failed to initiate ics20 withdrawal fee components") + .unwrap(); + + let init_bridge_account_fees = InitBridgeAccountFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_init_bridge_account_fees(init_bridge_account_fees) + .wrap_err("failed to initiate init bridge account fee components") + .unwrap(); + + let bridge_lock_fees = BridgeLockFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_bridge_lock_fees(bridge_lock_fees) + .wrap_err("failed to initiate bridge lock fee components") + .unwrap(); + + let bridge_unlock_fees = BridgeUnlockFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_bridge_unlock_fees(bridge_unlock_fees) + .wrap_err("failed to initiate bridge unlock fee components") + .unwrap(); + + let bridge_sudo_change_fees = BridgeSudoChangeFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_bridge_sudo_change_fees(bridge_sudo_change_fees) + .wrap_err("failed to initiate bridge sudo change fee components") + .unwrap(); + + let ibc_relay_fees = IbcRelayFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_ibc_relay_fees(ibc_relay_fees) + .wrap_err("failed to initiate ibc relay fee components") + .unwrap(); + + let validator_update_fees = ValidatorUpdateFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_validator_update_fees(validator_update_fees) + .wrap_err("failed to initiate validator update fee components") + .unwrap(); + + let fee_asset_change_fees = FeeAssetChangeFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_fee_asset_change_fees(fee_asset_change_fees) + .wrap_err("failed to initiate fee asset change fee components") + .unwrap(); + + let fee_change_fees = FeeChangeFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_fee_change_fees(fee_change_fees) + .wrap_err("failed to initiate fee change fees fee components") + .unwrap(); + + let ibc_relayer_change_fees = IbcRelayerChangeFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_ibc_relayer_change_fees(ibc_relayer_change_fees) + .wrap_err("failed to initiate ibc relayer change fee components") + .unwrap(); + + let sudo_address_change_fees = SudoAddressChangeFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_sudo_address_change_fees(sudo_address_change_fees) + .wrap_err("failed to initiate sudo address change fee components") + .unwrap(); + + let ibc_sudo_change_fees = IbcSudoChangeFeeComponents { + base: 0, + multiplier: 0, + }; + state + .put_ibc_sudo_change_fees(ibc_sudo_change_fees) + .wrap_err("failed to initiate ibc sudo change fee components") + .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 903b8161c..0b6aa3ef2 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 2231b3ab6..99d13a115 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; @@ -806,7 +808,7 @@ impl App { error = AsRef::::as_ref(&e), "transaction error: failed to execute transaction" ); - bail!("transaction failed to execute"); + return Err(e.wrap_err("transaction failed to execute")); } } diff --git a/crates/astria-sequencer/src/app/test_utils.rs b/crates/astria-sequencer/src/app/test_utils.rs index 8faa1f5d4..f9f1ba8de 100644 --- a/crates/astria-sequencer/src/app/test_utils.rs +++ b/crates/astria-sequencer/src/app/test_utils.rs @@ -1,39 +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::{ - fees::v1::{ - BridgeLockFeeComponents, - BridgeSudoChangeFeeComponents, - BridgeUnlockFeeComponents, - FeeAssetChangeFeeComponents, - FeeChangeFeeComponents, - IbcRelayFeeComponents, - IbcRelayerChangeFeeComponents, - IbcSudoChangeFeeComponents, - Ics20WithdrawalFeeComponents, - InitBridgeAccountFeeComponents, - RollupDataSubmissionFeeComponents, - SudoAddressChangeFeeComponents, - TransferFeeComponents, - ValidatorUpdateFeeComponents, - }, - genesis::v1::{ - Account, - AddressPrefixes, - GenesisAppState, - }, + genesis::v1::GenesisAppState, transaction::v1::{ action::{ group::Group, @@ -48,42 +19,21 @@ use astria_core::{ TransactionBody, }, }, - Protobuf, }; -use astria_eyre::eyre::WrapErr as _; use bytes::Bytes; -use cnidarium::{ - Snapshot, - StateDelta, - Storage, -}; -use telemetry::Metrics as _; use crate::{ - accounts::StateWriteExt, - app::App, - assets::StateWriteExt as AssetStateWriteExt, - fees::StateWriteExt as _, - mempool::Mempool, - metrics::Metrics, - test_utils::{ - astria_address_from_hex_string, - nria, + app::{ + benchmark_and_test_utils::{ + denom_0, + initialize_app_with_storage, + JUDY_ADDRESS, + }, + App, }, + benchmark_and_test_utils::astria_address_from_hex_string, }; -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] = @@ -94,12 +44,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] = @@ -110,12 +54,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] = @@ -126,12 +64,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] = @@ -142,12 +74,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") @@ -157,164 +83,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::v1::GenesisFees { - astria_core::protocol::genesis::v1::GenesisFees { - transfer: Some(TransferFeeComponents { - base: 12, - multiplier: 0, - }), - rollup_data_submission: Some(RollupDataSubmissionFeeComponents { - base: 32, - multiplier: 1, - }), - init_bridge_account: Some(InitBridgeAccountFeeComponents { - base: 48, - multiplier: 0, - }), - bridge_lock: Some(BridgeLockFeeComponents { - base: 12, // should reflect transfer fee - multiplier: 1, - }), - bridge_sudo_change: Some(BridgeSudoChangeFeeComponents { - base: 24, - multiplier: 0, - }), - ics20_withdrawal: Some(Ics20WithdrawalFeeComponents { - base: 24, - multiplier: 0, - }), - bridge_unlock: Some(BridgeUnlockFeeComponents { - base: 12, // should reflect transfer fee - multiplier: 0, - }), - ibc_relay: Some(IbcRelayFeeComponents { - base: 0, - multiplier: 0, - }), - validator_update: Some(ValidatorUpdateFeeComponents { - base: 0, - multiplier: 0, - }), - fee_asset_change: Some(FeeAssetChangeFeeComponents { - base: 0, - multiplier: 0, - }), - fee_change: FeeChangeFeeComponents { - base: 0, - multiplier: 0, - }, - ibc_relayer_change: Some(IbcRelayerChangeFeeComponents { - base: 0, - multiplier: 0, - }), - sudo_address_change: Some(SudoAddressChangeFeeComponents { - base: 0, - multiplier: 0, - }), - ibc_sudo_change: Some(IbcSudoChangeFeeComponents { - base: 0, - multiplier: 0, - }), - } -} - -pub(crate) fn address_prefixes() -> AddressPrefixes { - AddressPrefixes::try_from_raw( - astria_core::generated::protocol::genesis::v1::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::v1::GenesisAppState -{ - use astria_core::generated::protocol::genesis::v1::{ - 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 metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); - let mempool = Mempool::new(metrics, 100); - 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, @@ -323,12 +91,6 @@ 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, @@ -336,12 +98,6 @@ pub(crate) struct MockTxBuilder { group: Group, } -#[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 { @@ -420,266 +176,3 @@ impl MockTxBuilder { Arc::new(tx.sign(&self.signer)) } } - -pub(crate) const MOCK_SEQUENCE_FEE: u128 = 10; -pub(crate) fn denom_0() -> Denom { - nria().into() -} - -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, - clippy::too_many_lines, - 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 - let transfer_fees = TransferFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_transfer_fees(transfer_fees) - .wrap_err("failed to initiate transfer fee components") - .unwrap(); - - let rollup_data_submission_fees = RollupDataSubmissionFeeComponents { - base: MOCK_SEQUENCE_FEE, - multiplier: 0, - }; - state - .put_rollup_data_submission_fees(rollup_data_submission_fees) - .wrap_err("failed to initiate sequence action fee components") - .unwrap(); - - let ics20_withdrawal_fees = Ics20WithdrawalFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_ics20_withdrawal_fees(ics20_withdrawal_fees) - .wrap_err("failed to initiate ics20 withdrawal fee components") - .unwrap(); - - let init_bridge_account_fees = InitBridgeAccountFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_init_bridge_account_fees(init_bridge_account_fees) - .wrap_err("failed to initiate init bridge account fee components") - .unwrap(); - - let bridge_lock_fees = BridgeLockFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_bridge_lock_fees(bridge_lock_fees) - .wrap_err("failed to initiate bridge lock fee components") - .unwrap(); - - let bridge_unlock_fees = BridgeUnlockFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_bridge_unlock_fees(bridge_unlock_fees) - .wrap_err("failed to initiate bridge unlock fee components") - .unwrap(); - - let bridge_sudo_change_fees = BridgeSudoChangeFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_bridge_sudo_change_fees(bridge_sudo_change_fees) - .wrap_err("failed to initiate bridge sudo change fee components") - .unwrap(); - - let ibc_relay_fees = IbcRelayFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_ibc_relay_fees(ibc_relay_fees) - .wrap_err("failed to initiate ibc relay fee components") - .unwrap(); - - let validator_update_fees = ValidatorUpdateFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_validator_update_fees(validator_update_fees) - .wrap_err("failed to initiate validator update fee components") - .unwrap(); - - let fee_asset_change_fees = FeeAssetChangeFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_fee_asset_change_fees(fee_asset_change_fees) - .wrap_err("failed to initiate fee asset change fee components") - .unwrap(); - - let fee_change_fees = FeeChangeFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_fee_change_fees(fee_change_fees) - .wrap_err("failed to initiate fee change fees fee components") - .unwrap(); - - let ibc_relayer_change_fees = IbcRelayerChangeFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_ibc_relayer_change_fees(ibc_relayer_change_fees) - .wrap_err("failed to initiate ibc relayer change fee components") - .unwrap(); - - let sudo_address_change_fees = SudoAddressChangeFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_sudo_address_change_fees(sudo_address_change_fees) - .wrap_err("failed to initiate sudo address change fee components") - .unwrap(); - - let ibc_sudo_change_fees = IbcSudoChangeFeeComponents { - base: 0, - multiplier: 0, - }; - state - .put_ibc_sudo_change_fees(ibc_sudo_change_fees) - .wrap_err("failed to initiate ibc sudo change fee components") - .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 24b233589..adc304d58 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 0a03bd72b..1ca46cec4 100644 --- a/crates/astria-sequencer/src/app/tests_app/mod.rs +++ b/crates/astria-sequencer/src/app/tests_app/mod.rs @@ -22,6 +22,14 @@ use astria_core::{ }, sequencerblock::v1::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, @@ -58,15 +66,15 @@ use crate::{ StateWriteExt as _, ValidatorSet, }, - bridge::StateWriteExt as _, - fees::StateReadExt 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 _, + fees::StateReadExt as _, + proposal::commitment::generate_rollup_datas_commitment, }; fn default_tendermint_header() -> Header { @@ -158,7 +166,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(), @@ -872,11 +880,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 new file mode 100644 index 000000000..e69de29bb diff --git a/crates/astria-sequencer/src/app/tests_block_ordering.rs b/crates/astria-sequencer/src/app/tests_block_ordering.rs index f6c7f4642..c8c28893f 100644 --- a/crates/astria-sequencer/src/app/tests_block_ordering.rs +++ b/crates/astria-sequencer/src/app/tests_block_ordering.rs @@ -24,13 +24,17 @@ use tendermint::{ use super::test_utils::get_alice_signing_key; use crate::{ - app::test_utils::{ - get_bob_signing_key, - get_judy_signing_key, - initialize_app_with_storage, - mock_balances, - mock_tx_cost, - MockTxBuilder, + app::{ + benchmark_and_test_utils::{ + initialize_app_with_storage, + mock_balances, + mock_tx_cost, + }, + test_utils::{ + get_bob_signing_key, + get_judy_signing_key, + MockTxBuilder, + }, }, proposal::commitment::generate_rollup_datas_commitment, }; @@ -138,7 +142,6 @@ async fn app_process_proposal_ordering_fail() { .process_proposal(process_proposal.clone(), storage.clone()) .await .expect_err("expected ordering error"); - assert!( format!("{result:?}").contains("transactions have incorrect transaction group ordering"), "process proposal should fail due to incorrect ordering" diff --git a/crates/astria-sequencer/src/app/tests_breaking_changes.rs b/crates/astria-sequencer/src/app/tests_breaking_changes.rs index 9948ba851..031538a05 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 8b4f7409c..7f4d99d65 100644 --- a/crates/astria-sequencer/src/app/tests_execute_transaction.rs +++ b/crates/astria-sequencer/src/app/tests_execute_transaction.rs @@ -40,15 +40,24 @@ 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 _, }, authority::StateReadExt as _, + benchmark_and_test_utils::{ + astria_address, + astria_address_from_hex_string, + nria, + verification_key, + ASTRIA_PREFIX, + }, bridge::{ StateReadExt as _, StateWriteExt as _, @@ -58,13 +67,7 @@ use crate::{ StateWriteExt as _, }, ibc::StateReadExt as _, - test_utils::{ - astria_address, - astria_address_from_hex_string, - calculate_rollup_data_submission_fee_from_state, - nria, - ASTRIA_PREFIX, - }, + test_utils::calculate_rollup_data_submission_fee_from_state, transaction::{ InvalidChainId, InvalidNonce, @@ -86,7 +89,7 @@ fn proto_genesis_state() -> astria_core::generated::protocol::genesis::v1::Genes .unwrap() .to_raw(), ), - ..crate::app::test_utils::proto_genesis_state() + ..crate::app::benchmark_and_test_utils::proto_genesis_state() } } @@ -112,8 +115,8 @@ async fn app_execute_transaction_transfer() { Transfer { 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(), ]) @@ -346,7 +349,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 = TransactionBody::builder() @@ -365,7 +368,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 16b1dde85..e55b81fcb 100644 --- a/crates/astria-sequencer/src/authority/state_ext.rs +++ b/crates/astria-sequencer/src/authority/state_ext.rs @@ -126,7 +126,7 @@ mod tests { use super::*; use crate::{ address::StateWriteExt as _, - 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 000000000..3b94f6dbb --- /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 c99556bff..3d0769ecc 100644 --- a/crates/astria-sequencer/src/benchmark_utils.rs +++ b/crates/astria-sequencer/src/benchmark_utils.rs @@ -23,7 +23,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_sudo_change_action.rs b/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs index 2a44ae170..655694abf 100644 --- a/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs +++ b/crates/astria-sequencer/src/bridge/bridge_sudo_change_action.rs @@ -91,11 +91,11 @@ mod tests { use crate::{ accounts::StateWriteExt as _, address::StateWriteExt as _, - fees::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ astria_address, ASTRIA_PREFIX, }, + fees::StateWriteExt as _, transaction::{ StateWriteExt as _, TransactionContext, diff --git a/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs b/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs index ed90e15f5..52fcb2b40 100644 --- a/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs +++ b/crates/astria-sequencer/src/bridge/bridge_unlock_action.rs @@ -119,13 +119,13 @@ mod tests { accounts::StateWriteExt as _, address::StateWriteExt as _, app::ActionHandler as _, - bridge::StateWriteExt as _, - fees::StateWriteExt as _, - test_utils::{ + benchmark_and_test_utils::{ assert_eyre_error, astria_address, ASTRIA_PREFIX, }, + bridge::StateWriteExt as _, + fees::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 9f194f57e..2e3ff25ba 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 692274db8..68bd6c292 100644 --- a/crates/astria-sequencer/src/bridge/state_ext.rs +++ b/crates/astria-sequencer/src/bridge/state_ext.rs @@ -333,7 +333,7 @@ mod tests { use cnidarium::StateDelta; 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/fees/state_ext.rs b/crates/astria-sequencer/src/fees/state_ext.rs index dd66e87bb..06e954e3a 100644 --- a/crates/astria-sequencer/src/fees/state_ext.rs +++ b/crates/astria-sequencer/src/fees/state_ext.rs @@ -595,7 +595,7 @@ mod tests { use tokio::pin; use super::*; - use crate::app::test_utils::initialize_app_with_storage; + use crate::app::benchmark_and_test_utils::initialize_app_with_storage; fn asset_0() -> asset::Denom { "asset_0".parse().unwrap() diff --git a/crates/astria-sequencer/src/fees/tests.rs b/crates/astria-sequencer/src/fees/tests.rs index c7995b7cb..f486683a9 100644 --- a/crates/astria-sequencer/src/fees/tests.rs +++ b/crates/astria-sequencer/src/fees/tests.rs @@ -38,28 +38,30 @@ use crate::{ accounts::StateWriteExt as _, address::StateWriteExt as _, app::{ + benchmark_and_test_utils::{ + initialize_app_with_storage, + BOB_ADDRESS, + }, test_utils::{ get_alice_signing_key, get_bridge_signing_key, - initialize_app_with_storage, - BOB_ADDRESS, }, ActionHandler as _, }, - bridge::StateWriteExt as _, - fees::{ - StateReadExt as _, - StateWriteExt as _, - DEPOSIT_BASE_FEE, - }, - test_utils::{ + benchmark_and_test_utils::{ assert_eyre_error, astria_address, astria_address_from_hex_string, - calculate_rollup_data_submission_fee_from_state, nria, ASTRIA_PREFIX, }, + bridge::StateWriteExt as _, + fees::{ + StateReadExt as _, + StateWriteExt as _, + DEPOSIT_BASE_FEE, + }, + test_utils::calculate_rollup_data_submission_fee_from_state, transaction::{ StateWriteExt as _, TransactionContext, diff --git a/crates/astria-sequencer/src/grpc/sequencer.rs b/crates/astria-sequencer/src/grpc/sequencer.rs index fd1dacd5b..7d74f077f 100644 --- a/crates/astria-sequencer/src/grpc/sequencer.rs +++ b/crates/astria-sequencer/src/grpc/sequencer.rs @@ -231,15 +231,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 c1af73011..fc377d48c 100644 --- a/crates/astria-sequencer/src/grpc/state_ext.rs +++ b/crates/astria-sequencer/src/grpc/state_ext.rs @@ -342,7 +342,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 0dc0d2794..4fa98ee3c 100644 --- a/crates/astria-sequencer/src/ibc/ics20_transfer.rs +++ b/crates/astria-sequencer/src/ibc/ics20_transfer.rs @@ -776,6 +776,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 _, @@ -784,13 +790,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 10c955e69..12975e4ab 100644 --- a/crates/astria-sequencer/src/ibc/ics20_withdrawal.rs +++ b/crates/astria-sequencer/src/ibc/ics20_withdrawal.rs @@ -274,7 +274,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 5bc28cf21..3307e5b2e 100644 --- a/crates/astria-sequencer/src/ibc/state_ext.rs +++ b/crates/astria-sequencer/src/ibc/state_ext.rs @@ -167,11 +167,12 @@ mod tests { use super::*; use crate::{ - address::StateWriteExt as _, - test_utils::{ + address::StateWriteExt, + benchmark_and_test_utils::{ astria_address, ASTRIA_PREFIX, }, + ibc::StateWriteExt as _, }; fn asset_0() -> asset::Denom { diff --git a/crates/astria-sequencer/src/lib.rs b/crates/astria-sequencer/src/lib.rs index 6b109141e..8701ee6a7 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; @@ -19,7 +21,7 @@ pub(crate) mod rollup_data; mod sequencer; pub(crate) mod service; pub(crate) mod storage; -#[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 8190c3a68..3caaa920d 100644 --- a/crates/astria-sequencer/src/mempool/benchmarks.rs +++ b/crates/astria-sequencer/src/mempool/benchmarks.rs @@ -20,7 +20,7 @@ use sha2::{ use telemetry::Metrics; 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 ad970bc3f..e1676dcb0 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 623f6f963..68579620f 100644 --- a/crates/astria-sequencer/src/mempool/mod.rs +++ b/crates/astria-sequencer/src/mempool/mod.rs @@ -505,19 +505,23 @@ 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 54ba8266a..95d17b00c 100644 --- a/crates/astria-sequencer/src/mempool/transactions_container.rs +++ b/crates/astria-sequencer/src/mempool/transactions_container.rs @@ -859,25 +859,29 @@ 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, - get_judy_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, + get_judy_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 64d5a05cf..52a503973 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 rollup_data_submission = RollupDataSubmission { 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 = Transfer { - 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); @@ -145,6 +149,7 @@ mod tests { .unwrap(); let signed_tx = tx.sign(&signing_key); + let txs = vec![signed_tx]; let GeneratedCommitments { rollup_datas_root: commitment_1, @@ -164,13 +169,13 @@ mod tests { let rollup_data_submission = RollupDataSubmission { 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 = Transfer { - 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 2d0443513..057fa091e 100644 --- a/crates/astria-sequencer/src/service/consensus.rs +++ b/crates/astria-sequencer/src/service/consensus.rs @@ -227,7 +227,7 @@ mod tests { use super::*; use crate::{ - app::test_utils::{ + app::benchmark_and_test_utils::{ mock_balances, mock_tx_cost, }, @@ -242,7 +242,7 @@ mod tests { RollupDataSubmission { 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 { let accounts = if let Some(funded_key) = funded_key { vec![astria_core::generated::protocol::genesis::v1::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()), }] @@ -460,7 +461,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 07d71015f..23b863aeb 100644 --- a/crates/astria-sequencer/src/service/info/mod.rs +++ b/crates/astria-sequencer/src/service/info/mod.rs @@ -202,6 +202,7 @@ mod tests { }, app::StateWriteExt as _, assets::StateWriteExt as _, + benchmark_and_test_utils::nria, fees::{ StateReadExt as _, StateWriteExt as _, @@ -226,8 +227,8 @@ mod tests { .unwrap(); state.put_base_prefix("astria".to_string()).unwrap(); - state.put_native_asset(crate::test_utils::nria()).unwrap(); - state.put_ibc_asset(crate::test_utils::nria()).unwrap(); + state.put_native_asset(nria()).unwrap(); + state.put_ibc_asset(nria()).unwrap(); let address = state .try_base_prefixed(&hex::decode("a034c743bed8f26cb8ee7b8db2230fd8347ae131").unwrap()) @@ -236,7 +237,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(); @@ -263,7 +264,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/service/mempool/tests.rs b/crates/astria-sequencer/src/service/mempool/tests.rs index 1edb81e54..2baa7bda6 100644 --- a/crates/astria-sequencer/src/service/mempool/tests.rs +++ b/crates/astria-sequencer/src/service/mempool/tests.rs @@ -13,6 +13,7 @@ use tendermint::{ use crate::{ app::{ + benchmark_and_test_utils::genesis_state, test_utils::MockTxBuilder, App, }, @@ -31,7 +32,7 @@ async fn future_nonces_are_accepted() { let metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); let mut mempool = Mempool::new(metrics, 100); let mut app = App::new(snapshot, mempool.clone(), metrics).await.unwrap(); - let genesis_state = crate::app::test_utils::genesis_state(); + let genesis_state = genesis_state(); app.init_chain(storage.clone(), genesis_state, vec![], "test".to_string()) .await @@ -61,7 +62,7 @@ async fn rechecks_pass() { let metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); let mut mempool = Mempool::new(metrics, 100); let mut app = App::new(snapshot, mempool.clone(), metrics).await.unwrap(); - let genesis_state = crate::app::test_utils::genesis_state(); + let genesis_state = genesis_state(); app.init_chain(storage.clone(), genesis_state, vec![], "test".to_string()) .await @@ -99,7 +100,7 @@ async fn can_reinsert_after_recheck_fail() { let metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); let mut mempool = Mempool::new(metrics, 100); let mut app = App::new(snapshot, mempool.clone(), metrics).await.unwrap(); - let genesis_state = crate::app::test_utils::genesis_state(); + let genesis_state = genesis_state(); app.init_chain(storage.clone(), genesis_state, vec![], "test".to_string()) .await @@ -147,7 +148,7 @@ async fn recheck_adds_non_tracked_tx() { let metrics = Box::leak(Box::new(Metrics::noop_metrics(&()).unwrap())); let mut mempool = Mempool::new(metrics, 100); let mut app = App::new(snapshot, mempool.clone(), metrics).await.unwrap(); - let genesis_state = crate::app::test_utils::genesis_state(); + let genesis_state = genesis_state(); app.init_chain(storage.clone(), genesis_state, vec![], "test".to_string()) .await diff --git a/crates/astria-sequencer/src/test_utils.rs b/crates/astria-sequencer/src/test_utils.rs index e7e12935d..65c2ff327 100644 --- a/crates/astria-sequencer/src/test_utils.rs +++ b/crates/astria-sequencer/src/test_utils.rs @@ -1,21 +1,11 @@ use astria_core::primitive::v1::{ - asset::TracePrefixed, Address, Bech32, }; #[cfg(test)] use astria_core::protocol::fees::v1::RollupDataSubmissionFeeComponents; -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, @@ -31,37 +21,6 @@ pub(crate) fn astria_compat_address(bytes: &[u8]) -> Address { .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}", - ); -} - /// Calculates the fee for a sequence `Action` based on the length of the `data`. #[cfg(test)] pub(crate) async fn calculate_rollup_data_submission_fee_from_state< diff --git a/crates/astria-sequencer/src/transaction/checks.rs b/crates/astria-sequencer/src/transaction/checks.rs index 507a22153..3e00c7b0f 100644 --- a/crates/astria-sequencer/src/transaction/checks.rs +++ b/crates/astria-sequencer/src/transaction/checks.rs @@ -162,14 +162,15 @@ mod tests { }, app::test_utils::*, assets::StateWriteExt as _, + benchmark_and_test_utils::{ + nria, + ASTRIA_PREFIX, + }, fees::{ StateReadExt as _, StateWriteExt as _, }, - test_utils::{ - calculate_rollup_data_submission_fee_from_state, - ASTRIA_PREFIX, - }, + test_utils::calculate_rollup_data_submission_fee_from_state, }; #[tokio::test] @@ -180,9 +181,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(); let transfer_fees = TransferFeeComponents { base: 12, multiplier: 0, @@ -263,7 +262,7 @@ mod tests { .try_base_prefixed(&alice.address_bytes()) .await .unwrap(), - &crate::test_utils::nria(), + &nria(), transfer_fee + calculate_rollup_data_submission_fee_from_state(&data, &state_tx).await, ) @@ -285,13 +284,13 @@ mod tests { Action::Transfer(Transfer { 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::RollupDataSubmission(RollupDataSubmission { rollup_id: RollupId::from_unhashed_bytes([0; 32]), data, - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), }), ]; @@ -315,9 +314,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(); let transfer_fees = TransferFeeComponents { base: 12, multiplier: 0, @@ -398,7 +395,7 @@ mod tests { .try_base_prefixed(&alice.address_bytes()) .await .unwrap(), - &crate::test_utils::nria(), + &nria(), transfer_fee + calculate_rollup_data_submission_fee_from_state(&data, &state_tx).await, ) @@ -409,13 +406,13 @@ mod tests { Action::Transfer(Transfer { 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::RollupDataSubmission(RollupDataSubmission { rollup_id: RollupId::from_unhashed_bytes([0; 32]), data, - fee_asset: crate::test_utils::nria().into(), + fee_asset: nria().into(), }), ];