Skip to content

Commit

Permalink
updates for latest revm
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Jun 21, 2024
1 parent 70094b7 commit 1a7a026
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 210 deletions.
15 changes: 8 additions & 7 deletions crates/anvil/src/eth/backend/mem/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use foundry_evm::{
decode::decode_console_logs,
inspectors::{LogCollector, TracingInspector},
revm::{
interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter},
interpreter::{
CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter,
},
primitives::U256,
EvmContext,
},
traces::TracingInspectorConfig,
InspectorExt,
};
use revm::interpreter::{EOFCreateInput, EOFCreateOutcome};

/// The [`revm::Inspector`] used when transacting in the evm
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -122,8 +123,8 @@ impl<DB: Database> revm::Inspector<DB> for Inspector {
fn eofcreate(
&mut self,
ecx: &mut EvmContext<DB>,
inputs: &mut EOFCreateInput,
) -> Option<EOFCreateOutcome> {
inputs: &mut EOFCreateInputs,
) -> Option<CreateOutcome> {
if let Some(tracer) = &mut self.tracer {
if let Some(out) = tracer.eofcreate(ecx, inputs) {
return Some(out);
Expand All @@ -136,9 +137,9 @@ impl<DB: Database> revm::Inspector<DB> for Inspector {
fn eofcreate_end(
&mut self,
ecx: &mut EvmContext<DB>,
inputs: &EOFCreateInput,
outcome: EOFCreateOutcome,
) -> EOFCreateOutcome {
inputs: &EOFCreateInputs,
outcome: CreateOutcome,
) -> CreateOutcome {
if let Some(tracer) = &mut self.tracer {
return tracer.eofcreate_end(ecx, inputs, outcome);
}
Expand Down
96 changes: 40 additions & 56 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
prank::Prank,
DealRecord, RecordAccess,
},
inspector::utils::{CommonCreateInput, CommonCreateOutcome, CommonEndInput, CommonEndOutcome},
inspector::utils::CommonCreateInput,
script::{Broadcast, ScriptWallets},
test::expect::{
self, ExpectedCallData, ExpectedCallTracker, ExpectedCallType, ExpectedEmit,
Expand All @@ -30,9 +30,8 @@ use foundry_evm_core::{
use itertools::Itertools;
use revm::{
interpreter::{
opcode, CallInputs, CallOutcome, CallScheme, CreateInputs, CreateOutcome, EOFCreateInput,
EOFCreateOutcome, Gas, InstructionResult, Interpreter, InterpreterAction,
InterpreterResult,
opcode, CallInputs, CallOutcome, CallScheme, CreateInputs, CreateOutcome, EOFCreateInputs,
Gas, InstructionResult, Interpreter, InterpreterAction, InterpreterResult,
},
primitives::{BlockEnv, CreateScheme},
EvmContext, InnerEvmContext, Inspector,
Expand All @@ -48,6 +47,8 @@ use std::{
sync::Arc,
};

mod utils;

macro_rules! try_or_return {
($e:expr) => {
match $e {
Expand Down Expand Up @@ -340,7 +341,7 @@ impl Cheatcodes {
&mut self,
ecx: &mut EvmContext<DB>,
mut input: Input,
) -> Option<CommonCreateOutcome>
) -> Option<CreateOutcome>
where
DB: DatabaseExt,
Input: CommonCreateInput<DB>,
Expand Down Expand Up @@ -371,15 +372,14 @@ impl Cheatcodes {
if let Err(err) =
ecx.journaled_state.load_account(broadcast.new_origin, &mut ecx.db)
{
return Some(input.create_outcome(
InterpreterResult {
return Some(CreateOutcome {
result: InterpreterResult {
result: InstructionResult::Revert,
output: Error::encode(err),
gas,
},
input.computed_created_address(),
input.return_memory_range().clone(),
));
address: None,
})
}

ecx.env.tx.caller = broadcast.new_origin;
Expand All @@ -395,7 +395,7 @@ impl Cheatcodes {
from: Some(broadcast.new_origin),
to: None,
value: Some(input.value()),
input: TransactionInput::new(input.init_code().clone()),
input: TransactionInput::new(input.init_code()),
nonce: Some(account.info.nonce),
gas: if is_fixed_gas_limit {
Some(input.gas_limit() as u128)
Expand Down Expand Up @@ -428,7 +428,7 @@ impl Cheatcodes {
oldBalance: U256::ZERO, // updated on (eof)create_end
newBalance: U256::ZERO, // updated on (eof)create_end
value: input.value(),
data: input.init_code().clone(),
data: input.init_code(),
reverted: false,
deployedCode: Bytes::new(), // updated on (eof)create_end
storageAccesses: vec![], // updated on (eof)create_end
Expand All @@ -440,14 +440,13 @@ impl Cheatcodes {
}

// common create_end functionality for both legacy and EOF.
fn create_end_common<DB, Input>(
fn create_end_common<DB>(
&mut self,
ecx: &mut EvmContext<DB>,
input: Input,
) -> CommonEndOutcome
mut outcome: CreateOutcome,
) -> CreateOutcome
where
DB: DatabaseExt,
Input: CommonEndInput,
{
let ecx = &mut ecx.inner;

Expand Down Expand Up @@ -484,19 +483,20 @@ impl Cheatcodes {
return match expect::handle_expect_revert(
true,
expected_revert.reason.as_deref(),
input.outcome_result(),
input.outcome_output().clone(),
outcome.result.result,
outcome.result.output.clone(),
) {
Ok((new_address, retdata)) => input.create_outcome(
InstructionResult::Return,
retdata.clone(),
new_address,
),
Err(err) => input.create_outcome(
InstructionResult::Revert,
Bytes::from(err.abi_encode()),
input.address(),
),
Ok((address, retdata)) => {
outcome.result.result = InstructionResult::Return;
outcome.result.output = retdata;
outcome.address = address;
outcome
}
Err(err) => {
outcome.result.result = InstructionResult::Revert;
outcome.result.output = err.abi_encode().into();
outcome
}
};
}
}
Expand All @@ -510,7 +510,7 @@ impl Cheatcodes {
recorded_account_diffs_stack.pop().expect("missing CREATE account accesses");
// Update the reverted status of all deeper calls if this call reverted, in
// accordance with EVM behavior
if input.outcome_result().is_revert() {
if outcome.result.is_revert() {
last_depth.iter_mut().for_each(|element| {
element.reverted = true;
element
Expand All @@ -529,7 +529,7 @@ impl Cheatcodes {
create_access.kind as u8,
crate::Vm::AccountAccessKind::Create as u8
);
if let Some(address) = input.address() {
if let Some(address) = outcome.address {
if let Ok((created_acc, _)) =
ecx.journaled_state.load_account(address, &mut ecx.db)
{
Expand All @@ -549,11 +549,7 @@ impl Cheatcodes {
}
}
}
input.create_outcome(
input.outcome_result(),
input.outcome_output().clone(),
input.address(),
)
outcome
}
}

Expand Down Expand Up @@ -1152,10 +1148,7 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
ecx: &mut EvmContext<DB>,
call: &mut CreateInputs,
) -> Option<CreateOutcome> {
self.create_common(ecx, call).and_then(|outcome| match outcome {
CommonCreateOutcome::Create(outcome) => Some(outcome),
_ => None,
})
self.create_common(ecx, call)
}

fn create_end(
Expand All @@ -1164,33 +1157,24 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
_call: &CreateInputs,
outcome: CreateOutcome,
) -> CreateOutcome {
match self.create_end_common(ecx, outcome.clone()) {
CommonEndOutcome::Create(outcome) => outcome,
_ => outcome, // This case should never happen
}
self.create_end_common(ecx, outcome)
}

fn eofcreate(
&mut self,
ecx: &mut EvmContext<DB>,
call: &mut EOFCreateInput,
) -> Option<EOFCreateOutcome> {
self.create_common(ecx, call).and_then(|outcome| match outcome {
CommonCreateOutcome::EOFCreate(outcome) => Some(outcome),
_ => None,
})
call: &mut EOFCreateInputs,
) -> Option<CreateOutcome> {
self.create_common(ecx, call)
}

fn eofcreate_end(
&mut self,
ecx: &mut EvmContext<DB>,
_call: &EOFCreateInput,
outcome: EOFCreateOutcome,
) -> EOFCreateOutcome {
match self.create_end_common(ecx, outcome.clone()) {
CommonEndOutcome::EOFCreate(outcome) => outcome,
_ => outcome, // This case should never happen
}
_call: &EOFCreateInputs,
outcome: CreateOutcome,
) -> CreateOutcome {
self.create_end_common(ecx, outcome)
}
}

Expand Down
Loading

0 comments on commit 1a7a026

Please sign in to comment.