instantiate dht inside Discovery.new

This commit is contained in:
E M 2026-02-05 20:59:15 +11:00
parent 3203dfba41
commit bcabe66b05
No known key found for this signature in database
3 changed files with 28 additions and 13 deletions

View File

@ -190,7 +190,7 @@ proc new*(
Datastore(discoveryStoreRes.expect("Should create discovery datastore!"))
discovery = Discovery.new(
switch.peerInfo.privateKey,
switch = switch,
announceAddrs = config.listenAddrs,
bindPort = config.discoveryPort,
bootstrapNodes = config.bootstrapNodes,

View File

@ -14,7 +14,7 @@ import std/net
import std/sequtils
import pkg/chronos
import pkg/libp2p/[cid, multicodec, routing_record, signed_envelope]
import pkg/libp2p/[cid, multicodec, routing_record, signed_envelope, protocols/kademlia]
import pkg/questionable
import pkg/questionable/results
import pkg/contractabi/address as ca
@ -24,6 +24,7 @@ from pkg/nimcrypto import keccak256
import ./rng
import ./errors
import ./logutils
import ./utils/addrutils
export discv5
@ -35,7 +36,7 @@ logScope:
topics = "codex discovery"
type Discovery* = ref object of RootObj
protocol*: discv5.Protocol # dht protocol
protocol*: KadDHT # dht protocol
key: PrivateKey # private key
peerId: PeerId # the peer id of the local node
announceAddrs*: seq[MultiAddress] # addresses announced as part of the provider records
@ -234,7 +235,7 @@ proc close*(d: Discovery) {.async: (raises: []).} =
proc new*(
T: type Discovery,
key: PrivateKey,
switch: Switch,
bindIp = IPv4_any(),
bindPort = 0.Port,
announceAddrs: openArray[MultiAddress],
@ -244,6 +245,8 @@ proc new*(
## Create a new Discovery node instance for the given key and datastore
##
let key = switch.peerInfo.privateKey
var self = Discovery(
key: key, peerId: PeerId.init(key).expect("Should construct PeerId"), store: store
)
@ -259,15 +262,12 @@ proc new*(
)
# --------------------------------------------------------------------------
self.protocol = newProtocol(
key,
bindIp = bindIp,
bindPort = bindPort,
record = self.providerRecord.get,
bootstrapRecords = bootstrapNodes,
rng = Rng.instance(),
providers = ProvidersManager.new(store),
config = discoveryConfig,
# TODO: not sure why updateAnnounceRecord comes before initializing the dht
self.protocol = KadDHT.new(
switch = switch,
bootstrapNodes = bootstrapNodes.toBootstrapAddrs(),
config = KadDHTConfig.new(),
client = false
)
self

View File

@ -87,3 +87,18 @@ proc getAddressAndPort*(
(ip: ip, port: port)
except Exception:
(ip: none(IpAddress), port: none(Port))
proc toBootstrapAddr*(spr: SignedPeerRecord): (PeerId, seq[MultiAddress]) =
## Convert SignedPeerRecord to bootstrap address tuple
##
(spr.data.peerId, spr.data.addresses)
proc toBootstrapAddrs*(sprs: openArray[SignedPeerRecord]): seq[(PeerId, seq[MultiAddress])] =
## Convert seq[SignedPeerRecord] to seq of bootstrap address tuples
##
var res = newSeqUninit[(PeerId, seq[MultiAddress])]( sprs.len )
for spr in sprs:
res.add spr.toBootstrapAddr
return res