fix: only override ping metadata in peer store (#1984)

Co-authored-by: Sasha <118575614+weboko@users.noreply.github.com>
This commit is contained in:
Arseniy Klempner 2024-04-29 15:39:02 -07:00 committed by GitHub
parent 1a6bc4f8ce
commit fb34b7262a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View File

@ -56,7 +56,7 @@ export class KeepAliveManager {
}
try {
await peerStore.patch(peerId, {
await peerStore.merge(peerId, {
metadata: {
ping: utf8ToBytes(ping.toString())
}

View File

@ -233,4 +233,35 @@ describe("Metadata Protocol", function () {
expect(metadataShardInfo!.clusterId).to.eq(shardInfo.clusterId);
expect(metadataShardInfo.shards).to.include.members(shardInfo.shards);
});
it("receiving a ping from a peer does not overwrite shard info", async function () {
const shardInfo: ShardInfo = {
clusterId: 2,
shards: [1]
};
await nwaku1.start({
relay: true,
discv5Discovery: true,
peerExchange: true,
clusterId: shardInfo.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo)
});
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
const nwaku1PeerId = await nwaku1.getPeerId();
waku = await createLightNode({ shardInfo, pingKeepAlive: 1 });
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
// delay to ensure the connection is estabilished, shardInfo is updated, and there is a ping
await delay(1500);
const metadata = (await waku.libp2p.peerStore.get(nwaku1PeerId)).metadata;
expect(metadata.get("shardInfo")).to.not.be.undefined;
const pingInfo = metadata.get("ping");
expect(pingInfo).to.not.be.undefined;
});
});