2020-08-12 00:05:49 +00:00
|
|
|
# compile time options here
|
|
|
|
const
|
|
|
|
libp2p_pubsub_sign {.booldefine.} = true
|
|
|
|
libp2p_pubsub_verify {.booldefine.} = true
|
2020-09-28 07:11:18 +00:00
|
|
|
libp2p_pubsub_anonymize {.booldefine.} = false
|
2020-08-12 00:05:49 +00:00
|
|
|
|
2020-12-19 14:43:32 +00:00
|
|
|
import random, tables
|
2019-12-06 02:16:18 +00:00
|
|
|
import chronos
|
2021-04-02 01:20:51 +00:00
|
|
|
import ../../libp2p/[builders,
|
2020-08-12 00:05:49 +00:00
|
|
|
protocols/pubsub/pubsub,
|
2020-12-19 14:43:32 +00:00
|
|
|
protocols/pubsub/gossipsub,
|
2020-08-12 00:05:49 +00:00
|
|
|
protocols/pubsub/floodsub,
|
|
|
|
protocols/secure/secure]
|
|
|
|
|
2021-04-02 01:20:51 +00:00
|
|
|
export builders
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
randomize()
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc generateNodes*(
|
|
|
|
num: Natural,
|
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
2020-09-21 09:16:29 +00:00
|
|
|
SecureProtocol.Noise
|
2020-08-12 00:05:49 +00:00
|
|
|
],
|
|
|
|
msgIdProvider: MsgIdProvider = nil,
|
|
|
|
gossip: bool = false,
|
|
|
|
triggerSelf: bool = false,
|
|
|
|
verifySignature: bool = libp2p_pubsub_verify,
|
2020-09-28 07:11:18 +00:00
|
|
|
anonymize: bool = libp2p_pubsub_anonymize,
|
2021-10-25 10:58:38 +00:00
|
|
|
sign: bool = libp2p_pubsub_sign,
|
|
|
|
maxMessageSize: int = 1024 * 1024): seq[PubSub] =
|
2020-08-12 00:05:49 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
for i in 0..<num:
|
2020-08-12 00:05:49 +00:00
|
|
|
let switch = newStandardSwitch(secureManagers = secureManagers)
|
|
|
|
let pubsub = if gossip:
|
2020-12-19 14:43:32 +00:00
|
|
|
let g = GossipSub.init(
|
2020-08-12 00:05:49 +00:00
|
|
|
switch = switch,
|
|
|
|
triggerSelf = triggerSelf,
|
|
|
|
verifySignature = verifySignature,
|
|
|
|
sign = sign,
|
2020-09-21 09:16:29 +00:00
|
|
|
msgIdProvider = msgIdProvider,
|
2020-09-28 07:11:18 +00:00
|
|
|
anonymize = anonymize,
|
2021-10-25 10:58:38 +00:00
|
|
|
maxMessageSize = maxMessageSize,
|
2021-04-22 09:51:22 +00:00
|
|
|
parameters = (var p = GossipSubParams.init(); p.floodPublish = false; p.historyLength = 20; p.historyGossip = 20; p))
|
2020-12-19 14:43:32 +00:00
|
|
|
# set some testing params, to enable scores
|
|
|
|
g.topicParams.mgetOrPut("foobar", TopicParams.init()).topicWeight = 1.0
|
|
|
|
g.topicParams.mgetOrPut("foo", TopicParams.init()).topicWeight = 1.0
|
|
|
|
g.topicParams.mgetOrPut("bar", TopicParams.init()).topicWeight = 1.0
|
|
|
|
g.PubSub
|
2020-08-12 00:05:49 +00:00
|
|
|
else:
|
|
|
|
FloodSub.init(
|
|
|
|
switch = switch,
|
|
|
|
triggerSelf = triggerSelf,
|
|
|
|
verifySignature = verifySignature,
|
|
|
|
sign = sign,
|
2020-09-28 07:11:18 +00:00
|
|
|
msgIdProvider = msgIdProvider,
|
2021-10-25 10:58:38 +00:00
|
|
|
maxMessageSize = maxMessageSize,
|
2020-09-28 07:11:18 +00:00
|
|
|
anonymize = anonymize).PubSub
|
2020-08-12 00:05:49 +00:00
|
|
|
|
|
|
|
switch.mount(pubsub)
|
|
|
|
result.add(pubsub)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc subscribeNodes*(nodes: seq[PubSub]) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
for dialer in nodes:
|
|
|
|
for node in nodes:
|
2020-08-12 00:05:49 +00:00
|
|
|
if dialer.switch.peerInfo.peerId != node.switch.peerInfo.peerId:
|
|
|
|
await dialer.switch.connect(node.peerInfo.peerId, node.peerInfo.addrs)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc subscribeSparseNodes*(nodes: seq[PubSub], degree: int = 2) {.async.} =
|
2020-06-02 23:53:38 +00:00
|
|
|
if nodes.len < degree:
|
|
|
|
raise (ref CatchableError)(msg: "nodes count needs to be greater or equal to degree!")
|
|
|
|
|
|
|
|
for i, dialer in nodes:
|
|
|
|
if (i mod degree) != 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for node in nodes:
|
2020-08-12 00:05:49 +00:00
|
|
|
if dialer.switch.peerInfo.peerId != node.peerInfo.peerId:
|
|
|
|
await dialer.switch.connect(node.peerInfo.peerId, node.peerInfo.addrs)
|
2020-06-02 23:53:38 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc subscribeRandom*(nodes: seq[PubSub]) {.async.} =
|
2020-05-21 20:24:20 +00:00
|
|
|
for dialer in nodes:
|
2020-08-12 00:05:49 +00:00
|
|
|
var dialed: seq[PeerID]
|
2020-05-21 20:24:20 +00:00
|
|
|
while dialed.len < nodes.len - 1:
|
|
|
|
let node = sample(nodes)
|
2020-08-12 00:05:49 +00:00
|
|
|
if node.peerInfo.peerId notin dialed:
|
|
|
|
if dialer.peerInfo.peerId != node.peerInfo.peerId:
|
|
|
|
await dialer.switch.connect(node.peerInfo.peerId, node.peerInfo.addrs)
|
|
|
|
dialed.add(node.peerInfo.peerId)
|