Skip to content

Commit

Permalink
Add taiko module and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Dec 9, 2023
1 parent 0e7d779 commit 6c6b061
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 34 deletions.
25 changes: 10 additions & 15 deletions crates/primitives/src/env.rs
Expand Up @@ -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 {
Expand All @@ -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))]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -663,7 +658,7 @@ impl Env {
}

#[cfg(feature = "taiko")]
if self.is_anchor() {
if self.tx.taiko.is_anchor {
return Ok(());
}

Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/lib.rs
Expand Up @@ -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::{
Expand Down
8 changes: 8 additions & 0 deletions 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,
}
1 change: 1 addition & 0 deletions crates/primitives/src/taiko/mod.rs
@@ -0,0 +1 @@
pub mod env;
2 changes: 1 addition & 1 deletion crates/revm/src/evm_impl.rs
Expand Up @@ -194,7 +194,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact<DB::Error>
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 = {
Expand Down
11 changes: 5 additions & 6 deletions 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};
Expand Down Expand Up @@ -50,11 +48,12 @@ impl<DB: Database> Handler<DB> {
/// Handler for the taiko
#[cfg(feature = "taiko")]
pub fn taiko<SPEC: Spec>() -> Self {
use crate::taiko::handler;
Self {
call_return: taiko::handle_call_return::<SPEC>,
calculate_gas_refund: taiko::calculate_gas_refund::<SPEC>,
reimburse_caller: taiko::handle_reimburse_caller::<SPEC, DB>,
reward_beneficiary: taiko::reward_beneficiary::<SPEC, DB>,
call_return: handler::handle_call_return::<SPEC>,
calculate_gas_refund: handler::calculate_gas_refund::<SPEC>,
reimburse_caller: handler::handle_reimburse_caller::<SPEC, DB>,
reward_beneficiary: handler::reward_beneficiary::<SPEC, DB>,
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/revm/src/lib.rs
Expand Up @@ -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`.");

Expand Down
@@ -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<Address> = Lazy::new(|| {
Address::from_str("0xdf09A0afD09a63fb04ab3573922437e1e637dE8b")
.expect("invalid treasury account")
});

/// Handle output of the transaction
pub fn handle_call_return<SPEC: Spec>(
Expand All @@ -23,7 +16,7 @@ pub fn handle_call_return<SPEC: Spec>(
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);
Expand All @@ -48,7 +41,7 @@ pub fn handle_reimburse_caller<SPEC: Spec, DB: Database>(
gas_refund: u64,
) -> Result<(), EVMError<DB::Error>> {
let _ = data;
if data.env.is_anchor() {
if data.env.tx.taiko.is_anchor {
return Ok(());
}
let caller = data.env.tx.caller;
Expand All @@ -75,7 +68,7 @@ pub fn reward_beneficiary<SPEC: Spec, DB: Database>(
gas: &Gas,
gas_refund: u64,
) -> Result<(), EVMError<DB::Error>> {
if data.env.is_anchor() {
if data.env.tx.taiko.is_anchor {
return Ok(());
}
let beneficiary = data.env.block.coinbase;
Expand All @@ -100,7 +93,7 @@ pub fn reward_beneficiary<SPEC: Spec, DB: Database>(
.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
Expand Down
1 change: 1 addition & 0 deletions crates/revm/src/taiko/mod.rs
@@ -0,0 +1 @@
pub mod handler;

0 comments on commit 6c6b061

Please sign in to comment.