From bcabe66b056c74b2ad50f91907c881cf8792533d Mon Sep 17 00:00:00 2001 From: E M <5089238+emizzle@users.noreply.github.com> Date: Thu, 5 Feb 2026 20:59:15 +1100 Subject: [PATCH] instantiate dht inside Discovery.new --- codex/codex.nim | 2 +- codex/discovery.nim | 24 ++++++++++++------------ codex/utils/addrutils.nim | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/codex/codex.nim b/codex/codex.nim index 79e88a5b..5d8b8b74 100644 --- a/codex/codex.nim +++ b/codex/codex.nim @@ -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, diff --git a/codex/discovery.nim b/codex/discovery.nim index 1cded8e6..ba4b524e 100644 --- a/codex/discovery.nim +++ b/codex/discovery.nim @@ -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 diff --git a/codex/utils/addrutils.nim b/codex/utils/addrutils.nim index 1b7a556a..158d85dc 100644 --- a/codex/utils/addrutils.nim +++ b/codex/utils/addrutils.nim @@ -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