Skip to content
Open
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,13 @@ reth-trie-sparse = { git = "https://github.com/morph-l2/reth", tag = "v2.5.2-mor
reth-revm = { git = "https://github.com/morph-l2/reth", tag = "v2.5.2-morph.1", default-features = false, features = [
"optional-checks",
] }
# `optional_no_base_fee` keeps `CfgEnv::disable_base_fee` and
# `is_base_fee_check_disabled()` available to standalone `morph-revm` builds; the
# node build already enables it through `reth-engine-tree`.
revm = { version = "42.0.1", features = [
"optional_fee_charge",
"optional_eip7623",
"optional_no_base_fee",
], default-features = false }

alloy = { version = "2.3.0", default-features = false }
Expand Down
12 changes: 10 additions & 2 deletions crates/evm/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

mod factory;
mod receipt;
mod simulate;

pub(crate) use factory::MorphBlockExecutorFactory;
pub(crate) use receipt::{
Expand All @@ -24,7 +25,10 @@ use alloy_evm::{
};
use alloy_primitives::{Address, U256};
use morph_primitives::{MorphReceipt, MorphTxEnvelope};
use morph_revm::{L1_GAS_PRICE_ORACLE_ADDRESS, MorphHaltReason, TokenFeeInfo, evm::MorphContext};
use morph_revm::{
L1_GAS_PRICE_ORACLE_ADDRESS, MorphHaltReason, TokenFeeInfo, encode_simulated_tx_for_l1_fee,
evm::MorphContext,
};
use reth_primitives_traits::Recovered;
use reth_revm::{DatabaseCommit, Inspector, context::result::ResultAndState};
use revm::context::Block;
Expand Down Expand Up @@ -208,7 +212,7 @@ where
&mut self,
tx: impl ExecutableTx<Self>,
) -> Result<Self::Result, BlockExecutionError> {
let (tx_env, recovered) = tx.into_parts();
let (mut tx_env, recovered) = tx.into_parts();

// Validate gas limit against the geth-compatible block gas pool. Receipt gas cannot be
// used here: L1 messages report their actual execution cost in receipts but reserve their
Expand All @@ -226,6 +230,10 @@ where
)));
}

if simulate::needs_simulated_l1_encoding(self.evm.cfg_env(), tx_env.rlp_bytes.as_ref()) {
tx_env.rlp_bytes = Some(encode_simulated_tx_for_l1_fee(recovered.tx()));
}

// Clone the consensus tx and signer BEFORE transact (since we can't move out later).
let consensus_tx = recovered.tx().clone();
let signer = *recovered.signer();
Expand Down
80 changes: 80 additions & 0 deletions crates/evm/src/block/simulate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! L1 data fee encoding for `eth_simulateV1` calls executed by [`super::MorphBlockExecutor`].

use alloy_primitives::Bytes;
use revm::context::Cfg;

/// Whether a simulated transaction must be priced with a placeholder-signature encoding.
///
/// Upstream `eth_simulateV1` (`simulate::execute_transactions`) wraps resolved calls in an empty
/// `WithEncoded` envelope with a zero signature on the recovered transaction. A
/// `validation: true` simulation (EIP-3607 disabled, nonce checks and fee charging on) is
/// priced like a real signed transaction, so the executor encodes a copy with a full-size
/// placeholder signature for Morph's L1 data fee without touching the recovered transaction.
/// A `validation: false` simulation keeps the empty envelope, which the handler prices at zero
/// L1 fee as upstream does for L2s. Consensus execution never disables EIP-3607, so a real
/// transaction is never given a fabricated encoding.
pub(super) fn needs_simulated_l1_encoding(cfg: &impl Cfg, rlp_bytes: Option<&Bytes>) -> bool {
cfg.is_eip3607_disabled()
&& !cfg.is_nonce_check_disabled()
&& !cfg.is_fee_charge_disabled()
&& rlp_bytes.is_none_or(|bytes| bytes.is_empty())
}

#[cfg(test)]
mod tests {
use super::needs_simulated_l1_encoding;
use alloy_primitives::Bytes;
use morph_chainspec::MorphHardfork;
use revm::context::CfgEnv;

fn cfg(
eip3607_disabled: bool,
nonce_check_disabled: bool,
fee_charge_disabled: bool,
) -> CfgEnv<MorphHardfork> {
let mut cfg = CfgEnv::new_with_spec(MorphHardfork::Celadon);
cfg.disable_eip3607 = eip3607_disabled;
cfg.disable_nonce_check = nonce_check_disabled;
cfg.disable_fee_charge = fee_charge_disabled;
cfg
}

#[test]
fn consensus_execution_never_gets_a_placeholder_encoding() {
let consensus = cfg(false, false, false);
assert!(!needs_simulated_l1_encoding(&consensus, None));
assert!(!needs_simulated_l1_encoding(
&consensus,
Some(&Bytes::new())
));
}

#[test]
fn only_validating_simulations_get_a_placeholder_encoding() {
// `eth_simulateV1` with `validation: true`.
assert!(needs_simulated_l1_encoding(
&cfg(true, false, false),
Some(&Bytes::new())
));
assert!(needs_simulated_l1_encoding(&cfg(true, false, false), None));
// `validation: false` keeps the empty envelope, priced at zero L1 fee by the handler.
assert!(!needs_simulated_l1_encoding(
&cfg(true, true, false),
Some(&Bytes::new())
));
// Fee charging disabled (`eth_call`-style) never computes an L1 fee.
assert!(!needs_simulated_l1_encoding(
&cfg(true, false, true),
Some(&Bytes::new())
));
}

#[test]
fn real_encoding_is_never_replaced() {
let encoded = Bytes::from_static(&[0x02, 0xc0]);
assert!(!needs_simulated_l1_encoding(
&cfg(true, false, false),
Some(&encoded)
));
}
}
32 changes: 32 additions & 0 deletions crates/node/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,38 @@ impl TestNodeBuilder {
self
}

/// Override a storage slot for an account in the test genesis.
pub fn with_account_storage(mut self, address: Address, slot: U256, value: U256) -> Self {
let alloc = self
.genesis_json
.get_mut("alloc")
.and_then(serde_json::Value::as_object_mut)
.expect("test genesis alloc must be an object");
let address_lower = address.to_string().to_ascii_lowercase();
let key = alloc
.keys()
.find(|existing| existing.eq_ignore_ascii_case(&address_lower))
.cloned()
.unwrap_or(address_lower);
let entry = alloc
.entry(key)
.or_insert_with(|| serde_json::json!({ "balance": "0x0" }));
let entry = entry
.as_object_mut()
.expect("test genesis account entry must be an object");
let storage = entry
.entry("storage")
.or_insert_with(|| serde_json::json!({}));
let storage = storage
.as_object_mut()
.expect("test genesis account storage must be an object");
storage.insert(
format!("{slot:#066x}"),
serde_json::json!(format!("{value:#066x}")),
);
self
}

/// Set the number of nodes to start.
///
/// When `num_nodes > 1`, all nodes are interconnected via a simulated P2P network.
Expand Down
Loading