feat(discv5): filter out peers without any listed capability (#2186)

This commit is contained in:
Simon-Pierre Vivier 2023-11-06 07:31:36 -05:00 committed by GitHub
parent b9563ae0b5
commit 200a11da09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 15 deletions

View File

@ -320,6 +320,7 @@ procSuite "Waku Discovery v5":
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shardsTopics).isOk()
builder.withWakuCapabilities(Relay)
let recordRes = builder.build()
require recordRes.isOk()
@ -338,6 +339,7 @@ procSuite "Waku Discovery v5":
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shardsTopics).isOk()
builder.withWakuCapabilities(Relay)
let recordRes = builder.build()
require recordRes.isOk()
@ -356,6 +358,7 @@ procSuite "Waku Discovery v5":
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shardsTopics).isOk()
builder.withWakuCapabilities(Relay)
let recordRes = builder.build()
require recordRes.isOk()

View File

@ -52,25 +52,19 @@ type WakuDiscoveryV5* = ref object
proc shardingPredicate*(record: Record): Option[WakuDiscv5Predicate] =
## Filter peers based on relay sharding information
let typedRecord = record.toTyped().valueOr:
debug "peer filtering failed", reason=error
return none(WakuDiscv5Predicate)
let typeRecordRes = record.toTyped()
let typedRecord =
if typeRecordRes.isErr():
debug "peer filtering failed", reason= $typeRecordRes.error
return none(WakuDiscv5Predicate)
else: typeRecordRes.get()
let nodeShardOp = typedRecord.relaySharding()
let nodeShard =
if nodeShardOp.isNone():
debug "no relay sharding information, peer filtering disabled"
return none(WakuDiscv5Predicate)
else: nodeShardOp.get()
let nodeShard = typedRecord.relaySharding().valueOr:
debug "no relay sharding information, peer filtering disabled"
return none(WakuDiscv5Predicate)
debug "peer filtering updated"
let predicate = proc(record: waku_enr.Record): bool =
nodeShard.shardIds.anyIt(record.containsShard(nodeShard.clusterId, it))
record.getCapabilities().len > 0 and #RFC 31 requirement
nodeShard.shardIds.anyIt(record.containsShard(nodeShard.clusterId, it)) #RFC 64 guideline
return some(predicate)