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
|
2023-10-05 15:12:07 +00:00
|
|
|
import chronos, stew/[byteutils, results], chronos/ratelimit
|
2024-09-02 16:35:08 +00:00
|
|
|
import std/options
|
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,
|
2023-10-02 09:39:28 +00:00
|
|
|
protocols/pubsub/pubsubpeer,
|
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,
|
|
|
|
]
|
2023-10-02 09:39:28 +00:00
|
|
|
import ../helpers
|
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()
|
|
|
|
|
2024-09-02 16:35:08 +00:00
|
|
|
type
|
|
|
|
TestGossipSub* = ref object of GossipSub
|
|
|
|
DValues* = object
|
|
|
|
d*: Option[int]
|
|
|
|
dLow*: Option[int]
|
|
|
|
dHigh*: Option[int]
|
|
|
|
dScore*: Option[int]
|
|
|
|
dOut*: Option[int]
|
|
|
|
dLazy*: Option[int]
|
2023-10-02 09:39:28 +00:00
|
|
|
|
|
|
|
proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer =
|
|
|
|
proc getConn(): Future[Connection] =
|
2024-06-12 13:46:47 +00:00
|
|
|
p.switch.dial(peerId, GossipSubCodec_12)
|
2023-10-02 09:39:28 +00:00
|
|
|
|
2024-06-12 13:46:47 +00:00
|
|
|
let pubSubPeer = PubSubPeer.new(peerId, getConn, nil, GossipSubCodec_12, 1024 * 1024)
|
2023-10-02 09:39:28 +00:00
|
|
|
debug "created new pubsub peer", peerId
|
|
|
|
|
|
|
|
p.peers[peerId] = pubSubPeer
|
|
|
|
|
|
|
|
onNewPeer(p, pubSubPeer)
|
|
|
|
pubSubPeer
|
|
|
|
|
|
|
|
proc randomPeerId*(): PeerId =
|
|
|
|
try:
|
|
|
|
PeerId.init(PrivateKey.random(ECDSA, rng[]).get()).tryGet()
|
|
|
|
except CatchableError as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
2022-07-27 11:47:50 +00:00
|
|
|
func defaultMsgIdProvider*(m: Message): Result[MessageId, ValidationResult] =
|
2024-03-25 11:06:34 +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
|
|
|
|
$m.data.hash & $m.topic.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] = [SecureProtocol.Noise],
|
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,
|
2023-10-05 15:12:07 +00:00
|
|
|
enablePX: bool = false,
|
|
|
|
overheadRateLimit: Opt[tuple[bytes: int, interval: Duration]] =
|
|
|
|
Opt.none(tuple[bytes: int, interval: Duration]),
|
2024-06-13 10:25:48 +00:00
|
|
|
gossipSubVersion: string = "",
|
2024-09-04 16:35:28 +00:00
|
|
|
floodPublish: bool = false,
|
2024-09-02 16:35:08 +00:00
|
|
|
dValues: Option[DValues] = DValues.none(),
|
2024-09-04 20:05:16 +00:00
|
|
|
gossipFactor: Option[float] = float.none(),
|
2023-10-05 15:12:07 +00:00
|
|
|
): seq[PubSub] =
|
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,
|
2023-10-05 15:12:07 +00:00
|
|
|
parameters = (
|
|
|
|
var p = GossipSubParams.init()
|
2024-09-04 16:35:28 +00:00
|
|
|
p.floodPublish = floodPublish
|
2023-10-05 15:12:07 +00:00
|
|
|
p.historyLength = 20
|
|
|
|
p.historyGossip = 20
|
|
|
|
p.unsubscribeBackoff = unsubscribeBackoff
|
|
|
|
p.enablePX = enablePX
|
|
|
|
p.overheadRateLimit = overheadRateLimit
|
2024-09-02 16:35:08 +00:00
|
|
|
|
2024-09-04 20:05:16 +00:00
|
|
|
if gossipFactor.isSome:
|
|
|
|
p.gossipFactor = gossipFactor.get
|
|
|
|
|
2024-09-02 16:35:08 +00:00
|
|
|
if dValues.isSome:
|
|
|
|
let dValuesSome = dValues.get
|
|
|
|
|
|
|
|
if dValuesSome.d.isSome:
|
|
|
|
p.d = dValuesSome.d.get
|
|
|
|
|
|
|
|
if dValuesSome.dLow.isSome:
|
|
|
|
p.dLow = dValuesSome.dLow.get
|
|
|
|
|
|
|
|
if dValuesSome.dHigh.isSome:
|
|
|
|
p.dHigh = dValuesSome.dHigh.get
|
|
|
|
|
|
|
|
if dValuesSome.dScore.isSome:
|
|
|
|
p.dScore = dValuesSome.dScore.get
|
|
|
|
|
|
|
|
if dValuesSome.dOut.isSome:
|
|
|
|
p.dOut = dValuesSome.dOut.get
|
|
|
|
|
|
|
|
if dValuesSome.dLazy.isSome:
|
|
|
|
p.dLazy = dValuesSome.dLazy.get
|
|
|
|
|
2023-10-05 15:12:07 +00:00
|
|
|
p
|
2024-06-11 15:18:06 +00:00
|
|
|
),
|
|
|
|
)
|
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
|
2024-06-13 10:25:48 +00:00
|
|
|
if gossipSubVersion != "":
|
|
|
|
g.codecs = @[gossipSubVersion]
|
2020-12-19 14:43:32 +00:00
|
|
|
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
|
|
|
|
2024-08-26 17:36:17 +00:00
|
|
|
proc activeWait(
|
|
|
|
interval: Duration, maximum: Moment, timeoutErrorMessage = "Timeout on activeWait"
|
|
|
|
) {.async.} =
|
|
|
|
await sleepAsync(interval)
|
|
|
|
doAssert Moment.now() < maximum, timeoutErrorMessage
|
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
proc waitSub*(sender, receiver: auto, key: string) {.async.} =
|
2022-10-26 09:07:31 +00:00
|
|
|
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)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2022-10-26 09:07:31 +00:00
|
|
|
:
|
|
|
|
trace "waitSub sleeping..."
|
|
|
|
|
|
|
|
# await
|
|
|
|
await sleepAsync(5.milliseconds)
|
|
|
|
doAssert Moment.now() < timeout, "waitSub timeout!"
|
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
proc waitSubGraph*(nodes: seq[PubSub], key: string) {.async.} =
|
2022-10-26 09:07:31 +00:00
|
|
|
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)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
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!"
|
2024-08-26 17:36:17 +00:00
|
|
|
|
|
|
|
proc waitForMesh*(
|
|
|
|
sender: auto, receiver: auto, key: string, timeoutDuration = 5.seconds
|
|
|
|
) {.async.} =
|
|
|
|
if sender == receiver:
|
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
timeoutMoment = Moment.now() + timeoutDuration
|
|
|
|
gossipsubSender = GossipSub(sender)
|
|
|
|
receiverPeerId = receiver.peerInfo.peerId
|
|
|
|
|
|
|
|
while not gossipsubSender.mesh.hasPeerId(key, receiverPeerId):
|
|
|
|
trace "waitForMesh sleeping..."
|
|
|
|
await activeWait(5.milliseconds, timeoutMoment, "waitForMesh timeout!")
|