2020-04-29 04:49:27 +00:00
|
|
|
import
|
2020-11-10 07:13:16 +00:00
|
|
|
std/[options, tables, strutils, sequtils],
|
2020-09-11 04:16:45 +00:00
|
|
|
chronos, chronicles, stew/shims/net as stewNet,
|
2020-08-31 03:32:41 +00:00
|
|
|
# TODO: Why do we need eth keys?
|
2020-09-01 02:09:54 +00:00
|
|
|
eth/keys,
|
2020-05-15 04:11:14 +00:00
|
|
|
libp2p/multiaddress,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/protocols/protocol,
|
2020-07-28 08:17:50 +00:00
|
|
|
# NOTE For TopicHandler, solve with exports?
|
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2020-05-15 04:11:14 +00:00
|
|
|
libp2p/peerinfo,
|
2020-09-16 04:23:10 +00:00
|
|
|
libp2p/standard_setup,
|
2020-09-17 02:17:52 +00:00
|
|
|
../../protocol/v2/[waku_relay, waku_store, waku_filter, message_notifier],
|
2020-11-16 08:38:52 +00:00
|
|
|
./waku_types, ./message_store
|
2020-09-16 04:23:10 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
export waku_types
|
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
logScope:
|
|
|
|
topics = "wakunode"
|
2020-09-11 04:16:45 +00:00
|
|
|
|
|
|
|
# Default clientId
|
|
|
|
const clientId* = "Nimbus Waku v2 node"
|
2020-04-29 04:49:27 +00:00
|
|
|
|
|
|
|
# key and crypto modules different
|
|
|
|
type
|
|
|
|
KeyPair* = crypto.KeyPair
|
|
|
|
PublicKey* = crypto.PublicKey
|
|
|
|
PrivateKey* = crypto.PrivateKey
|
|
|
|
|
2020-09-01 15:20:38 +00:00
|
|
|
# TODO Get rid of this and use waku_types one
|
2020-08-28 09:08:28 +00:00
|
|
|
Topic* = waku_types.Topic
|
2020-07-29 13:24:01 +00:00
|
|
|
Message* = seq[byte]
|
|
|
|
|
2020-07-24 01:39:58 +00:00
|
|
|
# NOTE Any difference here in Waku vs Eth2?
|
|
|
|
# E.g. Devp2p/Libp2p support, etc.
|
|
|
|
#func asLibp2pKey*(key: keys.PublicKey): PublicKey =
|
|
|
|
# PublicKey(scheme: Secp256k1, skkey: secp.SkPublicKey(key))
|
|
|
|
|
|
|
|
func asEthKey*(key: PrivateKey): keys.PrivateKey =
|
|
|
|
keys.PrivateKey(key.skkey)
|
|
|
|
|
2020-05-01 04:24:34 +00:00
|
|
|
proc initAddress(T: type MultiAddress, str: string): T =
|
2020-06-01 03:15:37 +00:00
|
|
|
let address = MultiAddress.init(str).tryGet()
|
2020-05-01 04:24:34 +00:00
|
|
|
if IPFS.match(address) and matchPartial(multiaddress.TCP, address):
|
|
|
|
result = address
|
|
|
|
else:
|
2020-06-01 03:15:37 +00:00
|
|
|
raise newException(ValueError,
|
2020-05-01 04:24:34 +00:00
|
|
|
"Invalid bootstrap node multi-address")
|
|
|
|
|
2020-11-10 07:13:16 +00:00
|
|
|
proc removeContentFilters(filters: var Filters, contentFilters: seq[ContentFilter]) {.gcsafe.} =
|
|
|
|
# Flatten all unsubscribe topics into single seq
|
|
|
|
var unsubscribeTopics: seq[ContentTopic]
|
|
|
|
for cf in contentFilters:
|
|
|
|
unsubscribeTopics = unsubscribeTopics.concat(cf.topics)
|
|
|
|
|
|
|
|
debug "unsubscribing", unsubscribeTopics=unsubscribeTopics
|
|
|
|
|
|
|
|
var rIdToRemove: seq[string] = @[]
|
|
|
|
for rId, f in filters.mpairs:
|
|
|
|
# Iterate filter entries to remove matching content topics
|
|
|
|
for cf in f.contentFilters.mitems:
|
|
|
|
# Iterate content filters in filter entry
|
|
|
|
cf.topics.keepIf(proc (t: auto): bool = t notin unsubscribeTopics)
|
|
|
|
# make sure we delete the content filter
|
|
|
|
# if no more topics are left
|
|
|
|
f.contentFilters.keepIf(proc (cf: auto): bool = cf.topics.len > 0)
|
|
|
|
|
|
|
|
if f.contentFilters.len == 0:
|
|
|
|
rIdToRemove.add(rId)
|
|
|
|
|
|
|
|
# make sure we delete the filter entry
|
|
|
|
# if no more content filters left
|
|
|
|
for rId in rIdToRemove:
|
|
|
|
filters.del(rId)
|
|
|
|
|
|
|
|
debug "filters modified", filters=filters
|
|
|
|
|
2020-07-24 01:39:58 +00:00
|
|
|
template tcpEndPoint(address, port): auto =
|
|
|
|
MultiAddress.init(address, tcpProtocol, port)
|
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
## Public API
|
|
|
|
##
|
|
|
|
|
|
|
|
proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey,
|
|
|
|
bindIp: ValidIpAddress, bindPort: Port,
|
2020-10-20 02:36:27 +00:00
|
|
|
extIp = none[ValidIpAddress](), extPort = none[Port]()): T =
|
2020-10-06 03:33:28 +00:00
|
|
|
## Creates a Waku Node.
|
|
|
|
##
|
|
|
|
## Status: Implemented.
|
|
|
|
##
|
2020-04-29 04:49:27 +00:00
|
|
|
let
|
2020-10-14 03:32:37 +00:00
|
|
|
rng = crypto.newRng()
|
2020-09-01 02:09:54 +00:00
|
|
|
hostAddress = tcpEndPoint(bindIp, bindPort)
|
|
|
|
announcedAddresses = if extIp.isNone() or extPort.isNone(): @[]
|
|
|
|
else: @[tcpEndPoint(extIp.get(), extPort.get())]
|
2020-05-21 04:16:58 +00:00
|
|
|
peerInfo = PeerInfo.init(nodekey)
|
2020-09-01 02:09:54 +00:00
|
|
|
info "Initializing networking", hostAddress,
|
|
|
|
announcedAddresses
|
2020-07-24 01:39:58 +00:00
|
|
|
# XXX: Add this when we create node or start it?
|
|
|
|
peerInfo.addrs.add(hostAddress)
|
2020-05-22 06:18:14 +00:00
|
|
|
|
2020-10-14 03:32:37 +00:00
|
|
|
var switch = newStandardSwitch(some(nodekey), hostAddress,
|
|
|
|
transportFlags = {ServerFlags.ReuseAddr}, rng = rng)
|
2020-09-16 04:23:10 +00:00
|
|
|
# TODO Untested - verify behavior after switch interface change
|
|
|
|
# More like this:
|
|
|
|
# let pubsub = GossipSub.init(
|
|
|
|
# switch = switch,
|
|
|
|
# msgIdProvider = msgIdProvider,
|
|
|
|
# triggerSelf = true, sign = false,
|
|
|
|
# verifySignature = false).PubSub
|
2020-09-17 20:10:41 +00:00
|
|
|
result = WakuNode(
|
2020-09-28 21:44:14 +00:00
|
|
|
switch: switch,
|
2020-10-14 03:32:37 +00:00
|
|
|
rng: rng,
|
2020-09-17 20:10:41 +00:00
|
|
|
peerInfo: peerInfo,
|
2020-10-02 12:48:56 +00:00
|
|
|
subscriptions: newTable[string, MessageNotificationSubscription](),
|
|
|
|
filters: initTable[string, Filter]()
|
2020-09-17 20:10:41 +00:00
|
|
|
)
|
2020-09-11 11:28:27 +00:00
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
proc start*(node: WakuNode) {.async.} =
|
2020-10-06 03:33:28 +00:00
|
|
|
## Starts a created Waku Node.
|
|
|
|
##
|
|
|
|
## Status: Implemented.
|
|
|
|
##
|
2020-07-24 01:39:58 +00:00
|
|
|
node.libp2pTransportLoops = await node.switch.start()
|
2020-10-22 11:12:00 +00:00
|
|
|
|
2020-07-24 01:39:58 +00:00
|
|
|
# TODO Get this from WakuNode obj
|
|
|
|
let peerInfo = node.peerInfo
|
2020-09-16 04:23:10 +00:00
|
|
|
info "PeerInfo", peerId = peerInfo.peerId, addrs = peerInfo.addrs
|
|
|
|
let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId
|
2020-07-24 01:39:58 +00:00
|
|
|
## XXX: this should be /ip4..., / stripped?
|
2020-04-29 04:49:27 +00:00
|
|
|
info "Listening on", full = listenStr
|
|
|
|
|
2020-09-02 03:15:25 +00:00
|
|
|
proc stop*(node: WakuNode) {.async.} =
|
2020-10-20 02:36:27 +00:00
|
|
|
if not node.wakuRelay.isNil:
|
|
|
|
await node.wakuRelay.stop()
|
2020-07-28 08:17:50 +00:00
|
|
|
|
2020-09-02 03:15:25 +00:00
|
|
|
await node.switch.stop()
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
proc subscribe*(node: WakuNode, topic: Topic, handler: TopicHandler) {.async.} =
|
2020-07-27 09:01:06 +00:00
|
|
|
## Subscribes to a PubSub topic. Triggers handler when receiving messages on
|
2020-08-27 10:15:46 +00:00
|
|
|
## this topic. TopicHandler is a method that takes a topic and some data.
|
2020-07-27 09:01:06 +00:00
|
|
|
##
|
2020-08-27 10:15:46 +00:00
|
|
|
## NOTE The data field SHOULD be decoded as a WakuMessage.
|
|
|
|
## Status: Implemented.
|
2020-09-16 04:23:10 +00:00
|
|
|
info "subscribe", topic=topic
|
2020-07-28 08:17:50 +00:00
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
let wakuRelay = node.wakuRelay
|
|
|
|
await wakuRelay.subscribe(topic, handler)
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-10-02 12:48:56 +00:00
|
|
|
proc subscribe*(node: WakuNode, request: FilterRequest, handler: ContentFilterHandler) {.async, gcsafe.} =
|
|
|
|
## Registers for messages that match a specific filter. Triggers the handler whenever a message is received.
|
|
|
|
## FilterHandler is a method that takes a MessagePush.
|
|
|
|
##
|
|
|
|
## Status: Implemented.
|
2020-11-10 07:13:16 +00:00
|
|
|
|
|
|
|
# Sanity check for well-formed subscribe FilterRequest
|
|
|
|
doAssert(request.subscribe, "invalid subscribe request")
|
|
|
|
|
2020-10-02 12:48:56 +00:00
|
|
|
info "subscribe content", filter=request
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-10-20 02:36:27 +00:00
|
|
|
var id = generateRequestId(node.rng)
|
|
|
|
if node.wakuFilter.isNil == false:
|
|
|
|
# @TODO: ERROR HANDLING
|
|
|
|
id = await node.wakuFilter.subscribe(request)
|
2020-10-02 12:48:56 +00:00
|
|
|
node.filters[id] = Filter(contentFilters: request.contentFilters, handler: handler)
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-10-27 01:13:56 +00:00
|
|
|
proc unsubscribe*(node: WakuNode, topic: Topic, handler: TopicHandler) {.async.} =
|
|
|
|
## Unsubscribes a handler from a PubSub topic.
|
2020-07-27 09:01:06 +00:00
|
|
|
##
|
2020-10-27 01:13:56 +00:00
|
|
|
## Status: Implemented.
|
|
|
|
info "unsubscribe", topic=topic
|
|
|
|
|
|
|
|
let wakuRelay = node.wakuRelay
|
|
|
|
await wakuRelay.unsubscribe(@[(topic, handler)])
|
|
|
|
|
|
|
|
proc unsubscribeAll*(node: WakuNode, topic: Topic) {.async.} =
|
|
|
|
## Unsubscribes all handlers registered on a specific PubSub topic.
|
|
|
|
##
|
|
|
|
## Status: Implemented.
|
|
|
|
info "unsubscribeAll", topic=topic
|
|
|
|
|
|
|
|
let wakuRelay = node.wakuRelay
|
|
|
|
await wakuRelay.unsubscribeAll(topic)
|
|
|
|
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-11-10 07:13:16 +00:00
|
|
|
proc unsubscribe*(node: WakuNode, request: FilterRequest) {.async, gcsafe.} =
|
2020-07-27 09:01:06 +00:00
|
|
|
## Unsubscribe from a content filter.
|
|
|
|
##
|
2020-11-10 07:13:16 +00:00
|
|
|
## Status: Implemented.
|
|
|
|
|
|
|
|
# Sanity check for well-formed unsubscribe FilterRequest
|
|
|
|
doAssert(request.subscribe == false, "invalid unsubscribe request")
|
|
|
|
|
|
|
|
info "unsubscribe content", filter=request
|
|
|
|
|
|
|
|
await node.wakuFilter.unsubscribe(request)
|
|
|
|
node.filters.removeContentFilters(request.contentFilters)
|
2020-09-01 15:20:38 +00:00
|
|
|
|
|
|
|
proc publish*(node: WakuNode, topic: Topic, message: WakuMessage) =
|
|
|
|
## Publish a `WakuMessage` to a PubSub topic. `WakuMessage` should contain a
|
|
|
|
## `contentTopic` field for light node functionality. This field may be also
|
|
|
|
## be omitted.
|
2020-07-27 09:01:06 +00:00
|
|
|
##
|
2020-09-01 15:20:38 +00:00
|
|
|
## Status: Implemented.
|
2020-08-27 10:15:46 +00:00
|
|
|
##
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
let wakuRelay = node.wakuRelay
|
2020-07-29 13:24:01 +00:00
|
|
|
|
2020-09-02 03:15:25 +00:00
|
|
|
debug "publish", topic=topic, contentTopic=message.contentTopic
|
|
|
|
let data = message.encode().buffer
|
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
# XXX Consider awaiting here
|
2020-09-02 03:15:25 +00:00
|
|
|
discard wakuRelay.publish(topic, data)
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-10-06 03:33:28 +00:00
|
|
|
proc query*(node: WakuNode, query: HistoryQuery, handler: QueryHandlerFunc) {.async, gcsafe.} =
|
2020-09-24 02:16:25 +00:00
|
|
|
## Queries known nodes for historical messages. Triggers the handler whenever a response is received.
|
|
|
|
## QueryHandlerFunc is a method that takes a HistoryResponse.
|
2020-07-27 09:01:06 +00:00
|
|
|
##
|
2020-09-24 02:16:25 +00:00
|
|
|
## Status: Implemented.
|
2020-10-06 03:33:28 +00:00
|
|
|
await node.wakuStore.query(query, handler)
|
|
|
|
|
|
|
|
# TODO Extend with more relevant info: topics, peers, memory usage, online time, etc
|
|
|
|
proc info*(node: WakuNode): WakuInfo =
|
|
|
|
## Returns information about the Node, such as what multiaddress it can be reached at.
|
|
|
|
##
|
|
|
|
## Status: Implemented.
|
|
|
|
##
|
|
|
|
|
|
|
|
# TODO Generalize this for other type of multiaddresses
|
|
|
|
let peerInfo = node.peerInfo
|
|
|
|
let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId
|
|
|
|
let wakuInfo = WakuInfo(listenStr: listenStr)
|
|
|
|
return wakuInfo
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-10-20 02:36:27 +00:00
|
|
|
proc mountFilter*(node: WakuNode) =
|
|
|
|
info "mounting filter"
|
|
|
|
proc filterHandler(requestId: string, msg: MessagePush) {.gcsafe.} =
|
|
|
|
info "push received"
|
|
|
|
for message in msg.messages:
|
|
|
|
node.filters.notify(message, requestId)
|
|
|
|
|
|
|
|
node.wakuFilter = WakuFilter.init(node.switch, node.rng, filterHandler)
|
|
|
|
node.switch.mount(node.wakuFilter)
|
|
|
|
node.subscriptions.subscribe(WakuFilterCodec, node.wakuFilter.subscription())
|
|
|
|
|
2020-11-16 08:38:52 +00:00
|
|
|
proc mountStore*(node: WakuNode, store: MessageStore = nil) =
|
2020-10-20 02:36:27 +00:00
|
|
|
info "mounting store"
|
2020-11-16 08:38:52 +00:00
|
|
|
node.wakuStore = WakuStore.init(node.switch, node.rng, store)
|
2020-10-20 02:36:27 +00:00
|
|
|
node.switch.mount(node.wakuStore)
|
|
|
|
node.subscriptions.subscribe(WakuStoreCodec, node.wakuStore.subscription())
|
|
|
|
|
|
|
|
proc mountRelay*(node: WakuNode, topics: seq[string] = newSeq[string]()) {.async, gcsafe.} =
|
|
|
|
let wakuRelay = WakuRelay.init(
|
|
|
|
switch = node.switch,
|
|
|
|
# Use default
|
|
|
|
#msgIdProvider = msgIdProvider,
|
|
|
|
triggerSelf = true,
|
|
|
|
sign = false,
|
|
|
|
verifySignature = false
|
|
|
|
)
|
|
|
|
|
|
|
|
node.wakuRelay = wakuRelay
|
|
|
|
node.switch.mount(wakuRelay)
|
|
|
|
|
|
|
|
info "mounting relay"
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
node.filters.notify(msg.value(), "")
|
|
|
|
await node.subscriptions.notify(topic, msg.value())
|
|
|
|
|
2020-10-28 05:19:07 +00:00
|
|
|
await node.wakuRelay.subscribe("/waku/2/default-waku/proto", relayHandler)
|
2020-10-20 02:36:27 +00:00
|
|
|
|
|
|
|
for topic in topics:
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
debug "Hit handler", topic=topic, data=data
|
|
|
|
|
|
|
|
# XXX: Is using discard here fine? Not sure if we want init to be async?
|
|
|
|
# Can also move this to the start proc, possibly wiser?
|
|
|
|
discard node.subscribe(topic, handler)
|
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
## Helpers
|
|
|
|
proc parsePeerInfo(address: string): PeerInfo =
|
|
|
|
let multiAddr = MultiAddress.initAddress(address)
|
|
|
|
let parts = address.split("/")
|
|
|
|
return PeerInfo.init(parts[^1], [multiAddr])
|
2020-10-20 02:36:27 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
proc dialPeer*(n: WakuNode, address: string) {.async.} =
|
|
|
|
info "dialPeer", address = address
|
|
|
|
# XXX: This turns ipfs into p2p, not quite sure why
|
|
|
|
let remotePeer = parsePeerInfo(address)
|
2020-09-25 11:35:32 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
info "Dialing peer", ma = remotePeer.addrs[0]
|
|
|
|
# NOTE This is dialing on WakuRelay protocol specifically
|
|
|
|
# TODO Keep track of conn and connected state somewhere (WakuRelay?)
|
|
|
|
#p.conn = await p.switch.dial(remotePeer, WakuRelayCodec)
|
|
|
|
#p.connected = true
|
2020-10-22 11:12:00 +00:00
|
|
|
discard await n.switch.dial(remotePeer, WakuRelayCodec)
|
2020-10-21 09:54:29 +00:00
|
|
|
info "Post switch dial"
|
2020-09-11 04:16:45 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
proc setStorePeer*(n: WakuNode, address: string) =
|
|
|
|
info "dialPeer", address = address
|
2020-09-11 04:16:45 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
let remotePeer = parsePeerInfo(address)
|
2020-09-25 11:35:32 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
n.wakuStore.setPeer(remotePeer)
|
2020-09-25 11:35:32 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
proc setFilterPeer*(n: WakuNode, address: string) =
|
|
|
|
info "dialPeer", address = address
|
2020-09-25 11:35:32 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
let remotePeer = parsePeerInfo(address)
|
2020-10-09 13:58:50 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
n.wakuFilter.setPeer(remotePeer)
|
2020-10-09 13:58:50 +00:00
|
|
|
|
2020-10-22 11:12:00 +00:00
|
|
|
proc connectToNodes*(n: WakuNode, nodes: seq[string]) {.async.} =
|
2020-10-21 09:54:29 +00:00
|
|
|
for nodeId in nodes:
|
|
|
|
info "connectToNodes", node = nodeId
|
|
|
|
# XXX: This seems...brittle
|
2020-10-22 11:12:00 +00:00
|
|
|
await dialPeer(n, nodeId)
|
|
|
|
|
|
|
|
# The issue seems to be around peers not being fully connected when
|
|
|
|
# trying to subscribe. So what we do is sleep to guarantee nodes are
|
|
|
|
# fully connected.
|
|
|
|
#
|
|
|
|
# This issue was known to Dmitiry on nim-libp2p and may be resolvable
|
|
|
|
# later.
|
|
|
|
await sleepAsync(5.seconds)
|
|
|
|
|
|
|
|
proc connectToNodes*(n: WakuNode, nodes: seq[PeerInfo]) {.async.} =
|
|
|
|
for peerInfo in nodes:
|
|
|
|
info "connectToNodes", peer = peerInfo
|
|
|
|
discard await n.switch.dial(peerInfo, WakuRelayCodec)
|
|
|
|
|
|
|
|
# The issue seems to be around peers not being fully connected when
|
|
|
|
# trying to subscribe. So what we do is sleep to guarantee nodes are
|
|
|
|
# fully connected.
|
|
|
|
#
|
|
|
|
# This issue was known to Dmitiry on nim-libp2p and may be resolvable
|
|
|
|
# later.
|
|
|
|
await sleepAsync(5.seconds)
|
2020-10-09 13:58:50 +00:00
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
when isMainModule:
|
|
|
|
import
|
|
|
|
confutils, json_rpc/rpcserver, metrics,
|
|
|
|
./config, ./rpc/wakurpc, ../common
|
2020-09-11 04:16:45 +00:00
|
|
|
|
|
|
|
proc startRpc(node: WakuNode, rpcIp: ValidIpAddress, rpcPort: Port) =
|
|
|
|
let
|
|
|
|
ta = initTAddress(rpcIp, rpcPort)
|
|
|
|
rpcServer = newRpcHttpServer([ta])
|
|
|
|
setupWakuRPC(node, rpcServer)
|
|
|
|
rpcServer.start()
|
|
|
|
info "RPC Server started", ta
|
|
|
|
|
|
|
|
proc startMetricsServer(serverIp: ValidIpAddress, serverPort: Port) =
|
|
|
|
info "Starting metrics HTTP server", serverIp, serverPort
|
|
|
|
metrics.startHttpServer($serverIp, serverPort)
|
|
|
|
|
|
|
|
proc startMetricsLog() =
|
|
|
|
proc logMetrics(udata: pointer) {.closure, gcsafe.} =
|
|
|
|
{.gcsafe.}:
|
|
|
|
# TODO: libp2p_pubsub_peers is not public, so we need to make this either
|
|
|
|
# public in libp2p or do our own peer counting after all.
|
|
|
|
let
|
|
|
|
totalMessages = total_messages.value
|
|
|
|
|
|
|
|
info "Node metrics", totalMessages
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
|
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
let
|
|
|
|
conf = WakuNodeConf.load()
|
|
|
|
(extIp, extTcpPort, extUdpPort) = setupNat(conf.nat, clientId,
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift),
|
|
|
|
Port(uint16(conf.udpPort) + conf.portsShift))
|
2020-10-22 15:04:17 +00:00
|
|
|
node = WakuNode.init(conf.nodeKey, conf.listenAddress,
|
2020-10-20 02:36:27 +00:00
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort)
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
|
|
waitFor node.start()
|
|
|
|
|
2020-10-20 02:36:27 +00:00
|
|
|
if conf.store:
|
2020-11-16 08:38:52 +00:00
|
|
|
var store: MessageStore
|
|
|
|
|
|
|
|
if conf.dbpath != "":
|
|
|
|
let res = MessageStore.init(conf.dbpath)
|
|
|
|
if res.isErr:
|
|
|
|
warn "failed to init MessageStore", err = res.error
|
|
|
|
else:
|
|
|
|
store = res.value
|
|
|
|
|
|
|
|
mountStore(node, store)
|
2020-10-20 02:36:27 +00:00
|
|
|
|
|
|
|
if conf.filter:
|
|
|
|
mountFilter(node)
|
|
|
|
|
|
|
|
if conf.relay:
|
|
|
|
waitFor mountRelay(node, conf.topics.split(" "))
|
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
if conf.staticnodes.len > 0:
|
2020-10-22 11:12:00 +00:00
|
|
|
waitFor connectToNodes(node, conf.staticnodes)
|
2020-09-01 02:09:54 +00:00
|
|
|
|
2020-09-25 11:35:32 +00:00
|
|
|
if conf.storenode != "":
|
|
|
|
setStorePeer(node, conf.storenode)
|
|
|
|
|
2020-10-09 13:58:50 +00:00
|
|
|
if conf.filternode != "":
|
|
|
|
setFilterPeer(node, conf.filternode)
|
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
if conf.rpc:
|
|
|
|
startRpc(node, conf.rpcAddress, Port(conf.rpcPort + conf.portsShift))
|
|
|
|
|
|
|
|
if conf.logMetrics:
|
|
|
|
startMetricsLog()
|
|
|
|
|
|
|
|
when defined(insecure):
|
|
|
|
if conf.metricsServer:
|
|
|
|
startMetricsServer(conf.metricsServerAddress,
|
|
|
|
Port(conf.metricsServerPort + conf.portsShift))
|
|
|
|
|
2020-07-28 08:06:00 +00:00
|
|
|
runForever()
|