diff --git a/Cargo.lock b/Cargo.lock index ccb5359c..b4db803e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -110,9 +110,9 @@ dependencies = [ [[package]] name = "alloy-consensus" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41e46a465e50a339a817070ec23f06eb3fc9fbb8af71612868367b875a9d49e3" +checksum = "8e30ab0d3e3c32976f67fc1a96179989e45a69594af42003a6663332f9b0bb9d" dependencies = [ "alloy-eips", "alloy-primitives", @@ -138,9 +138,9 @@ dependencies = [ [[package]] name = "alloy-consensus-any" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07001b1693af794c7526aab400b42e38075f986ef8fef78841e5ebc745473e56" +checksum = "c20736b1f9d927d875d8777ef0c2250d4c57ea828529a9dbfa2c628db57b911e" dependencies = [ "alloy-consensus", "alloy-eips", @@ -153,9 +153,9 @@ dependencies = [ [[package]] name = "alloy-contract" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ef1b07c3ff5bf4fab5b8e6c46190cd40b2f2fd2cd72b5b02527a38125d0bff4" +checksum = "008aba161fce2a0d94956ae09d7d7a09f8fbdf18acbef921809ef126d6cdaf97" dependencies = [ "alloy-consensus", "alloy-dyn-abi", @@ -250,9 +250,9 @@ dependencies = [ [[package]] name = "alloy-eips" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707337efeb051ddbaece17a73eaec5150945a5a5541112f4146508248edc2e40" +checksum = "15b85157b7be31fc4adf6acfefcb0d4308cba5dbd7a8d8e62bcc02ff37d6131a" dependencies = [ "alloy-eip2124", "alloy-eip2930", @@ -299,9 +299,9 @@ dependencies = [ [[package]] name = "alloy-genesis" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64ba7afffa225272cf50c62ff04ac574adc7bfa73af2370db556340f26fcff5c" +checksum = "a838301c4e2546c96db1848f18ffe9f722f2fccd9715b83d4bf269a2cf00b5a1" dependencies = [ "alloy-eips", "alloy-primitives", @@ -340,9 +340,9 @@ dependencies = [ [[package]] name = "alloy-json-rpc" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48562f9b4c4e1514cab54af16feaffc18194a38216bbd0c23004ec4667ad696b" +checksum = "60f045b69b5e80b8944b25afe74ae6b974f3044d84b4a7a113da04745b2524cc" dependencies = [ "alloy-primitives", "alloy-sol-types", @@ -355,9 +355,9 @@ dependencies = [ [[package]] name = "alloy-network" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "364a5eaa598437d7a57bcbcb4b7fcb0518e192cf809a19b09b2b5cf73b9ba1cd" +checksum = "2b314ed5bdc7f449c53853125af2db5ac4d3954a9f4b205e7d694f02fc1932d1" dependencies = [ "alloy-consensus", "alloy-consensus-any", @@ -381,9 +381,9 @@ dependencies = [ [[package]] name = "alloy-network-primitives" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21af5255bd276e528ee625d97033884916e879a1c6edcd5b70a043bd440c0710" +checksum = "5e9762ac5cca67b0f6ab614f7f8314942eead1c8eeef61511ea43a6ff048dbe0" dependencies = [ "alloy-consensus", "alloy-eips", @@ -455,9 +455,9 @@ dependencies = [ [[package]] name = "alloy-provider" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc919fe241f9dd28c4c7f7dcff9e66e550c280bafe3545e1019622e1239db38" +checksum = "ea8f7ca47514e7f552aa9f3f141ab17351332c6637e3bf00462d8e7c5f10f51f" dependencies = [ "alloy-chains", "alloy-consensus", @@ -482,7 +482,7 @@ dependencies = [ "either", "futures", "futures-utils-wasm", - "lru 0.13.0", + "lru 0.16.3", "parking_lot", "pin-project", "reqwest", @@ -497,9 +497,9 @@ dependencies = [ [[package]] name = "alloy-pubsub" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23a0778833917a71a9e0065e0409bfc00cddef55ca962b3453472be38ebe7035" +checksum = "4082778c908aa801a1f9fdc85d758812842ab4b2aaba58e9dbe7626d708ab7e1" dependencies = [ "alloy-json-rpc", "alloy-primitives", @@ -541,9 +541,9 @@ dependencies = [ [[package]] name = "alloy-rpc-client" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b587e63d8c4af437b0a7830dc12d24cb495e956cc8ecbf93e96d62c9cb55b13" +checksum = "26dd083153d2cb73cce1516f5a3f9c3af74764a2761d901581a355777468bd8f" dependencies = [ "alloy-json-rpc", "alloy-primitives", @@ -567,9 +567,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3000edc72a300048cf461df94bfa29fc5d7760ddd88ca7d56ea6fc8b28729" +checksum = "8c998214325cfee1fbe61e5abaed3a435f4ca746ac7399b46feb57c364552452" dependencies = [ "alloy-primitives", "alloy-rpc-types-engine", @@ -580,9 +580,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-admin" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebb98103316e6f4a1ebc6e71328c2d18426cdd79fc999c44afd9f0f4e9f5edd6" +checksum = "730a38742dc0753f25b8ce7330c2fa88d79f165c5fc2f19f3d35291739c42e83" dependencies = [ "alloy-genesis", "alloy-primitives", @@ -592,9 +592,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-anvil" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1207e852f30297d6918f91df3e76f758fa7b519ea1e49fbd7d961ce796663f9" +checksum = "a2b03d65fcf579fbf17d3aac32271f99e2b562be04097436cd6e766b3e06613b" dependencies = [ "alloy-primitives", "alloy-rpc-types-eth", @@ -604,9 +604,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-any" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ebc96cf29095c10a183fb7106a097fe12ca8dd46733895582da255407f54b29" +checksum = "4b4a6f49d161ef83354d5ba3c8bc83c8ee464cb90182b215551d5c4b846579be" dependencies = [ "alloy-consensus-any", "alloy-rpc-types-eth", @@ -615,9 +615,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-beacon" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cea7c1c22628b13b25d31fd63fa5dfa7fac0b0b78f1c89a5068102b653ff65c" +checksum = "3b6654644613f33fd2e6f333f4ce8ad0a26f036c0513699d7bc168bba18d412d" dependencies = [ "alloy-eips", "alloy-primitives", @@ -635,9 +635,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-debug" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e1a6b13b6f95b80d3ff770998f81e61811264eb1d18b88dfa11c80180acdc1b" +checksum = "467025b916f32645f322a085d0017f2996d0200ac89dd82a4fc2bf0f17b9afa3" dependencies = [ "alloy-primitives", "derive_more", @@ -647,9 +647,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-engine" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f35af673cc14e89813ab33671d79b6e73fe38788c5f3a8ec3a75476b58225f53" +checksum = "933aaaace9faa6d7efda89472add89a8bfd15270318c47a2be8bb76192c951e2" dependencies = [ "alloy-consensus", "alloy-eips", @@ -667,9 +667,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-eth" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cc3f354a5079480acca0a6533d1d3838177a03ea494ef0ae8d1679efea88274" +checksum = "11920b16ab7c86052f990dcb4d25312fb2889faf506c4ee13dc946b450536989" dependencies = [ "alloy-consensus", "alloy-consensus-any", @@ -689,9 +689,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-mev" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10fbd905c35f780926ff0c4c2a74d3ce7d50576cb0e9997dc783ac99c6fd7afb" +checksum = "1826454c2890af6d642bf052909e0162ad7f261d172e56ef2e936d479960699c" dependencies = [ "alloy-consensus", "alloy-eips", @@ -704,9 +704,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-trace" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d782d80221dfaa5a2f8a7bf277370bdec10e4e8119f5a60d2e2b1adb2e806ca" +checksum = "498375e6a13b6edd04422a13d2b1a6187183e5a3aa14c5907b4c566551248bab" dependencies = [ "alloy-primitives", "alloy-rpc-types-eth", @@ -718,9 +718,9 @@ dependencies = [ [[package]] name = "alloy-rpc-types-txpool" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3076c226bb4365f9c3ac0cd4082ba86208aaa1485cbf664383a90aba7c36b26" +checksum = "6d9123d321ecd70925646eb2c60b1d9b7a965f860fbd717643e2c20fcf85d48d" dependencies = [ "alloy-primitives", "alloy-rpc-types-eth", @@ -730,9 +730,9 @@ dependencies = [ [[package]] name = "alloy-serde" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a438ce4cd49ec4bc213868c1fe94f2fe103d4c3f22f6a42073db974f9c0962da" +checksum = "d1a0d2d5c64881f3723232eaaf6c2d9f4f88b061c63e87194b2db785ff3aa31f" dependencies = [ "alloy-primitives", "arbitrary", @@ -742,9 +742,9 @@ dependencies = [ [[package]] name = "alloy-signer" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "389372d6ae4d62b88c8dca8238e4f7d0a7727b66029eb8a5516a908a03161450" +checksum = "5ea4ac9765e5a7582877ca53688e041fe184880fe75f16edf0945b24a319c710" dependencies = [ "alloy-primitives", "async-trait", @@ -757,9 +757,9 @@ dependencies = [ [[package]] name = "alloy-signer-local" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69c260e78b9c104c444f8a202f283d5e8c6637e6fa52a83f649ad6aaa0b91fd0" +checksum = "3c9d85b9f7105ab5ce7dae7b0da33cd9d977601a48f759e1c82958978dd1a905" dependencies = [ "alloy-consensus", "alloy-network", @@ -849,9 +849,9 @@ dependencies = [ [[package]] name = "alloy-transport" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f01c27edb3c0926919586a231d99e06284f9239da6044b5682033ef781e1cc62" +checksum = "4e72f5c4ba505ebead6a71144d72f21a70beadfb2d84e0a560a985491ecb71de" dependencies = [ "alloy-json-rpc", "auto_impl", @@ -872,9 +872,9 @@ dependencies = [ [[package]] name = "alloy-transport-http" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc57657fd3249fc8324cbbc8edbb7d5114af5fbc7c6c32dff944d6b5922f400" +checksum = "400dc298aaabdbd48be05448c4a19eaa38416c446043f3e54561249149269c32" dependencies = [ "alloy-json-rpc", "alloy-transport", @@ -887,9 +887,9 @@ dependencies = [ [[package]] name = "alloy-transport-ipc" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92a5a36d4ca1261a29dd1d791cd89c21b71d7465211910e43b0862d1c067a211" +checksum = "ba22ff961cf99495ee4fdbaf4623f8d5483d408ca2c6e1b1a54ef438ca87f8dd" dependencies = [ "alloy-json-rpc", "alloy-pubsub", @@ -907,9 +907,9 @@ dependencies = [ [[package]] name = "alloy-transport-ws" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e81effa6a2db6b2152eefb244b4aa6334b1c42819d0eca8d5a91826ec7a9fdba" +checksum = "c38b4472f2bbd96a27f393de9e2f12adca0dc1075fb4d0f7c8f3557c5c600392" dependencies = [ "alloy-pubsub", "alloy-transport", @@ -944,9 +944,9 @@ dependencies = [ [[package]] name = "alloy-tx-macros" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99dac443033e83b14f68fac56e8c27e76421f1253729574197ceccd06598f3ef" +checksum = "e2183706e24173309b0ab0e34d3e53cf3163b71a419803b2b3b0c1fb7ff7a941" dependencies = [ "darling 0.21.3", "proc-macro2", @@ -1662,6 +1662,7 @@ dependencies = [ "reth", "reth-db", "reth-db-common", + "reth-e2e-test-utils", "reth-evm", "reth-optimism-chainspec", "reth-optimism-evm", @@ -5323,15 +5324,6 @@ dependencies = [ "hashbrown 0.15.5", ] -[[package]] -name = "lru" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "227748d55f2f0ab4735d87fd623798cb6b664512fe979705f829c9f81c934465" -dependencies = [ - "hashbrown 0.15.5", -] - [[package]] name = "lru" version = "0.16.3" diff --git a/crates/client/metering/Cargo.toml b/crates/client/metering/Cargo.toml index 0c975fca..4bff481b 100644 --- a/crates/client/metering/Cargo.toml +++ b/crates/client/metering/Cargo.toml @@ -42,6 +42,7 @@ serde.workspace = true base-reth-test-utils.workspace = true reth-db = { workspace = true, features = ["test-utils"] } reth-db-common.workspace = true +reth-e2e-test-utils.workspace = true reth-optimism-node.workspace = true reth-testing-utils.workspace = true reth-transaction-pool = { workspace = true, features = ["test-utils"] } diff --git a/crates/client/metering/tests/meter.rs b/crates/client/metering/tests/meter.rs index 371be97a..976328f3 100644 --- a/crates/client/metering/tests/meter.rs +++ b/crates/client/metering/tests/meter.rs @@ -2,94 +2,48 @@ use std::sync::Arc; -use alloy_consensus::crypto::secp256k1::public_key_to_address; use alloy_eips::Encodable2718; -use alloy_genesis::GenesisAccount; -use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; +use alloy_primitives::{Address, B256, Bytes, U256, hex::FromHex, keccak256}; use base_bundles::{Bundle, ParsedBundle}; use base_reth_metering::meter_bundle; -use base_reth_test_utils::create_provider_factory; +use base_reth_test_utils::{TestAccounts, create_provider_factory, load_genesis}; use eyre::Context; use op_alloy_consensus::OpTxEnvelope; -use rand::{SeedableRng, rngs::StdRng}; use reth::{api::NodeTypesWithDBAdapter, chainspec::EthChainSpec}; use reth_db::{DatabaseEnv, test_utils::TempDatabase}; -use reth_optimism_chainspec::{BASE_MAINNET, OpChainSpec, OpChainSpecBuilder}; +use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder}; use reth_optimism_node::OpNode; use reth_optimism_primitives::OpTransactionSigned; use reth_primitives_traits::SealedHeader; use reth_provider::{HeaderProvider, StateProviderFactory, providers::BlockchainProvider}; -use reth_testing_utils::generators::generate_keys; use reth_transaction_pool::test_utils::TransactionBuilder; type NodeTypes = NodeTypesWithDBAdapter>>; -#[derive(Eq, PartialEq, Debug, Hash, Clone, Copy)] -enum User { - Alice, - Bob, -} - #[derive(Debug, Clone)] struct TestHarness { provider: BlockchainProvider, header: SealedHeader, chain_spec: Arc, - user_to_address: std::collections::HashMap, - user_to_private_key: std::collections::HashMap, + accounts: TestAccounts, } impl TestHarness { - fn address(&self, u: User) -> Address { - self.user_to_address[&u] + fn alice_signer(&self) -> B256 { + B256::from_hex(self.accounts.alice.private_key).expect("valid hex-encoded key") } - fn signer(&self, u: User) -> B256 { - self.user_to_private_key[&u] + fn bob_signer(&self) -> B256 { + B256::from_hex(self.accounts.bob.private_key).expect("valid hex-encoded key") } } -fn create_chain_spec( - seed: u64, -) -> ( - Arc, - std::collections::HashMap, - std::collections::HashMap, -) { - let keys = generate_keys(&mut StdRng::seed_from_u64(seed), 2); - - let mut addresses = std::collections::HashMap::new(); - let mut private_keys = std::collections::HashMap::new(); - - let alice_key = keys[0]; - let alice_address = public_key_to_address(alice_key.public_key()); - let alice_secret = B256::from(alice_key.secret_bytes()); - addresses.insert(User::Alice, alice_address); - private_keys.insert(User::Alice, alice_secret); - - let bob_key = keys[1]; - let bob_address = public_key_to_address(bob_key.public_key()); - let bob_secret = B256::from(bob_key.secret_bytes()); - addresses.insert(User::Bob, bob_address); - private_keys.insert(User::Bob, bob_secret); - - let genesis = BASE_MAINNET - .genesis - .clone() - .extend_accounts(vec![ - (alice_address, GenesisAccount::default().with_balance(U256::from(1_000_000_000_u64))), - (bob_address, GenesisAccount::default().with_balance(U256::from(1_000_000_000_u64))), - ]) - .with_gas_limit(100_000_000); - - let spec = +fn setup_harness() -> eyre::Result { + let accounts = TestAccounts::new(); + let genesis = load_genesis(); + let chain_spec = Arc::new(OpChainSpecBuilder::base_mainnet().genesis(genesis).isthmus_activated().build()); - (spec, addresses, private_keys) -} - -fn setup_harness() -> eyre::Result { - let (chain_spec, user_to_address, user_to_private_key) = create_chain_spec(1337); let factory = create_provider_factory::(chain_spec.clone()); reth_db_common::init::init_genesis(&factory).context("initializing genesis state")?; @@ -100,7 +54,7 @@ fn setup_harness() -> eyre::Result { .context("fetching genesis header")? .expect("genesis header exists"); - Ok(TestHarness { provider, header, chain_spec, user_to_address, user_to_private_key }) + Ok(TestHarness { provider, header, chain_spec, accounts }) } fn envelope_from_signed(tx: &OpTransactionSigned) -> eyre::Result { @@ -155,7 +109,7 @@ fn meter_bundle_single_transaction() -> eyre::Result<()> { let to = Address::random(); let signed_tx = TransactionBuilder::default() - .signer(harness.signer(User::Alice)) + .signer(harness.alice_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(to) @@ -185,7 +139,7 @@ fn meter_bundle_single_transaction() -> eyre::Result<()> { let result = &results[0]; assert!(total_execution_time > 0); - assert_eq!(result.from_address, harness.address(User::Alice)); + assert_eq!(result.from_address, harness.accounts.alice.address); assert_eq!(result.to_address, Some(to)); assert_eq!(result.tx_hash, tx_hash); assert_eq!(result.gas_price, U256::from(10)); @@ -213,7 +167,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> { // Create first transaction let signed_tx_1 = TransactionBuilder::default() - .signer(harness.signer(User::Alice)) + .signer(harness.alice_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(to_1) @@ -229,7 +183,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> { // Create second transaction let signed_tx_2 = TransactionBuilder::default() - .signer(harness.signer(User::Bob)) + .signer(harness.bob_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(to_2) @@ -263,7 +217,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> { // Check first transaction let result_1 = &results[0]; - assert_eq!(result_1.from_address, harness.address(User::Alice)); + assert_eq!(result_1.from_address, harness.accounts.alice.address); assert_eq!(result_1.to_address, Some(to_1)); assert_eq!(result_1.tx_hash, tx_hash_1); assert_eq!(result_1.gas_price, U256::from(10)); @@ -272,7 +226,7 @@ fn meter_bundle_multiple_transactions() -> eyre::Result<()> { // Check second transaction let result_2 = &results[1]; - assert_eq!(result_2.from_address, harness.address(User::Bob)); + assert_eq!(result_2.from_address, harness.accounts.bob.address); assert_eq!(result_2.to_address, Some(to_2)); assert_eq!(result_2.tx_hash, tx_hash_2); assert_eq!(result_2.gas_price, U256::from(15)); diff --git a/crates/client/metering/tests/meter_block.rs b/crates/client/metering/tests/meter_block.rs index 42af07b3..1df3e1ff 100644 --- a/crates/client/metering/tests/meter_block.rs +++ b/crates/client/metering/tests/meter_block.rs @@ -2,31 +2,22 @@ use std::sync::Arc; -use alloy_consensus::{BlockHeader, Header, crypto::secp256k1::public_key_to_address}; -use alloy_genesis::GenesisAccount; -use alloy_primitives::{Address, B256, U256}; +use alloy_consensus::{BlockHeader, Header}; +use alloy_primitives::{Address, B256, hex::FromHex}; use base_reth_metering::meter_block; -use base_reth_test_utils::create_provider_factory; +use base_reth_test_utils::{TestAccounts, create_provider_factory, load_genesis}; use eyre::Context; -use rand::{SeedableRng, rngs::StdRng}; use reth::{api::NodeTypesWithDBAdapter, chainspec::EthChainSpec}; use reth_db::{DatabaseEnv, test_utils::TempDatabase}; -use reth_optimism_chainspec::{BASE_MAINNET, OpChainSpec, OpChainSpecBuilder}; +use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder}; use reth_optimism_node::OpNode; use reth_optimism_primitives::{OpBlock, OpBlockBody, OpTransactionSigned}; use reth_primitives_traits::Block as BlockT; use reth_provider::{HeaderProvider, providers::BlockchainProvider}; -use reth_testing_utils::generators::generate_keys; use reth_transaction_pool::test_utils::TransactionBuilder; type NodeTypes = NodeTypesWithDBAdapter>>; -#[derive(Eq, PartialEq, Debug, Hash, Clone, Copy)] -enum User { - Alice, - Bob, -} - #[derive(Debug, Clone)] struct TestHarness { provider: BlockchainProvider, @@ -34,47 +25,25 @@ struct TestHarness { genesis_header_number: u64, genesis_header_timestamp: u64, chain_spec: Arc, - user_to_private_key: std::collections::HashMap, + accounts: TestAccounts, } impl TestHarness { - fn signer(&self, u: User) -> B256 { - self.user_to_private_key[&u] + fn alice_signer(&self) -> B256 { + B256::from_hex(self.accounts.alice.private_key).expect("valid hex-encoded key") } -} - -fn create_chain_spec(seed: u64) -> (Arc, std::collections::HashMap) { - let keys = generate_keys(&mut StdRng::seed_from_u64(seed), 2); - - let mut private_keys = std::collections::HashMap::new(); - - let alice_key = keys[0]; - let alice_address = public_key_to_address(alice_key.public_key()); - let alice_secret = B256::from(alice_key.secret_bytes()); - private_keys.insert(User::Alice, alice_secret); - - let bob_key = keys[1]; - let bob_address = public_key_to_address(bob_key.public_key()); - let bob_secret = B256::from(bob_key.secret_bytes()); - private_keys.insert(User::Bob, bob_secret); - let genesis = BASE_MAINNET - .genesis - .clone() - .extend_accounts(vec![ - (alice_address, GenesisAccount::default().with_balance(U256::from(1_000_000_000_u64))), - (bob_address, GenesisAccount::default().with_balance(U256::from(1_000_000_000_u64))), - ]) - .with_gas_limit(100_000_000); - - let spec = - Arc::new(OpChainSpecBuilder::base_mainnet().genesis(genesis).isthmus_activated().build()); - - (spec, private_keys) + fn bob_signer(&self) -> B256 { + B256::from_hex(self.accounts.bob.private_key).expect("valid hex-encoded key") + } } fn setup_harness() -> eyre::Result { - let (chain_spec, user_to_private_key) = create_chain_spec(1337); + let accounts = TestAccounts::new(); + let genesis = load_genesis(); + let chain_spec = + Arc::new(OpChainSpecBuilder::base_mainnet().genesis(genesis).isthmus_activated().build()); + let factory = create_provider_factory::(chain_spec.clone()); reth_db_common::init::init_genesis(&factory).context("initializing genesis state")?; @@ -91,7 +60,7 @@ fn setup_harness() -> eyre::Result { genesis_header_number: header.number(), genesis_header_timestamp: header.timestamp(), chain_spec, - user_to_private_key, + accounts, }) } @@ -144,7 +113,7 @@ fn meter_block_single_transaction() -> eyre::Result<()> { let to = Address::random(); let signed_tx = TransactionBuilder::default() - .signer(harness.signer(User::Alice)) + .signer(harness.alice_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(to) @@ -191,7 +160,7 @@ fn meter_block_multiple_transactions() -> eyre::Result<()> { // Create first transaction from Alice let signed_tx_1 = TransactionBuilder::default() - .signer(harness.signer(User::Alice)) + .signer(harness.alice_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(to_1) @@ -208,7 +177,7 @@ fn meter_block_multiple_transactions() -> eyre::Result<()> { // Create second transaction from Bob let signed_tx_2 = TransactionBuilder::default() - .signer(harness.signer(User::Bob)) + .signer(harness.bob_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(to_2) @@ -268,7 +237,7 @@ fn meter_block_timing_consistency() -> eyre::Result<()> { // Create a block with one transaction let signed_tx = TransactionBuilder::default() - .signer(harness.signer(User::Alice)) + .signer(harness.alice_signer()) .chain_id(harness.chain_spec.chain_id()) .nonce(0) .to(Address::random()) diff --git a/crates/client/metering/tests/meter_rpc.rs b/crates/client/metering/tests/meter_rpc.rs index 5ffe598c..5e2b9beb 100644 --- a/crates/client/metering/tests/meter_rpc.rs +++ b/crates/client/metering/tests/meter_rpc.rs @@ -1,33 +1,18 @@ //! Integration tests covering the Metering RPC surface area. -use std::{any::Any, net::SocketAddr, sync::Arc}; - use alloy_eips::Encodable2718; -use alloy_primitives::{Bytes, U256, address, b256, bytes}; +use alloy_primitives::{B256, Bytes, U256, bytes, hex::FromHex}; use alloy_rpc_client::RpcClient; use base_bundles::{Bundle, MeterBundleResponse}; use base_reth_metering::{MeteringApiImpl, MeteringApiServer}; -use base_reth_test_utils::{init_silenced_tracing, load_genesis}; +use base_reth_test_utils::{BASE_CHAIN_ID, OpAddOns, OpBuilder, TestHarness}; use op_alloy_consensus::OpTxEnvelope; -use reth::{ - args::{DiscoveryArgs, NetworkArgs, RpcServerArgs}, - builder::{Node, NodeBuilder, NodeConfig, NodeHandle}, - chainspec::Chain, - core::exit::NodeExitFuture, - tasks::TaskManager, -}; -use reth_optimism_chainspec::OpChainSpecBuilder; -use reth_optimism_node::{OpNode, args::RollupArgs}; +use reth::builder::NodeHandle; +use reth_e2e_test_utils::Adapter; +use reth_optimism_node::OpNode; use reth_optimism_primitives::OpTransactionSigned; -use reth_provider::providers::BlockchainProvider; use reth_transaction_pool::test_utils::TransactionBuilder; -struct NodeContext { - http_api_addr: SocketAddr, - _node_exit_future: NodeExitFuture, - _node: Box, -} - // Helper function to create a Bundle with default fields fn create_bundle(txs: Vec, block_number: u64, min_timestamp: Option) -> Bundle { Bundle { @@ -43,66 +28,29 @@ fn create_bundle(txs: Vec, block_number: u64, min_timestamp: Option) } } -impl NodeContext { - async fn rpc_client(&self) -> eyre::Result { - let url = format!("http://{}", self.http_api_addr); - let client = RpcClient::new_http(url.parse()?); - Ok(client) - } -} - -async fn setup_node() -> eyre::Result { - init_silenced_tracing(); - let tasks = TaskManager::current(); - let exec = tasks.executor(); - const BASE_SEPOLIA_CHAIN_ID: u64 = 84532; - - let genesis = load_genesis(); - let chain_spec = Arc::new( - OpChainSpecBuilder::base_mainnet() - .genesis(genesis) - .ecotone_activated() - .chain(Chain::from(BASE_SEPOLIA_CHAIN_ID)) - .build(), - ); - - let network_config = NetworkArgs { - discovery: DiscoveryArgs { disable_discovery: true, ..DiscoveryArgs::default() }, - ..NetworkArgs::default() - }; - - let node_config = NodeConfig::new(chain_spec.clone()) - .with_network(network_config.clone()) - .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()) - .with_unused_ports(); - - let node = OpNode::new(RollupArgs::default()); - - let NodeHandle { node, node_exit_future } = NodeBuilder::new(node_config.clone()) - .testing_node(exec.clone()) - .with_types_and_provider::>() - .with_components(node.components_builder()) - .with_add_ons(node.add_ons()) +/// Custom launcher that adds the MeteringApiImpl RPC extension. +async fn metering_launcher( + builder: OpBuilder, +) -> eyre::Result, OpAddOns>> { + let launcher = builder.engine_api_launcher(); + builder .extend_rpc_modules(move |ctx| { let metering_api = MeteringApiImpl::new(ctx.provider().clone()); ctx.modules.merge_configured(metering_api.into_rpc())?; Ok(()) }) - .launch() - .await?; - - let http_api_addr = node - .rpc_server_handle() - .http_local_addr() - .ok_or_else(|| eyre::eyre!("Failed to get http api address"))?; + .launch_with(launcher) + .await +} - Ok(NodeContext { http_api_addr, _node_exit_future: node_exit_future, _node: Box::new(node) }) +async fn setup_harness() -> eyre::Result { + TestHarness::with_launcher(metering_launcher).await } #[tokio::test] async fn test_meter_bundle_empty() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); let bundle = create_bundle(vec![], 0, None); @@ -118,21 +66,18 @@ async fn test_meter_bundle_empty() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_single_transaction() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); + let accounts = harness.accounts(); - // Use a funded account from genesis.json - // Account: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 - // Private key from common test accounts (Hardhat account #0) - let sender_address = address!("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"); - let sender_secret = b256!("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"); + let alice_signer = B256::from_hex(accounts.alice.private_key)?; - // Build a transaction + // Build a transaction using Alice's account let tx = TransactionBuilder::default() - .signer(sender_secret) - .chain_id(84532) + .signer(alice_signer) + .chain_id(BASE_CHAIN_ID) .nonce(0) - .to(address!("0x1111111111111111111111111111111111111111")) + .to(alloy_primitives::address!("0x1111111111111111111111111111111111111111")) .value(1000) .gas_limit(21_000) .max_fee_per_gas(1_000_000_000) // 1 gwei @@ -155,8 +100,11 @@ async fn test_meter_bundle_single_transaction() -> eyre::Result<()> { assert!(response.total_execution_time_us > 0); let result = &response.results[0]; - assert_eq!(result.from_address, sender_address); - assert_eq!(result.to_address, Some(address!("0x1111111111111111111111111111111111111111"))); + assert_eq!(result.from_address, accounts.alice.address); + assert_eq!( + result.to_address, + Some(alloy_primitives::address!("0x1111111111111111111111111111111111111111")) + ); assert_eq!(result.gas_used, 21_000); assert_eq!(result.gas_price, 1_000_000_000); assert!(result.execution_time_us > 0); @@ -166,19 +114,19 @@ async fn test_meter_bundle_single_transaction() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_multiple_transactions() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); + let accounts = harness.accounts(); - // Use funded accounts from genesis.json - // Hardhat account #0 and #1 - let address1 = address!("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"); - let secret1 = b256!("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"); + let alice_signer = B256::from_hex(accounts.alice.private_key)?; + let bob_signer = B256::from_hex(accounts.bob.private_key)?; + // First transaction from Alice let tx1_inner = TransactionBuilder::default() - .signer(secret1) - .chain_id(84532) + .signer(alice_signer) + .chain_id(BASE_CHAIN_ID) .nonce(0) - .to(address!("0x1111111111111111111111111111111111111111")) + .to(alloy_primitives::address!("0x1111111111111111111111111111111111111111")) .value(1000) .gas_limit(21_000) .max_fee_per_gas(1_000_000_000) @@ -190,15 +138,12 @@ async fn test_meter_bundle_multiple_transactions() -> eyre::Result<()> { let tx1_envelope: OpTxEnvelope = tx1_signed.into(); let tx1_bytes = Bytes::from(tx1_envelope.encoded_2718()); - // Second transaction from second account - let address2 = address!("0x70997970C51812dc3A010C7d01b50e0d17dc79C8"); - let secret2 = b256!("0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"); - + // Second transaction from Bob let tx2_inner = TransactionBuilder::default() - .signer(secret2) - .chain_id(84532) + .signer(bob_signer) + .chain_id(BASE_CHAIN_ID) .nonce(0) - .to(address!("0x2222222222222222222222222222222222222222")) + .to(alloy_primitives::address!("0x2222222222222222222222222222222222222222")) .value(2000) .gas_limit(21_000) .max_fee_per_gas(2_000_000_000) @@ -220,13 +165,13 @@ async fn test_meter_bundle_multiple_transactions() -> eyre::Result<()> { // Check first transaction let result1 = &response.results[0]; - assert_eq!(result1.from_address, address1); + assert_eq!(result1.from_address, accounts.alice.address); assert_eq!(result1.gas_used, 21_000); assert_eq!(result1.gas_price, 1_000_000_000); // Check second transaction let result2 = &response.results[1]; - assert_eq!(result2.from_address, address2); + assert_eq!(result2.from_address, accounts.bob.address); assert_eq!(result2.gas_used, 21_000); assert_eq!(result2.gas_price, 2_000_000_000); @@ -235,8 +180,8 @@ async fn test_meter_bundle_multiple_transactions() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_invalid_transaction() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); let bundle = create_bundle( vec![bytes!("0xdeadbeef")], // Invalid transaction data @@ -254,8 +199,8 @@ async fn test_meter_bundle_invalid_transaction() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_uses_latest_block() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); // Metering always uses the latest block state, regardless of bundle.block_number let bundle = create_bundle(vec![], 0, None); @@ -270,8 +215,8 @@ async fn test_meter_bundle_uses_latest_block() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_ignores_bundle_block_number() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); // Even if bundle.block_number is different, it should use the latest block // In this test, we specify block_number=0 in the bundle @@ -293,8 +238,8 @@ async fn test_meter_bundle_ignores_bundle_block_number() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_custom_timestamp() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); // Test that bundle.min_timestamp is used for simulation. // The timestamp affects block.timestamp in the EVM during simulation but is not @@ -313,8 +258,8 @@ async fn test_meter_bundle_custom_timestamp() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_arbitrary_block_number() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); // Since we now ignore bundle.block_number and always use the latest block, // any block_number value should work (it's only used for bundle validity in TIPS) @@ -330,19 +275,19 @@ async fn test_meter_bundle_arbitrary_block_number() -> eyre::Result<()> { #[tokio::test] async fn test_meter_bundle_gas_calculations() -> eyre::Result<()> { - let node = setup_node().await?; - let client = node.rpc_client().await?; + let harness = setup_harness().await?; + let client = RpcClient::new_http(harness.rpc_url().parse()?); + let accounts = harness.accounts(); - // Use two funded accounts from genesis.json with different gas prices - let secret1 = b256!("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"); - let secret2 = b256!("0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"); + let alice_signer = B256::from_hex(accounts.alice.private_key)?; + let bob_signer = B256::from_hex(accounts.bob.private_key)?; - // First transaction with 3 gwei gas price + // First transaction with 3 gwei gas price from Alice let tx1_inner = TransactionBuilder::default() - .signer(secret1) - .chain_id(84532) + .signer(alice_signer) + .chain_id(BASE_CHAIN_ID) .nonce(0) - .to(address!("0x1111111111111111111111111111111111111111")) + .to(alloy_primitives::address!("0x1111111111111111111111111111111111111111")) .value(1000) .gas_limit(21_000) .max_fee_per_gas(3_000_000_000) // 3 gwei @@ -354,12 +299,12 @@ async fn test_meter_bundle_gas_calculations() -> eyre::Result<()> { let envelope1: OpTxEnvelope = signed_tx1.into(); let tx1_bytes = Bytes::from(envelope1.encoded_2718()); - // Second transaction with 7 gwei gas price + // Second transaction with 7 gwei gas price from Bob let tx2_inner = TransactionBuilder::default() - .signer(secret2) - .chain_id(84532) + .signer(bob_signer) + .chain_id(BASE_CHAIN_ID) .nonce(0) - .to(address!("0x2222222222222222222222222222222222222222")) + .to(alloy_primitives::address!("0x2222222222222222222222222222222222222222")) .value(2000) .gas_limit(21_000) .max_fee_per_gas(7_000_000_000) // 7 gwei