From a8f4bb16a3fc5b2c911c21c3a03e8bdbace8eaf7 Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Sun, 18 Aug 2024 14:18:57 +0530 Subject: [PATCH] update: cleaner test integration on update_state_with_blobs --- crates/settlement-clients/ethereum/src/lib.rs | 37 ++++++++++--------- .../ethereum/src/tests/mod.rs | 7 +++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/crates/settlement-clients/ethereum/src/lib.rs b/crates/settlement-clients/ethereum/src/lib.rs index 03c05d8e..431fbfea 100644 --- a/crates/settlement-clients/ethereum/src/lib.rs +++ b/crates/settlement-clients/ethereum/src/lib.rs @@ -46,15 +46,6 @@ pub mod conversion; // - if "1" then : Testing via impersonating `Starknet Operator Address`. // Note : changing between "0" and "1" is handled automatically by each test function, `no` manual change in `env.test` is needed. -#[cfg(test)] -lazy_static! { - static ref SHOULD_IMPERSONATE_ACCOUNT: bool = get_env_var_or_panic("SHOULD_IMPERSONATE_ACCOUNT") == *"true"; - static ref TEST_DUMMY_CONTRACT_ADDRESS: String = get_env_var_or_panic("TEST_DUMMY_CONTRACT_ADDRESS"); - static ref ADDRESS_TO_IMPERSONATE: Address = - Address::from_str("0x2C169DFe5fBbA12957Bdd0Ba47d9CEDbFE260CA7").expect("Unable to parse address"); - static ref TEST_NONCE: u64 = 666068; -} - #[cfg(test)] mod tests; pub mod types; @@ -112,6 +103,7 @@ impl EthereumSettlementClient { #[cfg(test)] pub fn with_test_settings(settings: &impl SettingsProvider, provider: RootProvider>) -> Self { + use tests::{SHOULD_IMPERSONATE_ACCOUNT, TEST_DUMMY_CONTRACT_ADDRESS}; let settlement_cfg: EthereumSettlementConfig = settings.get_settings(SETTLEMENT_SETTINGS_NAME).unwrap(); let private_key = get_env_var_or_panic(ENV_PRIVATE_KEY); @@ -205,8 +197,6 @@ impl SettlementClient for EthereumSettlementClient { nonce: u64, ) -> Result { //TODO: better file management - #[cfg(test)] - use alloy::network::TransactionBuilder; let trusted_setup_path: String = CURRENT_PATH .join("src") @@ -262,12 +252,7 @@ impl SettlementClient for EthereumSettlementClient { let txn_request: TransactionRequest = tx_envelope.into(); #[cfg(test)] - let mut txn_request: TransactionRequest = tx_envelope.into(); - #[cfg(test)] - if *SHOULD_IMPERSONATE_ACCOUNT { - txn_request.set_nonce(*TEST_NONCE); - txn_request = txn_request.with_from(*ADDRESS_TO_IMPERSONATE); - } + let txn_request = test_config::configure_transaction(tx_envelope); let pending_transaction = self.provider.send_transaction(txn_request).await?; return Ok(pending_transaction.tx_hash().to_string()); @@ -307,3 +292,21 @@ impl SettlementClient for EthereumSettlementClient { Ok(nonce) } } + +#[cfg(test)] +mod test_config { + use super::*; + use alloy::network::TransactionBuilder; + use tests::{ADDRESS_TO_IMPERSONATE, SHOULD_IMPERSONATE_ACCOUNT, TEST_NONCE}; + + pub fn configure_transaction(tx_envelope: TxEnvelope) -> TransactionRequest { + let mut txn_request: TransactionRequest = tx_envelope.into(); + + if *SHOULD_IMPERSONATE_ACCOUNT { + txn_request.set_nonce(*TEST_NONCE); + txn_request = txn_request.with_from(*ADDRESS_TO_IMPERSONATE); + } + + txn_request + } +} diff --git a/crates/settlement-clients/ethereum/src/tests/mod.rs b/crates/settlement-clients/ethereum/src/tests/mod.rs index f0a42086..d323a30f 100644 --- a/crates/settlement-clients/ethereum/src/tests/mod.rs +++ b/crates/settlement-clients/ethereum/src/tests/mod.rs @@ -49,12 +49,15 @@ lazy_static! { .to_string(); static ref PORT: u16 = 3000_u16; static ref ETH_RPC: String = get_env_var_or_panic("ETHEREUM_BLAST_RPC_URL"); - static ref SHOULD_IMPERSONATE_ACCOUNT: bool = get_env_var_or_panic("SHOULD_IMPERSONATE_ACCOUNT") == *"true"; - static ref TEST_DUMMY_CONTRACT_ADDRESS: String = get_env_var_or_panic("TEST_DUMMY_CONTRACT_ADDRESS"); static ref STARKNET_OPERATOR_ADDRESS: Address = Address::from_str("0x2C169DFe5fBbA12957Bdd0Ba47d9CEDbFE260CA7").expect("Could not impersonate account."); static ref STARKNET_CORE_CONTRACT_ADDRESS: Address = Address::from_str("0xc662c410c0ecf747543f5ba90660f6abebd9c8c4").expect("Could not impersonate account."); + pub static ref ADDRESS_TO_IMPERSONATE: Address = + Address::from_str("0x2C169DFe5fBbA12957Bdd0Ba47d9CEDbFE260CA7").expect("Unable to parse address"); + pub static ref TEST_DUMMY_CONTRACT_ADDRESS: String = get_env_var_or_panic("TEST_DUMMY_CONTRACT_ADDRESS"); + pub static ref SHOULD_IMPERSONATE_ACCOUNT: bool = get_env_var_or_panic("SHOULD_IMPERSONATE_ACCOUNT") == *"true"; + pub static ref TEST_NONCE: u64 = 666068; } // SOLIDITY FUNCTIONS NEEDED