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.
|
|
|
|
|
|
|
|
import tables, sets, options, sequtils, random
|
2020-06-07 07:15:21 +00:00
|
|
|
import chronos, chronicles, metrics
|
2019-12-06 02:16:18 +00:00
|
|
|
import pubsub,
|
|
|
|
floodsub,
|
|
|
|
pubsubpeer,
|
|
|
|
mcache,
|
|
|
|
timedcache,
|
|
|
|
rpc/[messages, message],
|
|
|
|
../protocol,
|
|
|
|
../../peerinfo,
|
2020-06-19 17:29:43 +00:00
|
|
|
../../stream/connection,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../peerid,
|
2020-05-20 00:14:15 +00:00
|
|
|
../../errors,
|
|
|
|
../../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-09 20:21:47 +00:00
|
|
|
const GossipSubHeartbeatInterval* = 5.seconds # TODO: per the spec it should be 1 second
|
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-09 17:16:46 +00:00
|
|
|
mesh*: Table[string, HashSet[string]] # peers that we send messages to when we are subscribed to the topic
|
|
|
|
fanout*: Table[string, HashSet[string]] # peers that we send messages to when we're not subscribed to the topic
|
|
|
|
gossipsub*: Table[string, HashSet[string]] # 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
|
2020-05-27 18:33:49 +00:00
|
|
|
heartbeatLock: AsyncLock # heartbeat lock to prevent two consecutive concurrent heartbeats
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
declareGauge(libp2p_gossipsub_peers_per_topic_mesh,
|
|
|
|
"gossipsub peers per topic in mesh",
|
|
|
|
labels = ["topic"])
|
|
|
|
|
|
|
|
declareGauge(libp2p_gossipsub_peers_per_topic_fanout,
|
|
|
|
"gossipsub peers per topic in fanout",
|
|
|
|
labels = ["topic"])
|
|
|
|
|
|
|
|
declareGauge(libp2p_gossipsub_peers_per_topic_gossipsub,
|
|
|
|
"gossipsub peers per topic in gossipsub",
|
|
|
|
labels = ["topic"])
|
2020-06-07 07:15:21 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
func addPeer(
|
|
|
|
table: var Table[string, HashSet[string]], topic: string,
|
|
|
|
peerId: string): bool =
|
|
|
|
# returns true if the peer was added, false if it was already in the collection
|
|
|
|
not table.mgetOrPut(topic, initHashSet[string]()).containsOrIncl(peerId)
|
|
|
|
|
|
|
|
func removePeer(
|
|
|
|
table: var Table[string, HashSet[string]], topic, peerId: string) =
|
|
|
|
table.withValue(topic, peers):
|
|
|
|
peers[].excl(peerId)
|
|
|
|
if peers[].len == 0:
|
|
|
|
table.del(topic)
|
|
|
|
|
|
|
|
func hasPeer(table: Table[string, HashSet[string]], topic, peerId: string): bool =
|
|
|
|
(topic in table) and (peerId in table[topic])
|
|
|
|
|
|
|
|
func peers(table: Table[string, HashSet[string]], topic: string): int =
|
|
|
|
if topic in table:
|
|
|
|
table[topic].len
|
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
|
|
|
func getPeers(table: Table[string, HashSet[string]], topic: string): HashSet[string] =
|
|
|
|
table.getOrDefault(topic, initHashSet[string]())
|
|
|
|
|
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...
|
|
|
|
##
|
|
|
|
|
|
|
|
await g.handleConn(conn, proto)
|
|
|
|
|
|
|
|
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
|
|
|
|
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:
|
|
|
|
for peerId in g.gossipsub[topic]:
|
|
|
|
if g.fanout.addPeer(topic, peerId):
|
|
|
|
if g.fanout.peers(topic) == GossipSubD:
|
2020-05-21 20:24:20 +00:00
|
|
|
break
|
|
|
|
|
2020-06-12 02:20:58 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
2020-07-09 17:16:46 +00:00
|
|
|
.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.} =
|
|
|
|
trace "about to rebalance mesh"
|
|
|
|
# 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:
|
|
|
|
trace "replenishing mesh", topic, peers = g.mesh.peers(topic)
|
|
|
|
# replenish the mesh if we're below GossipSubDlo
|
|
|
|
var newPeers = toSeq(
|
|
|
|
g.gossipsub.getOrDefault(topic, initHashSet[string]()) -
|
|
|
|
g.mesh.getOrDefault(topic, initHashSet[string]())
|
|
|
|
)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
logScope:
|
|
|
|
topic = topic
|
|
|
|
meshPeers = g.mesh.peers(topic)
|
|
|
|
newPeers = newPeers.len
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
shuffle(newPeers)
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 20:21:47 +00:00
|
|
|
trace "getting peers", topic, peers = newPeers.len
|
2020-07-09 17:16:46 +00:00
|
|
|
|
|
|
|
for id in newPeers:
|
|
|
|
if g.mesh.peers(topic) >= GossipSubD:
|
|
|
|
break
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
let p = g.peers.getOrDefault(id)
|
|
|
|
if p != nil:
|
2020-07-08 00:33:05 +00:00
|
|
|
# send a graft message to the peer
|
2020-07-09 17:16:46 +00:00
|
|
|
grafts.add p
|
|
|
|
discard g.mesh.addPeer(topic, id)
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "got peer", peer = id
|
2020-07-09 17:16:46 +00:00
|
|
|
else:
|
|
|
|
# Peer should have been removed from mesh also!
|
|
|
|
warn "Unknown peer in mesh", peer = id
|
2020-07-08 00:33:05 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
if g.mesh.peers(topic) > GossipSubDhi:
|
|
|
|
# prune peers if we've gone over
|
|
|
|
var mesh = toSeq(g.mesh[topic])
|
|
|
|
shuffle(mesh)
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
trace "about to prune mesh", mesh = mesh.len
|
|
|
|
for id in mesh:
|
|
|
|
if g.mesh.peers(topic) <= GossipSubD:
|
|
|
|
break
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
trace "pruning peers", peers = g.mesh.peers(topic)
|
|
|
|
# send a graft message to the peer
|
|
|
|
g.mesh.removePeer(topic, id)
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
let p = g.peers.getOrDefault(id)
|
|
|
|
if p != nil:
|
|
|
|
prunes.add(p)
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
|
|
|
.set(g.gossipsub.peers(topic).int64, labelValues = [topic])
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
|
|
|
|
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
|
|
|
.set(g.mesh.peers(topic).int64, labelValues = [topic])
|
|
|
|
|
|
|
|
# Send changes to peers after table updates to avoid stale state
|
|
|
|
for p in grafts:
|
|
|
|
await p.sendGraft(@[topic])
|
|
|
|
for p in prunes:
|
|
|
|
await p.sendPrune(@[topic])
|
|
|
|
|
|
|
|
trace "mesh balanced, got peers", peers = g.mesh.peers(topic),
|
|
|
|
topicId = topic
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
2020-07-09 17:16:46 +00:00
|
|
|
.set(g.fanout.peers(topic).int64, labelValues = [topic])
|
2020-05-21 20:24:20 +00:00
|
|
|
|
|
|
|
proc getGossipPeers(g: GossipSub): Table[string, ControlMessage] {.gcsafe.} =
|
|
|
|
## 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-07-08 00:33:05 +00:00
|
|
|
if mids.len <= 0:
|
|
|
|
continue
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
let ihave = ControlIHave(topicID: topic,
|
|
|
|
messageIDs: toSeq(mids))
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
if topic notin g.gossipsub:
|
|
|
|
trace "topic not in gossip array, skipping", topicID = topic
|
|
|
|
continue
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
for id in allPeers:
|
|
|
|
if result.len >= GossipSubD:
|
|
|
|
trace "got gossip peers", peers = result.len
|
|
|
|
break
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
if id in gossipPeers:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if id notin result:
|
|
|
|
result[id] = controlMsg
|
|
|
|
|
|
|
|
result[id].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()
|
|
|
|
var sent: seq[Future[void]]
|
|
|
|
for peer in peers.keys:
|
2020-06-19 21:19:07 +00:00
|
|
|
if peer in g.peers:
|
|
|
|
sent &= g.peers[peer].send(@[RPCMsg(control: some(peers[peer]))])
|
2020-05-27 18:33:49 +00:00
|
|
|
checkFutures(await allFinished(sent))
|
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:
|
|
|
|
trace "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-07-08 00:33:05 +00:00
|
|
|
method handleDisconnect*(g: GossipSub, peer: PubSubPeer) =
|
2019-12-06 02:16:18 +00:00
|
|
|
## handle peer disconnects
|
2020-07-08 00:33:05 +00:00
|
|
|
procCall FloodSub(g).handleDisconnect(peer)
|
2020-06-19 21:19:07 +00:00
|
|
|
for t in toSeq(g.gossipsub.keys):
|
2020-07-09 17:16:46 +00:00
|
|
|
g.gossipsub.removePeer(t, peer.id)
|
2020-06-19 21:19:07 +00:00
|
|
|
|
2020-06-12 02:20:58 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
2020-07-09 17:16:46 +00:00
|
|
|
.set(g.gossipsub.peers(t).int64, labelValues = [t])
|
2020-06-12 02:20:58 +00:00
|
|
|
|
2020-06-19 21:19:07 +00:00
|
|
|
for t in toSeq(g.mesh.keys):
|
2020-07-09 17:16:46 +00:00
|
|
|
g.mesh.removePeer(t, peer.id)
|
2020-06-19 21:19:07 +00:00
|
|
|
|
2020-06-12 02:20:58 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
2020-07-09 17:16:46 +00:00
|
|
|
.set(g.mesh.peers(t).int64, labelValues = [t])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-19 21:19:07 +00:00
|
|
|
for t in toSeq(g.fanout.keys):
|
2020-07-09 17:16:46 +00:00
|
|
|
g.fanout.removePeer(t, peer.id)
|
2020-06-19 21:19:07 +00:00
|
|
|
|
2020-06-12 02:20:58 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_fanout
|
2020-07-09 17:16:46 +00:00
|
|
|
.set(g.fanout.peers(t).int64, labelValues = [t])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
method subscribePeer*(p: GossipSub,
|
2020-07-09 20:21:47 +00:00
|
|
|
conn: Connection) =
|
2020-07-08 00:33:05 +00:00
|
|
|
procCall PubSub(p).subscribePeer(conn)
|
2020-05-21 17:33:48 +00:00
|
|
|
asyncCheck p.handleConn(conn, GossipSubCodec)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
method subscribeTopic*(g: GossipSub,
|
|
|
|
topic: string,
|
|
|
|
subscribe: bool,
|
2020-05-27 18:33:49 +00:00
|
|
|
peerId: string) {.gcsafe, async.} =
|
2020-07-09 20:21:47 +00:00
|
|
|
await procCall FloodSub(g).subscribeTopic(topic, subscribe, peerId)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if subscribe:
|
|
|
|
trace "adding subscription for topic", peer = peerId, name = topic
|
2020-05-27 18:33:49 +00:00
|
|
|
# subscribe remote peer to the topic
|
2020-07-09 17:16:46 +00:00
|
|
|
discard g.gossipsub.addPeer(topic, peerId)
|
2019-12-10 20:50:35 +00:00
|
|
|
else:
|
|
|
|
trace "removing subscription for topic", peer = peerId, name = topic
|
2020-05-27 18:33:49 +00:00
|
|
|
# unsubscribe remote peer from the topic
|
2020-07-09 17:16:46 +00:00
|
|
|
g.gossipsub.removePeer(topic, peerId)
|
|
|
|
g.mesh.removePeer(topic, peerId)
|
|
|
|
g.fanout.removePeer(topic, peerId)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
libp2p_gossipsub_peers_per_topic_gossipsub
|
2020-07-09 17:16:46 +00:00
|
|
|
.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
|
|
|
|
|
|
|
# also rebalance current topic if we are subbed to
|
2020-05-27 18:33:49 +00:00
|
|
|
if topic in g.topics:
|
|
|
|
await g.rebalanceMesh(topic)
|
|
|
|
|
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] =
|
2020-07-09 17:16:46 +00:00
|
|
|
let peerId = peer.id
|
2019-12-06 02:16:18 +00:00
|
|
|
for graft in grafts:
|
2020-07-09 17:16:46 +00:00
|
|
|
let topic = graft.topicID
|
|
|
|
trace "processing graft message", topic, peerId
|
|
|
|
|
|
|
|
# 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
|
|
|
|
if g.mesh.addPeer(topic, peerId):
|
|
|
|
g.fanout.removePeer(topic, peer.id)
|
|
|
|
else:
|
|
|
|
trace "Peer already in mesh", topic, peerId
|
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-09 23:54:16 +00:00
|
|
|
result.add(ControlPrune(topicID: topic))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-12 02:20:58 +00:00
|
|
|
libp2p_gossipsub_peers_per_topic_mesh
|
2020-07-09 17:16:46 +00:00
|
|
|
.set(g.mesh.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:
|
2019-12-10 20:50:35 +00:00
|
|
|
trace "processing prune message", peer = peer.id,
|
2019-12-06 02:16:18 +00:00
|
|
|
topicID = prune.topicID
|
|
|
|
|
2020-07-09 17:16:46 +00:00
|
|
|
g.mesh.removePeer(prune.topicID, peer.id)
|
|
|
|
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:
|
|
|
|
trace "processing ihave message", peer = peer.id,
|
2020-05-21 20:24:20 +00:00
|
|
|
topicID = 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:
|
|
|
|
trace "processing iwant message", peer = peer.id,
|
|
|
|
messageID = mid
|
|
|
|
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,
|
2019-12-17 05:24:03 +00:00
|
|
|
rpcMsgs: seq[RPCMsg]) {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
await procCall PubSub(g).rpcHandler(peer, rpcMsgs)
|
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
for m in rpcMsgs: # for all RPC messages
|
2019-12-06 02:16:18 +00:00
|
|
|
if m.messages.len > 0: # if there are any messages
|
2020-05-21 20:24:20 +00:00
|
|
|
var toSendPeers: HashSet[string]
|
2019-12-06 02:16:18 +00:00
|
|
|
for msg in m.messages: # for every message
|
2020-06-28 15:56:38 +00:00
|
|
|
let msgId = g.msgIdProvider(msg)
|
|
|
|
logScope: msgId
|
|
|
|
|
|
|
|
if msgId in g.seen:
|
|
|
|
trace "message already processed, skipping"
|
2019-12-06 02:16:18 +00:00
|
|
|
continue
|
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
trace "processing message"
|
|
|
|
|
|
|
|
g.seen.put(msgId) # add the message to the seen cache
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-06 09:26:08 +00:00
|
|
|
if g.verifySignature and not msg.verify(peer.peerInfo):
|
2019-12-17 05:24:03 +00:00
|
|
|
trace "dropping message due to failed signature verification"
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not (await g.validate(msg)):
|
|
|
|
trace "dropping message due to failed validation"
|
|
|
|
continue
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
# this shouldn't happen
|
2020-06-28 15:56:38 +00:00
|
|
|
if g.peerInfo.peerId == msg.fromPeer:
|
|
|
|
trace "skipping messages from self"
|
2019-12-06 02:16:18 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
for t in msg.topicIDs: # for every topic in the message
|
|
|
|
if t in g.floodsub:
|
|
|
|
toSendPeers.incl(g.floodsub[t]) # get all floodsub peers for topic
|
|
|
|
|
|
|
|
if t in g.mesh:
|
|
|
|
toSendPeers.incl(g.mesh[t]) # get all mesh peers for topic
|
|
|
|
|
|
|
|
if t in g.topics: # if we're subscribed to the topic
|
|
|
|
for h in g.topics[t].handler:
|
2020-06-28 15:56:38 +00:00
|
|
|
trace "calling handler for message", topicId = t,
|
2019-12-07 16:36:39 +00:00
|
|
|
localPeer = g.peerInfo.id,
|
2020-06-28 15:56:38 +00:00
|
|
|
fromPeer = msg.fromPeer.pretty
|
2020-07-08 00:33:05 +00:00
|
|
|
try:
|
|
|
|
await h(t, msg.data) # trigger user provided handler
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception in message handler", exc = exc.msg
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
# forward the message to all peers interested in it
|
2020-07-08 00:33:05 +00:00
|
|
|
let (published, failed) = await g.sendHelper(toSendPeers, m.messages)
|
|
|
|
for p in failed:
|
|
|
|
let peer = g.peers.getOrDefault(p)
|
2020-07-09 08:20:45 +00:00
|
|
|
if not isNil(peer):
|
2020-07-08 00:33:05 +00:00
|
|
|
g.handleDisconnect(peer) # cleanup failed peers
|
|
|
|
|
|
|
|
trace "forwared message to peers", peers = published.len
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
var respControl: ControlMessage
|
|
|
|
if m.control.isSome:
|
2020-07-09 23:54:16 +00:00
|
|
|
let control = m.control.get()
|
2019-12-06 02:16:18 +00:00
|
|
|
g.handlePrune(peer, control.prune)
|
|
|
|
|
2020-07-09 23:54:16 +00:00
|
|
|
respControl.iwant.add(g.handleIHave(peer, control.ihave))
|
|
|
|
respControl.prune.add(g.handleGraft(peer, control.graft))
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if respControl.graft.len > 0 or respControl.prune.len > 0 or
|
|
|
|
respControl.ihave.len > 0 or respControl.iwant.len > 0:
|
2020-07-09 23:54:16 +00:00
|
|
|
await peer.send(
|
|
|
|
@[RPCMsg(control: some(respControl),
|
|
|
|
messages: g.handleIWant(peer, control.iwant))])
|
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)
|
|
|
|
|
|
|
|
for pair in topics:
|
|
|
|
let topic = pair.topic
|
|
|
|
if topic in g.mesh:
|
2020-06-12 02:20:58 +00:00
|
|
|
let peers = g.mesh.getOrDefault(topic)
|
2019-12-06 02:16:18 +00:00
|
|
|
g.mesh.del(topic)
|
2020-07-09 17:16:46 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
for id in peers:
|
2020-07-09 17:16:46 +00:00
|
|
|
let p = g.peers.getOrDefault(id)
|
|
|
|
if p != nil:
|
|
|
|
await p.sendPrune(@[topic])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
method publish*(g: GossipSub,
|
|
|
|
topic: string,
|
2020-07-08 00:33:05 +00:00
|
|
|
data: seq[byte]): Future[int] {.async.} =
|
|
|
|
# base returns always 0
|
|
|
|
discard await procCall PubSub(g).publish(topic, data)
|
2019-12-17 05:24:03 +00:00
|
|
|
trace "about to publish message on topic", name = topic,
|
2020-05-18 20:49:49 +00:00
|
|
|
data = data.shortLog
|
2020-07-08 00:33:05 +00:00
|
|
|
var peers: HashSet[string]
|
|
|
|
if topic.len <= 0: # data could be 0/empty
|
|
|
|
return 0
|
|
|
|
|
|
|
|
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-07-08 00:33:05 +00:00
|
|
|
let
|
|
|
|
msg = Message.init(g.peerInfo, data, topic, g.sign)
|
|
|
|
msgId = g.msgIdProvider(msg)
|
|
|
|
|
|
|
|
trace "created new message", msg
|
|
|
|
|
|
|
|
trace "publishing on topic", name = topic, peers = peers
|
|
|
|
if msgId notin g.mcache:
|
|
|
|
g.mcache.put(msgId, msg)
|
|
|
|
|
|
|
|
let (published, failed) = await g.sendHelper(peers, @[msg])
|
|
|
|
for p in failed:
|
|
|
|
let peer = g.peers.getOrDefault(p)
|
2020-07-09 08:20:45 +00:00
|
|
|
if not isNil(peer):
|
|
|
|
g.handleDisconnect(peer) # cleanup failed peers
|
2020-07-08 00:33:05 +00:00
|
|
|
|
|
|
|
if published.len > 0:
|
2020-06-17 04:14:02 +00:00
|
|
|
libp2p_pubsub_messages_published.inc(labelValues = [topic])
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "published message to peers", peers = published.len,
|
|
|
|
msg = msg.shortLog()
|
|
|
|
return published.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
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
## start pubsub
|
|
|
|
## start long running/repeating procedures
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2020-06-20 10:56:55 +00:00
|
|
|
# interlock start to to avoid overlapping to stops
|
|
|
|
await g.heartbeatLock.acquire()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
# setup the heartbeat interval
|
2020-06-20 10:56:55 +00:00
|
|
|
g.heartbeatRunning = true
|
|
|
|
g.heartbeatFut = g.heartbeat()
|
|
|
|
|
|
|
|
g.heartbeatLock.release()
|
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-06-28 15:56:38 +00:00
|
|
|
|
2020-06-20 10:56:55 +00:00
|
|
|
## stop pubsub
|
2019-12-06 02:16:18 +00:00
|
|
|
## stop long running tasks
|
|
|
|
|
|
|
|
await g.heartbeatLock.acquire()
|
|
|
|
|
|
|
|
# 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"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
g.heartbeatLock.release()
|
|
|
|
|
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()
|
2019-12-06 02:16:18 +00:00
|
|
|
g.mcache = newMCache(GossipSubHistoryGossip, GossipSubHistoryLength)
|
2020-06-11 18:09:34 +00:00
|
|
|
g.mesh = initTable[string, HashSet[string]]() # meshes - topic to peer
|
|
|
|
g.fanout = initTable[string, HashSet[string]]() # fanout - topic to peer
|
|
|
|
g.gossipsub = initTable[string, HashSet[string]]()# topic to peer map of all gossipsub peers
|
|
|
|
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
|
2019-12-06 02:16:18 +00:00
|
|
|
g.heartbeatLock = newAsyncLock()
|