nimbus-eth1/waku/wakunode.nim

151 lines
5.7 KiB
Nim
Raw Normal View History

2019-12-12 18:40:34 +00:00
import
2020-01-20 18:01:02 +00:00
confutils, config, strutils, chronos, json_rpc/rpcserver, metrics,
2019-12-12 18:40:34 +00:00
chronicles/topics_registry, # TODO: What? Need this for setLoglevel, weird.
2020-01-20 18:01:02 +00:00
eth/[keys, p2p, async_utils], eth/common/utils, eth/net/nat,
2019-12-12 18:40:34 +00:00
eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes],
eth/p2p/rlpx_protocols/[whisper_protocol, waku_protocol, waku_bridge],
2020-01-20 16:45:49 +00:00
../nimbus/rpc/[waku, wakusim, key_storage]
2019-12-12 18:40:34 +00:00
2020-01-20 18:01:02 +00:00
const clientId = "Nimbus waku node"
let globalListeningAddr = parseIpAddress("0.0.0.0")
2019-12-12 18:40:34 +00:00
proc setBootNodes(nodes: openArray[string]): seq[ENode] =
var bootnode: ENode
result = newSeqOfCap[ENode](nodes.len)
for nodeId in nodes:
2020-01-20 18:01:02 +00:00
# TODO: something more user friendly than an assert
2019-12-12 18:40:34 +00:00
doAssert(initENode(nodeId, bootnode) == ENodeStatus.Success)
result.add(bootnode)
proc connectToNodes(node: EthereumNode, nodes: openArray[string]) =
for nodeId in nodes:
var whisperENode: ENode
2020-01-20 18:01:02 +00:00
# TODO: something more user friendly than an assert
2019-12-12 18:40:34 +00:00
doAssert(initENode(nodeId, whisperENode) == ENodeStatus.Success)
traceAsyncErrors node.peerPool.connectToNode(newNode(whisperENode))
2020-01-20 18:01:02 +00:00
proc setupNat(conf: WakuNodeConf): tuple[ip: IpAddress,
tcpPort: Port,
udpPort: Port] =
# defaults
result.ip = globalListeningAddr
result.tcpPort = Port(conf.tcpPort + conf.portsShift)
result.udpPort = Port(conf.udpPort + conf.portsShift)
var nat: NatStrategy
case conf.nat.toLowerAscii():
of "any":
nat = NatAny
of "none":
nat = NatNone
of "upnp":
nat = NatUpnp
of "pmp":
nat = NatPmp
else:
if conf.nat.startsWith("extip:") and isIpAddress(conf.nat[6..^1]):
# any required port redirection is assumed to be done by hand
result.ip = parseIpAddress(conf.nat[6..^1])
nat = NatNone
else:
error "not a valid NAT mechanism, nor a valid IP address", value = conf.nat
quit(QuitFailure)
if nat != NatNone:
let extIP = getExternalIP(nat)
if extIP.isSome:
result.ip = extIP.get()
let extPorts = redirectPorts(tcpPort = result.tcpPort,
udpPort = result.udpPort,
description = clientId)
if extPorts.isSome:
(result.tcpPort, result.udpPort) = extPorts.get()
2019-12-12 18:40:34 +00:00
proc run(config: WakuNodeConf) =
if config.logLevel != LogLevel.NONE:
setLogLevel(config.logLevel)
2020-01-20 18:01:02 +00:00
let
(ip, tcpPort, udpPort) = setupNat(config)
address = Address(ip: ip, tcpPort: tcpPort, udpPort: udpPort)
2019-12-12 18:40:34 +00:00
# Set-up node
2020-01-20 18:01:02 +00:00
var node = newEthereumNode(config.nodekey, address, 1, nil, clientId,
2019-12-12 18:40:34 +00:00
addAllCapabilities = false)
if not config.bootnodeOnly:
node.addCapability Waku # Always enable Waku protocol
2020-02-06 23:35:13 +00:00
var topicInterest: Option[seq[waku_protocol.Topic]]
if config.wakuTopicInterest:
var topics: seq[waku_protocol.Topic]
topicInterest = some(topics)
2019-12-13 16:04:08 +00:00
let wakuConfig = WakuConfig(powRequirement: config.wakuPow,
bloom: fullBloom(),
2020-01-14 22:35:47 +00:00
isLightNode: config.lightNode,
2019-12-13 16:04:08 +00:00
maxMsgSize: waku_protocol.defaultMaxMsgSize,
2020-02-06 23:35:13 +00:00
topics: topicInterest)
2019-12-13 16:04:08 +00:00
node.configureWaku(wakuConfig)
2019-12-12 18:40:34 +00:00
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 == beta: setBootNodes(StatusBootNodes)
elif config.fleet == staging: setBootNodes(StatusBootNodesStaging)
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 == beta: connectToNodes(node, WhisperNodes)
elif config.fleet == staging: connectToNodes(node, WhisperNodesStaging)
if config.rpc:
2019-12-16 15:35:03 +00:00
let ta = initTAddress(config.rpcAddress,
Port(config.rpcPort + config.portsShift))
var rpcServer = newRpcHttpServer([ta])
2020-01-20 16:45:49 +00:00
let keys = newKeyStorage()
2019-12-12 18:40:34 +00:00
setupWakuRPC(node, keys, rpcServer)
setupWakuSimRPC(node, rpcServer)
2019-12-12 18:40:34 +00:00
rpcServer.start()
2019-12-17 23:26:53 +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))
2020-01-14 22:35:47 +00:00
if config.logMetrics:
proc logMetrics(udata: pointer) {.closure, gcsafe.} =
{.gcsafe.}:
let
connectedPeers = connected_peers.value
validEnvelopes = waku_protocol.valid_envelopes.value
invalidEnvelopes = waku_protocol.dropped_expired_envelopes.value +
waku_protocol.dropped_from_future_envelopes.value +
waku_protocol.dropped_low_pow_envelopes.value +
waku_protocol.dropped_too_large_envelopes.value +
waku_protocol.dropped_bloom_filter_mismatch_envelopes.value +
waku_protocol.dropped_topic_mismatch_envelopes.value +
waku_protocol.dropped_benign_duplicate_envelopes.value +
2020-01-23 16:45:39 +00:00
waku_protocol.dropped_duplicate_envelopes.value
2020-01-14 22:35:47 +00:00
info "Node metrics", connectedPeers, validEnvelopes, invalidEnvelopes
addTimer(Moment.fromNow(2.seconds), logMetrics)
addTimer(Moment.fromNow(2.seconds), logMetrics)
2019-12-12 18:40:34 +00:00
runForever()
when isMainModule:
let conf = WakuNodeConf.load()
run(conf)