Skip to content

Commit

Permalink
taiko reimburse_caller & reward_beneficiary
Browse files Browse the repository at this point in the history
  • Loading branch information
CeciliaZ030 committed Feb 1, 2024
1 parent 2ad9fa3 commit bc65348
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 42 deletions.
2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ serde = { version = "1.0", default-features = false, features = ["derive", "rc"]
hex = { version = "0.4", default-features = false }

[features]
default = ["std", "c-kzg"]
default = ["std", "c-kzg", "taiko"]
std = [
"serde?/std",
"alloy-primitives/std",
Expand Down
3 changes: 0 additions & 3 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,15 +802,12 @@ mod op_tests {
}
}



#[cfg(all(feature = "taiko", not(feature = "optimism")))]
#[test]
fn taiko_test() {
// TODO(Cecilia): taiko tests
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
42 changes: 6 additions & 36 deletions crates/primitives/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,7 @@ pub enum SpecId {
SHANGHAI = 18,
CANYON = 19,
CANCUN = 20,
ECOTONE = 21,
LATEST = u8::MAX,
}

/// Specification IDs and their activation block.
///
/// Information was obtained from the [Ethereum Execution Specifications](https://github.com/ethereum/execution-specs)
#[cfg(all(feature = "taiko", not(feature = "optimism")))]
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, enumn::N)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SpecId {
FRONTIER = 0,
FRONTIER_THAWING = 1,
HOMESTEAD = 2,
DAO_FORK = 3,
TANGERINE = 4,
SPURIOUS_DRAGON = 5,
BYZANTIUM = 6,
CONSTANTINOPLE = 7,
PETERSBURG = 8,
ISTANBUL = 9,
MUIR_GLACIER = 10,
BERLIN = 11,
LONDON = 12,
ARROW_GLACIER = 13,
GRAY_GLACIER = 14,
MERGE = 15,
SHANGHAI = 16,
CANCUN = 17,
KATLA = 18,
ECOTONE = 21,
LATEST = u8::MAX,
}

Expand Down Expand Up @@ -455,10 +425,10 @@ mod tests {
// TODO(Cecilia): update this range of bits
#[test]
fn test_katla_post_merge_hardforks() {
assert!(SpecId::enabled(SpecId::MERGE));
assert!(SpecId::enabled(SpecId::SHANGHAI));
assert!(!SpecId::enabled(SpecId::CANCUN));
assert!(!SpecId::enabled(SpecId::LATEST));
assert!(SpecId::enabled(SpecId::KATLA));
assert!(SpecId::enabled(SpecId::KATLA, SpecId::MERGE));
assert!(SpecId::enabled(SpecId::KATLA, SpecId::SHANGHAI));
assert!(!SpecId::enabled(SpecId::KATLA, SpecId::CANCUN));
assert!(!SpecId::enabled(SpecId::KATLA, SpecId::LATEST));
assert!(SpecId::enabled(SpecId::KATLA, SpecId::KATLA));
}
}
2 changes: 1 addition & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ criterion = "0.5"
indicatif = "0.17"

[features]
default = ["std", "c-kzg", "secp256k1"]
default = ["std", "c-kzg", "secp256k1", "taiko"]
std = ["serde?/std", "serde_json?/std", "serde_json?/preserve_order", "revm-interpreter/std", "revm-precompile/std"]
serde = ["dep:serde", "dep:serde_json", "revm-interpreter/serde"]
arbitrary = ["revm-interpreter/arbitrary"]
Expand Down
87 changes: 86 additions & 1 deletion crates/revm/src/taiko/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
handler::{
mainnet::{self, deduct_caller_inner},
mainnet::{self, deduct_caller, deduct_caller_inner, end, last_frame_return, output},
register::EvmHandler,
},
interpreter::{return_ok, return_revert, Gas, InstructionResult},
Expand All @@ -12,3 +12,88 @@ use crate::{
},
Context,
};
use alloc::sync::Arc;
use core::ops::Mul;
use SpecId::LONDON;

pub fn taiko_handle_register<DB: Database, EXT>(handler: &mut EvmHandler<'_, EXT, DB>) {
spec_to_generic!(handler.spec_id, {
handler.post_execution.reimburse_caller = Arc::new(reimburse_caller::<SPEC, EXT, DB>);
handler.post_execution.reward_beneficiary = Arc::new(reward_beneficiary::<SPEC, EXT, DB>);
});
}

#[inline]
pub fn reimburse_caller<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
gas: &Gas,
) -> Result<(), EVMError<DB::Error>> {
if context.evm.env.tx.taiko.is_anchor {
return Ok(());
}
let caller = context.evm.env.tx.caller;
let effective_gas_price = context.evm.env.effective_gas_price();

// return balance of not spend gas.
let (caller_account, _) = context
.evm
.journaled_state
.load_account(caller, &mut context.evm.db)
.map_err(EVMError::Database)?;

caller_account.info.balance = caller_account
.info
.balance
.saturating_add(effective_gas_price * U256::from(gas.remaining() + gas.refunded() as u64));

Ok(())
}

/// Reward beneficiary with gas fee.
#[inline]
pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
gas: &Gas,
) -> Result<(), EVMError<DB::Error>> {
if context.evm.env.tx.taiko.is_anchor {
return Ok(());
}
let beneficiary = context.evm.env.block.coinbase;
let effective_gas_price = context.evm.env.effective_gas_price();

// transfer fee to coinbase/beneficiary.
// EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded.
let coinbase_gas_price = if SPEC::enabled(LONDON) {
effective_gas_price.saturating_sub(context.evm.env.block.basefee)
} else {
effective_gas_price
};

let (coinbase_account, _) = context
.evm
.journaled_state
.load_account(beneficiary, &mut context.evm.db)
.map_err(EVMError::Database)?;

coinbase_account.mark_touch();
coinbase_account.info.balance = coinbase_account
.info
.balance
.saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas.refunded() as u64));

let treasury = context.evm.env.tx.taiko.treasury;
let basefee = context.evm.env.block.basefee;

let (treasury_account, _) = context
.evm
.journaled_state
.load_account(treasury, &mut context.evm.db)
.map_err(EVMError::Database)?;

treasury_account.mark_touch();
treasury_account.info.balance = treasury_account
.info
.balance
.saturating_add(basefee * U256::from(gas.spend() - gas.refunded() as u64));
Ok(())
}

0 comments on commit bc65348

Please sign in to comment.