diff --git a/lib/plugins/common/json-rpc.js b/lib/plugins/common/json-rpc.js index 33174591..5a5434c2 100644 --- a/lib/plugins/common/json-rpc.js +++ b/lib/plugins/common/json-rpc.js @@ -4,6 +4,7 @@ const uuid = require('uuid') const fs = require('fs') const _ = require('lodash/fp') const request = require('request-promise') +const Queue = require('queue-promise') const { utils: coinUtils } = require('@lamassu/coins') const options = require('../../options') @@ -11,7 +12,7 @@ const options = require('../../options') const blockchainDir = options.blockchainDir module.exports = { - fetch, fetchDigest, parseConf, rpcConfig + fetch, fetchDigest, createDigestRequest, parseConf, rpcConfig } function fetch (account = {}, method, params) { @@ -49,31 +50,47 @@ function fetch (account = {}, method, params) { }) } +const DIGEST_QUEUE = new Queue({ + concurrent: 1, + interval: 100, + start: false +}) + +function createDigestRequest (account = {}, method, params = []) { + DIGEST_QUEUE.enqueue(() => fetchDigest(account, method, params)) + return DIGEST_QUEUE.dequeue() +} + +function generateDigestOptions (account = {}, method, params) { + const headers = { + 'Content-Type': 'application/json' + } + + const dataString = `{"jsonrpc":"2.0","id":"${uuid.v4()}","method":"${method}","params":${JSON.stringify(params)}}` + + const options = { + url: `http://localhost:${account.port}/json_rpc`, + method: 'POST', + headers, + body: dataString, + forever: true, + auth: { + user: account.username, + pass: account.password, + sendImmediately: false + } + } + + return options +} + function fetchDigest(account = {}, method, params = []) { return Promise.resolve(true) .then(() => { if (_.isNil(account.port)) throw new Error('port attribute required for jsonRpc') - const headers = { - 'Content-Type': 'application/json' - } - - const dataString = `{"jsonrpc":"2.0","id":"${uuid.v4()}","method":"${method}","params":${JSON.stringify(params)}}` - - const options = { - url: `http://localhost:${account.port}/json_rpc`, - method: 'POST', - headers, - body: dataString, - forever: true, - auth: { - user: account.username, - pass: account.password, - sendImmediately: false - } - } - + const options = generateDigestOptions(account, method, params) return request(options) }) .then((res) => { diff --git a/lib/plugins/wallet/monerod/monerod.js b/lib/plugins/wallet/monerod/monerod.js index 971fda29..0cb315d6 100644 --- a/lib/plugins/wallet/monerod/monerod.js +++ b/lib/plugins/wallet/monerod/monerod.js @@ -15,7 +15,6 @@ const blockchainDir = options.blockchainDir const cryptoRec = utils.getCryptoCurrency(COINS.XMR) const configPath = utils.configPath(cryptoRec, blockchainDir) const walletDir = path.resolve(utils.cryptoDir(cryptoRec, blockchainDir), 'wallets') -const unitScale = cryptoRec.unitScale function rpcConfig () { try { @@ -31,7 +30,7 @@ function rpcConfig () { } function fetch (method, params) { - return jsonRpc.fetchDigest(rpcConfig(), method, params) + return jsonRpc.createDigestRequest(rpcConfig(), method, params) } function handleError (error, method) { @@ -151,16 +150,11 @@ function getStatus (account, tx, requested, settings, operatorId) { function newFunding (account, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) .then(() => refreshWallet()) - .then(() => fetch('get_balance', { account_index: 0, address_indices: [0] })) - .then(balanceRes => Promise.all([ - balanceRes, - fetch('create_address', { account_index: 0 }) - ])) - .then(([balanceRes, addressRes]) => Promise.all([ - balanceRes, - addressRes, - fetch('get_transfers', { pool: true, account_index: 0 }) - ])) + .then(() => Promise.all([ + fetch('get_balance', { account_index: 0, address_indices: [0] }), + fetch('create_address', { account_index: 0 }), + fetch('get_transfers', { pool: true, account_index: 0 }) + ])) .then(([balanceRes, addressRes, transferRes]) => { const memPoolBalance = _.reduce((acc, value) => acc.plus(value.amount), BN(0), transferRes.pool) return {