diff --git a/Cargo.lock b/Cargo.lock index 8547c117..58f5034e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,6 +135,22 @@ dependencies = [ "memchr", ] +[[package]] +name = "alloy-primitives" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c715249705afa1e32be79dabfd35e2ef0f1cc02ad2cf48c9d1e20026ee637b" +dependencies = [ + "bytes", + "cfg-if", + "const-hex", + "derive_more", + "hex-literal", + "itoa", + "ruint", + "tiny-keccak", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -788,6 +804,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "core-foundation" version = "0.9.4" @@ -1070,8 +1092,10 @@ version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ + "convert_case", "proc-macro2", "quote", + "rustc_version", "syn 1.0.109", ] @@ -2310,6 +2334,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hex_fmt" version = "0.3.0" @@ -3329,6 +3359,7 @@ name = "magi" version = "0.1.0" dependencies = [ "again", + "alloy-primitives", "ansi_term", "async-trait", "bytes", @@ -4780,6 +4811,26 @@ dependencies = [ "tokio", ] +[[package]] +name = "ruint" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f308135fef9fc398342da5472ce7c484529df23743fb7c734e0f3d472971e62" +dependencies = [ + "proptest", + "rand 0.8.5", + "ruint-macro", + "serde", + "valuable", + "zeroize", +] + +[[package]] +name = "ruint-macro" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f86854cf50259291520509879a5c294c3c9a4c334e9ff65071c51e42ef1e2343" + [[package]] name = "rustc-demangle" version = "0.1.23" diff --git a/Cargo.toml b/Cargo.toml index 9969db44..7a607b6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,9 @@ futures = "0.3.28" futures-timer = "0.3.0" again = "0.1" +# Alloy types +alloy-primitives = { version = "0.7.1", default-features = false } + # Logging and Metrics chrono = "0.4.22" tracing = "0.1.36" diff --git a/src/common/attributes_deposited.rs b/src/common/attributes_deposited.rs index 726dc125..f0d13b12 100644 --- a/src/common/attributes_deposited.rs +++ b/src/common/attributes_deposited.rs @@ -1,10 +1,8 @@ -use ethers::{ - types::{Bytes, H256, U256}, - utils::keccak256, -}; use eyre::Result; use lazy_static::lazy_static; +use alloy_primitives::{keccak256, Bytes, B256, U256}; + /// Represents the attributes deposited transcation call #[derive(Debug)] pub struct AttributesDepositedCall { @@ -15,11 +13,11 @@ pub struct AttributesDepositedCall { /// base fee pub basefee: U256, /// block hash - pub hash: H256, + pub hash: B256, /// sequence number of the L2 block pub sequence_number: u64, /// batcher hash (should contain an address) - pub batcher_hash: H256, + pub batcher_hash: B256, /// L1 fee overhead pub fee_overhead: U256, /// L1 fee scalar @@ -78,38 +76,47 @@ impl AttributesDepositedCall { } cursor += 4; - let number = U256::from_big_endian(calldata[cursor..cursor + 32].try_into()?); - let number = number.as_u64(); // down-casting to u64 is safe for the block number + let number = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?); + // down-casting to u64 is safe for the block number + let number = number + .try_into() + .map_err(|_| eyre::eyre!("invalid block number"))?; cursor += 32; - let timestamp = U256::from_big_endian(calldata[cursor..cursor + 32].try_into()?); - let timestamp = timestamp.as_u64(); // down-casting to u64 is safe for UNIX timestamp + let timestamp = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?); + // down-casting to u64 is safe for UNIX timestamp + let timestamp = timestamp + .try_into() + .map_err(|_| eyre::eyre!("invalid timestamp"))?; cursor += 32; - let basefee = U256::from_big_endian(&calldata[cursor..cursor + 32]); + let basefee = U256::from_be_slice(&calldata[cursor..cursor + 32]); cursor += 32; - let hash = H256::from_slice(&calldata[cursor..cursor + 32]); + let hash = B256::from_slice(&calldata[cursor..cursor + 32]); cursor += 32; - let sequence_number = U256::from_big_endian(calldata[cursor..cursor + 32].try_into()?); - let sequence_number = sequence_number.as_u64(); // down-casting to u64 is safe for the sequence number + let seq_num = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?); + // down-casting to u64 is safe for the sequence number + let seq_num = seq_num + .try_into() + .map_err(|_| eyre::eyre!("invalid sequence number"))?; cursor += 32; - let batcher_hash = H256::from_slice(&calldata[cursor..cursor + 32]); + let batcher_hash = B256::from_slice(&calldata[cursor..cursor + 32]); cursor += 32; - let fee_overhead = U256::from_big_endian(&calldata[cursor..cursor + 32]); + let fee_overhead = U256::from_be_slice(&calldata[cursor..cursor + 32]); cursor += 32; - let fee_scalar = U256::from_big_endian(&calldata[cursor..cursor + 32]); + let fee_scalar = U256::from_be_slice(&calldata[cursor..cursor + 32]); Ok(Self { number, timestamp, basefee, hash, - sequence_number, + sequence_number: seq_num, batcher_hash, fee_overhead, fee_scalar, @@ -167,16 +174,16 @@ impl AttributesDepositedCall { let number = u64::from_be_bytes(calldata[cursor..cursor + 8].try_into()?); cursor += 8; - let basefee = U256::from_big_endian(&calldata[cursor..cursor + 32]); + let basefee = U256::from_be_slice(&calldata[cursor..cursor + 32]); cursor += 32; - let blob_base_fee = Some(U256::from_big_endian(&calldata[cursor..cursor + 32])); + let blob_base_fee = Some(U256::from_be_slice(&calldata[cursor..cursor + 32])); cursor += 32; - let hash = H256::from_slice(&calldata[cursor..cursor + 32]); + let hash = B256::from_slice(&calldata[cursor..cursor + 32]); cursor += 32; - let batcher_hash = H256::from_slice(&calldata[cursor..cursor + 32]); + let batcher_hash = B256::from_slice(&calldata[cursor..cursor + 32]); Ok(Self { number, @@ -190,7 +197,7 @@ impl AttributesDepositedCall { blob_base_fee_scalar, // The pre-Ecotone L1 fee overhead value is dropped in Ecotone - fee_overhead: U256::zero(), + fee_overhead: U256::ZERO, }) } } @@ -200,7 +207,7 @@ mod tests { mod attributed_deposited_call { use std::str::FromStr; - use ethers::types::{Bytes, H256, U256}; + use alloy_primitives::{Bytes, B256, U256}; use crate::common::AttributesDepositedCall; @@ -210,7 +217,7 @@ mod tests { let calldata = "0x015d8eb900000000000000000000000000000000000000000000000000000000008768240000000000000000000000000000000000000000000000000000000064443450000000000000000000000000000000000000000000000000000000000000000e0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d00000000000000000000000000000000000000000000000000000000000000050000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240"; let expected_hash = - H256::from_str("0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d")?; + B256::from_str("0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d")?; let expected_block_number = 8874020; let expected_timestamp = 1682191440; @@ -234,7 +241,7 @@ mod tests { // https://goerli-optimism.etherscan.io/tx/0xc2288c5d1f6123406bfe8662bdbc1a3c999394da2e6f444f5aa8df78136f36ba let calldata = "0x440a5e2000001db0000d273000000000000000050000000065c8ad6c0000000000a085a20000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000041dfd80f2c8af7d7ba1c1a3962026e5c96b9105d528f8fed65c56cfa731a8751c7f712eb70000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9"; - let expected_hash = H256::from_str( + let expected_hash = B256::from_str( "0xc8af7d7ba1c1a3962026e5c96b9105d528f8fed65c56cfa731a8751c7f712eb7", ); let expected_block_number = 10519970; diff --git a/src/common/mod.rs b/src/common/mod.rs index 04f67682..59d23489 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -105,7 +105,7 @@ impl From<&AttributesDepositedCall> for Epoch { Self { number: call.number, timestamp: call.timestamp, - hash: call.hash, + hash: H256::from_slice(call.hash.as_slice()), } } } diff --git a/src/driver/types.rs b/src/driver/types.rs index 27dfead3..ca9288fc 100644 --- a/src/driver/types.rs +++ b/src/driver/types.rs @@ -42,8 +42,8 @@ impl HeadInfo { )); }; - let tx_calldata = first_tx.input.clone(); - let call = AttributesDepositedCall::try_from_bedrock(tx_calldata)?; + let tx_calldata = first_tx.input.to_vec(); + let call = AttributesDepositedCall::try_from_bedrock(tx_calldata.into())?; Ok(Self { l2_block_info: BlockInfo::try_from(block)?, @@ -61,8 +61,8 @@ impl HeadInfo { )); }; - let tx_calldata = first_tx.input.clone(); - let call = AttributesDepositedCall::try_from_ecotone(tx_calldata)?; + let tx_calldata = first_tx.input.to_vec(); + let call = AttributesDepositedCall::try_from_ecotone(tx_calldata.into())?; Ok(Self { l2_block_info: BlockInfo::try_from(block)?,