Skip to content

Commit

Permalink
fix: emit console.log events on nested calls (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
0129general committed Nov 28, 2021
1 parent b776bb7 commit cfac4a8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
6 changes: 5 additions & 1 deletion evm-adapters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ mod test_helpers {
let paths =
ProjectPathsConfig::builder().root("testdata").sources("testdata").build().unwrap();
let project = Project::builder().paths(paths).ephemeral().no_artifacts().build().unwrap();
project.compile().unwrap().output()
let res = project.compile().unwrap();
if res.has_compiler_errors() {
panic!("{}", res);
}
res.output()
});

pub fn can_call_vm_directly<S, E: Evm<S>>(mut evm: E, compiled: CompactContractRef) {
Expand Down
38 changes: 36 additions & 2 deletions evm-adapters/src/sputnik/cheatcodes/cheatcode_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl<'a, 'b, B: Backend, P: PrecompileSet> CheatcodeStackExecutor<'a, 'b, B, P>
}
}

// NB: This function is copy-pasted from uptream's call_inner
// NB: This function is copy-pasted from uptream's create_inner
fn create_inner(
&mut self,
caller: H160,
Expand Down Expand Up @@ -722,7 +722,16 @@ impl<'a, 'b, B: Backend, P: PrecompileSet> Handler for CheatcodeStackExecutor<'a
if code_address == *CHEATCODE_ADDRESS {
self.apply_cheatcode(input, transfer, target_gas)
} else {
self.handler.call(code_address, transfer, input, target_gas, is_static, context)
self.call_inner(
code_address,
transfer,
input,
target_gas,
is_static,
true,
true,
context,
)
}
}

Expand Down Expand Up @@ -899,6 +908,31 @@ mod tests {
assert_eq!(logs, expected);
}

#[test]
fn logs_external_contract() {
let config = Config::istanbul();
let vicinity = new_vicinity();
let backend = new_backend(&vicinity, Default::default());
let gas_limit = 10_000_000;
let precompiles = PRECOMPILES_MAP.clone();
let mut evm =
Executor::new_with_cheatcodes(backend, gas_limit, &config, &precompiles, true);

let compiled = COMPILED.find("DebugLogs").expect("could not find contract");
let (addr, _, _, _) =
evm.deploy(Address::zero(), compiled.bin.unwrap().clone(), 0.into()).unwrap();

// after the evm call is done, we call `logs` and print it all to the user
let (_, _, _, logs) = evm
.call::<(), _, _>(Address::zero(), addr, "test_log_elsewhere()", (), 0.into())
.unwrap();
let expected = ["0x1111111111111111111111111111111111111111", "Hi"]
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>();
assert_eq!(logs, expected);
}

#[test]
fn cheatcodes() {
let config = Config::istanbul();
Expand Down
12 changes: 12 additions & 0 deletions evm-adapters/testdata/DebugLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ contract DebugLogs is DSTest {
emit log_named_bytes("key", hex"4567");
emit log_named_string("key", "lol");
}

function test_log_elsewhere() public {
OtherContract otherContract = new OtherContract();
otherContract.test_log();
}
}

contract OtherContract is DSTest {
function test_log() public {
emit log_address(0x1111111111111111111111111111111111111111);
emit log("Hi");
}
}

0 comments on commit cfac4a8

Please sign in to comment.