From a4c91df8b746af814434b83883d3eedec12cee85 Mon Sep 17 00:00:00 2001 From: rakita Date: Fri, 12 Jul 2024 20:49:31 +0200 Subject: [PATCH] fix(EOF): Use cfg code size limit for eofcreate (#1606) * fix(eof): Use cfg code size limit for eofcreate * Update crates/revm/src/context/inner_evm_context.rs * simplify --- crates/primitives/src/env.rs | 10 ++++++-- crates/revm/src/context/inner_evm_context.rs | 25 +++++++++----------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index d89a67e698..a1bcd5f9b8 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -6,8 +6,8 @@ pub use handler_cfg::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg}; use crate::{ calc_blob_gasprice, AccessListItem, Account, Address, Bytes, InvalidHeader, InvalidTransaction, - Spec, SpecId, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, - U256, VERSIONED_HASH_VERSION_KZG, + Spec, SpecId, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_CODE_SIZE, + MAX_INITCODE_SIZE, U256, VERSIONED_HASH_VERSION_KZG, }; use alloy_primitives::TxKind; use core::cmp::{min, Ordering}; @@ -323,6 +323,12 @@ pub struct CfgEnv { } impl CfgEnv { + /// Returns max code size from [`Self::limit_contract_code_size`] if set + /// or default [`MAX_CODE_SIZE`] value. + pub fn max_code_size(&self) -> usize { + self.limit_contract_code_size.unwrap_or(MAX_CODE_SIZE) + } + pub fn with_chain_id(mut self, chain_id: u64) -> Self { self.chain_id = chain_id; self diff --git a/crates/revm/src/context/inner_evm_context.rs b/crates/revm/src/context/inner_evm_context.rs index b2314cbb35..b447e1f17e 100644 --- a/crates/revm/src/context/inner_evm_context.rs +++ b/crates/revm/src/context/inner_evm_context.rs @@ -2,12 +2,12 @@ use crate::{ db::Database, interpreter::{ analysis::to_analysed, gas, return_ok, InstructionResult, InterpreterResult, - LoadAccountResult, SStoreResult, SelfDestructResult, MAX_CODE_SIZE, + LoadAccountResult, SStoreResult, SelfDestructResult, }, journaled_state::JournaledState, primitives::{ - AccessListItem, Account, Address, AnalysisKind, Bytecode, Bytes, EVMError, Env, Eof, - HashSet, Spec, + AccessListItem, Account, Address, AnalysisKind, Bytecode, Bytes, CfgEnv, EVMError, Env, + Eof, HashSet, Spec, SpecId::{self, *}, B256, EOF_MAGIC_BYTES, EOF_MAGIC_HASH, U256, }, @@ -125,6 +125,11 @@ impl InnerEvmContext { &mut self.env } + /// Returns reference to [`CfgEnv`]. + pub fn cfg(&self) -> &CfgEnv { + &self.env.cfg + } + /// Returns the error by replacing it with `Ok(())`, if any. #[inline] pub fn take_error(&mut self) -> Result<(), EVMError> { @@ -271,7 +276,7 @@ impl InnerEvmContext { return; } - if interpreter_result.output.len() > MAX_CODE_SIZE { + if interpreter_result.output.len() > self.cfg().max_code_size() { self.journaled_state.checkpoint_revert(journal_checkpoint); interpreter_result.result = InstructionResult::CreateContractSizeLimit; return; @@ -329,10 +334,7 @@ impl InnerEvmContext { // if ok, check contract creation limit and calculate gas deduction on output len. // // EIP-3541: Reject new contract code starting with the 0xEF byte - if SPEC::enabled(LONDON) - && !interpreter_result.output.is_empty() - && interpreter_result.output.first() == Some(&0xEF) - { + if SPEC::enabled(LONDON) && interpreter_result.output.first() == Some(&0xEF) { self.journaled_state.checkpoint_revert(journal_checkpoint); interpreter_result.result = InstructionResult::CreateContractStartingWithEF; return; @@ -341,12 +343,7 @@ impl InnerEvmContext { // EIP-170: Contract code size limit // By default limit is 0x6000 (~25kb) if SPEC::enabled(SPURIOUS_DRAGON) - && interpreter_result.output.len() - > self - .env - .cfg - .limit_contract_code_size - .unwrap_or(MAX_CODE_SIZE) + && interpreter_result.output.len() > self.cfg().max_code_size() { self.journaled_state.checkpoint_revert(journal_checkpoint); interpreter_result.result = InstructionResult::CreateContractSizeLimit;