git.delta.rocks / unique-network / refs/commits / 31fca6536571

difftreelog

Add no-floating-promises rule to eslint

Max Andreev2022-12-22parent: #72b9c7c.patch.diff
in: master

6 files changed

modifiedtests/.eslintrc.jsondiffbeforeafterboth
--- a/tests/.eslintrc.json
+++ b/tests/.eslintrc.json
@@ -18,6 +18,9 @@
         "mocha"
     ],
     "rules": {
+        "@typescript-eslint/no-floating-promises": [
+            "error"
+        ],
         "indent": [
             "error",
             2,
modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -11,8 +11,8 @@
     "@types/chai-subset": "^1.3.3",
     "@types/mocha": "^10.0.0",
     "@types/node": "^18.11.2",
-    "@typescript-eslint/eslint-plugin": "^5.40.1",
-    "@typescript-eslint/parser": "^5.40.1",
+    "@typescript-eslint/eslint-plugin": "^5.47.0",
+    "@typescript-eslint/parser": "^5.47.0",
     "chai": "^4.3.6",
     "chai-subset": "^1.6.0",
     "eslint": "^8.25.0",
modifiedtests/src/calibrate.tsdiffbeforeafterboth
--- a/tests/src/calibrate.ts
+++ b/tests/src/calibrate.ts
@@ -295,6 +295,7 @@
   }
 }
 
+// eslint-disable-next-line @typescript-eslint/no-floating-promises
 (async () => {
   await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {
     // Subsequent runs reduce error, as price line is not actually straight, this is a curve
modifiedtests/src/transfer.nload.tsdiffbeforeafterboth
before · tests/src/transfer.nload.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import os from 'os';18import {IKeyringPair} from '@polkadot/types/types';19import {usingPlaygrounds} from './util';20import {UniqueHelper} from './util/playgrounds/unique';21import * as notReallyCluster from 'cluster'; // https://github.com/nodejs/node/issues/42271#issuecomment-106341534622const cluster = notReallyCluster as unknown as notReallyCluster.Cluster;2324async function findUnusedAddress(helper: UniqueHelper, privateKey: (account: string) => Promise<IKeyringPair>, seedAddition = ''): Promise<IKeyringPair> {25  let bal = 0n;26  let unused;27  do {28    const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;29    unused = await privateKey(`//${randomSeed}`);30    bal = await helper.balance.getSubstrate(unused.address);31  } while (bal !== 0n);32  return unused;33}3435function findUnusedAddresses(helper: UniqueHelper, privateKey: (account: string) => Promise<IKeyringPair>, amount: number): Promise<IKeyringPair[]> {36  return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(helper, privateKey, '_' + Date.now())));37}3839// Innacurate transfer fee40const FEE = 10n ** 8n;4142let counters: { [key: string]: number } = {};43function increaseCounter(name: string, amount: number) {44  if (!counters[name]) {45    counters[name] = 0;46  }47  counters[name] += amount;48}49function flushCounterToMaster() {50  if (Object.keys(counters).length === 0) {51    return;52  }53    process.send!(counters);54    counters = {};55}5657async function distributeBalance(source: IKeyringPair, helper: UniqueHelper, privateKey: (account: string) => Promise<IKeyringPair>, totalAmount: bigint, stages: number) {58  const accounts = [source];59  // we don't need source in output array60  const failedAccounts = [0];6162  const finalUserAmount = 2 ** stages - 1;63  accounts.push(...await findUnusedAddresses(helper, privateKey, finalUserAmount));64  // findUnusedAddresses produces at least 1 request per user65  increaseCounter('requests', finalUserAmount);6667  for (let stage = 0; stage < stages; stage++) {68    const usersWithBalance = 2 ** stage;69    const amount = totalAmount / (2n ** BigInt(stage)) - FEE * BigInt(stage);70    // console.log(`Stage ${stage}/${stages}, ${usersWithBalance} => ${usersWithBalance * 2} = ${amount}`);71    const txs: Promise<boolean | void>[] = [];72    for (let i = 0; i < usersWithBalance; i++) {73      const newUser = accounts[i + usersWithBalance];74      // console.log(`${accounts[i].address} => ${newUser.address} = ${amountToSplit}`);75      const tx = helper.balance.transferToSubstrate(accounts[i], newUser.address, amount);76      txs.push(tx.catch(() => {77        failedAccounts.push(i + usersWithBalance);78        increaseCounter('txFailed', 1);79      }));80      increaseCounter('tx', 1);81    }82    await Promise.all(txs);83  }8485  for (const account of failedAccounts.reverse()) {86    accounts.splice(account, 1);87  }88  return accounts;89}9091if (cluster.isMaster) {92  let testDone = false;93  usingPlaygrounds(async (helper) => {94    const prevCounters: { [key: string]: number } = {};95    while (!testDone) {96      for (const name in counters) {97        if (!(name in prevCounters)) {98          prevCounters[name] = 0;99        }100        if(counters[name] === prevCounters[name]) {101          continue;102        }103        console.log(`${name.padEnd(15)} = ${counters[name] - prevCounters[name]}`);104        prevCounters[name] = counters[name];105      }106      await helper.wait.newBlocks(1);107    }108  });109  const waiting: Promise<void>[] = [];110  console.log(`Starting ${os.cpus().length} workers`);111  usingPlaygrounds(async (helper, privateKey) => {112    const alice = await privateKey('//Alice');113    for (const id in os.cpus()) {114      const WORKER_NAME = `//LoadWorker${id}_${Date.now()}`;115      const workerAccount = await privateKey(WORKER_NAME);116      await helper.balance.transferToSubstrate(alice, workerAccount.address, 400n * 10n ** 23n);117118      const worker = cluster.fork({119        WORKER_NAME,120        STAGES: id + 2,121      });122      worker.on('message', msg => {123        for (const key in msg) {124          increaseCounter(key, msg[key]);125        }126      });127      waiting.push(new Promise(res => worker.on('exit', res)));128    }129    await Promise.all(waiting);130    testDone = true;131  });132} else {133  increaseCounter('startedWorkers', 1);134  usingPlaygrounds(async (helper, privateKey) => {135    await distributeBalance(await privateKey(process.env.WORKER_NAME as string), helper, privateKey, 400n * 10n ** 22n, 10);136  });137  const interval = setInterval(() => {138    flushCounterToMaster();139  }, 100);140  interval.unref();141}
after · tests/src/transfer.nload.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617/* eslint-disable @typescript-eslint/no-floating-promises */18import os from 'os';19import {IKeyringPair} from '@polkadot/types/types';20import {usingPlaygrounds} from './util';21import {UniqueHelper} from './util/playgrounds/unique';22import * as notReallyCluster from 'cluster'; // https://github.com/nodejs/node/issues/42271#issuecomment-106341534623const cluster = notReallyCluster as unknown as notReallyCluster.Cluster;2425async function findUnusedAddress(helper: UniqueHelper, privateKey: (account: string) => Promise<IKeyringPair>, seedAddition = ''): Promise<IKeyringPair> {26  let bal = 0n;27  let unused;28  do {29    const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;30    unused = await privateKey(`//${randomSeed}`);31    bal = await helper.balance.getSubstrate(unused.address);32  } while (bal !== 0n);33  return unused;34}3536function findUnusedAddresses(helper: UniqueHelper, privateKey: (account: string) => Promise<IKeyringPair>, amount: number): Promise<IKeyringPair[]> {37  return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(helper, privateKey, '_' + Date.now())));38}3940// Innacurate transfer fee41const FEE = 10n ** 8n;4243let counters: { [key: string]: number } = {};44function increaseCounter(name: string, amount: number) {45  if (!counters[name]) {46    counters[name] = 0;47  }48  counters[name] += amount;49}50function flushCounterToMaster() {51  if (Object.keys(counters).length === 0) {52    return;53  }54    process.send!(counters);55    counters = {};56}5758async function distributeBalance(source: IKeyringPair, helper: UniqueHelper, privateKey: (account: string) => Promise<IKeyringPair>, totalAmount: bigint, stages: number) {59  const accounts = [source];60  // we don't need source in output array61  const failedAccounts = [0];6263  const finalUserAmount = 2 ** stages - 1;64  accounts.push(...await findUnusedAddresses(helper, privateKey, finalUserAmount));65  // findUnusedAddresses produces at least 1 request per user66  increaseCounter('requests', finalUserAmount);6768  for (let stage = 0; stage < stages; stage++) {69    const usersWithBalance = 2 ** stage;70    const amount = totalAmount / (2n ** BigInt(stage)) - FEE * BigInt(stage);71    // console.log(`Stage ${stage}/${stages}, ${usersWithBalance} => ${usersWithBalance * 2} = ${amount}`);72    const txs: Promise<boolean | void>[] = [];73    for (let i = 0; i < usersWithBalance; i++) {74      const newUser = accounts[i + usersWithBalance];75      // console.log(`${accounts[i].address} => ${newUser.address} = ${amountToSplit}`);76      const tx = helper.balance.transferToSubstrate(accounts[i], newUser.address, amount);77      txs.push(tx.catch(() => {78        failedAccounts.push(i + usersWithBalance);79        increaseCounter('txFailed', 1);80      }));81      increaseCounter('tx', 1);82    }83    await Promise.all(txs);84  }8586  for (const account of failedAccounts.reverse()) {87    accounts.splice(account, 1);88  }89  return accounts;90}9192if (cluster.isMaster) {93  let testDone = false;94  usingPlaygrounds(async (helper) => {95    const prevCounters: { [key: string]: number } = {};96    while (!testDone) {97      for (const name in counters) {98        if (!(name in prevCounters)) {99          prevCounters[name] = 0;100        }101        if(counters[name] === prevCounters[name]) {102          continue;103        }104        console.log(`${name.padEnd(15)} = ${counters[name] - prevCounters[name]}`);105        prevCounters[name] = counters[name];106      }107      await helper.wait.newBlocks(1);108    }109  });110  const waiting: Promise<void>[] = [];111  console.log(`Starting ${os.cpus().length} workers`);112  usingPlaygrounds(async (helper, privateKey) => {113    const alice = await privateKey('//Alice');114    for (const id in os.cpus()) {115      const WORKER_NAME = `//LoadWorker${id}_${Date.now()}`;116      const workerAccount = await privateKey(WORKER_NAME);117      await helper.balance.transferToSubstrate(alice, workerAccount.address, 400n * 10n ** 23n);118119      const worker = cluster.fork({120        WORKER_NAME,121        STAGES: id + 2,122      });123      worker.on('message', msg => {124        for (const key in msg) {125          increaseCounter(key, msg[key]);126        }127      });128      waiting.push(new Promise(res => worker.on('exit', res)));129    }130    await Promise.all(waiting);131    testDone = true;132  });133} else {134  increaseCounter('startedWorkers', 1);135  usingPlaygrounds(async (helper, privateKey) => {136    await distributeBalance(await privateKey(process.env.WORKER_NAME as string), helper, privateKey, 400n * 10n ** 22n, 10);137  });138  const interval = setInterval(() => {139    flushCounterToMaster();140  }, 100);141  interval.unref();142}
modifiedtests/src/util/globalSetup.tsdiffbeforeafterboth
--- a/tests/src/util/globalSetup.ts
+++ b/tests/src/util/globalSetup.ts
@@ -17,7 +17,7 @@
       // 2. Create donors for test files
       await fundFilenamesWithRetries(3)
         .then((result) => {
-          if (!result) Promise.reject();
+          if (!result) throw Error('Some problems with fundFilenamesWithRetries');
         });
 
       // 3. Configure App Promotion
@@ -38,7 +38,7 @@
       }
     } catch (error) {
       console.error(error);
-      Promise.reject();
+      throw Error('Error during globalSetup');
     }
   });
 };
modifiedtests/yarn.lockdiffbeforeafterboth
--- a/tests/yarn.lock
+++ b/tests/yarn.lock
@@ -1064,14 +1064,14 @@
   dependencies:
     "@types/node" "*"
 
-"@typescript-eslint/eslint-plugin@^5.40.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.46.1.tgz#098abb4c9354e19f460d57ab18bff1f676a6cff0"
-  integrity sha512-YpzNv3aayRBwjs4J3oz65eVLXc9xx0PDbIRisHj+dYhvBn02MjYOD96P8YGiWEIFBrojaUjxvkaUpakD82phsA==
+"@typescript-eslint/eslint-plugin@^5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.47.0.tgz#dadb79df3b0499699b155839fd6792f16897d910"
+  integrity sha512-AHZtlXAMGkDmyLuLZsRpH3p4G/1iARIwc/T0vIem2YB+xW6pZaXYXzCBnZSF/5fdM97R9QqZWZ+h3iW10XgevQ==
   dependencies:
-    "@typescript-eslint/scope-manager" "5.46.1"
-    "@typescript-eslint/type-utils" "5.46.1"
-    "@typescript-eslint/utils" "5.46.1"
+    "@typescript-eslint/scope-manager" "5.47.0"
+    "@typescript-eslint/type-utils" "5.47.0"
+    "@typescript-eslint/utils" "5.47.0"
     debug "^4.3.4"
     ignore "^5.2.0"
     natural-compare-lite "^1.4.0"
@@ -1079,72 +1079,72 @@
     semver "^7.3.7"
     tsutils "^3.21.0"
 
-"@typescript-eslint/parser@^5.40.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.46.1.tgz#1fc8e7102c1141eb64276c3b89d70da8c0ba5699"
-  integrity sha512-RelQ5cGypPh4ySAtfIMBzBGyrNerQcmfA1oJvPj5f+H4jI59rl9xxpn4bonC0tQvUKOEN7eGBFWxFLK3Xepneg==
+"@typescript-eslint/parser@^5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.47.0.tgz#62e83de93499bf4b500528f74bf2e0554e3a6c8d"
+  integrity sha512-udPU4ckK+R1JWCGdQC4Qa27NtBg7w020ffHqGyAK8pAgOVuNw7YaKXGChk+udh+iiGIJf6/E/0xhVXyPAbsczw==
   dependencies:
-    "@typescript-eslint/scope-manager" "5.46.1"
-    "@typescript-eslint/types" "5.46.1"
-    "@typescript-eslint/typescript-estree" "5.46.1"
+    "@typescript-eslint/scope-manager" "5.47.0"
+    "@typescript-eslint/types" "5.47.0"
+    "@typescript-eslint/typescript-estree" "5.47.0"
     debug "^4.3.4"
 
-"@typescript-eslint/scope-manager@5.46.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.46.1.tgz#70af8425c79bbc1178b5a63fb51102ddf48e104a"
-  integrity sha512-iOChVivo4jpwUdrJZyXSMrEIM/PvsbbDOX1y3UCKjSgWn+W89skxWaYXACQfxmIGhPVpRWK/VWPYc+bad6smIA==
+"@typescript-eslint/scope-manager@5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.47.0.tgz#f58144a6b0ff58b996f92172c488813aee9b09df"
+  integrity sha512-dvJab4bFf7JVvjPuh3sfBUWsiD73aiftKBpWSfi3sUkysDQ4W8x+ZcFpNp7Kgv0weldhpmMOZBjx1wKN8uWvAw==
   dependencies:
-    "@typescript-eslint/types" "5.46.1"
-    "@typescript-eslint/visitor-keys" "5.46.1"
+    "@typescript-eslint/types" "5.47.0"
+    "@typescript-eslint/visitor-keys" "5.47.0"
 
-"@typescript-eslint/type-utils@5.46.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.46.1.tgz#195033e4b30b51b870dfcf2828e88d57b04a11cc"
-  integrity sha512-V/zMyfI+jDmL1ADxfDxjZ0EMbtiVqj8LUGPAGyBkXXStWmCUErMpW873zEHsyguWCuq2iN4BrlWUkmuVj84yng==
+"@typescript-eslint/type-utils@5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.47.0.tgz#2b440979c574e317d3473225ae781f292c99e55d"
+  integrity sha512-1J+DFFrYoDUXQE1b7QjrNGARZE6uVhBqIvdaXTe5IN+NmEyD68qXR1qX1g2u4voA+nCaelQyG8w30SAOihhEYg==
   dependencies:
-    "@typescript-eslint/typescript-estree" "5.46.1"
-    "@typescript-eslint/utils" "5.46.1"
+    "@typescript-eslint/typescript-estree" "5.47.0"
+    "@typescript-eslint/utils" "5.47.0"
     debug "^4.3.4"
     tsutils "^3.21.0"
 
-"@typescript-eslint/types@5.46.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.46.1.tgz#4e9db2107b9a88441c4d5ecacde3bb7a5ebbd47e"
-  integrity sha512-Z5pvlCaZgU+93ryiYUwGwLl9AQVB/PQ1TsJ9NZ/gHzZjN7g9IAn6RSDkpCV8hqTwAiaj6fmCcKSQeBPlIpW28w==
+"@typescript-eslint/types@5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.47.0.tgz#67490def406eaa023dbbd8da42ee0d0c9b5229d3"
+  integrity sha512-eslFG0Qy8wpGzDdYKu58CEr3WLkjwC5Usa6XbuV89ce/yN5RITLe1O8e+WFEuxnfftHiJImkkOBADj58ahRxSg==
 
-"@typescript-eslint/typescript-estree@5.46.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.46.1.tgz#5358088f98a8f9939355e0996f9c8f41c25eced2"
-  integrity sha512-j9W4t67QiNp90kh5Nbr1w92wzt+toiIsaVPnEblB2Ih2U9fqBTyqV9T3pYWZBRt6QoMh/zVWP59EpuCjc4VRBg==
+"@typescript-eslint/typescript-estree@5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.47.0.tgz#ed971a11c5c928646d6ba7fc9dfdd6e997649aca"
+  integrity sha512-LxfKCG4bsRGq60Sqqu+34QT5qT2TEAHvSCCJ321uBWywgE2dS0LKcu5u+3sMGo+Vy9UmLOhdTw5JHzePV/1y4Q==
   dependencies:
-    "@typescript-eslint/types" "5.46.1"
-    "@typescript-eslint/visitor-keys" "5.46.1"
+    "@typescript-eslint/types" "5.47.0"
+    "@typescript-eslint/visitor-keys" "5.47.0"
     debug "^4.3.4"
     globby "^11.1.0"
     is-glob "^4.0.3"
     semver "^7.3.7"
     tsutils "^3.21.0"
 
-"@typescript-eslint/utils@5.46.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.46.1.tgz#7da3c934d9fd0eb4002a6bb3429f33298b469b4a"
-  integrity sha512-RBdBAGv3oEpFojaCYT4Ghn4775pdjvwfDOfQ2P6qzNVgQOVrnSPe5/Pb88kv7xzYQjoio0eKHKB9GJ16ieSxvA==
+"@typescript-eslint/utils@5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.47.0.tgz#b5005f7d2696769a1fdc1e00897005a25b3a0ec7"
+  integrity sha512-U9xcc0N7xINrCdGVPwABjbAKqx4GK67xuMV87toI+HUqgXj26m6RBp9UshEXcTrgCkdGYFzgKLt8kxu49RilDw==
   dependencies:
     "@types/json-schema" "^7.0.9"
     "@types/semver" "^7.3.12"
-    "@typescript-eslint/scope-manager" "5.46.1"
-    "@typescript-eslint/types" "5.46.1"
-    "@typescript-eslint/typescript-estree" "5.46.1"
+    "@typescript-eslint/scope-manager" "5.47.0"
+    "@typescript-eslint/types" "5.47.0"
+    "@typescript-eslint/typescript-estree" "5.47.0"
     eslint-scope "^5.1.1"
     eslint-utils "^3.0.0"
     semver "^7.3.7"
 
-"@typescript-eslint/visitor-keys@5.46.1":
-  version "5.46.1"
-  resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.46.1.tgz#126cc6fe3c0f83608b2b125c5d9daced61394242"
-  integrity sha512-jczZ9noovXwy59KjRTk1OftT78pwygdcmCuBf8yMoWt/8O8l+6x2LSEze0E4TeepXK4MezW3zGSyoDRZK7Y9cg==
+"@typescript-eslint/visitor-keys@5.47.0":
+  version "5.47.0"
+  resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.47.0.tgz#4aca4efbdf6209c154df1f7599852d571b80bb45"
+  integrity sha512-ByPi5iMa6QqDXe/GmT/hR6MZtVPi0SqMQPDx15FczCBXJo/7M8T88xReOALAfpBLm+zxpPfmhuEvPb577JRAEg==
   dependencies:
-    "@typescript-eslint/types" "5.46.1"
+    "@typescript-eslint/types" "5.47.0"
     eslint-visitor-keys "^3.3.0"
 
 abortcontroller-polyfill@^1.7.3: