Skip to content

Commit 1054af9

Browse files
authored
fix(op): move l1block loading to verification (#1970)
* fix(op): move l1block loading to verification * clear full l1block data
1 parent ae26414 commit 1054af9

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

crates/revm/src/optimism.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ mod l1block;
77
mod precompile;
88

99
pub use handler_register::{
10-
clear, deduct_caller, end, last_frame_return, load_accounts, load_precompiles,
11-
optimism_handle_register, output, refund, reimburse_caller, reward_beneficiary, validate_env,
12-
validate_tx_against_state,
10+
clear, deduct_caller, end, last_frame_return, load_precompiles, optimism_handle_register,
11+
output, refund, reimburse_caller, reward_beneficiary, validate_env, validate_tx_against_state,
1312
};
1413
pub use l1block::{
1514
L1BlockInfo, BASE_FEE_RECIPIENT, L1_BLOCK_CONTRACT, L1_FEE_RECIPIENT, OPERATOR_FEE_RECIPIENT,

crates/revm/src/optimism/handler_register.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub fn optimism_handle_register<DB: Database, EXT>(handler: &mut EvmHandler<'_,
2828
handler.validation.tx_against_state = Arc::new(validate_tx_against_state::<SPEC, EXT, DB>);
2929
// Load additional precompiles for the given chain spec.
3030
handler.pre_execution.load_precompiles = Arc::new(load_precompiles::<SPEC, EXT, DB>);
31-
// load l1 data
32-
handler.pre_execution.load_accounts = Arc::new(load_accounts::<SPEC, EXT, DB>);
3331
// An estimated batch cost is charged from the caller and added to L1 Fee Vault.
3432
handler.pre_execution.deduct_caller = Arc::new(deduct_caller::<SPEC, EXT, DB>);
3533
// Refund is calculated differently then mainnet.
@@ -70,13 +68,22 @@ pub fn validate_env<SPEC: Spec, DB: Database>(env: &Env) -> Result<(), EVMError<
7068
pub fn validate_tx_against_state<SPEC: Spec, EXT, DB: Database>(
7169
context: &mut Context<EXT, DB>,
7270
) -> Result<(), EVMError<DB::Error>> {
73-
let env @ Env { cfg, tx, .. } = context.evm.inner.env.as_ref();
74-
7571
// No validation is needed for deposit transactions, as they are pre-verified on L1.
76-
if tx.optimism.source_hash.is_some() {
72+
if context.evm.inner.env.tx.optimism.source_hash.is_some() {
7773
return Ok(());
7874
}
7975

76+
// storage l1 block info for later use. l1_block_info is cleared after execution.
77+
if context.evm.inner.l1_block_info.is_none() {
78+
// the L1-cost fee is only computed for Optimism non-deposit transactions.
79+
let l1_block_info =
80+
crate::optimism::L1BlockInfo::try_fetch(&mut context.evm.inner.db, SPEC::SPEC_ID)
81+
.map_err(EVMError::Database)?;
82+
context.evm.inner.l1_block_info = Some(l1_block_info);
83+
}
84+
85+
let env @ Env { cfg, tx, .. } = context.evm.inner.env.as_ref();
86+
8087
// load acc
8188
let tx_caller = tx.caller;
8289
let account = context
@@ -312,25 +319,6 @@ pub fn load_precompiles<SPEC: Spec, EXT, DB: Database>() -> ContextPrecompiles<D
312319
}
313320
}
314321

315-
/// Load account (make them warm) and l1 data from database.
316-
#[inline]
317-
pub fn load_accounts<SPEC: Spec, EXT, DB: Database>(
318-
context: &mut Context<EXT, DB>,
319-
) -> Result<(), EVMError<DB::Error>> {
320-
// the L1-cost fee is only computed for Optimism non-deposit transactions.
321-
322-
if context.evm.inner.env.tx.optimism.source_hash.is_none() {
323-
let l1_block_info =
324-
crate::optimism::L1BlockInfo::try_fetch(&mut context.evm.inner.db, SPEC::SPEC_ID)
325-
.map_err(EVMError::Database)?;
326-
327-
// storage l1 block info for later use.
328-
context.evm.inner.l1_block_info = Some(l1_block_info);
329-
}
330-
331-
mainnet::load_accounts::<SPEC, EXT, DB>(context)
332-
}
333-
334322
/// Deduct max balance from caller
335323
#[inline]
336324
pub fn deduct_caller<SPEC: Spec, EXT, DB: Database>(
@@ -559,9 +547,7 @@ pub fn end<SPEC: Spec, EXT, DB: Database>(
559547
pub fn clear<EXT, DB: Database>(context: &mut Context<EXT, DB>) {
560548
// clear error and journaled state.
561549
mainnet::clear(context);
562-
if let Some(l1_block) = &mut context.evm.inner.l1_block_info {
563-
l1_block.clear_tx_l1_cost();
564-
}
550+
context.evm.inner.l1_block_info = None;
565551
}
566552

567553
#[cfg(test)]

crates/revm/src/optimism/l1block.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use revm_interpreter::Gas;
2-
31
use crate::optimism::fast_lz::flz_compress_len;
42
use crate::primitives::{address, db::Database, Address, SpecId, U256};
53
use core::ops::Mul;
4+
use revm_interpreter::Gas;
65

76
const ZERO_BYTE_COST: u64 = 4;
87
const NON_ZERO_BYTE_COST: u64 = 16;

0 commit comments

Comments
 (0)