drop PublicNetwork enum usage and replace it with NetworkId

we cannot limit the `--networkid` switch to values available in
`PublicNetwork` enum. it should able to accept very wide range of
custom NetworkId.
This commit is contained in:
jangko 2021-05-20 13:01:57 +07:00
parent d12e7d22bf
commit a0d10f5728
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
15 changed files with 90 additions and 79 deletions

View File

@ -27,7 +27,7 @@ proc processNode(genesisFile, chainFile,
conf = getConfiguration()
chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false,
id = toPublicNetwork(conf.net.networkId)
conf.net.networkId
)
initializeEmptyDb(chainDB)

View File

@ -78,11 +78,11 @@ proc main() =
ethNode = setupEthNode(eth)
chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false,
id = toPublicNetwork(conf.net.networkId)
conf.net.networkId
)
initializeEmptyDb(chainDB)
importRlpBlock(blocksFile, chainDB)
discard importRlpBlock(blocksFile, chainDB)
let ctx = setupGraphqlContext(chainDB, ethNode)
runTest("GraphQL", caseFolder):

View File

@ -53,6 +53,10 @@ type
muirGlacierBlock* : BlockNumber
berlinBlock* : BlockNumber
# TODO: this need to be fixed somehow
# using `real` engine configuration
poaEngine* : bool
Genesis* = object
nonce* : BlockNonce
timestamp* : EthTime
@ -156,6 +160,10 @@ proc loadCustomGenesis*(fileName: string, cg: var CustomGenesis): bool =
cg.config.daoForkSupport = cc.config.daoForkSupport
cg.config.eip150Hash = cc.config.eip150Hash
# TODO: this need to be fixed somehow
# using `real` engine configuration
cg.config.poaEngine = false
template validateFork(forkName: untyped, nextBlock: BlockNumber) =
let fork = astToStr(forkName)
if cc.config.forkName.isSome:

View File

@ -75,19 +75,11 @@ type
enabled*: bool
address*: TransportAddress
PublicNetwork* = enum
CustomNet = 0
MainNet = 1
# No longer used: MordenNet = 2
RopstenNet = 3
RinkebyNet = 4
GoerliNet = 5
KovanNet = 42
NetworkFlags* = enum
## Ethereum network flags
NoDiscover, ## Peer discovery disabled
V5Discover, ## Dicovery V5 enabled
NoDiscover ## Peer discovery disabled
V5Discover ## Dicovery V5 enabled
NetworkIdSet ## prevent CustomNetwork replacement
DebugFlags* {.pure.} = enum
## Debug selection flags
@ -148,6 +140,16 @@ type
accounts*: Table[EthAddress, NimbusAccount]
importFile*: string
const
# these are public network id
CustomNet* = 0.NetworkId
MainNet* = 1.NetworkId
# No longer used: MordenNet = 2
RopstenNet* = 3.NetworkId
RinkebyNet* = 4.NetworkId
GoerliNet* = 5.NetworkId
KovanNet* = 42.NetworkId
const
defaultRpcApi = {RpcFlags.Eth, RpcFlags.Shh}
defaultProtocols = {ProtocolFlags.Eth, ProtocolFlags.Shh}
@ -172,7 +174,7 @@ proc toFork*(c: ChainConfig, number: BlockNumber): Fork =
elif number >= c.homesteadBlock: FkHomestead
else: FkFrontier
proc publicChainConfig*(id: PublicNetwork): ChainConfig =
proc chainConfig*(id: NetworkId): ChainConfig =
# For some public networks, NetworkId and ChainId value are identical
# but that is not always the case
@ -242,14 +244,11 @@ proc publicChainConfig*(id: PublicNetwork): ChainConfig =
muirGlacierBlock: 4_460_644.toBlockNumber, # never occured in goerli network
berlinBlock: 4_460_644.toBlockNumber
)
of CustomNet:
else:
# everything else will use CustomNet config
let conf = getConfiguration()
trace "Custom genesis block configuration loaded", conf=conf.customGenesis.config
conf.customGenesis.config
else:
error "No chain config for public network", networkId = id
doAssert(false, "No chain config for " & $id)
ChainConfig()
proc processList(v: string, o: var seq[string]) =
## Process comma-separated list of strings.
@ -418,18 +417,9 @@ proc setBootnodes(onodes: var seq[ENode], nodeUris: openarray[string]) =
doAssert(processENode(item, node) == Success)
onodes.add(node)
macro availableEnumValues(T: type enum): untyped =
let impl = getTypeImpl(T)[1].getTypeImpl()
result = newNimNode(nnkBracket)
for i in 1 ..< impl.len: result.add(newCall("NetworkId", copyNimTree(impl[i])))
proc toPublicNetwork*(id: NetworkId): PublicNetwork {.inline.} =
if id in availableEnumValues(PublicNetwork):
result = PublicNetwork(id)
proc setNetwork(conf: var NetConfiguration, id: PublicNetwork) =
proc setNetwork(conf: var NetConfiguration, id: NetworkId) =
## Set network id and default network bootnodes
conf.networkId = NetworkId(id)
conf.networkId = id
case id
of MainNet:
conf.bootNodes.setBootnodes(MainnetBootnodes)
@ -441,16 +431,10 @@ proc setNetwork(conf: var NetConfiguration, id: PublicNetwork) =
conf.bootNodes.setBootnodes(GoerliBootnodes)
of KovanNet:
conf.bootNodes.setBootnodes(KovanBootnodes)
of CustomNet:
discard
proc setNetwork(conf: var NetConfiguration, id: NetworkId) =
## Set network id and default network bootnodes
let pubNet = toPublicNetwork(id)
if pubNet == CustomNet:
conf.networkId = id
else:
conf.setNetwork(pubNet)
# everything else will use bootnodes
# from --bootnodes switch
discard
proc processNetArguments(key, value: string): ConfigStatus =
## Processes only `Networking` related command line options
@ -480,12 +464,18 @@ proc processNetArguments(key, value: string): ConfigStatus =
elif skey == "customnetwork":
if not loadCustomGenesis(value, config.customGenesis):
result = Error
config.net.networkId = NetworkId(CustomNet)
if NetworkIdSet notin config.net.flags:
# prevent clash with --networkid if it already set
# because any --networkid value that is not
# in the public network will also translated as
# CustomNetwork
config.net.networkId = CustomNet
elif skey == "networkid":
var res = 0
result = processInteger(value, res)
if result == Success:
config.net.setNetwork(NetworkId(result))
config.net.flags.incl NetworkIdSet
elif skey == "nodiscover":
config.net.flags.incl(NoDiscover)
elif skey == "v5discover":
@ -655,7 +645,7 @@ proc initConfiguration(): NimbusConfiguration =
result.rpc.binds = @[initTAddress("127.0.0.1:8545")]
## Network defaults
result.net.setNetwork(defaultNetwork.NetworkId)
result.net.setNetwork(defaultNetwork)
result.net.maxPeers = 25
result.net.maxPendingPeers = 0
result.net.bindPort = 30303'u16

View File

@ -8,7 +8,7 @@
import
sequtils, algorithm,
stew/[byteutils], eth/trie/[hexary, db],
eth/[common, rlp], chronicles,
eth/[common, rlp, p2p], chronicles,
../errors, ../constants, ./storage_types,
../utils, ../config, ../chain_config
@ -17,7 +17,7 @@ type
db* : TrieDatabaseRef
pruneTrie*: bool
config* : ChainConfig
networkId*: PublicNetwork
networkId*: NetworkId
# startingBlock, currentBlock, and highestBlock
# are progress indicator
@ -29,11 +29,11 @@ type
blockNumber: BlockNumber
index: int
proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool = true, id: PublicNetwork = MainNet): BaseChainDB =
proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool = true, id: NetworkId = MainNet): BaseChainDB =
new(result)
result.db = db
result.pruneTrie = pruneTrie
result.config = publicChainConfig(id)
result.config = chainConfig(id)
result.networkId = id
proc `$`*(db: BaseChainDB): string =

View File

@ -1,12 +1,12 @@
import
std/[json, strutils, times, tables],
eth/[common, rlp, trie], stew/[byteutils],
eth/[common, rlp, trie, p2p], stew/[byteutils],
chronicles, eth/trie/db,
./db/[db_chain, state_db],
./genesis_alloc, ./config, ./constants,
./chain_config
proc defaultGenesisBlockForNetwork*(id: PublicNetwork): Genesis =
proc defaultGenesisBlockForNetwork*(id: NetworkId): Genesis =
result = case id
of MainNet:
Genesis(
@ -42,14 +42,10 @@ proc defaultGenesisBlockForNetwork*(id: PublicNetwork): Genesis =
difficulty: 1.u256,
alloc: decodePrealloc(goerliAllocData)
)
of CustomNet:
else:
# everything else will use custom genesis
let customGenesis = getConfiguration().customGenesis
customGenesis.genesis
else:
# TODO: Fill out the rest
error "No default genesis for network", id
doAssert(false, "No default genesis for " & $id)
Genesis()
proc toBlock*(g: Genesis, db: BaseChainDB = nil): BlockHeader =
let (tdb, pruneTrie) = if db.isNil: (newMemoryDB(), true)
@ -91,5 +87,5 @@ proc commit*(g: Genesis, db: BaseChainDB) =
proc initializeEmptyDb*(db: BaseChainDB) =
trace "Writing genesis to DB"
let networkId = getConfiguration().net.networkId.toPublicNetwork()
let networkId = getConfiguration().net.networkId
defaultGenesisBlockForNetwork(networkId).commit(db)

View File

@ -52,7 +52,8 @@ proc start(nimbus: NimbusNode) =
let trieDB = trieDB newChainDb(conf.dataDir)
var chainDB = newBaseChainDB(trieDB,
conf.prune == PruneMode.Full,
conf.net.networkId.toPublicNetwork())
conf.net.networkId
)
chainDB.populateProgress()
if canonicalHeadHashKey().toOpenArray notin trieDB:

View File

@ -69,9 +69,10 @@ proc setupTxContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt,
vmState.gasCosts = vmState.fork.forkToSchedule
proc consensusEnginePoA*(vmState: BaseVMState): bool =
let networkId = vmState.chainDB.networkId
# PoA consensus engine have no reward for miner
result = networkId in {GoerliNet, RinkebyNet, KovanNet}
# TODO: this need to be fixed somehow
# using `real` engine configuration
vmState.chainDB.config.poaEngine
proc getSignature(bytes: openArray[byte], output: var Signature): bool =
let sig = Signature.fromRaw(bytes)

View File

@ -57,9 +57,10 @@ proc newBaseVMState*(prevStateRoot: Hash256,
result.init(prevStateRoot, header, chainDB, tracerFlags)
proc consensusEnginePoA*(vmState: BaseVMState): bool =
let networkId = vmState.chainDB.networkId
# PoA consensus engine have no reward for miner
result = networkId in {GoerliNet, RinkebyNet, KovanNet}
# TODO: this need to be fixed somehow
# using `real` engine configuration
vmState.chainDB.config.poaEngine
proc getSignature(bytes: openArray[byte], output: var Signature): bool =
let sig = Signature.fromRaw(bytes)

View File

@ -1,5 +1,16 @@
import stint, os, parseopt, strutils
from ../nimbus/config import getDefaultDataDir, ConfigStatus, processInteger, PublicNetwork
import
std/[os, parseopt, strutils],
eth/p2p, stint
from ../nimbus/config import
getDefaultDataDir,
ConfigStatus,
processInteger,
MainNet,
RopstenNet,
RinkebyNet,
GoerliNet,
KovanNet
export ConfigStatus
@ -9,7 +20,7 @@ type
head*: Uint256
maxBlocks*: int
numCommits*: int
netId*: PublicNetwork
netId*: NetworkId
var premixConfig {.threadvar.}: PremixConfiguration
@ -38,7 +49,7 @@ proc processU256(val: string, o: var Uint256): ConfigStatus =
o = parse(val, Uint256)
result = Success
proc processNetId(val: string, o: var PublicNetwork): ConfigStatus =
proc processNetId(val: string, o: var NetworkId): ConfigStatus =
case val.toLowerAscii()
of "main": o = MainNet
of "ropsten": o = RopstenNet
@ -46,9 +57,9 @@ proc processNetId(val: string, o: var PublicNetwork): ConfigStatus =
of "goerli": o = GoerliNet
of "kovan": o = KovanNet
template checkArgument(fun, o: untyped) =
template checkArgument(fun, o, value: untyped) =
## Checks if arguments got processed successfully
var res = (fun)(value, o)
let res = fun(value, o)
if res == Success:
continue
elif res == ErrorParseOption:
@ -76,14 +87,14 @@ proc processArguments*(msg: var string): ConfigStatus =
case key.toLowerAscii()
of "datadir": config.dataDir = value
of "maxblocks":
checkArgument processInteger, config.maxBlocks
checkArgument(processInteger, config.maxBlocks, value)
of "head":
checkArgument processU256, config.head
checkArgument(processU256, config.head, value)
of "numcommits":
checkArgument processInteger, config.numCommits
checkArgument(processInteger, config.numCommits, value)
config.numCommits = max(config.numCommits, 512)
of "netid":
checkArgument processNetId, config.netId
checkArgument(processNetId, config.netId, value)
else:
msg = "Unknown option " & key
if value.len > 0: msg = msg & " : " & value

View File

@ -61,11 +61,11 @@ template runTests(name: string, hex: bool, calculator: typed) =
check diff == t.currentDifficulty
proc difficultyMain*() =
let mainnetConfig = publicChainConfig(MainNet)
let mainnetConfig = chainConfig(MainNet)
func calcDifficultyMainNetWork(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
mainnetConfig.calcDifficulty(timeStamp, parent)
let ropstenConfig = publicChainConfig(RopstenNet)
let ropstenConfig = chainConfig(RopstenNet)
func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
ropstenConfig.calcDifficulty(timeStamp, parent)

View File

@ -1,5 +1,5 @@
import
unittest2, eth/common, eth/trie/db,
unittest2, eth/[common, p2p], eth/trie/db,
../nimbus/db/db_chain, ../nimbus/p2p/chain,
../nimbus/config
@ -74,7 +74,7 @@ const
(blockNumber: 5000000'u64, id: (crc: 0x757a1c47'u32, nextFork: 0'u64)), # Future Berlin block
]
template runTest(network: PublicNetwork) =
template runTest(network: untyped) =
test network.astToStr:
var
memDB = newMemoryDB()

View File

@ -49,7 +49,7 @@ proc setupChain(chainDB: BaseChainDB) =
conf.customGenesis.genesis.timestamp = genesis.header.timestamp
if not parseGenesisAlloc($(jn["pre"]), conf.customGenesis.genesis.alloc):
quit(QuitFailure)
chainDB.initializeEmptyDb()
let blocks = jn["blocks"]
@ -69,7 +69,7 @@ proc setupChain(chainDB: BaseChainDB) =
proc graphqlMain*() =
let conf = getConfiguration()
conf.net.networkId = NetworkId(CustomNet)
conf.net.networkId = CustomNet
conf.customGenesis.config = ChainConfig(
chainId : MainNet.ChainId,
byzantiumBlock : 0.toBlockNumber,
@ -84,7 +84,7 @@ proc graphqlMain*() =
ethNode = setupEthNode(eth)
chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false,
id = toPublicNetwork(conf.net.networkId)
conf.net.networkId
)
chainDB.setupChain()

View File

@ -142,7 +142,7 @@ proc doTests {.async.} =
debugEcho unlock.error
doAssert(unlock.isOk)
defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain)
defaultGenesisBlockForNetwork(conf.net.networkId).commit(chain)
doAssert(canonicalHeadHashKey().toOpenArray in chain.db)
let env = setupEnv(chain, signer, ks2, conf)

View File

@ -19,4 +19,7 @@ import
../premix/hunter,
../premix/regress,
./tracerTestGen,
./persistBlockTestGen
./persistBlockTestGen,
../hive_integration/nodocker/consensus/extract_consensus_data,
../hive_integration/nodocker/consensus/consensus_sim,
../hive_integration/nodocker/graphql/graphql_sim