Skip to content

Commit dfb2baf

Browse files
authored
fix: prevent setting shard info from PX if it exists (#2561)
* prevent setting px shard info * address review
1 parent eab8ce8 commit dfb2baf

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

packages/discovery/src/peer-exchange/peer_exchange_discovery.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,20 @@ export class PeerExchangeDiscovery
207207
continue;
208208
}
209209

210+
const hasPrevShardInfo = await this.hasShardInfo(peerInfo.id);
211+
const metadata =
212+
!hasPrevShardInfo && shardInfo
213+
? { metadata: { shardInfo: encodeRelayShard(shardInfo) } }
214+
: undefined;
215+
210216
// merge is smart enough to overwrite only changed parts
211217
await this.components.peerStore.merge(peerInfo.id, {
212218
tags: {
213219
[DEFAULT_PEER_EXCHANGE_TAG_NAME]: {
214220
value: DEFAULT_PEER_EXCHANGE_TAG_VALUE
215221
}
216222
},
217-
...(shardInfo && {
218-
metadata: {
219-
shardInfo: encodeRelayShard(shardInfo)
220-
}
221-
}),
223+
...metadata,
222224
...(peerInfo.multiaddrs && {
223225
multiaddrs: peerInfo.multiaddrs
224226
})
@@ -236,6 +238,22 @@ export class PeerExchangeDiscovery
236238
);
237239
}
238240
}
241+
242+
private async hasShardInfo(peerId: PeerId): Promise<boolean> {
243+
try {
244+
const peer = await this.components.peerStore.get(peerId);
245+
246+
if (!peer) {
247+
return false;
248+
}
249+
250+
return peer.metadata.has("shardInfo");
251+
} catch (err) {
252+
log.warn(`Error getting shard info for ${peerId.toString()}`, err);
253+
}
254+
255+
return false;
256+
}
239257
}
240258

241259
export function wakuPeerExchangeDiscovery(

0 commit comments

Comments
 (0)