config: replace stdlib parseOpt with nim-confutils

fixes #581
This commit is contained in:
jangko 2021-09-11 21:58:01 +07:00
parent 6192cd7dc1
commit 69f2a0f95a
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
32 changed files with 873 additions and 1013 deletions

View File

@ -8,7 +8,7 @@
# those terms.
import
std/[os, parseopt, strformat, json],
std/[os, strformat, json],
eth/[common, trie/db], stew/byteutils,
../../../nimbus/db/db_chain,
../../../nimbus/[genesis, config, conf_utils],
@ -16,19 +16,13 @@ import
proc processNode(genesisFile, chainFile,
lastBlockHash: string, testStatusIMPL: var TestStatus) =
var msg: string
var opt = initOptParser("--customnetwork:" & genesisFile)
let res = processArguments(msg, opt)
if res != Success:
echo msg
quit(QuitFailure)
let
conf = getConfiguration()
conf = makeConfig(@["--customnetwork:" & genesisFile])
chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false,
conf.net.networkId,
conf.customNetwork
conf.networkId.get,
conf.customNetwork.get()
)
initializeEmptyDb(chainDB)

View File

@ -8,7 +8,7 @@
# those terms.
import
std/[os, parseopt, json],
std/[os, json],
eth/[p2p, trie/db], ../../../nimbus/db/db_chain,
../../../nimbus/sync/protocol_eth65,
../../../nimbus/[genesis, config, conf_utils, context],
@ -66,21 +66,14 @@ proc processNode(ctx: GraphqlRef, node: JsonNode, fileName: string, testStatusIM
ctx.purgeNames(savePoint)
proc main() =
var msg: string
var opt = initOptParser("--customnetwork:" & genesisFile)
let res = processArguments(msg, opt)
if res != Success:
echo msg
quit(QuitFailure)
let
conf = makeConfig(@["--customnetwork:" & genesisFile])
ethCtx = newEthContext()
conf = getConfiguration()
ethNode = setupEthNode(conf, ethCtx, eth)
chainDB = newBaseChainDB(newMemoryDb(),
pruneTrie = false,
conf.net.networkId,
conf.customNetwork
conf.networkId.get,
conf.customNetwork.get
)
initializeEmptyDb(chainDB)

View File

@ -9,12 +9,13 @@
import
std/[tables, strutils, options, times],
eth/[common, rlp], stint, stew/[byteutils],
eth/[common, rlp, p2p], stint, stew/[byteutils],
nimcrypto/hash,
json_serialization, chronicles,
json_serialization/std/options as jsoptions,
json_serialization/std/tables as jstable,
json_serialization/lexer
json_serialization/lexer,
./forks
type
CliqueOptions = object
@ -94,6 +95,16 @@ type
config : ChainOptions
genesis: Genesis
const
CustomNet* = 0.NetworkId
# these are public network id
MainNet* = 1.NetworkId
# No longer used: MordenNet = 2
RopstenNet* = 3.NetworkId
RinkebyNet* = 4.NetworkId
GoerliNet* = 5.NetworkId
KovanNet* = 42.NetworkId
proc read(rlp: var Rlp, x: var AddressBalance, _: type EthAddress): EthAddress {.inline.} =
let val = rlp.read(UInt256).toByteArrayBE()
result[0 .. ^1] = val.toOpenArray(12, val.high)
@ -213,3 +224,98 @@ proc parseGenesisAlloc*(data: string, ga: var GenesisAlloc): bool =
return false
return true
proc toFork*(c: ChainConfig, number: BlockNumber): Fork =
if number >= c.londonBlock: FkLondon
elif number >= c.berlinBlock: FkBerlin
elif number >= c.istanbulBlock: FkIstanbul
elif number >= c.petersburgBlock: FkPetersburg
elif number >= c.constantinopleBlock: FkConstantinople
elif number >= c.byzantiumBlock: FkByzantium
elif number >= c.eip158Block: FkSpurious
elif number >= c.eip150Block: FkTangerine
elif number >= c.homesteadBlock: FkHomestead
else: FkFrontier
proc chainConfig*(id: NetworkId, cn: CustomNetwork): ChainConfig =
# For some public networks, NetworkId and ChainId value are identical
# but that is not always the case
result = case id
of MainNet:
ChainConfig(
poaEngine: false, # TODO: use real engine conf: PoW
chainId: MainNet.ChainId,
homesteadBlock: 1_150_000.toBlockNumber, # 14/03/2016 20:49:53
daoForkBlock: 1_920_000.toBlockNumber,
daoForkSupport: true,
eip150Block: 2_463_000.toBlockNumber, # 18/10/2016 17:19:31
eip150Hash: toDigest("2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
eip155Block: 2_675_000.toBlockNumber, # 22/11/2016 18:15:44
eip158Block: 2_675_000.toBlockNumber,
byzantiumBlock: 4_370_000.toBlockNumber, # 16/10/2017 09:22:11
constantinopleBlock: 7_280_000.toBlockNumber, # Never Occured in MainNet
petersburgBlock:7_280_000.toBlockNumber, # 28/02/2019 07:52:04
istanbulBlock: 9_069_000.toBlockNumber, # 08/12/2019 12:25:09
muirGlacierBlock: 9_200_000.toBlockNumber, # 02/01/2020 08:30:49
berlinBlock: 12_244_000.toBlockNumber, # 15/04/2021 10:07:03
londonBlock: 12_965_000.toBlockNumber, # 05/08/2021 12:33:42
)
of RopstenNet:
ChainConfig(
poaEngine: false, # TODO: use real engine conf: PoW
chainId: RopstenNet.ChainId,
homesteadBlock: 0.toBlockNumber,
daoForkSupport: false,
eip150Block: 0.toBlockNumber,
eip150Hash: toDigest("41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
eip155Block: 10.toBlockNumber,
eip158Block: 10.toBlockNumber,
byzantiumBlock: 1_700_000.toBlockNumber,
constantinopleBlock: 4_230_000.toBlockNumber,
petersburgBlock:4_939_394.toBlockNumber,
istanbulBlock: 6_485_846.toBlockNumber,
muirGlacierBlock: 7_117_117.toBlockNumber,
berlinBlock: 9_812_189.toBlockNumber,
londonBlock: 10_499_401.toBlockNumber # June 24, 2021
)
of RinkebyNet:
ChainConfig(
poaEngine: true, # TODO: use real engine conf: PoA
chainId: RinkebyNet.ChainId,
homesteadBlock: 1.toBlockNumber,
daoForkSupport: false,
eip150Block: 2.toBlockNumber,
eip150Hash: toDigest("9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
eip155Block: 3.toBlockNumber,
eip158Block: 3.toBlockNumber,
byzantiumBlock: 1_035_301.toBlockNumber,
constantinopleBlock: 3_660_663.toBlockNumber,
petersburgBlock:4_321_234.toBlockNumber,
istanbulBlock: 5_435_345.toBlockNumber,
muirGlacierBlock: 8_290_928.toBlockNumber, # never occured in rinkeby network
berlinBlock: 8_290_928.toBlockNumber,
londonBlock: 8_897_988.toBlockNumber # July 7, 2021
)
of GoerliNet:
ChainConfig(
poaEngine: true, # TODO: use real engine conf: PoA
chainId: GoerliNet.ChainId,
homesteadBlock: 0.toBlockNumber,
daoForkSupport: false,
eip150Block: 0.toBlockNumber,
eip150Hash: toDigest("0000000000000000000000000000000000000000000000000000000000000000"),
eip155Block: 0.toBlockNumber,
eip158Block: 0.toBlockNumber,
byzantiumBlock: 0.toBlockNumber,
constantinopleBlock: 0.toBlockNumber,
petersburgBlock: 0.toBlockNumber,
istanbulBlock: 1_561_651.toBlockNumber,
muirGlacierBlock: 4_460_644.toBlockNumber, # never occured in goerli network
berlinBlock: 4_460_644.toBlockNumber,
londonBlock: 5_062_605.toBlockNumber # June 30, 2021
)
else:
# everything else will use CustomNet config
trace "Custom genesis block configuration loaded", conf=cn.config
cn.config

View File

@ -9,8 +9,7 @@
import
chronicles, eth/[common, rlp], stew/io2,
./p2p/chain, ./db/[db_chain, select_backend],
config
./p2p/chain, ./db/[db_chain, select_backend]
type
# trick the rlp decoder

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@ import
stew/[byteutils], eth/trie/[hexary, db],
eth/[common, rlp, p2p], chronicles,
../errors, ../constants, ./storage_types,
../utils, ../config, ../chain_config
../utils, ../chain_config
type
BaseChainDB* = ref object

View File

@ -1,9 +1,9 @@
import
std/[json, times, tables],
std/[times, tables],
eth/[common, rlp, trie, p2p], stew/[byteutils],
chronicles, eth/trie/db,
./db/[db_chain, state_db],
./genesis_alloc, ./config, ./constants,
./genesis_alloc, ./constants,
./chain_config, ./forks, ./p2p/gaslimit
proc genesisBlockForNetwork*(id: NetworkId, cn: CustomNetwork): Genesis =

View File

@ -11,6 +11,7 @@ import
std/[strutils, times],
stew/[results, byteutils], stint,
eth/[common, rlp], chronos,
stew/shims/net,
graphql, graphql/graphql as context,
graphql/common/types, graphql/httpserver,
graphql/instruments/query_complexity,
@ -1221,11 +1222,12 @@ proc setupGraphqlContext*(chainDB: BaseChainDB, ethNode: EthereumNode): GraphqlC
ctx.initEthApi()
ctx
proc setupGraphqlHttpServer*(conf: NimbusConfiguration,
proc setupGraphqlHttpServer*(conf: NimbusConf,
chainDB: BaseChainDB, ethNode: EthereumNode): GraphqlHttpServerRef =
let socketFlags = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}
let ctx = setupGraphqlContext(chainDB, ethNode)
let sres = GraphqlHttpServerRef.new(ctx, conf.graphql.address, socketFlags = socketFlags)
let address = initTAddress(conf.graphqlAddress, conf.graphqlPort)
let sres = GraphqlHttpServerRef.new(ctx, address, socketFlags = socketFlags)
if sres.isErr():
echo sres.error
quit(QuitFailure)

View File

@ -12,6 +12,7 @@ import
import
os, strutils, net, options,
stew/shims/net as stewNet,
eth/keys, db/[storage_types, db_chain, select_backend],
eth/common as eth_common, eth/p2p as eth_p2p,
chronos, json_rpc/rpcserver, chronicles,
@ -41,21 +42,28 @@ type
sealingEngine: SealingEngineRef
ctx: EthContext
proc start(nimbus: NimbusNode) =
var conf = getConfiguration()
template init(T: type RpcHttpServer, ip: ValidIpAddress, port: Port): T =
newRpcHttpServer([initTAddress(ip, port)])
template init(T: type RpcWebSocketServer, ip: ValidIpAddress, port: Port): T =
newRpcWebSocketServer(initTAddress(ip, port))
proc start(nimbus: NimbusNode, conf: NimbusConf) =
## logging
setLogLevel(conf.debug.logLevel)
if len(conf.debug.logFile) != 0:
setLogLevel(conf.logLevel)
if conf.logFile.isSome:
let logFile = string conf.logFile.get()
defaultChroniclesStream.output.outFile = nil # to avoid closing stdout
discard defaultChroniclesStream.output.open(conf.debug.logFile, fmAppend)
discard defaultChroniclesStream.output.open(logFile, fmAppend)
createDir(conf.dataDir)
let trieDB = trieDB newChainDb(conf.dataDir)
createDir(string conf.dataDir)
let trieDB = trieDB newChainDb(string conf.dataDir)
let networkId = conf.networkId.get()
let customNetwork = conf.customNetwork.get()
var chainDB = newBaseChainDB(trieDB,
conf.prune == PruneMode.Full,
conf.net.networkId,
conf.customNetwork
conf.pruneMode == PruneMode.Full,
networkId,
customNetwork
)
chainDB.populateProgress()
@ -63,55 +71,55 @@ proc start(nimbus: NimbusNode) =
initializeEmptyDb(chainDb)
doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
if conf.importFile.len > 0:
if string(conf.importBlocks).len > 0:
# success or not, we quit after importing blocks
if not importRlpBlock(conf.importFile, chainDB):
if not importRlpBlock(string conf.importBlocks, chainDB):
quit(QuitFailure)
else:
quit(QuitSuccess)
if conf.keyStore.len > 0:
let res = nimbus.ctx.am.loadKeystores(conf.keyStore)
if string(conf.keyStore).len > 0:
let res = nimbus.ctx.am.loadKeystores(string conf.keyStore)
if res.isErr:
echo res.error()
quit(QuitFailure)
if conf.importKey.len > 0:
let res = nimbus.ctx.am.importPrivateKey(conf.importKey)
if string(conf.importKey).len > 0:
let res = nimbus.ctx.am.importPrivateKey(string conf.importKey)
if res.isErr:
echo res.error()
quit(QuitFailure)
# metrics logging
if conf.debug.logMetrics:
if conf.logMetricsEnabled:
# https://github.com/nim-lang/Nim/issues/17369
var logMetrics: proc(udata: pointer) {.gcsafe, raises: [Defect].}
logMetrics = proc(udata: pointer) =
{.gcsafe.}:
let registry = defaultRegistry
info "metrics", registry
discard setTimer(Moment.fromNow(conf.debug.logMetricsInterval.seconds), logMetrics)
discard setTimer(Moment.fromNow(conf.debug.logMetricsInterval.seconds), logMetrics)
discard setTimer(Moment.fromNow(conf.logMetricsInterval.seconds), logMetrics)
discard setTimer(Moment.fromNow(conf.logMetricsInterval.seconds), logMetrics)
## Creating P2P Server
let kpres = nimbus.ctx.hexToKeyPair(conf.net.nodekey)
let kpres = nimbus.ctx.hexToKeyPair(conf.nodeKeyHex)
if kpres.isErr:
echo kpres.error()
quit(QuitFailure)
let keypair = kpres.get()
var address = Address(
ip: conf.listenAddress,
tcpPort: conf.tcpPort,
udpPort: conf.udpPort
)
var address: Address
address.ip = parseIpAddress("0.0.0.0")
address.tcpPort = Port(conf.net.bindPort)
address.udpPort = Port(conf.net.discPort)
if conf.net.nat == NatNone:
if conf.net.externalIP != "":
if conf.nat.hasExtIp:
# any required port redirection is assumed to be done by hand
address.ip = parseIpAddress(conf.net.externalIP)
address.ip = conf.nat.extIp
else:
# automated NAT traversal
let extIP = getExternalIP(conf.net.nat)
let extIP = getExternalIP(conf.nat.nat)
# This external IP only appears in the logs, so don't worry about dynamic
# IPs. Don't remove it either, because the above call does initialisation
# and discovery for NAT-related objects.
@ -123,55 +131,56 @@ proc start(nimbus: NimbusNode) =
if extPorts.isSome:
(address.tcpPort, address.udpPort) = extPorts.get()
nimbus.ethNode = newEthereumNode(keypair, address, conf.net.networkId,
nil, conf.net.ident,
nimbus.ethNode = newEthereumNode(keypair, address, networkId,
nil, conf.agentString,
addAllCapabilities = false,
minPeers = conf.net.maxPeers)
minPeers = conf.maxPeers)
# Add protocol capabilities based on protocol flags
if ProtocolFlags.Eth in conf.net.protocols:
if ProtocolFlag.Eth in conf.protocols.value:
nimbus.ethNode.addCapability eth
if ProtocolFlags.Les in conf.net.protocols:
if ProtocolFlag.Les in conf.protocols.value:
nimbus.ethNode.addCapability les
# chainRef: some name to avoid module-name/filed/function misunderstandings
let chainRef = newChain(chainDB)
nimbus.ethNode.chain = chainRef
if conf.verifyFromOk:
chainRef.extraValidation = 0 < conf.verifyFrom
chainRef.verifyFrom = conf.verifyFrom
if conf.verifyFrom.isSome:
let verifyFrom = conf.verifyFrom.get()
chainRef.extraValidation = 0 < verifyFrom
chainRef.verifyFrom = verifyFrom
## Creating RPC Server
if RpcFlags.Enabled in conf.rpc.flags:
nimbus.rpcServer = newRpcHttpServer(conf.rpc.binds)
# Creating RPC Server
if conf.rpcEnabled:
nimbus.rpcServer = RpcHttpServer.init(conf.rpcAddress, conf.rpcPort)
setupCommonRpc(nimbus.ethNode, conf, nimbus.rpcServer)
# Enable RPC APIs based on RPC flags and protocol flags
if RpcFlags.Eth in conf.rpc.flags and ProtocolFlags.Eth in conf.net.protocols:
if RpcFlag.Eth in conf.rpcApi.value and ProtocolFlag.Eth in conf.protocols.value:
setupEthRpc(nimbus.ethNode, nimbus.ctx, chainDB, nimbus.rpcServer)
if RpcFlags.Debug in conf.rpc.flags:
if RpcFlag.Debug in conf.rpcApi.value:
setupDebugRpc(chainDB, nimbus.rpcServer)
# Creating Websocket RPC Server
if RpcFlags.Enabled in conf.ws.flags:
doAssert(conf.ws.binds.len > 0)
nimbus.wsRpcServer = newRpcWebSocketServer(conf.ws.binds[0])
setupCommonRpc(nimbus.ethNode, conf, nimbus.wsRpcServer)
# Enable Websocket RPC APIs based on RPC flags and protocol flags
if RpcFlags.Eth in conf.ws.flags and ProtocolFlags.Eth in conf.net.protocols:
setupEthRpc(nimbus.ethNode, nimbus.ctx, chainDB, nimbus.wsRpcServer)
if RpcFlags.Debug in conf.ws.flags:
setupDebugRpc(chainDB, nimbus.wsRpcServer)
## Starting servers
if RpcFlags.Enabled in conf.rpc.flags:
nimbus.rpcServer.rpc("admin_quit") do() -> string:
{.gcsafe.}:
nimbus.state = Stopping
result = "EXITING"
nimbus.rpcServer.start()
if conf.graphql.enabled:
# Creating Websocket RPC Server
if conf.wsEnabled:
nimbus.wsRpcServer = RpcWebSocketServer.init(conf.wsAddress, conf.wsPort)
setupCommonRpc(nimbus.ethNode, conf, nimbus.wsRpcServer)
# Enable Websocket RPC APIs based on RPC flags and protocol flags
if RpcFlag.Eth in conf.wsApi.value and ProtocolFlag.Eth in conf.protocols.value:
setupEthRpc(nimbus.ethNode, nimbus.ctx, chainDB, nimbus.wsRpcServer)
if RpcFlag.Debug in conf.wsApi.value:
setupDebugRpc(chainDB, nimbus.wsRpcServer)
nimbus.wsRpcServer.start()
if conf.graphqlEnabled:
nimbus.graphqlServer = setupGraphqlHttpServer(conf, chainDB, nimbus.ethNode)
nimbus.graphqlServer.start()
@ -186,25 +195,20 @@ proc start(nimbus: NimbusNode) =
nimbus.sealingEngine.start()
# metrics server
if conf.net.metricsServer:
let metricsAddress = "127.0.0.1"
info "Starting metrics HTTP server", address = metricsAddress, port = conf.net.metricsServerPort
startMetricsHttpServer(metricsAddress, Port(conf.net.metricsServerPort))
if conf.metricsEnabled:
info "Starting metrics HTTP server", address = conf.metricsAddress, port = conf.metricsPort
startMetricsHttpServer($conf.metricsAddress, conf.metricsPort)
# Connect directly to the static nodes
for enode in conf.net.staticNodes:
for enode in conf.staticNodes.value:
asyncCheck nimbus.ethNode.peerPool.connectToNode(newNode(enode))
# Connect via discovery
if conf.net.customBootNodes.len > 0:
# override the default bootnodes from public network
waitFor nimbus.ethNode.connectToNetwork(conf.net.customBootNodes,
enableDiscovery = NoDiscover notin conf.net.flags)
else:
waitFor nimbus.ethNode.connectToNetwork(conf.net.bootNodes,
enableDiscovery = NoDiscover notin conf.net.flags)
let bootNodes = conf.getBootNodes()
waitFor nimbus.ethNode.connectToNetwork(bootNodes,
enableDiscovery = not conf.noDiscover)
if ProtocolFlags.Eth in conf.net.protocols:
if ProtocolFlag.Eth in conf.protocols.value:
# TODO: temp code until the CLI/RPC interface is fleshed out
let status = waitFor nimbus.ethNode.fastBlockchainSync()
if status != syncSuccess:
@ -214,17 +218,18 @@ proc start(nimbus: NimbusNode) =
# it might have been set to "Stopping" with Ctrl+C
nimbus.state = Running
proc stop*(nimbus: NimbusNode) {.async, gcsafe.} =
proc stop*(nimbus: NimbusNode, conf: NimbusConf) {.async, gcsafe.} =
trace "Graceful shutdown"
var conf = getConfiguration()
if RpcFlags.Enabled in conf.rpc.flags:
if conf.rpcEnabled:
nimbus.rpcServer.stop()
if conf.graphql.enabled:
if conf.wsEnabled:
nimbus.wsRpcServer.start()
if conf.graphqlEnabled:
await nimbus.graphqlServer.stop()
if conf.engineSigner != ZERO_ADDRESS:
await nimbus.sealingEngine.stop()
proc process*(nimbus: NimbusNode) =
proc process*(nimbus: NimbusNode, conf: NimbusConf) =
# Main event loop
while nimbus.state == Running:
try:
@ -234,7 +239,7 @@ proc process*(nimbus: NimbusNode) =
discard e # silence warning when chronicles not activated
# Stop loop
waitFor nimbus.stop()
waitFor nimbus.stop(conf)
when isMainModule:
var nimbus = NimbusNode(state: Starting, ctx: newEthContext())
@ -248,23 +253,11 @@ when isMainModule:
echo "\nCtrl+C pressed. Waiting for a graceful shutdown."
setControlCHook(controlCHandler)
var message: string
## Print Nimbus header
echo NimbusHeader
## Show logs on stdout until we get the user's logging choice
discard defaultChroniclesStream.output.open(stdout)
## Processing command line arguments
if processArguments(message) != ConfigStatus.Success:
echo message
quit(QuitFailure)
else:
if len(message) > 0:
echo message
quit(QuitSuccess)
nimbus.start()
nimbus.process()
let conf = makeConfig()
nimbus.start(conf)
nimbus.process(conf)

View File

@ -10,7 +10,7 @@
import
std/[strformat],
../../config,
../../chain_config,
../../db/accounts_cache,
../../forks,
../../vm_state,

View File

@ -16,8 +16,7 @@ import
../constants,
../errors,
../chain_config,
../forks,
../config
../forks
const
EIP1559_BASE_FEE_CHANGE_DENOMINATOR* = ##\

View File

@ -25,20 +25,20 @@ type
ip : string # address string
ports : NodePorts
proc setupCommonRPC*(node: EthereumNode, conf: NimbusConfiguration, server: RpcServer) =
proc setupCommonRPC*(node: EthereumNode, conf: NimbusConf, server: RpcServer) =
server.rpc("web3_clientVersion") do() -> string:
result = NimbusIdent
result = conf.agentString
server.rpc("web3_sha3") do(data: HexDataStr) -> string:
var rawdata = nimcrypto.fromHex(data.string[2 .. ^1])
result = "0x" & $keccak_256.digest(rawdata)
server.rpc("net_version") do() -> string:
result = $conf.net.networkId
result = $conf.networkId.get()
server.rpc("net_listening") do() -> bool:
let numPeers = node.peerPool.connectedNodes.len
result = numPeers < conf.net.maxPeers
result = numPeers < conf.maxPeers
server.rpc("net_peerCount") do() -> HexQuantityStr:
let peerCount = uint node.peerPool.connectedNodes.len
@ -48,7 +48,7 @@ proc setupCommonRPC*(node: EthereumNode, conf: NimbusConfiguration, server: RpcS
let enode = toEnode(node)
result = NodeInfo(
id: node.discovery.thisNode.id.toHex,
name: NimbusIdent,
name: conf.agentString,
enode: $enode,
ip: $enode.address.ip,
ports: NodePorts(

View File

@ -11,7 +11,7 @@ import
times, options, tables,
json_rpc/rpcserver, hexstrings, stint, stew/byteutils,
eth/[common, keys, rlp, p2p], nimcrypto,
".."/[transaction, config, vm_state, constants, utils, context],
".."/[transaction, vm_state, constants, utils, context],
../db/[db_chain, state_db],
rpc_types, rpc_utils,
../transaction/call_evm

View File

@ -15,8 +15,7 @@ import
"."/p2p/clique/[clique_defs,
clique_desc,
clique_cfg,
clique_sealer,
clique_snapshot],
clique_sealer],
./p2p/gaslimit,
"."/[chain_config, utils, context]
@ -32,7 +31,7 @@ type
ctx: EthContext
signer: EthAddress
proc validateSealer*(conf: NimbusConfiguration, ctx: EthContext, chain: Chain): Result[void, string] =
proc validateSealer*(conf: NimbusConf, ctx: EthContext, chain: Chain): Result[void, string] =
if conf.engineSigner == ZERO_ADDRESS:
return err("signer address should not zero, use --engine-signer to set signer address")

View File

@ -10,7 +10,7 @@ import
eth/common/eth_types, stint, options, stew/byteutils,
".."/[vm_types, vm_state, vm_internals, vm_gas_costs, forks],
".."/[db/db_chain, db/accounts_cache, transaction], eth/trie/db,
".."/[config, utils, rpc/hexstrings],
".."/[chain_config, utils, rpc/hexstrings],
./call_common
type

View File

@ -11,7 +11,7 @@
import
std/[json, macros, options, sets, strformat, tables],
../../stateless/[witness_from_tree, witness_types],
../config,
../chain_config,
../constants,
../db/[db_chain, accounts_cache],
../errors,

View File

@ -19,7 +19,7 @@ when not defined(vm2_enabled):
{.error: "NIM flag must be set: -d:vm2_enabled".}
import
../config,
../chain_config,
../constants,
../forks,
../db/accounts_cache,

View File

@ -8,13 +8,18 @@
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
func vmName(): string =
when defined(evmc_enabled):
{.warning: "*** Compiling with EVMC enabled".}
"evmc"
elif defined(vm2_enabled):
{.warning: "*** Compiling with VM2 enabled".}
"vm2"
else:
{.warning: "*** Compiling with standard NIM VM enabled".}
"nimvm"
const
VmName* = vmName()
warningMSg = "*** Compiling with " & VmName & " enabled"
{.warning: warningMsg.}
{.used.}

View File

@ -1,20 +1,24 @@
import
std/[os, parseopt, strutils],
eth/p2p, stint
eth/p2p, stint, ../nimbus/config
from ../nimbus/config import
getDefaultDataDir,
ConfigStatus,
processInteger,
from ../nimbus/chain_config import
MainNet,
RopstenNet,
RinkebyNet,
GoerliNet,
KovanNet
export ConfigStatus
type
ConfigStatus* = enum
## Configuration status flags
Success, ## Success
EmptyOption, ## No options in category
ErrorUnknownOption, ## Unknown option in command line found
ErrorParseOption, ## Error in parsing command line option
ErrorIncorrectOption, ## Option has incorrect value
Error ## Unspecified error
PremixConfiguration* = ref object
dataDir*: string
head*: Uint256
@ -26,12 +30,20 @@ var premixConfig {.threadvar.}: PremixConfiguration
proc getConfiguration*(): PremixConfiguration {.gcsafe.}
proc processInteger(v: string, o: var int): ConfigStatus =
## Convert string to integer.
try:
o = parseInt(v)
result = Success
except ValueError:
result = ErrorParseOption
proc initConfiguration(): PremixConfiguration =
result = new PremixConfiguration
const dataDir = getDefaultDataDir()
const dataDir = defaultDataDir()
result.dataDir = getHomeDir() / dataDir
result.dataDir = dataDir
result.head = 0.u256
result.maxBlocks = 0
result.numCommits = 128

View File

@ -1,6 +1,6 @@
import
json, strutils, times, options, os,
eth/[rlp, common], httputils, nimcrypto, chronicles,
eth/[rlp, common], httputils, nimcrypto,
stint, stew/byteutils
import ../nimbus/transaction

View File

@ -9,8 +9,7 @@ import
eth/trie/[hexary, db],
../nimbus/db/[storage_types, db_chain, select_backend],
../nimbus/[genesis],
../nimbus/p2p/chain,
../nimbus/chain_config
../nimbus/p2p/chain
const
manualCommit = nimbus_db_backend == "lmdb"

View File

@ -37,4 +37,5 @@ cliBuilder:
./test_misc,
./test_graphql,
./test_lru_cache,
./test_clique
./test_clique,
./test_configuration

View File

@ -2,8 +2,7 @@ import
json, eth/common, stint, chronicles, eth/rlp,
eth/trie/db, ../nimbus/db/[db_chain, capturedb, select_backend],
../nimbus/[tracer, config],
../nimbus/p2p/chain,
../nimbus/chain_config
../nimbus/p2p/chain
proc dumpTest(chainDB: BaseChainDB, blockNumber: int) =
let
@ -49,8 +48,8 @@ proc main() {.used.} =
# nimbus --rpcapi: eth, debug --prune: archive
var conf = getConfiguration()
let db = newChainDb(conf.dataDir)
var conf = makeConfig()
let db = newChainDb(string conf.dataDir)
let trieDB = trieDB db
let chainDB = newBaseChainDB(trieDB, false)
@ -105,17 +104,6 @@ proc main() {.used.} =
chainDB.dumpTest(4_370_000) # Byzantium first block
when isMainModule:
var message: string
## Processing command line arguments
if processArguments(message) != Success:
echo message
quit(QuitFailure)
else:
if len(message) > 0:
echo message
quit(QuitSuccess)
try:
main()
except:

View File

@ -10,7 +10,7 @@
import
std/[algorithm, os, sequtils, strformat,
strutils, times, parseopt, tables],
strutils, times, tables],
../nimbus/db/db_chain,
../nimbus/p2p/[chain,
clique,
@ -244,23 +244,17 @@ proc cliqueMiscTests() =
engineSigner = "658bdf435d810c91414ec09147daa6db62406379"
privateKey = "tests" / "test_clique" / "private.key"
var msg: string
var opt = initOptParser("--engine-signer:$1 --import-key:$2" % [engineSigner, privateKey])
let res = processArguments(msg, opt)
check res == Success
let
signer = hexToByteArray[20](engineSigner)
conf = makeConfig(@["--engine-signer:" & engineSigner, "--import-key:" & privateKey])
ctx = newEthContext()
conf = getConfiguration()
check ctx.am.importPrivateKey(conf.importKey).isOk()
check ctx.am.getAccount(signer).isOk()
check ctx.am.importPrivateKey(string conf.importKey).isOk()
check ctx.am.getAccount(conf.engineSigner).isOk()
proc signFunc(signer: EthAddress, message: openArray[byte]): Result[RawSignature, cstring] {.gcsafe.} =
let
hashData = keccakHash(message)
acc = ctx.am.getAccount(signer).tryGet()
acc = ctx.am.getAccount(conf.engineSigner).tryGet()
rawSign = sign(acc.privateKey, SkMessage(hashData.data)).toRaw
ok(rawSign)
@ -270,7 +264,7 @@ proc cliqueMiscTests() =
header.extraData.setLen(EXTRA_VANITY)
header.extraData.add 0.byte.repeat(EXTRA_SEAL)
let signature = signerFn(signer, header.encodeSealHeader).get()
let signature = signerFn(conf.engineSigner, header.encodeSealHeader).get()
let extraLen = header.extraData.len
if EXTRA_SEAL < extraLen:
header.extraData.setLen(extraLen - EXTRA_SEAL)
@ -278,7 +272,7 @@ proc cliqueMiscTests() =
let resAddr = ecRecover(header)
check resAddr.isOk
check resAddr.value == signer
check resAddr.value == conf.engineSigner
# ------------------------------------------------------------------------------
# Main function(s)

View File

@ -0,0 +1,76 @@
import
std/[os],
pkg/[unittest2, confutils],
eth/[p2p, common],
../nimbus/[config, chain_config]
proc `==`(a, b: ChainId): bool =
a.int == b.int
proc configurationMain*() =
suite "configuration test suite":
const genesisFile = "tests" / "customgenesis" / "calaveras.json"
test "data-dir and key-store":
let conf = makeConfig(@[]) # don't use makeConfig default cmdLine from inside all_tests
check conf.dataDir.string == defaultDataDir()
check conf.keyStore.string == defaultKeystoreDir()
let cc = makeConfig(@["-d:apple\\bin", "-k:banana/bin"])
check cc.dataDir.string == "apple\\bin"
check cc.keyStore.string == "banana/bin"
let dd = makeConfig(@["--data-dir:apple\\bin", "--key-store:banana/bin"])
check dd.dataDir.string == "apple\\bin"
check dd.keyStore.string == "banana/bin"
test "prune-mode":
let aa = makeConfig(@[])
check aa.pruneMode == PruneMode.Full
let bb = makeConfig(@["--prune-mode:full"])
check bb.pruneMode == PruneMode.Full
let cc = makeConfig(@["--prune-mode:archive"])
check cc.pruneMode == PruneMode.Archive
let dd = makeConfig(@["-p:archive"])
check dd.pruneMode == PruneMode.Archive
test "import":
let aa = makeConfig(@[])
check aa.importBlocks.string == ""
let bb = makeConfig(@["--import-blocks:" & genesisFile])
check bb.importBlocks.string == genesisFile
let cc = makeConfig(@["-b:" & genesisFile])
check cc.importBlocks.string == genesisFile
test "network-id":
let aa = makeConfig(@[])
check aa.networkId.get() == MainNet
check aa.mainnet == true
check aa.customNetwork.get() == CustomNetwork()
let conf = makeConfig(@["--custom-network:" & genesisFile, "--network-id:345"])
check conf.networkId.get() == 345.NetworkId
test "network-id first, custom-network next":
let conf = makeConfig(@["--network-id:678", "--custom-network:" & genesisFile])
check conf.networkId.get() == 678.NetworkId
test "network-id not set, copy from chainId of customnetwork":
let conf = makeConfig(@["--custom-network:" & genesisFile])
check conf.networkId.get() == 123.NetworkId
test "network-id not set, goerli set":
let conf = makeConfig(@["--goerli"])
check conf.networkId.get() == GoerliNet
test "network-id set, goerli set":
let conf = makeConfig(@["--goerli", "--network-id:123"])
check conf.networkId.get() == GoerliNet
when isMainModule:
configurationMain()

View File

@ -1,8 +1,7 @@
import unittest2, strutils, tables, os, json,
../nimbus/utils/difficulty, stint, times,
eth/common, test_helpers, stew/byteutils,
../nimbus/constants, ../nimbus/config,
../nimbus/chain_config
../nimbus/constants, ../nimbus/chain_config
type
Tester = object

View File

@ -1,7 +1,7 @@
import
unittest2, eth/[common, p2p], eth/trie/db,
../nimbus/db/db_chain, ../nimbus/p2p/chain,
../nimbus/config
../nimbus/chain_config
const
MainNetIDs = [

View File

@ -10,12 +10,12 @@
import
std/[os, json],
stew/byteutils, unittest2,
eth/[p2p, common, trie/db, rlp, trie],
eth/[p2p, common, trie/db, rlp],
graphql, ../nimbus/graphql/ethapi, graphql/test_common,
../nimbus/sync/protocol_eth65,
../nimbus/[genesis, config, chain_config, context],
../nimbus/db/[db_chain, state_db],
../nimbus/p2p/chain, ../premix/parser, ./test_helpers
../nimbus/db/[db_chain],
../nimbus/p2p/chain, ./test_helpers
type
EthBlock = object
@ -95,7 +95,7 @@ proc setupChain(): BaseChainDB =
proc graphqlMain*() =
let
conf = getConfiguration()
conf = makeConfig(@[]) # don't use makeConfig default cmdLine from inside all_tests
ethCtx = newEthContext()
ethNode = setupEthNode(conf, ethCtx, eth)
chainDB = setupChain()
@ -108,5 +108,4 @@ proc graphqlMain*() =
ctx.executeCases(caseFolder, purgeSchema = false)
when isMainModule:
processArguments()
graphqlMain()

View File

@ -272,14 +272,16 @@ proc getFixtureTransaction*(j: JsonNode, dataIndex, gasIndex, valueIndex: int):
proc hashLogEntries*(logs: seq[Log]): string =
toLowerAscii("0x" & $keccakHash(rlp.encode(logs)))
proc setupEthNode*(conf: NimbusConfiguration, ctx: EthContext, capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
let keypair = ctx.hexToKeyPair(conf.net.nodekey).tryGet()
proc setupEthNode*(conf: NimbusConf, ctx: EthContext, capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
let keypair = ctx.hexToKeyPair(conf.nodeKeyHex).tryGet()
var srvAddress: Address
srvAddress.ip = parseIpAddress("0.0.0.0")
srvAddress.tcpPort = Port(conf.net.bindPort)
srvAddress.udpPort = Port(conf.net.discPort)
srvAddress.tcpPort = conf.tcpPort
srvAddress.udpPort = conf.udpPort
result = newEthereumNode(
keypair, srvAddress, conf.net.networkId, nil, "nimbus 0.1.0",
keypair, srvAddress,
conf.networkId.get(),
nil, conf.agentString,
addAllCapabilities = false)
for capability in capabilities:
result.addCapability capability

View File

@ -1,10 +1,8 @@
import
std/[os, parseopt],
std/[os],
unittest2, stew/byteutils,
eth/common/eth_types,
eth/p2p,
../nimbus/vm_internals,
../nimbus/config,
../nimbus/utils/header
func toAddress(n: int): EthAddress =
@ -26,41 +24,6 @@ proc miscMain*() =
check toAddress(0x10, 0x0).toInt == 0x1000
check toAddress(0x10, 0x0, 0x0).toInt == 0x100000
const genesisFile = "tests" / "customgenesis" / "calaveras.json"
test "networkid cli":
var msg: string
var opt = initOptParser("--customnetwork:" & genesisFile & " --networkid:345")
let res = processArguments(msg, opt)
if res != Success:
echo msg
quit(QuitFailure)
let conf = getConfiguration()
check conf.net.networkId == 345.NetworkId
test "networkid first, customnetwork next":
var msg: string
var opt = initOptParser("--networkid:678 --customnetwork:" & genesisFile)
let res = processArguments(msg, opt)
if res != Success:
echo msg
quit(QuitFailure)
let conf = getConfiguration()
check conf.net.networkId == 678.NetworkId
test "networkid not set, copy from chainId of customnetwork":
let conf = getConfiguration()
conf.net.flags.excl NetworkIdSet
var msg: string
var opt = initOptParser("--customnetwork:" & genesisFile)
let res = processArguments(msg, opt)
if res != Success:
echo msg
quit(QuitFailure)
check conf.net.networkId == 123.NetworkId
test "calcGasLimitEIP1559":
type
GLT = object

View File

@ -12,12 +12,12 @@ import
eth/[rlp, keys, trie/db, p2p/private/p2p_types],
../nimbus/rpc/[common, p2p, hexstrings, rpc_types, rpc_utils],
../nimbus/[constants, vm_state, config, genesis, utils, transaction],
../nimbus/db/[accounts_cache, db_chain, storage_types, state_db],
../nimbus/db/[accounts_cache, db_chain, state_db],
../nimbus/p2p/[chain, executor, executor/executor_helpers],
../nimbus/sync/protocol_eth65,
../nimbus/utils/difficulty,
../nimbus/context,
./rpcclient/test_hexstrings, ./test_helpers, ./macro_assembler
../nimbus/[context, chain_config],
./test_helpers, ./macro_assembler
# Perform checks for hex string validation
#doHexStrTests()
@ -124,17 +124,22 @@ proc rpcMain*() =
suite "Remote Procedure Calls":
# TODO: Include other transports such as Http
let
conf = getConfiguration()
conf = makeConfig(@[]) # don't use makeConfig default cmdLine from inside all_tests
ctx = newEthContext()
ethNode = setupEthNode(conf, ctx, eth)
chain = newBaseChainDB(newMemoryDb())
chain = newBaseChainDB(
newMemoryDb(),
conf.pruneMode == PruneMode.Full,
conf.networkId.get(),
conf.customNetwork.get()
)
signer: EthAddress = hexToByteArray[20]("0x0e69cde81b1aa07a45c32c6cd85d67229d36bb1b")
ks2: EthAddress = hexToByteArray[20]("0xa3b2222afa5c987da6ef773fde8d01b9f23d481f")
ks3: EthAddress = hexToByteArray[20]("0x597176e9a64aad0845d83afdaf698fbeff77703b")
ethNode.chain = newChain(chain)
conf.keyStore = "tests" / "keystore"
let res = ctx.am.loadKeystores(conf.keyStore)
let keyStore = "tests" / "keystore"
let res = ctx.am.loadKeystores(keyStore)
if res.isErr:
debugEcho res.error
doAssert(res.isOk)
@ -145,8 +150,7 @@ proc rpcMain*() =
debugEcho unlock.error
doAssert(unlock.isOk)
genesisBlockForNetwork(conf.net.networkId, conf.customNetwork).commit(chain)
doAssert(canonicalHeadHashKey().toOpenArray in chain.db)
initializeEmptyDb(chain)
let env = setupEnv(chain, signer, ks2, ctx)
# Create Ethereum RPCs
@ -178,11 +182,11 @@ proc rpcMain*() =
test "net_version":
let res = await client.net_version()
check res == $conf.net.networkId
check res == $conf.networkId.get()
test "net_listening":
let res = await client.net_listening()
let listening = ethNode.peerPool.connectedNodes.len < conf.net.maxPeers
let listening = ethNode.peerPool.connectedNodes.len < conf.maxPeers
check res == listening
test "net_peerCount":

View File

@ -1,7 +1,7 @@
import
json, eth/common, stint, chronicles,
eth/trie/db, ../nimbus/db/[db_chain, capturedb, select_backend],
../nimbus/[tracer, vm_types, config]
../nimbus/[tracer, config, vm_types]
proc dumpTest(chainDB: BaseChainDB, blockNumber: int) =
let
@ -47,8 +47,8 @@ proc main() {.used.} =
# nimbus --rpcapi: eth, debug --prune: archive
var conf = getConfiguration()
let db = newChainDb(conf.dataDir)
var conf = makeConfig()
let db = newChainDb(string conf.dataDir)
let trieDB = trieDB db
let chainDB = newBaseChainDB(trieDB, false)
@ -62,17 +62,6 @@ proc main() {.used.} =
chainDB.dumpTest(49018)
when isMainModule:
var message: string
## Processing command line arguments
if processArguments(message) != Success:
echo message
quit(QuitFailure)
else:
if len(message) > 0:
echo message
quit(QuitSuccess)
try:
main()
except: