2020-04-29 04:49:27 +00:00
|
|
|
import
|
2020-08-31 03:32:41 +00:00
|
|
|
std/[strutils, options],
|
|
|
|
chronos, confutils, json_rpc/rpcserver, metrics, stew/shims/net as stewNet,
|
|
|
|
# TODO: Why do we need eth keys?
|
2020-09-01 02:09:54 +00:00
|
|
|
eth/keys,
|
2020-08-31 03:32:41 +00:00
|
|
|
# eth/[keys, p2p], eth/net/nat, eth/p2p/[discovery, enode],
|
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-01 02:09:54 +00:00
|
|
|
../../protocol/v2/waku_relay, ../common,
|
2020-08-31 03:32:41 +00:00
|
|
|
./waku_types, ./config, ./standard_setup, ./rpc/wakurpc
|
2020-04-29 04:49:27 +00:00
|
|
|
|
|
|
|
# key and crypto modules different
|
|
|
|
type
|
|
|
|
KeyPair* = crypto.KeyPair
|
|
|
|
PublicKey* = crypto.PublicKey
|
|
|
|
PrivateKey* = crypto.PrivateKey
|
|
|
|
|
2020-08-28 09:08:28 +00:00
|
|
|
Topic* = waku_types.Topic
|
2020-07-29 13:24:01 +00:00
|
|
|
Message* = seq[byte]
|
|
|
|
ContentFilter* = object
|
|
|
|
contentTopic*: string
|
|
|
|
|
|
|
|
HistoryQuery* = object
|
|
|
|
topics*: seq[string]
|
|
|
|
|
|
|
|
HistoryResponse* = object
|
|
|
|
messages*: seq[Message]
|
|
|
|
|
2020-09-01 08:32:19 +00:00
|
|
|
const clientId* = "Nimbus Waku v2 node"
|
2020-04-29 04:49:27 +00:00
|
|
|
|
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-08-28 09:08:28 +00:00
|
|
|
proc dialPeer(n: WakuNode, address: string) {.async.} =
|
2020-05-18 05:07:36 +00:00
|
|
|
info "dialPeer", address = address
|
|
|
|
# XXX: This turns ipfs into p2p, not quite sure why
|
2020-05-01 04:24:34 +00:00
|
|
|
let multiAddr = MultiAddress.initAddress(address)
|
2020-05-18 05:07:36 +00:00
|
|
|
info "multiAddr", ma = multiAddr
|
2020-05-01 04:24:34 +00:00
|
|
|
let parts = address.split("/")
|
|
|
|
let remotePeer = PeerInfo.init(parts[^1], [multiAddr])
|
|
|
|
|
|
|
|
info "Dialing peer", multiAddr
|
2020-08-28 09:08:28 +00:00
|
|
|
# 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)
|
2020-05-18 06:03:15 +00:00
|
|
|
info "Post switch dial"
|
2020-05-01 04:24:34 +00:00
|
|
|
|
2020-08-28 09:08:28 +00:00
|
|
|
proc connectToNodes(n: WakuNode, nodes: openArray[string]) =
|
2020-05-28 03:40:41 +00:00
|
|
|
for nodeId in nodes:
|
|
|
|
info "connectToNodes", node = nodeId
|
|
|
|
# XXX: This seems...brittle
|
2020-08-28 09:08:28 +00:00
|
|
|
discard dialPeer(n, nodeId)
|
2020-05-28 03:40:41 +00:00
|
|
|
# Waku 1
|
|
|
|
# let whisperENode = ENode.fromString(nodeId).expect("correct node")
|
|
|
|
# traceAsyncErrors node.peerPool.connectToNode(newNode(whisperENode))
|
2020-04-29 04:49:27 +00:00
|
|
|
|
2020-09-01 02:09:54 +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
|
2020-07-24 01:39:58 +00:00
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
info "Node metrics", totalMessages
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
|
|
|
|
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
|
2020-04-29 04:49:27 +00:00
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
## Public API
|
|
|
|
##
|
|
|
|
|
|
|
|
proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey,
|
|
|
|
bindIp: ValidIpAddress, bindPort: Port,
|
|
|
|
extIp = none[ValidIpAddress](), extPort = none[Port]()): T =
|
|
|
|
## 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-01 02:09:54 +00:00
|
|
|
var switch = newStandardSwitch(some(nodekey), hostAddress, triggerSelf = true)
|
2020-07-24 01:39:58 +00:00
|
|
|
|
2020-09-01 02:09:54 +00:00
|
|
|
return WakuNode(switch: switch, peerInfo: peerInfo)
|
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-08-28 09:08:28 +00:00
|
|
|
# NOTE WakuRelay is being instantiated as part of creating switch with PubSub field set
|
|
|
|
#
|
2020-08-26 11:28:24 +00:00
|
|
|
# TODO Mount Waku Store and Waku Filter here
|
2020-07-24 01:39:58 +00:00
|
|
|
|
|
|
|
# TODO Get this from WakuNode obj
|
|
|
|
let peerInfo = node.peerInfo
|
2020-04-29 04:49:27 +00:00
|
|
|
let id = peerInfo.peerId.pretty
|
|
|
|
info "PeerInfo", id = id, addrs = peerInfo.addrs
|
2020-05-18 06:03:15 +00:00
|
|
|
let listenStr = $peerInfo.addrs[0] & "/p2p/" & id
|
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-08-27 10:15:46 +00:00
|
|
|
# NOTE TopicHandler is defined in pubsub.nim, roughly:
|
2020-07-28 08:17:50 +00:00
|
|
|
#type TopicHandler* = proc(topic: string, data: seq[byte])
|
|
|
|
|
2020-07-27 09:01:06 +00:00
|
|
|
type ContentFilterHandler* = proc(contentFilter: ContentFilter, message: Message)
|
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
proc subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) =
|
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-07-28 08:17:50 +00:00
|
|
|
|
2020-08-27 10:15:46 +00:00
|
|
|
let wakuRelay = w.switch.pubSub.get()
|
2020-07-28 08:17:50 +00:00
|
|
|
# XXX Consider awaiting here
|
2020-08-27 10:15:46 +00:00
|
|
|
discard wakuRelay.subscribe(topic, handler)
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
proc subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) =
|
2020-07-27 09:01:06 +00:00
|
|
|
echo "NYI"
|
|
|
|
## 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`.
|
|
|
|
|
|
|
|
## Status: Not yet implemented.
|
2020-08-27 10:15:46 +00:00
|
|
|
## TODO Implement as wrapper around `waku_filter` and `subscribe` above.
|
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-08-31 03:32:41 +00:00
|
|
|
proc unsubscribe*(w: WakuNode, contentFilter: ContentFilter) =
|
2020-07-27 09:01:06 +00:00
|
|
|
echo "NYI"
|
|
|
|
## Unsubscribe from a content filter.
|
|
|
|
##
|
|
|
|
## Status: Not yet implemented.
|
|
|
|
## TODO Implement.
|
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
proc publish*(w: WakuNode, topic: Topic, message: Message) =
|
2020-07-27 09:01:06 +00:00
|
|
|
## Publish a `Message` to a PubSub topic.
|
|
|
|
##
|
2020-07-28 08:18:30 +00:00
|
|
|
## Status: Partially implemented.
|
2020-08-27 10:15:46 +00:00
|
|
|
##
|
|
|
|
## TODO WakuMessage OR seq[byte]. NOT PubSub Message.
|
2020-07-28 08:18:30 +00:00
|
|
|
let wakuSub = w.switch.pubSub.get()
|
|
|
|
# XXX Consider awaiting here
|
|
|
|
discard wakuSub.publish(topic, message)
|
2020-07-27 09:01:06 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
proc publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message: Message) =
|
2020-07-27 09:01:06 +00:00
|
|
|
## Publish a `Message` to a PubSub topic with a specific content filter.
|
|
|
|
## Currently this means a `contentTopic`.
|
|
|
|
##
|
|
|
|
## Status: Not yet implemented.
|
2020-08-27 10:15:46 +00:00
|
|
|
## TODO Implement as wrapper around `waku_relay` and `publish`.
|
|
|
|
## TODO WakuMessage. Ensure content filter is in it.
|
2020-07-29 13:24:01 +00:00
|
|
|
|
|
|
|
w.messages.insert((contentFilter.contentTopic, message))
|
|
|
|
|
|
|
|
let wakuSub = w.switch.pubSub.get()
|
|
|
|
# XXX Consider awaiting here
|
|
|
|
|
|
|
|
discard wakuSub.publish(topic, message)
|
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
|
|
|
|
|
|
|
|
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-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,
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort)
|
|
|
|
|
|
|
|
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()
|