Skip to content

Commit b4ce24f

Browse files
peer_manager set shard info and extend rest test to validate it
1 parent 8c2bbe4 commit b4ce24f

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,8 @@ proc onPeerMetadata(pm: PeerManager, peerId: PeerId) {.async.} =
660660

661661
# Store the shard information from metadata in the peer store
662662
if pm.switch.peerStore.peerExists(peerId):
663-
var peerInfo = pm.switch.peerStore.getPeer(peerId)
664-
peerInfo.shards = metadata.shards.mapIt(it.uint16)
665-
# Note: We don't need to call updatePeerInfo since we modified the reference directly
666-
debug "Updated peer shards from metadata",
667-
peerId = peerId, shards = peerInfo.shards
663+
let shards = metadata.shards.mapIt(it.uint16)
664+
pm.switch.peerStore.setShardInfo(peerId, shards)
668665

669666
return
670667

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
RemotePeerInfo(
4447
peerId: peerId,
@@ -49,6 +52,7 @@ proc getPeer*(peerStore: PeerStore, peerId: PeerId): RemotePeerInfo =
4952
else:
5053
none(enr.Record),
5154
protocols: peerStore[ProtoBook][peerId],
55+
shards: peerStore[ShardBook][peerId],
5256
agent: peerStore[AgentBook][peerId],
5357
protoVersion: peerStore[ProtoVersionBook][peerId],
5458
publicKey: peerStore[KeyBook][peerId],
@@ -69,6 +73,7 @@ proc peers*(peerStore: PeerStore): seq[RemotePeerInfo] =
6973
toSeq(peerStore[AddressBook].book.keys()),
7074
toSeq(peerStore[ProtoBook].book.keys()),
7175
toSeq(peerStore[KeyBook].book.keys()),
76+
toSeq(peerStore[ShardBook].book.keys()),
7277
)
7378
.toHashSet()
7479

@@ -120,6 +125,9 @@ proc addPeer*(peerStore: PeerStore, peer: RemotePeerInfo, origin = UnknownOrigin
120125
if peer.enr.isSome():
121126
peerStore[ENRBook][peer.peerId] = peer.enr.get()
122127

128+
proc setShardInfo*(peerStore: PeerStore, peerId: PeerID, shards: seq[uint16]) =
129+
peerStore[ShardBook][peerId] = shards
130+
123131
proc peers*(peerStore: PeerStore, proto: string): seq[RemotePeerInfo] =
124132
peerStore.peers().filterIt(it.protocols.contains(proto))
125133

0 commit comments

Comments
 (0)