Skip to content

Commit bc65348

Browse files
committed
taiko reimburse_caller & reward_beneficiary
1 parent 2ad9fa3 commit bc65348

File tree

5 files changed

+94
-42
lines changed

5 files changed

+94
-42
lines changed

crates/primitives/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ serde = { version = "1.0", default-features = false, features = ["derive", "rc"]
4040
hex = { version = "0.4", default-features = false }
4141

4242
[features]
43-
default = ["std", "c-kzg"]
43+
default = ["std", "c-kzg", "taiko"]
4444
std = [
4545
"serde?/std",
4646
"alloy-primitives/std",

crates/primitives/src/env.rs

-3
Original file line numberDiff line numberDiff line change
@@ -802,15 +802,12 @@ mod op_tests {
802802
}
803803
}
804804

805-
806-
807805
#[cfg(all(feature = "taiko", not(feature = "optimism")))]
808806
#[test]
809807
fn taiko_test() {
810808
// TODO(Cecilia): taiko tests
811809
}
812810

813-
814811
#[cfg(test)]
815812
mod tests {
816813
use super::*;

crates/primitives/src/specification.rs

+6-36
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,7 @@ pub enum SpecId {
6262
SHANGHAI = 18,
6363
CANYON = 19,
6464
CANCUN = 20,
65-
ECOTONE = 21,
66-
LATEST = u8::MAX,
67-
}
68-
69-
/// Specification IDs and their activation block.
70-
///
71-
/// Information was obtained from the [Ethereum Execution Specifications](https://github.com/ethereum/execution-specs)
72-
#[cfg(all(feature = "taiko", not(feature = "optimism")))]
73-
#[repr(u8)]
74-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, enumn::N)]
75-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76-
pub enum SpecId {
77-
FRONTIER = 0,
78-
FRONTIER_THAWING = 1,
79-
HOMESTEAD = 2,
80-
DAO_FORK = 3,
81-
TANGERINE = 4,
82-
SPURIOUS_DRAGON = 5,
83-
BYZANTIUM = 6,
84-
CONSTANTINOPLE = 7,
85-
PETERSBURG = 8,
86-
ISTANBUL = 9,
87-
MUIR_GLACIER = 10,
88-
BERLIN = 11,
89-
LONDON = 12,
90-
ARROW_GLACIER = 13,
91-
GRAY_GLACIER = 14,
92-
MERGE = 15,
93-
SHANGHAI = 16,
94-
CANCUN = 17,
95-
KATLA = 18,
65+
ECOTONE = 21,
9666
LATEST = u8::MAX,
9767
}
9868

@@ -455,10 +425,10 @@ mod tests {
455425
// TODO(Cecilia): update this range of bits
456426
#[test]
457427
fn test_katla_post_merge_hardforks() {
458-
assert!(SpecId::enabled(SpecId::MERGE));
459-
assert!(SpecId::enabled(SpecId::SHANGHAI));
460-
assert!(!SpecId::enabled(SpecId::CANCUN));
461-
assert!(!SpecId::enabled(SpecId::LATEST));
462-
assert!(SpecId::enabled(SpecId::KATLA));
428+
assert!(SpecId::enabled(SpecId::KATLA, SpecId::MERGE));
429+
assert!(SpecId::enabled(SpecId::KATLA, SpecId::SHANGHAI));
430+
assert!(!SpecId::enabled(SpecId::KATLA, SpecId::CANCUN));
431+
assert!(!SpecId::enabled(SpecId::KATLA, SpecId::LATEST));
432+
assert!(SpecId::enabled(SpecId::KATLA, SpecId::KATLA));
463433
}
464434
}

crates/revm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ criterion = "0.5"
4141
indicatif = "0.17"
4242

4343
[features]
44-
default = ["std", "c-kzg", "secp256k1"]
44+
default = ["std", "c-kzg", "secp256k1", "taiko"]
4545
std = ["serde?/std", "serde_json?/std", "serde_json?/preserve_order", "revm-interpreter/std", "revm-precompile/std"]
4646
serde = ["dep:serde", "dep:serde_json", "revm-interpreter/serde"]
4747
arbitrary = ["revm-interpreter/arbitrary"]

crates/revm/src/taiko/handler_register.rs

+86-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
handler::{
5-
mainnet::{self, deduct_caller_inner},
5+
mainnet::{self, deduct_caller, deduct_caller_inner, end, last_frame_return, output},
66
register::EvmHandler,
77
},
88
interpreter::{return_ok, return_revert, Gas, InstructionResult},
@@ -12,3 +12,88 @@ use crate::{
1212
},
1313
Context,
1414
};
15+
use alloc::sync::Arc;
16+
use core::ops::Mul;
17+
use SpecId::LONDON;
18+
19+
pub fn taiko_handle_register<DB: Database, EXT>(handler: &mut EvmHandler<'_, EXT, DB>) {
20+
spec_to_generic!(handler.spec_id, {
21+
handler.post_execution.reimburse_caller = Arc::new(reimburse_caller::<SPEC, EXT, DB>);
22+
handler.post_execution.reward_beneficiary = Arc::new(reward_beneficiary::<SPEC, EXT, DB>);
23+
});
24+
}
25+
26+
#[inline]
27+
pub fn reimburse_caller<SPEC: Spec, EXT, DB: Database>(
28+
context: &mut Context<EXT, DB>,
29+
gas: &Gas,
30+
) -> Result<(), EVMError<DB::Error>> {
31+
if context.evm.env.tx.taiko.is_anchor {
32+
return Ok(());
33+
}
34+
let caller = context.evm.env.tx.caller;
35+
let effective_gas_price = context.evm.env.effective_gas_price();
36+
37+
// return balance of not spend gas.
38+
let (caller_account, _) = context
39+
.evm
40+
.journaled_state
41+
.load_account(caller, &mut context.evm.db)
42+
.map_err(EVMError::Database)?;
43+
44+
caller_account.info.balance = caller_account
45+
.info
46+
.balance
47+
.saturating_add(effective_gas_price * U256::from(gas.remaining() + gas.refunded() as u64));
48+
49+
Ok(())
50+
}
51+
52+
/// Reward beneficiary with gas fee.
53+
#[inline]
54+
pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
55+
context: &mut Context<EXT, DB>,
56+
gas: &Gas,
57+
) -> Result<(), EVMError<DB::Error>> {
58+
if context.evm.env.tx.taiko.is_anchor {
59+
return Ok(());
60+
}
61+
let beneficiary = context.evm.env.block.coinbase;
62+
let effective_gas_price = context.evm.env.effective_gas_price();
63+
64+
// transfer fee to coinbase/beneficiary.
65+
// EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded.
66+
let coinbase_gas_price = if SPEC::enabled(LONDON) {
67+
effective_gas_price.saturating_sub(context.evm.env.block.basefee)
68+
} else {
69+
effective_gas_price
70+
};
71+
72+
let (coinbase_account, _) = context
73+
.evm
74+
.journaled_state
75+
.load_account(beneficiary, &mut context.evm.db)
76+
.map_err(EVMError::Database)?;
77+
78+
coinbase_account.mark_touch();
79+
coinbase_account.info.balance = coinbase_account
80+
.info
81+
.balance
82+
.saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas.refunded() as u64));
83+
84+
let treasury = context.evm.env.tx.taiko.treasury;
85+
let basefee = context.evm.env.block.basefee;
86+
87+
let (treasury_account, _) = context
88+
.evm
89+
.journaled_state
90+
.load_account(treasury, &mut context.evm.db)
91+
.map_err(EVMError::Database)?;
92+
93+
treasury_account.mark_touch();
94+
treasury_account.info.balance = treasury_account
95+
.info
96+
.balance
97+
.saturating_add(basefee * U256::from(gas.spend() - gas.refunded() as u64));
98+
Ok(())
99+
}

0 commit comments

Comments
 (0)