now dumping hexary trie

This commit is contained in:
Daniel Lamberger 2024-02-17 14:13:08 +02:00
parent 2ea16613fa
commit 9f00650c36
4 changed files with 35 additions and 14 deletions

View File

@ -25,14 +25,14 @@ type
LegacyDbRef* = ref object of CoreDbRef LegacyDbRef* = ref object of CoreDbRef
kvt: CoreDxKvtRef ## Cache, no need to rebuild methods descriptor kvt: CoreDxKvtRef ## Cache, no need to rebuild methods descriptor
tdb: TrieDatabaseRef ## Descriptor reference copy captured with closures tdb*: TrieDatabaseRef ## Descriptor reference copy captured with closures
top: LegacyCoreDxTxRef ## Top transaction (if any) top: LegacyCoreDxTxRef ## Top transaction (if any)
LegacyDbClose* = proc() {.gcsafe, raises: [].} LegacyDbClose* = proc() {.gcsafe, raises: [].}
## Custom destructor ## Custom destructor
HexaryChildDbRef = ref object HexaryChildDbRef* = ref object
trie: HexaryTrie ## For closure descriptor for capturing trie*: HexaryTrie ## For closure descriptor for capturing
when CoreDbEnableApiTracking: when CoreDbEnableApiTracking:
kind: CoreDbSubTrie ## Current sub-trie kind: CoreDbSubTrie ## Current sub-trie
address: Option[EthAddress] ## For storage tree debugging address: Option[EthAddress] ## For storage tree debugging
@ -50,7 +50,7 @@ type
appDb: LegacyDbRef appDb: LegacyDbRef
LegacyCoreDbTrie* = ref object of CoreDbTrieRef LegacyCoreDbTrie* = ref object of CoreDbTrieRef
root: Hash256 ## Hash key root*: Hash256 ## Hash key
when CoreDbEnableApiTracking: when CoreDbEnableApiTracking:
kind: CoreDbSubTrie ## Current sub-trie kind: CoreDbSubTrie ## Current sub-trie
address: Option[EthAddress] ## For storage tree debugging address: Option[EthAddress] ## For storage tree debugging

View File

@ -20,12 +20,14 @@ import
../../nimbus/db/ledger/base/base_desc, ../../nimbus/db/ledger/base/base_desc,
../../nimbus/db/core_db/base/base_desc, ../../nimbus/db/core_db/base/base_desc,
../../nimbus/db/core_db/backend/legacy_rocksdb, ../../nimbus/db/core_db/backend/legacy_rocksdb,
../../nimbus/db/core_db/backend/legacy_db,
../../nimbus/db/core_db/base_iterators, ../../nimbus/db/core_db/base_iterators,
../../nimbus/evm/types, ../../nimbus/evm/types,
../replay/[pp, undump_blocks, xcheck], ../replay/[pp, undump_blocks, xcheck],
./test_helpers, ./test_helpers,
../../vendor/nim-rocksdb/rocksdb, ../../vendor/nim-rocksdb/rocksdb,
../../vendor/nim-stew/stew/byteutils ../../vendor/nim-stew/stew/byteutils,
../../vendor/nim-eth/eth/trie/hexary
type StopMoaningAboutLedger {.used.} = LedgerType type StopMoaningAboutLedger {.used.} = LedgerType
@ -137,21 +139,21 @@ proc ledgerProfResults(info: string; indent = 4): string =
w.mapIt($it & ledgerProfTab.stats(it).pp).sorted.join(", ") w.mapIt($it & ledgerProfTab.stats(it).pp).sorted.join(", ")
proc createFileAndLogBlockHeaders(lastBlock: BlockHeader, vmState: BaseVMState, name: string): Stream = proc createFileAndLogBlockHeaders(lastBlock: BlockHeader, vmState: BaseVMState, name: string): tuple[stream: Stream, path: string] =
let blockNumber = lastBlock.blockNumber.truncate(uint) let blockNumber = lastBlock.blockNumber.truncate(uint)
let baseDir = cast[LegaPersDbRef](vmState.com.db).rdb.store.dbPath let baseDir = cast[LegaPersDbRef](vmState.com.db).rdb.store.dbPath
let path = &"{baseDir}/{name}_at_block_{blockNumber}.dump" let path = &"{baseDir}/_block_{blockNumber}_dump_{name}.txt"
let stream = newFileStream(path, fmWrite) let stream = newFileStream(path, fmWrite)
stream.writeLine(&"# Block number: {blockNumber}") stream.writeLine(&"# Block number: {blockNumber}")
stream.writeLine(&"# Block time: {lastBlock.timestamp.int64.fromUnix.utc}") stream.writeLine(&"# Block time: {lastBlock.timestamp.int64.fromUnix.utc}")
stream.writeLine(&"# Block root hash: {$lastBlock.stateRoot}") stream.writeLine(&"# Block root hash: {$lastBlock.stateRoot}")
stream.writeLine("#") stream.writeLine("#")
echo &"Block {blockNumber} reached; dumping world state key-values into {path}" return (stream, path)
return stream
proc dumpWorldStateKvs(lastBlock: BlockHeader, vmState: BaseVMState) = proc dumpWorldStateKvs(lastBlock: BlockHeader, vmState: BaseVMState) =
let stream = createFileAndLogBlockHeaders(lastBlock, vmState, "all_kvs") let (stream, path) = createFileAndLogBlockHeaders(lastBlock, vmState, "all_kvs")
echo &"Block {lastBlock.blockNumber} reached; dumping world state key-values into {path}"
defer: defer:
try: stream.close() except: discard try: stream.close() except: discard
let mpt = cast[CoreDxMptRef](vmState.stateDB.extras.getMptFn()) let mpt = cast[CoreDxMptRef](vmState.stateDB.extras.getMptFn())
@ -162,7 +164,8 @@ proc dumpWorldStateKvs(lastBlock: BlockHeader, vmState: BaseVMState) =
proc dumpWorldStateMptAccounts(lastBlock: BlockHeader, vmState: BaseVMState) = proc dumpWorldStateMptAccounts(lastBlock: BlockHeader, vmState: BaseVMState) =
let stream = createFileAndLogBlockHeaders(lastBlock, vmState, "mpt_accounts") let (stream, path) = createFileAndLogBlockHeaders(lastBlock, vmState, "mpt_accounts")
echo &"Block {lastBlock.blockNumber} reached; dumping world state accounts into {path}"
defer: defer:
try: stream.close() except: discard try: stream.close() except: discard
let accMethods = vmState.stateDB.methods let accMethods = vmState.stateDB.methods
@ -173,9 +176,26 @@ proc dumpWorldStateMptAccounts(lastBlock: BlockHeader, vmState: BaseVMState) =
let codeHash: Hash256 = accMethods.getCodeHashFn(address) let codeHash: Hash256 = accMethods.getCodeHashFn(address)
let codeSize: int = accMethods.getCodeSizeFn(address) let codeSize: int = accMethods.getCodeSizeFn(address)
let storageRoot: Hash256 = accMethods.getStorageRootFn(address) let storageRoot: Hash256 = accMethods.getStorageRootFn(address)
stream.writeLine(&"address={address.toHex} addrHash={addressHash.toHex} balance={balance.toHex:>22} nonce={nonce:>6} codeHash={$codeHash} codeSize={codeSize:>6} storageRoot={$storageRoot}") let code: Blob = accMethods.getCodeFn(address)
var storage: string = "|"
if storageRoot != EMPTY_ROOT_HASH:
for i in 0..<10:
storage.add $accMethods.getStorageFn(address, i.u256)
storage.add '|'
stream.writeLine(&"address={address.toHex} addrHash={addressHash.toHex} balance={balance.toHex:>22} nonce={nonce:>6} codeHash={$codeHash} codeSize={codeSize:>6} storageRoot={$storageRoot} code={code.toHex} storage={storage}...")
proc dumpWorldStateTree(lastBlock: BlockHeader, vmState: BaseVMState) =
let (stream, path) = createFileAndLogBlockHeaders(lastBlock, vmState, "mpt_tree")
echo &"Block {lastBlock.blockNumber} reached; dumping world state tree into {path}"
defer:
try: stream.close() except: discard
var ldbref: LegacyDbRef = vmState.com.db.LegacyDbRef
let tdb = ldbref.tdb
var trie: CoreDbTrieRef = LegacyCoreDbTrie(root: lastBlock.stateRoot)
var mpt = HexaryChildDbRef(trie: initHexaryTrie(tdb, trie.LegacyCoreDbTrie.root, false))
mpt.trie.dumpTree(stream)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public test function # Public test function
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -245,6 +265,7 @@ proc test_chainSync*(
inc dataDumpsPerformed inc dataDumpsPerformed
dumpWorldStateKvs(w[0][^1], vmState) dumpWorldStateKvs(w[0][^1], vmState)
dumpWorldStateMptAccounts(w[0][^1], vmState) dumpWorldStateMptAccounts(w[0][^1], vmState)
dumpWorldStateTree(w[0][^1], vmState)
continue continue

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 2ad07a60109a4609d672f9f147eeee987e43a15d Subproject commit 1d881eadcc9f737ed1ece572e20e42bb815febe7

2
vendor/nim-rocksdb vendored

@ -1 +1 @@
Subproject commit 9fab9369331934f355b6abb0a0e58c1e533228a2 Subproject commit 6622fbe72dc82e8c252e7b186157b55fe982c11b