2021-05-17 11:35:16 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2021-03-23 11:49:44 +00:00
|
|
|
import
|
2022-09-26 16:14:12 +00:00
|
|
|
std/[json, strutils, options],
|
2021-03-23 11:49:44 +00:00
|
|
|
stew/byteutils
|
|
|
|
|
|
|
|
type
|
2022-04-20 07:57:50 +00:00
|
|
|
ChainData* = object
|
|
|
|
genesis*: string
|
|
|
|
lastBlockHash*: string
|
|
|
|
blocksRlp*: seq[byte]
|
2021-03-23 11:49:44 +00:00
|
|
|
|
|
|
|
const genFields = [
|
|
|
|
"nonce",
|
|
|
|
"timestamp",
|
|
|
|
"extraData",
|
|
|
|
"gasLimit",
|
|
|
|
"difficulty",
|
|
|
|
"mixHash",
|
|
|
|
"coinbase"
|
|
|
|
]
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc processNetwork(network: string): JsonNode =
|
2021-03-23 11:49:44 +00:00
|
|
|
var
|
|
|
|
homesteadBlock = 2000
|
|
|
|
daoForkSupport = false
|
|
|
|
daoForkBlock = homesteadBlock
|
|
|
|
eip150Block = 2000
|
|
|
|
eip158Block = 2000
|
|
|
|
byzantiumBlock = 2000
|
|
|
|
constantinopleBlock = 2000
|
|
|
|
petersburgBlock = 2000
|
|
|
|
istanbulBlock = 2000
|
2021-12-10 11:36:35 +00:00
|
|
|
muirGlacierBlock = 2000
|
2021-03-23 11:49:44 +00:00
|
|
|
berlinBlock = 2000
|
2021-06-30 13:43:47 +00:00
|
|
|
londonBlock = 2000
|
2021-12-10 11:36:35 +00:00
|
|
|
arrowGlacierBlock = 2000
|
2022-09-26 16:14:12 +00:00
|
|
|
mergeForkBlock = none(int)
|
|
|
|
ttd = none(int)
|
2021-03-23 11:49:44 +00:00
|
|
|
|
|
|
|
case network
|
2021-12-10 11:36:35 +00:00
|
|
|
|
|
|
|
# All the network forks, which includes all the EVM, DAO and Glacier forks.
|
2021-03-23 11:49:44 +00:00
|
|
|
of "Frontier":
|
|
|
|
discard
|
|
|
|
of "Homestead":
|
|
|
|
homesteadBlock = 0
|
|
|
|
of "EIP150":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
of "EIP158":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
of "Byzantium":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
of "Constantinople":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
of "ConstantinopleFix":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
of "Istanbul":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
|
|
|
of "Berlin":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
2021-12-10 11:36:35 +00:00
|
|
|
muirGlacierBlock = 0
|
2021-03-23 11:49:44 +00:00
|
|
|
berlinBlock = 0
|
2021-06-30 13:43:47 +00:00
|
|
|
of "London":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
2021-12-10 11:36:35 +00:00
|
|
|
muirGlacierBlock = 0
|
|
|
|
berlinBlock = 0
|
|
|
|
londonBlock = 0
|
|
|
|
of "ArrowGlacier":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
|
|
|
muirGlacierBlock = 0
|
2021-06-30 13:43:47 +00:00
|
|
|
berlinBlock = 0
|
|
|
|
londonBlock = 0
|
2021-12-10 11:36:35 +00:00
|
|
|
arrowGlacierBlock = 0
|
2022-09-26 16:14:12 +00:00
|
|
|
of "Merge":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
|
|
|
muirGlacierBlock = 0
|
|
|
|
berlinBlock = 0
|
|
|
|
londonBlock = 0
|
|
|
|
arrowGlacierBlock = 0
|
|
|
|
mergeForkBlock = some(0)
|
|
|
|
ttd = some(0)
|
2021-12-10 11:36:35 +00:00
|
|
|
|
|
|
|
# Just the subset of "At5" networks mentioned in the test suite.
|
2021-03-23 11:49:44 +00:00
|
|
|
of "FrontierToHomesteadAt5":
|
|
|
|
homesteadBlock = 5
|
|
|
|
of "HomesteadToDaoAt5":
|
|
|
|
homesteadBlock = 0
|
|
|
|
daoForkSupport = true
|
|
|
|
daoForkBlock = 5
|
2021-12-10 11:36:35 +00:00
|
|
|
of "HomesteadToEIP150At5":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 5
|
2021-03-23 11:49:44 +00:00
|
|
|
of "EIP158ToByzantiumAt5":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 5
|
|
|
|
of "ByzantiumToConstantinopleFixAt5":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 5
|
|
|
|
petersburgBlock = 5
|
2021-06-30 13:43:47 +00:00
|
|
|
of "BerlinToLondonAt5":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
2021-12-10 11:36:35 +00:00
|
|
|
muirGlacierBlock = 0
|
2021-06-30 13:43:47 +00:00
|
|
|
berlinBlock = 0
|
|
|
|
londonBlock = 5
|
2022-09-26 16:14:12 +00:00
|
|
|
of "ArrowGlacierToMergeAtDiffC0000":
|
|
|
|
homesteadBlock = 0
|
|
|
|
eip150Block = 0
|
|
|
|
eip158Block = 0
|
|
|
|
byzantiumBlock = 0
|
|
|
|
constantinopleBlock = 0
|
|
|
|
petersburgBlock = 0
|
|
|
|
istanbulBlock = 0
|
|
|
|
muirGlacierBlock = 0
|
|
|
|
berlinBlock = 0
|
|
|
|
londonBlock = 0
|
|
|
|
arrowGlacierBlock = 0
|
|
|
|
ttd = some(0xC0000)
|
2021-12-10 11:36:35 +00:00
|
|
|
|
2021-03-23 11:49:44 +00:00
|
|
|
else:
|
|
|
|
doAssert(false, "unsupported network: " & network)
|
|
|
|
|
|
|
|
var n = newJObject()
|
|
|
|
n["homesteadBlock"] = newJInt(homesteadBlock)
|
|
|
|
if daoForkSupport:
|
|
|
|
n["daoForkSupport"] = newJBool(daoForkSupport)
|
|
|
|
n["daoForkBlock"] = newJInt(daoForkBlock)
|
|
|
|
n["eip150Block"] = newJInt(eip150Block)
|
|
|
|
n["eip158Block"] = newJInt(eip158Block)
|
|
|
|
n["byzantiumBlock"] = newJInt(byzantiumBlock)
|
|
|
|
n["constantinopleBlock"] = newJInt(constantinopleBlock)
|
|
|
|
n["petersburgBlock"] = newJInt(petersburgBlock)
|
|
|
|
n["istanbulBlock"] = newJInt(istanbulBlock)
|
2021-12-10 11:36:35 +00:00
|
|
|
n["muirGlacierBlock"] = newJInt(muirGlacierBlock)
|
2021-03-23 11:49:44 +00:00
|
|
|
n["berlinBlock"] = newJInt(berlinBlock)
|
2021-06-30 13:43:47 +00:00
|
|
|
n["londonBlock"] = newJInt(londonBlock)
|
2021-12-10 11:36:35 +00:00
|
|
|
n["arrowGlacierBlock"] = newJInt(arrowGlacierBlock)
|
2022-09-26 16:14:12 +00:00
|
|
|
if mergeForkBlock.isSome:
|
|
|
|
n["mergeForkBlock"] = newJInt(mergeForkBlock.get())
|
2021-03-23 11:49:44 +00:00
|
|
|
n["chainId"] = newJInt(1)
|
2022-09-26 16:14:12 +00:00
|
|
|
if ttd.isSome:
|
|
|
|
n["terminalTotalDifficulty"] = newJString("0x" & ttd.get().toHex(8))
|
2021-03-23 11:49:44 +00:00
|
|
|
result = n
|
|
|
|
|
2021-06-30 13:43:47 +00:00
|
|
|
proc optionalField(n: string, genesis, gen: JsonNode) =
|
|
|
|
if n in gen:
|
|
|
|
genesis[n] = gen[n]
|
|
|
|
|
2022-04-20 07:57:50 +00:00
|
|
|
proc extractChainData*(n: JsonNode): ChainData =
|
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)
|
2021-05-17 11:35:16 +00:00
|
|
|
genesis["alloc"] = n["pre"]
|
|
|
|
|
|
|
|
var ngen = newJObject()
|
|
|
|
ngen["genesis"] = genesis
|
2021-03-23 11:49:44 +00:00
|
|
|
ngen["config"] = processNetwork(n["network"].getStr)
|
2022-04-20 07:57:50 +00:00
|
|
|
result.lastblockhash = n["lastblockhash"].getStr
|
|
|
|
result.genesis = $ngen
|
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
|