2023-10-19 06:02:12 +00:00
|
|
|
# Nimbus
|
2024-06-21 07:44:10 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-10-19 06:02:12 +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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[json, tables, strutils],
|
|
|
|
stint,
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/[base, addresses, hashes],
|
2023-10-19 06:02:12 +00:00
|
|
|
stew/byteutils,
|
2023-12-12 19:12:56 +00:00
|
|
|
../db/ledger
|
2023-10-19 06:02:12 +00:00
|
|
|
|
|
|
|
type
|
2023-12-12 19:12:56 +00:00
|
|
|
DumpAccount* = ref object
|
|
|
|
balance* : UInt256
|
|
|
|
nonce* : AccountNonce
|
2024-10-16 01:34:12 +00:00
|
|
|
root* : Hash32
|
|
|
|
codeHash*: Hash32
|
|
|
|
code* : seq[byte]
|
|
|
|
key* : Hash32
|
2023-12-12 19:12:56 +00:00
|
|
|
storage* : Table[UInt256, UInt256]
|
2023-10-19 06:02:12 +00:00
|
|
|
|
|
|
|
StateDump* = ref object
|
2024-10-16 01:34:12 +00:00
|
|
|
root*: Hash32
|
|
|
|
accounts*: Table[Address, DumpAccount]
|
2023-10-19 06:02:12 +00:00
|
|
|
|
|
|
|
proc `%`*(x: UInt256): JsonNode =
|
|
|
|
%("0x" & x.toHex)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc `%`*(x: seq[byte]): JsonNode =
|
2023-10-19 06:02:12 +00:00
|
|
|
%("0x" & x.toHex)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc `%`*(x: Hash32): JsonNode =
|
2023-10-19 06:02:12 +00:00
|
|
|
%("0x" & x.data.toHex)
|
|
|
|
|
|
|
|
proc `%`*(x: AccountNonce): JsonNode =
|
|
|
|
%("0x" & x.toHex)
|
|
|
|
|
|
|
|
proc `%`*(x: Table[UInt256, UInt256]): JsonNode =
|
|
|
|
result = newJObject()
|
|
|
|
for k, v in x:
|
|
|
|
result["0x" & k.toHex] = %(v)
|
|
|
|
|
|
|
|
proc `%`*(x: DumpAccount): JsonNode =
|
|
|
|
result = %{
|
|
|
|
"balance" : %(x.balance),
|
|
|
|
"nonce" : %(x.nonce),
|
|
|
|
"root" : %(x.root),
|
|
|
|
"codeHash": %(x.codeHash),
|
|
|
|
"code" : %(x.code),
|
|
|
|
"key" : %(x.key)
|
|
|
|
}
|
|
|
|
if x.storage.len > 0:
|
|
|
|
result["storage"] = %(x.storage)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc `%`*(x: Table[Address, DumpAccount]): JsonNode =
|
2023-10-19 06:02:12 +00:00
|
|
|
result = newJObject()
|
|
|
|
for k, v in x:
|
|
|
|
result["0x" & k.toHex] = %(v)
|
|
|
|
|
|
|
|
proc `%`*(x: StateDump): JsonNode =
|
|
|
|
result = %{
|
|
|
|
"root": %(x.root),
|
|
|
|
"accounts": %(x.accounts)
|
|
|
|
}
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc dumpAccount*(db: LedgerRef, acc: Address): DumpAccount =
|
2023-10-19 06:02:12 +00:00
|
|
|
result = DumpAccount(
|
|
|
|
balance : db.getBalance(acc),
|
|
|
|
nonce : db.getNonce(acc),
|
|
|
|
root : db.getStorageRoot(acc),
|
|
|
|
codeHash: db.getCodeHash(acc),
|
2024-06-21 07:44:10 +00:00
|
|
|
code : db.getCode(acc).bytes(),
|
2024-10-16 01:34:12 +00:00
|
|
|
key : keccak256(acc.data)
|
2023-10-19 06:02:12 +00:00
|
|
|
)
|
|
|
|
for k, v in db.cachedStorage(acc):
|
|
|
|
result.storage[k] = v
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc dumpAccounts*(db: LedgerRef): Table[Address, DumpAccount] =
|
2023-10-19 06:02:12 +00:00
|
|
|
for acc in db.addresses():
|
|
|
|
result[acc] = dumpAccount(db, acc)
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc dumpState*(db: LedgerRef): StateDump =
|
2023-10-19 06:02:12 +00:00
|
|
|
StateDump(
|
2024-10-27 18:56:28 +00:00
|
|
|
root: db.getStateRoot(),
|
2023-10-19 06:02:12 +00:00
|
|
|
accounts: dumpAccounts(db)
|
|
|
|
)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc dumpAccounts*(stateDB: LedgerRef, addresses: openArray[Address]): JsonNode =
|
2023-10-19 06:02:12 +00:00
|
|
|
result = newJObject()
|
|
|
|
for ac in addresses:
|
|
|
|
result[ac.toHex] = %dumpAccount(stateDB, ac)
|
|
|
|
|