2022-12-10 12:53:24 +00:00
|
|
|
# Nimbus
|
2024-02-21 16:04:44 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-12-10 12:53:24 +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.
|
|
|
|
|
2022-10-26 15:46:13 +00:00
|
|
|
import
|
2024-06-14 07:31:08 +00:00
|
|
|
std/[json, strutils],
|
2024-10-08 05:52:32 +00:00
|
|
|
eth/common/keys,
|
|
|
|
eth/common/headers,
|
|
|
|
eth/common/transactions,
|
2022-10-26 15:46:13 +00:00
|
|
|
eth/trie/trie_defs,
|
|
|
|
stint,
|
|
|
|
stew/byteutils,
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/transaction,
|
2023-12-12 19:12:56 +00:00
|
|
|
../../nimbus/db/ledger,
|
2023-07-09 02:16:22 +00:00
|
|
|
../../nimbus/common/chain_config
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
template fromJson(T: type Address, n: JsonNode): Address =
|
|
|
|
Address.fromHex(n.getStr)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
proc fromJson(T: type UInt256, n: JsonNode): UInt256 =
|
|
|
|
# stTransactionTest/ValueOverflow.json
|
|
|
|
# prevent parsing exception and subtitute it with max uint256
|
|
|
|
let hex = n.getStr
|
|
|
|
if ':' in hex:
|
|
|
|
high(UInt256)
|
|
|
|
else:
|
|
|
|
UInt256.fromHex(hex)
|
|
|
|
|
2024-10-03 11:42:24 +00:00
|
|
|
template fromJson*(T: type Bytes32, n: JsonNode): Bytes32 =
|
|
|
|
Bytes32(hexToByteArray(n.getStr, 32))
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
template fromJson*(T: type Hash32, n: JsonNode): Hash32 =
|
2024-09-29 12:37:09 +00:00
|
|
|
Hash32(hexToByteArray(n.getStr, 32))
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc fromJson(T: type seq[byte], n: JsonNode): T =
|
2022-10-26 15:46:13 +00:00
|
|
|
let hex = n.getStr
|
|
|
|
if hex.len == 0:
|
|
|
|
@[]
|
|
|
|
else:
|
|
|
|
hexToSeqByte(hex)
|
|
|
|
|
2024-07-07 06:52:11 +00:00
|
|
|
template fromJson(T: type uint64, n: JsonNode): uint64 =
|
2022-10-26 15:46:13 +00:00
|
|
|
fromHex[AccountNonce](n.getStr)
|
|
|
|
|
|
|
|
template fromJson(T: type EthTime, n: JsonNode): EthTime =
|
2023-10-18 02:16:11 +00:00
|
|
|
EthTime(fromHex[uint64](n.getStr))
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
proc fromJson(T: type PrivateKey, n: JsonNode): PrivateKey =
|
|
|
|
var secretKey = n.getStr
|
|
|
|
removePrefix(secretKey, "0x")
|
|
|
|
PrivateKey.fromHex(secretKey).tryGet()
|
|
|
|
|
|
|
|
proc fromJson(T: type AccessList, n: JsonNode): AccessList =
|
|
|
|
if n.kind == JNull:
|
|
|
|
return
|
|
|
|
|
|
|
|
for x in n:
|
|
|
|
var ap = AccessPair(
|
2024-10-08 05:52:32 +00:00
|
|
|
address: Address.fromJson(x["address"])
|
2022-10-26 15:46:13 +00:00
|
|
|
)
|
|
|
|
let sks = x["storageKeys"]
|
|
|
|
for sk in sks:
|
2024-09-29 12:37:09 +00:00
|
|
|
ap.storageKeys.add Bytes32.fromHex(sk.getStr)
|
2022-10-26 15:46:13 +00:00
|
|
|
result.add ap
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc fromJson(T: type seq[Hash32], list: JsonNode): T =
|
2023-07-20 23:34:56 +00:00
|
|
|
for x in list:
|
2024-10-08 05:52:32 +00:00
|
|
|
result.add Hash32.fromHex(x.getStr)
|
2023-07-20 23:34:56 +00:00
|
|
|
|
2022-10-26 15:46:13 +00:00
|
|
|
template required(T: type, nField: string): auto =
|
|
|
|
fromJson(T, n[nField])
|
|
|
|
|
|
|
|
template required(T: type, nField: string, index: int): auto =
|
|
|
|
fromJson(T, n[nField][index])
|
|
|
|
|
|
|
|
template omitZero(T: type, nField: string): auto =
|
|
|
|
if n.hasKey(nField):
|
|
|
|
fromJson(T, n[nField])
|
|
|
|
else:
|
|
|
|
default(T)
|
|
|
|
|
|
|
|
template omitZero(T: type, nField: string, index: int): auto =
|
|
|
|
if n.hasKey(nField):
|
|
|
|
fromJson(T, n[nField][index])
|
|
|
|
else:
|
|
|
|
default(T)
|
|
|
|
|
|
|
|
template optional(T: type, nField: string): auto =
|
|
|
|
if n.hasKey(nField):
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.some(T.fromJson(n[nField]))
|
2022-10-26 15:46:13 +00:00
|
|
|
else:
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.none(T)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
proc txType(n: JsonNode): TxType =
|
2023-07-20 23:34:56 +00:00
|
|
|
if "blobVersionedHashes" in n:
|
|
|
|
return TxEip4844
|
2022-10-26 15:46:13 +00:00
|
|
|
if "gasPrice" notin n:
|
|
|
|
return TxEip1559
|
|
|
|
if "accessLists" in n:
|
|
|
|
return TxEip2930
|
|
|
|
TxLegacy
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc parseHeader*(n: JsonNode): Header =
|
|
|
|
Header(
|
|
|
|
coinbase : required(Address, "currentCoinbase"),
|
2022-10-26 15:46:13 +00:00
|
|
|
difficulty : required(DifficultyInt, "currentDifficulty"),
|
2024-06-14 07:31:08 +00:00
|
|
|
number : required(BlockNumber, "currentNumber"),
|
2022-10-26 15:46:13 +00:00
|
|
|
gasLimit : required(GasInt, "currentGasLimit"),
|
|
|
|
timestamp : required(EthTime, "currentTimestamp"),
|
|
|
|
stateRoot : emptyRlpHash,
|
2024-10-03 11:42:24 +00:00
|
|
|
mixHash : omitZero(Bytes32, "currentRandom"),
|
2024-06-14 07:31:08 +00:00
|
|
|
baseFeePerGas : optional(UInt256, "currentBaseFee"),
|
2024-10-08 05:52:32 +00:00
|
|
|
withdrawalsRoot: optional(Hash32, "currentWithdrawalsRoot"),
|
2024-06-14 07:31:08 +00:00
|
|
|
excessBlobGas : optional(uint64, "currentExcessBlobGas"),
|
2024-10-08 05:52:32 +00:00
|
|
|
parentBeaconBlockRoot: optional(Hash32, "currentBeaconRoot"),
|
2023-09-21 15:21:21 +00:00
|
|
|
)
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc parseParentHeader*(n: JsonNode): Header =
|
|
|
|
Header(
|
2023-09-21 15:21:21 +00:00
|
|
|
stateRoot: emptyRlpHash,
|
|
|
|
excessBlobGas: optional(uint64, "parentExcessBlobGas"),
|
|
|
|
blobGasUsed: optional(uint64, "parentBlobGasUsed"),
|
2022-10-26 15:46:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc parseTx*(n: JsonNode, dataIndex, gasIndex, valueIndex: int): Transaction =
|
|
|
|
var tx = Transaction(
|
|
|
|
txType : txType(n),
|
|
|
|
nonce : required(AccountNonce, "nonce"),
|
|
|
|
gasLimit: required(GasInt, "gasLimit", gasIndex),
|
|
|
|
value : required(UInt256, "value", valueIndex),
|
2024-10-08 05:52:32 +00:00
|
|
|
payload : required(seq[byte], "data", dataIndex),
|
2022-10-26 15:46:13 +00:00
|
|
|
chainId : ChainId(1),
|
|
|
|
gasPrice: omitZero(GasInt, "gasPrice"),
|
2024-06-14 07:31:08 +00:00
|
|
|
maxFeePerGas : omitZero(GasInt, "maxFeePerGas"),
|
|
|
|
accessList : omitZero(AccessList, "accessLists", dataIndex),
|
|
|
|
maxPriorityFeePerGas: omitZero(GasInt, "maxPriorityFeePerGas"),
|
|
|
|
maxFeePerBlobGas : omitZero(UInt256, "maxFeePerBlobGas"),
|
2024-10-08 05:52:32 +00:00
|
|
|
versionedHashes : omitZero(seq[Hash32], "blobVersionedHashes")
|
2022-10-26 15:46:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
let rawTo = n["to"].getStr
|
|
|
|
if rawTo != "":
|
2024-10-08 05:52:32 +00:00
|
|
|
tx.to = Opt.some(Address.fromHex(rawTo))
|
2022-10-26 15:46:13 +00:00
|
|
|
|
|
|
|
let secretKey = required(PrivateKey, "secretKey")
|
2024-10-04 14:34:31 +00:00
|
|
|
signTransaction(tx, secretKey, false)
|
2022-10-26 15:46:13 +00:00
|
|
|
|
2023-01-10 17:02:21 +00:00
|
|
|
proc parseTx*(txData, index: JsonNode): Transaction =
|
|
|
|
let
|
|
|
|
dataIndex = index["data"].getInt
|
|
|
|
gasIndex = index["gas"].getInt
|
|
|
|
valIndex = index["value"].getInt
|
|
|
|
parseTx(txData, dataIndex, gasIndex, valIndex)
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc setupStateDB*(wantedState: JsonNode, stateDB: LedgerRef) =
|
2022-10-26 15:46:13 +00:00
|
|
|
for ac, accountData in wantedState:
|
2024-09-29 12:37:09 +00:00
|
|
|
let account = Address.fromHex(ac)
|
2022-10-26 15:46:13 +00:00
|
|
|
for slot, value in accountData{"storage"}:
|
|
|
|
stateDB.setStorage(account, fromHex(UInt256, slot), fromHex(UInt256, value.getStr))
|
|
|
|
|
|
|
|
stateDB.setNonce(account, fromJson(AccountNonce, accountData["nonce"]))
|
2024-10-08 05:52:32 +00:00
|
|
|
stateDB.setCode(account, fromJson(seq[byte], accountData["code"]))
|
2022-10-26 15:46:13 +00:00
|
|
|
stateDB.setBalance(account, fromJson(UInt256, accountData["balance"]))
|
2023-07-09 02:16:22 +00:00
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
iterator postState*(node: JsonNode): (Address, GenesisAccount) =
|
2023-07-09 02:16:22 +00:00
|
|
|
for ac, accountData in node:
|
2024-10-08 05:52:32 +00:00
|
|
|
let account = Address.fromHex(ac)
|
2023-07-09 02:16:22 +00:00
|
|
|
var ga = GenesisAccount(
|
|
|
|
nonce : fromJson(AccountNonce, accountData["nonce"]),
|
2024-10-08 05:52:32 +00:00
|
|
|
code : fromJson(seq[byte], accountData["code"]),
|
2023-07-09 02:16:22 +00:00
|
|
|
balance: fromJson(UInt256, accountData["balance"]),
|
|
|
|
)
|
|
|
|
|
|
|
|
for slot, value in accountData{"storage"}:
|
|
|
|
ga.storage[fromHex(UInt256, slot)] = fromHex(UInt256, value.getStr)
|
|
|
|
|
|
|
|
yield (account, ga)
|