From 68d2857916aeffb81292c95a2a2bc3f40ee67407 Mon Sep 17 00:00:00 2001 From: anikaraghu Date: Wed, 15 Nov 2023 19:06:07 -0800 Subject: [PATCH] Feature flags and add one test --- Cargo.lock | 4 ++ crates/anvil/Cargo.toml | 2 +- crates/anvil/core/Cargo.toml | 10 ++-- crates/anvil/core/src/eth/transaction/mod.rs | 53 ++++++++++++++------ crates/evm/core/src/utils.rs | 1 + 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6cd3b0ffdc22a..f6813612c6d5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -327,8 +327,12 @@ name = "anvil-core" version = "0.2.0" dependencies = [ "alloy-primitives", + "anvil-core", "bytes", + "ethers-contract", "ethers-core", + "ethers-middleware", + "ethers-providers", "foundry-evm", "foundry-utils", "hash-db", diff --git a/crates/anvil/Cargo.toml b/crates/anvil/Cargo.toml index 8d63d7b795af9..97afd724833c6 100644 --- a/crates/anvil/Cargo.toml +++ b/crates/anvil/Cargo.toml @@ -20,7 +20,7 @@ vergen = { version = "8", default-features = false, features = ["build", "git", [dependencies] # foundry internal -anvil-core = { path = "core", features = ["fastrlp", "serde", "impersonated-tx"] } +anvil-core = { path = "core", features = ["fastrlp", "serde", "impersonated-tx", "optimism"] } anvil-rpc = { path = "rpc" } anvil-server = { path = "server" } foundry-common.workspace = true diff --git a/crates/anvil/core/Cargo.toml b/crates/anvil/core/Cargo.toml index 59b54b81f663f..d059033dce08d 100644 --- a/crates/anvil/core/Cargo.toml +++ b/crates/anvil/core/Cargo.toml @@ -16,7 +16,10 @@ foundry-utils.workspace = true revm = { workspace = true, default-features = false, features = ["std", "serde", "memory_limit"] } alloy-primitives = { workspace = true, features = ["serde"] } -ethers-core.workspace = true +ethers-core = { workspace = true, features = ["optimism"], optional = true } +ethers-contract = { workspace = true, features = ["optimism"], optional = true } +ethers-providers = { workspace = true, features = ["optimism"], optional = true } +ethers-middleware = { workspace = true, features = ["optimism"], optional = true } serde = { workspace = true, optional = true } serde_json.workspace = true bytes = { version = "1.4" } @@ -30,10 +33,11 @@ reference-trie = { version = "0.25" } keccak-hasher = { version = "0.15" } [dev-dependencies] -serde.workspace = true +anvil-core = { path = ".", features = ["serde"] } [features] -default = [] +default = ["serde","optimism"] impersonated-tx = [] fastrlp = ["dep:open-fastrlp"] serde = ["dep:serde"] +optimism = ["dep:ethers-core", "dep:ethers-contract", "dep:ethers-providers", "dep:ethers-middleware"] diff --git a/crates/anvil/core/src/eth/transaction/mod.rs b/crates/anvil/core/src/eth/transaction/mod.rs index c012af7c06a50..f92edeca4b45c 100644 --- a/crates/anvil/core/src/eth/transaction/mod.rs +++ b/crates/anvil/core/src/eth/transaction/mod.rs @@ -444,8 +444,8 @@ impl Encodable for EIP1559TransactionRequest { #[derive(Debug, Clone, PartialEq, Eq)] pub struct DepositTransactionRequest { - pub source_hash: H256, pub from: Address, + pub source_hash: H256, pub kind: TransactionKind, pub mint: U256, pub value: U256, @@ -465,8 +465,8 @@ impl DepositTransactionRequest { impl From for DepositTransactionRequest { fn from(tx: DepositTransaction) -> Self { Self { - source_hash: tx.source_hash, from: tx.from, + source_hash: tx.source_hash, kind: tx.kind, mint: tx.mint, value: tx.value, @@ -480,8 +480,8 @@ impl From for DepositTransactionRequest { impl Encodable for DepositTransactionRequest { fn rlp_append(&self, s: &mut RlpStream) { s.begin_list(8); - s.append(&self.source_hash); s.append(&self.from); + s.append(&self.source_hash); s.append(&self.kind); s.append(&self.mint); s.append(&self.value); @@ -1281,7 +1281,8 @@ impl DepositTransaction { impl Encodable for DepositTransaction { fn rlp_append(&self, s: &mut RlpStream) { - s.begin_list(8); + s.begin_list(9); + s.append(&self.nonce); s.append(&self.source_hash); s.append(&self.from); s.append(&self.kind); @@ -1295,20 +1296,20 @@ impl Encodable for DepositTransaction { impl Decodable for DepositTransaction { fn decode(rlp: &Rlp) -> Result { - if rlp.item_count()? != 8 { + if rlp.item_count()? != 9 { return Err(DecoderError::RlpIncorrectListLen) } Ok(Self { - source_hash: rlp.val_at(0)?, - from: rlp.val_at(1)?, - kind: rlp.val_at(2)?, - mint: rlp.val_at(3)?, - value: rlp.val_at(4)?, - gas_limit: rlp.val_at(5)?, - is_system_tx: rlp.val_at(6)?, - input: rlp.val_at::>(7)?.into(), - nonce: U256::from(0), + nonce: rlp.val_at(0)?, + source_hash: rlp.val_at(1)?, + from: rlp.val_at(2)?, + kind: rlp.val_at(3)?, + mint: rlp.val_at(4)?, + value: rlp.val_at(5)?, + gas_limit: rlp.val_at(6)?, + is_system_tx: rlp.val_at(7)?, + input: rlp.val_at::>(8)?.into(), }) } } @@ -1535,7 +1536,7 @@ impl TransactionInfo { #[cfg(test)] mod tests { use super::*; - use ethers_core::utils::hex; + use ethers_core::{types::H160, utils::hex}; #[test] fn can_recover_sender() { @@ -1781,6 +1782,28 @@ mod tests { expected, ::decode(bytes_fifth).unwrap() ); + + let bytes_sixth = &mut &hex::decode("b8587ef85507a0000000000000000000000000000000000000000000000000000000000000000094cf7f9e66af820a19257a2108375b180b0ec491679461815774383099e24810ab832a5b2a5425c154d5808230398287fb0180").unwrap()[..]; + let expected: TypedTransaction = TypedTransaction::Deposit(DepositTransaction { + nonce: 7u64.into(), + source_hash: H256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(), + from: H160::from_str("cf7f9e66af820a19257a2108375b180b0ec49167").unwrap(), + kind: TransactionKind::Call(Address::from_slice( + &hex::decode("61815774383099e24810ab832a5b2a5425c154d5").unwrap()[..], + )), + mint: U256::zero(), + value: 12345u64.into(), + gas_limit: 34811u64.into(), + input: Bytes::default(), + is_system_tx: true, + }); + let rlpbytes = expected.rlp_bytes(); + let bytes = rlpbytes.as_ref(); + println!("{}", hex::encode(bytes)); + assert_eq!( + expected, + ::decode(bytes_sixth).unwrap() + ); } // diff --git a/crates/evm/core/src/utils.rs b/crates/evm/core/src/utils.rs index f4196ac589c50..8ce3b7cc75d4b 100644 --- a/crates/evm/core/src/utils.rs +++ b/crates/evm/core/src/utils.rs @@ -132,6 +132,7 @@ pub fn halt_to_instruction_result(halt: Halt) -> InstructionResult { Halt::CallNotAllowedInsideStatic => InstructionResult::CallNotAllowedInsideStatic, Halt::OutOfFund => InstructionResult::OutOfFund, Halt::CallTooDeep => InstructionResult::CallTooDeep, + Halt::FailedDeposit => todo!(), } }