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.
|
|
|
|
|
|
|
|
## Gossip based publishing
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2023-03-06 15:36:10 +00:00
|
|
|
import std/[sets, sequtils]
|
2021-04-18 08:08:33 +00:00
|
|
|
import chronos, chronicles, metrics
|
2023-09-22 14:45:08 +00:00
|
|
|
import chronos/ratelimit
|
2020-09-04 06:10:32 +00:00
|
|
|
import ./pubsub,
|
|
|
|
./floodsub,
|
|
|
|
./pubsubpeer,
|
|
|
|
./peertable,
|
|
|
|
./mcache,
|
|
|
|
./timedcache,
|
2023-09-22 14:45:08 +00:00
|
|
|
./rpc/[messages, message, protobuf],
|
2019-12-06 02:16:18 +00:00
|
|
|
../protocol,
|
2020-06-19 17:29:43 +00:00
|
|
|
../../stream/connection,
|
2020-09-04 06:10:32 +00:00
|
|
|
../../peerinfo,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../peerid,
|
2021-01-13 14:49:44 +00:00
|
|
|
../../utility,
|
2021-01-15 04:48:03 +00:00
|
|
|
../../switch
|
2021-02-06 00:13:04 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
import stew/results
|
|
|
|
export results
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2022-05-11 08:38:43 +00:00
|
|
|
import ./gossipsub/[types, scoring, behavior], ../../utils/heartbeat
|
2021-05-21 16:27:01 +00:00
|
|
|
|
|
|
|
export types, scoring, behavior, pubsub
|
2021-02-06 00:13:04 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p gossipsub"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
declareCounter(libp2p_gossipsub_failed_publish, "number of failed publish")
|
2021-01-15 09:55:54 +00:00
|
|
|
declareCounter(libp2p_gossipsub_invalid_topic_subscription, "number of invalid topic subscriptions that happened")
|
2021-11-14 08:08:05 +00:00
|
|
|
declareCounter(libp2p_gossipsub_duplicate_during_validation, "number of duplicates received during message validation")
|
2023-09-06 16:05:59 +00:00
|
|
|
declareCounter(libp2p_gossipsub_idontwant_saved_messages, "number of duplicates avoided by idontwant")
|
|
|
|
declareCounter(libp2p_gossipsub_saved_bytes, "bytes saved by gossipsub optimizations", labels=["kind"])
|
2022-04-06 14:00:24 +00:00
|
|
|
declareCounter(libp2p_gossipsub_duplicate, "number of duplicates received")
|
|
|
|
declareCounter(libp2p_gossipsub_received, "number of messages received (deduplicated)")
|
2020-11-04 14:18:00 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
proc init*(_: type[GossipSubParams]): GossipSubParams =
|
|
|
|
GossipSubParams(
|
|
|
|
explicit: true,
|
|
|
|
pruneBackoff: 1.minutes,
|
2021-12-14 09:50:57 +00:00
|
|
|
unsubscribeBackoff: 5.seconds,
|
2020-09-21 09:16:29 +00:00
|
|
|
floodPublish: true,
|
|
|
|
gossipFactor: 0.25,
|
2020-11-19 07:48:17 +00:00
|
|
|
d: GossipSubD,
|
|
|
|
dLow: GossipSubDlo,
|
|
|
|
dHigh: GossipSubDhi,
|
|
|
|
dScore: GossipSubDlo,
|
|
|
|
dOut: GossipSubDlo - 1, # DLow - 1
|
|
|
|
dLazy: GossipSubD, # Like D
|
|
|
|
heartbeatInterval: GossipSubHeartbeatInterval,
|
|
|
|
historyLength: GossipSubHistoryLength,
|
|
|
|
historyGossip: GossipSubHistoryGossip,
|
|
|
|
fanoutTTL: GossipSubFanoutTTL,
|
2020-11-26 05:45:10 +00:00
|
|
|
seenTTL: 2.minutes,
|
2021-02-12 03:27:26 +00:00
|
|
|
gossipThreshold: -100,
|
|
|
|
publishThreshold: -1000,
|
2020-09-21 09:16:29 +00:00
|
|
|
graylistThreshold: -10000,
|
2020-10-03 00:26:45 +00:00
|
|
|
opportunisticGraftThreshold: 0,
|
2020-09-21 09:16:29 +00:00
|
|
|
decayInterval: 1.seconds,
|
|
|
|
decayToZero: 0.01,
|
2021-02-12 03:27:26 +00:00
|
|
|
retainScore: 2.minutes,
|
2020-09-21 09:16:29 +00:00
|
|
|
appSpecificWeight: 0.0,
|
|
|
|
ipColocationFactorWeight: 0.0,
|
|
|
|
ipColocationFactorThreshold: 1.0,
|
|
|
|
behaviourPenaltyWeight: -1.0,
|
|
|
|
behaviourPenaltyDecay: 0.999,
|
2022-05-10 08:39:43 +00:00
|
|
|
disconnectBadPeers: false,
|
2023-08-14 15:03:46 +00:00
|
|
|
enablePX: false,
|
2023-08-21 14:34:24 +00:00
|
|
|
bandwidthEstimatebps: 100_000_000, # 100 Mbps or 12.5 MBps
|
2023-10-05 12:51:27 +00:00
|
|
|
overheadRateLimit: Opt.none(tuple[bytes: int, interval: Duration]),
|
|
|
|
disconnectPeerAboveRateLimit: false
|
2020-09-21 09:16:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc validateParameters*(parameters: GossipSubParams): Result[void, cstring] =
|
2020-11-19 07:48:17 +00:00
|
|
|
if (parameters.dOut >= parameters.dLow) or
|
|
|
|
(parameters.dOut > (parameters.d div 2)):
|
2020-09-21 09:16:29 +00:00
|
|
|
err("gossipsub: dOut parameter error, Number of outbound connections to keep in the mesh. Must be less than D_lo and at most D/2")
|
|
|
|
elif parameters.gossipThreshold >= 0:
|
|
|
|
err("gossipsub: gossipThreshold parameter error, Must be < 0")
|
2021-12-14 09:50:57 +00:00
|
|
|
elif parameters.unsubscribeBackoff.seconds <= 0:
|
|
|
|
err("gossipsub: unsubscribeBackoff parameter error, Must be > 0 seconds")
|
2020-09-21 09:16:29 +00:00
|
|
|
elif parameters.publishThreshold >= parameters.gossipThreshold:
|
|
|
|
err("gossipsub: publishThreshold parameter error, Must be < gossipThreshold")
|
|
|
|
elif parameters.graylistThreshold >= parameters.publishThreshold:
|
|
|
|
err("gossipsub: graylistThreshold parameter error, Must be < publishThreshold")
|
|
|
|
elif parameters.acceptPXThreshold < 0:
|
|
|
|
err("gossipsub: acceptPXThreshold parameter error, Must be >= 0")
|
|
|
|
elif parameters.opportunisticGraftThreshold < 0:
|
|
|
|
err("gossipsub: opportunisticGraftThreshold parameter error, Must be >= 0")
|
|
|
|
elif parameters.decayToZero > 0.5 or parameters.decayToZero <= 0.0:
|
|
|
|
err("gossipsub: decayToZero parameter error, Should be close to 0.0")
|
|
|
|
elif parameters.appSpecificWeight < 0:
|
|
|
|
err("gossipsub: appSpecificWeight parameter error, Must be positive")
|
|
|
|
elif parameters.ipColocationFactorWeight > 0:
|
|
|
|
err("gossipsub: ipColocationFactorWeight parameter error, Must be negative or 0")
|
|
|
|
elif parameters.ipColocationFactorThreshold < 1.0:
|
|
|
|
err("gossipsub: ipColocationFactorThreshold parameter error, Must be at least 1")
|
|
|
|
elif parameters.behaviourPenaltyWeight >= 0:
|
|
|
|
err("gossipsub: behaviourPenaltyWeight parameter error, Must be negative")
|
|
|
|
elif parameters.behaviourPenaltyDecay < 0 or parameters.behaviourPenaltyDecay >= 1:
|
|
|
|
err("gossipsub: behaviourPenaltyDecay parameter error, Must be between 0 and 1")
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc validateParameters*(parameters: TopicParams): Result[void, cstring] =
|
|
|
|
if parameters.timeInMeshWeight <= 0.0 or parameters.timeInMeshWeight > 1.0:
|
|
|
|
err("gossipsub: timeInMeshWeight parameter error, Must be a small positive value")
|
|
|
|
elif parameters.timeInMeshCap <= 0.0:
|
|
|
|
err("gossipsub: timeInMeshCap parameter error, Should be a positive value")
|
|
|
|
elif parameters.firstMessageDeliveriesWeight <= 0.0:
|
|
|
|
err("gossipsub: firstMessageDeliveriesWeight parameter error, Should be a positive value")
|
|
|
|
elif parameters.meshMessageDeliveriesWeight >= 0.0:
|
|
|
|
err("gossipsub: meshMessageDeliveriesWeight parameter error, Should be a negative value")
|
|
|
|
elif parameters.meshMessageDeliveriesThreshold <= 0.0:
|
|
|
|
err("gossipsub: meshMessageDeliveriesThreshold parameter error, Should be a positive value")
|
|
|
|
elif parameters.meshMessageDeliveriesCap < parameters.meshMessageDeliveriesThreshold:
|
|
|
|
err("gossipsub: meshMessageDeliveriesCap parameter error, Should be >= meshMessageDeliveriesThreshold")
|
|
|
|
elif parameters.meshFailurePenaltyWeight >= 0.0:
|
|
|
|
err("gossipsub: meshFailurePenaltyWeight parameter error, Should be a negative value")
|
|
|
|
elif parameters.invalidMessageDeliveriesWeight >= 0.0:
|
|
|
|
err("gossipsub: invalidMessageDeliveriesWeight parameter error, Should be a negative value")
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
2020-06-12 23:54:12 +00:00
|
|
|
method init*(g: GossipSub) =
|
2019-12-17 05:24:03 +00:00
|
|
|
proc handler(conn: Connection, proto: string) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
## main protocol handler that gets triggered on every
|
|
|
|
## connection for a protocol string
|
|
|
|
## e.g. ``/floodsub/1.0.0``, etc...
|
|
|
|
##
|
2020-09-04 16:30:45 +00:00
|
|
|
try:
|
|
|
|
await g.handleConn(conn, proto)
|
|
|
|
except CancelledError:
|
|
|
|
# This is top-level procedure which will work as separate task, so it
|
|
|
|
# do not need to propogate CancelledError.
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "Unexpected cancellation in gossipsub handler", conn
|
2020-09-04 16:30:45 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "GossipSub handler leaks an error", exc = exc.msg, conn
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
g.handler = handler
|
2020-09-21 09:16:29 +00:00
|
|
|
g.codecs &= GossipSubCodec
|
|
|
|
g.codecs &= GossipSubCodec_10
|
|
|
|
|
2023-10-02 09:39:28 +00:00
|
|
|
method onNewPeer*(g: GossipSub, peer: PubSubPeer) =
|
2021-03-09 12:22:52 +00:00
|
|
|
g.withPeerStats(peer.peerId) do (stats: var PeerStats):
|
|
|
|
# Make sure stats and peer information match, even when reloading peer stats
|
|
|
|
# from a previous connection
|
2021-01-13 14:49:44 +00:00
|
|
|
peer.score = stats.score
|
|
|
|
peer.appScore = stats.appScore
|
|
|
|
peer.behaviourPenalty = stats.behaviourPenalty
|
2020-09-21 09:16:29 +00:00
|
|
|
|
2023-07-03 22:27:45 +00:00
|
|
|
# Check if the score is below the threshold and disconnect the peer if necessary
|
2023-09-22 14:45:08 +00:00
|
|
|
g.disconnectIfBadScorePeer(peer, stats.score)
|
2023-07-03 22:27:45 +00:00
|
|
|
|
2023-04-03 08:56:20 +00:00
|
|
|
peer.iHaveBudget = IHavePeerBudget
|
2023-06-21 08:40:10 +00:00
|
|
|
peer.pingBudget = PingsPeerBudget
|
2021-03-09 12:22:52 +00:00
|
|
|
|
2022-07-27 17:14:05 +00:00
|
|
|
method onPubSubPeerEvent*(p: GossipSub, peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe.} =
|
2020-09-22 07:05:53 +00:00
|
|
|
case event.kind
|
|
|
|
of PubSubPeerEventKind.Connected:
|
|
|
|
discard
|
|
|
|
of PubSubPeerEventKind.Disconnected:
|
|
|
|
# If a send connection is lost, it's better to remove peer from the mesh -
|
|
|
|
# if it gets reestablished, the peer will be readded to the mesh, and if it
|
|
|
|
# doesn't, well.. then we hope the peer is going away!
|
2020-10-02 04:09:31 +00:00
|
|
|
for topic, peers in p.mesh.mpairs():
|
|
|
|
p.pruned(peer, topic)
|
2020-09-22 07:05:53 +00:00
|
|
|
peers.excl(peer)
|
|
|
|
for _, peers in p.fanout.mpairs():
|
|
|
|
peers.excl(peer)
|
|
|
|
|
|
|
|
procCall FloodSub(p).onPubSubPeerEvent(peer, event)
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
method unsubscribePeer*(g: GossipSub, peer: PeerId) =
|
2019-12-06 02:16:18 +00:00
|
|
|
## handle peer disconnects
|
2020-08-12 00:05:49 +00:00
|
|
|
##
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "unsubscribing gossipsub peer", peer
|
2020-08-12 00:05:49 +00:00
|
|
|
let pubSubPeer = g.peers.getOrDefault(peer)
|
|
|
|
if pubSubPeer.isNil:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "no peer to unsubscribe", peer
|
2020-08-12 00:05:49 +00:00
|
|
|
return
|
2020-06-19 21:19:07 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
# remove from peer IPs collection too
|
2023-06-28 14:44:58 +00:00
|
|
|
pubSubPeer.address.withValue(address):
|
|
|
|
g.peersInIP.withValue(address, s):
|
2021-02-27 12:31:59 +00:00
|
|
|
s[].excl(pubSubPeer.peerId)
|
2021-01-15 04:48:03 +00:00
|
|
|
if s[].len == 0:
|
2023-06-28 14:44:58 +00:00
|
|
|
g.peersInIP.del(address)
|
2020-09-21 09:16:29 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
for t in toSeq(g.mesh.keys):
|
2020-12-15 01:25:22 +00:00
|
|
|
trace "pruning unsubscribing peer", pubSubPeer, score = pubSubPeer.score
|
2020-09-21 09:16:29 +00:00
|
|
|
g.pruned(pubSubPeer, t)
|
2020-08-12 00:05:49 +00:00
|
|
|
g.mesh.removePeer(t, pubSubPeer)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2022-01-15 11:47:41 +00:00
|
|
|
for t in toSeq(g.gossipsub.keys):
|
|
|
|
g.gossipsub.removePeer(t, pubSubPeer)
|
2023-09-15 15:22:02 +00:00
|
|
|
# also try to remove from direct peers table here
|
|
|
|
g.subscribedDirectPeers.removePeer(t, pubSubPeer)
|
2022-01-15 11:47:41 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
for t in toSeq(g.fanout.keys):
|
|
|
|
g.fanout.removePeer(t, pubSubPeer)
|
2020-08-06 02:12:52 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
g.peerStats.withValue(peer, stats):
|
2020-12-15 01:25:22 +00:00
|
|
|
for topic, info in stats[].topicInfos.mpairs:
|
2020-09-21 09:16:29 +00:00
|
|
|
info.firstMessageDeliveries = 0
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
procCall FloodSub(g).unsubscribePeer(peer)
|
2020-05-21 17:33:48 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
proc handleSubscribe*(g: GossipSub,
|
|
|
|
peer: PubSubPeer,
|
|
|
|
topic: string,
|
|
|
|
subscribe: bool) =
|
2020-07-16 19:26:57 +00:00
|
|
|
logScope:
|
2020-09-06 08:31:47 +00:00
|
|
|
peer
|
2020-07-16 19:26:57 +00:00
|
|
|
topic
|
2020-09-22 07:05:53 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if subscribe:
|
2021-05-06 22:43:45 +00:00
|
|
|
# this is a workaround for a race condition
|
|
|
|
# that can happen if we disconnect the peer very early
|
|
|
|
# in the future we might use this as a test case
|
|
|
|
# and eventually remove this workaround
|
|
|
|
if peer.peerId notin g.peers:
|
|
|
|
trace "ignoring unknown peer"
|
|
|
|
return
|
|
|
|
|
|
|
|
if not(isNil(g.subscriptionValidator)) and not(g.subscriptionValidator(topic)):
|
|
|
|
# this is a violation, so warn should be in order
|
|
|
|
trace "ignoring invalid topic subscription", topic, peer
|
|
|
|
libp2p_gossipsub_invalid_topic_subscription.inc()
|
|
|
|
return
|
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "peer subscribed to topic"
|
2021-01-13 14:49:44 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
# subscribe remote peer to the topic
|
2020-07-13 13:32:38 +00:00
|
|
|
discard g.gossipsub.addPeer(topic, peer)
|
2020-09-21 09:16:29 +00:00
|
|
|
if peer.peerId in g.parameters.directPeers:
|
2023-09-15 15:22:02 +00:00
|
|
|
discard g.subscribedDirectPeers.addPeer(topic, peer)
|
2019-12-10 20:50:35 +00:00
|
|
|
else:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "peer unsubscribed from topic"
|
2021-01-13 14:49:44 +00:00
|
|
|
|
2022-01-15 11:47:41 +00:00
|
|
|
if g.mesh.hasPeer(topic, peer):
|
|
|
|
#against spec
|
|
|
|
g.mesh.removePeer(topic, peer)
|
|
|
|
g.pruned(peer, topic)
|
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
# unsubscribe remote peer from the topic
|
2020-07-13 13:32:38 +00:00
|
|
|
g.gossipsub.removePeer(topic, peer)
|
2022-01-15 11:47:41 +00:00
|
|
|
|
2020-07-13 13:32:38 +00:00
|
|
|
g.fanout.removePeer(topic, peer)
|
2020-09-21 09:16:29 +00:00
|
|
|
if peer.peerId in g.parameters.directPeers:
|
2023-09-15 15:22:02 +00:00
|
|
|
g.subscribedDirectPeers.removePeer(topic, peer)
|
2020-07-09 17:16:46 +00:00
|
|
|
|
|
|
|
trace "gossip peers", peers = g.gossipsub.peers(topic), topic
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
proc handleControl(g: GossipSub, peer: PubSubPeer, control: ControlMessage) =
|
|
|
|
g.handlePrune(peer, control.prune)
|
|
|
|
|
|
|
|
var respControl: ControlMessage
|
2023-07-28 08:58:05 +00:00
|
|
|
g.handleIDontWant(peer, control.idontwant)
|
2021-05-06 22:43:45 +00:00
|
|
|
let iwant = g.handleIHave(peer, control.ihave)
|
2022-07-27 17:14:05 +00:00
|
|
|
if iwant.messageIds.len > 0:
|
2021-05-06 22:43:45 +00:00
|
|
|
respControl.iwant.add(iwant)
|
|
|
|
respControl.prune.add(g.handleGraft(peer, control.graft))
|
|
|
|
let messages = g.handleIWant(peer, control.iwant)
|
|
|
|
|
|
|
|
if
|
|
|
|
respControl.prune.len > 0 or
|
|
|
|
respControl.iwant.len > 0 or
|
|
|
|
messages.len > 0:
|
|
|
|
# iwant and prunes from here, also messages
|
|
|
|
|
|
|
|
for smsg in messages:
|
2022-07-27 17:14:05 +00:00
|
|
|
for topic in smsg.topicIds:
|
2021-05-06 22:43:45 +00:00
|
|
|
if g.knownTopics.contains(topic):
|
|
|
|
libp2p_pubsub_broadcast_messages.inc(labelValues = [topic])
|
2021-04-18 08:08:33 +00:00
|
|
|
else:
|
2021-05-06 22:43:45 +00:00
|
|
|
libp2p_pubsub_broadcast_messages.inc(labelValues = ["generic"])
|
2021-04-28 01:03:03 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
libp2p_pubsub_broadcast_iwant.inc(respControl.iwant.len.int64)
|
|
|
|
|
|
|
|
for prune in respControl.prune:
|
2022-07-27 11:47:50 +00:00
|
|
|
if g.knownTopics.contains(prune.topicId):
|
|
|
|
libp2p_pubsub_broadcast_prune.inc(labelValues = [prune.topicId])
|
2021-05-06 22:43:45 +00:00
|
|
|
else:
|
|
|
|
libp2p_pubsub_broadcast_prune.inc(labelValues = ["generic"])
|
|
|
|
|
|
|
|
trace "sending control message", msg = shortLog(respControl), peer
|
|
|
|
g.send(
|
|
|
|
peer,
|
|
|
|
RPCMsg(control: some(respControl), messages: messages))
|
2021-04-18 08:08:33 +00:00
|
|
|
|
2021-12-27 10:17:00 +00:00
|
|
|
proc validateAndRelay(g: GossipSub,
|
|
|
|
msg: Message,
|
|
|
|
msgId, msgIdSalted: MessageId,
|
|
|
|
peer: PubSubPeer) {.async.} =
|
|
|
|
try:
|
|
|
|
let validation = await g.validate(msg)
|
|
|
|
|
|
|
|
var seenPeers: HashSet[PubSubPeer]
|
|
|
|
discard g.validationSeen.pop(msgIdSalted, seenPeers)
|
|
|
|
libp2p_gossipsub_duplicate_during_validation.inc(seenPeers.len.int64)
|
2023-09-06 16:05:59 +00:00
|
|
|
libp2p_gossipsub_saved_bytes.inc((msg.data.len * seenPeers.len).int64, labelValues = ["validation_duplicate"])
|
2021-12-27 10:17:00 +00:00
|
|
|
|
|
|
|
case validation
|
|
|
|
of ValidationResult.Reject:
|
|
|
|
debug "Dropping message after validation, reason: reject",
|
|
|
|
msgId = shortLog(msgId), peer
|
2023-11-09 13:20:28 +00:00
|
|
|
await g.punishInvalidMessage(peer, msg)
|
2021-12-27 10:17:00 +00:00
|
|
|
return
|
|
|
|
of ValidationResult.Ignore:
|
|
|
|
debug "Dropping message after validation, reason: ignore",
|
|
|
|
msgId = shortLog(msgId), peer
|
|
|
|
return
|
|
|
|
of ValidationResult.Accept:
|
|
|
|
discard
|
|
|
|
|
|
|
|
# store in cache only after validation
|
|
|
|
g.mcache.put(msgId, msg)
|
|
|
|
|
2022-07-27 17:14:05 +00:00
|
|
|
g.rewardDelivered(peer, msg.topicIds, true)
|
2021-12-27 10:17:00 +00:00
|
|
|
|
|
|
|
var toSendPeers = HashSet[PubSubPeer]()
|
2022-07-27 17:14:05 +00:00
|
|
|
for t in msg.topicIds: # for every topic in the message
|
2021-12-27 10:17:00 +00:00
|
|
|
if t notin g.topics:
|
|
|
|
continue
|
|
|
|
|
|
|
|
g.floodsub.withValue(t, peers): toSendPeers.incl(peers[])
|
|
|
|
g.mesh.withValue(t, peers): toSendPeers.incl(peers[])
|
|
|
|
|
2023-09-15 15:22:02 +00:00
|
|
|
# add direct peers
|
|
|
|
toSendPeers.incl(g.subscribedDirectPeers.getOrDefault(t))
|
|
|
|
|
2021-12-27 10:17:00 +00:00
|
|
|
# Don't send it to source peer, or peers that
|
|
|
|
# sent it during validation
|
|
|
|
toSendPeers.excl(peer)
|
|
|
|
toSendPeers.excl(seenPeers)
|
|
|
|
|
2023-07-28 08:58:05 +00:00
|
|
|
# IDontWant is only worth it if the message is substantially
|
|
|
|
# bigger than the messageId
|
|
|
|
if msg.data.len > msgId.len * 10:
|
|
|
|
g.broadcast(toSendPeers, RPCMsg(control: some(ControlMessage(
|
|
|
|
idontwant: @[ControlIWant(messageIds: @[msgId])]
|
|
|
|
))))
|
|
|
|
|
|
|
|
for peer in toSendPeers:
|
|
|
|
for heDontWant in peer.heDontWants:
|
|
|
|
if msgId in heDontWant:
|
|
|
|
seenPeers.incl(peer)
|
2023-09-06 16:05:59 +00:00
|
|
|
libp2p_gossipsub_idontwant_saved_messages.inc
|
|
|
|
libp2p_gossipsub_saved_bytes.inc(msg.data.len.int64, labelValues = ["idontwant"])
|
2023-07-28 08:58:05 +00:00
|
|
|
break
|
|
|
|
toSendPeers.excl(seenPeers)
|
|
|
|
|
|
|
|
|
2021-12-27 10:17:00 +00:00
|
|
|
# In theory, if topics are the same in all messages, we could batch - we'd
|
|
|
|
# also have to be careful to only include validated messages
|
|
|
|
g.broadcast(toSendPeers, RPCMsg(messages: @[msg]))
|
2023-04-03 09:05:01 +00:00
|
|
|
trace "forwarded message to peers", peers = toSendPeers.len, msgId, peer
|
2022-07-27 17:14:05 +00:00
|
|
|
for topic in msg.topicIds:
|
2021-12-27 10:17:00 +00:00
|
|
|
if topic notin g.topics: continue
|
|
|
|
|
|
|
|
if g.knownTopics.contains(topic):
|
|
|
|
libp2p_pubsub_messages_rebroadcasted.inc(toSendPeers.len.int64, labelValues = [topic])
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_messages_rebroadcasted.inc(toSendPeers.len.int64, labelValues = ["generic"])
|
|
|
|
|
|
|
|
await handleData(g, topic, msg.data)
|
|
|
|
except CatchableError as exc:
|
|
|
|
info "validateAndRelay failed", msg=exc.msg
|
|
|
|
|
2023-09-22 14:45:08 +00:00
|
|
|
proc dataAndTopicsIdSize(msgs: seq[Message]): int =
|
|
|
|
msgs.mapIt(it.data.len + it.topicIds.mapIt(it.len).foldl(a + b, 0)).foldl(a + b, 0)
|
|
|
|
|
2023-10-05 12:51:27 +00:00
|
|
|
proc rateLimit*(g: GossipSub, peer: PubSubPeer, rpcMsgOpt: Opt[RPCMsg], msgSize: int) {.raises:[PeerRateLimitError, CatchableError], async.} =
|
2023-09-22 14:45:08 +00:00
|
|
|
# In this way we count even ignored fields by protobuf
|
|
|
|
|
|
|
|
var rmsg = rpcMsgOpt.valueOr:
|
|
|
|
peer.overheadRateLimitOpt.withValue(overheadRateLimit):
|
|
|
|
if not overheadRateLimit.tryConsume(msgSize):
|
2023-10-05 12:51:27 +00:00
|
|
|
libp2p_gossipsub_peers_rate_limit_hits.inc(labelValues = [peer.getAgent()]) # let's just measure at the beginning for test purposes.
|
|
|
|
debug "Peer sent a msg that couldn't be decoded and it's above rate limit.", peer, uselessAppBytesNum = msgSize
|
|
|
|
if g.parameters.disconnectPeerAboveRateLimit:
|
|
|
|
await g.disconnectPeer(peer)
|
|
|
|
raise newException(PeerRateLimitError, "Peer disconnected because it's above rate limit.")
|
2023-09-22 14:45:08 +00:00
|
|
|
|
|
|
|
raise newException(CatchableError, "Peer msg couldn't be decoded")
|
|
|
|
|
|
|
|
let usefulMsgBytesNum =
|
|
|
|
if g.verifySignature:
|
|
|
|
byteSize(rmsg.messages)
|
|
|
|
else:
|
|
|
|
dataAndTopicsIdSize(rmsg.messages)
|
|
|
|
|
|
|
|
var uselessAppBytesNum = msgSize - usefulMsgBytesNum
|
|
|
|
rmsg.control.withValue(control):
|
|
|
|
uselessAppBytesNum -= (byteSize(control.ihave) + byteSize(control.iwant))
|
|
|
|
|
|
|
|
peer.overheadRateLimitOpt.withValue(overheadRateLimit):
|
|
|
|
if not overheadRateLimit.tryConsume(uselessAppBytesNum):
|
2023-10-05 12:51:27 +00:00
|
|
|
libp2p_gossipsub_peers_rate_limit_hits.inc(labelValues = [peer.getAgent()]) # let's just measure at the beginning for test purposes.
|
2023-09-22 14:45:08 +00:00
|
|
|
debug "Peer sent too much useless application data and it's above rate limit.", peer, msgSize, uselessAppBytesNum, rmsg
|
2023-10-05 12:51:27 +00:00
|
|
|
if g.parameters.disconnectPeerAboveRateLimit:
|
|
|
|
await g.disconnectPeer(peer)
|
|
|
|
raise newException(PeerRateLimitError, "Peer disconnected because it's above rate limit.")
|
2023-09-22 14:45:08 +00:00
|
|
|
|
2020-06-12 23:54:12 +00:00
|
|
|
method rpcHandler*(g: GossipSub,
|
2019-12-06 02:16:18 +00:00
|
|
|
peer: PubSubPeer,
|
2023-09-22 14:45:08 +00:00
|
|
|
data: seq[byte]) {.async.} =
|
|
|
|
|
|
|
|
let msgSize = data.len
|
|
|
|
var rpcMsg = decodeRpcMsg(data).valueOr:
|
|
|
|
debug "failed to decode msg from peer", peer, err = error
|
2023-10-05 12:51:27 +00:00
|
|
|
await rateLimit(g, peer, Opt.none(RPCMsg), msgSize)
|
2023-09-22 14:45:08 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
trace "decoded msg from peer", peer, msg = rpcMsg.shortLog
|
2023-10-05 12:51:27 +00:00
|
|
|
await rateLimit(g, peer, Opt.some(rpcMsg), msgSize)
|
2023-09-22 14:45:08 +00:00
|
|
|
|
|
|
|
# trigger hooks
|
|
|
|
peer.recvObservers(rpcMsg)
|
|
|
|
|
2023-06-21 08:40:10 +00:00
|
|
|
if rpcMsg.ping.len in 1..<64 and peer.pingBudget > 0:
|
|
|
|
g.send(peer, RPCMsg(pong: rpcMsg.ping))
|
|
|
|
peer.pingBudget.dec
|
2021-05-06 22:43:45 +00:00
|
|
|
for i in 0..<min(g.topicsHigh, rpcMsg.subscriptions.len):
|
|
|
|
template sub: untyped = rpcMsg.subscriptions[i]
|
|
|
|
g.handleSubscribe(peer, sub.topic, sub.subscribe)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2022-10-26 09:07:31 +00:00
|
|
|
# the above call applied limits to subs number
|
2021-02-12 03:27:26 +00:00
|
|
|
# in gossipsub we want to apply scoring as well
|
|
|
|
if rpcMsg.subscriptions.len > g.topicsHigh:
|
|
|
|
debug "received an rpc message with an oversized amount of subscriptions", peer,
|
|
|
|
size = rpcMsg.subscriptions.len,
|
|
|
|
limit = g.topicsHigh
|
|
|
|
peer.behaviourPenalty += 0.1
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
for i in 0..<rpcMsg.messages.len(): # for every message
|
|
|
|
template msg: untyped = rpcMsg.messages[i]
|
2022-02-21 15:04:17 +00:00
|
|
|
let msgIdResult = g.msgIdProvider(msg)
|
|
|
|
|
|
|
|
if msgIdResult.isErr:
|
|
|
|
debug "Dropping message due to failed message id generation",
|
|
|
|
error = msgIdResult.error
|
|
|
|
# TODO: descore peers due to error during message validation (malicious?)
|
|
|
|
continue
|
|
|
|
|
2021-11-14 08:08:05 +00:00
|
|
|
let
|
2022-02-21 15:04:17 +00:00
|
|
|
msgId = msgIdResult.get
|
2021-11-14 08:08:05 +00:00
|
|
|
msgIdSalted = msgId & g.seenSalt
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
# addSeen adds salt to msgId to avoid
|
|
|
|
# remote attacking the hash function
|
2021-04-18 08:08:33 +00:00
|
|
|
if g.addSeen(msgId):
|
2020-12-18 15:14:07 +00:00
|
|
|
trace "Dropping already-seen message", msgId = shortLog(msgId), peer
|
2020-12-15 21:46:03 +00:00
|
|
|
|
2022-05-11 08:38:43 +00:00
|
|
|
var alreadyReceived = false
|
|
|
|
g.validationSeen.withValue(msgIdSalted, seen):
|
|
|
|
if seen[].containsOrIncl(peer):
|
|
|
|
# peer sent us this message twice
|
|
|
|
alreadyReceived = true
|
|
|
|
|
|
|
|
if not alreadyReceived:
|
|
|
|
let delay = Moment.now() - g.firstSeen(msgId)
|
2022-07-27 17:14:05 +00:00
|
|
|
g.rewardDelivered(peer, msg.topicIds, false, delay)
|
2021-11-14 08:08:05 +00:00
|
|
|
|
2022-04-06 14:00:24 +00:00
|
|
|
libp2p_gossipsub_duplicate.inc()
|
|
|
|
|
2020-12-15 01:25:22 +00:00
|
|
|
# onto the next message
|
2020-09-04 06:10:32 +00:00
|
|
|
continue
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2022-04-06 14:00:24 +00:00
|
|
|
libp2p_gossipsub_received.inc()
|
|
|
|
|
2021-01-13 14:49:44 +00:00
|
|
|
# avoid processing messages we are not interested in
|
2022-07-27 17:14:05 +00:00
|
|
|
if msg.topicIds.allIt(it notin g.topics):
|
2021-01-13 14:49:44 +00:00
|
|
|
debug "Dropping message of topic without subscription", msgId = shortLog(msgId), peer
|
|
|
|
continue
|
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
if (msg.signature.len > 0 or g.verifySignature) and not msg.verify():
|
|
|
|
# always validate if signature is present or required
|
2020-12-18 15:14:07 +00:00
|
|
|
debug "Dropping message due to failed signature verification",
|
|
|
|
msgId = shortLog(msgId), peer
|
2023-11-09 13:20:28 +00:00
|
|
|
await g.punishInvalidMessage(peer, msg)
|
2020-09-04 06:10:32 +00:00
|
|
|
continue
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
if msg.seqno.len > 0 and msg.seqno.len != 8:
|
|
|
|
# if we have seqno should be 8 bytes long
|
2020-12-18 15:14:07 +00:00
|
|
|
debug "Dropping message due to invalid seqno length",
|
|
|
|
msgId = shortLog(msgId), peer
|
2023-11-09 13:20:28 +00:00
|
|
|
await g.punishInvalidMessage(peer, msg)
|
2020-09-23 15:56:33 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# g.anonymize needs no evaluation when receiving messages
|
|
|
|
# as we have a "lax" policy and allow signed messages
|
|
|
|
|
2021-11-14 08:08:05 +00:00
|
|
|
# Be careful not to fill the validationSeen table
|
|
|
|
# (eg, pop everything you put in it)
|
|
|
|
g.validationSeen[msgIdSalted] = initHashSet[PubSubPeer]()
|
|
|
|
|
2021-12-27 10:17:00 +00:00
|
|
|
asyncSpawn g.validateAndRelay(msg, msgId, msgIdSalted, peer)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
if rpcMsg.control.isSome():
|
|
|
|
g.handleControl(peer, rpcMsg.control.unsafeGet())
|
2020-09-22 07:05:53 +00:00
|
|
|
|
2022-09-02 08:24:54 +00:00
|
|
|
# Now, check subscription to update the meshes if required
|
|
|
|
for i in 0..<min(g.topicsHigh, rpcMsg.subscriptions.len):
|
|
|
|
let topic = rpcMsg.subscriptions[i].topic
|
|
|
|
if topic in g.topics and g.mesh.peers(topic) < g.parameters.dLow:
|
|
|
|
# rebalance but don't update metrics here, we do that only in the heartbeat
|
|
|
|
g.rebalanceMesh(topic, metrics = nil)
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
g.updateMetrics(rpcMsg)
|
2020-09-22 07:05:53 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
method onTopicSubscription*(g: GossipSub, topic: string, subscribed: bool) =
|
|
|
|
if subscribed:
|
|
|
|
procCall PubSub(g).onTopicSubscription(topic, subscribed)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
# if we have a fanout on this topic break it
|
|
|
|
if topic in g.fanout:
|
|
|
|
g.fanout.del(topic)
|
2020-07-20 16:16:13 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
# rebalance but don't update metrics here, we do that only in the heartbeat
|
|
|
|
g.rebalanceMesh(topic, metrics = nil)
|
|
|
|
else:
|
2020-12-19 14:43:32 +00:00
|
|
|
let mpeers = g.mesh.getOrDefault(topic)
|
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
# Remove peers from the mesh since we're no longer both interested
|
|
|
|
# in the topic
|
|
|
|
let msg = RPCMsg(control: some(ControlMessage(
|
|
|
|
prune: @[ControlPrune(
|
|
|
|
topicID: topic,
|
|
|
|
peers: g.peerExchangeList(topic),
|
2021-12-14 09:50:57 +00:00
|
|
|
backoff: g.parameters.unsubscribeBackoff.seconds.uint64)])))
|
2021-05-06 22:43:45 +00:00
|
|
|
g.broadcast(mpeers, msg)
|
2020-12-19 14:43:32 +00:00
|
|
|
|
|
|
|
for peer in mpeers:
|
2021-12-14 09:50:57 +00:00
|
|
|
g.pruned(peer, topic, backoff = some(g.parameters.unsubscribeBackoff))
|
2020-12-19 14:43:32 +00:00
|
|
|
|
2020-12-19 15:45:34 +00:00
|
|
|
g.mesh.del(topic)
|
|
|
|
|
2020-12-19 14:43:32 +00:00
|
|
|
|
2021-05-06 22:43:45 +00:00
|
|
|
# Send unsubscribe (in reverse order to sub/graft)
|
|
|
|
procCall PubSub(g).onTopicSubscription(topic, subscribed)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
method publish*(g: GossipSub,
|
|
|
|
topic: string,
|
2020-09-01 07:33:03 +00:00
|
|
|
data: seq[byte]): Future[int] {.async.} =
|
2020-07-08 00:33:05 +00:00
|
|
|
# base returns always 0
|
2020-09-01 07:33:03 +00:00
|
|
|
discard await procCall PubSub(g).publish(topic, data)
|
2020-09-04 06:10:32 +00:00
|
|
|
|
2021-01-13 14:49:44 +00:00
|
|
|
logScope:
|
|
|
|
topic
|
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
trace "Publishing message on topic", data = data.shortLog
|
2021-05-24 17:55:33 +00:00
|
|
|
|
|
|
|
if topic.len <= 0: # data could be 0/empty
|
2020-09-04 06:10:32 +00:00
|
|
|
debug "Empty topic, skipping publish"
|
2020-07-08 00:33:05 +00:00
|
|
|
return 0
|
2020-09-22 07:05:53 +00:00
|
|
|
|
2021-05-24 17:55:33 +00:00
|
|
|
var peers: HashSet[PubSubPeer]
|
|
|
|
|
2023-07-31 09:13:51 +00:00
|
|
|
# add always direct peers
|
2023-09-15 15:22:02 +00:00
|
|
|
peers.incl(g.subscribedDirectPeers.getOrDefault(topic))
|
2023-07-31 09:13:51 +00:00
|
|
|
|
|
|
|
if topic in g.topics: # if we're subscribed use the mesh
|
|
|
|
peers.incl(g.mesh.getOrDefault(topic))
|
|
|
|
|
2021-05-24 17:55:33 +00:00
|
|
|
if g.parameters.floodPublish:
|
|
|
|
# With flood publishing enabled, the mesh is used when propagating messages from other peers,
|
2023-07-31 09:13:51 +00:00
|
|
|
# but a peer's own messages will always be published to all known peers in the topic, limited
|
|
|
|
# to the amount of peers we can send it to in one heartbeat
|
2023-08-14 15:03:46 +00:00
|
|
|
var maxPeersToFlodOpt: Opt[int64]
|
|
|
|
if g.parameters.bandwidthEstimatebps > 0:
|
|
|
|
let
|
|
|
|
bandwidth = (g.parameters.bandwidthEstimatebps) div 8 div 1000 # Divisions are to convert it to Bytes per ms TODO replace with bandwidth estimate
|
|
|
|
msToTransmit = max(data.len div bandwidth, 1)
|
|
|
|
maxPeersToFlodOpt = Opt.some(max(g.parameters.heartbeatInterval.milliseconds div msToTransmit, g.parameters.dLow))
|
2023-07-31 09:13:51 +00:00
|
|
|
|
2021-05-24 17:55:33 +00:00
|
|
|
for peer in g.gossipsub.getOrDefault(topic):
|
2023-08-14 15:03:46 +00:00
|
|
|
maxPeersToFlodOpt.withValue(maxPeersToFlod):
|
|
|
|
if peers.len >= maxPeersToFlod: break
|
2020-09-21 09:16:29 +00:00
|
|
|
if peer.score >= g.parameters.publishThreshold:
|
2021-05-24 17:55:33 +00:00
|
|
|
trace "publish: including flood/high score peer", peer
|
|
|
|
peers.incl(peer)
|
|
|
|
|
2023-07-31 09:13:51 +00:00
|
|
|
if peers.len < g.parameters.dLow:
|
|
|
|
# not subscribed, or bad mesh, send to fanout peers
|
2022-02-21 15:22:08 +00:00
|
|
|
var fanoutPeers = g.fanout.getOrDefault(topic).toSeq()
|
2023-07-31 09:13:51 +00:00
|
|
|
if fanoutPeers.len < g.parameters.dLow:
|
2020-07-08 00:33:05 +00:00
|
|
|
g.replenishFanout(topic)
|
2022-02-21 15:22:08 +00:00
|
|
|
fanoutPeers = g.fanout.getOrDefault(topic).toSeq()
|
|
|
|
|
2022-02-21 16:00:18 +00:00
|
|
|
g.rng.shuffle(fanoutPeers)
|
2022-06-30 07:56:49 +00:00
|
|
|
|
2022-02-21 15:22:08 +00:00
|
|
|
for fanPeer in fanoutPeers:
|
|
|
|
peers.incl(fanPeer)
|
|
|
|
if peers.len > g.parameters.d: break
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 20:21:47 +00:00
|
|
|
# even if we couldn't publish,
|
|
|
|
# we still attempted to publish
|
|
|
|
# on the topic, so it makes sense
|
|
|
|
# to update the last topic publish
|
|
|
|
# time
|
2020-11-19 07:48:17 +00:00
|
|
|
g.lastFanoutPubSub[topic] = Moment.fromNow(g.parameters.fanoutTTL)
|
2020-07-09 20:21:47 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
if peers.len == 0:
|
2021-02-09 09:42:59 +00:00
|
|
|
let topicPeers = g.gossipsub.getOrDefault(topic).toSeq()
|
2022-06-30 07:57:07 +00:00
|
|
|
debug "No peers for topic, skipping publish", peersOnTopic = topicPeers.len,
|
|
|
|
connectedPeers = topicPeers.filterIt(it.connected).len,
|
|
|
|
topic
|
2021-01-08 05:21:24 +00:00
|
|
|
libp2p_gossipsub_failed_publish.inc()
|
2020-09-04 06:10:32 +00:00
|
|
|
return 0
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
let
|
2020-09-23 15:56:33 +00:00
|
|
|
msg =
|
|
|
|
if g.anonymize:
|
|
|
|
Message.init(none(PeerInfo), data, topic, none(uint64), false)
|
|
|
|
else:
|
2022-06-30 07:56:49 +00:00
|
|
|
inc g.msgSeqno
|
2020-09-23 15:56:33 +00:00
|
|
|
Message.init(some(g.peerInfo), data, topic, some(g.msgSeqno), g.sign)
|
2022-06-30 07:56:49 +00:00
|
|
|
msgId = g.msgIdProvider(msg).valueOr:
|
|
|
|
trace "Error generating message id, skipping publish",
|
|
|
|
error = error
|
|
|
|
libp2p_gossipsub_failed_publish.inc()
|
|
|
|
return 0
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-12-18 15:14:07 +00:00
|
|
|
logScope: msgId = shortLog(msgId)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
trace "Created new message", msg = shortLog(msg), peers = peers.len
|
2020-06-17 04:14:02 +00:00
|
|
|
|
2021-04-18 08:08:33 +00:00
|
|
|
if g.addSeen(msgId):
|
2020-09-04 06:10:32 +00:00
|
|
|
# custom msgid providers might cause this
|
|
|
|
trace "Dropping already-seen message"
|
2020-08-15 19:50:31 +00:00
|
|
|
return 0
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
g.mcache.put(msgId, msg)
|
|
|
|
|
2021-04-18 08:08:33 +00:00
|
|
|
g.broadcast(peers, RPCMsg(messages: @[msg]))
|
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
if g.knownTopics.contains(topic):
|
2021-04-18 08:08:33 +00:00
|
|
|
libp2p_pubsub_messages_published.inc(peers.len.int64, labelValues = [topic])
|
2020-11-26 07:20:34 +00:00
|
|
|
else:
|
2021-04-18 08:08:33 +00:00
|
|
|
libp2p_pubsub_messages_published.inc(peers.len.int64, labelValues = ["generic"])
|
2020-09-04 06:10:32 +00:00
|
|
|
|
2021-04-22 09:51:22 +00:00
|
|
|
trace "Published message to peers", peers=peers.len
|
2020-09-04 06:10:32 +00:00
|
|
|
return peers.len
|
|
|
|
|
2022-06-30 09:21:33 +00:00
|
|
|
proc maintainDirectPeer(g: GossipSub, id: PeerId, addrs: seq[MultiAddress]) {.async.} =
|
2023-09-15 15:22:02 +00:00
|
|
|
if id notin g.peers:
|
2022-06-30 09:21:33 +00:00
|
|
|
trace "Attempting to dial a direct peer", peer = id
|
2023-09-15 15:22:02 +00:00
|
|
|
if g.switch.isConnected(id):
|
|
|
|
warn "We are connected to a direct peer, but it isn't a GossipSub peer!", id
|
|
|
|
return
|
2022-06-30 09:21:33 +00:00
|
|
|
try:
|
2023-09-15 15:22:02 +00:00
|
|
|
await g.switch.connect(id, addrs, forceDial = true)
|
2022-06-30 09:21:33 +00:00
|
|
|
# populate the peer after it's connected
|
|
|
|
discard g.getOrCreatePeer(id, g.codecs)
|
|
|
|
except CancelledError as exc:
|
|
|
|
trace "Direct peer dial canceled"
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
debug "Direct peer error dialing", msg = exc.msg
|
|
|
|
|
|
|
|
proc addDirectPeer*(g: GossipSub, id: PeerId, addrs: seq[MultiAddress]) {.async.} =
|
|
|
|
g.parameters.directPeers[id] = addrs
|
|
|
|
await g.maintainDirectPeer(id, addrs)
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
proc maintainDirectPeers(g: GossipSub) {.async.} =
|
2022-05-11 08:38:43 +00:00
|
|
|
heartbeat "GossipSub DirectPeers", 1.minutes:
|
2021-01-15 04:48:03 +00:00
|
|
|
for id, addrs in g.parameters.directPeers:
|
2022-06-30 09:21:33 +00:00
|
|
|
await g.addDirectPeer(id, addrs)
|
2020-09-21 09:16:29 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method start*(g: GossipSub) {.async.} =
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "gossipsub start"
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2020-09-04 16:31:43 +00:00
|
|
|
if not g.heartbeatFut.isNil:
|
|
|
|
warn "Starting gossipsub twice"
|
|
|
|
return
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-20 10:56:55 +00:00
|
|
|
g.heartbeatFut = g.heartbeat()
|
2022-05-11 08:38:43 +00:00
|
|
|
g.scoringHeartbeatFut = g.scoringHeartbeat()
|
2020-09-21 09:16:29 +00:00
|
|
|
g.directPeersLoop = g.maintainDirectPeers()
|
2022-06-30 09:21:33 +00:00
|
|
|
g.started = true
|
2020-06-20 10:56:55 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method stop*(g: GossipSub) {.async.} =
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "gossipsub stop"
|
2022-06-30 09:21:33 +00:00
|
|
|
g.started = false
|
2020-09-04 16:31:43 +00:00
|
|
|
if g.heartbeatFut.isNil:
|
|
|
|
warn "Stopping gossipsub without starting it"
|
|
|
|
return
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
# stop heartbeat interval
|
2020-09-21 09:16:29 +00:00
|
|
|
g.directPeersLoop.cancel()
|
2022-05-11 08:38:43 +00:00
|
|
|
g.scoringHeartbeatFut.cancel()
|
|
|
|
g.heartbeatFut.cancel()
|
|
|
|
g.heartbeatFut = nil
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
method initPubSub*(g: GossipSub)
|
2023-06-07 11:12:49 +00:00
|
|
|
{.raises: [InitializationError].} =
|
2019-12-06 02:16:18 +00:00
|
|
|
procCall FloodSub(g).initPubSub()
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
if not g.parameters.explicit:
|
|
|
|
g.parameters = GossipSubParams.init()
|
2020-09-22 07:05:53 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
let validationRes = g.parameters.validateParameters()
|
|
|
|
if validationRes.isErr:
|
|
|
|
raise newException(InitializationError, $validationRes.error)
|
2020-09-21 09:16:29 +00:00
|
|
|
|
2020-11-26 05:45:10 +00:00
|
|
|
# init the floodsub stuff here, we customize timedcache in gossip!
|
2022-07-27 11:47:50 +00:00
|
|
|
g.seen = TimedCache[MessageId].init(g.parameters.seenTTL)
|
2020-11-26 05:45:10 +00:00
|
|
|
|
|
|
|
# init gossip stuff
|
2020-11-19 07:48:17 +00:00
|
|
|
g.mcache = MCache.init(g.parameters.historyGossip, g.parameters.historyLength)
|
2023-09-22 14:45:08 +00:00
|
|
|
|
|
|
|
method getOrCreatePeer*(
|
|
|
|
g: GossipSub,
|
|
|
|
peerId: PeerId,
|
|
|
|
protos: seq[string]): PubSubPeer =
|
|
|
|
|
|
|
|
let peer = procCall PubSub(g).getOrCreatePeer(peerId, protos)
|
|
|
|
g.parameters.overheadRateLimit.withValue(overheadRateLimit):
|
|
|
|
peer.overheadRateLimitOpt = Opt.some(TokenBucket.new(overheadRateLimit.bytes, overheadRateLimit.interval))
|
|
|
|
return peer
|