Fix DNS Discovery (#411)

This commit is contained in:
Franck R 2022-01-17 14:21:23 +11:00 committed by GitHub
parent 96cf24d34e
commit e1629b1a96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 35 deletions

View File

@ -55,7 +55,7 @@ export class Bootstrap {
const maxPeers = opts.maxPeers ?? Bootstrap.DefaultMaxPeers;
if (opts.default) {
dbg('Bootstrap: Use hosted list of peers.');
dbg('Use hosted list of peers.');
this.getBootstrapPeers = getNodesFromHostedJson.bind(
{},
@ -64,7 +64,7 @@ export class Bootstrap {
maxPeers
);
} else if (opts.peers !== undefined && opts.peers.length > 0) {
dbg('Bootstrap: Use provided list of peers.');
dbg('Use provided list of peers.');
const allPeers: Multiaddr[] = opts.peers.map(
(node: string) => new Multiaddr(node)
@ -85,24 +85,14 @@ export class Bootstrap {
};
} else if (opts.enrUrl) {
const enrUrl = opts.enrUrl;
dbg('Bootstrap: Use provided EIP-1459 ENR Tree URL.');
dbg('Use provided EIP-1459 ENR Tree URL.');
const dns = DnsNodeDiscovery.dnsOverHttp();
this.getBootstrapPeers = async (): Promise<Multiaddr[]> => {
const enrs = await dns.getPeers(maxPeers, [enrUrl]);
const addresses: Multiaddr[] = [];
enrs.forEach((enr) => {
if (!enr.multiaddrs) return;
enr.multiaddrs.forEach((ma: Multiaddr) => {
// Only return secure websocket addresses
if (ma.protoNames().includes('wss')) {
addresses.push(ma);
}
});
});
return addresses;
dbg(`Found ${enrs.length} peers`);
return enrs.map((enr) => enr.getFullMultiaddrs()).flat();
};
} else {
dbg('No bootstrap method specified, no peer will be returned');

View File

@ -171,9 +171,8 @@ describe('DNS Node Discovery', () => {
describe('DNS Node Discovery [live data]', function () {
const publicKey = 'AOFTICU2XWDULNLZGRMQS4RIZPAZEHYMV4FYHAPW563HNRAOERP7C';
const fqdn = 'test.nodes.vac.dev';
const fqdn = 'test.waku.nodes.status.im';
const enrTree = `enrtree://${publicKey}@${fqdn}`;
const ipTestRegex = /^\d+\.\d+\.\d+\.\d+$/;
const maxQuantity = 3;
before(function () {
@ -182,22 +181,21 @@ describe('DNS Node Discovery [live data]', function () {
}
});
it(`should retrieve ${maxQuantity} PeerInfos for test.nodes.vac.dev`, async function () {
this.timeout(5000);
it(`should retrieve ${maxQuantity} multiaddrs for test.waku.nodes.status.im`, async function () {
this.timeout(10000);
// Google's dns server address. Needs to be set explicitly to run in CI
const dnsNodeDiscovery = DnsNodeDiscovery.dnsOverHttp();
const peers = await dnsNodeDiscovery.getPeers(maxQuantity, [enrTree]);
expect(peers.length).to.eq(maxQuantity);
// TODO: Test multiaddrs entry
console.log(peers.map((peer) => peer.multiaddrs));
const multiaddrs = peers.map((peer) => peer.multiaddrs).flat();
const seen: string[] = [];
for (const peer of peers) {
expect(peer!.ip!).to.match(ipTestRegex);
expect(seen).to.not.include(peer!.ip!);
seen.push(peer!.ip!);
for (const ma of multiaddrs) {
expect(ma).to.not.be.undefined;
expect(seen).to.not.include(ma!.toString());
seen.push(ma!.toString());
}
});
});

View File

@ -87,8 +87,7 @@ describe('ENR', function () {
expect(enr.ip).to.not.be.undefined;
expect(enr.ip).to.be.equal('134.209.139.210');
expect(enr.publicKey).to.not.be.undefined;
const peerId = await enr.peerId();
expect(peerId.toB58String()).to.be.equal(
expect(enr.peerId.toB58String()).to.be.equal(
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ'
);
});

View File

@ -136,7 +136,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
return createKeypair(this.keypairType, undefined, this.publicKey);
}
async peerId(): Promise<PeerId> {
get peerId(): PeerId {
return createPeerIdFromKeypair(this.keypair);
}
@ -416,17 +416,36 @@ export class ENR extends Map<ENRKey, ENRValue> {
}
}
async getFullMultiaddr(
/**
* Returns the full multiaddr from the ENR fields matching the provided
* `protocol` parameter.
* To return full multiaddrs from the `multiaddrs` ENR field,
* use [[ENR.getFullMultiaddrs]]
*
* @param protocol
*/
getFullMultiaddr(
protocol: 'udp' | 'udp4' | 'udp6' | 'tcp' | 'tcp4' | 'tcp6'
): Promise<Multiaddr | undefined> {
): Multiaddr | undefined {
const locationMultiaddr = this.getLocationMultiaddr(protocol);
if (locationMultiaddr) {
const peerId = await this.peerId();
return locationMultiaddr.encapsulate(`/p2p/${peerId.toB58String()}`);
return locationMultiaddr.encapsulate(`/p2p/${this.peerId.toB58String()}`);
}
return;
}
/**
* Returns the full multiaddrs from the `multiaddrs` ENR field.
*/
getFullMultiaddrs(): Multiaddr[] {
if (this.multiaddrs) {
return this.multiaddrs.map((ma) => {
return ma.encapsulate(`/p2p/${this.peerId.toB58String()}`);
});
}
return [];
}
verify(data: Buffer, signature: Buffer): boolean {
if (!this.get('id') || this.id !== 'v4') {
throw new Error(ERR_INVALID_ID);

View File

@ -33,9 +33,7 @@ export function createKeypair(
}
}
export async function createPeerIdFromKeypair(
keypair: IKeypair
): Promise<PeerId> {
export function createPeerIdFromKeypair(keypair: IKeypair): PeerId {
switch (keypair.type) {
case KeypairType.secp256k1: {
// manually create a peer id to avoid expensive ops