Skip to content

Commit e54851d

Browse files
fix: admin API peer shards field from metadata protocol (#3594)
* fix: admin API peer shards field from metadata protocol Store and return peer shard info from metadata protocol exchange instead of only checking ENR records. * peer_manager set shard info and extend rest test to validate it Co-authored-by: MorganaFuture <[email protected]>
1 parent adeb1a9 commit e54851d

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

tests/wakunode_rest/test_rest_admin.nim

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ suite "Waku v2 Rest API - Admin":
6565
): Future[void] {.async, gcsafe.} =
6666
await sleepAsync(0.milliseconds)
6767

68-
let shard = RelayShard(clusterId: clusterId, shardId: 0)
68+
let shard = RelayShard(clusterId: clusterId, shardId: 5)
6969
node1.subscribe((kind: PubsubSub, topic: $shard), simpleHandler).isOkOr:
7070
assert false, "Failed to subscribe to topic: " & $error
7171
node2.subscribe((kind: PubsubSub, topic: $shard), simpleHandler).isOkOr:
@@ -212,6 +212,18 @@ suite "Waku v2 Rest API - Admin":
212212
let conn2 = await node1.peerManager.connectPeer(peerInfo2)
213213
let conn3 = await node1.peerManager.connectPeer(peerInfo3)
214214

215+
var count = 0
216+
while count < 20:
217+
## Wait ~1s at most for the peer store to update shard info
218+
let getRes = await client.getPeers()
219+
if getRes.data.allIt(it.shards == @[5.uint16]):
220+
break
221+
222+
count.inc()
223+
await sleepAsync(50.milliseconds)
224+
225+
assert count < 20, "Timeout waiting for shards to be updated in peer store"
226+
215227
# Check successful connections
216228
check:
217229
conn2 == true

waku/node/peer_manager/peer_manager.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,11 @@ proc onPeerMetadata(pm: PeerManager, peerId: PeerId) {.async.} =
658658
$clusterId
659659
break guardClauses
660660

661+
# Store the shard information from metadata in the peer store
662+
if pm.switch.peerStore.peerExists(peerId):
663+
let shards = metadata.shards.mapIt(it.uint16)
664+
pm.switch.peerStore.setShardInfo(peerId, shards)
665+
661666
return
662667

663668
info "disconnecting from peer", peerId = peerId, reason = reason

waku/node/peer_manager/waku_peer_store.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type
3939
# Keeps track of the ENR (Ethereum Node Record) of a peer
4040
ENRBook* = ref object of PeerBook[enr.Record]
4141

42+
# Keeps track of peer shards
43+
ShardBook* = ref object of PeerBook[seq[uint16]]
44+
4245
proc getPeer*(peerStore: PeerStore, peerId: PeerId): RemotePeerInfo =
4346
let addresses =
4447
if peerStore[LastSeenBook][peerId].isSome():
@@ -55,6 +58,7 @@ proc getPeer*(peerStore: PeerStore, peerId: PeerId): RemotePeerInfo =
5558
else:
5659
none(enr.Record),
5760
protocols: peerStore[ProtoBook][peerId],
61+
shards: peerStore[ShardBook][peerId],
5862
agent: peerStore[AgentBook][peerId],
5963
protoVersion: peerStore[ProtoVersionBook][peerId],
6064
publicKey: peerStore[KeyBook][peerId],
@@ -76,6 +80,7 @@ proc peers*(peerStore: PeerStore): seq[RemotePeerInfo] =
7680
toSeq(peerStore[AddressBook].book.keys()),
7781
toSeq(peerStore[ProtoBook].book.keys()),
7882
toSeq(peerStore[KeyBook].book.keys()),
83+
toSeq(peerStore[ShardBook].book.keys()),
7984
)
8085
.toHashSet()
8186

@@ -127,6 +132,9 @@ proc addPeer*(peerStore: PeerStore, peer: RemotePeerInfo, origin = UnknownOrigin
127132
if peer.enr.isSome():
128133
peerStore[ENRBook][peer.peerId] = peer.enr.get()
129134

135+
proc setShardInfo*(peerStore: PeerStore, peerId: PeerID, shards: seq[uint16]) =
136+
peerStore[ShardBook][peerId] = shards
137+
130138
proc peers*(peerStore: PeerStore, proto: string): seq[RemotePeerInfo] =
131139
peerStore.peers().filterIt(it.protocols.contains(proto))
132140

waku/waku_core/peers.nim

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type RemotePeerInfo* = ref object
4848
addrs*: seq[MultiAddress]
4949
enr*: Option[enr.Record]
5050
protocols*: seq[string]
51+
shards*: seq[uint16]
5152

5253
agent*: string
5354
protoVersion*: string
@@ -73,6 +74,7 @@ proc init*(
7374
addrs: seq[MultiAddress] = @[],
7475
enr: Option[enr.Record] = none(enr.Record),
7576
protocols: seq[string] = @[],
77+
shards: seq[uint16] = @[],
7678
publicKey: crypto.PublicKey = crypto.PublicKey(),
7779
agent: string = "",
7880
protoVersion: string = "",
@@ -88,6 +90,7 @@ proc init*(
8890
addrs: addrs,
8991
enr: enr,
9092
protocols: protocols,
93+
shards: shards,
9194
publicKey: publicKey,
9295
agent: agent,
9396
protoVersion: protoVersion,
@@ -105,9 +108,12 @@ proc init*(
105108
addrs: seq[MultiAddress] = @[],
106109
enr: Option[enr.Record] = none(enr.Record),
107110
protocols: seq[string] = @[],
111+
shards: seq[uint16] = @[],
108112
): T {.raises: [Defect, ResultError[cstring], LPError].} =
109113
let peerId = PeerID.init(peerId).tryGet()
110-
RemotePeerInfo(peerId: peerId, addrs: addrs, enr: enr, protocols: protocols)
114+
RemotePeerInfo(
115+
peerId: peerId, addrs: addrs, enr: enr, protocols: protocols, shards: shards
116+
)
111117

112118
## Parse
113119

@@ -326,6 +332,7 @@ converter toRemotePeerInfo*(peerInfo: PeerInfo): RemotePeerInfo =
326332
addrs: peerInfo.listenAddrs,
327333
enr: none(enr.Record),
328334
protocols: peerInfo.protocols,
335+
shards: @[],
329336
agent: peerInfo.agentVersion,
330337
protoVersion: peerInfo.protoVersion,
331338
publicKey: peerInfo.publicKey,
@@ -361,6 +368,9 @@ proc getAgent*(peer: RemotePeerInfo): string =
361368
return peer.agent
362369

363370
proc getShards*(peer: RemotePeerInfo): seq[uint16] =
371+
if peer.shards.len > 0:
372+
return peer.shards
373+
364374
if peer.enr.isNone():
365375
return @[]
366376

0 commit comments

Comments
 (0)