2020-04-28 06:04:27 +00:00
|
|
|
import
|
2020-08-26 12:20:04 +00:00
|
|
|
std/strutils,
|
|
|
|
confutils, chronos, json_rpc/rpcserver, metrics, metrics/chronicles_support,
|
|
|
|
eth/[keys, p2p, async_utils], eth/common/utils,
|
2020-04-28 06:04:27 +00:00
|
|
|
eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes],
|
2020-05-01 12:43:25 +00:00
|
|
|
eth/p2p/rlpx_protocols/whisper_protocol,
|
|
|
|
../../protocol/v1/[waku_protocol, waku_bridge],
|
2020-08-26 12:20:04 +00:00
|
|
|
./rpc/[waku, wakusim, key_storage], ./waku_helpers, ./config
|
2020-04-28 06:04:27 +00:00
|
|
|
|
|
|
|
const clientId = "Nimbus waku node"
|
|
|
|
|
2020-07-13 10:08:03 +00:00
|
|
|
proc run(config: WakuNodeConf, rng: ref BrHmacDrbgContext) =
|
2020-04-28 06:04:27 +00:00
|
|
|
let
|
2020-08-26 12:20:04 +00:00
|
|
|
(ip, tcpPort, udpPort) = setupNat(config.nat, clientId, config.tcpPort,
|
|
|
|
config.udpPort, config.portsShift)
|
2020-04-28 06:04:27 +00:00
|
|
|
address = Address(ip: ip, tcpPort: tcpPort, udpPort: udpPort)
|
|
|
|
|
|
|
|
# Set-up node
|
|
|
|
var node = newEthereumNode(config.nodekey, address, 1, nil, clientId,
|
2020-08-26 12:20:04 +00:00
|
|
|
addAllCapabilities = false, rng = rng)
|
2020-04-28 06:04:27 +00:00
|
|
|
if not config.bootnodeOnly:
|
|
|
|
node.addCapability Waku # Always enable Waku protocol
|
|
|
|
var topicInterest: Option[seq[waku_protocol.Topic]]
|
|
|
|
var bloom: Option[Bloom]
|
|
|
|
if config.wakuTopicInterest:
|
|
|
|
var topics: seq[waku_protocol.Topic]
|
|
|
|
topicInterest = some(topics)
|
|
|
|
else:
|
|
|
|
bloom = some(fullBloom())
|
|
|
|
let wakuConfig = WakuConfig(powRequirement: config.wakuPow,
|
|
|
|
bloom: bloom,
|
|
|
|
isLightNode: config.lightNode,
|
|
|
|
maxMsgSize: waku_protocol.defaultMaxMsgSize,
|
|
|
|
topics: topicInterest)
|
|
|
|
node.configureWaku(wakuConfig)
|
|
|
|
if config.whisper or config.whisperBridge:
|
|
|
|
node.addCapability Whisper
|
|
|
|
node.protocolState(Whisper).config.powRequirement = 0.002
|
|
|
|
if config.whisperBridge:
|
|
|
|
node.shareMessageQueue()
|
|
|
|
|
|
|
|
# TODO: Status fleet bootnodes are discv5? That will not work.
|
|
|
|
let bootnodes = if config.bootnodes.len > 0: setBootNodes(config.bootnodes)
|
|
|
|
elif config.fleet == prod: setBootNodes(StatusBootNodes)
|
|
|
|
elif config.fleet == staging: setBootNodes(StatusBootNodesStaging)
|
|
|
|
elif config.fleet == test : setBootNodes(StatusBootNodesTest)
|
|
|
|
else: @[]
|
|
|
|
|
|
|
|
traceAsyncErrors node.connectToNetwork(bootnodes, not config.noListen,
|
|
|
|
config.discovery)
|
|
|
|
|
|
|
|
if not config.bootnodeOnly:
|
|
|
|
# Optionally direct connect with a set of nodes
|
|
|
|
if config.staticnodes.len > 0: connectToNodes(node, config.staticnodes)
|
|
|
|
elif config.fleet == prod: connectToNodes(node, WhisperNodes)
|
|
|
|
elif config.fleet == staging: connectToNodes(node, WhisperNodesStaging)
|
|
|
|
elif config.fleet == test: connectToNodes(node, WhisperNodesTest)
|
|
|
|
|
|
|
|
if config.rpc:
|
|
|
|
let ta = initTAddress(config.rpcAddress,
|
|
|
|
Port(config.rpcPort + config.portsShift))
|
|
|
|
var rpcServer = newRpcHttpServer([ta])
|
|
|
|
let keys = newKeyStorage()
|
2020-07-13 10:08:03 +00:00
|
|
|
setupWakuRPC(node, keys, rpcServer, rng)
|
2020-04-28 06:04:27 +00:00
|
|
|
setupWakuSimRPC(node, rpcServer)
|
|
|
|
rpcServer.start()
|
|
|
|
|
2020-06-18 03:16:23 +00:00
|
|
|
|
2020-07-02 21:52:54 +00:00
|
|
|
if config.logAccounting:
|
|
|
|
proc logPeerAccounting(udata: pointer) {.closure, gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
for peer in node.peerPool.peers:
|
|
|
|
let
|
|
|
|
sent = peer.state(Waku).accounting.sent
|
|
|
|
received = peer.state(Waku).accounting.received
|
|
|
|
id = peer.network.toEnode
|
|
|
|
info "Peer accounting", id, sent, received
|
|
|
|
peer.state(Waku).accounting = Accounting(sent: 0, received: 0)
|
|
|
|
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logPeerAccounting)
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logPeerAccounting)
|
2020-06-18 03:16:23 +00:00
|
|
|
|
2020-04-28 06:04:27 +00:00
|
|
|
when defined(insecure):
|
|
|
|
if config.metricsServer:
|
|
|
|
let
|
|
|
|
address = config.metricsServerAddress
|
|
|
|
port = config.metricsServerPort + config.portsShift
|
|
|
|
info "Starting metrics HTTP server", address, port
|
|
|
|
metrics.startHttpServer($address, Port(port))
|
|
|
|
|
|
|
|
if config.logMetrics:
|
|
|
|
proc logMetrics(udata: pointer) {.closure, gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
let
|
2020-06-09 11:44:39 +00:00
|
|
|
connectedPeers = connected_peers
|
|
|
|
validEnvelopes = waku_protocol.envelopes_valid
|
|
|
|
droppedEnvelopes = waku_protocol.envelopes_dropped
|
|
|
|
|
|
|
|
info "Node metrics", connectedPeers, validEnvelopes, droppedEnvelopes
|
2020-07-02 21:52:54 +00:00
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
|
2020-04-28 06:04:27 +00:00
|
|
|
|
|
|
|
runForever()
|
|
|
|
|
|
|
|
when isMainModule:
|
2020-07-13 10:08:03 +00:00
|
|
|
let
|
|
|
|
rng = keys.newRng()
|
|
|
|
conf = WakuNodeConf.load()
|
2020-06-03 18:31:18 +00:00
|
|
|
|
|
|
|
if conf.logLevel != LogLevel.NONE:
|
|
|
|
setLogLevel(conf.logLevel)
|
|
|
|
|
|
|
|
case conf.cmd
|
|
|
|
of genNodekey:
|
2020-07-13 10:08:03 +00:00
|
|
|
echo PrivateKey.random(rng[])
|
2020-06-03 18:31:18 +00:00
|
|
|
of noCommand:
|
2020-07-13 10:08:03 +00:00
|
|
|
run(conf, rng)
|