Skip to content

Commit

Permalink
fix: fix prank cheatcode (#973)
Browse files Browse the repository at this point in the history
Correctly apply `msg.sender` prank to both transfers
and calls.
  • Loading branch information
onbjerg authored Mar 17, 2022
1 parent a175141 commit a12e9fe
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions evm/src/executor/inspector/cheatcodes/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ fn prank(
new_caller,
new_origin,
// Note: When calling a cheatcode, the depth is not increased. Because of that, the depth
// we actually want to prank at is 1 level deeper.
depth: depth + 1,
// we actually want to prank at is 1 level deeper. However, because pranks are applied
// *before* the depth is increased, we need to go a whole TWO levels deeper!
depth: depth + 2,
single_call,
};

Expand Down
67 changes: 41 additions & 26 deletions evm/src/executor/inspector/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
fn call(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CallInputs,
call: &mut CallInputs,
_: bool,
) -> (Return, Gas, Bytes) {
if call.contract == *CHEATCODE_ADDRESS {
Expand Down Expand Up @@ -116,34 +116,26 @@ where
}
}

(Return::Continue, Gas::new(call.gas_limit), Bytes::new())
}
}

fn initialize_interp(
&mut self,
interpreter: &mut Interpreter,
data: &mut EVMData<'_, DB>,
_: bool,
) -> Return {
// Apply our prank
if let Some(prank) = &self.prank {
if data.subroutine.depth() >= prank.depth &&
interpreter.contract.caller == prank.prank_caller
{
// At the target depth we set `msg.sender`
if data.subroutine.depth() == prank.depth {
interpreter.contract.caller = prank.new_caller;
}

// At the target depth, or deeper, we set `tx.origin`
if let Some(new_origin) = prank.new_origin {
data.env.tx.caller = new_origin;
// Apply our prank
if let Some(prank) = &self.prank {
if data.subroutine.depth() >= prank.depth &&
call.context.caller == prank.prank_caller
{
// At the target depth we set `msg.sender`
if data.subroutine.depth() == prank.depth {
call.context.caller = prank.new_caller;
call.transfer.source = prank.new_caller;
}

// At the target depth, or deeper, we set `tx.origin`
if let Some(new_origin) = prank.new_origin {
data.env.tx.caller = new_origin;
}
}
}
}

Return::Continue
(Return::Continue, Gas::new(call.gas_limit), Bytes::new())
}
}

fn step(&mut self, interpreter: &mut Interpreter, _: &mut EVMData<'_, DB>, _: bool) -> Return {
Expand Down Expand Up @@ -271,6 +263,29 @@ where
(status, remaining_gas, retdata)
}

fn create(
&mut self,
data: &mut EVMData<'_, DB>,
call: &mut CreateInputs,
) -> (Return, Option<Address>, Gas, Bytes) {
// Apply our prank
if let Some(prank) = &self.prank {
if data.subroutine.depth() >= prank.depth && call.caller == prank.prank_caller {
// At the target depth we set `msg.sender`
if data.subroutine.depth() == prank.depth {
call.caller = prank.new_caller;
}

// At the target depth, or deeper, we set `tx.origin`
if let Some(new_origin) = prank.new_origin {
data.env.tx.caller = new_origin;
}
}
}

(Return::Continue, None, Gas::new(call.gas_limit), Bytes::new())
}

fn create_end(
&mut self,
data: &mut EVMData<'_, DB>,
Expand Down
4 changes: 2 additions & 2 deletions evm/src/executor/inspector/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
fn call(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CallInputs,
call: &mut CallInputs,
_: bool,
) -> (Return, Gas, Bytes) {
self.enter(data.subroutine.depth() as usize, call.contract, false);
Expand Down Expand Up @@ -201,7 +201,7 @@ where
fn create(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CreateInputs,
call: &mut CreateInputs,
) -> (Return, Option<Address>, Gas, Bytes) {
// TODO: Does this increase gas cost?
data.subroutine.load_account(call.caller, data.db);
Expand Down
2 changes: 1 addition & 1 deletion evm/src/executor/inspector/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
fn call(
&mut self,
_: &mut EVMData<'_, DB>,
call: &CallInputs,
call: &mut CallInputs,
_: bool,
) -> (Return, Gas, Bytes) {
if call.contract == *HARDHAT_CONSOLE_ADDRESS {
Expand Down
4 changes: 2 additions & 2 deletions evm/src/executor/inspector/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where
fn call(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CallInputs,
call: &mut CallInputs,
is_static: bool,
) -> (Return, Gas, Bytes) {
call_inspectors!(
Expand Down Expand Up @@ -164,7 +164,7 @@ where
fn create(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CreateInputs,
call: &mut CreateInputs,
) -> (Return, Option<Address>, Gas, Bytes) {
call_inspectors!(
inspector,
Expand Down
4 changes: 2 additions & 2 deletions evm/src/executor/inspector/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
fn call(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CallInputs,
call: &mut CallInputs,
_: bool,
) -> (Return, Gas, Bytes) {
if call.contract != *HARDHAT_CONSOLE_ADDRESS {
Expand Down Expand Up @@ -119,7 +119,7 @@ where
fn create(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CreateInputs,
call: &mut CreateInputs,
) -> (Return, Option<Address>, Gas, Bytes) {
// TODO: Does this increase gas cost?
data.subroutine.load_account(call.caller, data.db);
Expand Down

0 comments on commit a12e9fe

Please sign in to comment.