Skip to content

Commit

Permalink
feat: show hardhat console.logs in traces (#1367)
Browse files Browse the repository at this point in the history
* feat: show hardhat console.logs in traces

* chore: better rust
  • Loading branch information
mds1 authored Apr 21, 2022
1 parent 918f9b3 commit 7d8372f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
37 changes: 15 additions & 22 deletions evm/src/executor/inspector/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{
executor::{
inspector::utils::{gas_used, get_create_address},
HARDHAT_CONSOLE_ADDRESS,
},
executor::inspector::utils::{gas_used, get_create_address},
trace::{
CallTrace, CallTraceArena, LogCallOrder, RawOrDecodedCall, RawOrDecodedLog,
RawOrDecodedReturnData,
Expand Down Expand Up @@ -75,15 +72,13 @@ where
call: &mut CallInputs,
_: bool,
) -> (Return, Gas, Bytes) {
if call.contract != HARDHAT_CONSOLE_ADDRESS {
self.start_trace(
data.subroutine.depth() as usize,
call.context.code_address,
call.input.to_vec(),
call.transfer.value,
call.context.scheme.into(),
);
}
self.start_trace(
data.subroutine.depth() as usize,
call.context.code_address,
call.input.to_vec(),
call.transfer.value,
call.context.scheme.into(),
);

(Return::Continue, Gas::new(call.gas_limit), Bytes::new())
}
Expand All @@ -98,20 +93,18 @@ where
fn call_end(
&mut self,
data: &mut EVMData<'_, DB>,
call: &CallInputs,
_call: &CallInputs,
gas: Gas,
status: Return,
retdata: Bytes,
_: bool,
) -> (Return, Gas, Bytes) {
if call.contract != HARDHAT_CONSOLE_ADDRESS {
self.fill_trace(
matches!(status, return_ok!()),
gas_used(data.env.cfg.spec_id, gas.spend(), gas.refunded() as u64),
retdata.to_vec(),
None,
);
}
self.fill_trace(
matches!(status, return_ok!()),
gas_used(data.env.cfg.spec_id, gas.spend(), gas.refunded() as u64),
retdata.to_vec(),
None,
);

(status, gas, retdata)
}
Expand Down
19 changes: 13 additions & 6 deletions evm/src/trace/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
RawOrDecodedReturnData,
};
use crate::{
abi::{CHEATCODE_ADDRESS, CONSOLE_ABI, HEVM_ABI},
abi::{CHEATCODE_ADDRESS, CONSOLE_ABI, HARDHAT_CONSOLE_ABI, HARDHAT_CONSOLE_ADDRESS, HEVM_ABI},
trace::{node::CallTraceNode, utils},
};
use ethers::{
Expand Down Expand Up @@ -79,6 +79,12 @@ impl CallTraceDecoder {
/// The call trace decoder always knows how to decode calls to the cheatcode address, as well
/// as DSTest-style logs.
pub fn new() -> Self {
let functions = HARDHAT_CONSOLE_ABI
.functions()
.map(|func| (func.short_signature(), vec![func.clone()]))
.chain(HEVM_ABI.functions().map(|func| (func.short_signature(), vec![func.clone()])))
.collect::<BTreeMap<[u8; 4], Vec<Function>>>();

Self {
// TODO: These are the Ethereum precompiles. We should add a way to support precompiles
// for other networks, too.
Expand Down Expand Up @@ -153,11 +159,12 @@ impl CallTraceDecoder {
]
.into(),
contracts: Default::default(),
labels: [(CHEATCODE_ADDRESS, "VM".to_string())].into(),
functions: HEVM_ABI
.functions()
.map(|func| (func.short_signature(), vec![func.clone()]))
.collect::<BTreeMap<[u8; 4], Vec<Function>>>(),
labels: [
(CHEATCODE_ADDRESS, "VM".to_string()),
(HARDHAT_CONSOLE_ADDRESS, "console".to_string()),
]
.into(),
functions,
events: CONSOLE_ABI
.events()
.map(|event| ((event.signature(), indexed_inputs(event)), vec![event.clone()]))
Expand Down

0 comments on commit 7d8372f

Please sign in to comment.