Unverified Commit ce349060 authored by Pavel Ševčík's avatar Pavel Ševčík
Browse files

Catch unhandled promise rejections

parent 4b82bd63
......@@ -4,12 +4,11 @@
*/
'use strict'
const util = require('../util')
const errors = require('../errors')
const Logger = require('../logger')
const network = require('../bitcoin/network')
const keys = require('../../keys')[network.key]
const { createRpcClient } = require('./rpc-client')
const { createRpcClient, waitForBitcoindRpcApi } = require('./rpc-client')
const latestBlock = require('./latest-block')
......@@ -24,12 +23,20 @@ class Fees {
constructor() {
this.block = -1
this.targets = [2, 4, 6, 12, 24]
this.fees = {}
this.fees = {
2: 0,
4: 0,
6: 0,
12: 0,
24: 0
}
this.feeType = keys.bitcoind.feeType
this.rpcClient = createRpcClient()
this.refresh()
waitForBitcoindRpcApi().then(() => {
this.refresh()
})
}
/**
......@@ -53,15 +60,18 @@ class Fees {
* @returns {Promise<void>}
*/
async refresh() {
await util.parallelCall(this.targets, async tgt => {
try {
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)
} catch(e) {
Logger.error(e, 'Bitcoind RPC : Fees.refresh()')
this.fees[tgt] = 0
}
})
try {
const requests = this.targets.map((target) => {
return { method: 'estimatesmartfee', params: { conf_target: target, estimate_mode: this.feeType }, id: target }
})
const responses = await this.rpcClient.batch(requests)
responses.forEach((fee) => {
this.fees[fee.id] = (fee.result.errors && fee.result.errors.length > 0) ? 0 : Math.round(fee.result.feerate * 1e5)
})
} catch(e) {
Logger.error(e, 'Bitcoind RPC : Fees.refresh()')
}
this.block = latestBlock.height
}
......
......@@ -9,7 +9,7 @@ const Logger = require('../logger')
const util = require('../util')
const network = require('../bitcoin/network')
const keys = require('../../keys')[network.key]
const { createRpcClient } = require('./rpc-client')
const { createRpcClient, waitForBitcoindRpcApi } = require('./rpc-client')
/**
......@@ -29,8 +29,10 @@ class LatestBlock {
// Initialize the rpc client
this.rpcClient = createRpcClient()
// Gets the latest block from bitcoind
this.rpcClient.getbestblockhash().then(hash => this.onBlockHash(hash))
waitForBitcoindRpcApi().then(() => {
// Gets the latest block from bitcoind
this.rpcClient.getbestblockhash().then((hash) => this.onBlockHash(hash))
})
// Initializes zmq socket
this.sock = zmq.socket('sub')
......@@ -54,14 +56,20 @@ class LatestBlock {
* @returns {Promise}
*/
async onBlockHash(hash) {
const header = await this.rpcClient.getblockheader({ blockhash: hash })
try {
const header = await this.rpcClient.getblockheader({ blockhash: hash })
this.height = header.height
this.hash = hash
this.time = header.mediantime
this.diff = header.difficulty
this.height = header.height
this.hash = hash
this.time = header.mediantime
this.diff = header.difficulty
Logger.info(`Bitcoind RPC : Block ${this.height} ${this.hash}`)
Logger.info(`Bitcoind RPC : Block ${this.height} ${this.hash}`)
} catch (err) {
Logger.error(err, 'Bitcoind RPC : LatestBlock.onBlockHash()')
await util.delay(2000)
return this.onBlockHash(hash)
}
}
}
......
......@@ -45,13 +45,14 @@ class Transactions {
*/
async getTransactions(txids, fees) {
try {
const rpcCalls = txids.map(txid => {
const rpcCalls = txids.map((txid, index) => {
return {
method: 'getrawtransaction',
params: {
txid,
verbose: true
}
},
id: index
}
})
......
......@@ -11,7 +11,7 @@ const util = require('../lib/util')
const Logger = require('../lib/logger')
const db = require('../lib/db/mysql-db-wrapper')
const network = require('../lib/bitcoin/network')
const { createRpcClient } = require('../lib/bitcoind-rpc/rpc-client')
const { createRpcClient, waitForBitcoindRpcApi } = require('../lib/bitcoind-rpc/rpc-client')
const keys = require('../keys')[network.key]
const Block = require('./block')
const blocksProcessor = require('./blocks-processor')
......@@ -60,16 +60,23 @@ class BlockchainProcessor {
* @returns {Promise<void>}
*/
async catchup() {
const [highest, info] = await Promise.all([db.getHighestBlock(), this.client.getblockchaininfo()])
const daemonNbHeaders = info.headers
// Consider that we are in IBD mode if Dojo is far in the past (> 13,000 blocks)
this.isIBD = (highest.blockHeight < 681000) || (highest.blockHeight < daemonNbHeaders - 13000)
try {
await waitForBitcoindRpcApi()
const [highest, info] = await Promise.all([db.getHighestBlock(), this.client.getblockchaininfo()])
const daemonNbHeaders = info.headers
if (this.isIBD)
return this.catchupIBDMode()
else
return this.catchupNormalMode()
// Consider that we are in IBD mode if Dojo is far in the past (> 13,000 blocks)
this.isIBD = (highest.blockHeight < 681000) || (highest.blockHeight < daemonNbHeaders - 13000)
if (this.isIBD)
return this.catchupIBDMode()
else
return this.catchupNormalMode()
} catch (err) {
Logger.error(err, 'Tracker : BlockchainProcessor.catchup()');
await util.delay(2000)
return this.catchup()
}
}
/**
......
......@@ -303,16 +303,20 @@ class MempoolProcessor {
async _refreshActiveStatus() {
// Get highest header in the blockchain
// Get highest block processed by the tracker
const [highestBlock, info] = await Promise.all([db.getHighestBlock(), this.client.getblockchaininfo()])
const highestHeader = info.headers
try {
const [highestBlock, info] = await Promise.all([db.getHighestBlock(), this.client.getblockchaininfo()])
const highestHeader = info.headers
if (highestBlock == null || highestBlock.blockHeight === 0) {
this.isActive = false
return
}
if (highestBlock == null || highestBlock.blockHeight === 0) {
this.isActive = false
return
}
// Tolerate a delay of 6 blocks
this.isActive = (highestHeader >= 550000) && (highestHeader <= highestBlock.blockHeight + 6)
// Tolerate a delay of 6 blocks
this.isActive = (highestHeader >= 550000) && (highestHeader <= highestBlock.blockHeight + 6)
} catch (err) {
Logger.error(err, 'Tracker : MempoolProcessor._refreshActiveStatus()')
}
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment