2022-09-20 13:03:34 +02:00
|
|
|
|
import
|
2024-10-10 08:40:09 -04:00
|
|
|
|
std/[options, sequtils, random],
|
2024-07-09 13:14:28 +02:00
|
|
|
|
results,
|
2022-09-20 13:03:34 +02:00
|
|
|
|
chronicles,
|
|
|
|
|
|
chronos,
|
|
|
|
|
|
metrics,
|
|
|
|
|
|
libp2p/protocols/protocol,
|
|
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
|
eth/p2p/discoveryv5/enr
|
|
|
|
|
|
import
|
2023-08-09 18:11:50 +01:00
|
|
|
|
../common/nimchronos,
|
2023-04-18 15:22:10 +02:00
|
|
|
|
../node/peer_manager,
|
2023-04-19 13:29:23 +02:00
|
|
|
|
../waku_core,
|
2024-04-17 21:48:20 +02:00
|
|
|
|
../discovery/waku_discv5,
|
2022-09-20 13:03:34 +02:00
|
|
|
|
./rpc,
|
2024-09-18 15:58:07 +02:00
|
|
|
|
./rpc_codec,
|
2025-08-13 12:04:01 +05:30
|
|
|
|
../common/rate_limit/request_limiter,
|
|
|
|
|
|
./common
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2024-10-24 15:31:04 +03:00
|
|
|
|
from ../waku_core/codecs import WakuPeerExchangeCodec
|
|
|
|
|
|
export WakuPeerExchangeCodec
|
|
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
|
declarePublicGauge waku_px_peers_received_unknown,
|
|
|
|
|
|
"number of previously unknown ENRs received via peer exchange"
|
2022-09-20 13:03:34 +02:00
|
|
|
|
declarePublicGauge waku_px_peers_cached, "number of peer exchange peer ENRs cached"
|
2024-09-24 12:47:52 +02:00
|
|
|
|
declarePublicCounter waku_px_errors, "number of peer exchange errors", ["type"]
|
2025-08-13 12:04:01 +05:30
|
|
|
|
declarePublicCounter waku_px_peers_sent,
|
|
|
|
|
|
"number of ENRs sent to peer exchange requesters"
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
|
|
|
|
|
logScope:
|
2022-11-03 16:36:24 +01:00
|
|
|
|
topics = "waku peer_exchange"
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2025-08-13 12:04:01 +05:30
|
|
|
|
type WakuPeerExchange* = ref object of LPProtocol
|
|
|
|
|
|
peerManager*: PeerManager
|
|
|
|
|
|
enrCache*: seq[enr.Record]
|
|
|
|
|
|
cluster*: Option[uint16]
|
|
|
|
|
|
# todo: next step: ring buffer; future: implement cache satisfying https://rfc.vac.dev/spec/34/
|
|
|
|
|
|
requestRateLimiter*: RequestRateLimiter
|
|
|
|
|
|
pxLoopHandle*: Future[void]
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
|
proc respond(
|
|
|
|
|
|
wpx: WakuPeerExchange, enrs: seq[enr.Record], conn: Connection
|
|
|
|
|
|
): Future[WakuPeerExchangeResult[void]] {.async, gcsafe.} =
|
2024-09-18 15:58:07 +02:00
|
|
|
|
let rpc = PeerExchangeRpc.makeResponse(enrs.mapIt(PeerExchangePeerInfo(enr: it.raw)))
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2023-02-09 16:59:29 +01:00
|
|
|
|
try:
|
|
|
|
|
|
await conn.writeLP(rpc.encode().buffer)
|
|
|
|
|
|
except CatchableError as exc:
|
2025-01-28 15:37:33 +01:00
|
|
|
|
error "exception when trying to send a respond", error = getCurrentExceptionMsg()
|
2023-02-09 16:59:29 +01:00
|
|
|
|
waku_px_errors.inc(labelValues = [exc.msg])
|
2024-09-18 15:58:07 +02:00
|
|
|
|
return err(
|
|
|
|
|
|
(
|
|
|
|
|
|
status_code: PeerExchangeResponseStatusCode.DIAL_FAILURE,
|
|
|
|
|
|
status_desc: some("exception dialing peer: " & exc.msg),
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc respondError(
|
|
|
|
|
|
wpx: WakuPeerExchange,
|
|
|
|
|
|
status_code: PeerExchangeResponseStatusCode,
|
|
|
|
|
|
status_desc: Option[string],
|
|
|
|
|
|
conn: Connection,
|
|
|
|
|
|
): Future[WakuPeerExchangeResult[void]] {.async, gcsafe.} =
|
|
|
|
|
|
let rpc = PeerExchangeRpc.makeErrorResponse(status_code, status_desc)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
await conn.writeLP(rpc.encode().buffer)
|
|
|
|
|
|
except CatchableError as exc:
|
2025-01-28 15:37:33 +01:00
|
|
|
|
error "exception when trying to send a respond", error = getCurrentExceptionMsg()
|
2024-09-18 15:58:07 +02:00
|
|
|
|
waku_px_errors.inc(labelValues = [exc.msg])
|
|
|
|
|
|
return err(
|
|
|
|
|
|
(
|
|
|
|
|
|
status_code: PeerExchangeResponseStatusCode.SERVICE_UNAVAILABLE,
|
|
|
|
|
|
status_desc: some("exception dialing peer: " & exc.msg),
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
|
proc getEnrsFromCache(
|
|
|
|
|
|
wpx: WakuPeerExchange, numPeers: uint64
|
|
|
|
|
|
): seq[enr.Record] {.gcsafe.} =
|
2023-04-19 16:12:00 +02:00
|
|
|
|
if wpx.enrCache.len() == 0:
|
2025-10-15 10:49:36 +02:00
|
|
|
|
info "peer exchange ENR cache is empty"
|
2023-04-19 16:12:00 +02:00
|
|
|
|
return @[]
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2023-04-19 16:12:00 +02:00
|
|
|
|
# copy and shuffle
|
|
|
|
|
|
randomize()
|
|
|
|
|
|
var shuffledCache = wpx.enrCache
|
|
|
|
|
|
shuffledCache.shuffle()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2023-04-19 16:12:00 +02:00
|
|
|
|
# return numPeers or less if cache is smaller
|
2024-03-16 00:08:47 +01:00
|
|
|
|
return shuffledCache[0 ..< min(shuffledCache.len.int, numPeers.int)]
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2024-07-29 15:53:43 -04:00
|
|
|
|
proc poolFilter*(cluster: Option[uint16], peer: RemotePeerInfo): bool =
|
|
|
|
|
|
if peer.origin != Discv5:
|
2025-09-22 17:49:54 +05:30
|
|
|
|
trace "peer not from discv5", peer = $peer, origin = $peer.origin
|
2024-07-29 15:53:43 -04:00
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
if peer.enr.isNone():
|
2025-10-15 10:49:36 +02:00
|
|
|
|
info "peer has no ENR", peer = $peer
|
2024-07-29 15:53:43 -04:00
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
if cluster.isSome() and peer.enr.get().isClusterMismatched(cluster.get()):
|
2025-10-15 10:49:36 +02:00
|
|
|
|
info "peer has mismatching cluster", peer = $peer
|
2024-07-29 15:53:43 -04:00
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
2023-04-19 16:12:00 +02:00
|
|
|
|
proc populateEnrCache(wpx: WakuPeerExchange) =
|
2024-07-29 15:53:43 -04:00
|
|
|
|
# share only peers that i) are reachable ii) come from discv5 iii) share cluster
|
2025-04-07 12:24:03 +02:00
|
|
|
|
let withEnr = wpx.peerManager.switch.peerStore.getReachablePeers().filterIt(
|
2024-09-27 18:16:46 +05:30
|
|
|
|
poolFilter(wpx.cluster, it)
|
|
|
|
|
|
)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2023-04-19 16:12:00 +02:00
|
|
|
|
# either what we have or max cache size
|
|
|
|
|
|
var newEnrCache = newSeq[enr.Record](0)
|
2024-03-16 00:08:47 +01:00
|
|
|
|
for i in 0 ..< min(withEnr.len, MaxPeersCacheSize):
|
2023-04-19 16:12:00 +02:00
|
|
|
|
newEnrCache.add(withEnr[i].enr.get())
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2023-04-19 16:12:00 +02:00
|
|
|
|
# swap cache for new
|
|
|
|
|
|
wpx.enrCache = newEnrCache
|
2025-03-27 11:15:03 +01:00
|
|
|
|
trace "ENR cache populated"
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2023-04-19 16:12:00 +02:00
|
|
|
|
proc updatePxEnrCache(wpx: WakuPeerExchange) {.async.} =
|
|
|
|
|
|
# try more aggressively to fill the cache at startup
|
2024-10-24 10:33:32 +03:00
|
|
|
|
var attempts = 50
|
2024-09-25 11:51:50 +02:00
|
|
|
|
while wpx.enrCache.len < MaxPeersCacheSize and attempts > 0:
|
|
|
|
|
|
attempts -= 1
|
2023-04-19 16:12:00 +02:00
|
|
|
|
wpx.populateEnrCache()
|
2024-10-24 10:33:32 +03:00
|
|
|
|
await sleepAsync(1.seconds)
|
2023-04-19 16:12:00 +02:00
|
|
|
|
|
|
|
|
|
|
heartbeat "Updating px enr cache", CacheRefreshInterval:
|
|
|
|
|
|
wpx.populateEnrCache()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2022-11-02 09:45:21 +01:00
|
|
|
|
proc initProtocolHandler(wpx: WakuPeerExchange) =
|
2025-05-26 21:58:02 +02:00
|
|
|
|
proc handler(conn: Connection, proto: string) {.async: (raises: [CancelledError]).} =
|
2023-02-09 16:59:29 +01:00
|
|
|
|
var buffer: seq[byte]
|
2024-09-18 15:58:07 +02:00
|
|
|
|
wpx.requestRateLimiter.checkUsageLimit(WakuPeerExchangeCodec, conn):
|
|
|
|
|
|
try:
|
|
|
|
|
|
buffer = await conn.readLp(DefaultMaxRpcSize.int)
|
|
|
|
|
|
except CatchableError as exc:
|
2025-01-28 15:37:33 +01:00
|
|
|
|
error "exception when handling px request", error = getCurrentExceptionMsg()
|
2024-09-18 15:58:07 +02:00
|
|
|
|
waku_px_errors.inc(labelValues = [exc.msg])
|
|
|
|
|
|
|
|
|
|
|
|
(
|
2025-05-26 21:58:02 +02:00
|
|
|
|
try:
|
|
|
|
|
|
await wpx.respondError(
|
|
|
|
|
|
PeerExchangeResponseStatusCode.BAD_REQUEST, some(exc.msg), conn
|
|
|
|
|
|
)
|
|
|
|
|
|
except CatchableError:
|
|
|
|
|
|
error "could not send error response", error = getCurrentExceptionMsg()
|
|
|
|
|
|
return
|
2024-09-18 15:58:07 +02:00
|
|
|
|
).isOkOr:
|
|
|
|
|
|
error "Failed to respond with BAD_REQUEST:", error = $error
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2025-10-27 14:07:06 -03:00
|
|
|
|
let decBuf = PeerExchangeRpc.decode(buffer).valueOr:
|
2024-09-18 15:58:07 +02:00
|
|
|
|
waku_px_errors.inc(labelValues = [decodeRpcFailure])
|
2025-10-27 14:07:06 -03:00
|
|
|
|
error "Failed to decode PeerExchange request", error = $error
|
2024-09-18 15:58:07 +02:00
|
|
|
|
|
|
|
|
|
|
(
|
2025-05-26 21:58:02 +02:00
|
|
|
|
try:
|
|
|
|
|
|
await wpx.respondError(
|
2025-10-27 14:07:06 -03:00
|
|
|
|
PeerExchangeResponseStatusCode.BAD_REQUEST, some($error), conn
|
2025-05-26 21:58:02 +02:00
|
|
|
|
)
|
|
|
|
|
|
except CatchableError:
|
|
|
|
|
|
error "could not send error response decode",
|
|
|
|
|
|
error = getCurrentExceptionMsg()
|
|
|
|
|
|
return
|
2024-09-18 15:58:07 +02:00
|
|
|
|
).isOkOr:
|
|
|
|
|
|
error "Failed to respond with BAD_REQUEST:", error = $error
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2025-10-27 14:07:06 -03:00
|
|
|
|
let enrs = wpx.getEnrsFromCache(decBuf.request.numPeers)
|
2025-10-15 10:49:36 +02:00
|
|
|
|
info "peer exchange request received"
|
2025-10-02 12:13:07 +02:00
|
|
|
|
trace "px enrs to respond", enrs = $enrs
|
2025-05-26 21:58:02 +02:00
|
|
|
|
try:
|
|
|
|
|
|
(await wpx.respond(enrs, conn)).isErrOr:
|
|
|
|
|
|
waku_px_peers_sent.inc(enrs.len().int64())
|
|
|
|
|
|
except CatchableError:
|
|
|
|
|
|
error "could not send response", error = getCurrentExceptionMsg()
|
2024-09-18 15:58:07 +02:00
|
|
|
|
do:
|
2025-05-26 21:58:02 +02:00
|
|
|
|
defer:
|
|
|
|
|
|
# close, no data is expected
|
|
|
|
|
|
await conn.closeWithEof()
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
(
|
|
|
|
|
|
await wpx.respondError(
|
|
|
|
|
|
PeerExchangeResponseStatusCode.TOO_MANY_REQUESTS, none(string), conn
|
|
|
|
|
|
)
|
|
|
|
|
|
).isOkOr:
|
|
|
|
|
|
error "Failed to respond with TOO_MANY_REQUESTS:", error = $error
|
|
|
|
|
|
except CatchableError:
|
|
|
|
|
|
error "could not send error response", error = getCurrentExceptionMsg()
|
|
|
|
|
|
return
|
2023-05-25 16:42:48 +02:00
|
|
|
|
|
2022-11-02 09:45:21 +01:00
|
|
|
|
wpx.handler = handler
|
|
|
|
|
|
wpx.codec = WakuPeerExchangeCodec
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
2024-07-29 15:53:43 -04:00
|
|
|
|
proc new*(
|
|
|
|
|
|
T: type WakuPeerExchange,
|
|
|
|
|
|
peerManager: PeerManager,
|
|
|
|
|
|
cluster: Option[uint16] = none(uint16),
|
2024-09-18 15:58:07 +02:00
|
|
|
|
rateLimitSetting: Option[RateLimitSetting] = none[RateLimitSetting](),
|
2024-07-29 15:53:43 -04:00
|
|
|
|
): T =
|
2024-09-18 15:58:07 +02:00
|
|
|
|
let wpx = WakuPeerExchange(
|
|
|
|
|
|
peerManager: peerManager,
|
|
|
|
|
|
cluster: cluster,
|
|
|
|
|
|
requestRateLimiter: newRequestRateLimiter(rateLimitSetting),
|
|
|
|
|
|
)
|
2022-11-02 09:45:21 +01:00
|
|
|
|
wpx.initProtocolHandler()
|
2024-09-18 15:58:07 +02:00
|
|
|
|
setServiceLimitMetric(WakuPeerExchangeCodec, rateLimitSetting)
|
2023-04-19 16:12:00 +02:00
|
|
|
|
asyncSpawn wpx.updatePxEnrCache()
|
2022-11-02 09:45:21 +01:00
|
|
|
|
return wpx
|