Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ alloy-rpc-types-engine = { version = "1.5.2", default-features = false }

# op-alloy
alloy-op-hardforks = { version = "0.4.7" }
op-alloy = { version = "0.23", default-features = false, features = [
op-alloy = { version = "0.23.1", default-features = false, features = [
"consensus",
] }

Expand All @@ -66,3 +66,19 @@ serde = { version = "1", default-features = false, features = ["derive"] }
thiserror = { version = "2.0.0", default-features = false }
serde_json = "1"
test-case = "3"

[patch.crates-io]
revm = { git = "https://github.com/bluealloy/revm", rev = "6aa06829d2caa2aa38606ed22b83354a7a7ff98e" }
op-revm = { git = "https://github.com/bluealloy/revm", rev = "6aa06829d2caa2aa38606ed22b83354a7a7ff98e" }

# alloy bal-devnet2 patches
alloy-eips = { git = "https://github.com/alloy-rs/alloy", branch = "bal-devnet2" }
alloy-serde = { git = "https://github.com/alloy-rs/alloy", branch = "bal-devnet2" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", branch = "bal-devnet2" }
alloy-rpc-types-eth = { git = "https://github.com/alloy-rs/alloy", branch = "bal-devnet2" }
alloy-rpc-types-engine = { git = "https://github.com/alloy-rs/alloy", branch = "bal-devnet2" }
alloy-network-primitives = { git = "https://github.com/alloy-rs/alloy", branch = "bal-devnet2" }

# op-alloy bal-devnet2 patches
op-alloy-consensus = { git = "https://github.com/alloy-rs/op-alloy", branch = "bal-devnet2" }
op-alloy-rpc-types-engine = { git = "https://github.com/alloy-rs/op-alloy", branch = "bal-devnet2" }
6 changes: 2 additions & 4 deletions crates/evm/src/block/state_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ pub fn insert_post_block_withdrawals_balance_increments(
if spec.is_shanghai_active_at_timestamp(block_timestamp) {
if let Some(withdrawals) = withdrawals {
for withdrawal in withdrawals {
if withdrawal.amount > 0 {
*balance_increments.entry(withdrawal.address).or_default() +=
withdrawal.amount_wei().to::<u128>();
}
*balance_increments.entry(withdrawal.address).or_default() +=
withdrawal.amount_wei().to::<u128>();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ mod tests {
evm_env.cfg_env.max_initcode_size(),
revm::primitives::eip3860::MAX_INITCODE_SIZE
);
// None respects the spec's default (u64::MAX for pre-Osaka specs)
assert_eq!(evm_env.cfg_env.tx_gas_limit_cap(), u64::MAX);
// None respects the spec's default (OSAKA default uses EIP-7825 cap)
assert_eq!(evm_env.cfg_env.tx_gas_limit_cap(), revm::primitives::eip7825::TX_GAS_LIMIT_CAP);
}

#[test]
Expand Down
48 changes: 45 additions & 3 deletions crates/evm/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct EthBlockExecutionCtx<'a> {
pub extra_data: Bytes,
/// Block transactions count hint. Used to preallocate the receipts vector.
pub tx_count_hint: Option<usize>,
/// Slot number (EIP-7928, Amsterdam).
pub slot_number: Option<u64>,
}

/// Block executor for Ethereum.
Expand All @@ -62,8 +64,17 @@ pub struct EthBlockExecutor<'a, Evm, Spec, R: ReceiptBuilder> {
/// Receipts of executed transactions.
pub receipts: Vec<R::Receipt>,
/// Total gas used by transactions in this block.
///
/// Before Osaka, this tracks gas after refunds.
/// After Osaka (EIP-7778), this tracks gas before refunds for block gas accounting.
pub gas_used: u64,

/// Total gas spent by transactions in this block (after refunds, what users pay).
///
/// This is only tracked when EIP-7778 is active (Osaka hardfork).
/// Before Osaka, this is always `None`.
pub gas_spent: Option<u64>,

/// Blob gas used by the block.
/// Before cancun activation, this is always 0.
pub blob_gas_used: u64,
Expand Down Expand Up @@ -101,6 +112,7 @@ where
ctx,
receipts: Vec::with_capacity(tx_count_hint),
gas_used: 0,
gas_spent: None,
blob_gas_used: 0,
system_caller: SystemCaller::new(spec.clone()),
spec,
Expand Down Expand Up @@ -169,15 +181,44 @@ where
}

fn commit_transaction(&mut self, output: Self::Result) -> Result<u64, BlockExecutionError> {
use revm::context::result::ExecutionResult;

let EthTxResult { result: ResultAndState { result, state }, blob_gas_used, tx_type } =
output;

self.system_caller.on_state(StateChangeSource::Transaction(self.receipts.len()), &state);

let gas_used = result.gas_used();

// append gas used
self.gas_used += gas_used;
// EIP-7778: Track gas accounting differently for Amsterdam
// - gas_used (for block accounting): gas before refunds
// - gas_spent (for user receipts): gas after refunds (what user pays)
let is_amsterdam = self
.spec
.is_amsterdam_active_at_timestamp(self.evm.block().timestamp().saturating_to());

let (cumulative_gas_used, gas_spent) = if is_amsterdam {
// Get gas_refunded from the result (only Success variant has refunds)
let gas_refunded = match &result {
ExecutionResult::Success { gas_refunded, .. } => *gas_refunded,
_ => 0,
};

// gas_used from result is already after refunds
let tx_gas_spent = gas_used;
// gas before refunds = gas after refunds + refunded amount
let tx_gas_used_before_refunds = gas_used + gas_refunded;

self.gas_used += tx_gas_used_before_refunds;
let cumulative_gas_spent = self.gas_spent.get_or_insert(0).saturating_add(tx_gas_spent);
*self.gas_spent.as_mut().unwrap() = cumulative_gas_spent;

(self.gas_used, Some(cumulative_gas_spent))
} else {
// Pre-Amsterdam: gas_used tracks gas after refunds
self.gas_used += gas_used;
(self.gas_used, None)
};

// only determine cancun fields when active
if self.spec.is_cancun_active_at_timestamp(self.evm.block().timestamp().saturating_to()) {
Expand All @@ -190,7 +231,8 @@ where
evm: &self.evm,
result,
state: &state,
cumulative_gas_used: self.gas_used,
cumulative_gas_used,
gas_spent,
}));

// Commit the state changes.
Expand Down
4 changes: 4 additions & 0 deletions crates/evm/src/eth/eip6110.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ mod tests {
logs: serde_json::from_str(
r#"[{"address":"0x00000000219ab540356cbb839cbe05303d7705fa","topics":["0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"],"data":"0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030998c8086669bf65e24581cda47d8537966e9f5066fc6ffdcba910a1bfb91eae7a4873fcce166a1c4ea217e6b1afd396200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002001000000000000000000000001c340fb72ed14d4eaa71f7633ee9e33b88d4f3900000000000000000000000000000000000000000000000000000000000000080040597307000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006098ddbffd700c1aac324cfdf0492ff289223661eb26718ce3651ba2469b22f480d56efab432ed91af05a006bde0c1ea68134e0acd8cacca0c13ad1f716db874b44abfcc966368019753174753bca3af2ea84bc569c46f76592a91e97f311eddec0000000000000000000000000000000000000000000000000000000000000008e474160000000000000000000000000000000000000000000000000000000000","blockHash":"0x8d1289c5a7e0965b1d1bb75cdc4c3f73dda82d4ebb94ff5b98d1389cebd53b56","blockNumber":"0x12f0d8d","transactionHash":"0xa5239d4c542063d29022545835815b78b09f571f2bf1c8427f4765d6f5abbce9","transactionIndex":"0xc4","logIndex":"0x18f","removed":false}]"#
).unwrap(),

},
// https://etherscan.io/tx/0xd9734d4e3953bcaa939fd1c1d80950ee54aeecc02eef6ae8179f47f5b7103338
Receipt {
Expand All @@ -143,6 +144,7 @@ mod tests {
logs: serde_json::from_str(
r#"[{"address":"0x00000000219ab540356cbb839cbe05303d7705fa","topics":["0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"],"data":"0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030a1a2ba870a90e889aa594a0cc1c6feffb94c2d8f65646c937f1f456a315ef649533e25a4614d8f4f66ebdb06481b90af0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000a0f04a231efbc29e1db7d086300ff550211c2f6000000000000000000000000000000000000000000000000000000000000000800405973070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060ad416d590e1a7f52baff770a12835b68904efad22cc9f8ba531e50cbbd26f32b9c7373cf6538a0577f501e4d3e3e63e208767bcccaae94e1e3720bfb734a286f9c017d17af46536545ccb7ca94d71f295e71f6d25bf978c09ada6f8d3f7ba0390000000000000000000000000000000000000000000000000000000000000008e374160000000000000000000000000000000000000000000000000000000000","blockHash":"0x8d1289c5a7e0965b1d1bb75cdc4c3f73dda82d4ebb94ff5b98d1389cebd53b56","blockNumber":"0x12f0d8d","transactionHash":"0xd9734d4e3953bcaa939fd1c1d80950ee54aeecc02eef6ae8179f47f5b7103338","transactionIndex":"0x7c","logIndex":"0xe2","removed":false}]"#,
).unwrap(),

},
];

Expand Down Expand Up @@ -181,6 +183,7 @@ mod tests {
},
{"address":"0x00000000219ab540356cbb839cbe05303d7705fa","topics":["0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"],"data":"0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030998c8086669bf65e24581cda47d8537966e9f5066fc6ffdcba910a1bfb91eae7a4873fcce166a1c4ea217e6b1afd396200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002001000000000000000000000001c340fb72ed14d4eaa71f7633ee9e33b88d4f3900000000000000000000000000000000000000000000000000000000000000080040597307000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006098ddbffd700c1aac324cfdf0492ff289223661eb26718ce3651ba2469b22f480d56efab432ed91af05a006bde0c1ea68134e0acd8cacca0c13ad1f716db874b44abfcc966368019753174753bca3af2ea84bc569c46f76592a91e97f311eddec0000000000000000000000000000000000000000000000000000000000000008e474160000000000000000000000000000000000000000000000000000000000","blockHash":"0x8d1289c5a7e0965b1d1bb75cdc4c3f73dda82d4ebb94ff5b98d1389cebd53b56","blockNumber":"0x12f0d8d","transactionHash":"0xa5239d4c542063d29022545835815b78b09f571f2bf1c8427f4765d6f5abbce9","transactionIndex":"0xc4","logIndex":"0x18f","removed":false}]"#
).unwrap(),

},
// https://etherscan.io/tx/0xd9734d4e3953bcaa939fd1c1d80950ee54aeecc02eef6ae8179f47f5b7103338
Receipt {
Expand All @@ -189,6 +192,7 @@ mod tests {
logs: serde_json::from_str(
r#"[{"address":"0x00000000219ab540356cbb839cbe05303d7705fa","topics":["0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"],"data":"0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030a1a2ba870a90e889aa594a0cc1c6feffb94c2d8f65646c937f1f456a315ef649533e25a4614d8f4f66ebdb06481b90af0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000a0f04a231efbc29e1db7d086300ff550211c2f6000000000000000000000000000000000000000000000000000000000000000800405973070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060ad416d590e1a7f52baff770a12835b68904efad22cc9f8ba531e50cbbd26f32b9c7373cf6538a0577f501e4d3e3e63e208767bcccaae94e1e3720bfb734a286f9c017d17af46536545ccb7ca94d71f295e71f6d25bf978c09ada6f8d3f7ba0390000000000000000000000000000000000000000000000000000000000000008e374160000000000000000000000000000000000000000000000000000000000","blockHash":"0x8d1289c5a7e0965b1d1bb75cdc4c3f73dda82d4ebb94ff5b98d1389cebd53b56","blockNumber":"0x12f0d8d","transactionHash":"0xd9734d4e3953bcaa939fd1c1d80950ee54aeecc02eef6ae8179f47f5b7103338","transactionIndex":"0x7c","logIndex":"0xe2","removed":false}]"#,
).unwrap(),

},
];

Expand Down
5 changes: 5 additions & 0 deletions crates/evm/src/eth/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl EvmEnv<SpecId> {
gas_limit: input.gas_limit,
basefee: input.base_fee_per_gas,
blob_excess_gas_and_price,
slot_num: input.slot_number.unwrap_or_default(),
};

Self::new(cfg_env, block_env)
Expand All @@ -106,6 +107,7 @@ pub(crate) struct EvmEnvInput {
pub(crate) gas_limit: u64,
pub(crate) excess_blob_gas: Option<u64>,
pub(crate) base_fee_per_gas: u64,
pub(crate) slot_number: Option<u64>,
}

impl EvmEnvInput {
Expand All @@ -119,6 +121,7 @@ impl EvmEnvInput {
gas_limit: header.gas_limit(),
excess_blob_gas: header.excess_blob_gas(),
base_fee_per_gas: header.base_fee_per_gas().unwrap_or_default(),
slot_number: header.slot_number(),
}
}

Expand All @@ -141,6 +144,7 @@ impl EvmEnvInput {
.maybe_next_block_excess_blob_gas(blob_params)
.or_else(|| blob_params.map(|_| 0)),
base_fee_per_gas,
slot_number: None,
}
}
}
Expand Down Expand Up @@ -198,6 +202,7 @@ mod payload {
gas_limit: payload.gas_limit(),
excess_blob_gas: payload.excess_blob_gas(),
base_fee_per_gas: payload.saturated_base_fee_per_gas(),
slot_number: payload.slot_number(),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/evm/src/eth/receipt_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ pub struct ReceiptBuilderCtx<'a, T, E: Evm> {
pub state: &'a EvmState,
/// Cumulative gas used.
pub cumulative_gas_used: u64,
/// Gas spent by the transaction after refunds (what the user pays).
///
/// This is set when EIP-7778 is active (Osaka hardfork).
/// Before Osaka, this is `None` and `cumulative_gas_used` tracks gas after refunds.
/// After Osaka, `cumulative_gas_used` tracks gas before refunds, and `gas_spent`
/// tracks gas after refunds.
pub gas_spent: Option<u64>,
}

/// Type that knows how to build a receipt based on execution result.
Expand Down
4 changes: 3 additions & 1 deletion crates/evm/src/eth/spec_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub fn spec_by_timestamp_and_block_number<C>(
where
C: EthereumHardforks,
{
if chain_spec.is_osaka_active_at_timestamp(timestamp) {
if chain_spec.is_amsterdam_active_at_timestamp(timestamp) {
SpecId::AMSTERDAM
} else if chain_spec.is_osaka_active_at_timestamp(timestamp) {
SpecId::OSAKA
} else if chain_spec.is_prague_active_at_timestamp(timestamp) {
SpecId::PRAGUE
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/src/op/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl EvmEnv<OpSpecId> {
basefee: input.base_fee_per_gas,
// EIP-4844 excess blob gas of this block, introduced in Cancun
blob_excess_gas_and_price,
slot_num: 0,
};

Self::new(cfg_env, block_env)
Expand Down Expand Up @@ -114,6 +115,7 @@ mod payload {
gas_limit: payload.as_v1().gas_limit,
excess_blob_gas: payload.as_v3().map(|v| v.excess_blob_gas),
base_fee_per_gas: payload.as_v1().base_fee_per_gas.saturating_to(),
slot_number: None,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/evm/src/overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ impl<DB> OverrideBlockHashes for CacheDB<DB> {

impl<DB> OverrideBlockHashes for State<DB> {
fn override_block_hashes(&mut self, block_hashes: BTreeMap<u64, B256>) {
self.block_hashes.extend(block_hashes);
for (num, hash) in block_hashes {
self.block_hashes.insert(num, hash);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/op-evm/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ where
cumulative_gas_used: self.gas_used,
evm: &self.evm,
state: &state,
gas_spent: None,
}) {
Ok(receipt) => receipt,
Err(ctx) => {
Expand Down
Loading