Danish Arora 9ac2a3f363
feat: metadata protocol (#1732)
* add proto

* add rpc and interfaces

* add protocol implementation

* update faulty proto def

* add rpc and interfaces

* refactor implementation & write test

* setup the metadata protocol as a service

* fix cases where metadata service needs to be undefined

* remove redundant catch block

* remove addressed TODO

* update import path

* log errors

* remove redundant code from handling incoming metadata request

* update tests

* add test to check for active connections

* change expects

* save remote peer's shard info after successful connection
2023-12-05 19:26:52 +05:30

205 lines
6.0 KiB
TypeScript

import { MetadataCodec } from "@waku/core";
import { decodeRelayShard } from "@waku/enr";
import type { LightNode, ShardInfo } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { shardInfoToPubsubTopics } from "@waku/utils";
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import { delay, tearDownNodes } from "../src/index.js";
import { makeLogFileName } from "../src/log_file.js";
import { NimGoNode } from "../src/node/node.js";
chai.use(chaiAsPromised);
describe("Metadata Protocol", () => {
let waku: LightNode;
let nwaku1: NimGoNode;
beforeEach(function () {
nwaku1 = new NimGoNode(makeLogFileName(this) + "1");
});
afterEach(async function () {
this.timeout(15000);
await tearDownNodes([nwaku1], waku);
});
describe("connections", function () {
it("same cluster, same shard: nodes connect", async function () {
this.timeout(55_000);
const shardInfo: ShardInfo = {
clusterId: 1,
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 });
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
const shardInfoRes =
await waku.libp2p.services.metadata?.query(nwaku1PeerId);
expect(shardInfoRes).to.not.be.undefined;
expect(shardInfoRes?.clusterId).to.equal(shardInfo.clusterId);
expect(shardInfoRes?.shards).to.deep.equal(shardInfo.shards);
const activeConnections = waku.libp2p.getConnections();
expect(activeConnections.length).to.equal(1);
});
it("same cluster, different shard: nodes connect", async function () {
this.timeout(55_000);
const shardInfo1: ShardInfo = {
clusterId: 1,
shards: [1]
};
const shardInfo2: ShardInfo = {
clusterId: 1,
shards: [2]
};
await nwaku1.start({
relay: true,
discv5Discovery: true,
peerExchange: true,
clusterId: shardInfo1.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo1)
});
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
const nwaku1PeerId = await nwaku1.getPeerId();
waku = await createLightNode({ shardInfo: shardInfo2 });
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
const shardInfoRes =
await waku.libp2p.services.metadata?.query(nwaku1PeerId);
expect(shardInfoRes).to.not.be.undefined;
expect(shardInfoRes?.clusterId).to.equal(shardInfo1.clusterId);
expect(shardInfoRes?.shards).to.deep.equal(shardInfo1.shards);
const activeConnections = waku.libp2p.getConnections();
expect(activeConnections.length).to.equal(1);
});
it("different cluster, same shard: nodes don't connect", async function () {
this.timeout(55_000);
const shardInfo1: ShardInfo = {
clusterId: 1,
shards: [1]
};
const shardInfo2: ShardInfo = {
clusterId: 2,
shards: [1]
};
await nwaku1.start({
relay: true,
discv5Discovery: true,
peerExchange: true,
clusterId: shardInfo1.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo1)
});
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
waku = await createLightNode({ shardInfo: shardInfo2 });
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
// add a delay to make sure the connection is closed from the other side
await delay(100);
const activeConnections = waku.libp2p.getConnections();
expect(activeConnections.length).to.equal(0);
});
it("different cluster, different shard: nodes don't connect", async function () {
this.timeout(55_000);
const shardInfo1: ShardInfo = {
clusterId: 1,
shards: [1]
};
const shardInfo2: ShardInfo = {
clusterId: 2,
shards: [2]
};
await nwaku1.start({
relay: true,
discv5Discovery: true,
peerExchange: true,
clusterId: shardInfo1.clusterId,
pubsubTopic: shardInfoToPubsubTopics(shardInfo1)
});
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
waku = await createLightNode({ shardInfo: shardInfo2 });
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
// add a delay to make sure the connection is closed from the other side
await delay(100);
const activeConnections = waku.libp2p.getConnections();
expect(activeConnections.length).to.equal(0);
});
});
it("PeerStore has remote peer's shard info after successful connection", async function () {
const shardInfo: ShardInfo = {
clusterId: 1,
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 });
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
// delay to ensure the connection is estabilished and shardInfo is updated
await delay(500);
const encodedShardInfo = (
await waku.libp2p.peerStore.get(nwaku1PeerId)
).metadata.get("shardInfo");
expect(encodedShardInfo).to.not.be.undefined;
const metadataShardInfo = decodeRelayShard(encodedShardInfo!);
expect(metadataShardInfo).not.be.undefined;
expect(metadataShardInfo!.clusterId).to.eq(shardInfo.clusterId);
expect(metadataShardInfo.shards).to.deep.eq(shardInfo.shards);
});
});