Browse Source

Merge branch 'feat_dojo_rpc_client' into 'develop'

Transition from bitcoind-rpc-client to rpc-bitcoin

See merge request dojo/samourai-dojo!231
umbrel
Pavel Ševčík 4 years ago
parent
commit
88e0ec4e01
  1. 4
      accounts/index.js
  2. 7
      accounts/xpub-rest-api.js
  3. 6
      lib/bitcoind-rpc/fees.js
  4. 6
      lib/bitcoind-rpc/headers.js
  5. 6
      lib/bitcoind-rpc/latest-block.js
  6. 100
      lib/bitcoind-rpc/rpc-client.js
  7. 15
      lib/bitcoind-rpc/transactions.js
  8. 6
      lib/remote-importer/bitcoind-wrapper.js
  9. 956
      package-lock.json
  10. 3
      package.json
  11. 4
      pushtx/index-orchestrator.js
  12. 4
      pushtx/index.js
  13. 10
      pushtx/orchestrator.js
  14. 6
      pushtx/pushtx-processor.js
  15. 10
      pushtx/status.js
  16. 6
      pushtx/transactions-scheduler.js
  17. 8
      scripts/create-first-blocks.js
  18. 6
      tracker/block-worker.js
  19. 16
      tracker/blockchain-processor.js
  20. 4
      tracker/index.js
  21. 6
      tracker/mempool-processor.js

4
accounts/index.js

@ -7,7 +7,7 @@
'use strict' 'use strict'
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { waitForBitcoindRpcApi } = require('../lib/bitcoind-rpc/rpc-client')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
@ -34,7 +34,7 @@
// Wait for Bitcoind RPC API // Wait for Bitcoind RPC API
// being ready to process requests // being ready to process requests
await RpcClient.waitForBitcoindRpcApi() await waitForBitcoindRpcApi()
// Initialize the db wrapper // Initialize the db wrapper
const dbConfig = { const dbConfig = {

7
accounts/xpub-rest-api.js

@ -9,16 +9,14 @@ const bodyParser = require('body-parser')
const errors = require('../lib/errors') const errors = require('../lib/errors')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper')
const hdaHelper = require('../lib/bitcoin/hd-accounts-helper') const hdaHelper = require('../lib/bitcoin/hd-accounts-helper')
const hdaService = require('../lib/bitcoin/hd-accounts-service') const hdaService = require('../lib/bitcoin/hd-accounts-service')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client')
const HdAccountInfo = require('../lib/wallet/hd-account-info') const HdAccountInfo = require('../lib/wallet/hd-account-info')
const authMgr = require('../lib/auth/authorizations-manager') const authMgr = require('../lib/auth/authorizations-manager')
const HttpServer = require('../lib/http-server/http-server') const HttpServer = require('../lib/http-server/http-server')
const remoteImporter = require('../lib/remote-importer/remote-importer') const remoteImporter = require('../lib/remote-importer/remote-importer')
const debugApi = !!(process.argv.indexOf('api-debug') > -1) const debugApi = process.argv.indexOf('api-debug') > -1
const gap = require('../keys/')[network.key].gap const gap = require('../keys/')[network.key].gap
@ -34,9 +32,6 @@ class XPubRestApi {
constructor(httpServer) { constructor(httpServer) {
this.httpServer = httpServer this.httpServer = httpServer
// Initialize the rpc client
this.rpcClient = new RpcClient()
// Establish routes // Establish routes
const urlencodedParser = bodyParser.urlencoded({ extended: true }) const urlencodedParser = bodyParser.urlencoded({ extended: true })

6
lib/bitcoind-rpc/fees.js

@ -9,7 +9,7 @@ const errors = require('../errors')
const Logger = require('../logger') const Logger = require('../logger')
const network = require('../bitcoin/network') const network = require('../bitcoin/network')
const keys = require('../../keys')[network.key] const keys = require('../../keys')[network.key]
const RpcClient = require('./rpc-client') const { createRpcClient } = require('./rpc-client')
const latestBlock = require('./latest-block') const latestBlock = require('./latest-block')
@ -27,7 +27,7 @@ class Fees {
this.fees = {} this.fees = {}
this.feeType = keys.bitcoind.feeType this.feeType = keys.bitcoind.feeType
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
this.refresh() this.refresh()
} }
@ -55,7 +55,7 @@ class Fees {
async refresh() { async refresh() {
await util.parallelCall(this.targets, async tgt => { await util.parallelCall(this.targets, async tgt => {
try { try {
const level = await this.rpcClient.cmd('estimatesmartfee', tgt, this.feeType) const level = await this.rpcClient.estimatesmartfee({ conf_target: tgt, estimate_mode: this.feeType })
this.fees[tgt] = (level.errors && level.errors.length > 0) ? 0 : Math.round(level.feerate * 1e5) this.fees[tgt] = (level.errors && level.errors.length > 0) ? 0 : Math.round(level.feerate * 1e5)
} catch(e) { } catch(e) {
Logger.error(e, 'Bitcoind RPC : Fees.refresh()') Logger.error(e, 'Bitcoind RPC : Fees.refresh()')

6
lib/bitcoind-rpc/headers.js

@ -6,7 +6,7 @@
const LRU = require('lru-cache') const LRU = require('lru-cache')
const errors = require('../errors') const errors = require('../errors')
const RpcClient = require('./rpc-client') const { createRpcClient } = require('./rpc-client')
/** /**
@ -29,7 +29,7 @@ class Headers {
}) })
// Initialize the rpc client // Initialize the rpc client
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
} }
/** /**
@ -42,7 +42,7 @@ class Headers {
return this.headers.get(hash) return this.headers.get(hash)
try { try {
const header = await this.rpcClient.getblockheader(hash, true) const header = await this.rpcClient.getblockheader({ blockhash: hash, verbose: true })
const fmtHeader = JSON.stringify(header, null, 2) const fmtHeader = JSON.stringify(header, null, 2)
this.headers.set(hash, fmtHeader) this.headers.set(hash, fmtHeader)
return fmtHeader return fmtHeader

6
lib/bitcoind-rpc/latest-block.js

@ -9,7 +9,7 @@ const Logger = require('../logger')
const util = require('../util') const util = require('../util')
const network = require('../bitcoin/network') const network = require('../bitcoin/network')
const keys = require('../../keys')[network.key] const keys = require('../../keys')[network.key]
const RpcClient = require('./rpc-client') const { createRpcClient } = require('./rpc-client')
/** /**
@ -27,7 +27,7 @@ class LatestBlock {
this.diff = null this.diff = null
// Initialize the rpc client // Initialize the rpc client
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
// Gets the latest block from bitcoind // Gets the latest block from bitcoind
this.rpcClient.getbestblockhash().then(hash => this.onBlockHash(hash)) this.rpcClient.getbestblockhash().then(hash => this.onBlockHash(hash))
@ -54,7 +54,7 @@ class LatestBlock {
* @returns {Promise} * @returns {Promise}
*/ */
async onBlockHash(hash) { async onBlockHash(hash) {
const header = await this.rpcClient.getblockheader(hash) const header = await this.rpcClient.getblockheader({ blockhash: hash })
this.height = header.height this.height = header.height
this.hash = hash this.hash = hash

100
lib/bitcoind-rpc/rpc-client.js

@ -4,7 +4,7 @@
*/ */
'use strict' 'use strict'
const rpc = require('bitcoind-rpc-client') const {RPCClient} = require('rpc-bitcoin');
const network = require('../bitcoin/network') const network = require('../bitcoin/network')
const keys = require('../../keys')[network.key] const keys = require('../../keys')[network.key]
const util = require('../util') const util = require('../util')
@ -14,77 +14,51 @@ const Logger = require('../logger')
/** /**
* Wrapper for bitcoind rpc client * Wrapper for bitcoind rpc client
*/ */
class RpcClient { const createRpcClient = () => {
return new RPCClient({
/** url: `http://${keys.bitcoind.rpc.host}`,
* Constructor port: keys.bitcoind.rpc.port,
*/ user: keys.bitcoind.rpc.user,
constructor() { pass: keys.bitcoind.rpc.pass
// Initiliaze the rpc client
this.client = new rpc({
host: keys.bitcoind.rpc.host,
port: keys.bitcoind.rpc.port
}) })
}
this.client.set('user', keys.bitcoind.rpc.user) /**
this.client.set('pass', keys.bitcoind.rpc.pass) * Check if an error returned by bitcoin-rpc-client
* is a connection error.
// Initialize a proxy postprocessing api calls * @param {string} err - error message
return new Proxy(this, { * @returns {boolean} returns true if message related to a connection error
get: function(target, name, receiver) { */
const origMethod = target.client[name] const isConnectionError = (err) => {
return async function(...args) {
const result = await origMethod.apply(target.client, args)
if (Array.isArray(result)) {
return result
} else if (result.result) {
return result.result
} else if (result.error) {
throw result.error
} else {
throw 'A problem was met with a request sent to bitcoind RPC API'
}
}
}
})
}
/**
* Check if an error returned by bitcoin-rpc-client
* is a connection error.
* @param {string} err - error message
* @returns {boolean} returns true if message related to a connection error
*/
static isConnectionError(err) {
if (typeof err != 'string') if (typeof err != 'string')
return false return false
const isTimeoutError = (err.indexOf('connect ETIMEDOUT') != -1) const isTimeoutError = (err.indexOf('connect ETIMEDOUT') !== -1)
const isConnRejected = (err.indexOf('Connection Rejected') != -1) const isConnRejected = (err.indexOf('Connection Rejected') !== -1)
return (isTimeoutError || isConnRejected) return (isTimeoutError || isConnRejected)
} }
/** /**
* Check if the rpc api is ready to process requests * Check if the rpc api is ready to process requests
* @returns {Promise} * @returns {Promise}
*/ */
static async waitForBitcoindRpcApi() { const waitForBitcoindRpcApi = async () => {
let client = new RpcClient() let client = createRpcClient()
try { try {
await client.getblockchaininfo() await client.getblockchaininfo()
} catch(e) { } catch (e) {
client = null client = null
Logger.info('Bitcoind RPC : API is still unreachable. New attempt in 20s.') Logger.info('Bitcoind RPC : API is still unreachable. New attempt in 20s.')
return util.delay(20000).then(() => { return util.delay(20000).then(() => {
return RpcClient.waitForBitcoindRpcApi() return waitForBitcoindRpcApi()
}) })
} }
}
} }
module.exports = RpcClient module.exports = {
createRpcClient,
isConnectionError,
waitForBitcoindRpcApi
}

15
lib/bitcoind-rpc/transactions.js

@ -9,7 +9,7 @@ const LRU = require('lru-cache')
const errors = require('../errors') const errors = require('../errors')
const Logger = require('../logger') const Logger = require('../logger')
const util = require('../util') const util = require('../util')
const RpcClient = require('./rpc-client') const { createRpcClient } = require('./rpc-client')
const rpcLatestBlock = require('./latest-block') const rpcLatestBlock = require('./latest-block')
@ -34,7 +34,7 @@ class Transactions {
// Initialize the rpc client // Initialize the rpc client
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
} }
/** /**
@ -47,8 +47,11 @@ class Transactions {
try { try {
const rpcCalls = txids.map(txid => { const rpcCalls = txids.map(txid => {
return { return {
'method': 'getrawtransaction', method: 'getrawtransaction',
'params': [txid, true] params: {
txid,
verbose: true
}
} }
}) })
@ -77,7 +80,7 @@ class Transactions {
*/ */
async getTransaction(txid, fees) { async getTransaction(txid, fees) {
try { try {
const tx = await this.rpcClient.getrawtransaction(txid, true) const tx = await this.rpcClient.getrawtransaction({ txid, verbose: true })
return this._prepareTxResult(tx, fees) return this._prepareTxResult(tx, fees)
} catch(e) { } catch(e) {
Logger.error(e, 'Bitcoind RPC : Transaction.getTransaction()') Logger.error(e, 'Bitcoind RPC : Transaction.getTransaction()')
@ -177,7 +180,7 @@ class Transactions {
if (this.prevCache.has(inTxid)) { if (this.prevCache.has(inTxid)) {
ptx = this.prevCache.get(inTxid) ptx = this.prevCache.get(inTxid)
} else { } else {
ptx = await this.rpcClient.getrawtransaction(inTxid, true) ptx = await this.rpcClient.getrawtransaction({ txid: inTxid, verbose: true })
this.prevCache.set(inTxid, ptx) this.prevCache.set(inTxid, ptx)
} }

6
lib/remote-importer/bitcoind-wrapper.js

@ -5,7 +5,7 @@
'use strict' 'use strict'
const bitcoin = require('bitcoinjs-lib') const bitcoin = require('bitcoinjs-lib')
const RpcClient = require('../bitcoind-rpc/rpc-client') const { createRpcClient } = require('../bitcoind-rpc/rpc-client')
const rpcLatestBlock = require('../bitcoind-rpc/latest-block') const rpcLatestBlock = require('../bitcoind-rpc/latest-block')
const Logger = require('../logger') const Logger = require('../logger')
const addrHelper = require('../bitcoin/addresses-helper') const addrHelper = require('../bitcoin/addresses-helper')
@ -25,7 +25,7 @@ class BitcoindWrapper extends Wrapper {
constructor() { constructor() {
super(null, null) super(null, null)
// RPC client // RPC client
this.client = new RpcClient() this.client = createRpcClient()
} }
/** /**
@ -35,7 +35,7 @@ class BitcoindWrapper extends Wrapper {
* @returns {Promise} * @returns {Promise}
*/ */
async _get(descriptors) { async _get(descriptors) {
return this.client.cmd('scantxoutset', 'start', descriptors) return await this.client.scantxoutset({ action: 'start', scanobjects: descriptors })
} }
/** /**

956
package-lock.json

File diff suppressed because it is too large

3
package.json

@ -18,7 +18,6 @@
"async-sema": "2.1.2", "async-sema": "2.1.2",
"axios": "0.21.1", "axios": "0.21.1",
"bip39": "2.4.0", "bip39": "2.4.0",
"bitcoind-rpc-client": "0.3.1",
"bitcoinjs-lib": "5.2.0", "bitcoinjs-lib": "5.2.0",
"bitcoinjs-message": "1.0.1", "bitcoinjs-message": "1.0.1",
"body-parser": "1.19.0", "body-parser": "1.19.0",
@ -26,10 +25,12 @@
"jsonwebtoken": "8.5.1", "jsonwebtoken": "8.5.1",
"lodash": "4.17.21", "lodash": "4.17.21",
"lru-cache": "4.0.2", "lru-cache": "4.0.2",
"make-concurrent": "5.3.0",
"minimist": "1.2.5", "minimist": "1.2.5",
"mysql": "2.18.1", "mysql": "2.18.1",
"passport": "0.4.1", "passport": "0.4.1",
"passport-localapikey-update": "0.6.0", "passport-localapikey-update": "0.6.0",
"rpc-bitcoin": "2.0.0",
"sirv": "1.0.11", "sirv": "1.0.11",
"socks-proxy-agent": "4.0.1", "socks-proxy-agent": "4.0.1",
"validator": "10.8.0", "validator": "10.8.0",

4
pushtx/index-orchestrator.js

@ -8,7 +8,7 @@
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { waitForBitcoindRpcApi } = require('../lib/bitcoind-rpc/rpc-client')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const Orchestrator = require('./orchestrator') const Orchestrator = require('./orchestrator')
@ -23,7 +23,7 @@
// Wait for Bitcoind RPC API // Wait for Bitcoind RPC API
// being ready to process requests // being ready to process requests
await RpcClient.waitForBitcoindRpcApi() await waitForBitcoindRpcApi()
// Initialize the db wrapper // Initialize the db wrapper
const dbConfig = { const dbConfig = {

4
pushtx/index.js

@ -8,7 +8,7 @@
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { waitForBitcoindRpcApi } = require('../lib/bitcoind-rpc/rpc-client')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const HttpServer = require('../lib/http-server/http-server') const HttpServer = require('../lib/http-server/http-server')
@ -24,7 +24,7 @@
// Wait for Bitcoind RPC API // Wait for Bitcoind RPC API
// being ready to process requests // being ready to process requests
await RpcClient.waitForBitcoindRpcApi() await waitForBitcoindRpcApi()
// Initialize the db wrapper // Initialize the db wrapper
const dbConfig = { const dbConfig = {

10
pushtx/orchestrator.js

@ -8,7 +8,7 @@ const zmq = require('zeromq')
const Sema = require('async-sema') const Sema = require('async-sema')
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient, isConnectionError } = require('../lib/bitcoind-rpc/rpc-client')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const pushTxProcessor = require('./pushtx-processor') const pushTxProcessor = require('./pushtx-processor')
@ -24,7 +24,7 @@ class Orchestrator {
*/ */
constructor() { constructor() {
// RPC client // RPC client
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
// ZeroMQ socket for bitcoind blocks messages // ZeroMQ socket for bitcoind blocks messages
this.blkSock = null this.blkSock = null
// Initialize a semaphor protecting the onBlockHash() method // Initialize a semaphor protecting the onBlockHash() method
@ -77,7 +77,7 @@ class Orchestrator {
// Retrieve the block height // Retrieve the block height
const blockHash = buf.toString('hex') const blockHash = buf.toString('hex')
const header = await this.rpcClient.getblockheader(blockHash, true) const header = await this.rpcClient.getblockheader({ blockhash: blockHash, verbose: true })
const height = header.height const height = header.height
Logger.info(`Orchestrator : Block ${height} ${blockHash}`) Logger.info(`Orchestrator : Block ${height} ${blockHash}`)
@ -100,7 +100,7 @@ class Orchestrator {
// Check if previous transaction has been confirmed // Check if previous transaction has been confirmed
if (hasParentTx) { if (hasParentTx) {
try { try {
parentTx = await this.rpcClient.getrawtransaction(tx.schParentTxid, true) parentTx = await this.rpcClient.getrawtransaction({ txid: tx.schParentTxid, verbose: true })
} catch(e) { } catch(e) {
Logger.error(e, 'Orchestrator : Transaction.getTransaction()') Logger.error(e, 'Orchestrator : Transaction.getTransaction()')
} }
@ -116,7 +116,7 @@ class Orchestrator {
Logger.error(e, `Orchestrator : Orchestrator.onBlockHash() : ${msg}`) Logger.error(e, `Orchestrator : Orchestrator.onBlockHash() : ${msg}`)
// Check if it's an issue with the connection to the RPC API // Check if it's an issue with the connection to the RPC API
// (=> immediately stop the loop) // (=> immediately stop the loop)
if (RpcClient.isConnectionError(e)) { if (isConnectionError(e)) {
Logger.info('Orchestrator : Connection issue') Logger.info('Orchestrator : Connection issue')
rpcConnOk = false rpcConnOk = false
break break

6
pushtx/pushtx-processor.js

@ -9,7 +9,7 @@ const zmq = require('zeromq')
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const errors = require('../lib/errors') const errors = require('../lib/errors')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const addrHelper = require('../lib/bitcoin/addresses-helper') const addrHelper = require('../lib/bitcoin/addresses-helper')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const activeNet = network.network const activeNet = network.network
@ -37,7 +37,7 @@ class PushTxProcessor {
this.notifSock = null this.notifSock = null
this.sources = new Sources() this.sources = new Sources()
// Initialize the rpc client // Initialize the rpc client
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
} }
/** /**
@ -109,7 +109,7 @@ class PushTxProcessor {
// At this point, the raw hex parses as a legitimate transaction. // At this point, the raw hex parses as a legitimate transaction.
// Attempt to send via RPC to the bitcoind instance // Attempt to send via RPC to the bitcoind instance
try { try {
const txid = await this.rpcClient.sendrawtransaction(rawtx) const txid = await this.rpcClient.sendrawtransaction({ hexstring: rawtx })
Logger.info('PushTx : Pushed!') Logger.info('PushTx : Pushed!')
// Update the stats // Update the stats
status.updateStats(value) status.updateStats(value)

10
pushtx/status.js

@ -10,7 +10,7 @@ const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
/** /**
@ -49,7 +49,7 @@ class Status {
amount: 0, amount: 0,
count: 0 count: 0
} }
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
} }
/** /**
@ -106,7 +106,7 @@ class Status {
* Refresh network info * Refresh network info
*/ */
async _refreshNetworkInfo() { async _refreshNetworkInfo() {
const info = await this.rpcClient.getNetworkInfo() const info = await this.rpcClient.getnetworkinfo()
this.status.bitcoind.conn = info.connections this.status.bitcoind.conn = info.connections
this.status.bitcoind.version = info.version this.status.bitcoind.version = info.version
this.status.bitcoind.protocolversion = info.protocolversion this.status.bitcoind.protocolversion = info.protocolversion
@ -117,9 +117,9 @@ class Status {
* Refresh blockchain info * Refresh blockchain info
*/ */
async _refreshBlockchainInfo() { async _refreshBlockchainInfo() {
const info = await this.rpcClient.getBlockchainInfo() const info = await this.rpcClient.getblockchaininfo()
this.status.bitcoind.blocks = info.blocks this.status.bitcoind.blocks = info.blocks
this.status.bitcoind.testnet = (info.chain != 'main') this.status.bitcoind.testnet = (info.chain !== 'main')
this.status.bitcoind.up = true this.status.bitcoind.up = true
} }

6
pushtx/transactions-scheduler.js

@ -10,7 +10,7 @@ const errors = require('../lib/errors')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const pushTxProcessor = require('./pushtx-processor') const pushTxProcessor = require('./pushtx-processor')
@ -23,7 +23,7 @@ class TransactionsScheduler {
* Constructor * Constructor
*/ */
constructor() { constructor() {
this.rpcClient = new RpcClient() this.rpcClient = createRpcClient()
} }
/** /**
@ -41,7 +41,7 @@ class TransactionsScheduler {
script.sort((a,b) => a.hop - b.hop || a.nlocktime - b.nlocktime) script.sort((a,b) => a.hop - b.hop || a.nlocktime - b.nlocktime)
// Get the height of last block seen // Get the height of last block seen
const info = await this.rpcClient.getBlockchainInfo() const info = await this.rpcClient.getblockchaininfo()
const lastHeight = info.blocks const lastHeight = info.blocks
// Get the nLockTime associated to the first transaction // Get the nLockTime associated to the first transaction

8
scripts/create-first-blocks.js

@ -9,7 +9,7 @@ const Logger = require('../lib/logger')
const util = require('../lib/util') const util = require('../lib/util')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
@ -19,7 +19,7 @@ const keys = require('../keys')[network.key]
*/ */
// RPC Client requests data from bitcoind // RPC Client requests data from bitcoind
let client = new RpcClient() let client = createRpcClient()
// Database id of the previous block // Database id of the previous block
let prevID = null; let prevID = null;
@ -28,10 +28,10 @@ let prevID = null;
async function processBlock(height) { async function processBlock(height) {
Logger.info('Start processing block ' + height) Logger.info('Start processing block ' + height)
const blockHash = await client.getblockhash(height) const blockHash = await client.getblockhash({ height })
if (blockHash) { if (blockHash) {
const header = await client.getblockheader(blockHash, true) const header = await client.getblockheader({ blockhash: blockHash, verbose: true })
if (header) { if (header) {
const dbBlock = { const dbBlock = {

6
tracker/block-worker.js

@ -8,7 +8,7 @@ const { isMainThread, parentPort } = require('worker_threads')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const Block = require('./block') const Block = require('./block')
@ -104,7 +104,7 @@ async function processMessage(msg) {
*/ */
async function initBlock(header) { async function initBlock(header) {
status = INITIALIZED status = INITIALIZED
const hex = await rpcClient.getblock(header.hash, false) const hex = await rpcClient.getblock({ blockhash: header.hash, verbosity: 0 })
block = new Block(hex, header) block = new Block(hex, header)
return true return true
} }
@ -153,7 +153,7 @@ function reset() {
/** /**
* MAIN * MAIN
*/ */
const rpcClient = new RpcClient() const rpcClient = createRpcClient()
let block = null let block = null
let txsForBroadcast = [] let txsForBroadcast = []
let status = IDLE let status = IDLE

16
tracker/blockchain-processor.js

@ -11,7 +11,7 @@ const util = require('../lib/util')
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const Block = require('./block') const Block = require('./block')
const blocksProcessor = require('./blocks-processor') const blocksProcessor = require('./blocks-processor')
@ -28,7 +28,7 @@ class BlockchainProcessor {
*/ */
constructor(notifSock) { constructor(notifSock) {
// RPC client // RPC client
this.client = new RpcClient() this.client = createRpcClient()
// ZeroMQ socket for bitcoind blocks messages // ZeroMQ socket for bitcoind blocks messages
this.blkSock = null this.blkSock = null
// Initialize a semaphor protecting the onBlockHash() method // Initialize a semaphor protecting the onBlockHash() method
@ -117,8 +117,8 @@ class BlockchainProcessor {
await util.seriesCall(blockRange, async height => { await util.seriesCall(blockRange, async height => {
try { try {
const blockHash = await this.client.getblockhash(height) const blockHash = await this.client.getblockhash({ height })
const header = await this.client.getblockheader(blockHash, true) const header = await this.client.getblockheader({ blockhash: blockHash, verbose: true })
prevBlockId = await this.processBlockHeader(header, prevBlockId) prevBlockId = await this.processBlockHeader(header, prevBlockId)
} catch(e) { } catch(e) {
Logger.error(e, 'Tracker : BlockchainProcessor.catchupIBDMode()') Logger.error(e, 'Tracker : BlockchainProcessor.catchupIBDMode()')
@ -236,7 +236,7 @@ class BlockchainProcessor {
let headers = null let headers = null
try { try {
const header = await this.client.getblockheader(blockHash, true) const header = await this.client.getblockheader({ blockhash: blockHash, verbose: true })
Logger.info(`Tracker : Block #${header.height} ${blockHash}`) Logger.info(`Tracker : Block #${header.height} ${blockHash}`)
// Grab all headers between this block and last known // Grab all headers between this block and last known
headers = await this.chainBacktrace([header]) headers = await this.chainBacktrace([header])
@ -286,7 +286,7 @@ class BlockchainProcessor {
if (block == null) { if (block == null) {
// Previous block does not exist in database. Grab from bitcoind // Previous block does not exist in database. Grab from bitcoind
const header = await this.client.getblockheader(deepest.previousblockhash, true) const header = await this.client.getblockheader({ blockhash: deepest.previousblockhash, verbose: true })
headers.push(header) headers.push(header)
return this.chainBacktrace(headers) return this.chainBacktrace(headers)
} else { } else {
@ -363,8 +363,8 @@ class BlockchainProcessor {
return util.seriesCall(chunks, async chunk => { return util.seriesCall(chunks, async chunk => {
const headers = await util.parallelCall(chunk, async height => { const headers = await util.parallelCall(chunk, async height => {
const hash = await this.client.getblockhash(height) const hash = await this.client.getblockhash({ height })
return await this.client.getblockheader(hash) return await this.client.getblockheader({ blockhash: hash })
}) })
return this.processBlocks(headers) return this.processBlocks(headers)
}) })

4
tracker/index.js

@ -6,7 +6,7 @@
'use strict' 'use strict'
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { waitForBitcoindRpcApi } = require('../lib/bitcoind-rpc/rpc-client')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
@ -21,7 +21,7 @@
// Wait for Bitcoind RPC API // Wait for Bitcoind RPC API
// being ready to process requests // being ready to process requests
await RpcClient.waitForBitcoindRpcApi() await waitForBitcoindRpcApi()
// Initialize the db wrapper // Initialize the db wrapper
const dbConfig = { const dbConfig = {

6
tracker/mempool-processor.js

@ -11,7 +11,7 @@ const util = require('../lib/util')
const Logger = require('../lib/logger') const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper') const db = require('../lib/db/mysql-db-wrapper')
const network = require('../lib/bitcoin/network') const network = require('../lib/bitcoin/network')
const RpcClient = require('../lib/bitcoind-rpc/rpc-client') const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const keys = require('../keys')[network.key] const keys = require('../keys')[network.key]
const Transaction = require('./transaction') const Transaction = require('./transaction')
const TransactionsBundle = require('./transactions-bundle') const TransactionsBundle = require('./transactions-bundle')
@ -28,7 +28,7 @@ class MempoolProcessor {
*/ */
constructor(notifSock) { constructor(notifSock) {
// RPC client // RPC client
this.client = new RpcClient() this.client = createRpcClient()
// ZeroMQ socket for notifications sent to others components // ZeroMQ socket for notifications sent to others components
this.notifSock = notifSock this.notifSock = notifSock
// Mempool buffer // Mempool buffer
@ -261,7 +261,7 @@ class MempoolProcessor {
if (unconfirmedTxs.length > 0) { if (unconfirmedTxs.length > 0) {
await util.parallelCall(unconfirmedTxs, tx => { await util.parallelCall(unconfirmedTxs, tx => {
try { try {
return this.client.getrawtransaction(tx.txnTxid, true) return this.client.getrawtransaction( { txid: tx.txnTxid, verbose: true })
.then(async rtx => { .then(async rtx => {
if (!rtx.blockhash) return null if (!rtx.blockhash) return null
// Transaction is confirmed // Transaction is confirmed

Loading…
Cancel
Save