2018-04-27 08:53:53 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 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-04-21 10:15:06 +00:00
|
|
|
import
|
|
|
|
../nimbus/vm_compile_info
|
|
|
|
|
2018-07-22 21:34:43 +00:00
|
|
|
import
|
2019-04-17 01:56:28 +00:00
|
|
|
os, strutils, net, options,
|
2021-09-11 14:58:01 +00:00
|
|
|
stew/shims/net as stewNet,
|
2019-04-17 01:56:28 +00:00
|
|
|
eth/keys, db/[storage_types, db_chain, select_backend],
|
2019-02-05 19:15:50 +00:00
|
|
|
eth/common as eth_common, eth/p2p as eth_p2p,
|
2019-02-06 18:03:36 +00:00
|
|
|
chronos, json_rpc/rpcserver, chronicles,
|
2021-07-21 20:01:12 +00:00
|
|
|
eth/p2p/rlpx_protocols/les_protocol,
|
Sync: Move `blockchain_sync` code and use it with `eth/65`
Move `blockchain_sync.nim` from `nim-eth` to `nimbus-eth1`.
This lets `blockchain_sync` use the `eth/65` protocol to synchronise with more
modern peers than before.
Practically, the effect is the sync process runs more quickly and reliably than
before. It finds usable peers, and they are up to date.
Note, this is mostly old code, and it mostly performs "classic sync", the
original Ethereum method. Here's a summary of this code:
- It decides on a blockchain canonical head by sampling a few peers.
- Starting from block 0 (genesis), it downloads each block header and
block, mostly in order.
- After it downloads each block, it executes the EVM transactions in that block
and updates state trie from that, before going to the next block.
- This way the database state is updated by EVM executions in block order,
and new state is persisted to the trie database after each block.
Even though it mentions Geth "fast sync" (comments near end of file), and has
some elements, it isn't really. The most obvious missing part is this code
_doesn't download a state trie_, it calculates all state from block 0.
Geth "fast sync" has several parts:
1. Find an agreed common chain among several peers to treat as probably secure,
and a sufficiently long suffix to provide "statistical economic consensus"
when it is validated.
2. Perform a subset of PoW calculations, skipping forward over a segment to
verify some of the PoWs according to a pattern in the relevant paper.
3. Download the state trie from the block at the start of that last segment.
4. Execute only the blocks/transactions in that last segment, using the
downloaded state trie, to fill out the later states and properly validate the
blocks in the last segment.
Some other issues with `blockchain_sync` code:
- If it ever reaches the head of the chain, it doesn't follow new blocks with
increasing block numbers, at least not rapidly.
- If the chain undergoes a reorg, this code won't fetch a block number it has
already fetched, so it can't accept the reorg. It will end up conflicted
with peers. This hasn't mattered because the development focus has been on
the bulk of the catching up process, not the real-time head and reorgs.
- So it probably doesn't work correctly when it gets close to the head due to
many small reorgs, though it might for subtle reasons.
- Some of the network message handling isn't sufficiently robust, and it
discards some replies that have valid data according to specification.
- On rare occasions the initial query mapping block hash to number can
fail (because the peer's state changes).
- It makes some assumptions about the state of peers based on their responses
which may not be valid (I'm not convinced they are). The method for working
out "trusted" peers that agree a common chain prefix is clever. It compares
peers by asking each peer if it has the header matching another peer's
canonical head block by hash. But it's not clear that merely knowing about a
block constitutes agreement about the canonical chain. (If it did, query by
block number would give the same answer more authoritatively.)
Nonetheless, being able to run this sync process on `eth/65` is useful.
<# interactive rebase in progress; onto 66532e8a
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-07-22 13:36:10 +00:00
|
|
|
./p2p/blockchain_sync, eth/net/nat, eth/p2p/peer_pool,
|
2021-07-21 20:01:12 +00:00
|
|
|
./sync/protocol_eth65,
|
2021-08-11 19:37:00 +00:00
|
|
|
config, genesis, rpc/[common, p2p, debug], p2p/chain,
|
2021-04-09 07:26:06 +00:00
|
|
|
eth/trie/db, metrics, metrics/[chronos_httpserver, chronicles_support],
|
2021-09-07 13:45:01 +00:00
|
|
|
graphql/ethapi, context,
|
2021-09-08 13:28:17 +00:00
|
|
|
"."/[conf_utils, sealer, constants]
|
2018-07-26 20:08:43 +00:00
|
|
|
|
2018-06-20 17:27:32 +00:00
|
|
|
## TODO:
|
|
|
|
## * No IPv6 support
|
|
|
|
## * No multiple bind addresses support
|
|
|
|
## * No database support
|
|
|
|
|
|
|
|
type
|
|
|
|
NimbusState = enum
|
2020-05-21 01:33:11 +00:00
|
|
|
Starting, Running, Stopping
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2020-03-25 18:00:04 +00:00
|
|
|
NimbusNode = ref object
|
2021-09-07 13:45:01 +00:00
|
|
|
rpcServer: RpcHttpServer
|
|
|
|
ethNode: EthereumNode
|
|
|
|
state: NimbusState
|
|
|
|
graphqlServer: GraphqlHttpServerRef
|
|
|
|
wsRpcServer: RpcWebSocketServer
|
|
|
|
sealingEngine: SealingEngineRef
|
|
|
|
ctx: EthContext
|
2021-09-16 15:59:46 +00:00
|
|
|
chainRef: Chain
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
proc importBlocks(conf: NimbusConf, chainDB: BaseChainDB) =
|
|
|
|
if string(conf.blocksFile).len > 0:
|
2021-05-17 11:43:44 +00:00
|
|
|
# success or not, we quit after importing blocks
|
2021-09-16 15:59:46 +00:00
|
|
|
if not importRlpBlock(string conf.blocksFile, chainDB):
|
2021-05-17 11:43:44 +00:00
|
|
|
quit(QuitFailure)
|
|
|
|
else:
|
|
|
|
quit(QuitSuccess)
|
2021-03-18 15:05:15 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
proc manageAccounts(nimbus: NimbusNode, conf: NimbusConf) =
|
2021-09-11 14:58:01 +00:00
|
|
|
if string(conf.keyStore).len > 0:
|
|
|
|
let res = nimbus.ctx.am.loadKeystores(string conf.keyStore)
|
2021-09-07 13:45:01 +00:00
|
|
|
if res.isErr:
|
|
|
|
echo res.error()
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
if string(conf.importKey).len > 0:
|
|
|
|
let res = nimbus.ctx.am.importPrivateKey(string conf.importKey)
|
2021-09-07 13:45:01 +00:00
|
|
|
if res.isErr:
|
|
|
|
echo res.error()
|
|
|
|
quit(QuitFailure)
|
2021-03-18 15:05:15 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
proc setupP2P(nimbus: NimbusNode, conf: NimbusConf,
|
|
|
|
chainDB: BaseChainDB, protocols: set[ProtocolFlag]) =
|
2018-06-20 17:27:32 +00:00
|
|
|
## Creating P2P Server
|
2021-09-11 14:58:01 +00:00
|
|
|
let kpres = nimbus.ctx.hexToKeyPair(conf.nodeKeyHex)
|
2021-09-07 13:45:01 +00:00
|
|
|
if kpres.isErr:
|
|
|
|
echo kpres.error()
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
let keypair = kpres.get()
|
2021-09-11 14:58:01 +00:00
|
|
|
var address = Address(
|
|
|
|
ip: conf.listenAddress,
|
|
|
|
tcpPort: conf.tcpPort,
|
|
|
|
udpPort: conf.udpPort
|
|
|
|
)
|
|
|
|
|
|
|
|
if conf.nat.hasExtIp:
|
|
|
|
# any required port redirection is assumed to be done by hand
|
|
|
|
address.ip = conf.nat.extIp
|
2019-04-17 01:56:28 +00:00
|
|
|
else:
|
|
|
|
# automated NAT traversal
|
2021-09-11 14:58:01 +00:00
|
|
|
let extIP = getExternalIP(conf.nat.nat)
|
2019-04-17 23:17:06 +00:00
|
|
|
# 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.
|
2019-04-17 01:56:28 +00:00
|
|
|
if extIP.isSome:
|
|
|
|
address.ip = extIP.get()
|
|
|
|
let extPorts = redirectPorts(tcpPort = address.tcpPort,
|
2021-09-11 14:58:01 +00:00
|
|
|
udpPort = address.udpPort,
|
|
|
|
description = NIMBUS_NAME & " " & NIMBUS_VERSION)
|
2019-04-17 01:56:28 +00:00
|
|
|
if extPorts.isSome:
|
|
|
|
(address.tcpPort, address.udpPort) = extPorts.get()
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.ethNode = newEthereumNode(keypair, address, conf.networkId,
|
2021-09-11 14:58:01 +00:00
|
|
|
nil, conf.agentString,
|
2019-06-05 13:18:26 +00:00
|
|
|
addAllCapabilities = false,
|
2021-09-11 14:58:01 +00:00
|
|
|
minPeers = conf.maxPeers)
|
2019-04-23 13:49:49 +00:00
|
|
|
# Add protocol capabilities based on protocol flags
|
2021-09-16 15:59:46 +00:00
|
|
|
if ProtocolFlag.Eth in protocols:
|
2019-04-23 13:49:49 +00:00
|
|
|
nimbus.ethNode.addCapability eth
|
2021-09-16 15:59:46 +00:00
|
|
|
if ProtocolFlag.Les in protocols:
|
2019-04-23 13:49:49 +00:00
|
|
|
nimbus.ethNode.addCapability les
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
# chainRef: some name to avoid module-name/filed/function misunderstandings
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.chainRef = newChain(chainDB)
|
|
|
|
nimbus.ethNode.chain = nimbus.chainRef
|
2021-09-11 14:58:01 +00:00
|
|
|
if conf.verifyFrom.isSome:
|
|
|
|
let verifyFrom = conf.verifyFrom.get()
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.chainRef.extraValidation = 0 < verifyFrom
|
|
|
|
nimbus.chainRef.verifyFrom = verifyFrom
|
|
|
|
|
|
|
|
# Connect directly to the static nodes
|
|
|
|
let staticPeers = conf.getStaticPeers()
|
|
|
|
for enode in staticPeers:
|
|
|
|
asyncCheck nimbus.ethNode.peerPool.connectToNode(newNode(enode))
|
|
|
|
|
|
|
|
# Connect via discovery
|
|
|
|
let bootNodes = conf.getBootNodes()
|
|
|
|
if bootNodes.len > 0:
|
|
|
|
waitFor nimbus.ethNode.connectToNetwork(bootNodes,
|
|
|
|
enableDiscovery = conf.discovery != DiscoveryType.None)
|
|
|
|
|
|
|
|
proc localServices(nimbus: NimbusNode, conf: NimbusConf,
|
|
|
|
chainDB: BaseChainDB, protocols: set[ProtocolFlag]) =
|
|
|
|
# metrics logging
|
|
|
|
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.logMetricsInterval.seconds), logMetrics)
|
|
|
|
discard setTimer(Moment.fromNow(conf.logMetricsInterval.seconds), logMetrics)
|
2021-09-11 14:58:01 +00:00
|
|
|
|
|
|
|
# Creating RPC Server
|
|
|
|
if conf.rpcEnabled:
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.rpcServer = newRpcHttpServer([initTAddress(conf.rpcAddress, conf.rpcPort)])
|
2021-09-07 15:00:21 +00:00
|
|
|
setupCommonRpc(nimbus.ethNode, conf, nimbus.rpcServer)
|
2020-07-22 16:51:26 +00:00
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
# Enable RPC APIs based on RPC flags and protocol flags
|
2021-09-16 15:59:46 +00:00
|
|
|
let rpcFlags = conf.getRpcFlags()
|
|
|
|
if RpcFlag.Eth in rpcFlags and ProtocolFlag.Eth in protocols:
|
2021-09-11 14:58:01 +00:00
|
|
|
setupEthRpc(nimbus.ethNode, nimbus.ctx, chainDB, nimbus.rpcServer)
|
2021-09-16 15:59:46 +00:00
|
|
|
if RpcFlag.Debug in rpcFlags:
|
2021-09-11 14:58:01 +00:00
|
|
|
setupDebugRpc(chainDB, nimbus.rpcServer)
|
2021-08-05 07:51:28 +00:00
|
|
|
|
2018-06-20 17:27:32 +00:00
|
|
|
nimbus.rpcServer.rpc("admin_quit") do() -> string:
|
2019-07-10 21:23:11 +00:00
|
|
|
{.gcsafe.}:
|
|
|
|
nimbus.state = Stopping
|
2018-06-20 17:27:32 +00:00
|
|
|
result = "EXITING"
|
2021-09-11 14:58:01 +00:00
|
|
|
|
2018-06-20 17:27:32 +00:00
|
|
|
nimbus.rpcServer.start()
|
2020-03-25 18:00:04 +00:00
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
# Creating Websocket RPC Server
|
|
|
|
if conf.wsEnabled:
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.wsRpcServer = newRpcWebSocketServer(initTAddress(conf.wsAddress, conf.wsPort))
|
2021-09-11 14:58:01 +00:00
|
|
|
setupCommonRpc(nimbus.ethNode, conf, nimbus.wsRpcServer)
|
|
|
|
|
|
|
|
# Enable Websocket RPC APIs based on RPC flags and protocol flags
|
2021-09-16 15:59:46 +00:00
|
|
|
let wsFlags = conf.getWsFlags()
|
|
|
|
if RpcFlag.Eth in wsFlags and ProtocolFlag.Eth in protocols:
|
2021-09-11 14:58:01 +00:00
|
|
|
setupEthRpc(nimbus.ethNode, nimbus.ctx, chainDB, nimbus.wsRpcServer)
|
2021-09-16 15:59:46 +00:00
|
|
|
if RpcFlag.Debug in wsFlags:
|
2021-09-11 14:58:01 +00:00
|
|
|
setupDebugRpc(chainDB, nimbus.wsRpcServer)
|
|
|
|
|
|
|
|
nimbus.wsRpcServer.start()
|
|
|
|
|
|
|
|
if conf.graphqlEnabled:
|
2021-04-24 04:01:09 +00:00
|
|
|
nimbus.graphqlServer = setupGraphqlHttpServer(conf, chainDB, nimbus.ethNode)
|
|
|
|
nimbus.graphqlServer.start()
|
|
|
|
|
2021-08-24 07:34:58 +00:00
|
|
|
if conf.engineSigner != ZERO_ADDRESS:
|
2021-09-16 15:59:46 +00:00
|
|
|
let rs = validateSealer(conf, nimbus.ctx, nimbus.chainRef)
|
2021-08-24 07:34:58 +00:00
|
|
|
if rs.isErr:
|
|
|
|
echo rs.error
|
|
|
|
quit(QuitFailure)
|
2021-09-07 13:45:01 +00:00
|
|
|
nimbus.sealingEngine = SealingEngineRef.new(
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.chainRef, nimbus.ctx, conf.engineSigner
|
2021-09-07 13:45:01 +00:00
|
|
|
)
|
2021-08-24 07:34:58 +00:00
|
|
|
nimbus.sealingEngine.start()
|
|
|
|
|
2019-07-16 10:43:05 +00:00
|
|
|
# metrics server
|
2021-09-11 14:58:01 +00:00
|
|
|
if conf.metricsEnabled:
|
|
|
|
info "Starting metrics HTTP server", address = conf.metricsAddress, port = conf.metricsPort
|
|
|
|
startMetricsHttpServer($conf.metricsAddress, conf.metricsPort)
|
2019-06-26 12:51:59 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
proc start(nimbus: NimbusNode, conf: NimbusConf) =
|
|
|
|
## logging
|
|
|
|
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(logFile, fmAppend)
|
2019-04-23 13:49:49 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
createDir(string conf.dataDir)
|
|
|
|
let trieDB = trieDB newChainDb(string conf.dataDir)
|
|
|
|
var chainDB = newBaseChainDB(trieDB,
|
|
|
|
conf.pruneMode == PruneMode.Full,
|
|
|
|
conf.networkId,
|
|
|
|
conf.networkParams
|
|
|
|
)
|
|
|
|
chainDB.populateProgress()
|
2018-07-22 21:34:43 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
if canonicalHeadHashKey().toOpenArray notin trieDB:
|
|
|
|
initializeEmptyDb(chainDb)
|
|
|
|
doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
|
2018-07-12 11:14:04 +00:00
|
|
|
|
2021-09-16 15:59:46 +00:00
|
|
|
let protocols = conf.getProtocolFlags()
|
|
|
|
|
|
|
|
case conf.cmd
|
|
|
|
of NimbusCmd.`import`:
|
|
|
|
importBlocks(conf, chainDB)
|
|
|
|
else:
|
|
|
|
manageAccounts(nimbus, conf)
|
|
|
|
setupP2P(nimbus, conf, chainDB, protocols)
|
|
|
|
localServices(nimbus, conf, chainDB, protocols)
|
|
|
|
|
|
|
|
if ProtocolFlag.Eth in protocols:
|
|
|
|
# TODO: temp code until the CLI/RPC interface is fleshed out
|
|
|
|
let status = waitFor nimbus.ethNode.fastBlockchainSync()
|
|
|
|
if status != syncSuccess:
|
|
|
|
debug "Block sync failed: ", status
|
|
|
|
|
|
|
|
if nimbus.state == Starting:
|
|
|
|
# it might have been set to "Stopping" with Ctrl+C
|
|
|
|
nimbus.state = Running
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
proc stop*(nimbus: NimbusNode, conf: NimbusConf) {.async, gcsafe.} =
|
2018-12-06 23:16:34 +00:00
|
|
|
trace "Graceful shutdown"
|
2021-09-11 14:58:01 +00:00
|
|
|
if conf.rpcEnabled:
|
2019-07-10 21:23:11 +00:00
|
|
|
nimbus.rpcServer.stop()
|
2021-09-11 14:58:01 +00:00
|
|
|
if conf.wsEnabled:
|
2021-09-16 15:59:46 +00:00
|
|
|
nimbus.wsRpcServer.stop()
|
2021-09-11 14:58:01 +00:00
|
|
|
if conf.graphqlEnabled:
|
2021-04-24 04:01:09 +00:00
|
|
|
await nimbus.graphqlServer.stop()
|
2021-08-24 07:34:58 +00:00
|
|
|
if conf.engineSigner != ZERO_ADDRESS:
|
|
|
|
await nimbus.sealingEngine.stop()
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
proc process*(nimbus: NimbusNode, conf: NimbusConf) =
|
2020-05-21 01:33:11 +00:00
|
|
|
# Main event loop
|
|
|
|
while nimbus.state == Running:
|
|
|
|
try:
|
|
|
|
poll()
|
|
|
|
except CatchableError as e:
|
|
|
|
debug "Exception in poll()", exc = e.name, err = e.msg
|
2020-07-21 08:12:59 +00:00
|
|
|
discard e # silence warning when chronicles not activated
|
2018-06-20 17:27:32 +00:00
|
|
|
|
2019-07-10 21:23:11 +00:00
|
|
|
# Stop loop
|
2021-09-11 14:58:01 +00:00
|
|
|
waitFor nimbus.stop(conf)
|
2018-04-27 08:53:53 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2021-09-07 13:45:01 +00:00
|
|
|
var nimbus = NimbusNode(state: Starting, ctx: newEthContext())
|
2020-03-25 18:00:04 +00:00
|
|
|
|
|
|
|
## Ctrl+C handling
|
|
|
|
proc controlCHandler() {.noconv.} =
|
|
|
|
when defined(windows):
|
|
|
|
# workaround for https://github.com/nim-lang/Nim/issues/4057
|
|
|
|
setupForeignThreadGc()
|
|
|
|
nimbus.state = Stopping
|
|
|
|
echo "\nCtrl+C pressed. Waiting for a graceful shutdown."
|
|
|
|
setControlCHook(controlCHandler)
|
|
|
|
|
2019-07-10 21:23:11 +00:00
|
|
|
## Show logs on stdout until we get the user's logging choice
|
2019-04-17 01:56:28 +00:00
|
|
|
discard defaultChroniclesStream.output.open(stdout)
|
|
|
|
|
2018-06-20 17:27:32 +00:00
|
|
|
## Processing command line arguments
|
2021-09-11 14:58:01 +00:00
|
|
|
let conf = makeConfig()
|
2019-07-10 21:23:11 +00:00
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
nimbus.start(conf)
|
|
|
|
nimbus.process(conf)
|