From 75cb09447679abd382a24e38737df0c7cafc5040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 20 Jan 2022 20:24:24 +0000 Subject: [PATCH 1/6] chore: initial function stubs --- lib/blockchain/install.js | 15 ++++++++++++--- lib/plugins/wallet/bitcoind/bitcoind.js | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 717c9945..c2932383 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -112,6 +112,11 @@ function plugin (crypto) { return plugin } +function getSyncedBlockchains (cryptoList) { + const installedCryptos = _.reduce((acc, value) => ({ ...acc, [value.cryptoCode]: isInstalledSoftware(value) && isInstalledVolume(value) }), {}, cryptoList) + const syncedBlockchains = plugin() +} + function run () { const choices = _.flow([ _.filter(c => c.type !== 'erc-20'), @@ -132,10 +137,14 @@ function run () { questions.push({ type: 'checkbox', name: 'crypto', - message: 'Which cryptocurrencies would you like to install?', - choices + message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be installed simultaneously.\nMore coins can be installed after this process is over.', + choices, + validate: (a) => { + getSyncedBlockchains(cryptos) + return _.size(a) > 0 && _.size(a) <= 2 + } }) inquirer.prompt(questions) - .then(answers => processCryptos(answers.crypto)) + .then(answers => console.log('answers', answers) /* processCryptos(answers.crypto) */) } diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index 51d48910..3b9a4e0a 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -176,6 +176,12 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, From 2819b8a5192b286dd0d7dd7f3b4cd5634f43bb7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 21 Jan 2022 17:44:53 +0000 Subject: [PATCH 2/6] feat: add blockchain status getter to all daemons --- lib/blockchain/install.js | 27 ++++++++++++++----- .../wallet/bitcoincashd/bitcoincashd.js | 9 ++++++- lib/plugins/wallet/bitcoind/bitcoind.js | 3 ++- lib/plugins/wallet/bitgo/bitgo.js | 8 +++++- lib/plugins/wallet/dashd/dashd.js | 9 ++++++- lib/plugins/wallet/geth/base.js | 9 ++++++- lib/plugins/wallet/litecoind/litecoind.js | 9 ++++++- lib/plugins/wallet/mock-wallet/mock-wallet.js | 8 +++++- lib/plugins/wallet/monerod/monerod.js | 24 ++++++++++++++++- lib/plugins/wallet/zcashd/zcashd.js | 9 ++++++- lib/wallet.js | 8 +++++- 11 files changed, 107 insertions(+), 16 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index c2932383..16e2dd4e 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -9,6 +9,8 @@ const _ = require('lodash/fp') const { utils: coinUtils } = require('lamassu-coins') const options = require('../options') +const settingsLoader = require('../new-settings-loader') +const wallet = require('../wallet') const common = require('./common') const doVolume = require('./do-volume') @@ -112,9 +114,19 @@ function plugin (crypto) { return plugin } -function getSyncedBlockchains (cryptoList) { +function getBlockchainSyncStatus (cryptoList) { const installedCryptos = _.reduce((acc, value) => ({ ...acc, [value.cryptoCode]: isInstalledSoftware(value) && isInstalledVolume(value) }), {}, cryptoList) - const syncedBlockchains = plugin() + + return settingsLoader.loadLatest() + .then(settings => { + const blockchainStatuses = _.reduce((acc, value) => + _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: wallet.checkBlockchainStatus(settings, value.cryptoCode) }) : ({ ...acc })), + {}, + cryptoList + ) + + return blockchainStatuses + }) } function run () { @@ -137,14 +149,17 @@ function run () { questions.push({ type: 'checkbox', name: 'crypto', - message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be installed simultaneously.\nMore coins can be installed after this process is over.', + message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be syncing simultaneously.\nMore coins can be installed after this process is over.', choices, validate: (a) => { - getSyncedBlockchains(cryptos) - return _.size(a) > 0 && _.size(a) <= 2 + return getBlockchainSyncStatus(cryptos) + .then(chains => { + const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil() ? 1 : acc[value] + 1 }), {}, _.values(chains)) + return result.syncing > 0 || _.size(a) > 0 && _.size(a) <= 2 + }) } }) inquirer.prompt(questions) - .then(answers => console.log('answers', answers) /* processCryptos(answers.crypto) */) + .then(answers => processCryptos(answers.crypto)) } diff --git a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js index 00a37f73..c969cc71 100644 --- a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js +++ b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js @@ -123,6 +123,12 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, @@ -130,5 +136,6 @@ module.exports = { getStatus, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index 3b9a4e0a..f08acb46 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -192,5 +192,6 @@ module.exports = { fetchRBF, estimateFee, sendCoinsBatch, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/bitgo/bitgo.js b/lib/plugins/wallet/bitgo/bitgo.js index 34621840..c7c22abd 100644 --- a/lib/plugins/wallet/bitgo/bitgo.js +++ b/lib/plugins/wallet/bitgo/bitgo.js @@ -164,6 +164,11 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => Promise.resolve('ready')) +} + module.exports = { NAME, balance, @@ -172,5 +177,6 @@ module.exports = { getStatus, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/dashd/dashd.js b/lib/plugins/wallet/dashd/dashd.js index bb5d848a..c9e6a656 100644 --- a/lib/plugins/wallet/dashd/dashd.js +++ b/lib/plugins/wallet/dashd/dashd.js @@ -118,11 +118,18 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 26b3a50d..64b890c1 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -32,7 +32,8 @@ module.exports = { privateKey, isStrictAddress, connect, - supportsBatching + supportsBatching, + checkBlockchainStatus } function connect (url) { @@ -230,3 +231,9 @@ function supportsBatching (cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => SUPPORTS_BATCHING) } + +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(pify(web3.eth.isSyncing)) + .then(res => _.isObject(res) ? 'syncing' : 'ready') +} diff --git a/lib/plugins/wallet/litecoind/litecoind.js b/lib/plugins/wallet/litecoind/litecoind.js index 7b238f90..452a2553 100644 --- a/lib/plugins/wallet/litecoind/litecoind.js +++ b/lib/plugins/wallet/litecoind/litecoind.js @@ -118,11 +118,18 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index 5850fefc..9b17e4ad 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -115,6 +115,11 @@ function supportsBatching (cryptoCode) { return Promise.resolve(_.includes(cryptoCode, BATCHABLE_COINS)) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => Promise.resolve('ready')) +} + module.exports = { NAME, balance, @@ -123,5 +128,6 @@ module.exports = { newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/monerod/monerod.js b/lib/plugins/wallet/monerod/monerod.js index f28eca0f..1c9f8561 100644 --- a/lib/plugins/wallet/monerod/monerod.js +++ b/lib/plugins/wallet/monerod/monerod.js @@ -205,6 +205,27 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => { + try { + const config = jsonRpc.parseConf(configPath) + + // Daemon uses a different connection of the wallet + const rpcConfig = { + username: config['rpc-login'].split(':')[0], + password: config['rpc-login'].split(':')[1], + port: cryptoRec.defaultPort + } + + return jsonRpc.fetchDigest(rpcConfig, 'get_info') + .then(res => !!res.synchronized ? 'ready' : 'syncing') + } catch (err) { + throw new Error('XMR daemon is currently not installed') + } + }) +} + module.exports = { balance, sendCoins, @@ -212,5 +233,6 @@ module.exports = { getStatus, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/zcashd/zcashd.js b/lib/plugins/wallet/zcashd/zcashd.js index 5979cbe8..bc8c30c4 100644 --- a/lib/plugins/wallet/zcashd/zcashd.js +++ b/lib/plugins/wallet/zcashd/zcashd.js @@ -144,11 +144,18 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initial_block_download_complete'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/wallet.js b/lib/wallet.js index 13e972fa..2b39f260 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -237,6 +237,11 @@ function supportsBatching (settings, cryptoCode) { .then(r => r.wallet.supportsBatching(cryptoCode)) } +function checkBlockchainStatus (settings, cryptoCode) { + return fetchWallet(settings, cryptoCode) + .then(r => r.wallet.checkBlockchainStatus(cryptoCode)) +} + const coinFilter = ['ETH'] const balance = (settings, cryptoCode) => { @@ -265,5 +270,6 @@ module.exports = { isHd, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } From 9411b2ea69c6c2956c1cd417604e9687b1993c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Mon, 24 Jan 2022 15:56:43 +0000 Subject: [PATCH 3/6] fix: blockchain status --- lib/blockchain/install.js | 20 ++++++++++++++++--- .../wallet/bitcoincashd/bitcoincashd.js | 2 +- lib/plugins/wallet/bitcoind/bitcoind.js | 2 +- lib/plugins/wallet/dashd/dashd.js | 2 +- lib/plugins/wallet/litecoind/litecoind.js | 2 +- lib/plugins/wallet/monerod/monerod.js | 5 ++++- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 16e2dd4e..02b60fdd 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -119,8 +119,22 @@ function getBlockchainSyncStatus (cryptoList) { return settingsLoader.loadLatest() .then(settings => { - const blockchainStatuses = _.reduce((acc, value) => - _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: wallet.checkBlockchainStatus(settings, value.cryptoCode) }) : ({ ...acc })), + const blockchainStatuses = _.reduce((acc, value) => { + const processStatus = common.es(`sudo supervisorctl status ${value.code} | awk '{ print $2 }'`).trim() + return wallet.checkBlockchainStatus(settings, value.cryptoCode) + .then(res => { + console.log('res', res) + return _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc })) + }) + .catch(err => { + console.log('err', err) + if (processStatus === 'RUNNING') { + console.log(`Detected ${value.cryptoCode} installed on this machine, but couldn't establish a connection. Is this plugin configured via admin?`) + return { ...acc, [value.cryptoCode]: 'syncing' } + } + return { ...acc } + }) + }, {}, cryptoList ) @@ -154,7 +168,7 @@ function run () { validate: (a) => { return getBlockchainSyncStatus(cryptos) .then(chains => { - const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil() ? 1 : acc[value] + 1 }), {}, _.values(chains)) + const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(chains)) return result.syncing > 0 || _.size(a) > 0 && _.size(a) <= 2 }) } diff --git a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js index c969cc71..4bc7a0fd 100644 --- a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js +++ b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js @@ -126,7 +126,7 @@ function supportsBatching (cryptoCode) { function checkBlockchainStatus (cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('getblockchaininfo')) - .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') + .then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready') } module.exports = { diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index f08acb46..3075a3fa 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -179,7 +179,7 @@ function supportsBatching (cryptoCode) { function checkBlockchainStatus (cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('getblockchaininfo')) - .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') + .then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready') } module.exports = { diff --git a/lib/plugins/wallet/dashd/dashd.js b/lib/plugins/wallet/dashd/dashd.js index c9e6a656..b28e3d59 100644 --- a/lib/plugins/wallet/dashd/dashd.js +++ b/lib/plugins/wallet/dashd/dashd.js @@ -121,7 +121,7 @@ function supportsBatching (cryptoCode) { function checkBlockchainStatus (cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('getblockchaininfo')) - .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') + .then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready') } module.exports = { diff --git a/lib/plugins/wallet/litecoind/litecoind.js b/lib/plugins/wallet/litecoind/litecoind.js index 452a2553..7ae5f9b2 100644 --- a/lib/plugins/wallet/litecoind/litecoind.js +++ b/lib/plugins/wallet/litecoind/litecoind.js @@ -121,7 +121,7 @@ function supportsBatching (cryptoCode) { function checkBlockchainStatus (cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('getblockchaininfo')) - .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') + .then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready') } module.exports = { diff --git a/lib/plugins/wallet/monerod/monerod.js b/lib/plugins/wallet/monerod/monerod.js index 1c9f8561..37d46a77 100644 --- a/lib/plugins/wallet/monerod/monerod.js +++ b/lib/plugins/wallet/monerod/monerod.js @@ -219,7 +219,10 @@ function checkBlockchainStatus (cryptoCode) { } return jsonRpc.fetchDigest(rpcConfig, 'get_info') - .then(res => !!res.synchronized ? 'ready' : 'syncing') + .then(res => { + console.log('res XMR', res) + return !!res.synchronized ? 'ready' : 'syncing' + }) } catch (err) { throw new Error('XMR daemon is currently not installed') } From 4b910ff87b17ff41d7641283e0d01871a516dcf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Mon, 24 Jan 2022 16:30:55 +0000 Subject: [PATCH 4/6] fix: return values for validation function --- lib/blockchain/install.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 02b60fdd..f7a21cb0 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -119,17 +119,14 @@ function getBlockchainSyncStatus (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 wallet.checkBlockchainStatus(settings, value.cryptoCode) - .then(res => { - console.log('res', res) - return _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc })) - }) - .catch(err => { - console.log('err', err) + .then(res => _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc }))) + .catch(() => { if (processStatus === 'RUNNING') { - console.log(`Detected ${value.cryptoCode} installed on this machine, but couldn't establish a connection. Is this plugin configured via admin?`) + installedButNotConfigured.push(value.cryptoCode) return { ...acc, [value.cryptoCode]: 'syncing' } } return { ...acc } @@ -139,7 +136,7 @@ function getBlockchainSyncStatus (cryptoList) { cryptoList ) - return blockchainStatuses + return { blockchainStatuses, installedButNotConfigured } }) } @@ -165,11 +162,20 @@ function run () { name: 'crypto', message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be syncing simultaneously.\nMore coins can be installed after this process is over.', choices, - validate: (a) => { + validate: (answers) => { + if (_.size(answers) > 2) return `Please insert a maximum of two coins to install.` return getBlockchainSyncStatus(cryptos) - .then(chains => { - const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(chains)) - return result.syncing > 0 || _.size(a) > 0 && _.size(a) <= 2 + .then(({ blockchainStatuses, installedButNotConfigured }) => { + if (!_.isEmpty(installedButNotConfigured)) { + return `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?` + } + + const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(blockchainStatuses)) + if (result.syncing > 2) { + return `There are currently more than 2 blockchains in their initial synchronization. Please try again later.` + } + + return true }) } }) From fc5a13fcf3ba637ea14e2598b4c80f85612d927a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Mon, 24 Jan 2022 18:25:27 +0000 Subject: [PATCH 5/6] fix: workaround inquirer validation not working properly --- lib/blockchain/install.js | 45 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index f7a21cb0..4ba0f059 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -125,6 +125,7 @@ function getBlockchainSyncStatus (cryptoList) { return wallet.checkBlockchainStatus(settings, value.cryptoCode) .then(res => _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc }))) .catch(() => { + console.log('processStatus', processStatus) if (processStatus === 'RUNNING') { installedButNotConfigured.push(value.cryptoCode) return { ...acc, [value.cryptoCode]: 'syncing' } @@ -157,29 +158,37 @@ function run () { const questions = [] + 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)) { + return { message: `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?`, isValid: false} + } + + const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(blockchainStatuses)) + if (result.syncing > 2) { + return { message: `There are currently more than 2 blockchains in their initial synchronization. Please try again later.`, isValid: false } + } + + return { message: null, isValid: true } + }) + } + questions.push({ type: 'checkbox', name: 'crypto', message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be syncing simultaneously.\nMore coins can be installed after this process is over.', - choices, - validate: (answers) => { - if (_.size(answers) > 2) return `Please insert a maximum of two coins to install.` - return getBlockchainSyncStatus(cryptos) - .then(({ blockchainStatuses, installedButNotConfigured }) => { - if (!_.isEmpty(installedButNotConfigured)) { - return `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?` - } - - const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(blockchainStatuses)) - if (result.syncing > 2) { - return `There are currently more than 2 blockchains in their initial synchronization. Please try again later.` - } - - return true - }) - } + choices }) inquirer.prompt(questions) - .then(answers => processCryptos(answers.crypto)) + .then(answers => Promise.all([validateAnswers(answers), answers])) + .then(([res, answers]) => { + console.log('res', res) + if (res.isValid) { + return processCryptos(answers.crypto) + } + console.log(res.message) + }) } From 92efea080e209513c6c6f9be15a1609dad4f3375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Mon, 24 Jan 2022 20:17:54 +0000 Subject: [PATCH 6/6] fix: promise resolving in reducer function --- lib/blockchain/install.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 4ba0f059..53b359d3 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -122,23 +122,25 @@ function getBlockchainSyncStatus (cryptoList) { const installedButNotConfigured = [] const blockchainStatuses = _.reduce((acc, value) => { const processStatus = common.es(`sudo supervisorctl status ${value.code} | awk '{ print $2 }'`).trim() - return wallet.checkBlockchainStatus(settings, value.cryptoCode) - .then(res => _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc }))) - .catch(() => { - console.log('processStatus', processStatus) - if (processStatus === 'RUNNING') { - installedButNotConfigured.push(value.cryptoCode) - return { ...acc, [value.cryptoCode]: 'syncing' } - } - return { ...acc } - }) + 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 }) + }) + }) }, - {}, + Promise.resolve({}), cryptoList ) - return { blockchainStatuses, installedButNotConfigured } + return Promise.all([blockchainStatuses, installedButNotConfigured]) }) + .then(([blockchainStatuses, installedButNotConfigured]) => ({ blockchainStatuses, installedButNotConfigured })) } function run () { @@ -163,10 +165,14 @@ function run () { return getBlockchainSyncStatus(cryptos) .then(({ blockchainStatuses, installedButNotConfigured }) => { if (!_.isEmpty(installedButNotConfigured)) { - return { message: `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?`, isValid: false} + 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?`) } 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 } + } + if (result.syncing > 2) { return { message: `There are currently more than 2 blockchains in their initial synchronization. Please try again later.`, isValid: false } } @@ -183,12 +189,11 @@ function run () { }) inquirer.prompt(questions) - .then(answers => Promise.all([validateAnswers(answers), answers])) + .then(answers => Promise.all([validateAnswers(answers.crypto), answers])) .then(([res, answers]) => { - console.log('res', res) if (res.isValid) { return processCryptos(answers.crypto) } - console.log(res.message) + logger.error(res.message) }) }