2020-05-14 02:28:34 +00:00
|
|
|
#
|
|
|
|
# Waku
|
|
|
|
# (c) Copyright 2019
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
2020-08-31 03:32:41 +00:00
|
|
|
{.used.}
|
2020-05-14 02:28:34 +00:00
|
|
|
|
2020-07-21 12:19:31 +00:00
|
|
|
import unittest, options, tables, sets, sequtils
|
2020-05-15 03:35:32 +00:00
|
|
|
import chronos, chronicles
|
2020-05-14 02:28:34 +00:00
|
|
|
import utils,
|
2020-07-13 10:08:03 +00:00
|
|
|
libp2p/errors,
|
|
|
|
libp2p/switch,
|
2020-07-23 02:53:29 +00:00
|
|
|
libp2p/protobuf/minprotobuf,
|
2020-07-13 10:08:03 +00:00
|
|
|
libp2p/stream/[bufferstream, connection],
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/protocols/pubsub/floodsub
|
2020-11-17 09:34:53 +00:00
|
|
|
import ../../waku/v2/protocol/waku_relay
|
2020-05-14 02:28:34 +00:00
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
import ../test_helpers
|
|
|
|
|
2020-05-14 02:28:34 +00:00
|
|
|
const
|
|
|
|
StreamTransportTrackerName = "stream.transport"
|
|
|
|
StreamServerTrackerName = "stream.server"
|
|
|
|
|
|
|
|
# TODO: Start with floodsub here, then move other logic here
|
|
|
|
|
2020-08-26 11:28:24 +00:00
|
|
|
# XXX: If I cast to WakuRelay here I get a SIGSEGV
|
2020-05-14 02:28:34 +00:00
|
|
|
proc waitSub(sender, receiver: auto; key: string) {.async, gcsafe.} =
|
|
|
|
# turn things deterministic
|
|
|
|
# this is for testing purposes only
|
|
|
|
var ceil = 15
|
2020-08-26 11:28:24 +00:00
|
|
|
let fsub = cast[WakuRelay](sender.pubSub.get())
|
2020-05-14 02:28:34 +00:00
|
|
|
while not fsub.floodsub.hasKey(key) or
|
2020-07-21 12:19:31 +00:00
|
|
|
not fsub.floodsub[key].anyIt(it.peerInfo.id == receiver.peerInfo.id):
|
2020-05-14 02:28:34 +00:00
|
|
|
await sleepAsync(100.millis)
|
|
|
|
dec ceil
|
|
|
|
doAssert(ceil > 0, "waitSub timeout!")
|
|
|
|
|
2020-07-23 02:53:29 +00:00
|
|
|
proc message(): seq[byte] =
|
|
|
|
var pb = initProtoBuffer()
|
|
|
|
pb.write(1, "hello")
|
|
|
|
pb.finish()
|
|
|
|
|
|
|
|
pb.buffer
|
|
|
|
|
|
|
|
proc decodeMessage(data: seq[byte]): string =
|
|
|
|
var pb = initProtoBuffer(data)
|
|
|
|
|
|
|
|
result = ""
|
|
|
|
let res = pb.getField(1, result)
|
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
procSuite "FloodSub":
|
2020-05-14 02:28:34 +00:00
|
|
|
teardown:
|
|
|
|
let
|
|
|
|
trackers = [
|
|
|
|
# getTracker(ConnectionTrackerName),
|
|
|
|
getTracker(BufferStreamTrackerName),
|
|
|
|
getTracker(AsyncStreamWriterTrackerName),
|
|
|
|
getTracker(AsyncStreamReaderTrackerName),
|
|
|
|
getTracker(StreamTransportTrackerName),
|
|
|
|
getTracker(StreamServerTrackerName)
|
|
|
|
]
|
|
|
|
for tracker in trackers:
|
|
|
|
if not isNil(tracker):
|
|
|
|
check tracker.isLeaked() == false
|
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
asyncTest "FloodSub basic publish/subscribe A -> B":
|
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
debug "Hit handler", topic
|
|
|
|
let msg = decodeMessage(data)
|
|
|
|
check topic == "foobar"
|
|
|
|
check msg == "hello"
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
let
|
|
|
|
nodes = generateNodes(2)
|
2020-10-20 02:36:27 +00:00
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
nodesFut = await allFinished(
|
|
|
|
nodes[0].start(),
|
|
|
|
nodes[1].start()
|
2020-05-14 02:28:34 +00:00
|
|
|
)
|
|
|
|
|
2020-10-20 02:36:27 +00:00
|
|
|
for node in nodes:
|
2021-02-02 11:33:59 +00:00
|
|
|
node.mountRelay()
|
2020-10-20 02:36:27 +00:00
|
|
|
|
2020-07-29 13:24:01 +00:00
|
|
|
await subscribeNodes(nodes)
|
|
|
|
|
|
|
|
await nodes[1].subscribe("foobar", handler)
|
|
|
|
await waitSub(nodes[0], nodes[1], "foobar")
|
|
|
|
|
|
|
|
# TODO: you might want to check the value here
|
|
|
|
let msg = message()
|
|
|
|
discard await nodes[0].publish("foobar", msg)
|
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
check: await completionFut.wait(5.seconds)
|
2020-07-29 13:24:01 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop()
|
|
|
|
)
|
|
|
|
|
|
|
|
for fut in nodesFut:
|
|
|
|
let res = fut.read()
|
|
|
|
await allFuturesThrowing(res)
|