From fa0ef6b053245d382db1fbfdd6356b2376315b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Tue, 5 Jul 2022 21:22:00 +0100 Subject: [PATCH] fix: enqueue sweeping requests and loosen sweep polling --- lib/plugins.js | 4 ++-- lib/plugins/wallet/geth/base.js | 10 +++++++++- lib/poller.js | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 9f6688cd..e2b662f4 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -806,8 +806,8 @@ function plugins (settings, deviceId) { } function sweepHd () { - const sql = `select id, crypto_code, hd_index from cash_out_txs - where hd_index is not null and not swept and status in ('confirmed', 'instant')` + const sql = `SELECT id, crypto_code, hd_index FROM cash_out_txs + WHERE hd_index IS NOT NULL AND NOT swept AND status IN ('confirmed', 'instant') AND created < now() - interval '1 week'` return db.any(sql) .then(rows => Promise.all(rows.map(sweepHdRow))) diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index bdd7ac55..e10ca6a1 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -9,6 +9,8 @@ const { default: Common, Chain, Hardfork } = require('@ethereumjs/common') const Tx = require('ethereumjs-tx') const util = require('ethereumjs-util') const coins = require('@lamassu/coins') +const { default: PQueue } = require('p-queue') + const _pify = require('pify') const BN = require('../../../bn') const ABI = require('../../tokens') @@ -48,6 +50,11 @@ const logInfuraCall = call => { logger.info(`Calling web3 method ${call} via Infura. Current count for this session: ${JSON.stringify(infuraCalls)}`) } +const SWEEP_QUEUE = new PQueue({ + concurrency: 3, + interval: 250, +}) + function connect (url) { web3.setProvider(new web3.providers.HttpProvider(url)) } @@ -236,13 +243,14 @@ function sweep (account, cryptoCode, hdIndex, settings, operatorId) { const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet() const fromAddress = wallet.getChecksumAddressString() - return confirmedBalance(fromAddress, cryptoCode) + return SWEEP_QUEUE.add(() => confirmedBalance(fromAddress, cryptoCode) .then(r => { if (r.eq(0)) return return generateTx(defaultAddress(account), wallet, r, true, cryptoCode) .then(signedTx => pify(web3.eth.sendSignedTransaction)(signedTx)) }) + ) } function newAddress (account, info, tx, settings, operatorId) { diff --git a/lib/poller.js b/lib/poller.js index da333cd1..c1fbec00 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -22,7 +22,7 @@ const processBatches = require('./tx-batching-processing') const INCOMING_TX_INTERVAL = 30 * T.seconds const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds const UNNOTIFIED_INTERVAL = 10 * T.seconds -const SWEEP_HD_INTERVAL = T.minute +const SWEEP_HD_INTERVAL = 5 * T.minute const TRADE_INTERVAL = 60 * T.seconds const PONG_INTERVAL = 10 * T.seconds const LOGS_CLEAR_INTERVAL = 1 * T.day