2020-05-06 09:26:08 +00:00
|
|
|
include ../../libp2p/protocols/pubsub/gossipsub
|
|
|
|
|
2020-05-08 20:58:23 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
import options
|
2020-07-07 11:14:11 +00:00
|
|
|
import unittest, bearssl
|
2020-05-21 20:24:20 +00:00
|
|
|
import stew/byteutils
|
2020-08-12 00:05:49 +00:00
|
|
|
import ../../libp2p/standard_setup
|
2020-05-06 09:26:08 +00:00
|
|
|
import ../../libp2p/errors
|
2020-06-28 15:56:38 +00:00
|
|
|
import ../../libp2p/crypto/crypto
|
2020-05-06 09:26:08 +00:00
|
|
|
import ../../libp2p/stream/bufferstream
|
|
|
|
|
2020-05-08 20:10:06 +00:00
|
|
|
import ../helpers
|
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
type
|
|
|
|
TestGossipSub = ref object of GossipSub
|
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
proc noop(data: seq[byte]) {.async, gcsafe.} = discard
|
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc getPubSubPeer(p: TestGossipSub, peerId: PeerID): auto =
|
2020-09-22 07:05:53 +00:00
|
|
|
proc getConn(): Future[Connection] =
|
|
|
|
p.switch.dial(peerId, GossipSubCodec)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-22 07:05:53 +00:00
|
|
|
newPubSubPeer(peerId, getConn, nil, GossipSubCodec)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
proc randomPeerInfo(): PeerInfo =
|
|
|
|
PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
suite "GossipSub internal":
|
|
|
|
teardown:
|
2020-09-21 17:48:19 +00:00
|
|
|
checkTrackers()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
test "topic params":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
|
|
|
let params = TopicParams.init()
|
|
|
|
params.validateParameters().tryGet()
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
test "`rebalanceMesh` Degree Lo":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
var conns = newSeq[Connection]()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
|
2020-05-06 09:26:08 +00:00
|
|
|
for i in 0..<15:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-22 07:05:53 +00:00
|
|
|
peer.sendConn = conn
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-08-12 00:05:49 +00:00
|
|
|
gossipSub.peers[peerInfo.peerId] = peer
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.gossipsub[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
check gossipSub.peers.len == 15
|
|
|
|
await gossipSub.rebalanceMesh(topic)
|
2020-10-05 02:35:03 +00:00
|
|
|
check gossipSub.mesh[topic].len == GossipSubD # + 2 # account opportunistic grafts
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`rebalanceMesh` Degree Hi":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
var conns = newSeq[Connection]()
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
|
2020-05-06 09:26:08 +00:00
|
|
|
for i in 0..<15:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
|
|
|
gossipSub.grafted(peer, topic)
|
2020-08-12 00:05:49 +00:00
|
|
|
gossipSub.peers[peerInfo.peerId] = peer
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
check gossipSub.mesh[topic].len == 15
|
2020-05-06 09:26:08 +00:00
|
|
|
await gossipSub.rebalanceMesh(topic)
|
2020-09-21 09:16:29 +00:00
|
|
|
check gossipSub.mesh[topic].len == GossipSubD + gossipSub.parameters.dScore
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`replenishFanout` Degree Lo":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
var conns = newSeq[Connection]()
|
|
|
|
for i in 0..<15:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
var peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
|
|
|
gossipSub.gossipsub[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
check gossipSub.gossipsub[topic].len == 15
|
2020-07-08 00:33:05 +00:00
|
|
|
gossipSub.replenishFanout(topic)
|
2020-05-06 09:26:08 +00:00
|
|
|
check gossipSub.fanout[topic].len == GossipSubD
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`dropFanoutPeers` drop expired fanout topics":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.fanout[topic] = initHashSet[PubSubPeer]()
|
2020-05-21 20:24:20 +00:00
|
|
|
gossipSub.lastFanoutPubSub[topic] = Moment.fromNow(1.millis)
|
|
|
|
await sleepAsync(5.millis) # allow the topic to expire
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
var conns = newSeq[Connection]()
|
|
|
|
for i in 0..<6:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
|
|
|
gossipSub.fanout[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
check gossipSub.fanout[topic].len == GossipSubD
|
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
gossipSub.dropFanoutPeers()
|
2020-05-06 09:26:08 +00:00
|
|
|
check topic notin gossipSub.fanout
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`dropFanoutPeers` leave unexpired fanout topics":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic1 = "foobar1"
|
|
|
|
let topic2 = "foobar2"
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic1] = TopicParams.init()
|
|
|
|
gossipSub.topicParams[topic2] = TopicParams.init()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.fanout[topic1] = initHashSet[PubSubPeer]()
|
|
|
|
gossipSub.fanout[topic2] = initHashSet[PubSubPeer]()
|
2020-05-21 20:24:20 +00:00
|
|
|
gossipSub.lastFanoutPubSub[topic1] = Moment.fromNow(1.millis)
|
2020-05-15 02:01:33 +00:00
|
|
|
gossipSub.lastFanoutPubSub[topic2] = Moment.fromNow(1.minutes)
|
2020-05-21 20:24:20 +00:00
|
|
|
await sleepAsync(5.millis) # allow the topic to expire
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
var conns = newSeq[Connection]()
|
|
|
|
for i in 0..<6:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
|
|
|
gossipSub.fanout[topic1].incl(peer)
|
|
|
|
gossipSub.fanout[topic2].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
check gossipSub.fanout[topic1].len == GossipSubD
|
|
|
|
check gossipSub.fanout[topic2].len == GossipSubD
|
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
gossipSub.dropFanoutPeers()
|
2020-05-06 09:26:08 +00:00
|
|
|
check topic1 notin gossipSub.fanout
|
|
|
|
check topic2 in gossipSub.fanout
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`getGossipPeers` - should gather up to degree D non intersecting peers":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
|
|
|
|
gossipSub.fanout[topic] = initHashSet[PubSubPeer]()
|
|
|
|
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
|
2020-05-06 09:26:08 +00:00
|
|
|
var conns = newSeq[Connection]()
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
# generate mesh and fanout peers
|
2020-05-06 09:26:08 +00:00
|
|
|
for i in 0..<30:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
2020-05-06 09:26:08 +00:00
|
|
|
if i mod 2 == 0:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.fanout[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
else:
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.grafted(peer, topic)
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
# generate gossipsub (free standing) peers
|
2020-05-06 09:26:08 +00:00
|
|
|
for i in 0..<15:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
|
|
|
gossipSub.gossipsub[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
# generate messages
|
2020-07-15 03:51:33 +00:00
|
|
|
var seqno = 0'u64
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..5:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-21 20:24:20 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-21 20:24:20 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-07-15 03:51:33 +00:00
|
|
|
inc seqno
|
2020-09-23 15:56:33 +00:00
|
|
|
let msg = Message.init(some(peerInfo), ("HELLO" & $i).toBytes(), topic, some(seqno), false)
|
2020-06-28 15:56:38 +00:00
|
|
|
gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
check gossipSub.fanout[topic].len == 15
|
2020-05-21 20:24:20 +00:00
|
|
|
check gossipSub.mesh[topic].len == 15
|
2020-05-06 09:26:08 +00:00
|
|
|
check gossipSub.gossipsub[topic].len == 15
|
|
|
|
|
|
|
|
let peers = gossipSub.getGossipPeers()
|
|
|
|
check peers.len == GossipSubD
|
|
|
|
for p in peers.keys:
|
2020-08-12 00:05:49 +00:00
|
|
|
check not gossipSub.fanout.hasPeerID(topic, p.peerId)
|
|
|
|
check not gossipSub.mesh.hasPeerID(topic, p.peerId)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`getGossipPeers` - should not crash on missing topics in mesh":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.fanout[topic] = initHashSet[PubSubPeer]()
|
|
|
|
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
|
2020-05-06 09:26:08 +00:00
|
|
|
var conns = newSeq[Connection]()
|
|
|
|
for i in 0..<30:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
2020-05-06 09:26:08 +00:00
|
|
|
if i mod 2 == 0:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.fanout[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
else:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.gossipsub[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
# generate messages
|
2020-07-15 03:51:33 +00:00
|
|
|
var seqno = 0'u64
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..5:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-21 20:24:20 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-21 20:24:20 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-07-15 03:51:33 +00:00
|
|
|
inc seqno
|
2020-09-23 15:56:33 +00:00
|
|
|
let msg = Message.init(some(peerInfo), ("HELLO" & $i).toBytes(), topic, some(seqno), false)
|
2020-06-28 15:56:38 +00:00
|
|
|
gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
let peers = gossipSub.getGossipPeers()
|
|
|
|
check peers.len == GossipSubD
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
test "`getGossipPeers` - should not crash on missing topics in fanout":
|
2020-05-06 09:26:08 +00:00
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
|
|
|
|
gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]()
|
2020-05-06 09:26:08 +00:00
|
|
|
var conns = newSeq[Connection]()
|
|
|
|
for i in 0..<30:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
2020-05-06 09:26:08 +00:00
|
|
|
if i mod 2 == 0:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic].incl(peer)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.grafted(peer, topic)
|
2020-05-06 09:26:08 +00:00
|
|
|
else:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.gossipsub[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
# generate messages
|
2020-07-15 03:51:33 +00:00
|
|
|
var seqno = 0'u64
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..5:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-21 20:24:20 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-21 20:24:20 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-07-15 03:51:33 +00:00
|
|
|
inc seqno
|
2020-09-23 15:56:33 +00:00
|
|
|
let msg = Message.init(some(peerInfo), ("HELLO" & $i).toBytes(), topic, some(seqno), false)
|
2020-06-28 15:56:38 +00:00
|
|
|
gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
let peers = gossipSub.getGossipPeers()
|
|
|
|
check peers.len == GossipSubD
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|
|
|
|
|
|
|
|
test "`getGossipPeers` - should not crash on missing topics in gossip":
|
|
|
|
proc testRun(): Future[bool] {.async.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
let gossipSub = TestGossipSub.init(newStandardSwitch())
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
proc handler(peer: PubSubPeer, msg: RPCMsg) {.async.} =
|
2020-05-06 09:26:08 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
let topic = "foobar"
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.topicParams[topic] = TopicParams.init()
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic] = initHashSet[PubSubPeer]()
|
|
|
|
gossipSub.fanout[topic] = initHashSet[PubSubPeer]()
|
2020-05-06 09:26:08 +00:00
|
|
|
var conns = newSeq[Connection]()
|
|
|
|
for i in 0..<30:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-06 09:26:08 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-06 09:26:08 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-09-01 07:33:03 +00:00
|
|
|
let peer = gossipSub.getPubSubPeer(peerInfo.peerId)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.onNewPeer(peer)
|
2020-07-13 13:32:38 +00:00
|
|
|
peer.handler = handler
|
2020-05-06 09:26:08 +00:00
|
|
|
if i mod 2 == 0:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.mesh[topic].incl(peer)
|
2020-09-21 09:16:29 +00:00
|
|
|
gossipSub.grafted(peer, topic)
|
2020-05-06 09:26:08 +00:00
|
|
|
else:
|
2020-07-13 13:32:38 +00:00
|
|
|
gossipSub.fanout[topic].incl(peer)
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
# generate messages
|
2020-07-15 03:51:33 +00:00
|
|
|
var seqno = 0'u64
|
2020-05-21 20:24:20 +00:00
|
|
|
for i in 0..5:
|
2020-06-19 17:29:43 +00:00
|
|
|
let conn = newBufferStream(noop)
|
2020-05-21 20:24:20 +00:00
|
|
|
conns &= conn
|
2020-07-07 11:14:11 +00:00
|
|
|
let peerInfo = randomPeerInfo()
|
2020-05-21 20:24:20 +00:00
|
|
|
conn.peerInfo = peerInfo
|
2020-07-15 03:51:33 +00:00
|
|
|
inc seqno
|
2020-09-23 15:56:33 +00:00
|
|
|
let msg = Message.init(some(peerInfo), ("bar" & $i).toBytes(), topic, some(seqno), false)
|
2020-06-28 15:56:38 +00:00
|
|
|
gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
let peers = gossipSub.getGossipPeers()
|
|
|
|
check peers.len == 0
|
|
|
|
|
2020-06-02 23:53:38 +00:00
|
|
|
await allFuturesThrowing(conns.mapIt(it.close()))
|
2020-08-12 00:05:49 +00:00
|
|
|
await gossipSub.switch.stop()
|
2020-05-06 09:26:08 +00:00
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testRun()) == true
|