Skip to content

Commit

Permalink
revm: Return bytes in Create calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ngotchac committed Dec 9, 2022
1 parent 90fe01e commit 37a6032
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
17 changes: 9 additions & 8 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,13 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
// Host error if present on execution\
let (ret, address, gas, out) = match exit_reason {
return_ok!() => {
let b = Bytes::new();
// if ok, check contract creation limit and calculate gas deduction on output len.
let mut bytes = interpreter.return_value();

// EIP-3541: Reject new contract code starting with the 0xEF byte
if GSPEC::enabled(LONDON) && !bytes.is_empty() && bytes.first() == Some(&0xEF) {
self.data.journaled_state.checkpoint_revert(checkpoint);
return (Return::CreateContractWithEF, ret, interpreter.gas, b);
return (Return::CreateContractWithEF, ret, interpreter.gas, bytes);
}

// EIP-170: Contract code size limit
Expand All @@ -511,7 +510,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
&& bytes.len() > self.data.env.cfg.limit_contract_code_size.unwrap_or(0x6000)
{
self.data.journaled_state.checkpoint_revert(checkpoint);
return (Return::CreateContractLimit, ret, interpreter.gas, b);
return (Return::CreateContractLimit, ret, interpreter.gas, bytes);
}
if crate::USE_GAS {
let gas_for_code = bytes.len() as u64 * crate::gas::CODEDEPOSIT;
Expand All @@ -522,7 +521,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
// creation fails (i.e. goes out-of-gas) rather than leaving an empty contract.
if GSPEC::enabled(HOMESTEAD) {
self.data.journaled_state.checkpoint_revert(checkpoint);
return (Return::OutOfGas, ret, interpreter.gas, b);
return (Return::OutOfGas, ret, interpreter.gas, bytes);
} else {
bytes = Bytes::new();
}
Expand All @@ -532,15 +531,17 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
self.data.journaled_state.checkpoint_commit();
// Do analasis of bytecode streight away.
let bytecode = match self.data.env.cfg.perf_analyse_created_bytecodes {
AnalysisKind::Raw => Bytecode::new_raw(bytes),
AnalysisKind::Check => Bytecode::new_raw(bytes).to_checked(),
AnalysisKind::Analyse => Bytecode::new_raw(bytes).to_analysed::<GSPEC>(),
AnalysisKind::Raw => Bytecode::new_raw(bytes.clone()),
AnalysisKind::Check => Bytecode::new_raw(bytes.clone()).to_checked(),
AnalysisKind::Analyse => {
Bytecode::new_raw(bytes.clone()).to_analysed::<GSPEC>()
}
};

self.data
.journaled_state
.set_code(created_address, bytecode);
(Return::Continue, ret, interpreter.gas, b)
(Return::Continue, ret, interpreter.gas, bytes)
}
_ => {
self.data.journaled_state.checkpoint_revert(checkpoint);
Expand Down
7 changes: 6 additions & 1 deletion crates/revm/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@ pub fn create<const IS_CREATE2: bool, SPEC: Spec>(
};

let (return_reason, address, gas, return_data) = host.create(&mut create_input);
interpreter.return_data_buffer = return_data;
interpreter.return_data_buffer = match return_reason {
// Save data to return data buffer if the create reverted
return_revert!() => return_data,
// Otherwise clear it
_ => Bytes::new(),
};

match return_reason {
return_ok!() => {
Expand Down

0 comments on commit 37a6032

Please sign in to comment.