2021-02-04 10:32:58 +00:00
|
|
|
{.push raises: [Defect, Exception].}
|
|
|
|
|
|
|
|
import
|
2021-02-11 08:58:25 +00:00
|
|
|
std/[options, sets, sequtils],
|
2021-02-05 10:49:11 +00:00
|
|
|
chronos, chronicles, metrics,
|
2021-02-04 10:32:58 +00:00
|
|
|
libp2p/standard_setup,
|
|
|
|
libp2p/peerstore
|
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
export peerstore, standard_setup
|
|
|
|
|
|
|
|
declareCounter waku_peers_dials, "Number of peer dials", ["outcome"]
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "wakupeers"
|
|
|
|
|
2021-02-04 10:32:58 +00:00
|
|
|
type
|
2021-02-12 08:53:52 +00:00
|
|
|
Connectedness* = enum
|
|
|
|
# NotConnected: default state for a new peer. No connection and no further information on connectedness.
|
|
|
|
NotConnected,
|
|
|
|
# CannotConnect: attempted to connect to peer, but failed.
|
|
|
|
CannotConnect,
|
|
|
|
# CanConnect: was recently connected to peer and disconnected gracefully.
|
|
|
|
CanConnect,
|
|
|
|
# Connected: actively connected to peer.
|
|
|
|
Connected
|
|
|
|
|
|
|
|
ConnectionBook* = object of PeerBook[Connectedness]
|
|
|
|
|
|
|
|
WakuPeerStore* = ref object of PeerStore
|
|
|
|
connectionBook*: ConnectionBook
|
|
|
|
|
2021-02-04 10:32:58 +00:00
|
|
|
PeerManager* = ref object of RootObj
|
|
|
|
switch*: Switch
|
2021-02-12 08:53:52 +00:00
|
|
|
peerStore*: WakuPeerStore
|
2021-02-04 10:32:58 +00:00
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
const
|
|
|
|
defaultDialTimeout = 1.minutes # @TODO should this be made configurable?
|
|
|
|
|
2021-02-12 08:53:52 +00:00
|
|
|
proc onConnEvent(pm: PeerManager, peerId: PeerID, event: ConnEvent) {.async.} =
|
|
|
|
case event.kind
|
|
|
|
of ConnEventKind.Connected:
|
|
|
|
pm.peerStore.connectionBook.set(peerId, Connected)
|
|
|
|
return
|
|
|
|
of ConnEventKind.Disconnected:
|
|
|
|
pm.peerStore.connectionBook.set(peerId, CanConnect)
|
|
|
|
return
|
|
|
|
|
|
|
|
proc new*(T: type WakuPeerStore): WakuPeerStore =
|
|
|
|
var p: WakuPeerStore
|
|
|
|
new(p)
|
|
|
|
return p
|
|
|
|
|
2021-02-04 10:32:58 +00:00
|
|
|
proc new*(T: type PeerManager, switch: Switch): PeerManager =
|
2021-02-12 08:53:52 +00:00
|
|
|
let pm = PeerManager(switch: switch,
|
|
|
|
peerStore: WakuPeerStore.new())
|
|
|
|
|
|
|
|
proc peerHook(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.} =
|
|
|
|
onConnEvent(pm, peerId, event)
|
|
|
|
|
|
|
|
|
|
|
|
pm.switch.addConnEventHandler(peerHook, ConnEventKind.Connected)
|
|
|
|
pm.switch.addConnEventHandler(peerHook, ConnEventKind.Disconnected)
|
|
|
|
|
|
|
|
return pm
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
####################
|
2021-02-08 09:17:20 +00:00
|
|
|
# Helper functions #
|
2021-02-04 10:32:58 +00:00
|
|
|
####################
|
|
|
|
|
2021-02-11 08:58:25 +00:00
|
|
|
proc toPeerInfo(storedInfo: StoredInfo): PeerInfo =
|
|
|
|
PeerInfo.init(peerId = storedInfo.peerId,
|
|
|
|
addrs = toSeq(storedInfo.addrs),
|
|
|
|
protocols = toSeq(storedInfo.protos))
|
|
|
|
|
|
|
|
#####################
|
|
|
|
# Manager interface #
|
|
|
|
#####################
|
|
|
|
|
|
|
|
proc peers*(pm: PeerManager): seq[StoredInfo] =
|
|
|
|
# Return the known info for all peers
|
|
|
|
pm.peerStore.peers()
|
|
|
|
|
|
|
|
proc peers*(pm: PeerManager, proto: string): seq[StoredInfo] =
|
|
|
|
# Return the known info for all peers registered on the specified protocol
|
|
|
|
pm.peers.filterIt(it.protos.contains(proto))
|
|
|
|
|
2021-02-12 08:53:52 +00:00
|
|
|
proc connectedness*(pm: PeerManager, peerId: PeerId): Connectedness =
|
2021-02-11 08:58:25 +00:00
|
|
|
# Return the connection state of the given, managed peer
|
|
|
|
# @TODO the PeerManager should keep and update local connectedness state for peers, redial on disconnect, etc.
|
|
|
|
# @TODO richer return than just bool, e.g. add enum "CanConnect", "CannotConnect", etc. based on recent connection attempts
|
|
|
|
|
|
|
|
let storedInfo = pm.peerStore.get(peerId)
|
|
|
|
|
|
|
|
if (storedInfo == StoredInfo()):
|
|
|
|
# Peer is not managed, therefore not connected
|
2021-02-12 08:53:52 +00:00
|
|
|
return NotConnected
|
2021-02-11 08:58:25 +00:00
|
|
|
else:
|
2021-02-12 08:53:52 +00:00
|
|
|
pm.peerStore.connectionBook.get(peerId)
|
2021-02-11 08:58:25 +00:00
|
|
|
|
|
|
|
proc hasPeer*(pm: PeerManager, peerInfo: PeerInfo, proto: string): bool =
|
2021-02-08 09:17:20 +00:00
|
|
|
# Returns `true` if peer is included in manager for the specified protocol
|
2021-02-04 10:32:58 +00:00
|
|
|
|
2021-02-08 09:17:20 +00:00
|
|
|
pm.peerStore.get(peerInfo.peerId).protos.contains(proto)
|
|
|
|
|
2021-02-11 08:58:25 +00:00
|
|
|
proc addPeer*(pm: PeerManager, peerInfo: PeerInfo, proto: string) =
|
2021-02-08 09:17:20 +00:00
|
|
|
# Adds peer to manager for the specified protocol
|
|
|
|
|
|
|
|
debug "Adding peer to manager", peerId = peerInfo.peerId, addr = peerInfo.addrs[0], proto = proto
|
2021-02-04 10:32:58 +00:00
|
|
|
|
|
|
|
# ...known addresses
|
|
|
|
for multiaddr in peerInfo.addrs:
|
|
|
|
pm.peerStore.addressBook.add(peerInfo.peerId, multiaddr)
|
|
|
|
|
|
|
|
# ...public key
|
|
|
|
var publicKey: PublicKey
|
|
|
|
discard peerInfo.peerId.extractPublicKey(publicKey)
|
|
|
|
|
|
|
|
pm.peerStore.keyBook.set(peerInfo.peerId, publicKey)
|
|
|
|
|
|
|
|
# ...associated protocols
|
|
|
|
pm.peerStore.protoBook.add(peerInfo.peerId, proto)
|
|
|
|
|
2021-02-11 08:58:25 +00:00
|
|
|
proc selectPeer*(pm: PeerManager, proto: string): Option[PeerInfo] =
|
|
|
|
# Selects the best peer for a given protocol
|
|
|
|
let peers = pm.peers.filterIt(it.protos.contains(proto))
|
|
|
|
|
|
|
|
if peers.len >= 1:
|
|
|
|
# @TODO proper heuristic here that compares peer scores and selects "best" one. For now the first peer for the given protocol is returned
|
|
|
|
let peerStored = peers[0]
|
|
|
|
|
|
|
|
return some(peerStored.toPeerInfo())
|
|
|
|
else:
|
|
|
|
return none(PeerInfo)
|
2021-02-08 09:17:20 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Dialer interface #
|
|
|
|
####################
|
|
|
|
|
|
|
|
proc dialPeer*(pm: PeerManager, peerInfo: PeerInfo, proto: string, dialTimeout = defaultDialTimeout): Future[Option[Connection]] {.async.} =
|
|
|
|
# Dial a given peer and add it to the list of known peers
|
|
|
|
# @TODO check peer validity and score before continuing. Limit number of peers to be managed.
|
|
|
|
|
|
|
|
# First add dialed peer info to peer store, if it does not exist yet...
|
|
|
|
if not pm.hasPeer(peerInfo, proto):
|
|
|
|
trace "Adding newly dialed peer to manager", peerId = peerInfo.peerId, addr = peerInfo.addrs[0], proto = proto
|
|
|
|
pm.addPeer(peerInfo, proto)
|
|
|
|
|
2021-02-04 10:32:58 +00:00
|
|
|
info "Dialing peer from manager", wireAddr = peerInfo.addrs[0], peerId = peerInfo.peerId
|
|
|
|
|
|
|
|
# Dial Peer
|
|
|
|
# @TODO Keep track of conn and connected state in peer store
|
2021-02-05 10:49:11 +00:00
|
|
|
let dialFut = pm.switch.dial(peerInfo.peerId, peerInfo.addrs, proto)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Attempt to dial remote peer
|
|
|
|
if (await dialFut.withTimeout(dialTimeout)):
|
|
|
|
waku_peers_dials.inc(labelValues = ["successful"])
|
|
|
|
return some(dialFut.read())
|
|
|
|
else:
|
|
|
|
# @TODO any redial attempts?
|
|
|
|
# @TODO indicate CannotConnect on peer metadata
|
|
|
|
debug "Dialing remote peer timed out"
|
|
|
|
waku_peers_dials.inc(labelValues = ["timeout"])
|
2021-02-12 08:53:52 +00:00
|
|
|
pm.peerStore.connectionBook.set(peerInfo.peerId, CannotConnect)
|
2021-02-05 10:49:11 +00:00
|
|
|
return none(Connection)
|
|
|
|
except CatchableError as e:
|
|
|
|
# @TODO any redial attempts?
|
|
|
|
# @TODO indicate CannotConnect on peer metadata
|
|
|
|
debug "Dialing remote peer failed", msg = e.msg
|
|
|
|
waku_peers_dials.inc(labelValues = ["failed"])
|
2021-02-12 08:53:52 +00:00
|
|
|
pm.peerStore.connectionBook.set(peerInfo.peerId, CannotConnect)
|
2021-02-05 10:49:11 +00:00
|
|
|
return none(Connection)
|