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
|
|
|
|
2022-10-26 09:07:31 +00:00
|
|
|
import hashes, random, tables, sets, sequtils
|
2022-02-21 15:04:17 +00:00
|
|
|
import chronos, stew/[byteutils, results]
|
2021-04-02 01:20:51 +00:00
|
|
|
import ../../libp2p/[builders,
|
2022-02-21 15:04:17 +00:00
|
|
|
protocols/pubsub/errors,
|
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,
|
2022-02-21 15:04:17 +00:00
|
|
|
protocols/pubsub/rpc/messages,
|
2020-08-12 00:05:49 +00:00
|
|
|
protocols/secure/secure]
|
2022-10-26 09:07:31 +00:00
|
|
|
import chronicles
|
2020-08-12 00:05:49 +00:00
|
|
|
|
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()
|
|
|
|
|
2022-07-27 11:47:50 +00:00
|
|
|
func defaultMsgIdProvider*(m: Message): Result[MessageId, ValidationResult] =
|
2022-02-21 15:04:17 +00:00
|
|
|
let mid =
|
|
|
|
if m.seqno.len > 0 and m.fromPeer.data.len > 0:
|
|
|
|
byteutils.toHex(m.seqno) & $m.fromPeer
|
|
|
|
else:
|
|
|
|
# This part is irrelevant because it's not standard,
|
|
|
|
# We use it exclusively for testing basically and users should
|
|
|
|
# implement their own logic in the case they use anonymization
|
2022-07-27 17:14:05 +00:00
|
|
|
$m.data.hash & $m.topicIds.hash
|
2022-02-21 15:04:17 +00:00
|
|
|
ok mid.toBytes()
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc generateNodes*(
|
|
|
|
num: Natural,
|
2021-12-16 10:05:20 +00:00
|
|
|
secureManagers: openArray[SecureProtocol] = [
|
2020-09-21 09:16:29 +00:00
|
|
|
SecureProtocol.Noise
|
2020-08-12 00:05:49 +00:00
|
|
|
],
|
2022-02-21 15:04:17 +00:00
|
|
|
msgIdProvider: MsgIdProvider = defaultMsgIdProvider,
|
2020-08-12 00:05:49 +00:00
|
|
|
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,
|
2022-03-14 08:39:30 +00:00
|
|
|
sendSignedPeerRecord = false,
|
2022-02-21 17:14:43 +00:00
|
|
|
unsubscribeBackoff = 1.seconds,
|
2022-05-10 08:39:43 +00:00
|
|
|
maxMessageSize: int = 1024 * 1024,
|
|
|
|
enablePX: bool = false): seq[PubSub] =
|
2020-08-12 00:05:49 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
for i in 0..<num:
|
2022-03-14 08:39:30 +00:00
|
|
|
let switch = newStandardSwitch(secureManagers = secureManagers, sendSignedPeerRecord = sendSignedPeerRecord)
|
2020-08-12 00:05:49 +00:00
|
|
|
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,
|
2022-05-10 08:39:43 +00:00
|
|
|
parameters = (var p = GossipSubParams.init(); p.floodPublish = false; p.historyLength = 20; p.historyGossip = 20; p.unsubscribeBackoff = unsubscribeBackoff; p.enablePX = enablePX; 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:
|
2021-12-16 10:05:20 +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)
|
2022-10-26 09:07:31 +00:00
|
|
|
|
|
|
|
proc waitSub*(sender, receiver: auto; key: string) {.async, gcsafe.} =
|
|
|
|
if sender == receiver:
|
|
|
|
return
|
|
|
|
let timeout = Moment.now() + 5.seconds
|
|
|
|
let fsub = GossipSub(sender)
|
|
|
|
|
|
|
|
# this is for testing purposes only
|
|
|
|
# peers can be inside `mesh` and `fanout`, not just `gossipsub`
|
|
|
|
while (not fsub.gossipsub.hasKey(key) or
|
|
|
|
not fsub.gossipsub.hasPeerId(key, receiver.peerInfo.peerId)) and
|
|
|
|
(not fsub.mesh.hasKey(key) or
|
|
|
|
not fsub.mesh.hasPeerId(key, receiver.peerInfo.peerId)) and
|
|
|
|
(not fsub.fanout.hasKey(key) or
|
|
|
|
not fsub.fanout.hasPeerId(key , receiver.peerInfo.peerId)):
|
|
|
|
trace "waitSub sleeping..."
|
|
|
|
|
|
|
|
# await
|
|
|
|
await sleepAsync(5.milliseconds)
|
|
|
|
doAssert Moment.now() < timeout, "waitSub timeout!"
|
|
|
|
|
|
|
|
proc waitSubGraph*(nodes: seq[PubSub], key: string) {.async, gcsafe.} =
|
|
|
|
let timeout = Moment.now() + 5.seconds
|
|
|
|
while true:
|
|
|
|
var
|
|
|
|
nodesMesh: Table[PeerId, seq[PeerId]]
|
|
|
|
seen: HashSet[PeerId]
|
|
|
|
for n in nodes:
|
|
|
|
nodesMesh[n.peerInfo.peerId] = toSeq(GossipSub(n).mesh.getOrDefault(key).items()).mapIt(it.peerId)
|
2023-03-08 11:30:19 +00:00
|
|
|
var ok = 0
|
|
|
|
for n in nodes:
|
|
|
|
seen.clear()
|
|
|
|
proc explore(p: PeerId) =
|
|
|
|
if p in seen: return
|
|
|
|
seen.incl(p)
|
|
|
|
for peer in nodesMesh.getOrDefault(p):
|
|
|
|
explore(peer)
|
|
|
|
explore(n.peerInfo.peerId)
|
|
|
|
if seen.len == nodes.len: ok.inc()
|
|
|
|
if ok == nodes.len: return
|
2022-10-26 09:07:31 +00:00
|
|
|
trace "waitSubGraph sleeping..."
|
|
|
|
|
|
|
|
await sleepAsync(5.milliseconds)
|
|
|
|
doAssert Moment.now() < timeout, "waitSubGraph timeout!"
|