From b159b5c945112cd70ad42e407e79e51246dd0e68 Mon Sep 17 00:00:00 2001 From: andri lim Date: Tue, 11 Dec 2018 16:23:15 +0700 Subject: [PATCH] remove lastOpCodeHasRetVal, make it simpler --- nimbus/vm/computation.nim | 9 ++++----- nimbus/vm/interpreter_dispatch.nim | 10 ++++------ nimbus/vm/transaction_tracer.nim | 11 ++++++----- nimbus/vm_types.nim | 1 - 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index db4824313..ed45c4191 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -63,7 +63,6 @@ func output*(c: BaseComputation): seq[byte] = func `output=`*(c: var BaseComputation, value: openarray[byte]) = c.rawOutput = @value - c.lastOpCodeHasRetVal = true proc outputHex*(c: BaseComputation): string = if c.shouldEraseReturnData: @@ -295,11 +294,11 @@ proc getGasRemaining*(c: BaseComputation): GasInt = proc tracingEnabled*(c: BaseComputation): bool = c.vmState.tracingEnabled -proc traceOpCodeStarted*(c: BaseComputation, op: string) = - traceOpCodeStarted(c.vmState.tracer, c, op) +proc traceOpCodeStarted*(c: BaseComputation, op: Op) = + c.vmState.tracer.traceOpCodeStarted(c, op) -proc traceOpCodeEnded*(c: BaseComputation) = - c.vmState.tracer.traceOpCodeEnded(c) +proc traceOpCodeEnded*(c: BaseComputation, op: Op) = + c.vmState.tracer.traceOpCodeEnded(c, op) proc traceError*(c: BaseComputation) = c.vmState.tracer.traceError(c) diff --git a/nimbus/vm/interpreter_dispatch.nim b/nimbus/vm/interpreter_dispatch.nim index 2b7a2123c..fb4ae3c26 100644 --- a/nimbus/vm/interpreter_dispatch.nim +++ b/nimbus/vm/interpreter_dispatch.nim @@ -197,21 +197,19 @@ proc opTableToCaseStmt(opTable: array[Op, NimNode], computation: NimNode): NimNo if BaseGasCosts[op].kind == GckFixed: quote do: if `computation`.tracingEnabled: - `computation`.traceOpCodeStarted($`asOp`) + `computation`.traceOpCodeStarted(`asOp`) `computation`.gasMeter.consumeGas(`computation`.gasCosts[`asOp`].cost, reason = $`asOp`) - `computation`.lastOpCodeHasRetVal = false `opImpl`(`computation`) if `computation`.tracingEnabled: - `computation`.traceOpCodeEnded() + `computation`.traceOpCodeEnded(`asOp`) `instr` = `computation`.code.next() else: quote do: if `computation`.tracingEnabled: - `computation`.traceOpCodeStarted($`asOp`) - `computation`.lastOpCodeHasRetVal = false + `computation`.traceOpCodeStarted(`asOp`) `opImpl`(`computation`) if `computation`.tracingEnabled: - `computation`.traceOpCodeEnded() + `computation`.traceOpCodeEnded(`asOp`) when `asOp` in {Return, Revert, SelfDestruct}: break else: diff --git a/nimbus/vm/transaction_tracer.nim b/nimbus/vm/transaction_tracer.nim index 664292102..bb5c47a03 100644 --- a/nimbus/vm/transaction_tracer.nim +++ b/nimbus/vm/transaction_tracer.nim @@ -2,7 +2,8 @@ import json, strutils, chronicles, nimcrypto, eth_common, stint, ../vm_types, memory, stack, ../db/[db_chain, state_db], - eth_trie/hexary, ./message, ranges/typedranges + eth_trie/hexary, ./message, ranges/typedranges, + ./interpreter/opcode_values logScope: topics = "vm opcode" @@ -18,14 +19,14 @@ proc initTracer*(tracer: var TransactionTracer, flags: set[TracerFlags] = {}) = tracer.trace["structLogs"] = newJArray() tracer.flags = flags -proc traceOpCodeStarted*(tracer: var TransactionTracer, c: BaseComputation, op: string) = +proc traceOpCodeStarted*(tracer: var TransactionTracer, c: BaseComputation, op: Op) = if unlikely tracer.trace.isNil: tracer.initTracer() let j = newJObject() tracer.trace["structLogs"].add(j) - j["op"] = %op.toUpperAscii + j["op"] = %(($op).toUpperAscii) j["pc"] = %(c.code.pc - 1) j["depth"] = %1 # stub j["gas"] = %c.gasMeter.gasRemaining @@ -47,7 +48,7 @@ proc traceOpCodeStarted*(tracer: var TransactionTracer, c: BaseComputation, op: mem.add(%c.memory.bytes.toOpenArray(i * chunkLen, (i + 1) * chunkLen - 1).toHex()) j["memory"] = mem -proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) = +proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation, op: Op) = let j = tracer.trace["structLogs"].elems[^1] # TODO: figure out how to get storage @@ -61,7 +62,7 @@ proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) = j["gasCost"] = %(tracer.gasRemaining - c.gasMeter.gasRemaining) - if c.lastOpCodeHasRetVal: + if op in {Return, Revert}: let returnValue = %("0x" & toHex(c.rawOutput, true)) j["returnValue"] = returnValue tracer.trace["returnValue"] = returnValue diff --git a/nimbus/vm_types.nim b/nimbus/vm_types.nim index c279ff1bb..a35cd7cc5 100644 --- a/nimbus/vm_types.nim +++ b/nimbus/vm_types.nim @@ -58,7 +58,6 @@ type opcodes*: Table[Op, proc(computation: var BaseComputation){.nimcall.}] gasCosts*: GasCosts # TODO - will be hidden at a lower layer opCodeExec*: OpcodeExecutor - lastOpCodeHasRetVal*: bool forkOverride*: Option[Fork] Error* = ref object