2022-11-26 14:59:19 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2022-03-21 09:19:19 +00:00
|
|
|
import
|
2023-03-20 11:51:09 +00:00
|
|
|
std/[options, times, json, strutils],
|
2022-12-06 05:55:40 +00:00
|
|
|
../common/common,
|
2022-12-02 04:35:41 +00:00
|
|
|
stew/byteutils,
|
|
|
|
../vm_state,
|
|
|
|
../vm_types,
|
|
|
|
../db/accounts_cache
|
2022-03-21 09:19:19 +00:00
|
|
|
|
|
|
|
proc `$`(hash: Hash256): string =
|
|
|
|
hash.data.toHex
|
|
|
|
|
|
|
|
proc `$`(bloom: BloomFilter): string =
|
|
|
|
bloom.toHex
|
|
|
|
|
|
|
|
proc `$`(nonce: BlockNonce): string =
|
|
|
|
nonce.toHex
|
|
|
|
|
2022-03-23 14:05:57 +00:00
|
|
|
proc `$`(data: Blob): string =
|
2023-07-09 01:46:18 +00:00
|
|
|
if data.len == 0:
|
|
|
|
return "zero length"
|
2022-03-23 14:05:57 +00:00
|
|
|
data.toHex
|
2022-09-28 06:09:33 +00:00
|
|
|
|
2022-03-21 09:19:19 +00:00
|
|
|
proc debug*(h: BlockHeader): string =
|
2022-11-26 14:59:19 +00:00
|
|
|
result.add "parentHash : " & $h.parentHash & "\n"
|
|
|
|
result.add "ommersHash : " & $h.ommersHash & "\n"
|
|
|
|
result.add "coinbase : " & $h.coinbase & "\n"
|
|
|
|
result.add "stateRoot : " & $h.stateRoot & "\n"
|
|
|
|
result.add "txRoot : " & $h.txRoot & "\n"
|
|
|
|
result.add "receiptRoot : " & $h.receiptRoot & "\n"
|
|
|
|
result.add "bloom : " & $h.bloom & "\n"
|
|
|
|
result.add "difficulty : " & $h.difficulty & "\n"
|
|
|
|
result.add "blockNumber : " & $h.blockNumber & "\n"
|
|
|
|
result.add "gasLimit : " & $h.gasLimit & "\n"
|
|
|
|
result.add "gasUsed : " & $h.gasUsed & "\n"
|
|
|
|
result.add "timestamp : " & $h.timestamp.toUnix & "\n"
|
|
|
|
result.add "extraData : " & $h.extraData & "\n"
|
|
|
|
result.add "mixDigest : " & $h.mixDigest & "\n"
|
|
|
|
result.add "nonce : " & $h.nonce & "\n"
|
|
|
|
result.add "fee.isSome : " & $h.fee.isSome & "\n"
|
2022-03-21 09:19:19 +00:00
|
|
|
if h.fee.isSome:
|
2022-11-26 14:59:19 +00:00
|
|
|
result.add "fee : " & $h.fee.get() & "\n"
|
|
|
|
if h.withdrawalsRoot.isSome:
|
|
|
|
result.add "withdrawalsRoot: " & $h.withdrawalsRoot.get() & "\n"
|
2023-08-04 12:43:30 +00:00
|
|
|
if h.blobGasUsed.isSome:
|
|
|
|
result.add "blobGasUsed : " & $h.blobGasUsed.get() & "\n"
|
|
|
|
if h.excessBlobGas.isSome:
|
|
|
|
result.add "excessBlobGas : " & $h.excessBlobGas.get() & "\n"
|
2022-11-26 14:59:19 +00:00
|
|
|
result.add "blockHash : " & $blockHash(h) & "\n"
|
2022-12-02 04:35:41 +00:00
|
|
|
|
|
|
|
proc dumpAccount(stateDB: AccountsCache, address: EthAddress): JsonNode =
|
|
|
|
var storage = newJObject()
|
|
|
|
for k, v in stateDB.cachedStorage(address):
|
|
|
|
storage[k.toHex] = %v.toHex
|
|
|
|
|
|
|
|
result = %{
|
|
|
|
"nonce": %toHex(stateDB.getNonce(address)),
|
|
|
|
"balance": %stateDB.getBalance(address).toHex(),
|
|
|
|
"codehash": %($stateDB.getCodeHash(address)),
|
|
|
|
"storageRoot": %($stateDB.getStorageRoot(address)),
|
|
|
|
"storage": storage
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:20:52 +00:00
|
|
|
proc dumpAccounts*(vmState: BaseVMState): JsonNode =
|
|
|
|
result = newJObject()
|
|
|
|
for ac in vmState.stateDB.addresses:
|
|
|
|
result[ac.toHex] = dumpAccount(vmState.stateDB, ac)
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc debugAccounts*(vmState: BaseVMState): string =
|
|
|
|
var
|
|
|
|
accounts = newJObject()
|
|
|
|
accountList = newSeq[EthAddress]()
|
|
|
|
|
|
|
|
for address in vmState.stateDB.addresses:
|
|
|
|
accountList.add address
|
|
|
|
|
|
|
|
for i, ac in accountList:
|
|
|
|
accounts[ac.toHex] = dumpAccount(vmState.stateDB, ac)
|
|
|
|
|
|
|
|
let res = %{
|
|
|
|
"rootHash": %($vmState.readOnlyStateDB.rootHash),
|
|
|
|
"accounts": accounts
|
|
|
|
}
|
|
|
|
|
|
|
|
res.pretty
|
2022-12-06 05:55:40 +00:00
|
|
|
|
|
|
|
proc debug*(vms: BaseVMState): string =
|
2022-12-07 16:11:03 +00:00
|
|
|
result.add "com.consensus : " & $vms.com.consensus & "\n"
|
|
|
|
result.add "parent : " & $vms.parent.blockHash & "\n"
|
2023-09-24 15:25:41 +00:00
|
|
|
result.add "timestamp : " & $vms.blockCtx.timestamp.toUnix & "\n"
|
|
|
|
result.add "gasLimit : " & $vms.blockCtx.gasLimit & "\n"
|
|
|
|
result.add "fee : " & $vms.blockCtx.fee & "\n"
|
|
|
|
result.add "prevRandao : " & $vms.blockCtx.prevRandao & "\n"
|
|
|
|
result.add "blockDifficulty : " & $vms.blockCtx.difficulty & "\n"
|
|
|
|
result.add "coinbase : " & $vms.blockCtx.coinbase & "\n"
|
|
|
|
result.add "excessBlobGas : " & $vms.blockCtx.excessBlobGas & "\n"
|
2022-12-07 16:11:03 +00:00
|
|
|
result.add "flags : " & $vms.flags & "\n"
|
|
|
|
result.add "receipts.len : " & $vms.receipts.len & "\n"
|
|
|
|
result.add "stateDB.root : " & $vms.stateDB.rootHash & "\n"
|
|
|
|
result.add "cumulativeGasUsed: " & $vms.cumulativeGasUsed & "\n"
|
|
|
|
result.add "txOrigin : " & $vms.txOrigin & "\n"
|
|
|
|
result.add "txGasPrice : " & $vms.txGasPrice & "\n"
|
|
|
|
result.add "fork : " & $vms.fork & "\n"
|
|
|
|
|
|
|
|
proc `$`(x: ChainId): string =
|
|
|
|
$int(x)
|
|
|
|
|
|
|
|
proc `$`(acl: AccessList): string =
|
2023-07-09 01:46:18 +00:00
|
|
|
if acl.len == 0:
|
|
|
|
return "zero length"
|
|
|
|
|
2022-12-07 16:11:03 +00:00
|
|
|
if acl.len > 0:
|
|
|
|
result.add "\n"
|
|
|
|
|
|
|
|
for ap in acl:
|
|
|
|
result.add " * " & $ap.address & "\n"
|
|
|
|
for i, k in ap.storageKeys:
|
|
|
|
result.add " - " & k.toHex
|
|
|
|
if i < ap.storageKeys.len-1:
|
|
|
|
result.add "\n"
|
|
|
|
|
|
|
|
proc debug*(tx: Transaction): string =
|
|
|
|
result.add "txType : " & $tx.txType & "\n"
|
|
|
|
result.add "chainId : " & $tx.chainId & "\n"
|
|
|
|
result.add "nonce : " & $tx.nonce & "\n"
|
|
|
|
result.add "gasPrice : " & $tx.gasPrice & "\n"
|
|
|
|
result.add "maxPriorityFee: " & $tx.maxPriorityFee & "\n"
|
|
|
|
result.add "maxFee : " & $tx.maxFee & "\n"
|
|
|
|
result.add "gasLimit : " & $tx.gasLimit & "\n"
|
|
|
|
result.add "to : " & $tx.to & "\n"
|
|
|
|
result.add "value : " & $tx.value & "\n"
|
|
|
|
result.add "payload : " & $tx.payload & "\n"
|
|
|
|
result.add "accessList : " & $tx.accessList & "\n"
|
|
|
|
result.add "V : " & $tx.V & "\n"
|
|
|
|
result.add "R : " & $tx.R & "\n"
|
|
|
|
result.add "S : " & $tx.S & "\n"
|