2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
## A set of utilities to integrate EIP-1459 DNS-based discovery
|
|
|
|
## for Waku v2 nodes.
|
2022-12-07 11:30:32 +00:00
|
|
|
##
|
2021-08-12 08:51:38 +00:00
|
|
|
## EIP-1459 is defined in https://eips.ethereum.org/EIPS/eip-1459
|
|
|
|
|
|
|
|
import
|
|
|
|
std/options,
|
|
|
|
stew/shims/net,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
metrics,
|
|
|
|
eth/keys,
|
|
|
|
eth/p2p/discoveryv5/enr,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/crypto/secp,
|
|
|
|
libp2p/multiaddress,
|
2021-10-06 12:29:08 +00:00
|
|
|
libp2p/peerid,
|
2024-01-15 14:54:02 +00:00
|
|
|
dnsdisc/client
|
2024-04-17 19:48:20 +00:00
|
|
|
import ../waku_core
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
export client
|
|
|
|
|
|
|
|
declarePublicGauge waku_dnsdisc_discovered, "number of nodes discovered"
|
|
|
|
declarePublicGauge waku_dnsdisc_errors, "number of waku dnsdisc errors", ["type"]
|
|
|
|
|
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
topics = "waku dnsdisc"
|
2021-08-12 08:51:38 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
type WakuDnsDiscovery* = object
|
|
|
|
client*: Client
|
|
|
|
resolver*: Resolver
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
#####################
|
|
|
|
# DNS Discovery API #
|
|
|
|
#####################
|
|
|
|
|
|
|
|
proc emptyResolver*(domain: string): Future[string] {.async, gcsafe.} =
|
2024-03-15 23:08:47 +00:00
|
|
|
debug "Empty resolver called", domain = domain
|
2021-08-12 08:51:38 +00:00
|
|
|
return ""
|
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
proc findPeers*(wdd: var WakuDnsDiscovery): Result[seq[RemotePeerInfo], cstring] =
|
2021-08-12 08:51:38 +00:00
|
|
|
## Find peers to connect to using DNS based discovery
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
info "Finding peers using Waku DNS discovery"
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
# Synchronise client tree using configured resolver
|
|
|
|
var tree: Tree
|
|
|
|
try:
|
2024-03-15 23:08:47 +00:00
|
|
|
tree = wdd.client.getTree(wdd.resolver)
|
|
|
|
# @TODO: this is currently a blocking operation to not violate memory safety
|
2021-08-12 08:51:38 +00:00
|
|
|
except Exception:
|
|
|
|
error "Failed to synchronise client tree"
|
|
|
|
waku_dnsdisc_errors.inc(labelValues = ["tree_sync_failure"])
|
|
|
|
return err("Node discovery failed")
|
|
|
|
|
|
|
|
let discoveredEnr = wdd.client.getNodeRecords()
|
|
|
|
|
|
|
|
if discoveredEnr.len > 0:
|
2024-03-15 23:08:47 +00:00
|
|
|
info "Successfully discovered ENR", count = discoveredEnr.len
|
2021-08-12 08:51:38 +00:00
|
|
|
else:
|
|
|
|
trace "No ENR retrieved from client tree"
|
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
var discoveredNodes: seq[RemotePeerInfo]
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
for enr in discoveredEnr:
|
2021-10-06 12:29:08 +00:00
|
|
|
# Convert discovered ENR to RemotePeerInfo and add to discovered nodes
|
|
|
|
let res = enr.toRemotePeerInfo()
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
if res.isOk():
|
|
|
|
discoveredNodes.add(res.get())
|
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
error "Failed to convert ENR to peer info", enr = $enr, err = res.error()
|
2021-08-12 08:51:38 +00:00
|
|
|
waku_dnsdisc_errors.inc(labelValues = ["peer_info_failure"])
|
|
|
|
|
|
|
|
if discoveredNodes.len > 0:
|
2024-03-15 23:08:47 +00:00
|
|
|
info "Successfully discovered nodes", count = discoveredNodes.len
|
2021-08-12 08:51:38 +00:00
|
|
|
waku_dnsdisc_discovered.inc(discoveredNodes.len.int64)
|
|
|
|
|
|
|
|
return ok(discoveredNodes)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc init*(
|
|
|
|
T: type WakuDnsDiscovery, locationUrl: string, resolver: Resolver
|
|
|
|
): Result[T, cstring] =
|
2021-08-12 08:51:38 +00:00
|
|
|
## Initialise Waku peer discovery via DNS
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
debug "init WakuDnsDiscovery", locationUrl = locationUrl
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
let
|
2024-03-15 23:08:47 +00:00
|
|
|
client = ?Client.init(locationUrl)
|
2021-08-17 08:30:17 +00:00
|
|
|
wakuDnsDisc = WakuDnsDiscovery(client: client, resolver: resolver)
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
debug "init success"
|
|
|
|
|
|
|
|
return ok(wakuDnsDisc)
|