Skip to content

Commit 6dbcde0

Browse files
committed
chore!: directly convert from ENR to PeerInfo, remove unneeded utility
1 parent 84f114b commit 6dbcde0

File tree

10 files changed

+140
-92
lines changed

10 files changed

+140
-92
lines changed

package-lock.json

+4-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dns-discovery/src/index.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { PeerInfo } from "@libp2p/interface-peer-info";
77
import type { PeerStore } from "@libp2p/interface-peer-store";
88
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
99
import type { IEnr } from "@waku/interfaces";
10-
import { multiaddrsToPeerInfo } from "@waku/utils";
1110
import debug from "debug";
1211

1312
import { DnsNodeDiscovery, NodeCapabilityCount } from "./dns.js";
@@ -94,27 +93,28 @@ export class PeerDiscoveryDns
9493
this._started = true;
9594
for await (const peer of this.nextPeer()) {
9695
if (!this._started) return;
97-
const peerInfos = multiaddrsToPeerInfo(peer.getFullMultiaddrs());
98-
peerInfos.forEach(async (peerInfo) => {
99-
if (
100-
(await this._components.peerStore.getTags(peerInfo.id)).find(
101-
({ name }) => name === DEFAULT_BOOTSTRAP_TAG_NAME
102-
)
96+
97+
const peerInfo = peer.peerInfo;
98+
if (!peerInfo) continue;
99+
100+
if (
101+
(await this._components.peerStore.getTags(peerInfo.id)).find(
102+
({ name }) => name === DEFAULT_BOOTSTRAP_TAG_NAME
103103
)
104-
return;
105-
106-
await this._components.peerStore.tagPeer(
107-
peerInfo.id,
108-
DEFAULT_BOOTSTRAP_TAG_NAME,
109-
{
110-
value: this._options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
111-
ttl: this._options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL,
112-
}
113-
);
114-
this.dispatchEvent(
115-
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
116-
);
117-
});
104+
)
105+
continue;
106+
107+
await this._components.peerStore.tagPeer(
108+
peerInfo.id,
109+
DEFAULT_BOOTSTRAP_TAG_NAME,
110+
{
111+
value: this._options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
112+
ttl: this._options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL,
113+
}
114+
);
115+
this.dispatchEvent(
116+
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
117+
);
118118
}
119119
}
120120

packages/enr/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
},
6565
"devDependencies": {
6666
"@libp2p/interface-peer-id": "^2.0.1",
67+
"@libp2p/interface-peer-info": "^1.0.8",
6768
"@libp2p/peer-id-factory": "^2.0.1",
6869
"@rollup/plugin-commonjs": "^24.0.1",
6970
"@rollup/plugin-json": "^6.0.0",

packages/enr/src/enr.spec.ts

+57-33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PeerId } from "@libp2p/interface-peer-id";
12
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
23
import { multiaddr } from "@multiformats/multiaddr";
34
import * as secp from "@noble/secp256k1";
@@ -10,7 +11,11 @@ import { ERR_INVALID_ID } from "./constants.js";
1011
import { EnrCreator } from "./creator.js";
1112
import { EnrDecoder } from "./decoder.js";
1213
import { EnrEncoder } from "./encoder.js";
13-
import { ENR } from "./enr.js";
14+
import {
15+
ENR,
16+
TransportProtocol,
17+
TransportProtocolPerIpVersion,
18+
} from "./enr.js";
1419
import { getPrivateKeyFromPeerId } from "./peer_id.js";
1520

1621
describe("ENR", function () {
@@ -42,7 +47,7 @@ describe("ENR", function () {
4247
if (!enr2.signature) throw "enr.signature is undefined";
4348

4449
expect(bytesToHex(enr2.signature)).to.be.equal(bytesToHex(enr.signature));
45-
const ma = enr2.getLocationMultiaddr("udp")!;
50+
const ma = enr2.getLocationMultiaddr(TransportProtocol.UDP)!;
4651
expect(ma.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
4752
expect(enr2.multiaddrs).to.not.be.undefined;
4853
expect(enr2.multiaddrs!.length).to.be.equal(3);
@@ -256,16 +261,16 @@ describe("ENR", function () {
256261
record.set("ip", tuples0[0][1]);
257262
record.set("udp", tuples0[1][1]);
258263
// and get the multiaddr
259-
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(
260-
multi0.toString()
261-
);
264+
expect(
265+
record.getLocationMultiaddr(TransportProtocol.UDP)!.toString()
266+
).to.equal(multi0.toString());
262267
// set the multiaddr
263268
const multi1 = multiaddr("/ip4/0.0.0.0/udp/30300");
264269
record.setLocationMultiaddr(multi1);
265270
// and get the multiaddr
266-
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(
267-
multi1.toString()
268-
);
271+
expect(
272+
record.getLocationMultiaddr(TransportProtocol.UDP)!.toString()
273+
).to.equal(multi1.toString());
269274
// and get the underlying records
270275
const tuples1 = multi1.tuples();
271276
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
@@ -284,16 +289,16 @@ describe("ENR", function () {
284289
record.set("ip", tuples0[0][1]);
285290
record.set("tcp", tuples0[1][1]);
286291
// and get the multiaddr
287-
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(
288-
multi0.toString()
289-
);
292+
expect(
293+
record.getLocationMultiaddr(TransportProtocol.TCP)!.toString()
294+
).to.equal(multi0.toString());
290295
// set the multiaddr
291296
const multi1 = multiaddr("/ip4/0.0.0.0/tcp/30300");
292297
record.setLocationMultiaddr(multi1);
293298
// and get the multiaddr
294-
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(
295-
multi1.toString()
296-
);
299+
expect(
300+
record.getLocationMultiaddr(TransportProtocol.TCP)!.toString()
301+
).to.equal(multi1.toString());
297302
// and get the underlying records
298303
const tuples1 = multi1.tuples();
299304
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
@@ -306,7 +311,7 @@ describe("ENR", function () {
306311
const ip6 = "::1";
307312
const tcp = 8080;
308313
const udp = 8080;
309-
let peerId;
314+
let peerId: PeerId;
310315
let enr: ENR;
311316

312317
before(async function () {
@@ -321,66 +326,85 @@ describe("ENR", function () {
321326
});
322327

323328
it("should properly create location multiaddrs - udp4", () => {
324-
expect(enr.getLocationMultiaddr("udp4")).to.deep.equal(
325-
multiaddr(`/ip4/${ip4}/udp/${udp}`)
326-
);
329+
expect(
330+
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.UDP4)
331+
).to.deep.equal(multiaddr(`/ip4/${ip4}/udp/${udp}`));
327332
});
328333

329334
it("should properly create location multiaddrs - tcp4", () => {
330-
expect(enr.getLocationMultiaddr("tcp4")).to.deep.equal(
331-
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
332-
);
335+
expect(
336+
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.TCP4)
337+
).to.deep.equal(multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
333338
});
334339

335340
it("should properly create location multiaddrs - udp6", () => {
336-
expect(enr.getLocationMultiaddr("udp6")).to.deep.equal(
337-
multiaddr(`/ip6/${ip6}/udp/${udp}`)
338-
);
341+
expect(
342+
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.UDP6)
343+
).to.deep.equal(multiaddr(`/ip6/${ip6}/udp/${udp}`));
339344
});
340345

341346
it("should properly create location multiaddrs - tcp6", () => {
342-
expect(enr.getLocationMultiaddr("tcp6")).to.deep.equal(
343-
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
344-
);
347+
expect(
348+
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.TCP6)
349+
).to.deep.equal(multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
345350
});
346351

347352
it("should properly create location multiaddrs - udp", () => {
348353
// default to ip4
349-
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
354+
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
350355
multiaddr(`/ip4/${ip4}/udp/${udp}`)
351356
);
352357
// if ip6 is set, use it
353358
enr.ip = undefined;
354-
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
359+
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
355360
multiaddr(`/ip6/${ip6}/udp/${udp}`)
356361
);
357362
// if ip6 does not exist, use ip4
358363
enr.ip6 = undefined;
359364
enr.ip = ip4;
360-
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
365+
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
361366
multiaddr(`/ip4/${ip4}/udp/${udp}`)
362367
);
363368
enr.ip6 = ip6;
364369
});
365370

366371
it("should properly create location multiaddrs - tcp", () => {
367372
// default to ip4
368-
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
373+
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
369374
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
370375
);
371376
// if ip6 is set, use it
372377
enr.ip = undefined;
373-
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
378+
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
374379
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
375380
);
376381
// if ip6 does not exist, use ip4
377382
enr.ip6 = undefined;
378383
enr.ip = ip4;
379-
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
384+
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
380385
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
381386
);
382387
enr.ip6 = ip6;
383388
});
389+
390+
it("should properly create peer info with all multiaddrs", () => {
391+
const peerInfo = enr.peerInfo!;
392+
console.log(peerInfo);
393+
expect(peerInfo.id.toString()).to.equal(peerId.toString());
394+
expect(peerInfo.multiaddrs.length).to.equal(4);
395+
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
396+
multiaddr(`/ip4/${ip4}/tcp/${tcp}`).toString()
397+
);
398+
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
399+
multiaddr(`/ip6/${ip6}/tcp/${tcp}`).toString()
400+
);
401+
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
402+
multiaddr(`/ip4/${ip4}/udp/${udp}`).toString()
403+
);
404+
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
405+
multiaddr(`/ip6/${ip6}/udp/${udp}`).toString()
406+
);
407+
});
384408
});
385409

386410
describe("waku2 key round trip", async () => {

packages/enr/src/enr.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PeerId } from "@libp2p/interface-peer-id";
2+
import type { PeerInfo } from "@libp2p/interface-peer-info";
23
import type { Multiaddr } from "@multiformats/multiaddr";
34
import {
45
convertToBytes,
@@ -25,6 +26,17 @@ import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
2526

2627
const log = debug("waku:enr");
2728

29+
export enum TransportProtocol {
30+
TCP = "tcp",
31+
UDP = "udp",
32+
}
33+
export enum TransportProtocolPerIpVersion {
34+
TCP4 = "tcp4",
35+
UDP4 = "udp4",
36+
TCP6 = "tcp6",
37+
UDP6 = "udp6",
38+
}
39+
2840
export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
2941
public static readonly RECORD_PREFIX = "enr:";
3042
public seq: SequenceNumber;
@@ -232,7 +244,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
232244
}
233245

234246
getLocationMultiaddr: (
235-
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
247+
protocol: TransportProtocol | TransportProtocolPerIpVersion
236248
) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
237249

238250
setLocationMultiaddr(multiaddr: Multiaddr): void {
@@ -259,6 +271,32 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
259271
}
260272
}
261273

274+
getAllLocationMultiaddrs(): Multiaddr[] {
275+
const multiaddrs = [];
276+
277+
for (const protocol of Object.values(TransportProtocolPerIpVersion)) {
278+
const ma = this.getLocationMultiaddr(
279+
protocol as TransportProtocolPerIpVersion
280+
);
281+
if (ma) multiaddrs.push(ma);
282+
}
283+
284+
const _multiaddrs = this.multiaddrs ?? [];
285+
multiaddrs.concat(_multiaddrs);
286+
287+
return multiaddrs;
288+
}
289+
290+
get peerInfo(): PeerInfo | undefined {
291+
const id = this.peerId;
292+
if (!id) return;
293+
return {
294+
id,
295+
multiaddrs: this.getAllLocationMultiaddrs(),
296+
protocols: [],
297+
};
298+
}
299+
262300
/**
263301
* Returns the full multiaddr from the ENR fields matching the provided
264302
* `protocol` parameter.
@@ -268,7 +306,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
268306
* @param protocol
269307
*/
270308
getFullMultiaddr(
271-
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
309+
protocol: TransportProtocol | TransportProtocolPerIpVersion
272310
): Multiaddr | undefined {
273311
if (this.peerId) {
274312
const locationMultiaddr = this.getLocationMultiaddr(protocol);

packages/interfaces/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@libp2p/interface-connection-manager": "^1.3.7",
5555
"@libp2p/interface-libp2p": "^1.1.1",
5656
"@libp2p/interface-peer-id": "^2.0.1",
57+
"@libp2p/interface-peer-info": "^1.0.8",
5758
"@libp2p/interface-peer-store": "^1.2.8",
5859
"@libp2p/interface-registrar": "^2.0.8",
5960
"@multiformats/multiaddr": "^11.4.0",

0 commit comments

Comments
 (0)