Skip to content

Commit

Permalink
fix(EOF): Use cfg code size limit for eofcreate (bluealloy#1606)
Browse files Browse the repository at this point in the history
* fix(eof): Use cfg code size limit for eofcreate

* Update crates/revm/src/context/inner_evm_context.rs

* simplify
  • Loading branch information
rakita authored and j75689 committed Aug 1, 2024
1 parent 5bc25e6 commit a4c91df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
10 changes: 8 additions & 2 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions crates/revm/src/context/inner_evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -125,6 +125,11 @@ impl<DB: Database> InnerEvmContext<DB> {
&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<DB::Error>> {
Expand Down Expand Up @@ -271,7 +276,7 @@ impl<DB: Database> InnerEvmContext<DB> {
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;
Expand Down Expand Up @@ -329,10 +334,7 @@ impl<DB: Database> InnerEvmContext<DB> {
// 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;
Expand All @@ -341,12 +343,7 @@ impl<DB: Database> InnerEvmContext<DB> {
// 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;
Expand Down

0 comments on commit a4c91df

Please sign in to comment.