2019-12-06 02:16:18 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
|
|
|
## 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.
|
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
import std/[options, random, sequtils, sets, tables]
|
2020-06-07 07:15:21 +00:00
|
|
|
import chronos, chronicles, metrics
|
2020-09-04 06:10:32 +00:00
|
|
|
import ./pubsub,
|
|
|
|
./floodsub,
|
|
|
|
./pubsubpeer,
|
|
|
|
./peertable,
|
|
|
|
./mcache,
|
|
|
|
./timedcache,
|
|
|
|
./rpc/[messages, message],
|
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,
|
2020-05-20 00:14:15 +00:00
|
|
|
../../utility
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "gossipsub"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
const GossipSubCodec* = "/meshsub/1.0.0"
|
|
|
|
|
|
|
|
# overlay parameters
|
2019-12-10 20:50:35 +00:00
|
|
|
const GossipSubD* = 6
|
2019-12-06 02:16:18 +00:00
|
|
|
const GossipSubDlo* = 4
|
|
|
|
const GossipSubDhi* = 12
|
|
|
|
|
|
|
|
# gossip parameters
|
|
|
|
const GossipSubHistoryLength* = 5
|
|
|
|
const GossipSubHistoryGossip* = 3
|
|
|
|
|
|
|
|
# heartbeat interval
|
|
|
|
const GossipSubHeartbeatInitialDelay* = 100.millis
|
2020-07-27 19:33:51 +00:00
|
|
|
const GossipSubHeartbeatInterval* = 1.seconds
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
# fanout ttl
|
2020-01-07 08:04:02 +00:00
|
|
|
const GossipSubFanoutTTL* = 1.minutes
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
GossipSub* = ref object of FloodSub
|
2020-07-13 13:32:38 +00:00
|
|
|
mesh*: PeerTable # peers that we send messages to when we are subscribed to the topic
|
|
|
|
fanout*: PeerTable # peers that we send messages to when we're not subscribed to the topic
|
|
|
|
gossipsub*: PeerTable # peers that are subscribed to a topic
|
2020-01-07 08:04:02 +00:00
|
|
|
lastFanoutPubSub*: Table[string, Moment] # last publish time for fanout topics
|
|
|
|
gossip*: Table[string, seq[ControlIHave]] # pending gossip
|
|
|
|
control*: Table[string, ControlMessage] # pending control messages
|
|
|
|
mcache*: MCache # messages cache
|
2020-06-29 15:15:31 +00:00
|
|
|
heartbeatFut: Future[void] # cancellation future for heartbeat interval
|
2020-06-20 10:56:55 +00:00
|
|
|
heartbeatRunning: bool
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
declareGauge(libp2p_gossipsub_peers_per_topic_mesh,
|
|
|
|
"gossipsub peers per topic in mesh",
|
|
|
|
labels = ["topic"])
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
declareGauge(libp2p_gossipsub_peers_per_topic_fanout,
|
|
|
|
"gossipsub peers per topic in fanout",
|
|
|
|
labels = ["topic"])
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
declareGauge(libp2p_gossipsub_peers_per_topic_gossipsub,
|
|
|
|
"gossipsub peers per topic in gossipsub",
|
|
|
|
labels = ["topic"])
|
2020-06-07 07:15:21 +00:00
|
|
|
|
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
|
|
|
|
g.codec = GossipSubCodec
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
proc replenishFanout(g: GossipSub, topic: string) =
|
2020-05-21 20:24:20 +00:00
|
|
|
## get fanout peers for a topic
|
2020-09-06 08:31:47 +00:00
|
|
|
logScope: topic
|
2020-05-21 20:24:20 +00:00
|
|
|
trace "about to replenish fanout"
|
2020-07-09 17:16:46 +00:00
|
|
|
|
|
|
|
if g.fanout.peers(topic) < GossipSubDLo:
|
|
|
|
trace "replenishing fanout", peers = g.fanout.peers(topic)
|
|
|
|
if topic in g.gossipsub:
|
2020-07-13 13:32:38 +00:00
|
|
|
for peer in g.gossipsub[topic]:
|
|
|
|
if g.fanout.addPeer(topic, peer):
|
2020-07-09 17:16:46 +00:00
|
|
|
if g.fanout.peers(topic) == GossipSubD:
|
2020-05-21 20:24:20 +00:00
|
|
|
break
|
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
trace "fanout replenished with peers", peers = g.fanout.peers(topic)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
proc rebalanceMesh(g: GossipSub, topic: string) {.async.} =
|
2020-07-16 19:26:57 +00:00
|
|
|
logScope:
|
|
|
|
topic
|
2020-08-15 19:50:31 +00:00
|
|
|
mesh = g.mesh.peers(topic)
|
|
|
|
gossipsub = g.gossipsub.peers(topic)
|
2020-07-16 19:26:57 +00:00
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
trace "rebalancing mesh"
|
2020-07-16 19:26:57 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
# create a mesh topic that we're subscribing to
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
var
|
|
|
|
grafts, prunes: seq[PubSubPeer]
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
if g.mesh.peers(topic) < GossipSubDlo:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "replenishing mesh", peers = g.mesh.peers(topic)
|
|
|
|
# replenish the mesh if we're below Dlo
|
|
|
|
grafts = toSeq(
|
2020-07-13 13:32:38 +00:00
|
|
|
g.gossipsub.getOrDefault(topic, initHashSet[PubSubPeer]()) -
|
|
|
|
g.mesh.getOrDefault(topic, initHashSet[PubSubPeer]())
|
2020-07-09 17:16:46 +00:00
|
|
|
)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
shuffle(grafts)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
# Graft peers so we reach a count of D
|
|
|
|
grafts.setLen(min(grafts.len, GossipSubD - g.mesh.peers(topic)))
|
2020-07-09 17:16:46 +00:00
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
trace "grafting", grafts = grafts.len
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
for peer in grafts:
|
|
|
|
if g.mesh.addPeer(topic, peer):
|
|
|
|
g.fanout.removePeer(topic, peer)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-16 19:26:57 +00:00
|
|
|
if g.mesh.peers(topic) > GossipSubDhi:
|
|
|
|
# prune peers if we've gone over Dhi
|
|
|
|
prunes = toSeq(g.mesh[topic])
|
|
|
|
shuffle(prunes)
|
|
|
|
prunes.setLen(prunes.len - GossipSubD) # .. down to D peers
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
trace "pruning", prunes = prunes.len
|
2020-07-16 19:26:57 +00:00
|
|
|
for peer in prunes:
|
2020-07-13 13:32:38 +00:00
|
|
|
g.mesh.removePeer(topic, peer)
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
|
|
|
.set(g.gossipsub.peers(topic).int64, labelValues = [topic])
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
2020-07-09 17:16:46 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
|
|
|
.set(g.mesh.peers(topic).int64, labelValues = [topic])
|
2020-07-09 17:16:46 +00:00
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
trace "mesh balanced"
|
2020-07-09 17:16:46 +00:00
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
# Send changes to peers after table updates to avoid stale state
|
|
|
|
if grafts.len > 0:
|
|
|
|
let graft = RPCMsg(control: some(ControlMessage(graft: @[ControlGraft(topicID: topic)])))
|
2020-09-01 07:33:03 +00:00
|
|
|
g.broadcast(grafts, graft)
|
2020-08-15 19:50:31 +00:00
|
|
|
if prunes.len > 0:
|
|
|
|
let prune = RPCMsg(control: some(ControlMessage(prune: @[ControlPrune(topicID: topic)])))
|
2020-09-01 07:33:03 +00:00
|
|
|
g.broadcast(prunes, prune)
|
2020-07-09 17:16:46 +00:00
|
|
|
|
|
|
|
proc dropFanoutPeers(g: GossipSub) =
|
2020-05-21 20:24:20 +00:00
|
|
|
# drop peers that we haven't published to in
|
|
|
|
# GossipSubFanoutTTL seconds
|
2020-07-09 17:16:46 +00:00
|
|
|
let now = Moment.now()
|
2020-07-09 23:54:16 +00:00
|
|
|
for topic in toSeq(g.lastFanoutPubSub.keys):
|
|
|
|
let val = g.lastFanoutPubSub[topic]
|
2020-07-09 17:16:46 +00:00
|
|
|
if now > val:
|
2020-05-21 20:24:20 +00:00
|
|
|
g.fanout.del(topic)
|
2020-07-09 23:54:16 +00:00
|
|
|
g.lastFanoutPubSub.del(topic)
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "dropping fanout topic", topic
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
proc getGossipPeers(g: GossipSub): Table[PubSubPeer, ControlMessage] {.gcsafe.} =
|
2020-05-21 20:24:20 +00:00
|
|
|
## gossip iHave messages to peers
|
2020-07-08 00:33:05 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
trace "getting gossip peers (iHave)"
|
2020-05-21 20:24:20 +00:00
|
|
|
let topics = toHashSet(toSeq(g.mesh.keys)) + toHashSet(toSeq(g.fanout.keys))
|
2020-07-08 00:33:05 +00:00
|
|
|
let controlMsg = ControlMessage()
|
2020-05-21 20:24:20 +00:00
|
|
|
for topic in topics:
|
2020-07-08 00:33:05 +00:00
|
|
|
var allPeers = toSeq(g.gossipsub.getOrDefault(topic))
|
|
|
|
shuffle(allPeers)
|
|
|
|
|
|
|
|
let mesh = g.mesh.getOrDefault(topic)
|
|
|
|
let fanout = g.fanout.getOrDefault(topic)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
|
|
|
let gossipPeers = mesh + fanout
|
|
|
|
let mids = g.mcache.window(topic)
|
2020-08-03 05:20:11 +00:00
|
|
|
if not mids.len > 0:
|
2020-07-08 00:33:05 +00:00
|
|
|
continue
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
if topic notin g.gossipsub:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "topic not in gossip array, skipping", topic
|
2020-07-08 00:33:05 +00:00
|
|
|
continue
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-08-03 05:20:11 +00:00
|
|
|
let ihave = ControlIHave(topicID: topic, messageIDs: toSeq(mids))
|
2020-07-13 13:32:38 +00:00
|
|
|
for peer in allPeers:
|
2020-07-08 00:33:05 +00:00
|
|
|
if result.len >= GossipSubD:
|
|
|
|
trace "got gossip peers", peers = result.len
|
|
|
|
break
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-13 13:32:38 +00:00
|
|
|
if peer in gossipPeers:
|
2020-07-08 00:33:05 +00:00
|
|
|
continue
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
if peer notin result:
|
|
|
|
result[peer] = controlMsg
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
result[peer].ihave.add(ihave)
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
proc heartbeat(g: GossipSub) {.async.} =
|
2020-06-20 10:56:55 +00:00
|
|
|
while g.heartbeatRunning:
|
2020-05-27 18:33:49 +00:00
|
|
|
try:
|
|
|
|
trace "running heartbeat"
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-06-15 18:30:09 +00:00
|
|
|
for t in toSeq(g.topics.keys):
|
2020-05-27 18:33:49 +00:00
|
|
|
await g.rebalanceMesh(t)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
g.dropFanoutPeers()
|
2020-07-08 00:33:05 +00:00
|
|
|
|
|
|
|
# replenish known topics to the fanout
|
|
|
|
for t in toSeq(g.fanout.keys):
|
|
|
|
g.replenishFanout(t)
|
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
let peers = g.getGossipPeers()
|
2020-08-02 10:22:49 +00:00
|
|
|
for peer, control in peers:
|
2020-08-12 00:05:49 +00:00
|
|
|
g.peers.withValue(peer.peerId, pubsubPeer) do:
|
2020-09-01 07:33:03 +00:00
|
|
|
g.send(
|
2020-08-12 00:05:49 +00:00
|
|
|
pubsubPeer[],
|
2020-09-01 07:33:03 +00:00
|
|
|
RPCMsg(control: some(control)))
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
g.mcache.shift() # shift the cache
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-27 18:33:49 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-04 16:31:43 +00:00
|
|
|
warn "exception ocurred in gossipsub heartbeat", exc = exc.msg
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-09 20:21:47 +00:00
|
|
|
await sleepAsync(GossipSubHeartbeatInterval)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-08-12 00:05:49 +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-08-12 00:05:49 +00:00
|
|
|
for t in toSeq(g.gossipsub.keys):
|
|
|
|
g.gossipsub.removePeer(t, pubSubPeer)
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
|
|
|
.set(g.gossipsub.peers(t).int64, labelValues = [t])
|
2020-06-19 21:19:07 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
for t in toSeq(g.mesh.keys):
|
|
|
|
g.mesh.removePeer(t, pubSubPeer)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
|
|
|
.set(g.mesh.peers(t).int64, labelValues = [t])
|
2020-06-19 21:19:07 +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
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(t).int64, labelValues = [t])
|
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
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method subscribeTopic*(g: GossipSub,
|
|
|
|
topic: string,
|
|
|
|
subscribe: bool,
|
2020-08-17 10:10:22 +00:00
|
|
|
peer: PubSubPeer) {.gcsafe.} =
|
2020-09-04 20:53:03 +00:00
|
|
|
# Skip floodsub - we don't want it to add the peer to `g.floodsub`
|
|
|
|
procCall PubSub(g).subscribeTopic(topic, subscribe, peer)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
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
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if subscribe:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "peer subscribed to topic"
|
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)
|
2019-12-10 20:50:35 +00:00
|
|
|
else:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "peer unsubscribed from 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)
|
|
|
|
g.mesh.removePeer(topic, peer)
|
|
|
|
g.fanout.removePeer(topic, peer)
|
2020-07-09 17:16:46 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
|
|
|
.set(g.mesh.peers(topic).int64, labelValues = [topic])
|
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
|
|
|
.set(g.gossipsub.peers(topic).int64, labelValues = [topic])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
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
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc handleGraft(g: GossipSub,
|
2019-12-06 02:16:18 +00:00
|
|
|
peer: PubSubPeer,
|
2020-07-09 23:54:16 +00:00
|
|
|
grafts: seq[ControlGraft]): seq[ControlPrune] =
|
2019-12-06 02:16:18 +00:00
|
|
|
for graft in grafts:
|
2020-07-09 17:16:46 +00:00
|
|
|
let topic = graft.topicID
|
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
|
|
|
|
|
|
|
|
trace "peer grafted topic"
|
2020-07-09 17:16:46 +00:00
|
|
|
|
|
|
|
# If they send us a graft before they send us a subscribe, what should
|
|
|
|
# we do? For now, we add them to mesh but don't add them to gossipsub.
|
|
|
|
|
|
|
|
if topic in g.topics:
|
|
|
|
if g.mesh.peers(topic) < GossipSubDHi:
|
|
|
|
# In the spec, there's no mention of DHi here, but implicitly, a
|
|
|
|
# peer will be removed from the mesh on next rebalance, so we don't want
|
|
|
|
# this peer to push someone else out
|
2020-07-13 13:32:38 +00:00
|
|
|
if g.mesh.addPeer(topic, peer):
|
|
|
|
g.fanout.removePeer(topic, peer)
|
2020-07-09 17:16:46 +00:00
|
|
|
else:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "peer already in mesh"
|
2019-12-06 02:16:18 +00:00
|
|
|
else:
|
2020-07-09 23:54:16 +00:00
|
|
|
result.add(ControlPrune(topicID: topic))
|
2019-12-06 02:16:18 +00:00
|
|
|
else:
|
2020-07-16 19:26:57 +00:00
|
|
|
debug "peer grafting topic we're not interested in"
|
2020-07-09 23:54:16 +00:00
|
|
|
result.add(ControlPrune(topicID: topic))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
|
|
|
.set(g.mesh.peers(topic).int64, labelValues = [topic])
|
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc handlePrune(g: GossipSub, peer: PubSubPeer, prunes: seq[ControlPrune]) =
|
|
|
|
for prune in prunes:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "peer pruned topic", peer, topic = prune.topicID
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-13 13:32:38 +00:00
|
|
|
g.mesh.removePeer(prune.topicID, peer)
|
2020-08-05 12:02:26 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
|
|
|
.set(g.mesh.peers(prune.topicID).int64, labelValues = [prune.topicID])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
proc handleIHave(g: GossipSub,
|
|
|
|
peer: PubSubPeer,
|
|
|
|
ihaves: seq[ControlIHave]): ControlIWant =
|
2019-12-06 02:16:18 +00:00
|
|
|
for ihave in ihaves:
|
2020-07-16 19:26:57 +00:00
|
|
|
trace "peer sent ihave",
|
2020-09-06 08:31:47 +00:00
|
|
|
peer, topic = ihave.topicID, msgs = ihave.messageIDs
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
if ihave.topicID in g.mesh:
|
|
|
|
for m in ihave.messageIDs:
|
|
|
|
if m notin g.seen:
|
|
|
|
result.messageIDs.add(m)
|
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
proc handleIWant(g: GossipSub,
|
|
|
|
peer: PubSubPeer,
|
|
|
|
iwants: seq[ControlIWant]): seq[Message] =
|
2019-12-06 02:16:18 +00:00
|
|
|
for iwant in iwants:
|
|
|
|
for mid in iwant.messageIDs:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "peer sent iwant", peer, messageID = mid
|
2019-12-06 02:16:18 +00:00
|
|
|
let msg = g.mcache.get(mid)
|
|
|
|
if msg.isSome:
|
|
|
|
result.add(msg.get())
|
|
|
|
|
2020-06-12 23:54:12 +00:00
|
|
|
method rpcHandler*(g: GossipSub,
|
2019-12-06 02:16:18 +00:00
|
|
|
peer: PubSubPeer,
|
2020-09-01 07:33:03 +00:00
|
|
|
rpcMsg: RPCMsg) {.async.} =
|
|
|
|
await procCall PubSub(g).rpcHandler(peer, rpcMsg)
|
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
for msg in rpcMsg.messages: # for every message
|
|
|
|
let msgId = g.msgIdProvider(msg)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
if g.seen.put(msgId):
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "Dropping already-seen message", msgId, peer
|
2020-09-04 06:10:32 +00:00
|
|
|
continue
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
g.mcache.put(msgId, msg)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
if g.verifySignature and not msg.verify(peer.peerId):
|
2020-09-06 08:31:47 +00:00
|
|
|
debug "Dropping message due to failed signature verification", msgId, peer
|
2020-09-04 06:10:32 +00:00
|
|
|
continue
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
if not (await g.validate(msg)):
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "Dropping message due to failed validation", msgId, peer
|
2020-09-04 06:10:32 +00:00
|
|
|
continue
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
var toSendPeers = initHashSet[PubSubPeer]()
|
|
|
|
for t in msg.topicIDs: # for every topic in the message
|
|
|
|
g.floodsub.withValue(t, peers): toSendPeers.incl(peers[])
|
|
|
|
g.mesh.withValue(t, peers): toSendPeers.incl(peers[])
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
await handleData(g, t, msg.data)
|
2020-09-01 07:33:03 +00:00
|
|
|
|
2020-09-04 06:10:32 +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(toSeq(toSendPeers), RPCMsg(messages: @[msg]))
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "forwared message to peers", peers = toSendPeers.len, msgId, peer
|
2020-09-01 07:33:03 +00:00
|
|
|
|
|
|
|
if rpcMsg.control.isSome:
|
|
|
|
let control = rpcMsg.control.get()
|
|
|
|
g.handlePrune(peer, control.prune)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var respControl: ControlMessage
|
2020-09-01 07:33:03 +00:00
|
|
|
respControl.iwant.add(g.handleIHave(peer, control.ihave))
|
|
|
|
respControl.prune.add(g.handleGraft(peer, control.graft))
|
|
|
|
let messages = g.handleIWant(peer, control.iwant)
|
|
|
|
|
|
|
|
if respControl.graft.len > 0 or respControl.prune.len > 0 or
|
|
|
|
respControl.ihave.len > 0 or messages.len > 0:
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
debug "sending control message", msg = shortLog(respControl), peer
|
2020-09-01 07:33:03 +00:00
|
|
|
g.send(
|
|
|
|
peer,
|
|
|
|
RPCMsg(control: some(respControl), messages: messages))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
method subscribe*(g: GossipSub,
|
|
|
|
topic: string,
|
2019-12-17 05:24:03 +00:00
|
|
|
handler: TopicHandler) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
await procCall PubSub(g).subscribe(topic, handler)
|
2020-05-21 20:24:20 +00:00
|
|
|
await g.rebalanceMesh(topic)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
method unsubscribe*(g: GossipSub,
|
2019-12-17 05:24:03 +00:00
|
|
|
topics: seq[TopicPair]) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
await procCall PubSub(g).unsubscribe(topics)
|
|
|
|
|
2020-07-20 16:16:13 +00:00
|
|
|
for (topic, handler) in topics:
|
|
|
|
# delete from mesh only if no handlers are left
|
2020-09-04 06:10:32 +00:00
|
|
|
if topic notin g.topics:
|
2020-07-20 16:16:13 +00:00
|
|
|
if topic in g.mesh:
|
2020-09-04 06:10:32 +00:00
|
|
|
let peers = g.mesh[topic]
|
2020-07-20 16:16:13 +00:00
|
|
|
g.mesh.del(topic)
|
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
let prune = RPCMsg(
|
|
|
|
control: some(ControlMessage(prune: @[ControlPrune(topicID: topic)])))
|
2020-09-01 07:33:03 +00:00
|
|
|
g.broadcast(toSeq(peers), prune)
|
2020-07-20 16:16:13 +00:00
|
|
|
|
|
|
|
method unsubscribeAll*(g: GossipSub, topic: string) {.async.} =
|
|
|
|
await procCall PubSub(g).unsubscribeAll(topic)
|
|
|
|
|
|
|
|
if topic in g.mesh:
|
|
|
|
let peers = g.mesh.getOrDefault(topic)
|
|
|
|
g.mesh.del(topic)
|
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
let prune = RPCMsg(control: some(ControlMessage(prune: @[ControlPrune(topicID: topic)])))
|
2020-09-01 07:33:03 +00:00
|
|
|
g.broadcast(toSeq(peers), prune)
|
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
|
|
|
|
|
|
|
logScope: topic
|
|
|
|
trace "Publishing message on topic", data = data.shortLog
|
2020-07-16 19:26:57 +00:00
|
|
|
|
2020-07-08 00:33:05 +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-08-15 19:50:31 +00:00
|
|
|
var peers: HashSet[PubSubPeer]
|
2020-07-08 00:33:05 +00:00
|
|
|
if topic in g.topics: # if we're subscribed use the mesh
|
|
|
|
peers = g.mesh.getOrDefault(topic)
|
|
|
|
else: # not subscribed, send to fanout peers
|
|
|
|
# try optimistically
|
|
|
|
peers = g.fanout.getOrDefault(topic)
|
|
|
|
if peers.len == 0:
|
|
|
|
# ok we had nothing.. let's try replenish inline
|
|
|
|
g.replenishFanout(topic)
|
|
|
|
peers = g.fanout.getOrDefault(topic)
|
|
|
|
|
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
|
|
|
|
g.lastFanoutPubSub[topic] = Moment.fromNow(GossipSubFanoutTTL)
|
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
if peers.len == 0:
|
|
|
|
debug "No peers for topic, skipping publish"
|
|
|
|
return 0
|
|
|
|
|
2020-07-15 03:51:33 +00:00
|
|
|
inc g.msgSeqno
|
2020-07-08 00:33:05 +00:00
|
|
|
let
|
2020-07-15 03:51:33 +00:00
|
|
|
msg = Message.init(g.peerInfo, data, topic, g.msgSeqno, g.sign)
|
2020-07-08 00:33:05 +00:00
|
|
|
msgId = g.msgIdProvider(msg)
|
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
logScope: 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
|
|
|
|
2020-09-04 06:10:32 +00:00
|
|
|
if g.seen.put(msgId):
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
g.broadcast(toSeq(peers), RPCMsg(messages: @[msg]))
|
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
if peers.len > 0:
|
|
|
|
libp2p_pubsub_messages_published.inc(labelValues = [topic])
|
|
|
|
|
|
|
|
trace "Published message to peers"
|
|
|
|
|
|
|
|
return peers.len
|
|
|
|
|
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.heartbeatRunning = true
|
|
|
|
g.heartbeatFut = g.heartbeat()
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method stop*(g: GossipSub) {.async.} =
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "gossipsub stop"
|
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-06-20 10:56:55 +00:00
|
|
|
g.heartbeatRunning = false
|
|
|
|
if not g.heartbeatFut.finished:
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "awaiting last heartbeat"
|
2020-06-20 10:56:55 +00:00
|
|
|
await g.heartbeatFut
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "heartbeat stopped"
|
2020-09-04 16:31:43 +00:00
|
|
|
g.heartbeatFut = nil
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
|
2020-05-29 15:35:03 +00:00
|
|
|
method initPubSub*(g: GossipSub) =
|
2019-12-06 02:16:18 +00:00
|
|
|
procCall FloodSub(g).initPubSub()
|
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
randomize()
|
2020-09-04 06:10:32 +00:00
|
|
|
g.mcache = MCache.init(GossipSubHistoryGossip, GossipSubHistoryLength)
|
2020-07-13 13:32:38 +00:00
|
|
|
g.mesh = initTable[string, HashSet[PubSubPeer]]() # meshes - topic to peer
|
|
|
|
g.fanout = initTable[string, HashSet[PubSubPeer]]() # fanout - topic to peer
|
|
|
|
g.gossipsub = initTable[string, HashSet[PubSubPeer]]()# topic to peer map of all gossipsub peers
|
2020-06-11 18:09:34 +00:00
|
|
|
g.lastFanoutPubSub = initTable[string, Moment]() # last publish time for fanout topics
|
2019-12-06 02:16:18 +00:00
|
|
|
g.gossip = initTable[string, seq[ControlIHave]]() # pending gossip
|
2020-06-11 18:09:34 +00:00
|
|
|
g.control = initTable[string, ControlMessage]() # pending control messages
|