From 6c6b061ca06460012bbf53af52d113b8d9da4ec1 Mon Sep 17 00:00:00 2001 From: john xu Date: Sat, 9 Dec 2023 07:13:20 +0000 Subject: [PATCH] Add taiko module and handler --- crates/primitives/src/env.rs | 25 ++++++++----------- crates/primitives/src/lib.rs | 2 ++ crates/primitives/src/taiko/env.rs | 8 ++++++ crates/primitives/src/taiko/mod.rs | 1 + crates/revm/src/evm_impl.rs | 2 +- crates/revm/src/handler.rs | 11 ++++---- crates/revm/src/lib.rs | 3 +++ .../{handler/taiko.rs => taiko/handler.rs} | 17 ++++--------- crates/revm/src/taiko/mod.rs | 1 + 9 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 crates/primitives/src/taiko/env.rs create mode 100644 crates/primitives/src/taiko/mod.rs rename crates/revm/src/{handler/taiko.rs => taiko/handler.rs} (92%) create mode 100644 crates/revm/src/taiko/mod.rs diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index 853b289191..862da8a46f 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -5,6 +5,9 @@ use crate::{ }; use core::cmp::{min, Ordering}; +#[cfg(feature = "taiko")] +use crate::taiko::env::TaikoFields; + #[derive(Clone, Debug, Default, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Env { @@ -13,13 +16,6 @@ pub struct Env { pub tx: TxEnv, } -#[cfg(feature = "taiko")] -impl Env { - pub fn is_anchor(&self) -> bool { - self.tx.index == 0 - } -} - /// The block environment. #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -136,10 +132,6 @@ impl BlockEnv { #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct TxEnv { - /// The index of the transaction in the block. - #[cfg(feature = "taiko")] - pub index: usize, - /// Caller aka Author aka transaction signer. pub caller: Address, /// The gas limit of the transaction. @@ -194,6 +186,10 @@ pub struct TxEnv { #[cfg_attr(feature = "serde", serde(flatten))] #[cfg(feature = "optimism")] pub optimism: OptimismFields, + + #[cfg_attr(feature = "serde", serde(flatten))] + #[cfg(feature = "taiko")] + pub taiko: TaikoFields, } impl TxEnv { @@ -439,9 +435,6 @@ impl Default for BlockEnv { impl Default for TxEnv { fn default() -> Self { Self { - #[cfg(feature = "taiko")] - index: 0, - caller: Address::ZERO, gas_limit: u64::MAX, gas_price: U256::ZERO, @@ -456,6 +449,8 @@ impl Default for TxEnv { max_fee_per_blob_gas: None, #[cfg(feature = "optimism")] optimism: OptimismFields::default(), + #[cfg(feature = "taiko")] + taiko: TaikoFields::default(), } } } @@ -663,7 +658,7 @@ impl Env { } #[cfg(feature = "taiko")] - if self.is_anchor() { + if self.tx.taiko.is_anchor { return Ok(()); } diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 00cda698d4..644a19c0ee 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -14,6 +14,8 @@ 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::{ diff --git a/crates/primitives/src/taiko/env.rs b/crates/primitives/src/taiko/env.rs new file mode 100644 index 0000000000..df3bb55128 --- /dev/null +++ b/crates/primitives/src/taiko/env.rs @@ -0,0 +1,8 @@ +use crate::Address; + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct TaikoFields { + pub treasury: Address, + pub is_anchor: bool, +} diff --git a/crates/primitives/src/taiko/mod.rs b/crates/primitives/src/taiko/mod.rs new file mode 100644 index 0000000000..3d7924f6ab --- /dev/null +++ b/crates/primitives/src/taiko/mod.rs @@ -0,0 +1 @@ +pub mod env; diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 6dfdc995d2..a062613e75 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -194,7 +194,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact let tx_data = env.tx.data.clone(); let tx_gas_limit = env.tx.gas_limit; #[cfg(feature = "taiko")] - let is_anchor = env.is_anchor(); + let is_anchor = env.tx.taiko.is_anchor; #[cfg(feature = "optimism")] let tx_l1_cost = { diff --git a/crates/revm/src/handler.rs b/crates/revm/src/handler.rs index cdff4f69d0..ba347f852f 100644 --- a/crates/revm/src/handler.rs +++ b/crates/revm/src/handler.rs @@ -1,8 +1,6 @@ pub mod mainnet; #[cfg(feature = "optimism")] pub mod optimism; -#[cfg(feature = "taiko")] -pub mod taiko; use revm_interpreter::primitives::db::Database; use revm_interpreter::primitives::{EVMError, EVMResultGeneric}; @@ -50,11 +48,12 @@ impl Handler { /// Handler for the taiko #[cfg(feature = "taiko")] pub fn taiko() -> Self { + use crate::taiko::handler; Self { - call_return: taiko::handle_call_return::, - calculate_gas_refund: taiko::calculate_gas_refund::, - reimburse_caller: taiko::handle_reimburse_caller::, - reward_beneficiary: taiko::reward_beneficiary::, + call_return: handler::handle_call_return::, + calculate_gas_refund: handler::calculate_gas_refund::, + reimburse_caller: handler::handle_reimburse_caller::, + reward_beneficiary: handler::reward_beneficiary::, } } diff --git a/crates/revm/src/lib.rs b/crates/revm/src/lib.rs index fbaeab6c18..61c7dac1fb 100644 --- a/crates/revm/src/lib.rs +++ b/crates/revm/src/lib.rs @@ -15,6 +15,9 @@ mod journaled_state; #[cfg(feature = "optimism")] pub mod optimism; +#[cfg(feature = "taiko")] +pub mod taiko; + #[cfg(all(feature = "with-serde", not(feature = "serde")))] compile_error!("`with-serde` feature has been renamed to `serde`."); diff --git a/crates/revm/src/handler/taiko.rs b/crates/revm/src/taiko/handler.rs similarity index 92% rename from crates/revm/src/handler/taiko.rs rename to crates/revm/src/taiko/handler.rs index 86ac78f8ea..311c8eed45 100644 --- a/crates/revm/src/handler/taiko.rs +++ b/crates/revm/src/taiko/handler.rs @@ -1,18 +1,11 @@ //! Mainnet related handlers. -use core::str::FromStr; use revm_interpreter::primitives::EVMError; use crate::{ interpreter::{return_ok, return_revert, Gas, InstructionResult}, - primitives::{db::Database, Address, Env, Spec, SpecId::LONDON, U256}, + primitives::{db::Database, 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( @@ -23,7 +16,7 @@ pub fn handle_call_return( let tx_gas_limit = env.tx.gas_limit; // Spend the gas limit. Gas is reimbursed when the tx returns successfully. let mut gas = Gas::new(tx_gas_limit); - if env.is_anchor() { + if env.tx.taiko.is_anchor { return gas; } gas.record_cost(tx_gas_limit); @@ -48,7 +41,7 @@ pub fn handle_reimburse_caller( gas_refund: u64, ) -> Result<(), EVMError> { let _ = data; - if data.env.is_anchor() { + if data.env.tx.taiko.is_anchor { return Ok(()); } let caller = data.env.tx.caller; @@ -75,7 +68,7 @@ pub fn reward_beneficiary( gas: &Gas, gas_refund: u64, ) -> Result<(), EVMError> { - if data.env.is_anchor() { + if data.env.tx.taiko.is_anchor { return Ok(()); } let beneficiary = data.env.block.coinbase; @@ -100,7 +93,7 @@ pub fn reward_beneficiary( .balance .saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas_refund)); - let treasury = *TREASURY; + let treasury = data.env.tx.taiko.treasury; let basefee = data.env.block.basefee; let (treasury_account, _) = data diff --git a/crates/revm/src/taiko/mod.rs b/crates/revm/src/taiko/mod.rs new file mode 100644 index 0000000000..062ae9d9be --- /dev/null +++ b/crates/revm/src/taiko/mod.rs @@ -0,0 +1 @@ +pub mod handler;