chore: new eslint rules (#1416)

* add new eslint rules

* remove eslint scripts for build-utils

* refactor all files to fix breaking changes with new rules

* add a basic eslintrc for build-utils

* test

* fix more linting errors

* test

* update package.lock

* update stopping container

* update px node spec

* enable tests

* fix: conn manager events

* fix: peer exchange

* increase timeout on px test
This commit is contained in:
Danish Arora 2023-07-24 16:04:24 +05:30 committed by GitHub
parent d2f675d690
commit 376bcf2a0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 181 additions and 135 deletions

View File

@ -49,7 +49,10 @@
"error",
{ "ignoreDeclarationSort": true, "ignoreCase": true }
],
"no-console": "error"
"no-console": "error",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/no-misused-promises": "error"
},
"overrides": [
{

15
package-lock.json generated
View File

@ -33,6 +33,7 @@
"gh-pages": "^5.0.0",
"husky": "^8.0.3",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"size-limit": "^8.1.2",
"typedoc": "^0.23.26",
"typedoc-plugin-resolve-crossmodule-references": "^0.3.3"
@ -31044,8 +31045,6 @@
"@types/debug": "^4.1.7",
"@types/mocha": "^10.0.1",
"@types/uuid": "^9.0.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"chai": "^4.3.7",
"cspell": "^6.31.1",
@ -31387,8 +31386,6 @@
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"cspell": "^6.31.1",
"npm-run-all": "^4.1.5",
@ -31575,6 +31572,8 @@
"@types/dockerode": "^3.3.17",
"@types/mocha": "^10.0.1",
"@types/tail": "^2.2.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/dns-discovery": "*",
"@waku/message-encryption": "*",
"@waku/peer-exchange": "*",
@ -37031,8 +37030,6 @@
"@types/debug": "^4.1.7",
"@types/mocha": "^10.0.1",
"@types/uuid": "^9.0.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.15",
"@waku/proto": "0.0.5",
@ -37272,8 +37269,6 @@
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"cspell": "^6.31.1",
"npm-run-all": "^4.1.5",
@ -37413,6 +37408,8 @@
"@types/dockerode": "^3.3.17",
"@types/mocha": "^10.0.1",
"@types/tail": "^2.2.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/core": "*",
"@waku/dns-discovery": "*",
"@waku/enr": "*",
@ -37431,7 +37428,7 @@
"p-timeout": "^6.1.0",
"portfinder": "^1.0.32",
"prettier": "^2.8.8",
"sinon": "*",
"sinon": "^15.2.0",
"tail": "^2.2.6",
"typescript": "^5.0.4"
}

View File

@ -0,0 +1,24 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
},
env: {
browser: true,
es2021: true,
node: true,
},
extends: ["eslint:recommended"],
rules: {
indent: ["error", 2],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"],
"no-unused-vars": [
"error",
{ vars: "all", args: "after-used", ignoreRestSiblings: false },
],
"no-console": "warn",
},
};

View File

@ -69,7 +69,9 @@ export class ConnectionManager {
// libp2p emits `peer:discovery` events during its initialization
// which means that before the ConnectionManager is initialized, some peers may have been discovered
// we will dial the peers in peerStore ONCE before we start to listen to the `peer:discovery` events within the ConnectionManager
this.dialPeerStorePeers();
this.dialPeerStorePeers().catch((error) =>
log(`Unexpected error while dialing peer store peers`, error)
);
}
private async dialPeerStorePeers(): Promise<void> {
@ -177,7 +179,7 @@ export class ConnectionManager {
}
}
private async processDialQueue(): Promise<void> {
private processDialQueue(): void {
if (
this.pendingPeerDialQueue.length > 0 &&
this.currentActiveDialCount < this.options.maxParallelDials
@ -237,37 +239,43 @@ export class ConnectionManager {
}
private onEventHandlers = {
"peer:discovery": async (evt: CustomEvent<PeerInfo>): Promise<void> => {
const { id: peerId } = evt.detail;
"peer:discovery": (evt: CustomEvent<PeerInfo>): void => {
void (async () => {
const { id: peerId } = evt.detail;
this.attemptDial(peerId).catch((err) =>
log(`Error dialing peer ${peerId.toString()} : ${err}`)
);
},
"peer:connect": async (evt: CustomEvent<Connection>): Promise<void> => {
const { remotePeer: peerId } = evt.detail;
this.keepAliveManager.start(
peerId,
this.libp2pComponents.ping.bind(this)
);
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
Tags.BOOTSTRAP
);
if (isBootstrap) {
const bootstrapConnections = this.libp2pComponents
.getConnections()
.filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
// If we have too many bootstrap connections, drop one
if (
bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
) {
await this.dropConnection(peerId);
try {
await this.attemptDial(peerId);
} catch (error) {
log(`Error dialing peer ${peerId.toString()} : ${error}`);
}
}
})();
},
"peer:connect": (evt: CustomEvent<Connection>): void => {
void (async () => {
const { remotePeer: peerId } = evt.detail;
this.keepAliveManager.start(
peerId,
this.libp2pComponents.ping.bind(this)
);
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
Tags.BOOTSTRAP
);
if (isBootstrap) {
const bootstrapConnections = this.libp2pComponents
.getConnections()
.filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
// If we have too many bootstrap connections, drop one
if (
bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
) {
await this.dropConnection(peerId);
}
}
})();
},
"peer:disconnect": () => {
return (evt: CustomEvent<Connection>): void => {

View File

@ -200,7 +200,7 @@ class Filter extends BaseProtocol implements IFilter {
// We don't want to wait for decoding failure, just attempt to decode
// all messages and do the call back on the one that works
// noinspection ES6MissingAwait
decoders.forEach(async (dec: IDecoder<T>) => {
for (const dec of decoders) {
if (didDecodeMsg) return;
const decoded = await dec.fromProtoObj(
pubSubTopic,
@ -208,13 +208,13 @@ class Filter extends BaseProtocol implements IFilter {
);
if (!decoded) {
log("Not able to decode message");
return;
continue;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
});
}
}
}

View File

@ -312,7 +312,7 @@ class FilterV2 extends BaseProtocol implements IReceiver {
): Promise<Unsubscribe> {
const subscription = await this.createSubscription(undefined, opts?.peerId);
subscription.subscribe(decoders, callback);
await subscription.subscribe(decoders, callback);
const contentTopics = Array.from(
groupByContentTopic(
@ -394,20 +394,19 @@ async function pushMessage<T extends IDecodedMessage>(
// We don't want to wait for decoding failure, just attempt to decode
// all messages and do the call back on the one that works
// noinspection ES6MissingAwait
decoders.forEach(async (dec: IDecoder<T>) => {
if (didDecodeMsg) return;
for (const dec of decoders) {
if (didDecodeMsg) break;
const decoded = await dec.fromProtoObj(
pubSubTopic,
message as IProtoMessage
);
// const decoded = await dec.fromProtoObj(pubSubTopic, message);
if (!decoded) {
log("Not able to decode message");
return;
continue;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
});
}
}

View File

@ -129,11 +129,11 @@ describe("ENR", function () {
}
});
it("should throw error - no public key", () => {
it("should throw error - no public key", async () => {
try {
const txt =
"enr:-IS4QJ2d11eu6dC7E7LoXeLMgMP3kom1u3SE8esFSWvaHoo0dP1jg8O3-nx9ht-EO3CmG7L6OkHcMmoIh00IYWB92QABgmlkgnY0gmlwhH8AAAGJc2d11eu6dCsxoQIB_c-jQMOXsbjWkbN-kj99H57gfId5pfb4wa1qxwV4CIN1ZHCCIyk";
EnrDecoder.fromString(txt);
await EnrDecoder.fromString(txt);
assert.fail("Expect error here");
} catch (err: unknown) {
const e = err as Error;
@ -306,7 +306,7 @@ describe("ENR", function () {
});
});
describe("Location multiaddr", async () => {
describe("Location multiaddr", () => {
const ip4 = "127.0.0.1";
const ip6 = "::1";
const tcp = 8080;
@ -414,7 +414,7 @@ describe("ENR", function () {
});
});
describe("waku2 key round trip", async () => {
describe("waku2 key round trip", () => {
let peerId;
let enr: ENR;
let waku2Protocols: Waku2;

View File

@ -127,7 +127,7 @@ export async function encrypt(
): Promise<Uint8Array> {
const ephemPrivateKey = randomBytes(32);
const sharedPx = await derive(ephemPrivateKey, publicKeyTo);
const sharedPx = derive(ephemPrivateKey, publicKeyTo);
const hash = await kdf(sharedPx, 32);

View File

@ -148,7 +148,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
return;
}
const res = await postCipher(payload);
const res = postCipher(payload);
if (!res) {
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);

View File

@ -138,7 +138,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
return;
}
const res = await postCipher(payload);
const res = postCipher(payload);
if (!res) {
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);

View File

@ -63,9 +63,9 @@ export class PeerExchangeDiscovery
private queryingPeers: Set<string> = new Set();
private queryAttempts: Map<string, number> = new Map();
private readonly eventHandler = async (
private readonly eventHandler = (
event: CustomEvent<PeerProtocolsChangeData>
): Promise<void> => {
): void => {
const { protocols, peerId } = event.detail;
if (
!protocols.includes(PeerExchangeCodec) ||
@ -143,9 +143,11 @@ export class PeerExchangeDiscovery
return;
}
setTimeout(async () => {
setTimeout(() => {
this.queryAttempts.set(peerIdStr, currentAttempt + 1);
await this.startRecurringQueries(peerId);
this.startRecurringQueries(peerId).catch((error) => {
log(`Error in startRecurringQueries: ${error}`);
});
}, queryInterval * currentAttempt);
};

View File

@ -193,18 +193,26 @@ class Relay implements IRelay {
return;
}
await Promise.all(
Array.from(observers).map(async ({ decoder, callback }) => {
const protoMsg = await decoder.fromWireToProtoObj(bytes);
if (!protoMsg) {
log("Internal error: message previously decoded failed on 2nd pass.");
return;
}
const msg = await decoder.fromProtoObj(pubSubTopic, protoMsg);
if (msg) {
callback(msg);
} else {
log("Failed to decode messages on", topicOnlyMsg.contentTopic);
}
Array.from(observers).map(({ decoder, callback }) => {
return (async () => {
try {
const protoMsg = await decoder.fromWireToProtoObj(bytes);
if (!protoMsg) {
log(
"Internal error: message previously decoded failed on 2nd pass."
);
return;
}
const msg = await decoder.fromProtoObj(pubSubTopic, protoMsg);
if (msg) {
await callback(msg);
} else {
log("Failed to decode messages on", topicOnlyMsg.contentTopic);
}
} catch (error) {
log("Error while decoding message:", error);
}
})();
})
);
}
@ -217,7 +225,7 @@ class Relay implements IRelay {
private gossipSubSubscribe(pubSubTopic: string): void {
this.gossipSub.addEventListener(
"gossipsub:message",
async (event: CustomEvent<GossipsubMessage>) => {
(event: CustomEvent<GossipsubMessage>) => {
if (event.detail.msg.topic !== pubSubTopic) return;
log(`Message received on ${pubSubTopic}`);

View File

@ -170,7 +170,7 @@ export default class Dockerode {
await this.container.stop();
await this.container.remove();
this.containerId = undefined;
delete this.containerId;
}
private async confirmImageExistsOrPull(): Promise<void> {

View File

@ -165,7 +165,8 @@ export class NimGoNode {
}
public async stop(): Promise<void> {
this.docker?.stop();
await this.docker?.container?.stop();
delete this.docker;
}
async waitForLog(msg: string, timeout: number): Promise<void> {

View File

@ -17,7 +17,7 @@ import { delay } from "../src/delay.js";
const maxQuantity = 3;
describe("DNS Discovery: Compliance Test", async function () {
describe("DNS Discovery: Compliance Test", function () {
this.timeout(10000);
tests({
async setup() {

View File

@ -57,8 +57,8 @@ describe("Waku Light Push [node only]", () => {
afterEach(async function () {
try {
nwaku?.stop();
waku?.stop();
await nwaku?.stop();
await waku?.stop();
} catch (e) {
console.error("Failed to stop nodes: ", e);
}
@ -124,8 +124,8 @@ describe("Waku Light Push [node only] - custom pubsub topic", () => {
afterEach(async function () {
try {
nwaku?.stop();
waku?.stop();
await nwaku?.stop();
await waku?.stop();
} catch (e) {
console.error("Failed to stop nodes: ", e);
}

View File

@ -19,49 +19,51 @@ import { makeLogFileName } from "../src/log_file.js";
import { NimGoNode } from "../src/node/node.js";
describe("Peer Exchange", () => {
let waku: LightNode;
describe("Auto Discovery", function () {
let waku: LightNode;
afterEach(async function () {
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("Auto discovery", async function () {
// skipping in CI as this test demonstrates Peer Exchange working with the test fleet
// but not with locally run nwaku nodes
if (process.env.CI) {
this.skip();
}
this.timeout(50_000);
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: getPredefinedBootstrapNodes(Fleet.Test, 3) }),
wakuPeerExchangeDiscovery(),
],
},
afterEach(async function () {
await waku?.stop();
});
await waku.start();
it("connection with fleet nodes", async function () {
// skipping in CI as this test demonstrates Peer Exchange working with the test fleet
// but not with locally run nwaku nodes
if (process.env.CI) {
this.skip();
}
const foundPxPeer = await new Promise<boolean>((resolve) => {
const testNodes = getPredefinedBootstrapNodes(Fleet.Test, 3);
waku.libp2p.addEventListener("peer:discovery", (evt) => {
const { multiaddrs } = evt.detail;
multiaddrs.forEach((ma) => {
const isBootstrapNode = testNodes.find((n) => n === ma.toString());
if (!isBootstrapNode) {
resolve(true);
}
this.timeout(50_000);
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: getPredefinedBootstrapNodes(Fleet.Test, 3) }),
wakuPeerExchangeDiscovery(),
],
},
});
await waku.start();
const foundPxPeer = await new Promise<boolean>((resolve) => {
const testNodes = getPredefinedBootstrapNodes(Fleet.Test, 3);
waku.libp2p.addEventListener("peer:discovery", (evt) => {
const { multiaddrs } = evt.detail;
multiaddrs.forEach((ma) => {
const isBootstrapNode = testNodes.find((n) => n === ma.toString());
if (!isBootstrapNode) {
resolve(true);
}
});
});
});
});
expect(foundPxPeer).to.be.true;
expect(foundPxPeer).to.be.true;
});
});
describe("Locally run nodes", () => {
describe("Locally Run Nodes", () => {
let waku: LightNode;
let nwaku1: NimGoNode;
let nwaku2: NimGoNode;
@ -72,9 +74,10 @@ describe("Peer Exchange", () => {
});
afterEach(async function () {
!!nwaku1 && nwaku1.stop();
!!nwaku2 && nwaku2.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
this.timeout(10_000);
await nwaku1?.stop();
await nwaku2?.stop();
await waku?.stop();
});
it("nwaku interop", async function () {
@ -146,7 +149,7 @@ describe("Peer Exchange", () => {
});
});
describe("compliance test", async function () {
describe("Compliance Test", function () {
this.timeout(55_000);
let waku: LightNode;
@ -201,9 +204,9 @@ describe("Peer Exchange", () => {
return new PeerExchangeDiscovery(components);
},
teardown: async () => {
!!nwaku1 && (await nwaku1.stop());
!!nwaku2 && (await nwaku2.stop());
!!waku && (await waku.stop());
await nwaku1?.stop();
await nwaku2?.stop();
await waku?.stop();
},
});
});

View File

@ -121,7 +121,7 @@ describe("Waku Relay [node only]", () => {
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([TestDecoder], resolve);
void waku2.relay.subscribe([TestDecoder], resolve);
}
);
@ -152,12 +152,12 @@ describe("Waku Relay [node only]", () => {
const barDecoder = createDecoder(barContentTopic);
const fooMessages: DecodedMessage[] = [];
waku2.relay.subscribe([fooDecoder], (msg) => {
void waku2.relay.subscribe([fooDecoder], (msg) => {
fooMessages.push(msg);
});
const barMessages: DecodedMessage[] = [];
waku2.relay.subscribe([barDecoder], (msg) => {
void waku2.relay.subscribe([barDecoder], (msg) => {
barMessages.push(msg);
});
@ -207,10 +207,10 @@ describe("Waku Relay [node only]", () => {
const symDecoder = createSymDecoder(symTopic, symKey);
const msgs: DecodedMessage[] = [];
waku2.relay.subscribe([eciesDecoder], (wakuMsg) => {
void waku2.relay.subscribe([eciesDecoder], (wakuMsg) => {
msgs.push(wakuMsg);
});
waku2.relay.subscribe([symDecoder], (wakuMsg) => {
void waku2.relay.subscribe([symDecoder], (wakuMsg) => {
msgs.push(wakuMsg);
});
@ -313,7 +313,7 @@ describe("Waku Relay [node only]", () => {
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([TestDecoder], resolve);
void waku2.relay.subscribe([TestDecoder], resolve);
}
);
@ -321,7 +321,7 @@ describe("Waku Relay [node only]", () => {
// pubsub topic.
const waku3NoMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve, reject) => {
waku3.relay.subscribe([TestDecoder], reject);
void waku3.relay.subscribe([TestDecoder], reject);
setTimeout(resolve, 1000);
}
);
@ -369,7 +369,7 @@ describe("Waku Relay [node only]", () => {
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([TestDecoder], () =>
void waku2.relay.subscribe([TestDecoder], () =>
resolve({
payload: new Uint8Array([]),
} as DecodedMessage)
@ -463,7 +463,7 @@ describe("Waku Relay [node only]", () => {
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku.relay.subscribe<DecodedMessage>(TestDecoder, (msg) =>
void waku.relay.subscribe<DecodedMessage>(TestDecoder, (msg) =>
resolve(msg)
);
}
@ -535,7 +535,7 @@ describe("Waku Relay [node only]", () => {
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe(TestDecoder, resolve);
void waku2.relay.subscribe(TestDecoder, resolve);
}
);

View File

@ -90,7 +90,8 @@ describe("Wait for remote peer", function () {
done();
}
);
});
})
.catch((e) => done(e));
});
it("Store - dialed first", async function () {

View File

@ -63,7 +63,7 @@ describe("Waku Dial [node only]", function () {
let nwaku: NimGoNode;
afterEach(async function () {
!!nwaku && nwaku.stop();
!!nwaku && (await nwaku.stop());
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
@ -175,7 +175,7 @@ describe("Decryption Keys", () => {
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([decoder], resolve);
void waku2.relay.subscribe([decoder], resolve);
}
);