nimbus-eth1/nimbus/tracer.nim

456 lines
13 KiB
Nim
Raw Normal View History

2023-11-01 03:32:09 +00:00
# Nimbus
# Copyright (c) 2019-2024 Status Research & Development GmbH
2023-11-01 03:32:09 +00:00
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
{.push raises: [].}
2018-12-03 10:54:19 +00:00
import
2022-12-02 04:39:12 +00:00
std/[strutils, json],
nimcrypto/utils as ncrutils,
results,
web3/conversions,
./beacon/web3_eth_conv,
2022-12-02 04:39:12 +00:00
./common/common,
./constants,
./core/executor,
./db/[core_db, ledger],
./evm/[code_bytes, state, types],
./evm/tracer/legacy_tracer,
./launcher,
./transaction,
./utils/utils
2018-12-03 16:22:08 +00:00
when not CoreDbEnableCaptJournal:
{.error: "Compiler flag missing for tracer, try -d:dbjapi_enabled".}
2018-12-03 16:22:08 +00:00
type
CaptCtxRef = ref object
db: CoreDbRef # not `nil`
root: common.Hash256
ctx: CoreDbCtxRef # not `nil`
cpt: CoreDbCaptRef # not `nil`
restore: CoreDbCtxRef # `nil` unless `ctx` activated
const
senderName = "sender"
recipientName = "recipient"
minerName = "miner"
uncleName = "uncle"
internalTxName = "internalTx"
proc dumpMemoryDB*(node: JsonNode, cpt: CoreDbCaptRef) {.gcsafe.}
proc toJson*(receipts: seq[Receipt]): JsonNode {.gcsafe.}
# ------------------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------------------
template safeTracer(info: string; code: untyped) =
try:
code
except CatchableError as e:
raiseAssert info & " name=" & $e.name & " msg=" & e.msg
# -------------------
proc init(
T: type CaptCtxRef;
com: CommonRef;
root: common.Hash256;
): T =
let ctx = block:
let rc = com.db.ctx.newCtxByKey(root)
if rc.isErr:
raiseAssert "newCptCtx: " & $$rc.error
rc.value
T(db: com.db, root: root, cpt: com.db.pushCapture(), ctx: ctx)
proc init(
T: type CaptCtxRef;
com: CommonRef;
topHeader: BlockHeader;
): T
{.raises: [CatchableError].} =
T.init(com, com.db.getBlockHeader(topHeader.parentHash).stateRoot)
proc activate(cc: CaptCtxRef): CaptCtxRef {.discardable.} =
## Install/activate new context `cc.ctx`, old one in `cc.restore`
doAssert not cc.isNil
doAssert cc.restore.isNil # otherwise activated, already
cc.restore = cc.ctx.swapCtx cc.db
cc
proc release(cc: CaptCtxRef) =
if not cc.restore.isNil: # switch to original context (if any)
let ctx = cc.restore.swapCtx(cc.db)
doAssert ctx == cc.ctx
cc.ctx.forget() # dispose
cc.cpt.pop() # discard top layer of actions tracer
# -------------------
proc `%`(x: addresses.Address|Bytes32|Bytes256|Hash32): JsonNode =
result = %toHex(x)
2018-12-25 10:31:51 +00:00
proc toJson(receipt: Receipt): JsonNode =
result = newJObject()
result["cumulativeGasUsed"] = %receipt.cumulativeGasUsed
result["bloom"] = %receipt.logsBloom
2018-12-25 10:31:51 +00:00
result["logs"] = %receipt.logs
if receipt.hasStateRoot:
result["root"] = %(receipt.stateRoot.toHex)
2018-12-25 10:31:51 +00:00
else:
result["status"] = %receipt.status
proc dumpReceiptsImpl(
chainDB: CoreDbRef;
header: BlockHeader;
): JsonNode
{.raises: [CatchableError].} =
2018-12-25 10:31:51 +00:00
result = newJArray()
for receipt in chainDB.getReceipts(header.receiptsRoot):
2018-12-25 10:31:51 +00:00
result.add receipt.toJson
# ------------------------------------------------------------------------------
# Private functions
# ------------------------------------------------------------------------------
2018-12-03 10:54:19 +00:00
proc captureAccount(
n: JsonNode;
db: LedgerRef;
address: EthAddress;
name: string;
) =
2018-12-12 03:40:37 +00:00
var jaccount = newJObject()
jaccount["name"] = %name
jaccount["address"] = %(address.to0xHex)
let nonce = db.getNonce(address)
let balance = db.getBalance(address)
let codeHash = db.getCodeHash(address)
let storageRoot = db.getStorageRoot(address)
jaccount["nonce"] = %(conversions.`$`(nonce.Web3Quantity))
jaccount["balance"] = %("0x" & balance.toHex)
2018-12-11 09:53:05 +00:00
let code = db.getCode(address)
jaccount["codeHash"] = %(codeHash.to0xHex)
jaccount["code"] = %("0x" & code.bytes.toHex(true))
jaccount["storageRoot"] = %(storageRoot.to0xHex)
2018-12-11 09:53:05 +00:00
var storage = newJObject()
for key, value in db.storage(address):
2019-01-11 06:53:18 +00:00
storage["0x" & key.dumpHex] = %("0x" & 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 traceTransactionImpl(
com: CommonRef;
header: BlockHeader;
transactions: openArray[Transaction];
txIndex: uint64;
tracerFlags: set[TracerFlags] = {};
): JsonNode
{.raises: [CatchableError].}=
if header.txRoot == EMPTY_ROOT_HASH:
return newJNull()
2018-12-03 16:22:08 +00:00
let
tracerInst = newLegacyTracer(tracerFlags)
cc = activate CaptCtxRef.init(com, header)
vmState = BaseVMState.new(header, com, storeSlotHash = true).valueOr: return newJNull()
stateDb = vmState.stateDB
defer: cc.release()
2018-12-26 03:34:16 +00:00
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
doAssert(transactions.calcTxRoot == header.txRoot)
doAssert(transactions.len != 0)
2018-12-03 10:54:19 +00:00
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}
stateCtx = CaptCtxRef(nil)
2018-12-11 09:53:05 +00:00
let
miner = vmState.coinbase()
2019-04-23 12:50:45 +00:00
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for idx, tx in transactions:
2018-12-12 03:40:37 +00:00
let sender = tx.getSender
let recipient = tx.getRecipient(sender)
2018-12-11 09:53:05 +00:00
if idx.uint64 == txIndex:
vmState.tracer = tracerInst # only enable tracer on target tx
2018-12-12 03:40:37 +00:00
before.captureAccount(stateDb, sender, senderName)
before.captureAccount(stateDb, recipient, recipientName)
before.captureAccount(stateDb, miner, minerName)
stateDb.persist()
stateDiff["beforeRoot"] = %(stateDb.rootHash.toHex)
discard com.db.ctx.getAccounts.state(updateOk=true) # lazy hashing!
stateCtx = CaptCtxRef.init(com, stateDb.rootHash)
2018-12-11 09:53:05 +00:00
let rc = vmState.processTransaction(tx, sender, header)
2022-04-08 04:54:11 +00:00
gasUsed = if rc.isOk: rc.value else: 0
2018-12-12 03:40:37 +00:00
if idx.uint64 == txIndex:
discard com.db.ctx.getAccounts.state(updateOk=true) # lazy hashing!
2018-12-12 03:40:37 +00:00
after.captureAccount(stateDb, sender, senderName)
after.captureAccount(stateDb, recipient, recipientName)
after.captureAccount(stateDb, miner, minerName)
tracerInst.removeTracedAccounts(sender, recipient, miner)
stateDb.persist()
stateDiff["afterRoot"] = %(stateDb.rootHash.toHex)
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:
let
cx = activate stateCtx
ldgBefore = LedgerRef.init(com.db, cx.root, storeSlotHash = true)
defer: cx.release()
for idx, acc in tracedAccountsPairs(tracerInst):
before.captureAccount(ldgBefore, acc, internalTxName & $idx)
2018-12-25 07:31:12 +00:00
for idx, acc in tracedAccountsPairs(tracerInst):
2018-12-25 07:31:12 +00:00
after.captureAccount(stateDb, acc, internalTxName & $idx)
result = tracerInst.getTracingResult()
2018-12-03 16:22:08 +00:00
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:
result.dumpMemoryDB(cx.cpt)
2018-12-11 10:05:49 +00:00
proc dumpBlockStateImpl(
com: CommonRef;
blk: EthBlock;
dumpState = false;
): JsonNode
{.raises: [CatchableError].} =
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
template header: BlockHeader = blk.header
2018-12-12 03:40:37 +00:00
let
cc = activate CaptCtxRef.init(com, header)
parent = com.db.getBlockHeader(header.parentHash)
# only need a stack dump when scanning for internal transaction address
captureFlags = {DisableMemory, DisableStorage, EnableAccount}
tracerInst = newLegacyTracer(captureFlags)
vmState = BaseVMState.new(header, com, tracerInst, storeSlotHash = true).valueOr:
return newJNull()
miner = vmState.coinbase()
defer: cc.release()
2018-12-12 03:40:37 +00:00
var
2018-12-12 09:41:18 +00:00
before = newJArray()
after = newJArray()
stateBefore = LedgerRef.init(com.db, parent.stateRoot, storeSlotHash = true)
2018-12-12 03:40:37 +00:00
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for idx, tx in blk.transactions:
2018-12-12 03:40:37 +00:00
let sender = tx.getSender
let recipient = tx.getRecipient(sender)
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, miner, minerName)
2018-12-12 03:40:37 +00:00
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for idx, uncle in blk.uncles:
2018-12-12 03:40:37 +00:00
before.captureAccount(stateBefore, uncle.coinbase, uncleName & $idx)
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
discard vmState.processBlock(blk)
2018-12-12 03:40:37 +00:00
var stateAfter = vmState.stateDB
2019-02-02 09:20:45 +00:00
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for idx, tx in blk.transactions:
2018-12-12 03:40:37 +00:00
let sender = tx.getSender
let recipient = tx.getRecipient(sender)
2018-12-12 09:41:18 +00:00
after.captureAccount(stateAfter, sender, senderName & $idx)
after.captureAccount(stateAfter, recipient, recipientName & $idx)
tracerInst.removeTracedAccounts(sender, recipient)
2018-12-12 03:40:37 +00:00
after.captureAccount(stateAfter, miner, minerName)
tracerInst.removeTracedAccounts(miner)
2018-12-12 03:40:37 +00:00
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for idx, uncle in blk.uncles:
2018-12-12 03:40:37 +00:00
after.captureAccount(stateAfter, uncle.coinbase, uncleName & $idx)
tracerInst.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(tracerInst):
2018-12-25 07:31:12 +00:00
before.captureAccount(stateBefore, acc, internalTxName & $idx)
for idx, acc in tracedAccountsPairs(tracerInst):
2018-12-25 07:31:12 +00:00
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(cc.cpt)
2018-12-12 15:18:46 +00:00
proc traceBlockImpl(
com: CommonRef;
blk: EthBlock;
tracerFlags: set[TracerFlags] = {};
): JsonNode
{.raises: [CatchableError].} =
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
template header: BlockHeader = blk.header
2018-12-12 04:16:40 +00:00
let
cc = activate CaptCtxRef.init(com, header)
tracerInst = newLegacyTracer(tracerFlags)
# Tracer needs a database where the reverse slot hash table has been set up
vmState = BaseVMState.new(header, com, tracerInst, storeSlotHash = true).valueOr:
return newJNull()
defer: cc.release()
2018-12-12 04:16:40 +00:00
if header.txRoot == EMPTY_ROOT_HASH: return newJNull()
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
doAssert(blk.transactions.calcTxRoot == header.txRoot)
doAssert(blk.transactions.len != 0)
2018-12-12 04:16:40 +00:00
var gasUsed = GasInt(0)
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for tx in blk.transactions:
let
sender = tx.getSender
rc = vmState.processTransaction(tx, sender, header)
2022-04-08 04:54:11 +00:00
if rc.isOk:
gasUsed = gasUsed + rc.value
2018-12-12 04:16:40 +00:00
result = tracerInst.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(cc.cpt)
proc traceTransactionsImpl(
com: CommonRef;
header: BlockHeader;
transactions: openArray[Transaction];
): JsonNode
{.raises: [CatchableError].} =
2018-12-25 10:31:51 +00:00
result = newJArray()
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
for i in 0 ..< transactions.len:
result.add traceTransactionImpl(
com, header, transactions, i.uint64, {DisableState})
2018-12-25 10:31:51 +00:00
proc dumpDebuggingMetaDataImpl(
vmState: BaseVMState;
blk: EthBlock;
launchDebugger = true;
) {.raises: [CatchableError].} =
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
template header: BlockHeader = blk.header
2018-12-25 10:31:51 +00:00
let
cc = activate CaptCtxRef.init(vmState.com, header)
blockNumber = header.number
bloom = createBloom(vmState.receipts)
defer: cc.release()
let blockSummary = %{
"receiptsRoot": %("0x" & toHex(calcReceiptsRoot(vmState.receipts).data)),
"stateRoot": %("0x" & toHex(vmState.stateDB.rootHash.data)),
"logsBloom": %("0x" & toHex(bloom))
}
2018-12-25 10:31:51 +00:00
var metaData = %{
"blockNumber": %blockNumber.toHex,
"txTraces": traceTransactionsImpl(vmState.com, header, blk.transactions),
"stateDump": dumpBlockStateImpl(vmState.com, blk),
"blockTrace": traceBlockImpl(vmState.com, blk, {DisableState}),
"receipts": toJson(vmState.receipts),
"block": blockSummary
2018-12-25 10:31:51 +00:00
}
metaData.dumpMemoryDB(cc.cpt)
2019-01-12 12:48:28 +00:00
let jsonFileName = "debug" & $blockNumber & ".json"
if launchDebugger:
launchPremix(jsonFileName, metaData)
else:
writeFile(jsonFileName, metaData.pretty())
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc traceBlock*(
com: CommonRef;
blk: EthBlock;
tracerFlags: set[TracerFlags] = {};
): JsonNode =
"traceBlock".safeTracer:
result = com.traceBlockImpl(blk, tracerFlags)
proc toJson*(receipts: seq[Receipt]): JsonNode =
result = newJArray()
for receipt in receipts:
result.add receipt.toJson
proc dumpMemoryDB*(node: JsonNode, cpt: CoreDbCaptRef) =
var n = newJObject()
for (k,v) in cpt.kvtLog:
n[k.toHex(false)] = %v
node["state"] = n
proc dumpReceipts*(chainDB: CoreDbRef, header: BlockHeader): JsonNode =
"dumpReceipts".safeTracer:
result = chainDB.dumpReceiptsImpl header
proc traceTransaction*(
com: CommonRef;
header: BlockHeader;
txs: openArray[Transaction];
txIndex: uint64;
tracerFlags: set[TracerFlags] = {};
): JsonNode =
"traceTransaction".safeTracer:
result = com.traceTransactionImpl(header, txs, txIndex,tracerFlags)
proc dumpBlockState*(
com: CommonRef;
blk: EthBlock;
dumpState = false;
): JsonNode =
"dumpBlockState".safeTracer:
result = com.dumpBlockStateImpl(blk, dumpState)
proc traceTransactions*(
com: CommonRef;
header: BlockHeader;
transactions: openArray[Transaction];
): JsonNode =
"traceTransactions".safeTracer:
result = com.traceTransactionsImpl(header, transactions)
proc dumpDebuggingMetaData*(
vmState: BaseVMState;
blk: EthBlock;
launchDebugger = true;
) =
"dumpDebuggingMetaData".safeTracer:
vmState.dumpDebuggingMetaDataImpl(blk, launchDebugger)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------