2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
## Base interface for pubsub protocols
|
|
|
|
##
|
|
|
|
## You can `subscribe<#subscribe%2CPubSub%2Cstring%2CTopicHandler>`_ to a topic,
|
|
|
|
## `publish<#publish.e%2CPubSub%2Cstring%2Cseq%5Bbyte%5D>`_ something on it,
|
|
|
|
## and eventually `unsubscribe<#unsubscribe%2CPubSub%2Cstring%2CTopicHandler>`_ from it.
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
import std/[tables, sequtils, sets, strutils]
|
2022-06-16 08:08:52 +00:00
|
|
|
import chronos, chronicles, metrics
|
2023-09-22 14:45:08 +00:00
|
|
|
import chronos/ratelimit
|
2022-02-21 15:04:17 +00:00
|
|
|
import
|
|
|
|
./errors as pubsub_errors,
|
|
|
|
./pubsubpeer,
|
2021-04-18 08:08:33 +00:00
|
|
|
./rpc/[message, messages, protobuf],
|
2020-08-12 00:05:49 +00:00
|
|
|
../../switch,
|
2019-09-10 02:15:52 +00:00
|
|
|
../protocol,
|
2021-10-25 08:26:32 +00:00
|
|
|
../../crypto/crypto,
|
2020-06-19 17:29:43 +00:00
|
|
|
../../stream/connection,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../peerid,
|
2020-07-17 15:36:48 +00:00
|
|
|
../../peerinfo,
|
2021-01-08 05:21:24 +00:00
|
|
|
../../errors,
|
|
|
|
../../utility
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
import stew/results
|
|
|
|
export results
|
|
|
|
|
2023-03-06 15:36:10 +00:00
|
|
|
export tables, sets
|
2019-09-10 02:15:52 +00:00
|
|
|
export PubSubPeer
|
2020-04-30 13:22:31 +00:00
|
|
|
export PubSubObserver
|
2020-09-21 09:16:29 +00:00
|
|
|
export protocol
|
2022-02-24 16:32:20 +00:00
|
|
|
export pubsub_errors
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p pubsub"
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
const
|
|
|
|
KnownLibP2PTopics* {.strdefine.} = ""
|
|
|
|
KnownLibP2PTopicsSeq* = KnownLibP2PTopics.toLowerAscii().split(",")
|
|
|
|
|
2020-06-07 07:15:21 +00:00
|
|
|
declareGauge(libp2p_pubsub_peers, "pubsub peer instances")
|
|
|
|
declareGauge(libp2p_pubsub_topics, "pubsub subscribed topics")
|
2021-01-08 05:21:24 +00:00
|
|
|
declareCounter(libp2p_pubsub_subscriptions, "pubsub subscription operations")
|
|
|
|
declareCounter(libp2p_pubsub_unsubscriptions, "pubsub unsubscription operations")
|
|
|
|
declareGauge(
|
|
|
|
libp2p_pubsub_topic_handlers,
|
|
|
|
"pubsub subscribed topics handlers count",
|
|
|
|
labels = ["topic"],
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2020-06-07 07:41:23 +00:00
|
|
|
declareCounter(
|
|
|
|
libp2p_pubsub_validation_success, "pubsub successfully validated messages"
|
|
|
|
)
|
|
|
|
declareCounter(libp2p_pubsub_validation_failure, "pubsub failed validated messages")
|
2020-10-12 07:56:00 +00:00
|
|
|
declareCounter(libp2p_pubsub_validation_ignore, "pubsub ignore validated messages")
|
2021-01-08 05:21:24 +00:00
|
|
|
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_messages_published, "published messages", labels = ["topic"]
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_messages_rebroadcasted, "re-broadcasted messages", labels = ["topic"]
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_broadcast_subscriptions,
|
|
|
|
"pubsub broadcast subscriptions",
|
|
|
|
labels = ["topic"],
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_broadcast_unsubscriptions,
|
|
|
|
"pubsub broadcast unsubscriptions",
|
|
|
|
labels = ["topic"],
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_broadcast_messages, "pubsub broadcast messages", labels = ["topic"]
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_received_subscriptions,
|
|
|
|
"pubsub received subscriptions",
|
|
|
|
labels = ["topic"],
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_received_unsubscriptions,
|
|
|
|
"pubsub received subscriptions",
|
|
|
|
labels = ["topic"],
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_received_messages, "pubsub received messages", labels = ["topic"]
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2021-01-08 05:21:24 +00:00
|
|
|
|
|
|
|
declarePublicCounter(libp2p_pubsub_broadcast_iwant, "pubsub broadcast iwant")
|
|
|
|
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_broadcast_ihave, "pubsub broadcast ihave", labels = ["topic"]
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_broadcast_graft, "pubsub broadcast graft", labels = ["topic"]
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_broadcast_prune, "pubsub broadcast prune", labels = ["topic"]
|
|
|
|
)
|
|
|
|
|
|
|
|
declarePublicCounter(libp2p_pubsub_received_iwant, "pubsub broadcast iwant")
|
|
|
|
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_received_ihave, "pubsub broadcast ihave", labels = ["topic"]
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_received_graft, "pubsub broadcast graft", labels = ["topic"]
|
|
|
|
)
|
|
|
|
declarePublicCounter(
|
|
|
|
libp2p_pubsub_received_prune, "pubsub broadcast prune", labels = ["topic"]
|
|
|
|
)
|
2020-06-07 07:15:21 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
type
|
2021-05-21 16:27:01 +00:00
|
|
|
InitializationError* = object of LPError
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
TopicHandler* {.public.} =
|
2023-06-07 11:12:49 +00:00
|
|
|
proc(topic: string, data: seq[byte]): Future[void] {.gcsafe, raises: [].}
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
ValidatorHandler* {.public.} = proc(
|
2023-06-07 11:12:49 +00:00
|
|
|
topic: string, message: Message
|
|
|
|
): Future[ValidationResult] {.gcsafe, raises: [].}
|
2019-09-24 16:16:39 +00:00
|
|
|
|
|
|
|
TopicPair* = tuple[topic: string, handler: TopicHandler]
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
MsgIdProvider* {.public.} = proc(m: Message): Result[MessageId, ValidationResult] {.
|
|
|
|
noSideEffect, raises: [], gcsafe
|
2024-06-11 15:18:06 +00:00
|
|
|
.}
|
2021-01-13 14:49:44 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
SubscriptionValidator* {.public.} = proc(topic: string): bool {.raises: [], gcsafe.}
|
2022-07-01 18:19:57 +00:00
|
|
|
## Every time a peer send us a subscription (even to an unknown topic),
|
|
|
|
## we have to store it, which may be an attack vector.
|
|
|
|
## This callback can be used to reject topic we're not interested in
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
PubSub* {.public.} = ref object of LPProtocol
|
2020-09-22 07:05:53 +00:00
|
|
|
switch*: Switch # the switch used to dial/connect to peers
|
|
|
|
peerInfo*: PeerInfo # this peer's info
|
2021-05-06 22:43:45 +00:00
|
|
|
topics*: Table[string, seq[TopicHandler]] # the topics that _we_ are interested in
|
2022-07-01 18:19:57 +00:00
|
|
|
peers*: Table[PeerId, PubSubPeer]
|
|
|
|
#\
|
|
|
|
# Peers that we are interested to gossip with (but not necessarily
|
|
|
|
# yet connected to)
|
|
|
|
triggerSelf*: bool ## trigger own local handler on publish
|
|
|
|
verifySignature*: bool ## enable signature verification
|
|
|
|
sign*: bool ## enable message signing
|
2019-12-17 05:24:03 +00:00
|
|
|
validators*: Table[string, HashSet[ValidatorHandler]]
|
2020-09-22 07:05:53 +00:00
|
|
|
observers: ref seq[PubSubObserver] # ref as in smart_ptr
|
2022-07-01 18:19:57 +00:00
|
|
|
msgIdProvider*: MsgIdProvider ## Turn message into message id (not nil)
|
2020-07-15 03:51:33 +00:00
|
|
|
msgSeqno*: uint64
|
2022-07-01 18:19:57 +00:00
|
|
|
anonymize*: bool ## if we omit fromPeer and seqno from RPC messages we send
|
2021-01-13 14:49:44 +00:00
|
|
|
subscriptionValidator*: SubscriptionValidator
|
|
|
|
# callback used to validate subscriptions
|
2022-07-01 18:19:57 +00:00
|
|
|
topicsHigh*: int ## the maximum number of topics a peer is allowed to subscribe to
|
2023-03-06 15:36:10 +00:00
|
|
|
maxMessageSize*: int
|
|
|
|
##\
|
2021-10-25 10:58:38 +00:00
|
|
|
## the maximum raw message size we'll globally allow
|
|
|
|
## for finer tuning, check message size on topic validator
|
|
|
|
##
|
|
|
|
## sending a big message to a peer with a lower size limit can
|
|
|
|
## lead to issues, from descoring to connection drops
|
|
|
|
##
|
|
|
|
## defaults to 1mB
|
2022-06-16 08:08:52 +00:00
|
|
|
rng*: ref HmacDrbgContext
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
knownTopics*: HashSet[string]
|
|
|
|
|
2022-11-05 01:04:05 +00:00
|
|
|
method unsubscribePeer*(p: PubSub, peerId: PeerId) {.base, gcsafe.} =
|
2020-07-08 00:33:05 +00:00
|
|
|
## handle peer disconnects
|
|
|
|
##
|
|
|
|
|
2020-11-10 04:14:46 +00:00
|
|
|
debug "unsubscribing pubsub peer", peerId
|
2020-11-01 20:49:25 +00:00
|
|
|
p.peers.del(peerId)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
libp2p_pubsub_peers.set(p.peers.len.int64)
|
2020-07-17 19:46:24 +00:00
|
|
|
|
2024-03-05 15:05:21 +00:00
|
|
|
proc send*(
|
|
|
|
p: PubSub, peer: PubSubPeer, msg: RPCMsg, isHighPriority: bool
|
|
|
|
) {.raises: [].} =
|
|
|
|
## This procedure attempts to send a `msg` (of type `RPCMsg`) to the specified remote peer in the PubSub network.
|
2020-08-12 00:05:49 +00:00
|
|
|
##
|
2024-03-05 15:05:21 +00:00
|
|
|
## Parameters:
|
|
|
|
## - `p`: The `PubSub` instance.
|
|
|
|
## - `peer`: An instance of `PubSubPeer` representing the peer to whom the message should be sent.
|
|
|
|
## - `msg`: The `RPCMsg` instance that contains the message to be sent.
|
|
|
|
## - `isHighPriority`: A boolean indicating whether the message should be treated as high priority.
|
|
|
|
## High priority messages are sent immediately, while low priority messages are queued and sent only after all high
|
|
|
|
## priority messages have been sent.
|
2020-07-17 19:46:24 +00:00
|
|
|
|
2020-09-22 07:05:53 +00:00
|
|
|
trace "sending pubsub message to peer", peer, msg = shortLog(msg)
|
2024-03-05 15:05:21 +00:00
|
|
|
peer.send(msg, p.anonymize, isHighPriority)
|
2020-08-12 00:05:49 +00:00
|
|
|
|
|
|
|
proc broadcast*(
|
|
|
|
p: PubSub,
|
2021-04-18 08:08:33 +00:00
|
|
|
sendPeers: auto, # Iteratble[PubSubPeer]
|
2024-03-05 15:05:21 +00:00
|
|
|
msg: RPCMsg,
|
|
|
|
isHighPriority: bool,
|
|
|
|
) {.raises: [].} =
|
|
|
|
## This procedure attempts to send a `msg` (of type `RPCMsg`) to a specified group of peers in the PubSub network.
|
|
|
|
##
|
|
|
|
## Parameters:
|
|
|
|
## - `p`: The `PubSub` instance.
|
|
|
|
## - `sendPeers`: An iterable of `PubSubPeer` instances representing the peers to whom the message should be sent.
|
|
|
|
## - `msg`: The `RPCMsg` instance that contains the message to be broadcast.
|
|
|
|
## - `isHighPriority`: A boolean indicating whether the message should be treated as high priority.
|
|
|
|
## High priority messages are sent immediately, while low priority messages are queued and sent only after all high
|
|
|
|
## priority messages have been sent.
|
2020-08-12 00:05:49 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
let npeers = sendPeers.len.int64
|
|
|
|
for sub in msg.subscriptions:
|
|
|
|
if sub.subscribe:
|
|
|
|
if p.knownTopics.contains(sub.topic):
|
|
|
|
libp2p_pubsub_broadcast_subscriptions.inc(npeers, labelValues = [sub.topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_subscriptions.inc(npeers, labelValues = ["generic"])
|
|
|
|
else:
|
|
|
|
if p.knownTopics.contains(sub.topic):
|
|
|
|
libp2p_pubsub_broadcast_unsubscriptions.inc(npeers, labelValues = [sub.topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_unsubscriptions.inc(npeers, labelValues = ["generic"])
|
|
|
|
|
|
|
|
for smsg in msg.messages:
|
2024-03-25 11:06:34 +00:00
|
|
|
let topic = smsg.topic
|
|
|
|
if p.knownTopics.contains(topic):
|
|
|
|
libp2p_pubsub_broadcast_messages.inc(npeers, labelValues = [topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_messages.inc(npeers, labelValues = ["generic"])
|
2021-01-08 05:21:24 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.control.withValue(control):
|
|
|
|
libp2p_pubsub_broadcast_iwant.inc(npeers * control.iwant.len.int64)
|
2021-01-08 05:21:24 +00:00
|
|
|
|
|
|
|
for ihave in control.ihave:
|
2024-03-25 11:06:34 +00:00
|
|
|
if p.knownTopics.contains(ihave.topicID):
|
|
|
|
libp2p_pubsub_broadcast_ihave.inc(npeers, labelValues = [ihave.topicID])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_ihave.inc(npeers, labelValues = ["generic"])
|
|
|
|
for graft in control.graft:
|
2024-03-25 11:06:34 +00:00
|
|
|
if p.knownTopics.contains(graft.topicID):
|
|
|
|
libp2p_pubsub_broadcast_graft.inc(npeers, labelValues = [graft.topicID])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_graft.inc(npeers, labelValues = ["generic"])
|
|
|
|
for prune in control.prune:
|
2024-03-25 11:06:34 +00:00
|
|
|
if p.knownTopics.contains(prune.topicID):
|
|
|
|
libp2p_pubsub_broadcast_prune.inc(npeers, labelValues = [prune.topicID])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_prune.inc(npeers, labelValues = ["generic"])
|
|
|
|
|
2020-09-04 16:31:43 +00:00
|
|
|
trace "broadcasting messages to peers", peers = sendPeers.len, msg = shortLog(msg)
|
2021-04-18 08:08:33 +00:00
|
|
|
|
|
|
|
if anyIt(sendPeers, it.hasObservers):
|
|
|
|
for peer in sendPeers:
|
2024-03-05 15:05:21 +00:00
|
|
|
p.send(peer, msg, isHighPriority)
|
2021-04-18 08:08:33 +00:00
|
|
|
else:
|
|
|
|
# Fast path that only encodes message once
|
|
|
|
let encoded = encodeRpcMsg(msg, p.anonymize)
|
|
|
|
for peer in sendPeers:
|
2024-03-05 15:05:21 +00:00
|
|
|
asyncSpawn peer.sendEncoded(encoded, isHighPriority)
|
2020-07-17 19:46:24 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc sendSubs*(
|
2021-05-06 22:43:45 +00:00
|
|
|
p: PubSub, peer: PubSubPeer, topics: openArray[string], subscribe: bool
|
2020-09-01 07:33:03 +00:00
|
|
|
) =
|
2019-12-06 02:16:18 +00:00
|
|
|
## send subscriptions to remote peer
|
2024-03-05 15:05:21 +00:00
|
|
|
p.send(peer, RPCMsg.withSubs(topics, subscribe), isHighPriority = true)
|
2021-05-06 22:43:45 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
for topic in topics:
|
|
|
|
if subscribe:
|
|
|
|
if p.knownTopics.contains(topic):
|
|
|
|
libp2p_pubsub_broadcast_subscriptions.inc(labelValues = [topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_subscriptions.inc(labelValues = ["generic"])
|
|
|
|
else:
|
|
|
|
if p.knownTopics.contains(topic):
|
|
|
|
libp2p_pubsub_broadcast_unsubscriptions.inc(labelValues = [topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_unsubscriptions.inc(labelValues = ["generic"])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
proc updateMetrics*(p: PubSub, rpcMsg: RPCMsg) =
|
2021-02-12 03:27:26 +00:00
|
|
|
for i in 0 ..< min(rpcMsg.subscriptions.len, p.topicsHigh):
|
2021-05-06 22:43:45 +00:00
|
|
|
template sub(): untyped =
|
|
|
|
rpcMsg.subscriptions[i]
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
if sub.subscribe:
|
|
|
|
if p.knownTopics.contains(sub.topic):
|
|
|
|
libp2p_pubsub_received_subscriptions.inc(labelValues = [sub.topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_received_subscriptions.inc(labelValues = ["generic"])
|
|
|
|
else:
|
|
|
|
if p.knownTopics.contains(sub.topic):
|
|
|
|
libp2p_pubsub_received_unsubscriptions.inc(labelValues = [sub.topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_received_unsubscriptions.inc(labelValues = ["generic"])
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
for i in 0 ..< rpcMsg.messages.len():
|
2024-03-25 11:06:34 +00:00
|
|
|
let topic = rpcMsg.messages[i].topic
|
|
|
|
if p.knownTopics.contains(topic):
|
|
|
|
libp2p_pubsub_received_messages.inc(labelValues = [topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_received_messages.inc(labelValues = ["generic"])
|
2021-01-08 05:21:24 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
rpcMsg.control.withValue(control):
|
|
|
|
libp2p_pubsub_received_iwant.inc(control.iwant.len.int64)
|
2021-01-08 05:21:24 +00:00
|
|
|
for ihave in control.ihave:
|
2024-03-25 11:06:34 +00:00
|
|
|
if p.knownTopics.contains(ihave.topicID):
|
|
|
|
libp2p_pubsub_received_ihave.inc(labelValues = [ihave.topicID])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_received_ihave.inc(labelValues = ["generic"])
|
|
|
|
for graft in control.graft:
|
2024-03-25 11:06:34 +00:00
|
|
|
if p.knownTopics.contains(graft.topicID):
|
|
|
|
libp2p_pubsub_received_graft.inc(labelValues = [graft.topicID])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_received_graft.inc(labelValues = ["generic"])
|
|
|
|
for prune in control.prune:
|
2024-03-25 11:06:34 +00:00
|
|
|
if p.knownTopics.contains(prune.topicID):
|
|
|
|
libp2p_pubsub_received_prune.inc(labelValues = [prune.topicID])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_received_prune.inc(labelValues = ["generic"])
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
method rpcHandler*(
|
|
|
|
p: PubSub, peer: PubSubPeer, data: seq[byte]
|
2023-09-22 14:45:08 +00:00
|
|
|
): Future[void] {.base, async.} =
|
2021-05-06 22:43:45 +00:00
|
|
|
## Handler that must be overridden by concrete implementation
|
|
|
|
raiseAssert "Unimplemented"
|
2021-04-18 08:08:33 +00:00
|
|
|
|
2022-10-29 21:26:44 +00:00
|
|
|
method onNewPeer(p: PubSub, peer: PubSubPeer) {.base, gcsafe.} =
|
|
|
|
discard
|
2020-09-21 09:16:29 +00:00
|
|
|
|
2022-07-27 17:14:05 +00:00
|
|
|
method onPubSubPeerEvent*(
|
|
|
|
p: PubSub, peer: PubSubPeer, event: PubSubPeerEvent
|
|
|
|
) {.base, gcsafe.} =
|
2020-09-22 07:05:53 +00:00
|
|
|
# Peer event is raised for the send connection in particular
|
|
|
|
case event.kind
|
2024-03-25 21:00:11 +00:00
|
|
|
of PubSubPeerEventKind.StreamOpened:
|
2020-09-28 07:11:18 +00:00
|
|
|
if p.topics.len > 0:
|
|
|
|
p.sendSubs(peer, toSeq(p.topics.keys), true)
|
2024-03-25 21:00:11 +00:00
|
|
|
of PubSubPeerEventKind.StreamClosed:
|
2020-09-22 07:05:53 +00:00
|
|
|
discard
|
2024-03-25 21:00:11 +00:00
|
|
|
of PubSubPeerEventKind.DisconnectionRequested:
|
|
|
|
discard
|
|
|
|
|
2023-09-22 14:45:08 +00:00
|
|
|
method getOrCreatePeer*(
|
2024-06-13 10:25:48 +00:00
|
|
|
p: PubSub, peerId: PeerId, protosToDial: seq[string], protoNegotiated: string = ""
|
2023-09-22 14:45:08 +00:00
|
|
|
): PubSubPeer {.base, gcsafe.} =
|
2021-05-06 22:43:45 +00:00
|
|
|
p.peers.withValue(peerId, peer):
|
2024-06-13 10:25:48 +00:00
|
|
|
if peer[].codec == "":
|
|
|
|
peer[].codec = protoNegotiated
|
2021-05-06 22:43:45 +00:00
|
|
|
return peer[]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
proc getConn(): Future[Connection] {.async.} =
|
2024-06-13 10:25:48 +00:00
|
|
|
return await p.switch.dial(peerId, protosToDial)
|
2020-09-22 07:05:53 +00:00
|
|
|
|
2022-07-27 17:14:05 +00:00
|
|
|
proc onEvent(peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe.} =
|
2020-09-22 07:05:53 +00:00
|
|
|
p.onPubSubPeerEvent(peer, event)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
# create new pubsub peer
|
2024-06-13 10:25:48 +00:00
|
|
|
let pubSubPeer =
|
|
|
|
PubSubPeer.new(peerId, getConn, onEvent, protoNegotiated, p.maxMessageSize)
|
2021-05-06 22:43:45 +00:00
|
|
|
debug "created new pubsub peer", peerId
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
p.peers[peerId] = pubSubPeer
|
2020-08-12 00:05:49 +00:00
|
|
|
pubSubPeer.observers = p.observers
|
2020-07-13 14:15:27 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
onNewPeer(p, pubSubPeer)
|
|
|
|
|
|
|
|
# metrics
|
2020-07-13 14:15:27 +00:00
|
|
|
libp2p_pubsub_peers.set(p.peers.len.int64)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
return pubSubPeer
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
proc handleData*(p: PubSub, topic: string, data: seq[byte]): Future[void] =
|
|
|
|
# Start work on all data handlers without copying data into closure like
|
|
|
|
# happens on {.async.} transformation
|
|
|
|
p.topics.withValue(topic, handlers):
|
|
|
|
var futs = newSeq[Future[void]]()
|
|
|
|
|
|
|
|
for handler in handlers[]:
|
|
|
|
if handler != nil: # allow nil handlers
|
|
|
|
let fut = handler(topic, data)
|
|
|
|
if not fut.completed(): # Fast path for successful sync handlers
|
|
|
|
futs.add(fut)
|
|
|
|
|
|
|
|
if futs.len() > 0:
|
|
|
|
proc waiter(): Future[void] {.async.} =
|
|
|
|
# slow path - we have to wait for the handlers to complete
|
|
|
|
try:
|
|
|
|
futs = await allFinished(futs)
|
|
|
|
except CancelledError:
|
|
|
|
# propagate cancellation
|
|
|
|
for fut in futs:
|
|
|
|
if not (fut.finished):
|
|
|
|
fut.cancel()
|
|
|
|
|
|
|
|
# check for errors in futures
|
|
|
|
for fut in futs:
|
|
|
|
if fut.failed:
|
|
|
|
let err = fut.readError()
|
|
|
|
warn "Error in topic handler", msg = err.msg
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
return waiter()
|
|
|
|
|
|
|
|
# Fast path - futures finished synchronously or nobody cared about data
|
|
|
|
var res = newFuture[void]()
|
|
|
|
res.complete()
|
|
|
|
return res
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
method handleConn*(p: PubSub, conn: Connection, proto: string) {.base, async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
## handle incoming connections
|
|
|
|
##
|
|
|
|
## this proc will:
|
|
|
|
## 1) register a new PubSubPeer for the connection
|
|
|
|
## 2) register a handler with the peer;
|
|
|
|
## this handler gets called on every rpc message
|
|
|
|
## that the peer receives
|
|
|
|
## 3) ask the peer to subscribe us to every topic
|
|
|
|
## that we're interested in
|
|
|
|
##
|
|
|
|
|
2023-09-22 14:45:08 +00:00
|
|
|
proc handler(peer: PubSubPeer, data: seq[byte]): Future[void] =
|
2020-07-08 00:33:05 +00:00
|
|
|
# call pubsub rpc handler
|
2023-09-22 14:45:08 +00:00
|
|
|
p.rpcHandler(peer, data)
|
2020-05-21 15:01:36 +00:00
|
|
|
|
2024-06-13 10:25:48 +00:00
|
|
|
let peer = p.getOrCreatePeer(conn.peerId, @[], proto)
|
2020-08-21 02:50:33 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
try:
|
2020-05-21 15:01:36 +00:00
|
|
|
peer.handler = handler
|
|
|
|
await peer.handle(conn) # spawn peer read loop
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "pubsub peer handler ended", conn
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-21 15:01:36 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "exception ocurred in pubsub handle", exc = exc.msg, conn
|
2020-07-08 00:33:05 +00:00
|
|
|
finally:
|
2020-09-24 05:30:19 +00:00
|
|
|
await conn.closeWithEOF()
|
2020-04-07 15:49:43 +00:00
|
|
|
|
2022-11-05 01:04:05 +00:00
|
|
|
method subscribePeer*(p: PubSub, peer: PeerId) {.base, gcsafe.} =
|
2020-08-12 00:05:49 +00:00
|
|
|
## subscribe to remote peer to receive/send pubsub
|
|
|
|
## messages
|
|
|
|
##
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2023-01-10 12:33:14 +00:00
|
|
|
let pubSubPeer = p.getOrCreatePeer(peer, p.codecs)
|
|
|
|
pubSubPeer.connect()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
proc updateTopicMetrics(p: PubSub, topic: string) =
|
|
|
|
# metrics
|
|
|
|
libp2p_pubsub_topics.set(p.topics.len.int64)
|
2021-05-06 22:43:45 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
if p.knownTopics.contains(topic):
|
2021-05-06 22:43:45 +00:00
|
|
|
p.topics.withValue(topic, handlers):
|
|
|
|
libp2p_pubsub_topic_handlers.set(handlers[].len.int64, labelValues = [topic])
|
|
|
|
do:
|
|
|
|
libp2p_pubsub_topic_handlers.set(0, labelValues = [topic])
|
2021-01-08 05:21:24 +00:00
|
|
|
else:
|
2021-05-06 22:43:45 +00:00
|
|
|
var others: int64 = 0
|
2021-01-08 05:21:24 +00:00
|
|
|
for key, val in p.topics:
|
2021-05-06 22:43:45 +00:00
|
|
|
if key notin p.knownTopics:
|
|
|
|
others += 1
|
2021-01-08 05:21:24 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
libp2p_pubsub_topic_handlers.set(others, labelValues = ["other"])
|
2020-10-30 12:49:54 +00:00
|
|
|
|
2022-11-05 01:04:05 +00:00
|
|
|
method onTopicSubscription*(
|
|
|
|
p: PubSub, topic: string, subscribed: bool
|
|
|
|
) {.base, gcsafe.} =
|
2021-05-06 22:43:45 +00:00
|
|
|
# Called when subscribe is called the first time for a topic or unsubscribe
|
|
|
|
# removes the last handler
|
2020-10-30 12:49:54 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
# Notify others that we are no longer interested in the topic
|
|
|
|
for _, peer in p.peers:
|
2023-03-08 11:30:19 +00:00
|
|
|
# If we don't have a sendConn yet, we will
|
|
|
|
# send the full sub list when we get the sendConn,
|
|
|
|
# so no need to send it here
|
|
|
|
if peer.hasSendConn:
|
|
|
|
p.sendSubs(peer, [topic], subscribed)
|
2021-01-08 05:21:24 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
if subscribed:
|
|
|
|
libp2p_pubsub_subscriptions.inc()
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_unsubscriptions.inc()
|
2020-07-09 20:21:47 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc unsubscribe*(p: PubSub, topic: string, handler: TopicHandler) {.public.} =
|
2019-09-24 16:16:39 +00:00
|
|
|
## unsubscribe from a ``topic`` string
|
2020-07-27 19:33:51 +00:00
|
|
|
##
|
2021-05-06 22:43:45 +00:00
|
|
|
p.topics.withValue(topic, handlers):
|
|
|
|
handlers[].keepItIf(it != handler)
|
|
|
|
|
|
|
|
if handlers[].len() == 0:
|
|
|
|
p.topics.del(topic)
|
|
|
|
|
|
|
|
p.onTopicSubscription(topic, false)
|
|
|
|
|
|
|
|
p.updateTopicMetrics(topic)
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc unsubscribe*(p: PubSub, topics: openArray[TopicPair]) {.public.} =
|
2021-05-06 22:43:45 +00:00
|
|
|
## unsubscribe from a list of ``topic`` handlers
|
|
|
|
for t in topics:
|
|
|
|
p.unsubscribe(t.topic, t.handler)
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2022-11-05 01:04:05 +00:00
|
|
|
proc unsubscribeAll*(p: PubSub, topic: string) {.public, gcsafe.} =
|
2022-07-01 18:19:57 +00:00
|
|
|
## unsubscribe every `handler` from `topic`
|
2021-01-08 05:21:24 +00:00
|
|
|
if topic notin p.topics:
|
|
|
|
debug "unsubscribeAll called for an unknown topic", topic
|
|
|
|
else:
|
|
|
|
p.topics.del(topic)
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
p.onTopicSubscription(topic, false)
|
2021-01-08 05:21:24 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
p.updateTopicMetrics(topic)
|
2020-07-20 16:16:13 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc subscribe*(p: PubSub, topic: string, handler: TopicHandler) {.public.} =
|
2019-09-10 02:15:52 +00:00
|
|
|
## subscribe to a topic
|
|
|
|
##
|
|
|
|
## ``topic`` - a string topic to subscribe to
|
|
|
|
##
|
2022-07-01 18:19:57 +00:00
|
|
|
## ``handler`` - user provided proc that
|
|
|
|
## will be triggered on every
|
|
|
|
## received message
|
2019-09-28 19:55:35 +00:00
|
|
|
|
2022-01-14 18:40:30 +00:00
|
|
|
# Check that this is an allowed topic
|
|
|
|
if p.subscriptionValidator != nil and p.subscriptionValidator(topic) == false:
|
|
|
|
warn "Trying to subscribe to a topic not passing validation!", topic
|
|
|
|
return
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
p.topics.withValue(topic, handlers):
|
|
|
|
# Already subscribed, just adding another handler
|
|
|
|
handlers[].add(handler)
|
|
|
|
do:
|
|
|
|
trace "subscribing to topic", name = topic
|
|
|
|
p.topics[topic] = @[handler]
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
# Notify on first handler
|
|
|
|
p.onTopicSubscription(topic, true)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
p.updateTopicMetrics(topic)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method publish*(
|
|
|
|
p: PubSub, topic: string, data: seq[byte]
|
2022-07-01 18:19:57 +00:00
|
|
|
): Future[int] {.base, async, public.} =
|
2019-09-10 02:15:52 +00:00
|
|
|
## publish to a ``topic``
|
2022-07-01 18:19:57 +00:00
|
|
|
##
|
2020-09-04 16:31:43 +00:00
|
|
|
## The return value is the number of neighbours that we attempted to send the
|
|
|
|
## message to, excluding self. Note that this is an optimistic number of
|
|
|
|
## attempts - the number of peers that actually receive the message might
|
|
|
|
## be lower.
|
2020-09-01 07:33:03 +00:00
|
|
|
if p.triggerSelf:
|
|
|
|
await handleData(p, topic, data)
|
2019-10-03 22:22:49 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
return 0
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
method initPubSub*(p: PubSub) {.base, raises: [InitializationError].} =
|
2020-05-27 18:33:49 +00:00
|
|
|
## perform pubsub initialization
|
2020-04-30 13:22:31 +00:00
|
|
|
p.observers = new(seq[PubSubObserver])
|
2020-06-28 15:56:38 +00:00
|
|
|
if p.msgIdProvider == nil:
|
|
|
|
p.msgIdProvider = defaultMsgIdProvider
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
method addValidator*(
|
|
|
|
p: PubSub, topic: varargs[string], hook: ValidatorHandler
|
2022-11-05 01:04:05 +00:00
|
|
|
) {.base, public, gcsafe.} =
|
2022-07-01 18:19:57 +00:00
|
|
|
## Add a validator to a `topic`. Each new message received in this
|
|
|
|
## will be sent to `hook`. `hook` can return either `Accept`,
|
|
|
|
## `Ignore` or `Reject` (which can descore the peer)
|
2019-12-17 05:24:03 +00:00
|
|
|
for t in topic:
|
2024-03-25 11:06:34 +00:00
|
|
|
trace "adding validator for topic", topic = t
|
2021-05-06 22:43:45 +00:00
|
|
|
p.validators.mgetOrPut(t, HashSet[ValidatorHandler]()).incl(hook)
|
2019-12-17 05:24:03 +00:00
|
|
|
|
|
|
|
method removeValidator*(
|
|
|
|
p: PubSub, topic: varargs[string], hook: ValidatorHandler
|
2022-07-01 18:19:57 +00:00
|
|
|
) {.base, public.} =
|
2019-12-17 05:24:03 +00:00
|
|
|
for t in topic:
|
2021-05-06 22:43:45 +00:00
|
|
|
p.validators.withValue(t, validators):
|
|
|
|
validators[].excl(hook)
|
|
|
|
if validators[].len() == 0:
|
|
|
|
p.validators.del(t)
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-10-12 07:56:00 +00:00
|
|
|
method validate*(
|
|
|
|
p: PubSub, message: Message
|
|
|
|
): Future[ValidationResult] {.async, base.} =
|
|
|
|
var pending: seq[Future[ValidationResult]]
|
2019-12-17 05:24:03 +00:00
|
|
|
trace "about to validate message"
|
2024-03-25 11:06:34 +00:00
|
|
|
let topic = message.topic
|
|
|
|
trace "looking for validators on topic",
|
|
|
|
topic = topic, registered = toSeq(p.validators.keys)
|
|
|
|
if topic in p.validators:
|
|
|
|
trace "running validators for topic", topic = topic
|
|
|
|
for validator in p.validators[topic]:
|
|
|
|
pending.add(validator(topic, message))
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-10-12 07:56:00 +00:00
|
|
|
result = ValidationResult.Accept
|
2020-04-11 04:08:25 +00:00
|
|
|
let futs = await allFinished(pending)
|
2020-10-12 07:56:00 +00:00
|
|
|
for fut in futs:
|
|
|
|
if fut.failed:
|
|
|
|
result = ValidationResult.Reject
|
|
|
|
break
|
|
|
|
let res = fut.read()
|
|
|
|
if res != ValidationResult.Accept:
|
|
|
|
result = res
|
2021-04-18 08:08:33 +00:00
|
|
|
if res == ValidationResult.Reject:
|
|
|
|
break
|
2020-11-10 04:14:46 +00:00
|
|
|
|
2020-10-12 07:56:00 +00:00
|
|
|
case result
|
2020-11-10 04:14:46 +00:00
|
|
|
of ValidationResult.Accept:
|
2020-06-07 07:15:21 +00:00
|
|
|
libp2p_pubsub_validation_success.inc()
|
2020-10-12 07:56:00 +00:00
|
|
|
of ValidationResult.Reject:
|
2020-06-07 07:15:21 +00:00
|
|
|
libp2p_pubsub_validation_failure.inc()
|
2020-10-12 07:56:00 +00:00
|
|
|
of ValidationResult.Ignore:
|
|
|
|
libp2p_pubsub_validation_ignore.inc()
|
2019-12-17 05:24:03 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
proc init*[PubParams: object | bool](
|
2020-08-12 00:05:49 +00:00
|
|
|
P: typedesc[PubSub],
|
|
|
|
switch: Switch,
|
|
|
|
triggerSelf: bool = false,
|
2020-09-23 15:56:33 +00:00
|
|
|
anonymize: bool = false,
|
2020-08-12 00:05:49 +00:00
|
|
|
verifySignature: bool = true,
|
|
|
|
sign: bool = true,
|
2020-09-21 09:16:29 +00:00
|
|
|
msgIdProvider: MsgIdProvider = defaultMsgIdProvider,
|
2021-01-13 14:49:44 +00:00
|
|
|
subscriptionValidator: SubscriptionValidator = nil,
|
2021-10-25 10:58:38 +00:00
|
|
|
maxMessageSize: int = 1024 * 1024,
|
2022-06-16 08:08:52 +00:00
|
|
|
rng: ref HmacDrbgContext = newRng(),
|
2021-05-21 16:27:01 +00:00
|
|
|
parameters: PubParams = false,
|
2023-06-07 11:12:49 +00:00
|
|
|
): P {.raises: [InitializationError], public.} =
|
2020-09-21 09:16:29 +00:00
|
|
|
let pubsub =
|
|
|
|
when PubParams is bool:
|
|
|
|
P(
|
|
|
|
switch: switch,
|
|
|
|
peerInfo: switch.peerInfo,
|
|
|
|
triggerSelf: triggerSelf,
|
2020-09-23 15:56:33 +00:00
|
|
|
anonymize: anonymize,
|
2020-09-21 09:16:29 +00:00
|
|
|
verifySignature: verifySignature,
|
|
|
|
sign: sign,
|
2021-01-13 14:49:44 +00:00
|
|
|
msgIdProvider: msgIdProvider,
|
2021-02-12 03:27:26 +00:00
|
|
|
subscriptionValidator: subscriptionValidator,
|
2021-10-25 10:58:38 +00:00
|
|
|
maxMessageSize: maxMessageSize,
|
2021-10-25 08:26:32 +00:00
|
|
|
rng: rng,
|
2021-02-12 03:27:26 +00:00
|
|
|
topicsHigh: int.high,
|
|
|
|
)
|
2020-09-21 09:16:29 +00:00
|
|
|
else:
|
|
|
|
P(
|
|
|
|
switch: switch,
|
|
|
|
peerInfo: switch.peerInfo,
|
|
|
|
triggerSelf: triggerSelf,
|
2020-09-23 15:56:33 +00:00
|
|
|
anonymize: anonymize,
|
2020-09-21 09:16:29 +00:00
|
|
|
verifySignature: verifySignature,
|
|
|
|
sign: sign,
|
|
|
|
msgIdProvider: msgIdProvider,
|
2021-01-13 14:49:44 +00:00
|
|
|
subscriptionValidator: subscriptionValidator,
|
2021-02-12 03:27:26 +00:00
|
|
|
parameters: parameters,
|
2021-10-25 10:58:38 +00:00
|
|
|
maxMessageSize: maxMessageSize,
|
2021-10-25 08:26:32 +00:00
|
|
|
rng: rng,
|
2021-02-12 03:27:26 +00:00
|
|
|
topicsHigh: int.high,
|
|
|
|
)
|
2020-09-15 20:19:22 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
proc peerEventHandler(peerId: PeerId, event: PeerEvent) {.async.} =
|
2020-11-28 16:59:47 +00:00
|
|
|
if event.kind == PeerEventKind.Joined:
|
2021-09-08 09:07:46 +00:00
|
|
|
pubsub.subscribePeer(peerId)
|
2020-09-15 20:19:22 +00:00
|
|
|
else:
|
2021-09-08 09:07:46 +00:00
|
|
|
pubsub.unsubscribePeer(peerId)
|
2020-09-15 20:19:22 +00:00
|
|
|
|
2020-11-28 16:59:47 +00:00
|
|
|
switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Joined)
|
|
|
|
switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Left)
|
2020-09-15 20:19:22 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
pubsub.knownTopics = KnownLibP2PTopicsSeq.toHashSet()
|
|
|
|
|
2020-09-15 20:19:22 +00:00
|
|
|
pubsub.initPubSub()
|
2020-11-10 04:14:46 +00:00
|
|
|
|
2020-09-15 20:19:22 +00:00
|
|
|
return pubsub
|
2020-04-30 13:22:31 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc addObserver*(p: PubSub, observer: PubSubObserver) {.public.} =
|
|
|
|
p.observers[] &= observer
|
2020-04-30 13:22:31 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc removeObserver*(p: PubSub, observer: PubSubObserver) {.public.} =
|
2020-04-30 13:22:31 +00:00
|
|
|
let idx = p.observers[].find(observer)
|
|
|
|
if idx != -1:
|
|
|
|
p.observers[].del(idx)
|