2018-10-28 12:11:19 +00:00
|
|
|
import
|
2019-02-06 10:17:35 +00:00
|
|
|
json, strutils, sets, hashes,
|
2019-02-05 19:15:50 +00:00
|
|
|
chronicles, nimcrypto, eth/common, stint,
|
2018-12-04 08:53:03 +00:00
|
|
|
../vm_types, memory, stack, ../db/[db_chain, state_db],
|
2019-02-05 19:15:50 +00:00
|
|
|
eth/trie/hexary, ./message, ranges/typedranges,
|
2018-12-11 09:23:15 +00:00
|
|
|
./interpreter/opcode_values
|
2018-10-28 12:11:19 +00:00
|
|
|
|
2018-12-11 23:26:08 +00:00
|
|
|
logScope:
|
|
|
|
topics = "vm opcode"
|
|
|
|
|
2019-02-06 10:17:35 +00:00
|
|
|
proc hash*(x: Uint256): Hash =
|
|
|
|
result = hash(x.toByteArrayBE)
|
|
|
|
|
2018-12-03 10:54:19 +00:00
|
|
|
proc initTracer*(tracer: var TransactionTracer, flags: set[TracerFlags] = {}) =
|
|
|
|
tracer.trace = newJObject()
|
2018-12-03 16:22:08 +00:00
|
|
|
|
|
|
|
# make appear at the top of json object
|
|
|
|
tracer.trace["gas"] = %0
|
|
|
|
tracer.trace["failed"] = %false
|
|
|
|
tracer.trace["returnValue"] = %""
|
|
|
|
|
2018-12-03 10:54:19 +00:00
|
|
|
tracer.trace["structLogs"] = newJArray()
|
|
|
|
tracer.flags = flags
|
2019-01-01 04:06:33 +00:00
|
|
|
tracer.accounts = initSet[EthAddress]()
|
2019-02-16 12:15:52 +00:00
|
|
|
tracer.storageKeys = @[]
|
|
|
|
|
2019-02-25 13:02:16 +00:00
|
|
|
proc prepare*(tracer: var TransactionTracer, compDepth: int) =
|
2019-02-27 02:27:26 +00:00
|
|
|
# this uncommon arragement is intentional
|
|
|
|
# compDepth will be varying up and down: 1,2,3,4,3,3,2,2,1
|
|
|
|
# see issue #245 and PR #247 discussion
|
2019-02-17 00:31:11 +00:00
|
|
|
if compDepth >= tracer.storageKeys.len:
|
|
|
|
let prevLen = tracer.storageKeys.len
|
2019-02-16 12:15:52 +00:00
|
|
|
tracer.storageKeys.setLen(compDepth + 1)
|
2019-02-25 13:02:16 +00:00
|
|
|
for i in prevLen ..< tracer.storageKeys.len - 1:
|
2019-02-17 00:31:11 +00:00
|
|
|
tracer.storageKeys[i] = initSet[Uint256]()
|
2019-02-16 12:15:52 +00:00
|
|
|
|
2019-02-25 13:02:16 +00:00
|
|
|
tracer.storageKeys[compDepth] = initSet[Uint256]()
|
|
|
|
|
|
|
|
proc rememberStorageKey(tracer: var TransactionTracer, compDepth: int, key: Uint256) =
|
2019-02-16 12:15:52 +00:00
|
|
|
tracer.storageKeys[compDepth].incl key
|
|
|
|
|
|
|
|
iterator storage(tracer: TransactionTracer, compDepth: int): Uint256 =
|
|
|
|
assert compDepth >= 0 and compDepth < tracer.storageKeys.len
|
|
|
|
for key in tracer.storageKeys[compDepth]:
|
|
|
|
yield key
|
2018-10-28 12:11:19 +00:00
|
|
|
|
2019-02-21 08:17:43 +00:00
|
|
|
proc traceOpCodeStarted*(tracer: var TransactionTracer, c: BaseComputation, op: Op): int =
|
2018-12-03 10:54:19 +00:00
|
|
|
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
|
|
|
|
2018-12-11 09:23:15 +00:00
|
|
|
j["op"] = %(($op).toUpperAscii)
|
2018-10-28 12:11:19 +00:00
|
|
|
j["pc"] = %(c.code.pc - 1)
|
2019-02-15 02:23:36 +00:00
|
|
|
j["depth"] = %(c.msg.depth + 1)
|
2018-10-28 12:11:19 +00:00
|
|
|
j["gas"] = %c.gasMeter.gasRemaining
|
|
|
|
|
|
|
|
# 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-25 07:31:12 +00:00
|
|
|
if TracerFlags.EnableAccount in tracer.flags:
|
|
|
|
case op
|
|
|
|
of Call, CallCode, DelegateCall, StaticCall:
|
2019-02-23 13:08:59 +00:00
|
|
|
if c.stack.values.len > 2:
|
|
|
|
tracer.accounts.incl c.stack[^2, EthAddress]
|
2019-01-01 04:06:33 +00:00
|
|
|
of ExtCodeCopy, ExtCodeSize, Balance, SelfDestruct:
|
2019-02-23 13:08:59 +00:00
|
|
|
if c.stack.values.len > 1:
|
|
|
|
tracer.accounts.incl c.stack[^1, EthAddress]
|
2018-12-25 07:31:12 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2019-02-06 10:17:35 +00:00
|
|
|
if TracerFlags.DisableStorage notin tracer.flags:
|
|
|
|
if op == Sstore:
|
2019-02-23 13:08:59 +00:00
|
|
|
if c.stack.values.len > 1:
|
|
|
|
tracer.rememberStorageKey(c.msg.depth, c.stack[^1, Uint256])
|
2019-02-06 10:17:35 +00:00
|
|
|
|
2019-02-21 08:17:43 +00:00
|
|
|
result = tracer.trace["structLogs"].len - 1
|
|
|
|
|
|
|
|
proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation, op: Op, lastIndex: int) =
|
|
|
|
let j = tracer.trace["structLogs"].elems[lastIndex]
|
2018-12-04 08:13:35 +00:00
|
|
|
|
2018-12-03 16:22:08 +00:00
|
|
|
# TODO: figure out how to get storage
|
2018-12-11 23:26:08 +00:00
|
|
|
# when contract execution interrupted by exception
|
2018-12-03 10:54:19 +00:00
|
|
|
if TracerFlags.DisableStorage notin tracer.flags:
|
2018-12-03 16:22:08 +00:00
|
|
|
var storage = newJObject()
|
2019-02-16 12:15:52 +00:00
|
|
|
if c.msg.depth < tracer.storageKeys.len:
|
|
|
|
var stateDB = c.vmState.accountDb
|
|
|
|
for key in tracer.storage(c.msg.depth):
|
|
|
|
let (value, _) = stateDB.getStorage(c.msg.storageAddress, key)
|
|
|
|
storage[key.dumpHex] = %(value.dumpHex)
|
|
|
|
j["storage"] = storage
|
2018-10-28 12:11:19 +00:00
|
|
|
|
2019-02-21 08:17:43 +00:00
|
|
|
let gasRemaining = j["gas"].getInt()
|
|
|
|
j["gasCost"] = %(gasRemaining - c.gasMeter.gasRemaining)
|
2018-12-03 16:22:08 +00:00
|
|
|
|
2018-12-11 09:23:15 +00:00
|
|
|
if op in {Return, Revert}:
|
2018-12-04 10:56:16 +00:00
|
|
|
let returnValue = %("0x" & toHex(c.rawOutput, true))
|
|
|
|
j["returnValue"] = returnValue
|
|
|
|
tracer.trace["returnValue"] = returnValue
|
2018-12-04 08:53:03 +00:00
|
|
|
|
2018-12-11 23:26:08 +00:00
|
|
|
trace "Op", json = j.pretty()
|
|
|
|
|
2018-12-03 16:22:08 +00:00
|
|
|
proc traceError*(tracer: var TransactionTracer, c: BaseComputation) =
|
|
|
|
let j = tracer.trace["structLogs"].elems[^1]
|
|
|
|
|
|
|
|
# TODO: figure out how to get gasCost
|
|
|
|
# when contract execution failed before traceOpCodeEnded called
|
|
|
|
# because exception raised
|
|
|
|
#j["gasCost"] = %
|
|
|
|
|
|
|
|
j["error"] = %(c.error.info)
|
|
|
|
tracer.trace["failed"] = %true
|
2018-12-11 23:26:08 +00:00
|
|
|
|
|
|
|
trace "Error", json = j.pretty()
|