Built-in tracers
Last updated
Last updated
Etn-sc comes bundled with a choice of tracers that can be invoked via the tracing API. Some of these built-in tracers are implemented natively in Go, and others in Javascript. The default tracer is the opcode logger (otherwise known as struct logger) which is the default tracer for all the methods. Other tracers have to be specified by passing their name to the tracer parameter in the API call.
The struct logger (aka opcode logger) is a native Go tracer which executes a transaction and emits the opcode and execution context at every step. This is the tracer that will be used when no name is passed to the API, e.g. debug.traceTransaction(<txhash>)
. The following information is emitted at each step:
FIELD | TYPE | DESCRIPTION |
---|---|---|
Note that the fields memory
, stack
, returnData
, and storage
have dynamic size and depending on the exact transaction they could grow large in size. This is specially true for memory which could blow up the trace size. It is recommended to keep them disabled unless they are explicitly required for a given use case.
It is also possible to configure the trace by passing Boolean (true/false) values for four parameters that tweak the verbosity of the trace. By default, the EVM memory and Return data are not reported but the EVM stack and EVM storage are to report the maximum amount of data:
An example call:
Return:
The following tracers are implement in Go. This means they are much more performant than other tracers that are written in Javascript. The tracers are selected by passing their name to the tracer parameter when invoking a tracing API method, e.g. debug.traceTransaction(<txhash>, { tracer: 'callTracer' })
.
Solidity contract functions are addressed using the first four byte of the Keccak-256 hash of their signature. Therefore when calling the function of a contract, the caller must send this function selector as well as the ABI-encoded arguments as call data.
The 4byteTracer
collects the function selectors of every function executed in the lifetime of a transaction, along with the size of the supplied call data. The result is a map[string]int
where the keys are SELECTOR-CALLDATASIZE
and the values are number of occurrences of this key. For example:
Example call:
Return:
The callTracer
tracks all the call frames executed during a transaction, including depth 0. The result will be a nested list of call frames, resembling how EVM works. They form a tree with the top-level call at root and sub-calls as children of the higher levels. Each call frame has the following fields:
Example Call:
Return:
Things to note about the call tracer:
Calls to precompiles are also included in the result
In case a frame reverts, the field output
will contain the raw return data
In case the top level frame reverts, its revertReason
field will contain the parsed reason of revert as returned by the Solidity contract
callTracer
accepts two options:
onlyTopCall
: true
instructs the tracer to only process the main (top-level) call and none of the sub-calls. This avoids extra processing for each call frame if only the top-level call info are required.
withLog
: true
instructs the tracer to also collect the logs emitted during each call.
Example invokation with the onlyTopCall
flag:
The prestate tracer has two modes: prestate
and diff
. The prestate
mode returns the accounts necessary to execute a given transaction. diff
mode returns the differences between the transaction's pre and post-state (i.e. what changed because the transaction happened). The prestateTracer
defaults to prestate
mode. It reexecutes the given transaction and tracks every part of state that is touched. This is similar to the concept of a stateless witness, the difference being this tracer doesn't return any cryptographic proof, rather only the trie leaves. The result is an object. The keys are addresses of accounts. The value is an object with the following fields:
In diff
mode the result object will contain a pre
and a post
object:
Any read-only access is omitted completely from the result. This mode is only concerned with state modifications.
In pre
you will find the state of an account before the tx started, and in post its state after tx execution finished.
post
will contain only the modified fields. e.g. if nonce
of an account hasn't changed it will be omitted from post.
Deletion (i.e. account selfdestruct, or storage clearing) will be signified by inclusion in pre
and omission in post
.
Insertion (i.e. account creation or new slots) will be signified by omission in pre
and inclusion in post
.
To run this tracer in diff
mode, pass tracerConfig: {diffMode: true}
in the API call.
Example of prestate
mode:
Return:
Return (same call with {diffMode: True}
):
This tracer is noop. It returns an empty object and is only meant for testing the setup.
There are also a set of tracers written in Javascript. These are less performant than the Go native tracers because of overheads associated with interpreting the Javascript in Etn-sc's Go environment.
bigramTracer
counts the opcode bigrams, i.e. how many times 2 opcodes were executed one after the other.
Example:
Returns:
evmdisTracer returns sufficient information from a trace to perform evmdis-style disassembly
Example:
Returns:
opcountTracer
counts the total number of opcodes executed and simply returns the number.
Example:
Returns:
trigramTracer
counts the opcode trigrams. Trigrams are the possible combinations of three opcodes this tracer reports how many times each combination is seen during execution.
Example:
Returns:
unigramTracer
counts the frequency of occurrence of each opcode.
Example:
Returns:
It is possible to give temporary state modifications to Etn-sc in order to simulate the effects of eth_call
. For example, some new bytecode could be deployed to some address temporarily just for the duration of the execution and then a transaction interacting with that address can be traced. This can be used for scenario testing or determining the outcome of some hypothetical transaction before executing for real.
To do this, the tracer is written as normal, but the parameter stateOverrides
is passed an address and some bytecode.
This page showed how to use the tracers that come bundled with Etn-sc. There are a set written in Go and a set written in Javascript. They are invoked by passing their names when calling an API method. State overrides can be used in combination with tracers to examine precisely what the EVM will do in some hypothetical scenarios.
FIELD | TYPE | DESCRIPTION |
---|---|---|
FIELD | TYPE | DESCRIPTION |
---|---|---|
pc
uint64
program counter
op
byte
opcode to be executed
gas
uint64
remaining gas
gasCost
uint64
cost for executing op
memory
[]byte
EVM memory. Enabled via enableMemory
memSize
int
Size of memory
stack
[]uint256
EVM stack. Disabled via disableStack
returnData
[]byte
Last call's return data. Enabled via enableReturnData
storage
map[hash]hash
Storage slots of current contract read from and written to. Only emitted for SLOAD and SSTORE. Disabled via disableStorage
depth
int
Current call depth
refund
uint64
Refund counter
error
string
Error message if any
type
string
CALL or CREATE
from
string
address
to
string
address
value
string
hex-encoded amount of value transfer
gas
string
hex-encoded gas provided for call
gasUsed
string
hex-encoded gas used during call
input
string
call data
output
string
return data
error
string
error, if any
revertReason
string
Solidity revert reason, if any
calls
[]callframe
list of sub-calls
balance
string
balance in Wei
nonce
uint64
nonce
code
string
hex-encoded bytecode
storage
map[string]string
storage slots of the contract