2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-02-08 10:50:35 +00:00
|
|
|
# Copyright (c) 2020-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.
|
|
|
|
|
2019-01-06 15:21:34 +00:00
|
|
|
import
|
2024-10-08 03:07:24 +00:00
|
|
|
std/[json, strutils, os],
|
|
|
|
eth/common/transaction_utils,
|
|
|
|
eth/common/blocks,
|
|
|
|
eth/common/eth_types_rlp,
|
|
|
|
httputils,
|
|
|
|
nimcrypto/utils,
|
|
|
|
stint,
|
|
|
|
stew/byteutils
|
2023-12-27 00:48:53 +00:00
|
|
|
|
2024-02-08 10:50:35 +00:00
|
|
|
from stew/objects import checkedEnumAssign
|
|
|
|
|
2023-12-27 00:48:53 +00:00
|
|
|
template stripLeadingZeros(value: string): string =
|
|
|
|
var cidx = 0
|
|
|
|
# ignore the last character so we retain '0' on zero value
|
|
|
|
while cidx < value.len - 1 and value[cidx] == '0':
|
|
|
|
cidx.inc
|
|
|
|
value[cidx .. ^1]
|
|
|
|
|
|
|
|
func encodeQuantity(value: SomeUnsignedInt): string =
|
|
|
|
var hValue = value.toHex.stripLeadingZeros
|
|
|
|
result = "0x" & hValue.toLowerAscii
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2019-01-11 06:53:18 +00:00
|
|
|
func hexToInt*(s: string, T: typedesc[SomeInteger]): T =
|
2019-01-06 15:21:34 +00:00
|
|
|
var i = 0
|
2024-10-08 03:07:24 +00:00
|
|
|
if s[i] == '0' and (s[i + 1] in {'x', 'X'}):
|
|
|
|
inc(i, 2)
|
2019-01-06 15:21:34 +00:00
|
|
|
if s.len - i > sizeof(T) * 2:
|
|
|
|
raise newException(ValueError, "input hex too big for destination int")
|
|
|
|
while i < s.len:
|
|
|
|
result = result shl 4 or readHexChar(s[i]).T
|
|
|
|
inc(i)
|
|
|
|
|
2024-09-29 12:37:09 +00:00
|
|
|
proc to0xHex*(x: int64 | uint64 | byte | int): string =
|
2024-02-16 09:08:07 +00:00
|
|
|
toLowerAscii(encodeQuantity(x.uint64))
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2024-09-29 12:37:09 +00:00
|
|
|
proc to0xHex*(x: UInt256): string =
|
2019-01-06 15:21:34 +00:00
|
|
|
"0x" & stint.toHex(x)
|
|
|
|
|
2024-09-29 12:37:09 +00:00
|
|
|
proc to0xHex*(x: string): string =
|
2019-01-06 15:21:34 +00:00
|
|
|
"0x" & toLowerAscii(x)
|
|
|
|
|
2024-10-08 03:07:24 +00:00
|
|
|
type SomeData* = Address | Bloom | Bytes8
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2019-09-03 15:06:43 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var SomeData) =
|
2021-09-14 09:49:31 +00:00
|
|
|
let node = n[name]
|
|
|
|
if node.kind == JString:
|
2024-09-29 12:37:09 +00:00
|
|
|
hexToByteArray(node.getStr(), x.data)
|
|
|
|
doAssert(x.to0xHex == toLowerAscii(node.getStr()), name)
|
2021-09-14 09:49:31 +00:00
|
|
|
else:
|
2024-09-29 12:37:09 +00:00
|
|
|
hexToByteArray(node["value"].getStr(), x.data)
|
|
|
|
doAssert(x.to0xHex == toLowerAscii(node["value"].getStr()), name)
|
2019-09-03 15:06:43 +00:00
|
|
|
|
2024-10-08 03:07:24 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var (Hash32 | Bytes32)) =
|
2021-09-14 09:49:31 +00:00
|
|
|
let node = n[name]
|
|
|
|
if node.kind == JString:
|
|
|
|
hexToByteArray(node.getStr(), x.data)
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert(x.to0xHex == toLowerAscii(node.getStr()), name)
|
2021-09-14 09:49:31 +00:00
|
|
|
else:
|
|
|
|
hexToByteArray(node["value"].getStr(), x.data)
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert(x.to0xHex == toLowerAscii(node["value"].getStr()), name)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2024-10-08 03:07:24 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var seq[byte]) =
|
2019-01-06 15:21:34 +00:00
|
|
|
x = hexToSeqByte(n[name].getStr())
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert(x.to0xHex == toLowerAscii(n[name].getStr()), name)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2019-09-03 15:06:43 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var UInt256) =
|
2021-09-14 09:49:31 +00:00
|
|
|
let node = n[name]
|
|
|
|
if node.kind == JString:
|
|
|
|
x = UInt256.fromHex(node.getStr())
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert(x.to0xHex == toLowerAscii(node.getStr()), name)
|
2021-09-14 09:49:31 +00:00
|
|
|
else:
|
|
|
|
x = node.getInt().u256
|
|
|
|
doAssert($x == $node.getInt, name)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2019-09-03 15:06:43 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var SomeInteger) =
|
2021-09-14 09:49:31 +00:00
|
|
|
let node = n[name]
|
|
|
|
if node.kind == JString:
|
|
|
|
x = hexToInt(node.getStr(), type(x))
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert(x.to0xHex == toLowerAscii(node.getStr()), name)
|
2021-09-14 09:49:31 +00:00
|
|
|
else:
|
|
|
|
type T = type x
|
|
|
|
x = T(node.getInt)
|
|
|
|
doAssert($x == $node.getInt, name)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2019-09-03 15:06:43 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var EthTime) =
|
2023-10-18 02:16:11 +00:00
|
|
|
x = EthTime(hexToInt(n[name].getStr(), uint64))
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert(x.uint64.to0xHex == toLowerAscii(n[name].getStr()), name)
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc fromJson*[T](n: JsonNode, name: string, x: var Opt[T]) =
|
2021-06-30 13:41:29 +00:00
|
|
|
if name in n:
|
|
|
|
var val: T
|
|
|
|
n.fromJson(name, val)
|
2024-06-14 07:31:08 +00:00
|
|
|
x = Opt.some(val)
|
2021-06-30 13:41:29 +00:00
|
|
|
|
2021-09-26 03:38:41 +00:00
|
|
|
proc fromJson*(n: JsonNode, name: string, x: var TxType) =
|
|
|
|
let node = n[name]
|
|
|
|
if node.kind == JInt:
|
|
|
|
x = TxType(node.getInt)
|
|
|
|
else:
|
|
|
|
x = hexToInt(node.getStr(), int).TxType
|
|
|
|
|
2024-10-08 03:07:24 +00:00
|
|
|
proc fromJson*[T: Bytes32 | Hash32](n: JsonNode, name: string, x: var seq[T]) =
|
2023-08-21 02:10:18 +00:00
|
|
|
let node = n[name]
|
2024-10-01 17:40:37 +00:00
|
|
|
var h: T
|
|
|
|
x = newSeqOfCap[T](node.len)
|
2023-08-21 02:10:18 +00:00
|
|
|
for v in node:
|
|
|
|
hexToByteArray(v.getStr(), h.data)
|
|
|
|
x.add h
|
|
|
|
|
2024-10-08 03:07:24 +00:00
|
|
|
proc parseBlockHeader*(n: JsonNode): Header =
|
2019-01-06 15:21:34 +00:00
|
|
|
n.fromJson "parentHash", result.parentHash
|
|
|
|
n.fromJson "sha3Uncles", result.ommersHash
|
|
|
|
n.fromJson "miner", result.coinbase
|
|
|
|
n.fromJson "stateRoot", result.stateRoot
|
|
|
|
n.fromJson "transactionsRoot", result.txRoot
|
2024-06-14 07:31:08 +00:00
|
|
|
n.fromJson "receiptsRoot", result.receiptsRoot
|
|
|
|
n.fromJson "logsBloom", result.logsBloom
|
2019-01-06 15:21:34 +00:00
|
|
|
n.fromJson "difficulty", result.difficulty
|
2024-06-14 07:31:08 +00:00
|
|
|
n.fromJson "number", result.number
|
2019-01-06 15:21:34 +00:00
|
|
|
n.fromJson "gasLimit", result.gasLimit
|
|
|
|
n.fromJson "gasUsed", result.gasUsed
|
|
|
|
n.fromJson "timestamp", result.timestamp
|
|
|
|
n.fromJson "extraData", result.extraData
|
2024-06-14 07:31:08 +00:00
|
|
|
n.fromJson "mixHash", result.mixHash
|
2019-01-06 15:21:34 +00:00
|
|
|
n.fromJson "nonce", result.nonce
|
2024-06-14 07:31:08 +00:00
|
|
|
n.fromJson "baseFeePerGas", result.baseFeePerGas
|
2023-03-09 23:40:55 +00:00
|
|
|
n.fromJson "withdrawalsRoot", result.withdrawalsRoot
|
2023-09-24 23:53:20 +00:00
|
|
|
n.fromJson "blobGasUsed", result.blobGasUsed
|
|
|
|
n.fromJson "excessBlobGas", result.excessBlobGas
|
|
|
|
n.fromJson "parentBeaconBlockRoot", result.parentBeaconBlockRoot
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
if result.baseFeePerGas.get(0.u256) == 0.u256:
|
2021-09-14 09:49:31 +00:00
|
|
|
# probably geth bug
|
2024-06-14 07:31:08 +00:00
|
|
|
result.baseFeePerGas = Opt.none(UInt256)
|
2021-09-14 09:49:31 +00:00
|
|
|
|
|
|
|
proc parseAccessPair(n: JsonNode): AccessPair =
|
|
|
|
n.fromJson "address", result.address
|
|
|
|
let keys = n["storageKeys"]
|
|
|
|
for kn in keys:
|
2024-09-29 12:37:09 +00:00
|
|
|
result.storageKeys.add Bytes32.fromHex(kn.getStr())
|
2021-09-14 09:49:31 +00:00
|
|
|
|
2019-01-06 15:21:34 +00:00
|
|
|
proc parseTransaction*(n: JsonNode): Transaction =
|
2021-06-27 04:19:43 +00:00
|
|
|
var tx = Transaction(txType: TxLegacy)
|
2021-05-15 06:37:40 +00:00
|
|
|
n.fromJson "nonce", tx.nonce
|
|
|
|
n.fromJson "gasPrice", tx.gasPrice
|
|
|
|
n.fromJson "gas", tx.gasLimit
|
|
|
|
|
2021-06-27 04:19:43 +00:00
|
|
|
if n["to"].kind != JNull:
|
2024-10-08 03:07:24 +00:00
|
|
|
var to: Address
|
2021-06-27 04:19:43 +00:00
|
|
|
n.fromJson "to", to
|
2024-06-14 07:31:08 +00:00
|
|
|
tx.to = Opt.some(to)
|
2021-05-15 06:37:40 +00:00
|
|
|
|
|
|
|
n.fromJson "value", tx.value
|
|
|
|
n.fromJson "input", tx.payload
|
|
|
|
n.fromJson "v", tx.V
|
|
|
|
n.fromJson "r", tx.R
|
|
|
|
n.fromJson "s", tx.S
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
if n.hasKey("type") and n["type"].kind != JNull:
|
2021-09-26 03:38:41 +00:00
|
|
|
n.fromJson "type", tx.txType
|
2021-09-14 09:49:31 +00:00
|
|
|
|
2021-09-26 03:38:41 +00:00
|
|
|
if tx.txType >= TxEip1559:
|
2024-06-14 07:31:08 +00:00
|
|
|
n.fromJson "maxPriorityFeePerGas", tx.maxPriorityFeePerGas
|
|
|
|
n.fromJson "maxFeePerGas", tx.maxFeePerGas
|
2021-09-14 09:49:31 +00:00
|
|
|
|
2021-09-26 03:38:41 +00:00
|
|
|
if tx.txType >= TxEip2930:
|
|
|
|
if n.hasKey("chainId"):
|
|
|
|
let id = hexToInt(n["chainId"].getStr(), int)
|
|
|
|
tx.chainId = ChainId(id)
|
|
|
|
|
2021-09-14 09:49:31 +00:00
|
|
|
let accessList = n["accessList"]
|
|
|
|
if accessList.len > 0:
|
|
|
|
for acn in accessList:
|
|
|
|
tx.accessList.add parseAccessPair(acn)
|
2023-08-21 02:10:18 +00:00
|
|
|
|
|
|
|
if tx.txType >= TxEip4844:
|
|
|
|
n.fromJson "maxFeePerBlobGas", tx.maxFeePerBlobGas
|
|
|
|
|
|
|
|
if n.hasKey("versionedHashes") and n["versionedHashes"].kind != JNull:
|
|
|
|
n.fromJson "versionedHashes", tx.versionedHashes
|
|
|
|
|
2021-09-14 09:49:31 +00:00
|
|
|
tx
|
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
proc parseWithdrawal*(n: JsonNode): Withdrawal =
|
|
|
|
n.fromJson "index", result.index
|
|
|
|
n.fromJson "validatorIndex", result.validatorIndex
|
|
|
|
n.fromJson "address", result.address
|
|
|
|
n.fromJson "amount", result.amount
|
|
|
|
|
2021-09-14 09:49:31 +00:00
|
|
|
proc validateTxSenderAndHash*(n: JsonNode, tx: Transaction) =
|
2024-10-04 14:34:31 +00:00
|
|
|
var sender = tx.recoverSender().expect("valid signature")
|
2024-10-08 03:07:24 +00:00
|
|
|
var fromAddr: Address
|
2021-09-14 09:49:31 +00:00
|
|
|
n.fromJson "from", fromAddr
|
2024-09-29 12:37:09 +00:00
|
|
|
doAssert sender.to0xHex == fromAddr.to0xHex
|
|
|
|
doAssert n["hash"].getStr() == tx.rlpHash().to0xHex
|
2019-01-06 15:21:34 +00:00
|
|
|
|
|
|
|
proc parseLog(n: JsonNode): Log =
|
|
|
|
n.fromJson "address", result.address
|
|
|
|
n.fromJson "data", result.data
|
|
|
|
let topics = n["topics"]
|
|
|
|
result.topics = newSeqOfCap[Topic](n.len)
|
|
|
|
var topicHash: Topic
|
|
|
|
for tp in topics:
|
2024-09-29 12:37:09 +00:00
|
|
|
hexToByteArray(tp.getStr(), topicHash.data)
|
2019-01-06 15:21:34 +00:00
|
|
|
result.topics.add topicHash
|
|
|
|
|
|
|
|
proc parseLogs(n: JsonNode): seq[Log] =
|
|
|
|
if n.len > 0:
|
|
|
|
result = newSeqOfCap[Log](n.len)
|
|
|
|
for log in n:
|
|
|
|
result.add parseLog(log)
|
|
|
|
else:
|
|
|
|
result = @[]
|
|
|
|
|
|
|
|
proc parseReceipt*(n: JsonNode): Receipt =
|
2024-02-08 10:50:35 +00:00
|
|
|
var recType: byte
|
|
|
|
n.fromJson "type", recType
|
|
|
|
var txVal: ReceiptType
|
|
|
|
var rec =
|
|
|
|
if checkedEnumAssign(txVal, recType):
|
|
|
|
Receipt(receiptType: txVal)
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Unknown receipt type")
|
|
|
|
|
2019-01-06 15:21:34 +00:00
|
|
|
if n.hasKey("root"):
|
2024-10-08 03:07:24 +00:00
|
|
|
var hash: Hash32
|
2019-01-06 15:21:34 +00:00
|
|
|
n.fromJson "root", hash
|
2021-06-27 04:19:43 +00:00
|
|
|
rec.isHash = true
|
|
|
|
rec.hash = hash
|
2019-01-06 15:21:34 +00:00
|
|
|
else:
|
|
|
|
var status: int
|
|
|
|
n.fromJson "status", status
|
2021-06-27 04:19:43 +00:00
|
|
|
rec.isHash = false
|
|
|
|
rec.status = status == 1
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2021-05-15 06:37:40 +00:00
|
|
|
n.fromJson "cumulativeGasUsed", rec.cumulativeGasUsed
|
2024-06-14 07:31:08 +00:00
|
|
|
n.fromJson "logsBloom", rec.logsBloom
|
2021-05-15 06:37:40 +00:00
|
|
|
rec.logs = parseLogs(n["logs"])
|
2021-06-27 04:19:43 +00:00
|
|
|
rec
|
2019-01-06 15:21:34 +00:00
|
|
|
|
2024-10-08 03:07:24 +00:00
|
|
|
proc headerHash*(n: JsonNode): Hash32 =
|
2019-01-06 15:21:34 +00:00
|
|
|
n.fromJson "hash", result
|
2019-01-15 08:55:39 +00:00
|
|
|
|
|
|
|
proc parseAccount*(n: JsonNode): Account =
|
|
|
|
n.fromJson "nonce", result.nonce
|
|
|
|
n.fromJson "balance", result.balance
|
|
|
|
n.fromJson "storageRoot", result.storageRoot
|
|
|
|
n.fromJson "codeHash", result.codeHash
|