2019-12-06 02:16:18 +00:00
|
|
|
## Nim-Libp2p
|
|
|
|
## Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2020-05-08 20:58:23 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
import unittest, sequtils, options, tables, sets
|
2020-06-03 02:21:11 +00:00
|
|
|
import chronos, stew/byteutils
|
2020-04-21 01:24:42 +00:00
|
|
|
import chronicles
|
|
|
|
import utils, ../../libp2p/[errors,
|
2020-07-01 06:25:09 +00:00
|
|
|
peerid,
|
2019-12-06 02:16:18 +00:00
|
|
|
peerinfo,
|
2020-06-19 17:29:43 +00:00
|
|
|
stream/connection,
|
2019-12-06 02:16:18 +00:00
|
|
|
crypto/crypto,
|
|
|
|
protocols/pubsub/pubsub,
|
2019-12-17 05:24:03 +00:00
|
|
|
protocols/pubsub/rpc/messages]
|
|
|
|
|
2020-07-04 02:58:35 +00:00
|
|
|
when not defined(gossip11):
|
|
|
|
import ../../libp2p/protocols/pubsub/gossipsub
|
|
|
|
else:
|
|
|
|
import ../../libp2p/protocols/pubsub/gossipsub11
|
|
|
|
|
2020-05-08 20:10:06 +00:00
|
|
|
import ../helpers
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
proc waitSub(sender, receiver: auto; key: string) {.async, gcsafe.} =
|
|
|
|
if sender == receiver:
|
|
|
|
return
|
|
|
|
# turn things deterministic
|
|
|
|
# this is for testing purposes only
|
|
|
|
# peers can be inside `mesh` and `fanout`, not just `gossipsub`
|
|
|
|
var ceil = 15
|
2020-05-21 17:33:48 +00:00
|
|
|
let fsub = GossipSub(sender.pubSub.get())
|
2020-04-21 01:24:42 +00:00
|
|
|
while (not fsub.gossipsub.hasKey(key) or
|
|
|
|
not fsub.gossipsub[key].contains(receiver.peerInfo.id)) and
|
|
|
|
(not fsub.mesh.hasKey(key) or
|
|
|
|
not fsub.mesh[key].contains(receiver.peerInfo.id)) and
|
|
|
|
(not fsub.fanout.hasKey(key) or
|
|
|
|
not fsub.fanout[key].contains(receiver.peerInfo.id)):
|
2020-05-27 18:33:49 +00:00
|
|
|
trace "waitSub sleeping..."
|
2020-06-29 15:10:11 +00:00
|
|
|
await sleepAsync(1.seconds)
|
2020-04-21 01:24:42 +00:00
|
|
|
dec ceil
|
|
|
|
doAssert(ceil > 0, "waitSub timeout!")
|
|
|
|
|
2020-06-28 03:50:11 +00:00
|
|
|
template tryPublish(call: untyped, require: int, wait: Duration = 1.seconds, times: int = 10): untyped =
|
|
|
|
var
|
|
|
|
limit = times
|
|
|
|
pubs = 0
|
|
|
|
while pubs < require and limit > 0:
|
|
|
|
pubs = pubs + call
|
2020-06-27 05:59:48 +00:00
|
|
|
await sleepAsync(wait)
|
|
|
|
limit.dec()
|
|
|
|
if limit == 0:
|
|
|
|
doAssert(false, "Failed to publish!")
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
suite "GossipSub":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
2020-05-08 20:10:06 +00:00
|
|
|
for tracker in testTrackers():
|
2020-06-24 15:08:44 +00:00
|
|
|
# echo tracker.dump()
|
2020-05-08 20:10:06 +00:00
|
|
|
check tracker.isLeaked() == false
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
test "GossipSub validation should succeed":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var handlerFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-05-20 00:33:18 +00:00
|
|
|
check topic == "foobar"
|
2019-12-17 05:24:03 +00:00
|
|
|
handlerFut.complete(true)
|
|
|
|
|
|
|
|
var nodes = generateNodes(2, true)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await subscribeNodes(nodes)
|
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
await nodes[0].subscribe("foobar", handler)
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[1].subscribe("foobar", handler)
|
|
|
|
|
|
|
|
var validatorFut = newFuture[bool]()
|
|
|
|
proc validator(topic: string,
|
|
|
|
message: Message):
|
|
|
|
Future[bool] {.async.} =
|
2020-05-20 00:33:18 +00:00
|
|
|
check topic == "foobar"
|
2019-12-17 05:24:03 +00:00
|
|
|
validatorFut.complete(true)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
nodes[1].addValidator("foobar", validator)
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await nodes[0].publish("foobar", "Hello!".toBytes()), 1
|
2020-01-07 08:06:27 +00:00
|
|
|
result = (await validatorFut) and (await handlerFut)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop())
|
|
|
|
await allFuturesThrowing(awaiters)
|
2020-01-07 08:06:27 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "GossipSub validation should fail":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-05-20 00:33:18 +00:00
|
|
|
check false # if we get here, it should fail
|
2019-12-17 05:24:03 +00:00
|
|
|
|
|
|
|
var nodes = generateNodes(2, true)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[1].subscribe("foobar", handler)
|
|
|
|
|
|
|
|
var validatorFut = newFuture[bool]()
|
|
|
|
proc validator(topic: string,
|
|
|
|
message: Message):
|
|
|
|
Future[bool] {.async.} =
|
|
|
|
result = false
|
2020-06-27 05:59:48 +00:00
|
|
|
validatorFut.complete(true)
|
2019-12-17 05:24:03 +00:00
|
|
|
|
|
|
|
nodes[1].addValidator("foobar", validator)
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await nodes[0].publish("foobar", "Hello!".toBytes()), 1
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
result = await validatorFut
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop())
|
|
|
|
await allFuturesThrowing(awaiters)
|
2019-12-17 05:24:03 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "GossipSub validation one fails and one succeeds":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var handlerFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-05-20 00:33:18 +00:00
|
|
|
check topic == "foo"
|
2019-12-17 05:24:03 +00:00
|
|
|
handlerFut.complete(true)
|
|
|
|
|
|
|
|
var nodes = generateNodes(2, true)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await subscribeNodes(nodes)
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[1].subscribe("foo", handler)
|
|
|
|
await nodes[1].subscribe("bar", handler)
|
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
var passed, failed: Future[bool] = newFuture[bool]()
|
2019-12-17 05:24:03 +00:00
|
|
|
proc validator(topic: string,
|
|
|
|
message: Message):
|
|
|
|
Future[bool] {.async.} =
|
2020-01-07 08:06:27 +00:00
|
|
|
result = if topic == "foo":
|
|
|
|
passed.complete(true)
|
|
|
|
true
|
2019-12-17 05:24:03 +00:00
|
|
|
else:
|
2020-01-07 08:06:27 +00:00
|
|
|
failed.complete(true)
|
|
|
|
false
|
2019-12-17 05:24:03 +00:00
|
|
|
|
|
|
|
nodes[1].addValidator("foo", "bar", validator)
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await nodes[0].publish("foo", "Hello!".toBytes()), 1
|
|
|
|
tryPublish await nodes[0].publish("bar", "Hello!".toBytes()), 1
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
result = ((await passed) and (await failed) and (await handlerFut))
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop())
|
|
|
|
await allFuturesThrowing(awaiters)
|
2019-12-17 05:24:03 +00:00
|
|
|
result = true
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
test "e2e - GossipSub should add remote peer topic subscriptions":
|
2019-12-06 02:16:18 +00:00
|
|
|
proc testBasicGossipSub(): Future[bool] {.async.} =
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
var nodes: seq[Switch] = newSeq[Switch]()
|
|
|
|
for i in 0..<2:
|
2020-07-08 00:33:05 +00:00
|
|
|
nodes.add newStandardSwitch(gossip = true,
|
|
|
|
secureManagers = [SecureProtocol.Noise])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var awaitters: seq[Future[void]]
|
|
|
|
for node in nodes:
|
|
|
|
awaitters.add(await node.start())
|
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
2020-04-21 01:24:42 +00:00
|
|
|
await nodes[1].subscribe("foobar", handler)
|
2020-06-27 05:59:48 +00:00
|
|
|
await sleepAsync(10.seconds)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
let gossip1 = GossipSub(nodes[0].pubSub.get())
|
|
|
|
let gossip2 = GossipSub(nodes[1].pubSub.get())
|
|
|
|
|
|
|
|
check:
|
|
|
|
"foobar" in gossip2.topics
|
|
|
|
"foobar" in gossip1.gossipsub
|
2019-12-07 16:36:39 +00:00
|
|
|
gossip2.peerInfo.id in gossip1.gossipsub["foobar"]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
|
|
|
await allFuturesThrowing(awaitters)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testBasicGossipSub()) == true
|
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
test "e2e - GossipSub should add remote peer topic subscriptions if both peers are subscribed":
|
2019-12-06 02:16:18 +00:00
|
|
|
proc testBasicGossipSub(): Future[bool] {.async.} =
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
var nodes: seq[Switch] = newSeq[Switch]()
|
|
|
|
for i in 0..<2:
|
2020-06-20 10:56:55 +00:00
|
|
|
nodes.add newStandardSwitch(gossip = true, secureManagers = [SecureProtocol.Secio])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var awaitters: seq[Future[void]]
|
|
|
|
for node in nodes:
|
|
|
|
awaitters.add(await node.start())
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await subscribeNodes(nodes)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await nodes[0].subscribe("foobar", handler)
|
|
|
|
await nodes[1].subscribe("foobar", handler)
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var subs: seq[Future[void]]
|
|
|
|
subs &= waitSub(nodes[1], nodes[0], "foobar")
|
|
|
|
subs &= waitSub(nodes[0], nodes[1], "foobar")
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(subs)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
gossip1 = GossipSub(nodes[0].pubSub.get())
|
|
|
|
gossip2 = GossipSub(nodes[1].pubSub.get())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
"foobar" in gossip1.topics
|
|
|
|
"foobar" in gossip2.topics
|
|
|
|
|
|
|
|
"foobar" in gossip1.gossipsub
|
|
|
|
"foobar" in gossip2.gossipsub
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
gossip2.peerInfo.id in gossip1.gossipsub["foobar"] or
|
|
|
|
gossip2.peerInfo.id in gossip1.mesh["foobar"]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
gossip1.peerInfo.id in gossip2.gossipsub["foobar"] or
|
|
|
|
gossip1.peerInfo.id in gossip2.mesh["foobar"]
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
|
|
|
await allFuturesThrowing(awaitters)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testBasicGossipSub()) == true
|
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
test "e2e - GossipSub send over fanout A -> B":
|
2019-12-17 05:24:03 +00:00
|
|
|
proc runTests(): Future[bool] {.async.} =
|
2020-04-21 01:24:42 +00:00
|
|
|
var passed = newFuture[void]()
|
2019-12-10 20:50:35 +00:00
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-05-20 00:33:18 +00:00
|
|
|
check topic == "foobar"
|
2020-04-21 01:24:42 +00:00
|
|
|
passed.complete()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var nodes = generateNodes(2, true)
|
|
|
|
var wait = newSeq[Future[void]]()
|
|
|
|
wait.add(await nodes[0].start())
|
|
|
|
wait.add(await nodes[1].start())
|
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
|
|
|
|
|
|
|
await nodes[1].subscribe("foobar", handler)
|
2020-04-21 01:24:42 +00:00
|
|
|
await waitSub(nodes[0], nodes[1], "foobar")
|
2020-04-30 13:22:31 +00:00
|
|
|
|
|
|
|
var observed = 0
|
|
|
|
let
|
|
|
|
obs1 = PubSubObserver(onRecv: proc(peer: PubSubPeer; msgs: var RPCMsg) =
|
|
|
|
inc observed
|
|
|
|
)
|
|
|
|
obs2 = PubSubObserver(onSend: proc(peer: PubSubPeer; msgs: var RPCMsg) =
|
|
|
|
inc observed
|
|
|
|
)
|
|
|
|
nodes[1].pubsub.get().addObserver(obs1)
|
|
|
|
nodes[0].pubsub.get().addObserver(obs2)
|
|
|
|
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await nodes[0].publish("foobar", "Hello!".toBytes()), 1
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var gossipSub1: GossipSub = GossipSub(nodes[0].pubSub.get())
|
|
|
|
|
|
|
|
check:
|
|
|
|
"foobar" in gossipSub1.gossipsub
|
|
|
|
|
2020-06-27 17:33:34 +00:00
|
|
|
await passed.wait(2.seconds)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
trace "test done, stopping..."
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await nodes[0].stop()
|
2020-04-21 01:24:42 +00:00
|
|
|
await nodes[1].stop()
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(wait)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
check observed == 2
|
2020-06-27 17:33:34 +00:00
|
|
|
result = true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
check:
|
2019-12-17 05:24:03 +00:00
|
|
|
waitFor(runTests()) == true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
test "e2e - GossipSub send over mesh A -> B":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var passed: Future[bool] = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-05-20 00:33:18 +00:00
|
|
|
check topic == "foobar"
|
2020-01-10 03:59:27 +00:00
|
|
|
passed.complete(true)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
var nodes = generateNodes(2, true)
|
|
|
|
var wait: seq[Future[void]]
|
|
|
|
wait.add(await nodes[0].start())
|
|
|
|
wait.add(await nodes[1].start())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
await subscribeNodes(nodes)
|
2020-01-07 08:06:27 +00:00
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
await nodes[1].subscribe("foobar", handler)
|
2020-04-21 01:24:42 +00:00
|
|
|
await waitSub(nodes[0], nodes[1], "foobar")
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await nodes[0].publish("foobar", "Hello!".toBytes()), 1
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
result = await passed
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-01-10 03:59:27 +00:00
|
|
|
await nodes[0].stop()
|
|
|
|
await nodes[1].stop()
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(wait)
|
2020-01-10 03:59:27 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-01-07 08:06:27 +00:00
|
|
|
test "e2e - GossipSub with multiple peers":
|
2019-12-17 05:24:03 +00:00
|
|
|
proc runTests(): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var nodes: seq[Switch] = newSeq[Switch]()
|
|
|
|
var awaitters: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
var runs = 10
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-07-08 00:33:05 +00:00
|
|
|
nodes.add newStandardSwitch(triggerSelf = true,
|
|
|
|
gossip = true,
|
|
|
|
secureManagers = [SecureProtocol.Noise])
|
2019-12-06 02:16:18 +00:00
|
|
|
awaitters.add((await nodes[i].start()))
|
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
await subscribeRandom(nodes)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
var seen: Table[string, int]
|
2020-01-07 08:06:27 +00:00
|
|
|
var subs: seq[Future[void]]
|
|
|
|
var seenFut = newFuture[void]()
|
2019-12-06 02:16:18 +00:00
|
|
|
for dialer in nodes:
|
|
|
|
var handler: TopicHandler
|
|
|
|
closureScope:
|
|
|
|
var dialerNode = dialer
|
|
|
|
handler = proc(topic: string, data: seq[byte]) {.async, gcsafe, closure.} =
|
2019-12-07 16:36:39 +00:00
|
|
|
if dialerNode.peerInfo.id notin seen:
|
|
|
|
seen[dialerNode.peerInfo.id] = 0
|
|
|
|
seen[dialerNode.peerInfo.id].inc
|
2019-12-06 02:16:18 +00:00
|
|
|
check topic == "foobar"
|
2020-05-21 20:24:20 +00:00
|
|
|
if not seenFut.finished() and seen.len >= runs:
|
2020-01-07 08:06:27 +00:00
|
|
|
seenFut.complete()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
subs &= dialer.subscribe("foobar", handler)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(subs)
|
2020-05-23 17:14:22 +00:00
|
|
|
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await wait(nodes[0].publish("foobar",
|
|
|
|
cast[seq[byte]]("from node " &
|
|
|
|
nodes[1].peerInfo.id)),
|
2020-06-28 03:50:11 +00:00
|
|
|
1.minutes), runs, 5.seconds
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-02-25 23:05:12 +00:00
|
|
|
await wait(seenFut, 2.minutes)
|
2020-05-21 20:24:20 +00:00
|
|
|
check: seen.len >= runs
|
2019-12-06 02:16:18 +00:00
|
|
|
for k, v in seen.pairs:
|
2020-06-28 01:53:43 +00:00
|
|
|
check: v >= 1
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
|
|
|
await allFuturesThrowing(awaitters)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "e2e - GossipSub with multiple peers (sparse)":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var nodes: seq[Switch] = newSeq[Switch]()
|
|
|
|
var awaitters: seq[Future[void]]
|
|
|
|
var runs = 10
|
|
|
|
|
|
|
|
for i in 0..<runs:
|
2020-07-08 00:33:05 +00:00
|
|
|
nodes.add newStandardSwitch(triggerSelf = true,
|
|
|
|
gossip = true,
|
|
|
|
secureManagers = [SecureProtocol.Secio])
|
2020-06-02 23:53:38 +00:00
|
|
|
awaitters.add((await nodes[i].start()))
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
await subscribeSparseNodes(nodes, 1) # TODO: figure out better sparse mesh
|
2020-06-02 23:53:38 +00:00
|
|
|
|
|
|
|
var seen: Table[string, int]
|
|
|
|
var subs: seq[Future[void]]
|
|
|
|
var seenFut = newFuture[void]()
|
|
|
|
for dialer in nodes:
|
|
|
|
var handler: TopicHandler
|
|
|
|
closureScope:
|
|
|
|
var dialerNode = dialer
|
|
|
|
handler = proc(topic: string, data: seq[byte])
|
|
|
|
{.async, gcsafe, closure.} =
|
|
|
|
if dialerNode.peerInfo.id notin seen:
|
|
|
|
seen[dialerNode.peerInfo.id] = 0
|
|
|
|
seen[dialerNode.peerInfo.id].inc
|
|
|
|
check topic == "foobar"
|
|
|
|
if not seenFut.finished() and seen.len >= runs:
|
|
|
|
seenFut.complete()
|
|
|
|
|
|
|
|
subs &= dialer.subscribe("foobar", handler)
|
2020-06-26 07:24:56 +00:00
|
|
|
subs &= waitSub(nodes[0], dialer, "foobar")
|
2020-06-02 23:53:38 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subs)
|
2020-06-27 05:59:48 +00:00
|
|
|
tryPublish await wait(nodes[0].publish("foobar",
|
|
|
|
cast[seq[byte]]("from node " &
|
|
|
|
nodes[1].peerInfo.id)),
|
2020-06-28 03:50:11 +00:00
|
|
|
1.minutes), 3, 5.seconds
|
2020-06-02 23:53:38 +00:00
|
|
|
|
|
|
|
await wait(seenFut, 5.minutes)
|
|
|
|
check: seen.len >= runs
|
|
|
|
for k, v in seen.pairs:
|
2020-06-28 01:53:43 +00:00
|
|
|
check: v >= 1
|
2020-06-02 23:53:38 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
|
|
|
await allFuturesThrowing(awaitters)
|
2019-12-06 02:16:18 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
2019-12-17 05:24:03 +00:00
|
|
|
waitFor(runTests()) == true
|