nimbus-eth1/nimbus/vm/transaction_tracer.nim

48 lines
1.4 KiB
Nim
Raw Normal View History

2018-10-28 12:11:19 +00:00
import
json, strutils,
eth_common, stint, byteutils,
../vm_types, memory, stack
2018-12-03 10:54:19 +00:00
proc initTracer*(tracer: var TransactionTracer, flags: set[TracerFlags] = {}) =
tracer.trace = newJObject()
tracer.trace["structLogs"] = newJArray()
tracer.flags = flags
2018-10-28 12:11:19 +00:00
2018-12-03 10:54:19 +00:00
proc traceOpCodeStarted*(tracer: var TransactionTracer, c: BaseComputation, op: string) =
if unlikely tracer.trace.isNil:
tracer.initTracer()
2018-10-28 12:11:19 +00:00
let j = newJObject()
2018-12-03 10:54:19 +00:00
tracer.trace["structLogs"].add(j)
2018-10-28 12:11:19 +00:00
j["op"] = %op.toUpperAscii
j["pc"] = %(c.code.pc - 1)
j["depth"] = %1 # stub
j["gas"] = %c.gasMeter.gasRemaining
2018-12-03 10:54:19 +00:00
tracer.gasRemaining = c.gasMeter.gasRemaining
2018-10-28 12:11:19 +00:00
# log stack
2018-12-03 10:54:19 +00:00
if TracerFlags.DisableStack notin tracer.flags:
let st = newJArray()
for v in c.stack.values:
st.add(%v.dumpHex())
j["stack"] = st
2018-10-28 12:11:19 +00:00
# log memory
2018-12-03 10:54:19 +00:00
if TracerFlags.DisableMemory notin tracer.flags:
let mem = newJArray()
const chunkLen = 32
let numChunks = c.memory.len div chunkLen
for i in 0 ..< numChunks:
mem.add(%c.memory.bytes.toOpenArray(i * chunkLen, (i + 1) * chunkLen - 1).toHex())
j["memory"] = mem
2018-10-28 12:11:19 +00:00
2018-12-03 10:54:19 +00:00
# TODO: log storage
if TracerFlags.DisableStorage notin tracer.flags:
let storage = newJArray()
j["storage"] = storage
2018-10-28 12:11:19 +00:00
2018-12-03 10:54:19 +00:00
proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) =
let j = tracer.trace["structLogs"].elems[^1]
j["gasCost"] = %(tracer.gasRemaining - c.gasMeter.gasRemaining)