mirror of
https://github.com/vacp2p/dst-libp2p-test-node.git
synced 2026-08-28 21:31:08 +00:00
chore: do not manually start disco
This commit is contained in:
@@ -76,7 +76,8 @@ proc connectToBootstraps*(
|
||||
var backoff = 1.seconds
|
||||
for attempt in 1 .. 10:
|
||||
try:
|
||||
let peerId = await switch.connect(addr, allowUnknownPeerId = true).wait(10.seconds)
|
||||
let peerId =
|
||||
await switch.connect(addr, allowUnknownPeerId = true).wait(10.seconds)
|
||||
notice "Connected to bootstrap", address = addr, peerId, attempt
|
||||
bootstraps.add((peerId, @[addr]))
|
||||
break
|
||||
@@ -89,24 +90,21 @@ proc connectToBootstraps*(
|
||||
|
||||
if bootstraps.len == 0:
|
||||
return err(
|
||||
"Could not connect to any bootstrap resolved from '" & service &
|
||||
"' (candidates=" & $addrs.len & "). Last error: " & lastErr
|
||||
"Could not connect to any bootstrap resolved from '" & service & "' (candidates=" &
|
||||
$addrs.len & "). Last error: " & lastErr
|
||||
)
|
||||
|
||||
ok(bootstraps)
|
||||
|
||||
proc mountServiceDiscovery*(
|
||||
switch: Switch,
|
||||
bootstrapNodes: seq[(PeerId, seq[MultiAddress])],
|
||||
safetyParam: float64,
|
||||
ipSimCoefficient: float64,
|
||||
advertExpiry: Duration,
|
||||
xprPublishing: bool,
|
||||
): Future[ServiceDiscovery] {.async.} =
|
||||
let kadCfg = KadDHTConfig.new(
|
||||
validator = ExtEntryValidator(),
|
||||
selector = ExtEntrySelector(),
|
||||
)
|
||||
): ServiceDiscovery =
|
||||
let kadCfg =
|
||||
KadDHTConfig.new(validator = ExtEntryValidator(), selector = ExtEntrySelector())
|
||||
|
||||
let discoCfg = ServiceDiscoveryConfig.new(
|
||||
safetyParam = safetyParam,
|
||||
@@ -116,7 +114,7 @@ proc mountServiceDiscovery*(
|
||||
|
||||
let disco = ServiceDiscovery.new(
|
||||
switch,
|
||||
bootstrapNodes = bootstrapNodes,
|
||||
bootstrapNodes = @[],
|
||||
config = kadCfg,
|
||||
rng = libp2p.newRng(),
|
||||
codec = ExtendedServiceDiscoveryCodec,
|
||||
@@ -124,11 +122,7 @@ proc mountServiceDiscovery*(
|
||||
xprPublishing = xprPublishing,
|
||||
)
|
||||
|
||||
info "Starting service discovery"
|
||||
await disco.start()
|
||||
info "Mouting service discovery"
|
||||
switch.mount(disco)
|
||||
info "Service discovery mounted"
|
||||
disco
|
||||
|
||||
proc startHealthServer*(port: Port): Future[HttpServerRef] {.async.} =
|
||||
@@ -140,9 +134,7 @@ proc startHealthServer*(port: Port): Future[HttpServerRef] {.async.} =
|
||||
|
||||
if req.meth == MethodGet and (req.uri.path == "/health" or req.uri.path == "/ready"):
|
||||
return await req.respond(
|
||||
Http200,
|
||||
"ok",
|
||||
HttpTable.init([("Content-Type", "text/plain")]),
|
||||
Http200, "ok", HttpTable.init([("Content-Type", "text/plain")])
|
||||
)
|
||||
|
||||
return await req.respond(Http404, "Not Found")
|
||||
@@ -150,7 +142,9 @@ proc startHealthServer*(port: Port): Future[HttpServerRef] {.async.} =
|
||||
let addrs = initTAddress("0.0.0.0:" & $port)
|
||||
let serverRes = HttpServerRef.new(addrs, handler)
|
||||
if serverRes.isErr():
|
||||
raise newException(CatchableError, "Failed to create health HTTP server: " & $serverRes.error)
|
||||
raise newException(
|
||||
CatchableError, "Failed to create health HTTP server: " & $serverRes.error
|
||||
)
|
||||
|
||||
let server = serverRes.get()
|
||||
server.start()
|
||||
|
||||
@@ -2,6 +2,7 @@ import chronos, chronicles
|
||||
import std/sequtils
|
||||
import libp2p, libp2p/[multiaddress]
|
||||
import libp2p/extended_peer_record
|
||||
import libp2p/protocols/kademlia
|
||||
import env, helpers, core
|
||||
|
||||
proc main() {.async.} =
|
||||
@@ -10,44 +11,36 @@ proc main() {.async.} =
|
||||
quit(1)
|
||||
|
||||
var switch = buildSwitch(cfg.muxer, cfg.maxConnections, cfg.listenAddress)
|
||||
|
||||
let disco = mountServiceDiscovery(
|
||||
switch, cfg.safetyParam, cfg.ipSimCoefficient, cfg.advertExpiry, cfg.xprPublishing
|
||||
)
|
||||
|
||||
await switch.start()
|
||||
|
||||
let selfId = switch.peerInfo.peerId
|
||||
notice "Service discovery node started",
|
||||
peerId = $selfId,
|
||||
role = cfg.role,
|
||||
listen = cfg.listenAddress
|
||||
peerId = $selfId, role = cfg.role, listen = cfg.listenAddress
|
||||
|
||||
var bootstrapNodes: seq[(PeerId, seq[MultiAddress])] = @[]
|
||||
if cfg.role != RoleBootstrap:
|
||||
if cfg.startupJitterMs > 0:
|
||||
notice "Applying startup jitter", delayMs = cfg.startupJitterMs
|
||||
await sleepAsync(cfg.startupJitterMs.milliseconds)
|
||||
|
||||
let connectedBootstraps = await connectToBootstraps(
|
||||
switch,
|
||||
cfg.muxer,
|
||||
cfg.bootstrapService,
|
||||
cfg.listenPort,
|
||||
)
|
||||
bootstrapNodes = connectedBootstraps.valueOr:
|
||||
error "Failed to connect to bootstrap nodes", service = cfg.bootstrapService, error
|
||||
let connectedBootstraps =
|
||||
await connectToBootstraps(switch, cfg.muxer, cfg.bootstrapService, cfg.listenPort)
|
||||
let bootstrapNodes = connectedBootstraps.valueOr:
|
||||
error "Failed to connect to bootstrap nodes",
|
||||
service = cfg.bootstrapService, error
|
||||
quit(1)
|
||||
|
||||
let disco = await mountServiceDiscovery(
|
||||
switch,
|
||||
bootstrapNodes,
|
||||
cfg.safetyParam,
|
||||
cfg.ipSimCoefficient,
|
||||
cfg.advertExpiry,
|
||||
cfg.xprPublishing,
|
||||
)
|
||||
disco.updatePeers(bootstrapNodes)
|
||||
await disco.bootstrap(forceRefresh = true)
|
||||
|
||||
discard await startHealthServer(cfg.healthPort)
|
||||
|
||||
let advertisedServices = cfg.advertiseServices.mapIt(
|
||||
ServiceInfo(id: it, data: cfg.serviceData)
|
||||
)
|
||||
let advertisedServices =
|
||||
cfg.advertiseServices.mapIt(ServiceInfo(id: it, data: cfg.serviceData))
|
||||
|
||||
case cfg.role
|
||||
of RoleBootstrap:
|
||||
|
||||
Reference in New Issue
Block a user