Skip to content

Commit 0ba5040

Browse files
peer_manager set shard info and extend rest test to validate it
1 parent d985db3 commit 0ba5040

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
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

0 commit comments

Comments
 (0)