nimbus-eth1/nimbus/tracer.nim

260 lines
8.9 KiB
Nim
Raw Normal View History

2018-12-03 10:54:19 +00:00
import
2018-12-03 16:22:08 +00:00
db/[db_chain, state_db, capturedb], eth_common, utils, json,
2018-12-04 11:42:55 +00:00
constants, vm_state, vm_types, transaction, p2p/executor,
2018-12-12 03:40:37 +00:00
eth_trie/db, nimcrypto, strutils, ranges, ./utils/addresses,
2018-12-12 09:41:18 +00:00
chronicles
2018-12-03 16:22:08 +00:00
proc getParentHeader(self: BaseChainDB, header: BlockHeader): BlockHeader =
self.getBlockHeader(header.parentHash)
2018-12-25 10:31:51 +00:00
proc `%`(x: openArray[byte]): JsonNode =
result = %toHex(x, false)
proc toJson(receipt: Receipt): JsonNode =
result = newJObject()
result["cumulativeGasUsed"] = %receipt.cumulativeGasUsed
result["bloom"] = %receipt.bloom
result["logs"] = %receipt.logs
if receipt.hasStateRoot:
result["root"] = %($receipt.stateRoot)
else:
result["status"] = %receipt.status
proc dumpReceipts*(chainDB: BaseChainDB, header: BlockHeader): JsonNode =
result = newJArray()
for receipt in chainDB.getReceipts(header):
result.add receipt.toJson
2018-12-31 03:27:02 +00:00
proc toJson*(receipts: seq[Receipt]): JsonNode =
2018-12-25 10:31:51 +00:00
result = newJArray()
for receipt in receipts:
result.add receipt.toJson
2018-12-03 10:54:19 +00:00
2018-12-12 03:40:37 +00:00
proc getSender(tx: Transaction): EthAddress =
if not tx.getSender(result):
raise newException(ValueError, "Could not get sender")
proc getRecipient(tx: Transaction): EthAddress =
if tx.isContractCreation:
let sender = tx.getSender()
result = generateAddress(sender, tx.accountNonce)
else:
result = tx.to
proc captureAccount(n: JsonNode, db: AccountStateDB, address: EthAddress, name: string) =
var jaccount = newJObject()
jaccount["name"] = %name
jaccount["address"] = %($address)
2018-12-11 09:53:05 +00:00
let account = db.getAccount(address)
2018-12-12 03:40:37 +00:00
jaccount["nonce"] = %(account.nonce.toHex)
jaccount["balance"] = %(account.balance.toHex)
2018-12-11 09:53:05 +00:00
let code = db.getCode(address)
2018-12-12 09:41:18 +00:00
jaccount["codeHash"] = %(($account.codeHash).toLowerAscii)
2018-12-12 03:40:37 +00:00
jaccount["code"] = %(toHex(code.toOpenArray, true))
2018-12-12 09:41:18 +00:00
jaccount["storageRoot"] = %(($account.storageRoot).toLowerAscii)
2018-12-11 09:53:05 +00:00
var storage = newJObject()
for key, value in db.storage(address):
storage[key.dumpHex] = %(value.dumpHex)
2018-12-12 03:40:37 +00:00
jaccount["storage"] = storage
2018-12-11 09:53:05 +00:00
2018-12-12 03:40:37 +00:00
n.add jaccount
2018-12-11 09:53:05 +00:00
2018-12-12 15:18:46 +00:00
proc dumpMemoryDB*(node: JsonNode, memoryDB: TrieDatabaseRef) =
var n = newJObject()
for k, v in pairsInMemoryDB(memoryDB):
2018-12-25 10:31:51 +00:00
n[k.toHex(false)] = %v
2018-12-12 15:18:46 +00:00
node["state"] = n
2018-12-12 03:40:37 +00:00
const
senderName = "sender"
recipientName = "recipient"
minerName = "miner"
uncleName = "uncle"
2018-12-25 07:31:12 +00:00
internalTxName = "internalTx"
2018-12-11 09:53:05 +00:00
2018-12-03 10:54:19 +00:00
proc traceTransaction*(db: BaseChainDB, header: BlockHeader,
2018-12-04 11:42:55 +00:00
body: BlockBody, txIndex: int, tracerFlags: set[TracerFlags] = {}): JsonNode =
2018-12-03 16:22:08 +00:00
let
2018-12-04 01:53:21 +00:00
parent = db.getParentHeader(header)
# we add a memory layer between backend/lower layer db
2018-12-04 01:53:21 +00:00
# and capture state db snapshot during transaction execution
2018-12-03 16:22:08 +00:00
memoryDB = newMemoryDB()
captureDB = newCaptureDB(db.db, memoryDB)
captureTrieDB = trieDB captureDB
captureChainDB = newBaseChainDB(captureTrieDB, false) # prune or not prune?
2018-12-25 07:31:12 +00:00
vmState = newBaseVMState(parent, captureChainDB, tracerFlags + {EnableAccount})
2018-12-31 03:27:02 +00:00
var stateDb = vmState.accountDb
2018-12-26 03:34:16 +00:00
2018-12-12 15:18:46 +00:00
if header.txRoot == BLANK_ROOT_HASH: return newJNull()
2018-12-04 01:53:21 +00:00
assert(body.transactions.calcTxRoot == header.txRoot)
2018-12-03 10:54:19 +00:00
assert(body.transactions.len != 0)
2018-12-11 09:53:05 +00:00
var
gasUsed: GasInt
2018-12-12 09:41:18 +00:00
before = newJArray()
after = newJArray()
2018-12-11 09:53:05 +00:00
stateDiff = %{"before": before, "after": after}
2018-12-26 03:34:16 +00:00
beforeRoot: Hash256
2018-12-11 09:53:05 +00:00
2018-12-03 10:54:19 +00:00
for idx, tx in body.transactions:
2018-12-12 03:40:37 +00:00
let sender = tx.getSender
let recipient = tx.getRecipient
2018-12-11 09:53:05 +00:00
2018-12-12 03:40:37 +00:00
if idx == txIndex:
vmState.enableTracing()
before.captureAccount(stateDb, sender, senderName)
before.captureAccount(stateDb, recipient, recipientName)
before.captureAccount(stateDb, header.coinbase, minerName)
2018-12-25 10:31:51 +00:00
stateDiff["beforeRoot"] = %($stateDb.rootHash)
2018-12-26 03:34:16 +00:00
beforeRoot = stateDb.rootHash
2018-12-11 09:53:05 +00:00
2018-12-31 03:27:02 +00:00
let txFee = processTransaction(tx, sender, vmState)
2018-12-12 09:41:18 +00:00
stateDb.addBalance(header.coinbase, txFee)
2018-12-12 03:40:37 +00:00
if idx == txIndex:
gasUsed = (txFee div tx.gasPrice.u256).truncate(GasInt)
after.captureAccount(stateDb, sender, senderName)
after.captureAccount(stateDb, recipient, recipientName)
after.captureAccount(stateDb, header.coinbase, minerName)
vmState.removeTracedAccounts(sender, recipient, header.coinbase)
2018-12-25 10:31:51 +00:00
stateDiff["afterRoot"] = %($stateDb.rootHash)
2018-12-12 03:40:37 +00:00
break
2018-12-03 10:54:19 +00:00
2018-12-25 07:31:12 +00:00
# internal transactions:
2018-12-26 03:34:16 +00:00
var stateBefore = newAccountStateDB(captureTrieDB, beforeRoot, db.pruneTrie)
2018-12-25 07:31:12 +00:00
for idx, acc in tracedAccountsPairs(vmState):
before.captureAccount(stateBefore, acc, internalTxName & $idx)
for idx, acc in tracedAccountsPairs(vmState):
after.captureAccount(stateDb, acc, internalTxName & $idx)
2018-12-03 16:22:08 +00:00
result = vmState.getTracingResult()
result["gas"] = %gasUsed
2018-12-25 10:31:51 +00:00
if TracerFlags.DisableStateDiff notin tracerFlags:
result["stateDiff"] = stateDiff
2018-12-03 16:22:08 +00:00
2018-12-04 01:53:21 +00:00
# now we dump captured state db
if TracerFlags.DisableState notin tracerFlags:
2018-12-12 15:18:46 +00:00
result.dumpMemoryDB(memoryDB)
2018-12-11 10:05:49 +00:00
2018-12-12 15:18:46 +00:00
proc dumpBlockState*(db: BaseChainDB, header: BlockHeader, body: BlockBody, dumpState = false): JsonNode =
2018-12-12 03:40:37 +00:00
let
parent = db.getParentHeader(header)
memoryDB = newMemoryDB()
captureDB = newCaptureDB(db.db, memoryDB)
captureTrieDB = trieDB captureDB
captureChainDB = newBaseChainDB(captureTrieDB, false)
# we only need stack dump if we want to scan for internal transaction address
2018-12-25 07:31:12 +00:00
vmState = newBaseVMState(parent, captureChainDB, {EnableTracing, DisableMemory, DisableStorage, EnableAccount})
2018-12-12 03:40:37 +00:00
var
2018-12-12 09:41:18 +00:00
before = newJArray()
after = newJArray()
2018-12-12 03:40:37 +00:00
stateBefore = newAccountStateDB(captureTrieDB, parent.stateRoot, db.pruneTrie)
stateAfter = newAccountStateDB(captureTrieDB, header.stateRoot, db.pruneTrie)
2018-12-12 09:41:18 +00:00
for idx, tx in body.transactions:
2018-12-12 03:40:37 +00:00
let sender = tx.getSender
let recipient = tx.getRecipient
2018-12-12 09:41:18 +00:00
before.captureAccount(stateBefore, sender, senderName & $idx)
before.captureAccount(stateBefore, recipient, recipientName & $idx)
2018-12-12 03:40:37 +00:00
before.captureAccount(stateBefore, header.coinbase, minerName)
for idx, uncle in body.uncles:
before.captureAccount(stateBefore, uncle.coinbase, uncleName & $idx)
discard captureChainDB.processBlock(parent, header, body, vmState)
2018-12-12 09:41:18 +00:00
for idx, tx in body.transactions:
2018-12-12 03:40:37 +00:00
let sender = tx.getSender
let recipient = tx.getRecipient
2018-12-12 09:41:18 +00:00
after.captureAccount(stateAfter, sender, senderName & $idx)
after.captureAccount(stateAfter, recipient, recipientName & $idx)
vmState.removeTracedAccounts(sender, recipient)
2018-12-12 03:40:37 +00:00
after.captureAccount(stateAfter, header.coinbase, minerName)
vmState.removeTracedAccounts(header.coinbase)
2018-12-12 03:40:37 +00:00
for idx, uncle in body.uncles:
after.captureAccount(stateAfter, uncle.coinbase, uncleName & $idx)
vmState.removeTracedAccounts(uncle.coinbase)
2018-12-12 03:40:37 +00:00
2018-12-25 07:31:12 +00:00
# internal transactions:
for idx, acc in tracedAccountsPairs(vmState):
before.captureAccount(stateBefore, acc, internalTxName & $idx)
for idx, acc in tracedAccountsPairs(vmState):
after.captureAccount(stateAfter, acc, internalTxName & $idx)
2018-12-12 03:40:37 +00:00
result = %{"before": before, "after": after}
2018-12-12 04:16:40 +00:00
2018-12-12 15:18:46 +00:00
if dumpState:
result.dumpMemoryDB(memoryDB)
2018-12-12 04:16:40 +00:00
proc traceBlock*(db: BaseChainDB, header: BlockHeader, body: BlockBody, tracerFlags: set[TracerFlags] = {}): JsonNode =
let
parent = db.getParentHeader(header)
memoryDB = newMemoryDB()
captureDB = newCaptureDB(db.db, memoryDB)
captureTrieDB = trieDB captureDB
captureChainDB = newBaseChainDB(captureTrieDB, false)
vmState = newBaseVMState(parent, captureChainDB, tracerFlags + {EnableTracing})
2018-12-12 15:18:46 +00:00
if header.txRoot == BLANK_ROOT_HASH: return newJNull()
2018-12-12 04:16:40 +00:00
assert(body.transactions.calcTxRoot == header.txRoot)
assert(body.transactions.len != 0)
var gasUsed = GasInt(0)
for tx in body.transactions:
let
sender = tx.getSender
2018-12-31 03:27:02 +00:00
txFee = processTransaction(tx, sender, vmState)
2018-12-12 04:16:40 +00:00
gasUsed = gasUsed + (txFee div tx.gasPrice.u256).truncate(GasInt)
result = vmState.getTracingResult()
2018-12-12 04:31:53 +00:00
result["gas"] = %gasUsed
2018-12-12 15:18:46 +00:00
if TracerFlags.DisableState notin tracerFlags:
result.dumpMemoryDB(memoryDB)
2018-12-25 10:31:51 +00:00
proc traceTransactions*(chainDB: BaseChainDB, header: BlockHeader, blockBody: BlockBody): JsonNode =
result = newJArray()
for i in 0 ..< blockBody.transactions.len:
result.add traceTransaction(chainDB, header, blockBody, i, {DisableState})
proc dumpDebuggingMetaData*(chainDB: BaseChainDB, header: BlockHeader, blockBody: BlockBody, receipts: seq[Receipt]) =
let
blockNumber = header.blockNumber
var
memoryDB = newMemoryDB()
captureDB = newCaptureDB(chainDB.db, memoryDB)
captureTrieDB = trieDB captureDB
captureChainDB = newBaseChainDB(captureTrieDB, false)
let
txTraces = traceTransactions(captureChainDB, header, blockBody)
stateDump = dumpBlockState(captureChainDB, header, blockBody)
blockTrace = traceBlock(captureChainDB, header, blockBody, {DisableState})
receipts = toJson(receipts)
var metaData = %{
"blockNumber": %blockNumber.toHex,
"txTraces": txTraces,
"stateDump": stateDump,
"blockTrace": blockTrace,
"receipts": receipts
}
metaData.dumpMemoryDB(memoryDB)
# this is a placeholder until premix debugging tool is ready
writeFile("debug_meta_data.json", metaData.pretty())