chore!: directly convert from ENR to `PeerInfo`, remove unneeded utility

This commit is contained in:
fryorcraken.eth 2023-03-07 14:36:21 +11:00
parent 84f114bfa4
commit 6dbcde041a
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
10 changed files with 141 additions and 93 deletions

10
package-lock.json generated
View File

@ -28228,6 +28228,7 @@
},
"devDependencies": {
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/peer-id-factory": "^2.0.1",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
@ -28274,6 +28275,7 @@
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-libp2p": "^1.1.1",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@multiformats/multiaddr": "^11.4.0",
@ -28520,16 +28522,13 @@
"version": "0.0.1",
"license": "MIT OR Apache-2.0",
"dependencies": {
"@libp2p/peer-id": "^2.0.2",
"debug": "^4.3.4",
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@multiformats/multiaddr": "^11.4.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
@ -32902,6 +32901,7 @@
"@ethersproject/rlp": "^5.7.0",
"@libp2p/crypto": "^1.0.12",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/peer-id": "^2.0.2",
"@libp2p/peer-id-factory": "^2.0.1",
"@multiformats/multiaddr": "^11.4.0",
@ -32949,6 +32949,7 @@
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-libp2p": "^1.1.1",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@multiformats/multiaddr": "^11.4.0",
@ -33151,10 +33152,7 @@
"requires": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/peer-id": "^2.0.2",
"@multiformats/multiaddr": "^11.4.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",

View File

@ -7,7 +7,6 @@ import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { PeerStore } from "@libp2p/interface-peer-store";
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
import type { IEnr } from "@waku/interfaces";
import { multiaddrsToPeerInfo } from "@waku/utils";
import debug from "debug";
import { DnsNodeDiscovery, NodeCapabilityCount } from "./dns.js";
@ -94,14 +93,16 @@ export class PeerDiscoveryDns
this._started = true;
for await (const peer of this.nextPeer()) {
if (!this._started) return;
const peerInfos = multiaddrsToPeerInfo(peer.getFullMultiaddrs());
peerInfos.forEach(async (peerInfo) => {
const peerInfo = peer.peerInfo;
if (!peerInfo) continue;
if (
(await this._components.peerStore.getTags(peerInfo.id)).find(
({ name }) => name === DEFAULT_BOOTSTRAP_TAG_NAME
)
)
return;
continue;
await this._components.peerStore.tagPeer(
peerInfo.id,
@ -114,7 +115,6 @@ export class PeerDiscoveryDns
this.dispatchEvent(
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
);
});
}
}

View File

@ -64,6 +64,7 @@
},
"devDependencies": {
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/peer-id-factory": "^2.0.1",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",

View File

@ -1,3 +1,4 @@
import type { PeerId } from "@libp2p/interface-peer-id";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { multiaddr } from "@multiformats/multiaddr";
import * as secp from "@noble/secp256k1";
@ -10,7 +11,11 @@ import { ERR_INVALID_ID } from "./constants.js";
import { EnrCreator } from "./creator.js";
import { EnrDecoder } from "./decoder.js";
import { EnrEncoder } from "./encoder.js";
import { ENR } from "./enr.js";
import {
ENR,
TransportProtocol,
TransportProtocolPerIpVersion,
} from "./enr.js";
import { getPrivateKeyFromPeerId } from "./peer_id.js";
describe("ENR", function () {
@ -42,7 +47,7 @@ describe("ENR", function () {
if (!enr2.signature) throw "enr.signature is undefined";
expect(bytesToHex(enr2.signature)).to.be.equal(bytesToHex(enr.signature));
const ma = enr2.getLocationMultiaddr("udp")!;
const ma = enr2.getLocationMultiaddr(TransportProtocol.UDP)!;
expect(ma.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
expect(enr2.multiaddrs).to.not.be.undefined;
expect(enr2.multiaddrs!.length).to.be.equal(3);
@ -256,16 +261,16 @@ describe("ENR", function () {
record.set("ip", tuples0[0][1]);
record.set("udp", tuples0[1][1]);
// and get the multiaddr
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(
multi0.toString()
);
expect(
record.getLocationMultiaddr(TransportProtocol.UDP)!.toString()
).to.equal(multi0.toString());
// set the multiaddr
const multi1 = multiaddr("/ip4/0.0.0.0/udp/30300");
record.setLocationMultiaddr(multi1);
// and get the multiaddr
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(
multi1.toString()
);
expect(
record.getLocationMultiaddr(TransportProtocol.UDP)!.toString()
).to.equal(multi1.toString());
// and get the underlying records
const tuples1 = multi1.tuples();
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
@ -284,16 +289,16 @@ describe("ENR", function () {
record.set("ip", tuples0[0][1]);
record.set("tcp", tuples0[1][1]);
// and get the multiaddr
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(
multi0.toString()
);
expect(
record.getLocationMultiaddr(TransportProtocol.TCP)!.toString()
).to.equal(multi0.toString());
// set the multiaddr
const multi1 = multiaddr("/ip4/0.0.0.0/tcp/30300");
record.setLocationMultiaddr(multi1);
// and get the multiaddr
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(
multi1.toString()
);
expect(
record.getLocationMultiaddr(TransportProtocol.TCP)!.toString()
).to.equal(multi1.toString());
// and get the underlying records
const tuples1 = multi1.tuples();
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
@ -306,7 +311,7 @@ describe("ENR", function () {
const ip6 = "::1";
const tcp = 8080;
const udp = 8080;
let peerId;
let peerId: PeerId;
let enr: ENR;
before(async function () {
@ -321,43 +326,43 @@ describe("ENR", function () {
});
it("should properly create location multiaddrs - udp4", () => {
expect(enr.getLocationMultiaddr("udp4")).to.deep.equal(
multiaddr(`/ip4/${ip4}/udp/${udp}`)
);
expect(
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.UDP4)
).to.deep.equal(multiaddr(`/ip4/${ip4}/udp/${udp}`));
});
it("should properly create location multiaddrs - tcp4", () => {
expect(enr.getLocationMultiaddr("tcp4")).to.deep.equal(
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
);
expect(
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.TCP4)
).to.deep.equal(multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
});
it("should properly create location multiaddrs - udp6", () => {
expect(enr.getLocationMultiaddr("udp6")).to.deep.equal(
multiaddr(`/ip6/${ip6}/udp/${udp}`)
);
expect(
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.UDP6)
).to.deep.equal(multiaddr(`/ip6/${ip6}/udp/${udp}`));
});
it("should properly create location multiaddrs - tcp6", () => {
expect(enr.getLocationMultiaddr("tcp6")).to.deep.equal(
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
);
expect(
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.TCP6)
).to.deep.equal(multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
});
it("should properly create location multiaddrs - udp", () => {
// default to ip4
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
multiaddr(`/ip4/${ip4}/udp/${udp}`)
);
// if ip6 is set, use it
enr.ip = undefined;
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
multiaddr(`/ip6/${ip6}/udp/${udp}`)
);
// if ip6 does not exist, use ip4
enr.ip6 = undefined;
enr.ip = ip4;
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
multiaddr(`/ip4/${ip4}/udp/${udp}`)
);
enr.ip6 = ip6;
@ -365,22 +370,41 @@ describe("ENR", function () {
it("should properly create location multiaddrs - tcp", () => {
// default to ip4
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
);
// if ip6 is set, use it
enr.ip = undefined;
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
);
// if ip6 does not exist, use ip4
enr.ip6 = undefined;
enr.ip = ip4;
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
);
enr.ip6 = ip6;
});
it("should properly create peer info with all multiaddrs", () => {
const peerInfo = enr.peerInfo!;
console.log(peerInfo);
expect(peerInfo.id.toString()).to.equal(peerId.toString());
expect(peerInfo.multiaddrs.length).to.equal(4);
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
multiaddr(`/ip4/${ip4}/tcp/${tcp}`).toString()
);
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
multiaddr(`/ip6/${ip6}/tcp/${tcp}`).toString()
);
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
multiaddr(`/ip4/${ip4}/udp/${udp}`).toString()
);
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
multiaddr(`/ip6/${ip6}/udp/${udp}`).toString()
);
});
});
describe("waku2 key round trip", async () => {

View File

@ -1,4 +1,5 @@
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { Multiaddr } from "@multiformats/multiaddr";
import {
convertToBytes,
@ -25,6 +26,17 @@ import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
const log = debug("waku:enr");
export enum TransportProtocol {
TCP = "tcp",
UDP = "udp",
}
export enum TransportProtocolPerIpVersion {
TCP4 = "tcp4",
UDP4 = "udp4",
TCP6 = "tcp6",
UDP6 = "udp6",
}
export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
public static readonly RECORD_PREFIX = "enr:";
public seq: SequenceNumber;
@ -232,7 +244,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
}
getLocationMultiaddr: (
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
protocol: TransportProtocol | TransportProtocolPerIpVersion
) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
setLocationMultiaddr(multiaddr: Multiaddr): void {
@ -259,6 +271,32 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
}
}
getAllLocationMultiaddrs(): Multiaddr[] {
const multiaddrs = [];
for (const protocol of Object.values(TransportProtocolPerIpVersion)) {
const ma = this.getLocationMultiaddr(
protocol as TransportProtocolPerIpVersion
);
if (ma) multiaddrs.push(ma);
}
const _multiaddrs = this.multiaddrs ?? [];
multiaddrs.concat(_multiaddrs);
return multiaddrs;
}
get peerInfo(): PeerInfo | undefined {
const id = this.peerId;
if (!id) return;
return {
id,
multiaddrs: this.getAllLocationMultiaddrs(),
protocols: [],
};
}
/**
* Returns the full multiaddr from the ENR fields matching the provided
* `protocol` parameter.
@ -268,7 +306,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
* @param protocol
*/
getFullMultiaddr(
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
protocol: TransportProtocol | TransportProtocolPerIpVersion
): Multiaddr | undefined {
if (this.peerId) {
const locationMultiaddr = this.getLocationMultiaddr(protocol);

View File

@ -54,6 +54,7 @@
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-libp2p": "^1.1.1",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@multiformats/multiaddr": "^11.4.0",

View File

@ -1,4 +1,5 @@
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { Multiaddr } from "@multiformats/multiaddr";
export type ENRKey = string;
@ -32,6 +33,10 @@ export interface IEnr extends Map<ENRKey, ENRValue> {
udp6?: number;
multiaddrs?: Multiaddr[];
waku2?: Waku2;
peerInfo: PeerInfo | undefined;
/**
* @deprecated: use { @link IEnr.peerInfo } instead.
*/
getFullMultiaddrs(): Multiaddr[];
}

View File

@ -165,20 +165,25 @@ export class PeerExchangeDiscovery
continue;
}
const { peerId } = ENR;
const multiaddrs = ENR.getFullMultiaddrs();
if (!peerId || !multiaddrs || multiaddrs.length === 0) continue;
const peerInfo = ENR.peerInfo;
if (
(await this.components.peerStore.getTags(peerId)).find(
!peerInfo ||
!peerInfo.id ||
!peerInfo.multiaddrs ||
!peerInfo.multiaddrs.length
)
continue;
if (
(await this.components.peerStore.getTags(peerInfo.id)).find(
({ name }) => name === DEFAULT_PEER_EXCHANGE_TAG_NAME
)
)
continue;
await this.components.peerStore.tagPeer(
peerId,
peerInfo.id,
DEFAULT_PEER_EXCHANGE_TAG_NAME,
{
value:
@ -191,11 +196,7 @@ export class PeerExchangeDiscovery
this.dispatchEvent(
new CustomEvent<PeerInfo>("peer", {
detail: {
id: peerId,
multiaddrs,
protocols: [],
},
detail: peerInfo,
})
);
}

View File

@ -50,16 +50,13 @@
"node": ">=16"
},
"dependencies": {
"@libp2p/peer-id": "^2.0.2",
"debug": "^4.3.4",
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@multiformats/multiaddr": "^11.4.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",

View File

@ -1,9 +1,6 @@
import type { Connection } from "@libp2p/interface-connection";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
import { peerIdFromString } from "@libp2p/peer-id";
import type { Multiaddr } from "@multiformats/multiaddr";
import debug from "debug";
const log = debug("waku:libp2p-utils");
@ -78,20 +75,6 @@ export async function selectPeerForProtocol(
return { peer, protocol };
}
export function multiaddrsToPeerInfo(mas: Multiaddr[]): PeerInfo[] {
return mas
.map((ma) => {
const peerIdStr = ma.getPeerId();
const protocols: string[] = [];
return {
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
multiaddrs: [ma.decapsulateCode(421)],
protocols,
};
})
.filter((peerInfo): peerInfo is PeerInfo => peerInfo.id !== null);
}
export function selectConnection(
connections: Connection[]
): Connection | undefined {