From f23f31e4d9bd6d7de76132f7ddc539ec074e41b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 24 Feb 2022 20:57:12 +0000 Subject: [PATCH] feat: blockchain status checking no longer dependent on plugin status --- lib/blockchain/install.js | 26 +++++++------------------- lib/plugins/wallet/geth/base.js | 5 +++-- lib/plugins/wallet/monerod/monerod.js | 5 +---- lib/wallet.js | 14 ++++++++++++-- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 88d8906a..edab198e 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -118,32 +118,24 @@ function plugin (crypto) { } function getBlockchainSyncStatus (cryptoList) { - const installedCryptos = _.reduce((acc, value) => ({ ...acc, [value.cryptoCode]: isInstalledSoftware(value) && isInstalledVolume(value) }), {}, cryptoList) - return settingsLoader.loadLatest() .then(settings => { - const installedButNotConfigured = [] const blockchainStatuses = _.reduce((acc, value) => { const processStatus = common.es(`sudo supervisorctl status ${value.code} | awk '{ print $2 }'`).trim() return acc.then(a => { - return wallet.checkBlockchainStatus(settings, value.cryptoCode) - .then(res => _.includes(value.cryptoCode, _.keys(installedCryptos)) ? Promise.resolve({ ...a, [value.cryptoCode]: res }) : Promise.resolve({ ...a })) - .catch(() => { - if (processStatus === 'RUNNING') { - installedButNotConfigured.push(value.cryptoCode) - return Promise.resolve({ ...a, [value.cryptoCode]: 'syncing' }) - } - return Promise.resolve({ ...a }) - }) + if (processStatus === 'RUNNING') { + return wallet.checkBlockchainStatus(settings, value.cryptoCode) + .then(res => Promise.resolve({ ...a, [value.cryptoCode]: res })) + } + return Promise.resolve({ ...a }) }) }, Promise.resolve({}), cryptoList ) - return Promise.all([blockchainStatuses, installedButNotConfigured]) + return blockchainStatuses }) - .then(([blockchainStatuses, installedButNotConfigured]) => ({ blockchainStatuses, installedButNotConfigured })) } function run () { @@ -166,11 +158,7 @@ function run () { const validateAnswers = async (answers) => { if (_.size(answers) > 2) return { message: `Please insert a maximum of two coins to install.`, isValid: false } return getBlockchainSyncStatus(cryptos) - .then(({ blockchainStatuses, installedButNotConfigured }) => { - if (!_.isEmpty(installedButNotConfigured)) { - logger.warn(`Detected ${_.join(' and ', installedButNotConfigured)} installed on this machine, but couldn't establish connection. ${_.size(installedButNotConfigured) === 1 ? `Is this plugin` : `Are these plugins`} configured via admin?`) - } - + .then(blockchainStatuses => { const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(blockchainStatuses)) if (_.size(answers) + result.syncing > 2) { return { message: `Installing these coins would pass the 2 parallel blockchain synchronization limit. Please try again with fewer coins or try again later.`, isValid: false } diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 48e0c4b4..24f4cf23 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -228,6 +228,7 @@ function newFunding (account, cryptoCode, settings, operatorId) { function checkBlockchainStatus (cryptoCode) { return checkCryptoCode(cryptoCode) - .then(pify(web3.eth.isSyncing)) - .then(res => _.isObject(res) ? 'syncing' : 'ready') + .then(() => connect(`http://localhost:${coins.utils.getCryptoCurrency(cryptoCode).defaultPort}`)) + .then(() => web3.eth.syncing) + .then(res => res === false ? 'ready' : 'syncing') } diff --git a/lib/plugins/wallet/monerod/monerod.js b/lib/plugins/wallet/monerod/monerod.js index 971fda29..d826a0f8 100644 --- a/lib/plugins/wallet/monerod/monerod.js +++ b/lib/plugins/wallet/monerod/monerod.js @@ -202,10 +202,7 @@ function checkBlockchainStatus (cryptoCode) { } return jsonRpc.fetchDigest(rpcConfig, 'get_info') - .then(res => { - console.log('res XMR', res) - return !!res.synchronized ? 'ready' : 'syncing' - }) + .then(res => !!res.synchronized ? 'ready' : 'syncing') } catch (err) { throw new Error('XMR daemon is currently not installed') } diff --git a/lib/wallet.js b/lib/wallet.js index b00eea52..ccec5133 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -239,8 +239,18 @@ function supportsBatching (settings, cryptoCode) { } function checkBlockchainStatus (settings, cryptoCode) { - return fetchWallet(settings, cryptoCode) - .then(r => r.wallet.checkBlockchainStatus(cryptoCode)) + const walletDaemons = { + BTC: require('./plugins/wallet/bitcoind/bitcoind.js'), + BCH: require('./plugins/wallet/bitcoincashd/bitcoincashd.js'), + DASH: require('./plugins/wallet/dashd/dashd.js'), + ETH: require('./plugins/wallet/geth/base.js'), + LTC: require('./plugins/wallet/litecoind/litecoind.js'), + XMR: require('./plugins/wallet/monerod/monerod.js'), + ZEC: require('./plugins/wallet/zcashd/zcashd.js') + } + + return Promise.resolve(walletDaemons[cryptoCode]) + .then(({ checkBlockchainStatus }) => checkBlockchainStatus(cryptoCode)) } const coinFilter = ['ETH']