2024-06-28 10:34:57 +00:00
|
|
|
{.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
|
2024-12-03 13:39:37 +00:00
|
|
|
std/[options, net, sequtils],
|
2021-08-12 08:51:38 +00:00
|
|
|
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-05-01 19:13:08 +00:00
|
|
|
import libp2p/nameresolving/dnsresolver
|
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 ""
|
|
|
|
|
2024-12-03 13:39:37 +00:00
|
|
|
proc findPeers*(
|
|
|
|
wdd: WakuDnsDiscovery
|
|
|
|
): Future[Result[seq[RemotePeerInfo], cstring]] {.async.} =
|
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-12-03 13:39:37 +00:00
|
|
|
tree = (await syncTree(wdd.resolver, wdd.client.loc)).tryGet()
|
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")
|
|
|
|
|
2024-12-03 13:39:37 +00:00
|
|
|
let discoveredEnr = tree.getNodes().mapIt(it.record)
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
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)
|
2024-05-01 19:13:08 +00:00
|
|
|
|
|
|
|
proc retrieveDynamicBootstrapNodes*(
|
|
|
|
dnsDiscovery: bool, dnsDiscoveryUrl: string, dnsDiscoveryNameServers: seq[IpAddress]
|
2024-12-03 13:39:37 +00:00
|
|
|
): Future[Result[seq[RemotePeerInfo], string]] {.async.} =
|
2024-05-01 19:13:08 +00:00
|
|
|
## Retrieve dynamic bootstrap nodes (DNS discovery)
|
|
|
|
|
|
|
|
if dnsDiscovery and dnsDiscoveryUrl != "":
|
|
|
|
# DNS discovery
|
|
|
|
debug "Discovering nodes using Waku DNS discovery", url = dnsDiscoveryUrl
|
|
|
|
|
|
|
|
var nameServers: seq[TransportAddress]
|
|
|
|
for ip in dnsDiscoveryNameServers:
|
|
|
|
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
|
|
|
|
|
|
|
|
let dnsResolver = DnsResolver.new(nameServers)
|
|
|
|
|
|
|
|
proc resolver(domain: string): Future[string] {.async, gcsafe.} =
|
|
|
|
trace "resolving", domain = domain
|
|
|
|
let resolved = await dnsResolver.resolveTxt(domain)
|
2024-12-03 13:39:37 +00:00
|
|
|
if resolved.len > 0:
|
|
|
|
return resolved[0] # Use only first answer
|
2024-05-01 19:13:08 +00:00
|
|
|
|
|
|
|
var wakuDnsDiscovery = WakuDnsDiscovery.init(dnsDiscoveryUrl, resolver)
|
|
|
|
if wakuDnsDiscovery.isOk():
|
2024-12-03 13:39:37 +00:00
|
|
|
return (await wakuDnsDiscovery.get().findPeers()).mapErr(
|
|
|
|
proc(e: cstring): string =
|
|
|
|
$e
|
|
|
|
)
|
2024-05-01 19:13:08 +00:00
|
|
|
else:
|
|
|
|
warn "Failed to init Waku DNS discovery"
|
|
|
|
|
|
|
|
debug "No method for retrieving dynamic bootstrap nodes specified."
|
|
|
|
ok(newSeq[RemotePeerInfo]()) # Return an empty seq by default
|