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-07-14 00:02:16 +00:00
|
|
|
import unittest, sequtils, options, tables
|
2020-06-03 02:21:11 +00:00
|
|
|
import chronos, stew/byteutils
|
2019-12-08 21:06:58 +00:00
|
|
|
import utils,
|
2020-04-21 01:24:42 +00:00
|
|
|
../../libp2p/[errors,
|
|
|
|
switch,
|
2020-06-19 17:29:43 +00:00
|
|
|
stream/connection,
|
2019-12-17 05:24:03 +00:00
|
|
|
crypto/crypto,
|
|
|
|
protocols/pubsub/pubsub,
|
2020-04-21 01:24:42 +00:00
|
|
|
protocols/pubsub/floodsub,
|
2020-07-15 19:18:55 +00:00
|
|
|
protocols/pubsub/rpc/messages,
|
|
|
|
protocols/pubsub/peertable]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
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.} =
|
|
|
|
# turn things deterministic
|
|
|
|
# this is for testing purposes only
|
|
|
|
var ceil = 15
|
|
|
|
let fsub = cast[FloodSub](sender.pubSub.get())
|
|
|
|
while not fsub.floodsub.hasKey(key) or
|
2020-07-13 13:32:38 +00:00
|
|
|
not fsub.floodsub.hasPeerID(key, receiver.peerInfo.id):
|
2020-04-21 01:24:42 +00:00
|
|
|
await sleepAsync(100.millis)
|
|
|
|
dec ceil
|
|
|
|
doAssert(ceil > 0, "waitSub timeout!")
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
suite "FloodSub":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
2020-05-08 20:10:06 +00:00
|
|
|
for tracker in testTrackers():
|
2020-05-18 13:49:49 +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-06 02:16:18 +00:00
|
|
|
test "FloodSub basic publish/subscribe A -> B":
|
2019-12-17 05:24:03 +00:00
|
|
|
proc runTests(): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check topic == "foobar"
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
let
|
|
|
|
nodes = generateNodes(2)
|
2020-06-02 23:53:38 +00:00
|
|
|
nodesFut = await allFinished(
|
2020-04-21 01:24:42 +00:00
|
|
|
nodes[0].start(),
|
|
|
|
nodes[1].start()
|
|
|
|
)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-06 02:16:18 +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-07-08 00:33:05 +00:00
|
|
|
check (await nodes[0].publish("foobar", "Hello!".toBytes())) > 0
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
result = await completionFut.wait(5.seconds)
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
2020-04-21 01:24:42 +00:00
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop()
|
|
|
|
)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(nodesFut.concat())
|
2020-07-27 19:33:51 +00:00
|
|
|
await allFuturesThrowing(subscribes)
|
2020-05-23 17:14:22 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
test "FloodSub basic publish/subscribe B -> A":
|
2019-12-17 05:24:03 +00:00
|
|
|
proc runTests(): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
var completionFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check topic == "foobar"
|
|
|
|
completionFut.complete(true)
|
|
|
|
|
|
|
|
var nodes = generateNodes(2)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await nodes[0].subscribe("foobar", handler)
|
2020-04-21 01:24:42 +00:00
|
|
|
await waitSub(nodes[1], nodes[0], "foobar")
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
check (await nodes[1].publish("foobar", "Hello!".toBytes())) > 0
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
result = await completionFut.wait(5.seconds)
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(nodes[0].stop(), nodes[1].stop())
|
2020-07-27 19:33:51 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subscribes)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(awaiters)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
check:
|
2019-12-17 05:24:03 +00:00
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "FloodSub validation should succeed":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var handlerFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check topic == "foobar"
|
|
|
|
handlerFut.complete(true)
|
|
|
|
|
|
|
|
var nodes = generateNodes(2)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2019-12-17 05:24:03 +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-17 05:24:03 +00:00
|
|
|
|
|
|
|
var validatorFut = newFuture[bool]()
|
|
|
|
proc validator(topic: string,
|
|
|
|
message: Message): Future[bool] {.async.} =
|
|
|
|
check topic == "foobar"
|
|
|
|
validatorFut.complete(true)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
nodes[1].addValidator("foobar", validator)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
check (await nodes[0].publish("foobar", "Hello!".toBytes())) > 0
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-05-23 17:14:22 +00:00
|
|
|
check (await handlerFut) == true
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop())
|
2020-07-27 19:33:51 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subscribes)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(awaiters)
|
2019-12-17 05:24:03 +00:00
|
|
|
result = true
|
2020-02-12 14:43:42 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "FloodSub validation should fail":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check false # if we get here, it should fail
|
|
|
|
|
|
|
|
var nodes = generateNodes(2)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2019-12-17 05:24:03 +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-17 05:24:03 +00:00
|
|
|
|
|
|
|
var validatorFut = newFuture[bool]()
|
|
|
|
proc validator(topic: string,
|
|
|
|
message: Message): Future[bool] {.async.} =
|
|
|
|
validatorFut.complete(true)
|
|
|
|
result = false
|
|
|
|
|
|
|
|
nodes[1].addValidator("foobar", validator)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
discard await nodes[0].publish("foobar", "Hello!".toBytes())
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop())
|
2020-07-27 19:33:51 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subscribes)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(awaiters)
|
2019-12-17 05:24:03 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
|
|
|
|
|
|
|
test "FloodSub validation one fails and one succeeds":
|
|
|
|
proc runTests(): Future[bool] {.async.} =
|
|
|
|
var handlerFut = newFuture[bool]()
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check topic == "foo"
|
|
|
|
handlerFut.complete(true)
|
|
|
|
|
|
|
|
var nodes = generateNodes(2)
|
|
|
|
var awaiters: seq[Future[void]]
|
|
|
|
awaiters.add((await nodes[0].start()))
|
|
|
|
awaiters.add((await nodes[1].start()))
|
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[1].subscribe("foo", handler)
|
2020-04-21 01:24:42 +00:00
|
|
|
await waitSub(nodes[0], nodes[1], "foo")
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[1].subscribe("bar", handler)
|
2020-04-21 01:24:42 +00:00
|
|
|
await waitSub(nodes[0], nodes[1], "bar")
|
2019-12-17 05:24:03 +00:00
|
|
|
|
|
|
|
proc validator(topic: string,
|
|
|
|
message: Message): Future[bool] {.async.} =
|
|
|
|
if topic == "foo":
|
|
|
|
result = true
|
|
|
|
else:
|
|
|
|
result = false
|
|
|
|
|
|
|
|
nodes[1].addValidator("foo", "bar", validator)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
check (await nodes[0].publish("foo", "Hello!".toBytes())) > 0
|
|
|
|
check (await nodes[0].publish("bar", "Hello!".toBytes())) > 0
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop())
|
2020-07-27 19:33:51 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subscribes)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(awaiters)
|
2019-12-17 05:24:03 +00:00
|
|
|
result = true
|
2020-02-12 14:43:42 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(runTests()) == true
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
test "FloodSub multiple peers, no self trigger":
|
2019-12-17 05:24:03 +00:00
|
|
|
proc runTests(): Future[bool] {.async.} =
|
2020-05-21 20:24:20 +00:00
|
|
|
var runs = 10
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
var futs = newSeq[(Future[void], TopicHandler, ref int)](runs)
|
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
closureScope:
|
|
|
|
var
|
|
|
|
fut = newFuture[void]()
|
|
|
|
counter = new int
|
|
|
|
futs[i] = (
|
|
|
|
fut,
|
|
|
|
(proc(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check topic == "foobar"
|
|
|
|
inc counter[]
|
2020-05-23 17:14:22 +00:00
|
|
|
if counter[] == runs - 1:
|
2020-04-21 01:24:42 +00:00
|
|
|
fut.complete()),
|
|
|
|
counter
|
|
|
|
)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var nodes: seq[Switch] = newSeq[Switch]()
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-06-20 10:56:55 +00:00
|
|
|
nodes.add newStandardSwitch(secureManagers = [SecureProtocol.Noise])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var awaitters: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
awaitters.add(await nodes[i].start())
|
2020-05-08 20:10:06 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
await nodes[i].subscribe("foobar", futs[i][1])
|
2020-02-12 14:43:42 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var subs: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
|
|
|
for y in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
if y != i:
|
|
|
|
subs &= waitSub(nodes[i], nodes[y], "foobar")
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(subs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
var pubs: seq[Future[int]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-06-03 02:21:11 +00:00
|
|
|
pubs &= nodes[i].publish("foobar", "Hello!".toBytes())
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(pubs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(futs.mapIt(it[0]))
|
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
2020-07-27 19:33:51 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subscribes)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(awaitters)
|
2020-04-21 01:24:42 +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
|
|
|
|
|
|
|
test "FloodSub multiple peers, with self trigger":
|
2019-12-17 05:24:03 +00:00
|
|
|
proc runTests(): Future[bool] {.async.} =
|
2020-05-21 20:24:20 +00:00
|
|
|
var runs = 10
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
var futs = newSeq[(Future[void], TopicHandler, ref int)](runs)
|
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
closureScope:
|
|
|
|
var
|
|
|
|
fut = newFuture[void]()
|
|
|
|
counter = new int
|
|
|
|
futs[i] = (
|
|
|
|
fut,
|
|
|
|
(proc(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
check topic == "foobar"
|
|
|
|
inc counter[]
|
2020-05-21 20:24:20 +00:00
|
|
|
if counter[] == runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
fut.complete()),
|
|
|
|
counter
|
|
|
|
)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var nodes: seq[Switch] = newSeq[Switch]()
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-06-20 10:56:55 +00:00
|
|
|
nodes.add newStandardSwitch(triggerSelf = true, secureManagers = [SecureProtocol.Secio])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
var awaitters: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
awaitters.add(await nodes[i].start())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
let subscribes = await subscribeNodes(nodes)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
await nodes[i].subscribe("foobar", futs[i][1])
|
2020-02-12 14:43:42 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var subs: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
|
|
|
for y in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
if y != i:
|
|
|
|
subs &= waitSub(nodes[i], nodes[y], "foobar")
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(subs)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
var pubs: seq[Future[int]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-06-03 02:21:11 +00:00
|
|
|
pubs &= nodes[i].publish("foobar", "Hello!".toBytes())
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(pubs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(futs.mapIt(it[0]))
|
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
2020-07-27 19:33:51 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(subscribes)
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(awaitters)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
result = true
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
check:
|
2019-12-17 05:24:03 +00:00
|
|
|
waitFor(runTests()) == true
|