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-04-21 01:24:42 +00:00
|
|
|
import unittest, sequtils, options, tables, sets
|
2019-12-06 02:16:18 +00:00
|
|
|
import chronos
|
2019-12-08 21:06:58 +00:00
|
|
|
import utils,
|
2020-04-21 01:24:42 +00:00
|
|
|
../../libp2p/[errors,
|
|
|
|
switch,
|
|
|
|
connection,
|
|
|
|
stream/bufferstream,
|
2019-12-17 05:24:03 +00:00
|
|
|
crypto/crypto,
|
|
|
|
protocols/pubsub/pubsub,
|
2020-04-21 01:24:42 +00:00
|
|
|
protocols/pubsub/floodsub,
|
2019-12-17 05:24:03 +00:00
|
|
|
protocols/pubsub/rpc/messages,
|
|
|
|
protocols/pubsub/rpc/message]
|
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
|
|
|
|
not fsub.floodsub[key].contains(receiver.peerInfo.id):
|
|
|
|
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)
|
|
|
|
nodesFut = await allFinished(
|
|
|
|
nodes[0].start(),
|
|
|
|
nodes[1].start()
|
|
|
|
)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!"))
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
result = await completionFut.wait(5.seconds)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
nodes[0].stop(),
|
|
|
|
nodes[1].stop()
|
|
|
|
)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
for fut in nodesFut:
|
|
|
|
let res = fut.read()
|
|
|
|
await allFuturesThrowing(res)
|
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()))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
await nodes[1].publish("foobar", cast[seq[byte]]("Hello!"))
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
result = await completionFut.wait(5.seconds)
|
|
|
|
|
|
|
|
await allFuturesThrowing(nodes[0].stop(), nodes[1].stop())
|
|
|
|
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()))
|
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
|
|
|
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
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!"))
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await allFuturesThrowing(handlerFut, handlerFut)
|
|
|
|
await allFuturesThrowing(nodes[0].stop(), nodes[1].stop())
|
|
|
|
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()))
|
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
|
|
|
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
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[0].publish("foobar", cast[seq[byte]]("Hello!"))
|
2020-04-21 01:24:42 +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
|
|
|
|
|
|
|
|
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()))
|
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
|
|
|
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
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
await nodes[0].publish("foo", cast[seq[byte]]("Hello!"))
|
|
|
|
await nodes[0].publish("bar", cast[seq[byte]]("Hello!"))
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await allFuturesThrowing(nodes[0].stop(), nodes[1].stop())
|
|
|
|
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[]
|
|
|
|
if counter[] == 9:
|
|
|
|
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-04-21 01:24:42 +00:00
|
|
|
nodes.add newStandardSwitch()
|
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
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
await subscribeNodes(nodes)
|
|
|
|
|
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")
|
|
|
|
await allFuturesThrowing(subs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var pubs: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
pubs &= nodes[i].publish("foobar", cast[seq[byte]]("Hello!"))
|
|
|
|
await allFuturesThrowing(pubs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await allFuturesThrowing(futs.mapIt(it[0]))
|
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
|
|
|
await allFuturesThrowing(awaitters)
|
|
|
|
|
|
|
|
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:
|
2019-12-08 21:06:58 +00:00
|
|
|
nodes.add newStandardSwitch(triggerSelf = true)
|
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
|
|
|
|
|
|
|
await subscribeNodes(nodes)
|
|
|
|
|
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")
|
|
|
|
await allFuturesThrowing(subs)
|
|
|
|
|
|
|
|
var pubs: seq[Future[void]]
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..<runs:
|
2020-04-21 01:24:42 +00:00
|
|
|
pubs &= nodes[i].publish("foobar", cast[seq[byte]]("Hello!"))
|
|
|
|
await allFuturesThrowing(pubs)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await allFuturesThrowing(futs.mapIt(it[0]))
|
|
|
|
await allFuturesThrowing(nodes.mapIt(it.stop()))
|
|
|
|
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
|