Skip to content

Commit

Permalink
fix: filter discv5 bootstrap nodes by shards (#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
SionoiS committed Sep 26, 2023
1 parent 7a376f5 commit d178105
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions waku/waku_discv5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ else:
{.push raises: [].}

import
std/[sequtils, strutils, options, sugar, sets],
std/[sequtils, strutils, options, sets],
stew/results,
stew/shims/net,
chronos,
Expand Down Expand Up @@ -42,7 +42,7 @@ type WakuDiscoveryV5Config* = object

## Protocol

type WakuDiscv5Predicate* = proc(record: waku_enr.Record): bool {.closure, gcsafe.}
type WakuDiscv5Predicate* = proc(record: waku_enr.Record): bool {.closure, gcsafe, raises: [].}

type WakuDiscoveryV5* = ref object
conf: WakuDiscoveryV5Config
Expand Down Expand Up @@ -74,27 +74,39 @@ proc shardingPredicate*(record: Record): Option[WakuDiscv5Predicate] =

return some(predicate)

proc new*(T: type WakuDiscoveryV5, rng: ref HmacDrbgContext, conf: WakuDiscoveryV5Config, record: Option[waku_enr.Record]): T =
proc new*(
T: type WakuDiscoveryV5,
rng: ref HmacDrbgContext,
conf: WakuDiscoveryV5Config,
record: Option[waku_enr.Record]
): T =
let shardPredOp =
if record.isSome(): shardingPredicate(record.get())
else: none(WakuDiscv5Predicate)

var bootstrapRecords = conf.bootstrapRecords

# Remove bootstrap nodes with which we don't share shards.
if shardPredOp.isSome():
bootstrapRecords.keepIf(shardPredOp.get())

if conf.bootstrapRecords.len > 0 and bootstrapRecords.len == 0:
warn "No discv5 bootstrap nodes share this node configured shards"

let protocol = newProtocol(
rng = rng,
config = conf.discv5Config.get(protocol.defaultDiscoveryConfig),
bindPort = conf.port,
bindIp = conf.address,
privKey = conf.privateKey,
bootstrapRecords = conf.bootstrapRecords,
bootstrapRecords = bootstrapRecords,
enrAutoUpdate = conf.autoupdateRecord,
previousRecord = record,
enrIp = none(ValidIpAddress),
enrTcpPort = none(Port),
enrUdpPort = none(Port),
)

let shardPredOp =
if record.isSome():
shardingPredicate(record.get())
else:
none(WakuDiscv5Predicate)

WakuDiscoveryV5(conf: conf, protocol: protocol, listening: false, predicate: shardPredOp)

proc new*(T: type WakuDiscoveryV5,
Expand Down

0 comments on commit d178105

Please sign in to comment.