2020-04-29 04:49:27 +00:00
|
|
|
import
|
2020-09-11 04:16:45 +00:00
|
|
|
std/[options, tables],
|
|
|
|
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-09-16 04:23:10 +00:00
|
|
|
./waku_types
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
HistoryQuery* = object
|
|
|
|
topics*: seq[string]
|
|
|
|
|
|
|
|
HistoryResponse* = object
|
|
|
|
messages*: seq[Message]
|
|
|
|
|
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-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-09-11 11:28:27 +00:00
|
|
|
extIp = none[ValidIpAddress](), extPort = none[Port](), topics = newSeq[string]()): T =
|
2020-09-01 02:09:54 +00:00
|
|
|
## Creates and starts a Waku node.
|
2020-04-29 04:49:27 +00:00
|
|
|
let
|
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-09-16 04:23:10 +00:00
|
|
|
var switch = newStandardSwitch(some(nodekey), hostAddress)
|
|
|
|
# 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
|
|
|
|
let wakuRelay = WakuRelay.init(
|
|
|
|
switch = switch,
|
|
|
|
# Use default
|
|
|
|
#msgIdProvider = msgIdProvider,
|
|
|
|
triggerSelf = true,
|
|
|
|
sign = false,
|
|
|
|
verifySignature = false)
|
|
|
|
# This gets messy with: .PubSub
|
|
|
|
switch.mount(wakuRelay)
|
|
|
|
|
|
|
|
result = WakuNode(switch: switch, peerInfo: peerInfo, wakuRelay: wakuRelay)
|
2020-09-11 11:28:27 +00:00
|
|
|
|
|
|
|
for topic in topics:
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
debug "Hit handler", topic=topic, data=data
|
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
# 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 result.subscribe(topic, handler)
|
2020-07-24 01:39:58 +00:00
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
proc start*(node: WakuNode) {.async.} =
|
2020-07-24 01:39:58 +00:00
|
|
|
node.libp2pTransportLoops = await node.switch.start()
|
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
# NOTE WakuRelay is being instantiated as part of initing node
|
2020-09-02 03:08:48 +00:00
|
|
|
let storeProto = WakuStore.init()
|
|
|
|
node.switch.mount(storeProto)
|
2020-09-17 02:17:52 +00:00
|
|
|
node.subscriptions.subscribe(WakuStoreCodec, storeProto.subscription())
|
2020-09-02 03:08:48 +00:00
|
|
|
|
2020-09-07 12:22:46 +00:00
|
|
|
let filterProto = WakuFilter.init()
|
|
|
|
node.switch.mount(filterProto)
|
2020-09-17 02:17:52 +00:00
|
|
|
node.subscriptions.subscribe(WakuFilterCodec, filterProto.subscription())
|
|
|
|
|
|
|
|
proc relayHandler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
node.subscriptions.notify(topic, msg.value())
|
|
|
|
|
|
|
|
await node.wakuRelay.subscribe("waku", relayHandler)
|
2020-09-07 12:22:46 +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-09-16 04:23:10 +00:00
|
|
|
let wakuRelay = node.wakuRelay
|
2020-09-02 03:15:25 +00:00
|
|
|
await 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-09-16 04:23:10 +00:00
|
|
|
proc subscribe*(node: WakuNode, contentFilter: waku_types.ContentFilter, handler: ContentFilterHandler) {.async.} =
|
2020-07-27 09:01:06 +00:00
|
|
|
## Subscribes to a ContentFilter. Triggers handler when receiving messages on
|
|
|
|
## this content filter. ContentFilter is a method that takes some content
|
|
|
|
## filter, specifically with `ContentTopic`, and a `Message`. The `Message`
|
|
|
|
## has to match the `ContentTopic`.
|
2020-09-16 04:23:10 +00:00
|
|
|
info "subscribe content", contentFilter=contentFilter
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-09-02 03:15:25 +00:00
|
|
|
# TODO: get some random id, or use the Filter directly as key
|
2020-09-16 04:23:10 +00:00
|
|
|
node.filters.add("some random id", Filter(contentFilter: contentFilter, handler: handler))
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
proc unsubscribe*(w: WakuNode, topic: Topic) =
|
2020-07-27 09:01:06 +00:00
|
|
|
echo "NYI"
|
|
|
|
## Unsubscribe from a topic.
|
|
|
|
##
|
|
|
|
## Status: Not yet implemented.
|
|
|
|
## TODO Implement.
|
|
|
|
|
2020-09-07 12:22:46 +00:00
|
|
|
proc unsubscribe*(w: WakuNode, contentFilter: waku_types.ContentFilter) =
|
2020-07-27 09:01:06 +00:00
|
|
|
echo "NYI"
|
|
|
|
## Unsubscribe from a content filter.
|
|
|
|
##
|
|
|
|
## Status: Not yet implemented.
|
|
|
|
## TODO Implement.
|
|
|
|
|
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-01 15:20:38 +00:00
|
|
|
# XXX Unclear what the purpose of this is
|
|
|
|
# Commenting out as it is later expected to be Message type, not WakuMessage
|
|
|
|
#node.messages.insert((topic, message))
|
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-08-31 03:32:41 +00:00
|
|
|
proc query*(w: WakuNode, query: HistoryQuery): HistoryResponse =
|
2020-07-27 09:01:06 +00:00
|
|
|
## Queries for historical messages.
|
|
|
|
##
|
|
|
|
## Status: Not yet implemented.
|
2020-08-27 10:15:46 +00:00
|
|
|
## TODO Implement as wrapper around `waku_store` and send RPC.
|
2020-07-29 13:24:01 +00:00
|
|
|
result.messages = newSeq[Message]()
|
|
|
|
|
|
|
|
for msg in w.messages:
|
|
|
|
if msg[0] notin query.topics:
|
|
|
|
continue
|
|
|
|
|
2020-09-01 15:20:38 +00:00
|
|
|
# XXX Unclear how this should be hooked up, Message or WakuMessage?
|
|
|
|
# result.messages.insert(msg[1])
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-04-29 04:49:27 +00:00
|
|
|
when isMainModule:
|
2020-09-11 04:16:45 +00:00
|
|
|
import
|
|
|
|
std/strutils,
|
|
|
|
confutils, json_rpc/rpcserver, metrics,
|
|
|
|
./config, ./rpc/wakurpc, ../common
|
|
|
|
|
|
|
|
proc dialPeer(n: WakuNode, address: string) {.async.} =
|
|
|
|
info "dialPeer", address = address
|
|
|
|
# XXX: This turns ipfs into p2p, not quite sure why
|
|
|
|
let multiAddr = MultiAddress.initAddress(address)
|
|
|
|
info "multiAddr", ma = multiAddr
|
|
|
|
let parts = address.split("/")
|
|
|
|
let remotePeer = PeerInfo.init(parts[^1], [multiAddr])
|
|
|
|
|
|
|
|
info "Dialing peer", multiAddr
|
|
|
|
# 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
|
|
|
|
discard n.switch.dial(remotePeer, WakuRelayCodec)
|
|
|
|
info "Post switch dial"
|
|
|
|
|
|
|
|
proc connectToNodes(n: WakuNode, nodes: openArray[string]) =
|
|
|
|
for nodeId in nodes:
|
|
|
|
info "connectToNodes", node = nodeId
|
|
|
|
# XXX: This seems...brittle
|
|
|
|
discard dialPeer(n, nodeId)
|
|
|
|
# Waku 1
|
|
|
|
# let whisperENode = ENode.fromString(nodeId).expect("correct node")
|
|
|
|
# traceAsyncErrors node.peerPool.connectToNode(newNode(whisperENode))
|
|
|
|
|
|
|
|
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))
|
|
|
|
node = WakuNode.init(conf.nodeKey, conf.libp2pAddress,
|
2020-09-11 11:28:27 +00:00
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort, conf.topics.split(" "))
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
|
|
waitFor node.start()
|
|
|
|
|
|
|
|
if conf.staticnodes.len > 0:
|
|
|
|
connectToNodes(node, conf.staticnodes)
|
|
|
|
|
|
|
|
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()
|