From 0e7d7792b85a9ea1312ce1e5e8db41acf83bcf0c Mon Sep 17 00:00:00 2001 From: john xu Date: Thu, 19 Oct 2023 12:28:10 +0000 Subject: [PATCH] feat: anchor check in zeth --- Cargo.lock | 1 + crates/primitives/Cargo.toml | 2 +- crates/primitives/src/env.rs | 17 ----------------- crates/primitives/src/lib.rs | 4 ---- crates/primitives/src/taiko.rs | 5 ----- crates/primitives/src/taiko/anchor.rs | 27 --------------------------- crates/primitives/src/taiko/env.rs | 15 --------------- crates/revm/Cargo.toml | 3 ++- crates/revm/src/evm_impl.rs | 3 --- crates/revm/src/handler/taiko.rs | 11 +++++++++-- 10 files changed, 13 insertions(+), 75 deletions(-) delete mode 100644 crates/primitives/src/taiko.rs delete mode 100644 crates/primitives/src/taiko/anchor.rs delete mode 100644 crates/primitives/src/taiko/env.rs diff --git a/Cargo.lock b/Cargo.lock index 6d8a6bb64b..0b0138a65d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2362,6 +2362,7 @@ dependencies = [ "ethers-core", "ethers-providers", "futures", + "once_cell", "revm-interpreter", "revm-precompile", "serde", diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 1b27147d56..4e4b700be7 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -47,7 +47,7 @@ serde = [ arbitrary = ["std", "alloy-primitives/arbitrary", "bitflags/arbitrary"] optimism = [] -taiko = ["dep:once_cell"] +taiko = [] dev = [ "memory_limit", diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index 11634bc4fd..853b289191 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -3,8 +3,6 @@ use crate::{ InvalidTransaction, Spec, SpecId, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, U256, VERSIONED_HASH_VERSION_KZG, }; -#[cfg(feature = "taiko")] -use crate::{EVMError, TaikoEnv, TxType}; use core::cmp::{min, Ordering}; #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -13,20 +11,10 @@ pub struct Env { pub cfg: CfgEnv, pub block: BlockEnv, pub tx: TxEnv, - #[cfg(feature = "taiko")] - /// Configuration of the taiko - pub taiko: TaikoEnv, } #[cfg(feature = "taiko")] impl Env { - pub fn pre_check(&self) -> Result<(), EVMError> { - if !crate::anchor::validate(self) { - return Err(InvalidTransaction::InvalidAnchorTransaction.into()); - } - Ok(()) - } - pub fn is_anchor(&self) -> bool { self.tx.index == 0 } @@ -151,9 +139,6 @@ pub struct TxEnv { /// The index of the transaction in the block. #[cfg(feature = "taiko")] pub index: usize, - /// The type of the transaction. - #[cfg(feature = "taiko")] - pub tx_type: TxType, /// Caller aka Author aka transaction signer. pub caller: Address, @@ -456,8 +441,6 @@ impl Default for TxEnv { Self { #[cfg(feature = "taiko")] index: 0, - #[cfg(feature = "taiko")] - tx_type: TxType::Legacy, caller: Address::ZERO, gas_limit: u64::MAX, diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 7590cdda81..00cda698d4 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -14,8 +14,6 @@ pub mod precompile; pub mod result; pub mod specification; pub mod state; -#[cfg(feature = "taiko")] -pub mod taiko; pub mod utilities; pub use alloy_primitives::{ @@ -34,6 +32,4 @@ pub use precompile::*; pub use result::*; pub use specification::*; pub use state::*; -#[cfg(feature = "taiko")] -pub use taiko::*; pub use utilities::*; diff --git a/crates/primitives/src/taiko.rs b/crates/primitives/src/taiko.rs deleted file mode 100644 index d9c7e06738..0000000000 --- a/crates/primitives/src/taiko.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod anchor; -pub mod env; - -pub use anchor::*; -pub use env::*; diff --git a/crates/primitives/src/taiko/anchor.rs b/crates/primitives/src/taiko/anchor.rs deleted file mode 100644 index c63c1dac70..0000000000 --- a/crates/primitives/src/taiko/anchor.rs +++ /dev/null @@ -1,27 +0,0 @@ -use super::env::TxType; -use crate::{Address, Env, TransactTo, U256}; -use once_cell::sync::Lazy; -use std::str::FromStr; - -const ANCHOR_SELECTOR: u32 = 0xda69d3db; -const ANCHOR_GAS_LIMIT: u64 = 180_000; -static GOLDEN_TOUCH_ACCOUNT: Lazy
= Lazy::new(|| { - Address::from_str("0x0000777735367b36bC9B61C50022d9D0700dB4Ec") - .expect("invalid golden touch account") -}); - -pub static TREASURY: Lazy
= Lazy::new(|| { - Address::from_str("0xdf09A0afD09a63fb04ab3573922437e1e637dE8b") - .expect("invalid treasury account") -}); - -pub(crate) fn validate(env: &Env) -> bool { - !env.is_anchor() - || (env.tx.tx_type == TxType::Eip1559 - && env.tx.transact_to == TransactTo::Call(env.taiko.l2_address) - && u32::from_be_bytes(env.tx.data[..4].try_into().unwrap()) == ANCHOR_SELECTOR - && env.tx.value == U256::ZERO - && env.tx.gas_limit == ANCHOR_GAS_LIMIT - && env.tx.gas_price == env.block.basefee - && env.tx.caller == *GOLDEN_TOUCH_ACCOUNT) -} diff --git a/crates/primitives/src/taiko/env.rs b/crates/primitives/src/taiko/env.rs deleted file mode 100644 index b46879fd9d..0000000000 --- a/crates/primitives/src/taiko/env.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::Address; - -#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct TaikoEnv { - pub l2_address: Address, -} - -#[derive(Clone, Debug, Eq, PartialEq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub enum TxType { - Legacy, - Eip2930, - Eip1559, -} diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index b70ed60f00..e8f4c43b3d 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -28,6 +28,7 @@ tokio = { version = "1.32", features = [ ethers-providers = { version = "2.0", optional = true } ethers-core = { version = "2.0", optional = true } futures = { version = "0.3.27", optional = true } +once_cell = { version = "1.18", default-features = false, optional = true } [dev-dependencies] ethers-contract = { version = "2.0.10", default-features = false } @@ -41,7 +42,7 @@ serde = ["dep:serde", "dep:serde_json", "revm-interpreter/serde"] arbitrary = ["revm-interpreter/arbitrary"] optimism = ["revm-interpreter/optimism", "revm-precompile/optimism"] -taiko = ["revm-interpreter/taiko", "revm-precompile/taiko"] +taiko = ["revm-interpreter/taiko", "revm-precompile/taiko", "dep:once_cell"] ethersdb = ["std", "tokio", "futures", "ethers-providers", "ethers-core"] diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 69c6e106c8..6dfdc995d2 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -158,9 +158,6 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact fn preverify_transaction(&mut self) -> Result<(), EVMError> { let env = self.env(); - #[cfg(feature = "taiko")] - env.pre_check()?; - // Important: validate block before tx. env.validate_block_env::()?; env.validate_tx::()?; diff --git a/crates/revm/src/handler/taiko.rs b/crates/revm/src/handler/taiko.rs index 1caa7cfa78..86ac78f8ea 100644 --- a/crates/revm/src/handler/taiko.rs +++ b/crates/revm/src/handler/taiko.rs @@ -1,11 +1,18 @@ //! Mainnet related handlers. +use core::str::FromStr; use revm_interpreter::primitives::EVMError; use crate::{ interpreter::{return_ok, return_revert, Gas, InstructionResult}, - primitives::{db::Database, Env, Spec, SpecId::LONDON, U256}, + primitives::{db::Database, Address, Env, Spec, SpecId::LONDON, U256}, EVMData, }; +use once_cell::sync::Lazy; + +static TREASURY: Lazy
= Lazy::new(|| { + Address::from_str("0xdf09A0afD09a63fb04ab3573922437e1e637dE8b") + .expect("invalid treasury account") +}); /// Handle output of the transaction pub fn handle_call_return( @@ -93,7 +100,7 @@ pub fn reward_beneficiary( .balance .saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas_refund)); - let treasury = *crate::primitives::anchor::TREASURY; + let treasury = *TREASURY; let basefee = data.env.block.basefee; let (treasury_account, _) = data