2021-05-17 11:35:16 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
2022-11-25 05:26:29 +00:00
|
|
|
# * 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.
|
2021-05-17 11:35:16 +00:00
|
|
|
|
2021-03-23 11:49:44 +00:00
|
|
|
import
|
2023-06-25 13:30:34 +00:00
|
|
|
std/[json],
|
2022-11-25 05:26:29 +00:00
|
|
|
stew/byteutils,
|
|
|
|
../../../tools/common/helpers,
|
2022-12-02 04:39:12 +00:00
|
|
|
../../../nimbus/common/chain_config
|
2021-03-23 11:49:44 +00:00
|
|
|
|
|
|
|
type
|
2023-06-27 00:30:05 +00:00
|
|
|
Blob = seq[byte]
|
2023-07-25 05:00:59 +00:00
|
|
|
|
2022-04-20 07:57:50 +00:00
|
|
|
ChainData* = object
|
2022-11-25 05:26:29 +00:00
|
|
|
params*: NetworkParams
|
2022-04-20 07:57:50 +00:00
|
|
|
lastBlockHash*: string
|
2023-06-27 00:30:05 +00:00
|
|
|
blocksRlp*: seq[Blob]
|
2021-03-23 11:49:44 +00:00
|
|
|
|
|
|
|
const genFields = [
|
|
|
|
"nonce",
|
|
|
|
"timestamp",
|
|
|
|
"extraData",
|
|
|
|
"gasLimit",
|
|
|
|
"difficulty",
|
|
|
|
"mixHash",
|
|
|
|
"coinbase"
|
|
|
|
]
|
|
|
|
|
2022-11-25 05:26:29 +00:00
|
|
|
proc parseChainConfig(n: JsonNode): ChainConfig =
|
|
|
|
getChainConfig(n["network"].getStr)
|
2021-03-23 11:49:44 +00:00
|
|
|
|
2021-06-30 13:43:47 +00:00
|
|
|
proc optionalField(n: string, genesis, gen: JsonNode) =
|
|
|
|
if n in gen:
|
|
|
|
genesis[n] = gen[n]
|
|
|
|
|
2022-11-25 05:26:29 +00:00
|
|
|
proc parseGenesis(n: JsonNode): Genesis =
|
2021-03-23 11:49:44 +00:00
|
|
|
let gen = n["genesisBlockHeader"]
|
2021-05-17 11:35:16 +00:00
|
|
|
var genesis = newJObject()
|
2021-03-23 11:49:44 +00:00
|
|
|
for x in genFields:
|
2021-05-17 11:35:16 +00:00
|
|
|
genesis[x] = gen[x]
|
2021-06-30 13:43:47 +00:00
|
|
|
optionalField("baseFeePerGas", genesis, gen)
|
2023-08-04 12:43:30 +00:00
|
|
|
optionalField("blobGasUsed", genesis, gen)
|
|
|
|
optionalField("excessBlobGas", genesis, gen)
|
2021-05-17 11:35:16 +00:00
|
|
|
genesis["alloc"] = n["pre"]
|
2022-11-25 05:26:29 +00:00
|
|
|
parseGenesis($genesis)
|
2021-05-17 11:35:16 +00:00
|
|
|
|
2022-11-25 05:26:29 +00:00
|
|
|
proc extractChainData*(n: JsonNode): ChainData =
|
|
|
|
result.params = NetworkParams(
|
|
|
|
genesis: parseGenesis(n),
|
|
|
|
config : parseChainConfig(n))
|
2022-04-20 07:57:50 +00:00
|
|
|
result.lastblockhash = n["lastblockhash"].getStr
|
2021-03-23 11:49:44 +00:00
|
|
|
|
|
|
|
let blks = n["blocks"]
|
|
|
|
for x in blks:
|
|
|
|
let hex = x["rlp"].getStr
|
|
|
|
let bytes = hexToSeqByte(hex)
|
|
|
|
result.blocksRlp.add bytes
|