2022-11-04 08:40:13 +00:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
|
|
import
|
2023-04-14 13:12:22 +00:00
|
|
|
|
std/[options, sets, sequtils, times, strutils, math],
|
2022-11-24 13:11:23 +00:00
|
|
|
|
chronos,
|
|
|
|
|
chronicles,
|
2022-11-04 08:40:13 +00:00
|
|
|
|
metrics,
|
2023-04-12 11:05:34 +00:00
|
|
|
|
libp2p/multistream,
|
2023-05-31 07:47:56 +00:00
|
|
|
|
libp2p/muxers/muxer,
|
|
|
|
|
libp2p/nameresolving/nameresolver
|
2022-11-04 08:40:13 +00:00
|
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
|
../../common/nimchronos,
|
2023-04-24 14:37:54 +00:00
|
|
|
|
../../waku_core,
|
2023-04-18 13:22:10 +00:00
|
|
|
|
../../waku_relay,
|
2023-09-22 19:13:50 +00:00
|
|
|
|
../../waku_enr/sharding,
|
2023-10-11 06:58:45 +00:00
|
|
|
|
../../waku_metadata,
|
2022-11-04 08:40:13 +00:00
|
|
|
|
./peer_store/peer_storage,
|
|
|
|
|
./waku_peer_store
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
|
export waku_peer_store, peer_storage, peers
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
declareCounter waku_peers_dials, "Number of peer dials", ["outcome"]
|
2022-12-14 15:04:11 +00:00
|
|
|
|
# TODO: Populate from PeerStore.Source when ready
|
2022-09-20 11:03:34 +00:00
|
|
|
|
declarePublicCounter waku_node_conns_initiated, "Number of connections initiated", ["source"]
|
2021-03-26 08:49:51 +00:00
|
|
|
|
declarePublicGauge waku_peers_errors, "Number of peer manager errors", ["type"]
|
2023-04-12 11:05:34 +00:00
|
|
|
|
declarePublicGauge waku_connected_peers, "Number of physical connections per direction and protocol", labels = ["direction", "protocol"]
|
|
|
|
|
declarePublicGauge waku_streams_peers, "Number of streams per direction and protocol", labels = ["direction", "protocol"]
|
2023-01-31 12:24:49 +00:00
|
|
|
|
declarePublicGauge waku_peer_store_size, "Number of peers managed by the peer store"
|
2023-02-27 17:24:31 +00:00
|
|
|
|
declarePublicGauge waku_service_peers, "Service peer protocol and multiaddress ", labels = ["protocol", "peerId"]
|
2022-11-04 08:40:13 +00:00
|
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
|
topics = "waku node peer_manager"
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
2022-12-14 15:04:11 +00:00
|
|
|
|
const
|
|
|
|
|
# TODO: Make configurable
|
|
|
|
|
DefaultDialTimeout = chronos.seconds(10)
|
2021-02-05 10:49:11 +00:00
|
|
|
|
|
2023-01-23 20:24:46 +00:00
|
|
|
|
# Max attempts before removing the peer
|
|
|
|
|
MaxFailedAttempts = 5
|
|
|
|
|
|
|
|
|
|
# Time to wait before attempting to dial again is calculated as:
|
|
|
|
|
# initialBackoffInSec*(backoffFactor^(failedAttempts-1))
|
|
|
|
|
# 120s, 480s, 1920, 7680s
|
|
|
|
|
InitialBackoffInSec = 120
|
|
|
|
|
BackoffFactor = 4
|
|
|
|
|
|
2023-01-26 09:20:20 +00:00
|
|
|
|
# Limit the amount of paralel dials
|
2023-01-18 14:17:56 +00:00
|
|
|
|
MaxParalelDials = 10
|
|
|
|
|
|
2023-01-26 09:20:20 +00:00
|
|
|
|
# Delay between consecutive relayConnectivityLoop runs
|
2023-02-14 14:38:32 +00:00
|
|
|
|
ConnectivityLoopInterval = chronos.seconds(15)
|
2023-01-18 14:17:56 +00:00
|
|
|
|
|
2023-01-31 12:24:49 +00:00
|
|
|
|
# How often the peer store is pruned
|
2023-09-08 11:36:26 +00:00
|
|
|
|
PrunePeerStoreInterval = chronos.minutes(10)
|
2023-01-31 12:24:49 +00:00
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
# How often metrics and logs are shown/updated
|
2023-07-04 11:31:18 +00:00
|
|
|
|
LogAndMetricsInterval = chronos.minutes(3)
|
2023-04-12 11:05:34 +00:00
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
# Max peers that we allow from the same IP
|
|
|
|
|
ColocationLimit = 5
|
2023-05-18 07:40:14 +00:00
|
|
|
|
|
2023-01-23 20:24:46 +00:00
|
|
|
|
type
|
|
|
|
|
PeerManager* = ref object of RootObj
|
|
|
|
|
switch*: Switch
|
|
|
|
|
peerStore*: PeerStore
|
2023-10-11 06:58:45 +00:00
|
|
|
|
wakuMetadata*: WakuMetadata
|
2023-01-23 20:24:46 +00:00
|
|
|
|
initialBackoffInSec*: int
|
|
|
|
|
backoffFactor*: int
|
|
|
|
|
maxFailedAttempts*: int
|
|
|
|
|
storage: PeerStorage
|
2023-01-26 09:20:20 +00:00
|
|
|
|
serviceSlots*: Table[string, RemotePeerInfo]
|
2023-06-23 13:30:28 +00:00
|
|
|
|
maxRelayPeers*: int
|
2023-07-04 11:31:18 +00:00
|
|
|
|
outRelayPeersTarget: int
|
|
|
|
|
inRelayPeersTarget: int
|
2023-05-31 07:47:56 +00:00
|
|
|
|
ipTable*: Table[string, seq[PeerId]]
|
|
|
|
|
colocationLimit*: int
|
2023-01-26 09:20:20 +00:00
|
|
|
|
started: bool
|
2023-01-23 20:24:46 +00:00
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
proc protocolMatcher*(codec: string): Matcher =
|
|
|
|
|
## Returns a protocol matcher function for the provided codec
|
|
|
|
|
proc match(proto: string): bool {.gcsafe.} =
|
|
|
|
|
## Matches a proto with any postfix to the provided codec.
|
|
|
|
|
## E.g. if the codec is `/vac/waku/filter/2.0.0` it matches the protos:
|
|
|
|
|
## `/vac/waku/filter/2.0.0`, `/vac/waku/filter/2.0.0-beta3`, `/vac/waku/filter/2.0.0-actualnonsense`
|
|
|
|
|
return proto.startsWith(codec)
|
|
|
|
|
|
|
|
|
|
return match
|
|
|
|
|
|
2023-04-14 13:12:22 +00:00
|
|
|
|
proc calculateBackoff(initialBackoffInSec: int,
|
|
|
|
|
backoffFactor: int,
|
|
|
|
|
failedAttempts: int): timer.Duration =
|
|
|
|
|
if failedAttempts == 0:
|
|
|
|
|
return chronos.seconds(0)
|
|
|
|
|
return chronos.seconds(initialBackoffInSec*(backoffFactor^(failedAttempts-1)))
|
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
####################
|
|
|
|
|
# Helper functions #
|
|
|
|
|
####################
|
|
|
|
|
|
2021-04-21 09:36:56 +00:00
|
|
|
|
proc insertOrReplace(ps: PeerStorage,
|
|
|
|
|
peerId: PeerID,
|
2023-03-09 18:05:50 +00:00
|
|
|
|
remotePeerInfo: RemotePeerInfo,
|
2021-06-09 14:37:08 +00:00
|
|
|
|
connectedness: Connectedness,
|
2021-07-14 17:58:46 +00:00
|
|
|
|
disconnectTime: int64 = 0) =
|
2021-03-26 08:49:51 +00:00
|
|
|
|
# Insert peer entry into persistent storage, or replace existing entry with updated info
|
2023-03-09 18:05:50 +00:00
|
|
|
|
let res = ps.put(peerId, remotePeerInfo, connectedness, disconnectTime)
|
2021-03-26 08:49:51 +00:00
|
|
|
|
if res.isErr:
|
|
|
|
|
warn "failed to store peers", err = res.error
|
|
|
|
|
waku_peers_errors.inc(labelValues = ["storage_failure"])
|
|
|
|
|
|
2023-04-19 14:12:00 +00:00
|
|
|
|
proc addPeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, origin = UnknownOrigin) =
|
2023-03-28 11:29:48 +00:00
|
|
|
|
# Adds peer to manager for the specified protocol
|
|
|
|
|
|
|
|
|
|
if remotePeerInfo.peerId == pm.switch.peerInfo.peerId:
|
|
|
|
|
# Do not attempt to manage our unmanageable self
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# ...public key
|
|
|
|
|
var publicKey: PublicKey
|
|
|
|
|
discard remotePeerInfo.peerId.extractPublicKey(publicKey)
|
|
|
|
|
|
|
|
|
|
if pm.peerStore[AddressBook][remotePeerInfo.peerId] == remotePeerInfo.addrs and
|
2023-09-11 08:30:12 +00:00
|
|
|
|
pm.peerStore[KeyBook][remotePeerInfo.peerId] == publicKey and
|
|
|
|
|
pm.peerStore[ENRBook][remotePeerInfo.peerId].raw.len > 0:
|
|
|
|
|
# Peer already managed and ENR info is already saved
|
2023-03-28 11:29:48 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
trace "Adding peer to manager", peerId = remotePeerInfo.peerId, addresses = remotePeerInfo.addrs
|
|
|
|
|
|
|
|
|
|
pm.peerStore[AddressBook][remotePeerInfo.peerId] = remotePeerInfo.addrs
|
|
|
|
|
pm.peerStore[KeyBook][remotePeerInfo.peerId] = publicKey
|
2023-04-19 14:12:00 +00:00
|
|
|
|
pm.peerStore[SourceBook][remotePeerInfo.peerId] = origin
|
|
|
|
|
|
|
|
|
|
if remotePeerInfo.enr.isSome():
|
|
|
|
|
pm.peerStore[ENRBook][remotePeerInfo.peerId] = remotePeerInfo.enr.get()
|
2023-03-28 11:29:48 +00:00
|
|
|
|
|
|
|
|
|
# Add peer to storage. Entry will subsequently be updated with connectedness information
|
|
|
|
|
if not pm.storage.isNil:
|
2023-10-11 06:58:45 +00:00
|
|
|
|
pm.storage.insertOrReplace(remotePeerInfo.peerId, remotePeerInfo, NotConnected)
|
2023-03-28 11:29:48 +00:00
|
|
|
|
|
|
|
|
|
# Connects to a given node. Note that this function uses `connect` and
|
|
|
|
|
# does not provide a protocol. Streams for relay (gossipsub) are created
|
|
|
|
|
# automatically without the needing to dial.
|
|
|
|
|
proc connectRelay*(pm: PeerManager,
|
|
|
|
|
peer: RemotePeerInfo,
|
|
|
|
|
dialTimeout = DefaultDialTimeout,
|
|
|
|
|
source = "api"): Future[bool] {.async.} =
|
|
|
|
|
|
|
|
|
|
let peerId = peer.peerId
|
2022-12-14 15:04:11 +00:00
|
|
|
|
|
|
|
|
|
# Do not attempt to dial self
|
|
|
|
|
if peerId == pm.switch.peerInfo.peerId:
|
2023-03-28 11:29:48 +00:00
|
|
|
|
return false
|
2022-12-14 15:04:11 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
if not pm.peerStore.hasPeer(peerId, WakuRelayCodec):
|
|
|
|
|
pm.addPeer(peer)
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
let failedAttempts = pm.peerStore[NumberFailedConnBook][peerId]
|
|
|
|
|
debug "Connecting to relay peer", wireAddr=peer.addrs, peerId=peerId, failedAttempts=failedAttempts
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
var deadline = sleepAsync(dialTimeout)
|
|
|
|
|
var workfut = pm.switch.connect(peerId, peer.addrs)
|
2023-01-23 20:24:46 +00:00
|
|
|
|
var reasonFailed = ""
|
2023-03-28 11:29:48 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
try:
|
2023-03-28 11:29:48 +00:00
|
|
|
|
await workfut or deadline
|
|
|
|
|
if workfut.finished():
|
|
|
|
|
if not deadline.finished():
|
|
|
|
|
deadline.cancel()
|
2021-03-26 08:49:51 +00:00
|
|
|
|
waku_peers_dials.inc(labelValues = ["successful"])
|
2022-12-14 15:04:11 +00:00
|
|
|
|
waku_node_conns_initiated.inc(labelValues = [source])
|
2023-01-23 20:24:46 +00:00
|
|
|
|
pm.peerStore[NumberFailedConnBook][peerId] = 0
|
2023-03-28 11:29:48 +00:00
|
|
|
|
return true
|
2021-03-26 08:49:51 +00:00
|
|
|
|
else:
|
2023-03-28 11:29:48 +00:00
|
|
|
|
reasonFailed = "timed out"
|
|
|
|
|
await cancelAndWait(workfut)
|
2023-01-23 20:24:46 +00:00
|
|
|
|
except CatchableError as exc:
|
2023-03-28 11:29:48 +00:00
|
|
|
|
reasonFailed = "remote peer failed"
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
2023-01-23 20:24:46 +00:00
|
|
|
|
# Dial failed
|
|
|
|
|
pm.peerStore[NumberFailedConnBook][peerId] = pm.peerStore[NumberFailedConnBook][peerId] + 1
|
|
|
|
|
pm.peerStore[LastFailedConnBook][peerId] = Moment.init(getTime().toUnix, Second)
|
|
|
|
|
pm.peerStore[ConnectionBook][peerId] = CannotConnect
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
debug "Connecting relay peer failed",
|
2023-01-26 09:20:20 +00:00
|
|
|
|
peerId = peerId,
|
|
|
|
|
reason = reasonFailed,
|
|
|
|
|
failedAttempts = pm.peerStore[NumberFailedConnBook][peerId]
|
2023-01-23 20:24:46 +00:00
|
|
|
|
waku_peers_dials.inc(labelValues = [reasonFailed])
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
# Dialing should be used for just protocols that require a stream to write and read
|
|
|
|
|
# This shall not be used to dial Relay protocols, since that would create
|
|
|
|
|
# unneccesary unused streams.
|
|
|
|
|
proc dialPeer(pm: PeerManager,
|
|
|
|
|
peerId: PeerID,
|
|
|
|
|
addrs: seq[MultiAddress],
|
|
|
|
|
proto: string,
|
|
|
|
|
dialTimeout = DefaultDialTimeout,
|
|
|
|
|
source = "api"): Future[Option[Connection]] {.async.} =
|
|
|
|
|
|
|
|
|
|
if peerId == pm.switch.peerInfo.peerId:
|
|
|
|
|
error "could not dial self"
|
|
|
|
|
return none(Connection)
|
|
|
|
|
|
|
|
|
|
if proto == WakuRelayCodec:
|
|
|
|
|
error "dial shall not be used to connect to relays"
|
|
|
|
|
return none(Connection)
|
|
|
|
|
|
|
|
|
|
debug "Dialing peer", wireAddr=addrs, peerId=peerId, proto=proto
|
|
|
|
|
|
|
|
|
|
# Dial Peer
|
|
|
|
|
let dialFut = pm.switch.dial(peerId, addrs, proto)
|
|
|
|
|
var reasonFailed = ""
|
|
|
|
|
try:
|
|
|
|
|
if (await dialFut.withTimeout(dialTimeout)):
|
|
|
|
|
return some(dialFut.read())
|
|
|
|
|
else:
|
|
|
|
|
reasonFailed = "timeout"
|
|
|
|
|
await cancelAndWait(dialFut)
|
|
|
|
|
except CatchableError as exc:
|
|
|
|
|
reasonFailed = "failed"
|
|
|
|
|
|
|
|
|
|
debug "Dialing peer failed", peerId=peerId, reason=reasonFailed, proto=proto
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-01-23 20:24:46 +00:00
|
|
|
|
return none(Connection)
|
|
|
|
|
|
2021-07-14 17:58:46 +00:00
|
|
|
|
proc loadFromStorage(pm: PeerManager) =
|
2021-07-27 06:48:56 +00:00
|
|
|
|
debug "loading peers from storage"
|
2021-03-26 08:49:51 +00:00
|
|
|
|
# Load peers from storage, if available
|
2023-10-11 06:58:45 +00:00
|
|
|
|
var amount = 0
|
2023-03-09 18:05:50 +00:00
|
|
|
|
proc onData(peerId: PeerID, remotePeerInfo: RemotePeerInfo, connectedness: Connectedness, disconnectTime: int64) =
|
|
|
|
|
trace "loading peer", peerId=peerId, connectedness=connectedness
|
2021-07-27 06:48:56 +00:00
|
|
|
|
|
2021-04-16 09:57:45 +00:00
|
|
|
|
if peerId == pm.switch.peerInfo.peerId:
|
|
|
|
|
# Do not manage self
|
|
|
|
|
return
|
|
|
|
|
|
2022-11-24 13:11:23 +00:00
|
|
|
|
# nim-libp2p books
|
2023-03-09 18:05:50 +00:00
|
|
|
|
pm.peerStore[AddressBook][peerId] = remotePeerInfo.addrs
|
|
|
|
|
pm.peerStore[ProtoBook][peerId] = remotePeerInfo.protocols
|
|
|
|
|
pm.peerStore[KeyBook][peerId] = remotePeerInfo.publicKey
|
|
|
|
|
pm.peerStore[AgentBook][peerId] = remotePeerInfo.agent
|
|
|
|
|
pm.peerStore[ProtoVersionBook][peerId] = remotePeerInfo.protoVersion
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
|
|
|
|
# custom books
|
|
|
|
|
pm.peerStore[ConnectionBook][peerId] = NotConnected # Reset connectedness state
|
|
|
|
|
pm.peerStore[DisconnectBook][peerId] = disconnectTime
|
2023-03-09 18:05:50 +00:00
|
|
|
|
pm.peerStore[SourceBook][peerId] = remotePeerInfo.origin
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-10-11 06:58:45 +00:00
|
|
|
|
amount.inc()
|
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
let res = pm.storage.getAll(onData)
|
|
|
|
|
if res.isErr:
|
|
|
|
|
warn "failed to load peers from storage", err = res.error
|
|
|
|
|
waku_peers_errors.inc(labelValues = ["storage_load_failure"])
|
2023-10-11 06:58:45 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
debug "successfully queried peer storage", amount = amount
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-04-14 13:12:22 +00:00
|
|
|
|
proc canBeConnected*(pm: PeerManager,
|
|
|
|
|
peerId: PeerId): bool =
|
|
|
|
|
# Returns if we can try to connect to this peer, based on past failed attempts
|
|
|
|
|
# It uses an exponential backoff. Each connection attempt makes us
|
|
|
|
|
# wait more before trying again.
|
|
|
|
|
let failedAttempts = pm.peerStore[NumberFailedConnBook][peerId]
|
|
|
|
|
|
|
|
|
|
# if it never errored, we can try to connect
|
|
|
|
|
if failedAttempts == 0:
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
# if there are too many failed attempts, do not reconnect
|
|
|
|
|
if failedAttempts >= pm.maxFailedAttempts:
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
# If it errored we wait an exponential backoff from last connection
|
|
|
|
|
# the more failed attempts, the greater the backoff since last attempt
|
|
|
|
|
let now = Moment.init(getTime().toUnix, Second)
|
|
|
|
|
let lastFailed = pm.peerStore[LastFailedConnBook][peerId]
|
|
|
|
|
let backoff = calculateBackoff(pm.initialBackoffInSec, pm.backoffFactor, failedAttempts)
|
|
|
|
|
if now >= (lastFailed + backoff):
|
|
|
|
|
return true
|
|
|
|
|
return false
|
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
##################
|
|
|
|
|
# Initialisation #
|
2022-11-24 13:11:23 +00:00
|
|
|
|
##################
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
2023-06-28 07:14:11 +00:00
|
|
|
|
proc getPeerIp(pm: PeerManager, peerId: PeerId): Option[string] =
|
|
|
|
|
if pm.switch.connManager.getConnections().hasKey(peerId):
|
|
|
|
|
let conns = pm.switch.connManager.getConnections().getOrDefault(peerId)
|
|
|
|
|
if conns.len != 0:
|
|
|
|
|
let observedAddr = conns[0].connection.observedAddr
|
|
|
|
|
let ip = observedAddr.get.getHostname()
|
|
|
|
|
if observedAddr.isSome:
|
|
|
|
|
# TODO: think if circuit relay ips should be handled differently
|
|
|
|
|
let ip = observedAddr.get.getHostname()
|
|
|
|
|
return some(ip)
|
|
|
|
|
return none(string)
|
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
# called when a connection i) is created or ii) is closed
|
2021-02-12 08:53:52 +00:00
|
|
|
|
proc onConnEvent(pm: PeerManager, peerId: PeerID, event: ConnEvent) {.async.} =
|
|
|
|
|
case event.kind
|
|
|
|
|
of ConnEventKind.Connected:
|
2022-11-29 16:35:25 +00:00
|
|
|
|
let direction = if event.incoming: Inbound else: Outbound
|
2023-02-14 14:38:32 +00:00
|
|
|
|
discard
|
|
|
|
|
of ConnEventKind.Disconnected:
|
|
|
|
|
discard
|
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
# called when a peer i) first connects to us ii) disconnects all connections from us
|
2023-02-14 14:38:32 +00:00
|
|
|
|
proc onPeerEvent(pm: PeerManager, peerId: PeerId, event: PeerEvent) {.async.} =
|
2023-05-31 07:47:56 +00:00
|
|
|
|
var direction: PeerDirection
|
|
|
|
|
var connectedness: Connectedness
|
|
|
|
|
|
2023-02-14 14:38:32 +00:00
|
|
|
|
if event.kind == PeerEventKind.Joined:
|
2023-05-31 07:47:56 +00:00
|
|
|
|
direction = if event.initiator: Outbound else: Inbound
|
|
|
|
|
connectedness = Connected
|
2023-06-28 07:14:11 +00:00
|
|
|
|
|
2023-10-11 06:58:45 +00:00
|
|
|
|
var clusterOk = false
|
|
|
|
|
var reason = ""
|
|
|
|
|
# To prevent metadata protocol from breaking prev nodes, by now we only
|
|
|
|
|
# disconnect if the clusterid is specified.
|
|
|
|
|
if not pm.wakuMetadata.isNil() and pm.wakuMetadata.clusterId != 0:
|
|
|
|
|
block wakuMetadata:
|
|
|
|
|
var conn: Connection
|
|
|
|
|
try:
|
|
|
|
|
conn = await pm.switch.dial(peerId, WakuMetadataCodec)
|
|
|
|
|
except CatchableError:
|
|
|
|
|
reason = "waku metadata codec not supported"
|
|
|
|
|
break wakuMetadata
|
|
|
|
|
|
|
|
|
|
# request metadata from connecting peer
|
|
|
|
|
let metadata = (await pm.wakuMetadata.request(conn)).valueOr:
|
|
|
|
|
reason = "failed waku metadata codec request"
|
|
|
|
|
break wakuMetadata
|
|
|
|
|
|
|
|
|
|
# does not report any clusterId
|
|
|
|
|
let clusterId = metadata.clusterId.valueOr:
|
|
|
|
|
reason = "empty clusterId reported"
|
|
|
|
|
break wakuMetadata
|
|
|
|
|
|
|
|
|
|
# drop it if it doesnt match our network id
|
|
|
|
|
if pm.wakuMetadata.clusterId != clusterId:
|
|
|
|
|
reason = "different clusterId reported: " & $pm.wakuMetadata.clusterId & " vs " & $clusterId
|
|
|
|
|
break wakuMetadata
|
|
|
|
|
|
|
|
|
|
# reaching here means the clusterId matches
|
|
|
|
|
clusterOk = true
|
|
|
|
|
|
|
|
|
|
if not pm.wakuMetadata.isNil() and pm.wakuMetadata.clusterId != 0 and not clusterOk:
|
|
|
|
|
info "disconnecting from peer", peerId=peerId, reason=reason
|
|
|
|
|
asyncSpawn(pm.switch.disconnect(peerId))
|
|
|
|
|
pm.peerStore.delete(peerId)
|
|
|
|
|
|
|
|
|
|
# TODO: Take action depending on the supported shards as reported by metadata
|
|
|
|
|
|
2023-06-28 07:14:11 +00:00
|
|
|
|
let ip = pm.getPeerIp(peerId)
|
|
|
|
|
if ip.isSome:
|
|
|
|
|
pm.ipTable.mgetOrPut(ip.get, newSeq[PeerId]()).add(peerId)
|
|
|
|
|
|
|
|
|
|
let peersBehindIp = pm.ipTable[ip.get]
|
|
|
|
|
if peersBehindIp.len > pm.colocationLimit:
|
|
|
|
|
# in theory this should always be one, but just in case
|
|
|
|
|
for peerId in peersBehindIp[0..<(peersBehindIp.len - pm.colocationLimit)]:
|
|
|
|
|
debug "Pruning connection due to ip colocation", peerId = peerId, ip = ip
|
|
|
|
|
asyncSpawn(pm.switch.disconnect(peerId))
|
|
|
|
|
pm.peerStore.delete(peerId)
|
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
elif event.kind == PeerEventKind.Left:
|
|
|
|
|
direction = UnknownDirection
|
|
|
|
|
connectedness = CanConnect
|
2022-11-29 16:35:25 +00:00
|
|
|
|
|
2023-06-28 07:14:11 +00:00
|
|
|
|
# note we cant access the peerId ip here as the connection was already closed
|
|
|
|
|
for ip, peerIds in pm.ipTable.pairs:
|
|
|
|
|
if peerIds.contains(peerId):
|
|
|
|
|
pm.ipTable[ip] = pm.ipTable[ip].filterIt(it != peerId)
|
|
|
|
|
if pm.ipTable[ip].len == 0:
|
|
|
|
|
pm.ipTable.del(ip)
|
|
|
|
|
break
|
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
pm.peerStore[ConnectionBook][peerId] = connectedness
|
|
|
|
|
pm.peerStore[DirectionBook][peerId] = direction
|
|
|
|
|
if not pm.storage.isNil:
|
|
|
|
|
pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), connectedness, getTime().toUnix)
|
2022-11-29 16:35:25 +00:00
|
|
|
|
|
2023-01-23 20:24:46 +00:00
|
|
|
|
proc new*(T: type PeerManager,
|
|
|
|
|
switch: Switch,
|
2023-10-11 06:58:45 +00:00
|
|
|
|
wakuMetadata: WakuMetadata = nil,
|
2023-07-04 11:31:18 +00:00
|
|
|
|
maxRelayPeers: Option[int] = none(int),
|
2023-01-23 20:24:46 +00:00
|
|
|
|
storage: PeerStorage = nil,
|
|
|
|
|
initialBackoffInSec = InitialBackoffInSec,
|
|
|
|
|
backoffFactor = BackoffFactor,
|
2023-05-31 07:47:56 +00:00
|
|
|
|
maxFailedAttempts = MaxFailedAttempts,
|
|
|
|
|
colocationLimit = ColocationLimit,): PeerManager =
|
2023-01-23 20:24:46 +00:00
|
|
|
|
|
2023-01-31 12:24:49 +00:00
|
|
|
|
let capacity = switch.peerStore.capacity
|
|
|
|
|
let maxConnections = switch.connManager.inSema.size
|
|
|
|
|
if maxConnections > capacity:
|
|
|
|
|
error "Max number of connections can't be greater than PeerManager capacity",
|
|
|
|
|
capacity = capacity,
|
|
|
|
|
maxConnections = maxConnections
|
|
|
|
|
raise newException(Defect, "Max number of connections can't be greater than PeerManager capacity")
|
|
|
|
|
|
2023-07-04 11:31:18 +00:00
|
|
|
|
var maxRelayPeersValue = 0
|
|
|
|
|
if maxRelayPeers.isSome():
|
|
|
|
|
if maxRelayPeers.get() > maxConnections:
|
|
|
|
|
error "Max number of relay peers can't be greater than the max amount of connections",
|
|
|
|
|
maxConnections = maxConnections,
|
|
|
|
|
maxRelayPeers = maxRelayPeers.get()
|
|
|
|
|
raise newException(Defect, "Max number of relay peers can't be greater than the max amount of connections")
|
|
|
|
|
|
|
|
|
|
if maxRelayPeers.get() == maxConnections:
|
|
|
|
|
warn "Max number of relay peers is equal to max amount of connections, peer won't be contributing to service peers",
|
|
|
|
|
maxConnections = maxConnections,
|
|
|
|
|
maxRelayPeers = maxRelayPeers.get()
|
|
|
|
|
maxRelayPeersValue = maxRelayPeers.get()
|
|
|
|
|
else:
|
|
|
|
|
# Leave by default 20% of connections for service peers
|
|
|
|
|
maxRelayPeersValue = maxConnections - (maxConnections div 5)
|
2023-06-23 13:30:28 +00:00
|
|
|
|
|
2023-04-14 13:12:22 +00:00
|
|
|
|
# attempt to calculate max backoff to prevent potential overflows or unreasonably high values
|
|
|
|
|
let backoff = calculateBackoff(initialBackoffInSec, backoffFactor, maxFailedAttempts)
|
|
|
|
|
if backoff.weeks() > 1:
|
|
|
|
|
error "Max backoff time can't be over 1 week",
|
|
|
|
|
maxBackoff=backoff
|
|
|
|
|
raise newException(Defect, "Max backoff time can't be over 1 week")
|
|
|
|
|
|
2023-07-04 11:31:18 +00:00
|
|
|
|
let outRelayPeersTarget = max(maxRelayPeersValue div 3, 10)
|
|
|
|
|
|
2021-02-12 08:53:52 +00:00
|
|
|
|
let pm = PeerManager(switch: switch,
|
2023-10-11 06:58:45 +00:00
|
|
|
|
wakuMetadata: wakuMetadata,
|
2022-11-24 13:11:23 +00:00
|
|
|
|
peerStore: switch.peerStore,
|
2023-01-23 20:24:46 +00:00
|
|
|
|
storage: storage,
|
|
|
|
|
initialBackoffInSec: initialBackoffInSec,
|
|
|
|
|
backoffFactor: backoffFactor,
|
2023-07-04 11:31:18 +00:00
|
|
|
|
outRelayPeersTarget: outRelayPeersTarget,
|
|
|
|
|
inRelayPeersTarget: maxRelayPeersValue - outRelayPeersTarget,
|
|
|
|
|
maxRelayPeers: maxRelayPeersValue,
|
2023-05-31 07:47:56 +00:00
|
|
|
|
maxFailedAttempts: maxFailedAttempts,
|
2023-07-04 11:31:18 +00:00
|
|
|
|
colocationLimit: colocationLimit)
|
2023-04-14 13:12:22 +00:00
|
|
|
|
|
2023-02-14 14:38:32 +00:00
|
|
|
|
proc connHook(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.} =
|
2021-10-06 12:29:08 +00:00
|
|
|
|
onConnEvent(pm, peerId, event)
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-02-14 14:38:32 +00:00
|
|
|
|
proc peerHook(peerId: PeerId, event: PeerEvent): Future[void] {.gcsafe.} =
|
|
|
|
|
onPeerEvent(pm, peerId, event)
|
|
|
|
|
|
2023-01-31 12:24:49 +00:00
|
|
|
|
proc peerStoreChanged(peerId: PeerId) {.gcsafe.} =
|
|
|
|
|
waku_peer_store_size.set(toSeq(pm.peerStore[AddressBook].book.keys).len.int64)
|
|
|
|
|
|
2023-02-14 14:38:32 +00:00
|
|
|
|
# currently disabled
|
|
|
|
|
#pm.switch.addConnEventHandler(connHook, ConnEventKind.Connected)
|
|
|
|
|
#pm.switch.addConnEventHandler(connHook, ConnEventKind.Disconnected)
|
|
|
|
|
|
|
|
|
|
pm.switch.addPeerEventHandler(peerHook, PeerEventKind.Joined)
|
|
|
|
|
pm.switch.addPeerEventHandler(peerHook, PeerEventKind.Left)
|
2021-02-12 08:53:52 +00:00
|
|
|
|
|
2023-01-31 12:24:49 +00:00
|
|
|
|
# called every time the peerstore is updated
|
|
|
|
|
pm.peerStore[AddressBook].addHandler(peerStoreChanged)
|
|
|
|
|
|
2023-01-26 09:20:20 +00:00
|
|
|
|
pm.serviceSlots = initTable[string, RemotePeerInfo]()
|
2023-05-31 07:47:56 +00:00
|
|
|
|
pm.ipTable = initTable[string, seq[PeerId]]()
|
2023-01-26 09:20:20 +00:00
|
|
|
|
|
2022-11-03 15:36:24 +00:00
|
|
|
|
if not storage.isNil():
|
2021-07-27 06:48:56 +00:00
|
|
|
|
debug "found persistent peer storage"
|
2021-03-26 08:49:51 +00:00
|
|
|
|
pm.loadFromStorage() # Load previously managed peers.
|
2021-07-27 06:48:56 +00:00
|
|
|
|
else:
|
|
|
|
|
debug "no peer storage found"
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2021-02-12 08:53:52 +00:00
|
|
|
|
return pm
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
2021-02-11 08:58:25 +00:00
|
|
|
|
#####################
|
|
|
|
|
# Manager interface #
|
|
|
|
|
#####################
|
|
|
|
|
|
2023-01-26 09:20:20 +00:00
|
|
|
|
proc addServicePeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, proto: string) =
|
|
|
|
|
# Do not add relay peers
|
|
|
|
|
if proto == WakuRelayCodec:
|
|
|
|
|
warn "Can't add relay peer to service peers slots"
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
info "Adding peer to service slots", peerId = remotePeerInfo.peerId, addr = remotePeerInfo.addrs[0], service = proto
|
2023-02-27 17:24:31 +00:00
|
|
|
|
waku_service_peers.set(1, labelValues = [$proto, $remotePeerInfo.addrs[0]])
|
2023-01-26 09:20:20 +00:00
|
|
|
|
|
|
|
|
|
# Set peer for service slot
|
|
|
|
|
pm.serviceSlots[proto] = remotePeerInfo
|
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
pm.addPeer(remotePeerInfo)
|
2023-01-26 09:20:20 +00:00
|
|
|
|
|
2021-07-27 06:48:56 +00:00
|
|
|
|
proc reconnectPeers*(pm: PeerManager,
|
|
|
|
|
proto: string,
|
|
|
|
|
backoff: chronos.Duration = chronos.seconds(0)) {.async.} =
|
2021-03-26 08:49:51 +00:00
|
|
|
|
## Reconnect to peers registered for this protocol. This will update connectedness.
|
|
|
|
|
## Especially useful to resume connections from persistent storage after a restart.
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
debug "Reconnecting peers", proto=proto
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
# Proto is not persisted, we need to iterate over all peers.
|
2023-03-28 11:29:48 +00:00
|
|
|
|
for peerInfo in pm.peerStore.peers(protocolMatcher(proto)):
|
2022-11-24 13:11:23 +00:00
|
|
|
|
# Check that the peer can be connected
|
2023-03-28 11:29:48 +00:00
|
|
|
|
if peerInfo.connectedness == CannotConnect:
|
|
|
|
|
debug "Not reconnecting to unreachable or non-existing peer", peerId=peerInfo.peerId
|
2021-04-21 09:36:56 +00:00
|
|
|
|
continue
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2021-04-21 09:36:56 +00:00
|
|
|
|
# Respect optional backoff period where applicable.
|
|
|
|
|
let
|
2022-11-24 13:11:23 +00:00
|
|
|
|
# TODO: Add method to peerStore (eg isBackoffExpired())
|
2023-03-28 11:29:48 +00:00
|
|
|
|
disconnectTime = Moment.init(peerInfo.disconnectTime, Second) # Convert
|
2021-04-21 09:36:56 +00:00
|
|
|
|
currentTime = Moment.init(getTime().toUnix, Second) # Current time comparable to persisted value
|
|
|
|
|
backoffTime = disconnectTime + backoff - currentTime # Consider time elapsed since last disconnect
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2021-04-21 09:36:56 +00:00
|
|
|
|
trace "Respecting backoff", backoff=backoff, disconnectTime=disconnectTime, currentTime=currentTime, backoffTime=backoffTime
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
|
|
|
|
# TODO: This blocks the whole function. Try to connect to another peer in the meantime.
|
2021-04-21 09:36:56 +00:00
|
|
|
|
if backoffTime > ZeroDuration:
|
2023-03-28 11:29:48 +00:00
|
|
|
|
debug "Backing off before reconnect...", peerId=peerInfo.peerId, backoffTime=backoffTime
|
2021-04-21 09:36:56 +00:00
|
|
|
|
# We disconnected recently and still need to wait for a backoff period before connecting
|
|
|
|
|
await sleepAsync(backoffTime)
|
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
discard await pm.connectRelay(peerInfo)
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
2021-02-08 09:17:20 +00:00
|
|
|
|
####################
|
|
|
|
|
# Dialer interface #
|
|
|
|
|
####################
|
|
|
|
|
|
2022-12-14 15:04:11 +00:00
|
|
|
|
proc dialPeer*(pm: PeerManager,
|
|
|
|
|
remotePeerInfo: RemotePeerInfo,
|
|
|
|
|
proto: string,
|
|
|
|
|
dialTimeout = DefaultDialTimeout,
|
2023-01-23 20:24:46 +00:00
|
|
|
|
source = "api",
|
|
|
|
|
): Future[Option[Connection]] {.async.} =
|
2021-02-08 09:17:20 +00:00
|
|
|
|
# Dial a given peer and add it to the list of known peers
|
2022-11-04 08:40:13 +00:00
|
|
|
|
# TODO: check peer validity and score before continuing. Limit number of peers to be managed.
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
# First add dialed peer info to peer store, if it does not exist yet..
|
|
|
|
|
# TODO: nim libp2p peerstore already adds them
|
2022-11-24 13:11:23 +00:00
|
|
|
|
if not pm.peerStore.hasPeer(remotePeerInfo.peerId, proto):
|
2022-12-07 11:30:32 +00:00
|
|
|
|
trace "Adding newly dialed peer to manager", peerId= $remotePeerInfo.peerId, address= $remotePeerInfo.addrs[0], proto= proto
|
2023-02-27 17:24:31 +00:00
|
|
|
|
pm.addPeer(remotePeerInfo)
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2022-12-14 15:04:11 +00:00
|
|
|
|
return await pm.dialPeer(remotePeerInfo.peerId,remotePeerInfo.addrs, proto, dialTimeout, source)
|
2021-10-06 12:29:08 +00:00
|
|
|
|
|
2022-12-14 15:04:11 +00:00
|
|
|
|
proc dialPeer*(pm: PeerManager,
|
|
|
|
|
peerId: PeerID,
|
|
|
|
|
proto: string,
|
|
|
|
|
dialTimeout = DefaultDialTimeout,
|
2023-01-23 20:24:46 +00:00
|
|
|
|
source = "api",
|
2022-12-14 15:04:11 +00:00
|
|
|
|
): Future[Option[Connection]] {.async.} =
|
2021-10-06 12:29:08 +00:00
|
|
|
|
# Dial an existing peer by looking up it's existing addrs in the switch's peerStore
|
2022-11-04 08:40:13 +00:00
|
|
|
|
# TODO: check peer validity and score before continuing. Limit number of peers to be managed.
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2022-06-01 09:49:41 +00:00
|
|
|
|
let addrs = pm.switch.peerStore[AddressBook][peerId]
|
2022-12-14 15:04:11 +00:00
|
|
|
|
return await pm.dialPeer(peerId, addrs, proto, dialTimeout, source)
|
2021-10-06 12:29:08 +00:00
|
|
|
|
|
2022-12-14 15:04:11 +00:00
|
|
|
|
proc connectToNodes*(pm: PeerManager,
|
|
|
|
|
nodes: seq[string]|seq[RemotePeerInfo],
|
|
|
|
|
dialTimeout = DefaultDialTimeout,
|
|
|
|
|
source = "api") {.async.} =
|
2023-01-23 20:24:46 +00:00
|
|
|
|
if nodes.len == 0:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
info "Dialing multiple peers", numOfPeers = nodes.len
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
var futConns: seq[Future[bool]]
|
2022-12-14 15:04:11 +00:00
|
|
|
|
for node in nodes:
|
2023-04-12 09:29:11 +00:00
|
|
|
|
let node = parsePeerInfo(node)
|
|
|
|
|
if node.isOk():
|
|
|
|
|
futConns.add(pm.connectRelay(node.value))
|
|
|
|
|
else:
|
|
|
|
|
error "Couldn't parse node info", error = node.error
|
2023-01-09 20:45:50 +00:00
|
|
|
|
|
|
|
|
|
await allFutures(futConns)
|
2023-04-17 09:46:15 +00:00
|
|
|
|
let successfulConns = futConns.mapIt(it.read()).countIt(it == true)
|
2023-01-23 20:24:46 +00:00
|
|
|
|
|
|
|
|
|
info "Finished dialing multiple peers", successfulConns=successfulConns, attempted=nodes.len
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
# The issue seems to be around peers not being fully connected when
|
|
|
|
|
# trying to subscribe. So what we do is sleep to guarantee nodes are
|
|
|
|
|
# fully connected.
|
|
|
|
|
#
|
|
|
|
|
# This issue was known to Dmitiry on nim-libp2p and may be resolvable
|
|
|
|
|
# later.
|
|
|
|
|
await sleepAsync(chronos.seconds(5))
|
2023-01-18 14:17:56 +00:00
|
|
|
|
|
2023-05-18 07:40:14 +00:00
|
|
|
|
# Returns the peerIds of physical connections (in and out)
|
2023-04-12 11:05:34 +00:00
|
|
|
|
# containing at least one stream with the given protocol.
|
2023-05-18 07:40:14 +00:00
|
|
|
|
proc connectedPeers*(pm: PeerManager, protocol: string): (seq[PeerId], seq[PeerId]) =
|
|
|
|
|
var inPeers: seq[PeerId]
|
|
|
|
|
var outPeers: seq[PeerId]
|
|
|
|
|
|
2023-04-12 11:05:34 +00:00
|
|
|
|
for peerId, muxers in pm.switch.connManager.getConnections():
|
|
|
|
|
for peerConn in muxers:
|
|
|
|
|
let streams = peerConn.getStreams()
|
2023-04-26 08:47:46 +00:00
|
|
|
|
if streams.anyIt(it.protocol == protocol):
|
|
|
|
|
if peerConn.connection.transportDir == Direction.In:
|
2023-05-18 07:40:14 +00:00
|
|
|
|
inPeers.add(peerId)
|
2023-04-26 08:47:46 +00:00
|
|
|
|
elif peerConn.connection.transportDir == Direction.Out:
|
2023-05-18 07:40:14 +00:00
|
|
|
|
outPeers.add(peerId)
|
2023-04-26 08:47:46 +00:00
|
|
|
|
|
2023-05-18 07:40:14 +00:00
|
|
|
|
return (inPeers, outPeers)
|
2023-04-26 08:47:46 +00:00
|
|
|
|
|
|
|
|
|
proc getNumStreams*(pm: PeerManager, protocol: string): (int, int) =
|
|
|
|
|
var
|
|
|
|
|
numStreamsIn = 0
|
|
|
|
|
numStreamsOut = 0
|
2023-04-12 11:05:34 +00:00
|
|
|
|
for peerId, muxers in pm.switch.connManager.getConnections():
|
|
|
|
|
for peerConn in muxers:
|
|
|
|
|
for stream in peerConn.getStreams():
|
2023-04-26 08:47:46 +00:00
|
|
|
|
if stream.protocol == protocol:
|
|
|
|
|
if stream.dir == Direction.In:
|
|
|
|
|
numStreamsIn += 1
|
|
|
|
|
elif stream.dir == Direction.Out:
|
|
|
|
|
numStreamsOut += 1
|
|
|
|
|
return (numStreamsIn, numStreamsOut)
|
2023-04-12 11:05:34 +00:00
|
|
|
|
|
2023-05-18 07:40:14 +00:00
|
|
|
|
proc pruneInRelayConns(pm: PeerManager, amount: int) {.async.} =
|
|
|
|
|
let (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec)
|
|
|
|
|
let connsToPrune = min(amount, inRelayPeers.len)
|
|
|
|
|
|
|
|
|
|
for p in inRelayPeers[0..<connsToPrune]:
|
2023-06-28 07:14:11 +00:00
|
|
|
|
asyncSpawn(pm.switch.disconnect(p))
|
2023-05-31 07:47:56 +00:00
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
proc connectToRelayPeers*(pm: PeerManager) {.async.} =
|
2023-05-18 07:40:14 +00:00
|
|
|
|
let (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec)
|
2023-02-27 17:24:31 +00:00
|
|
|
|
let maxConnections = pm.switch.connManager.inSema.size
|
2023-05-18 07:40:14 +00:00
|
|
|
|
let totalRelayPeers = inRelayPeers.len + outRelayPeers.len
|
2023-07-04 11:31:18 +00:00
|
|
|
|
let inPeersTarget = maxConnections - pm.outRelayPeersTarget
|
2023-05-18 07:40:14 +00:00
|
|
|
|
|
2023-09-08 11:36:26 +00:00
|
|
|
|
# TODO: Temporally disabled. Might be causing connection issues
|
|
|
|
|
#if inRelayPeers.len > pm.inRelayPeersTarget:
|
|
|
|
|
# await pm.pruneInRelayConns(inRelayPeers.len - pm.inRelayPeersTarget)
|
2023-01-18 14:17:56 +00:00
|
|
|
|
|
2023-07-04 11:31:18 +00:00
|
|
|
|
if outRelayPeers.len >= pm.outRelayPeersTarget:
|
2023-02-27 17:24:31 +00:00
|
|
|
|
return
|
2023-01-18 14:17:56 +00:00
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
let notConnectedPeers = pm.peerStore.getNotConnectedPeers().mapIt(RemotePeerInfo.init(it.peerId, it.addrs))
|
2023-04-14 13:12:22 +00:00
|
|
|
|
let outsideBackoffPeers = notConnectedPeers.filterIt(pm.canBeConnected(it.peerId))
|
2023-09-08 11:36:26 +00:00
|
|
|
|
let numPeersToConnect = min(outsideBackoffPeers.len, MaxParalelDials)
|
2023-01-18 14:17:56 +00:00
|
|
|
|
|
2023-03-28 11:29:48 +00:00
|
|
|
|
await pm.connectToNodes(outsideBackoffPeers[0..<numPeersToConnect])
|
2023-01-26 09:20:20 +00:00
|
|
|
|
|
2023-01-31 12:24:49 +00:00
|
|
|
|
proc prunePeerStore*(pm: PeerManager) =
|
|
|
|
|
let numPeers = toSeq(pm.peerStore[AddressBook].book.keys).len
|
|
|
|
|
let capacity = pm.peerStore.capacity
|
|
|
|
|
if numPeers < capacity:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
debug "Peer store capacity exceeded", numPeers = numPeers, capacity = capacity
|
|
|
|
|
let peersToPrune = numPeers - capacity
|
|
|
|
|
|
|
|
|
|
# prune peers with too many failed attempts
|
|
|
|
|
var pruned = 0
|
2023-02-13 17:10:20 +00:00
|
|
|
|
# copy to avoid modifying the book while iterating
|
|
|
|
|
let peerKeys = toSeq(pm.peerStore[NumberFailedConnBook].book.keys)
|
|
|
|
|
for peerId in peerKeys:
|
2023-01-31 12:24:49 +00:00
|
|
|
|
if peersToPrune - pruned == 0:
|
|
|
|
|
break
|
|
|
|
|
if pm.peerStore[NumberFailedConnBook][peerId] >= pm.maxFailedAttempts:
|
|
|
|
|
pm.peerStore.del(peerId)
|
|
|
|
|
pruned += 1
|
|
|
|
|
|
|
|
|
|
# if we still need to prune, prune peers that are not connected
|
|
|
|
|
let notConnected = pm.peerStore.getNotConnectedPeers().mapIt(it.peerId)
|
|
|
|
|
for peerId in notConnected:
|
|
|
|
|
if peersToPrune - pruned == 0:
|
|
|
|
|
break
|
|
|
|
|
pm.peerStore.del(peerId)
|
|
|
|
|
pruned += 1
|
|
|
|
|
|
|
|
|
|
let afterNumPeers = toSeq(pm.peerStore[AddressBook].book.keys).len
|
|
|
|
|
debug "Finished pruning peer store", beforeNumPeers = numPeers,
|
|
|
|
|
afterNumPeers = afterNumPeers,
|
|
|
|
|
capacity = capacity,
|
|
|
|
|
pruned = pruned
|
|
|
|
|
|
2023-09-22 19:13:50 +00:00
|
|
|
|
proc selectPeer*(pm: PeerManager, proto: string, shard: Option[PubsubTopic] = none(PubsubTopic)): Option[RemotePeerInfo] =
|
2023-01-26 09:20:20 +00:00
|
|
|
|
debug "Selecting peer from peerstore", protocol=proto
|
|
|
|
|
|
|
|
|
|
# Selects the best peer for a given protocol
|
2023-09-22 19:13:50 +00:00
|
|
|
|
var peers = pm.peerStore.getPeersByProtocol(proto)
|
|
|
|
|
|
|
|
|
|
if shard.isSome():
|
|
|
|
|
peers.keepItIf((it.enr.isSome() and it.enr.get().containsShard(shard.get())))
|
2023-01-26 09:20:20 +00:00
|
|
|
|
|
|
|
|
|
# No criteria for selecting a peer for WakuRelay, random one
|
|
|
|
|
if proto == WakuRelayCodec:
|
|
|
|
|
# TODO: proper heuristic here that compares peer scores and selects "best" one. For now the first peer for the given protocol is returned
|
|
|
|
|
if peers.len > 0:
|
|
|
|
|
debug "Got peer from peerstore", peerId=peers[0].peerId, multi=peers[0].addrs[0], protocol=proto
|
2023-03-09 18:05:50 +00:00
|
|
|
|
return some(peers[0])
|
2023-01-26 09:20:20 +00:00
|
|
|
|
debug "No peer found for protocol", protocol=proto
|
|
|
|
|
return none(RemotePeerInfo)
|
|
|
|
|
|
|
|
|
|
# For other protocols, we select the peer that is slotted for the given protocol
|
|
|
|
|
pm.serviceSlots.withValue(proto, serviceSlot):
|
|
|
|
|
debug "Got peer from service slots", peerId=serviceSlot[].peerId, multi=serviceSlot[].addrs[0], protocol=proto
|
|
|
|
|
return some(serviceSlot[])
|
|
|
|
|
|
|
|
|
|
# If not slotted, we select a random peer for the given protocol
|
|
|
|
|
if peers.len > 0:
|
|
|
|
|
debug "Got peer from peerstore", peerId=peers[0].peerId, multi=peers[0].addrs[0], protocol=proto
|
2023-03-09 18:05:50 +00:00
|
|
|
|
return some(peers[0])
|
2023-01-26 09:20:20 +00:00
|
|
|
|
debug "No peer found for protocol", protocol=proto
|
|
|
|
|
return none(RemotePeerInfo)
|
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
|
# Prunes peers from peerstore to remove old/stale ones
|
|
|
|
|
proc prunePeerStoreLoop(pm: PeerManager) {.async.} =
|
|
|
|
|
debug "Starting prune peerstore loop"
|
|
|
|
|
while pm.started:
|
|
|
|
|
pm.prunePeerStore()
|
|
|
|
|
await sleepAsync(PrunePeerStoreInterval)
|
|
|
|
|
|
|
|
|
|
# Ensures a healthy amount of connected relay peers
|
|
|
|
|
proc relayConnectivityLoop*(pm: PeerManager) {.async.} =
|
|
|
|
|
debug "Starting relay connectivity loop"
|
|
|
|
|
while pm.started:
|
|
|
|
|
await pm.connectToRelayPeers()
|
|
|
|
|
await sleepAsync(ConnectivityLoopInterval)
|
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
proc logAndMetrics(pm: PeerManager) {.async.} =
|
|
|
|
|
heartbeat "Scheduling log and metrics run", LogAndMetricsInterval:
|
|
|
|
|
# log metrics
|
2023-05-18 07:40:14 +00:00
|
|
|
|
let (inRelayPeers, outRelayPeers) = pm.connectedPeers(WakuRelayCodec)
|
|
|
|
|
let maxConnections = pm.switch.connManager.inSema.size
|
|
|
|
|
let totalRelayPeers = inRelayPeers.len + outRelayPeers.len
|
|
|
|
|
let notConnectedPeers = pm.peerStore.getNotConnectedPeers().mapIt(RemotePeerInfo.init(it.peerId, it.addrs))
|
|
|
|
|
let outsideBackoffPeers = notConnectedPeers.filterIt(pm.canBeConnected(it.peerId))
|
2023-07-04 11:31:18 +00:00
|
|
|
|
let totalConnections = pm.switch.connManager.getConnections().len
|
2023-05-18 07:40:14 +00:00
|
|
|
|
|
|
|
|
|
info "Relay peer connections",
|
2023-07-04 11:31:18 +00:00
|
|
|
|
inRelayConns = $inRelayPeers.len & "/" & $pm.inRelayPeersTarget,
|
|
|
|
|
outRelayConns = $outRelayPeers.len & "/" & $pm.outRelayPeersTarget,
|
|
|
|
|
totalConnections = $totalConnections & "/" & $maxConnections,
|
2023-05-18 07:40:14 +00:00
|
|
|
|
notConnectedPeers = notConnectedPeers.len,
|
|
|
|
|
outsideBackoffPeers = outsideBackoffPeers.len
|
|
|
|
|
|
2023-05-31 07:47:56 +00:00
|
|
|
|
# update prometheus metrics
|
2023-04-26 08:47:46 +00:00
|
|
|
|
for proto in pm.peerStore.getWakuProtos():
|
2023-05-18 07:40:14 +00:00
|
|
|
|
let (protoConnsIn, protoConnsOut) = pm.connectedPeers(proto)
|
2023-04-26 08:47:46 +00:00
|
|
|
|
let (protoStreamsIn, protoStreamsOut) = pm.getNumStreams(proto)
|
2023-05-18 07:40:14 +00:00
|
|
|
|
waku_connected_peers.set(protoConnsIn.len.float64, labelValues = [$Direction.In, proto])
|
|
|
|
|
waku_connected_peers.set(protoConnsOut.len.float64, labelValues = [$Direction.Out, proto])
|
2023-04-26 08:47:46 +00:00
|
|
|
|
waku_streams_peers.set(protoStreamsIn.float64, labelValues = [$Direction.In, proto])
|
|
|
|
|
waku_streams_peers.set(protoStreamsOut.float64, labelValues = [$Direction.Out, proto])
|
2023-04-12 11:05:34 +00:00
|
|
|
|
|
2023-01-26 09:20:20 +00:00
|
|
|
|
proc start*(pm: PeerManager) =
|
|
|
|
|
pm.started = true
|
|
|
|
|
asyncSpawn pm.relayConnectivityLoop()
|
2023-01-31 12:24:49 +00:00
|
|
|
|
asyncSpawn pm.prunePeerStoreLoop()
|
2023-05-31 07:47:56 +00:00
|
|
|
|
asyncSpawn pm.logAndMetrics()
|
2023-01-26 09:20:20 +00:00
|
|
|
|
|
|
|
|
|
proc stop*(pm: PeerManager) =
|
|
|
|
|
pm.started = false
|