Skip to content

Commit 6524032

Browse files
setup the metadata protocol as a service
1 parent d76f62f commit 6524032

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

packages/core/src/lib/metadata/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PeerId } from "@libp2p/interface/peer-id";
22
import { IncomingStreamData } from "@libp2p/interface/stream-handler";
3-
import type { IMetadata, Libp2p, ShardInfo } from "@waku/interfaces";
3+
import type { IMetadata, Libp2pComponents, ShardInfo } from "@waku/interfaces";
44
import { proto_metadata } from "@waku/proto";
55
import { Logger } from "@waku/utils";
66
import all from "it-all";
@@ -16,10 +16,10 @@ export const MetadataCodec = "/vac/waku/metadata/1.0.0";
1616

1717
class Metadata extends BaseProtocol {
1818
private readonly shardInfo: ShardInfo;
19-
constructor(shardInfo: ShardInfo, libp2p: Libp2p) {
19+
constructor(shardInfo: ShardInfo, libp2p: Libp2pComponents) {
2020
super(MetadataCodec, libp2p.components);
2121
this.shardInfo = shardInfo;
22-
void libp2p.handle(MetadataCodec, (streamData) => {
22+
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
2323
void this.onRequest(streamData);
2424
});
2525
}
@@ -97,6 +97,6 @@ class Metadata extends BaseProtocol {
9797

9898
export function wakuMetadata(
9999
shardInfo: ShardInfo
100-
): (libp2p: Libp2p) => IMetadata {
101-
return (libp2p: Libp2p) => new Metadata(shardInfo, libp2p);
100+
): (components: Libp2pComponents) => IMetadata {
101+
return (components: Libp2pComponents) => new Metadata(shardInfo, components);
102102
}

packages/interfaces/src/libp2p.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import type { Libp2pInit, Libp2pOptions } from "libp2p";
44
import type { identifyService } from "libp2p/identify";
55
import type { PingService } from "libp2p/ping";
66

7+
import { IMetadata } from ".";
8+
79
export type Libp2pServices = {
810
ping: PingService;
11+
metadata?: IMetadata;
912
pubsub?: GossipSub;
1013
identify: ReturnType<ReturnType<typeof identifyService>>;
1114
};

packages/sdk/src/create.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
DefaultUserAgent,
99
wakuFilter,
1010
wakuLightPush,
11+
wakuMetadata,
1112
WakuNode,
1213
WakuOptions,
1314
wakuStore
@@ -20,7 +21,8 @@ import type {
2021
Libp2pComponents,
2122
LightNode,
2223
ProtocolCreateOptions,
23-
RelayNode
24+
RelayNode,
25+
ShardInfo
2426
} from "@waku/interfaces";
2527
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
2628
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
@@ -54,6 +56,7 @@ export async function createLightNode(
5456
}
5557

5658
const libp2p = await defaultLibp2p(
59+
options.shardInfo,
5760
undefined,
5861
libp2pOptions,
5962
options?.userAgent
@@ -90,6 +93,7 @@ export async function createRelayNode(
9093
}
9194

9295
const libp2p = await defaultLibp2p(
96+
options.shardInfo,
9397
wakuGossipSub(options),
9498
libp2pOptions,
9599
options?.userAgent
@@ -134,6 +138,7 @@ export async function createFullNode(
134138
}
135139

136140
const libp2p = await defaultLibp2p(
141+
options.shardInfo,
137142
wakuGossipSub(options),
138143
libp2pOptions,
139144
options?.userAgent
@@ -170,6 +175,7 @@ type PubsubService = {
170175
};
171176

172177
export async function defaultLibp2p(
178+
shardInfo?: ShardInfo,
173179
wakuGossipSub?: PubsubService["pubsub"],
174180
options?: Partial<CreateLibp2pOptions>,
175181
userAgent?: string
@@ -191,6 +197,8 @@ export async function defaultLibp2p(
191197
? { pubsub: wakuGossipSub }
192198
: {};
193199

200+
const metadataService = shardInfo && wakuMetadata(shardInfo);
201+
194202
return createLibp2p({
195203
connectionManager: {
196204
minConnections: 1
@@ -204,6 +212,7 @@ export async function defaultLibp2p(
204212
agentVersion: userAgent ?? DefaultUserAgent
205213
}),
206214
ping: pingService(),
215+
metadata: metadataService,
207216
...pubsubService,
208217
...options?.services
209218
}

packages/tests/tests/metadata.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MetadataCodec, wakuMetadata } from "@waku/core";
1+
import { MetadataCodec } from "@waku/core";
22
import type { LightNode, ShardInfo } from "@waku/interfaces";
33
import { createLightNode } from "@waku/sdk";
44
import { shardInfoToPubsubTopics } from "@waku/utils";
@@ -40,12 +40,12 @@ describe("Metadata Protocol", () => {
4040
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
4141
const nwaku1PeerId = await nwaku1.getPeerId();
4242

43-
waku = await createLightNode();
43+
waku = await createLightNode({ shardInfo });
4444
await waku.start();
45-
const metadata = wakuMetadata(shardInfo)(waku.libp2p);
4645
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
4746

48-
const shardInfoRes = await metadata.query(nwaku1PeerId);
47+
const shardInfoRes =
48+
await waku.libp2p.services.metadata?.query(nwaku1PeerId);
4949
expect(shardInfoRes).to.not.be.undefined;
5050
expect(shardInfoRes?.clusterId).to.equal(shardInfo.clusterId);
5151
expect(shardInfoRes?.shards).to.deep.equal(shardInfo.shards);

0 commit comments

Comments
 (0)