From 01a3d2362507857bb6307e9f48065fcae5e5eb9d Mon Sep 17 00:00:00 2001 From: JSKitty Date: Sun, 17 Nov 2024 15:48:33 +0000 Subject: [PATCH 1/2] Fix all linting warnings --- cypress.config.js | 2 +- scripts/chain_params.js | 3 --- scripts/database.js | 3 +-- scripts/global.js | 1 - scripts/network/__mocks__/network_manager.js | 2 +- scripts/network/network.js | 14 +++++++------- scripts/network/network_manager.js | 8 ++++---- scripts/wallet.js | 6 +++--- 8 files changed, 17 insertions(+), 22 deletions(-) diff --git a/cypress.config.js b/cypress.config.js index 8d621707b..759bf51a4 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -1,5 +1,5 @@ import { defineConfig } from 'cypress'; -import { existsSync, readFileSync } from 'fs'; +import { existsSync } from 'fs'; import cypressPlayback from '@oreillymedia/cypress-playback/addTasks.js'; export default defineConfig({ e2e: { diff --git a/scripts/chain_params.js b/scripts/chain_params.js index 329c4c076..c0722bb05 100644 --- a/scripts/chain_params.js +++ b/scripts/chain_params.js @@ -12,9 +12,6 @@ export const COIN = 10 ** 8; /** The maximum gap (absence of transactions within a range of derived addresses) before an account search ends */ export const MAX_ACCOUNT_GAP = 20; -/** The batch size of Shield block synchronisation */ -export const SHIELD_BATCH_SYNC_SIZE = 32; - /** Transaction Sapling Version */ export const SAPLING_TX_VERSION = 3; diff --git a/scripts/database.js b/scripts/database.js index 393583131..8ff6ace93 100644 --- a/scripts/database.js +++ b/scripts/database.js @@ -2,10 +2,9 @@ import { openDB } from 'idb'; import Masternode from './masternode.js'; import { Settings } from './settings.js'; import { cChainParams } from './chain_params.js'; -import { confirmPopup, sanitizeHTML, isSameType, isEmpty } from './misc.js'; +import { isSameType, isEmpty } from './misc.js'; import { createAlert } from './alerts/alert.js'; import { PromoWallet } from './promos.js'; -import { ALERTS, translation } from './i18n.js'; import { Account } from './accounts.js'; import { COutpoint, CTxIn, CTxOut, Transaction } from './transaction.js'; import { debugError, debugLog, DebugTopics } from './debug.js'; diff --git a/scripts/global.js b/scripts/global.js index 451faa2ac..1c296ccf3 100644 --- a/scripts/global.js +++ b/scripts/global.js @@ -28,7 +28,6 @@ import Stake from './stake/Stake.vue'; import { createPinia } from 'pinia'; import { cOracle } from './prices.js'; -import pIconCopy from '../assets/icons/icon-copy.svg'; import pIconCheck from '../assets/icons/icon-check.svg'; import SideNavbar from './SideNavbar.vue'; import { AsyncInterval } from './async_interval.js'; diff --git a/scripts/network/__mocks__/network_manager.js b/scripts/network/__mocks__/network_manager.js index 5e99a2782..f5c7f6d98 100644 --- a/scripts/network/__mocks__/network_manager.js +++ b/scripts/network/__mocks__/network_manager.js @@ -39,7 +39,7 @@ class TestNetwork { return 1; }); - getTxPage = vi.fn((nStartHeight, addr, n) => { + getTxPage = vi.fn((nStartHeight, addr, _n) => { if (addr === 'DTSTGkncpC86sbEUZ2rCBLEe2aXSeZPLnC') { // 1) Legacy mainnet wallet // tx_1 provides a spendable balance of 0.1 * 10^8 satoshi diff --git a/scripts/network/network.js b/scripts/network/network.js index 2ccf72acb..a0767dfba 100644 --- a/scripts/network/network.js +++ b/scripts/network/network.js @@ -121,13 +121,13 @@ export class Network { } async submitProposal({ - name, - url, - nPayments, - start, - address, - monthlyPayment, - txid, + _name, + _url, + _nPayments, + _start, + _address, + _monthlyPayment, + _txid, }) { throw new Error('submitProposal must be implemented'); } diff --git a/scripts/network/network_manager.js b/scripts/network/network_manager.js index 5425861be..57ee11a3e 100644 --- a/scripts/network/network_manager.js +++ b/scripts/network/network_manager.js @@ -1,4 +1,4 @@ -import { ExplorerNetwork, Network, RPCNodeNetwork } from './network.js'; +import { ExplorerNetwork, RPCNodeNetwork } from './network.js'; import { cChainParams } from '../chain_params.js'; import { fAutoSwitch } from '../settings.js'; import { debugLog, DebugTopics, debugWarn } from '../debug.js'; @@ -7,17 +7,17 @@ import { getEventEmitter } from '../event_bus.js'; class NetworkManager { /** - * @type {Network} - Current selected Explorer + * @type {import('./network.js').Network} - Current selected Explorer */ #currentExplorer; /** - * @type {Network} - Current selected RPC node + * @type {import('./network.js').Network} - Current selected RPC node */ #currentNode; /** - * @type {Array} - List of all available Networks + * @type {Array} - List of all available Networks */ #networks = []; diff --git a/scripts/wallet.js b/scripts/wallet.js index d5aa0443a..1fd5ef567 100644 --- a/scripts/wallet.js +++ b/scripts/wallet.js @@ -1,9 +1,9 @@ import { validateMnemonic } from 'bip39'; import { decrypt } from './aes-gcm.js'; -import { bytesToNum, mergeUint8Arrays, parseWIF } from './encoding.js'; +import { bytesToNum, parseWIF } from './encoding.js'; import { beforeUnloadListener, blockCount } from './global.js'; import { getNetwork } from './network/network_manager.js'; -import { MAX_ACCOUNT_GAP, SHIELD_BATCH_SYNC_SIZE } from './chain_params.js'; +import { MAX_ACCOUNT_GAP } from './chain_params.js'; import { HistoricalTx, HistoricalTxType } from './historical_tx.js'; import { COutpoint, Transaction } from './transaction.js'; import { confirmPopup, isShieldAddress } from './misc.js'; @@ -15,7 +15,7 @@ import { Database } from './database.js'; import { RECEIVE_TYPES } from './contacts-book.js'; import { Account } from './accounts.js'; import { fAdvancedMode } from './settings.js'; -import { bytesToHex, hexToBytes, sleep, startBatch } from './utils.js'; +import { bytesToHex, hexToBytes, sleep } from './utils.js'; import { strHardwareName } from './ledger.js'; import { OutpointState, Mempool } from './mempool.js'; import { getEventEmitter } from './event_bus.js'; From b5f14cacf62f14d1c3393e1f6129331be7965117 Mon Sep 17 00:00:00 2001 From: JSKitty Date: Sun, 17 Nov 2024 15:58:41 +0000 Subject: [PATCH 2/2] Remove unused `startBatch()` --- scripts/utils.js | 50 ---------------------------------- tests/unit/start_batch.test.js | 39 -------------------------- 2 files changed, 89 deletions(-) delete mode 100644 tests/unit/start_batch.test.js diff --git a/scripts/utils.js b/scripts/utils.js index bcd78d6f3..2a34c1dcf 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -75,56 +75,6 @@ export function arrayToCSV(data) { .join('\r\n'); // rows starting on new lines } -/** - * Start a batch of promises, processing them concurrently up to `batchSize`. - * This does *not* run them in parallel. Only 1 CPU core is used - * @template T - * @param {(number)=>Promise} promiseFactory - Function that spawns promises based - * on a number. 0 is the first, length-1 is the last one. - * @param {number} length - How many promises to spawn - * @param {number} batchSize - How many promises to spawn at a time - * @returns {Promise} array of the return value of the promise. - * It's guaranteed to be sorted, i.e. it will contain - * [ await promsieFactory(0), await promisefactory(1), ... ] - * If the promises depend on each other, then behavior is undefined - */ -export async function startBatch( - promiseFactory, - length, - batchSize, - retryTime = 10000 -) { - if (length === 0) { - return; - } - return new Promise((res) => { - const running = []; - let i = 0; - const startNext = async (current) => { - let result; - try { - result = await promiseFactory(current); - } catch (e) { - // Try again later - await sleep(retryTime); - return await startNext(current); - } - i++; - if (i < length) { - running.push(startNext(i)); - } else { - (async () => res(await Promise.all(running)))(); - } - return result; - }; - // Start fisrt batchsize promises - for (i = 0; i < batchSize && i < length; i++) { - running.push(startNext(i)); - } - --i; - }); -} - /** * An artificial sleep function to pause code execution * diff --git a/tests/unit/start_batch.test.js b/tests/unit/start_batch.test.js deleted file mode 100644 index ca723dab8..000000000 --- a/tests/unit/start_batch.test.js +++ /dev/null @@ -1,39 +0,0 @@ -import { startBatch } from '../../scripts/utils.js'; -import { describe, it, test } from 'vitest'; - -describe('Start batch tests', () => { - const basicFunc = async (arrSize, batchSize) => { - const arr = new Array(arrSize).fill(0).map((_, i) => i * 2); - const promiseFactory = async (i) => { - return arr[i] * 2; - }; - const res = await startBatch(promiseFactory, arr.length, batchSize); - for (let i = 0; i < arr.length; i++) { - expect(res[i]).toBe(arr[i] * 2); - } - }; - test('basic functionality', async () => await basicFunc(100, 8)); - it('works with batchSize > length', async () => basicFunc(4, 500)); - it('works with batchSize = length', async () => basicFunc(500, 500)); - - it('correctly handles errors', async () => { - const arr = new Array(100).fill(0).map((_, i) => i * 2); - // Simulated fail calls with a ~80% success rate - const failCalls = [ - 5, 13, 16, 18, 20, 28, 29, 34, 40, 41, 51, 58, 65, 67, 70, 71, 73, - 81, 88, 91, 94, 97, 100, 105, 112, 115, 117, 119, 127, 128, 130, - ]; - let counter = 0; - const promiseFactory = async (i) => { - counter++; - if (failCalls.includes(counter)) { - throw new Error(':('); - } - return arr[i] * 2; - }; - const res = await startBatch(promiseFactory, arr.length, 8, 50); - for (let i = 0; i < arr.length; i++) { - expect(res[i]).toBe(arr[i] * 2); - } - }); -});