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
|
|
|
|
std/[json, os, strutils, parseopt, terminal],
|
|
|
|
stew/byteutils
|
|
|
|
|
|
|
|
type
|
|
|
|
ChainData = object
|
|
|
|
genesis: JsonNode
|
|
|
|
blocksRlp: seq[byte]
|
|
|
|
|
|
|
|
Config = object
|
|
|
|
outPath: string
|
|
|
|
filter: string
|
|
|
|
|
|
|
|
const genFields = [
|
|
|
|
"nonce",
|
|
|
|
"timestamp",
|
|
|
|
"extraData",
|
|
|
|
"gasLimit",
|
|
|
|
"difficulty",
|
|
|
|
"mixHash",
|
|
|
|
"coinbase"
|
|
|
|
]
|
|
|
|
|
|
|
|
proc processNetWork(network: string): JsonNode =
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
# 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
|
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)
|
2021-03-23 11:49:44 +00:00
|
|
|
n["chainId"] = newJInt(1)
|
|
|
|
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]
|
|
|
|
|
2021-05-17 11:35:16 +00:00
|
|
|
proc extractChainData(n: JsonNode, chainFile: string): 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)
|
2021-05-17 11:35:16 +00:00
|
|
|
ngen["lastblockhash"] = n["lastblockhash"]
|
|
|
|
ngen["chainfile"] = %chainFile
|
2021-03-23 11:49:44 +00:00
|
|
|
result.genesis = ngen
|
|
|
|
|
|
|
|
let blks = n["blocks"]
|
|
|
|
for x in blks:
|
|
|
|
let hex = x["rlp"].getStr
|
|
|
|
let bytes = hexToSeqByte(hex)
|
|
|
|
result.blocksRlp.add bytes
|
|
|
|
|
|
|
|
proc processFile(fileName, outPath: string): int =
|
|
|
|
let n = json.parseFile(fileName)
|
2021-05-17 11:35:16 +00:00
|
|
|
let (folder, name) = fileName.splitPath()
|
|
|
|
let last = folder.splitPath().tail
|
|
|
|
|
2021-03-23 11:49:44 +00:00
|
|
|
for name, unit in n:
|
2021-05-17 11:35:16 +00:00
|
|
|
let name = last & "_" & name
|
|
|
|
let cd = extractChainData(unit, outPath / name & "_chain.rlp")
|
2021-03-23 11:49:44 +00:00
|
|
|
writeFile(outPath / name & "_config.json", cd.genesis.pretty)
|
|
|
|
writeFile(outPath / name & "_chain.rlp", cd.blocksRlp)
|
|
|
|
inc result
|
|
|
|
|
|
|
|
proc initConfig(): Config =
|
|
|
|
result.outPath = "consensus_data"
|
|
|
|
|
|
|
|
proc processArguments(conf: var Config) =
|
|
|
|
var opt = initOptParser()
|
|
|
|
|
|
|
|
for kind, key, value in opt.getopt():
|
|
|
|
case kind
|
|
|
|
of cmdArgument:
|
|
|
|
conf.filter = key
|
|
|
|
of cmdLongOption, cmdShortOption:
|
|
|
|
case key.toLowerAscii()
|
|
|
|
of "o": conf.outPath = value
|
|
|
|
else:
|
|
|
|
var msg = "Unknown option " & key
|
|
|
|
if value.len > 0: msg = msg & " : " & value
|
|
|
|
quit(QuitFailure)
|
|
|
|
of cmdEnd:
|
|
|
|
doAssert(false)
|
|
|
|
|
|
|
|
proc main() =
|
|
|
|
const basePath = "tests" / "fixtures" / "eth_tests" / "BlockchainTests"
|
|
|
|
|
|
|
|
var conf = initConfig()
|
|
|
|
processArguments(conf)
|
|
|
|
createDir(conf.outPath)
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
for fileName in walkDirRec(basePath):
|
|
|
|
if not fileName.endsWith(".json"):
|
|
|
|
continue
|
|
|
|
let (_, name) = fileName.splitPath()
|
|
|
|
if conf.filter notin fileName:
|
|
|
|
continue
|
|
|
|
|
|
|
|
terminal.eraseLine(stdout)
|
|
|
|
stdout.write "\r"
|
|
|
|
stdout.write name
|
|
|
|
count += processFile(fileName, conf.outPath)
|
|
|
|
|
|
|
|
echo "\ntest data generated: ", count
|
|
|
|
|
|
|
|
main()
|