Browse Source

add new getChaintipHeight() method to remote importer and data sources

use-env-var-docker
kenshin-samourai 4 years ago
parent
commit
08e8e82f40
  1. 4
      lib/indexer-rpc/rpc-client.js
  2. 10
      lib/remote-importer/bitcoind-wrapper.js
  3. 13
      lib/remote-importer/esplora-wrapper.js
  4. 25
      lib/remote-importer/local-indexer-wrapper.js
  5. 13
      lib/remote-importer/oxt-wrapper.js
  6. 9
      lib/remote-importer/remote-importer.js
  7. 16
      lib/remote-importer/sources.js

4
lib/indexer-rpc/rpc-client.js

@ -181,9 +181,9 @@ class RpcClient {
throw new Error(JSON.stringify(parsed.error))
// Add the parsed reponse to the array of responses
if (batched) {
responses = parsed.map(p => { return {idxAddr: p.id, txs: p.result} })
responses = parsed.map(p => { return {id: p.id, response: p.result} })
} else {
responses.push({idxAddr: parsed.id, txs: parsed.result})
responses.push({id: parsed.id, response: parsed.result})
}
// Reset the response
response = ''

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

@ -6,6 +6,7 @@
const bitcoin = require('bitcoinjs-lib')
const RpcClient = require('../bitcoind-rpc/rpc-client')
const rpcLatestBlock = require('../bitcoind-rpc/latest-block')
const Logger = require('../logger')
const network = require('../bitcoin/network')
const activeNet = network.network
@ -124,6 +125,15 @@ class BitcoindWrapper extends Wrapper {
return aRet
}
/**
* Retrieve the height of the chaintip for the remote source
* @returns {Promise} returns an object
* {chainTipHeight: <chaintip_height>}
*/
async getChainTipHeight() {
return {'chainTipHeight': rpcLatestBlock.height}
}
}
module.exports = BitcoindWrapper

13
lib/remote-importer/esplora-wrapper.js

@ -123,6 +123,19 @@ class EsploraWrapper extends Wrapper {
return ret
}
/**
* Retrieve the height of the chaintip for the remote source
* @returns {Promise} returns an object
* {chainTipHeight: <chaintip_height>}
*/
async getChainTipHeight() {
let chainTipHeight = null
const result = await this._get(`/api/blocks/tip/height`)
if (result != null)
chainTipHeight = parseInt(result)
return {'chainTipHeight': chainTipHeight}
}
}
// Esplora returns a max of 25 txs per page

25
lib/remote-importer/local-indexer-wrapper.js

@ -64,7 +64,7 @@ class LocalIndexerWrapper extends Wrapper {
scriptHash
)
for (let r of results.txs) {
for (let r of results.response) {
ret.txids.push(r.tx_hash)
ret.ntx++
}
@ -109,8 +109,8 @@ class LocalIndexerWrapper extends Wrapper {
: await this.client.sendRequests(commands)
for (let r of results) {
const addr = addresses[r.idxAddr]
const txids = r.txs.map(t => t.tx_hash)
const addr = addresses[r.id]
const txids = r.response.map(t => t.tx_hash)
ret[addr] = {
address: addr,
@ -131,12 +131,31 @@ class LocalIndexerWrapper extends Wrapper {
return aRet
}
/**
* Retrieve the height of the chaintip for the remote source
* @returns {Promise} returns an object
* {chainTipHeight: <chaintip_height>}
*/
async getChainTipHeight() {
let chainTipHeight = null
const result = await this.client.sendRequest(
LocalIndexerWrapper.HEADERS_SUBSCRIBE_RPC_CMD,
null
)
if (result != null && result['response'] != null && result['response']['height'] != null)
chainTipHeight = parseInt(result['response']['height'])
return {'chainTipHeight': chainTipHeight}
}
}
/**
* Get history RPC command (Electrum protocol)
*/
LocalIndexerWrapper.GET_HISTORY_RPC_CMD = 'blockchain.scripthash.get_history'
/**
* Get history RPC command (Electrum protocol)
*/
LocalIndexerWrapper.HEADERS_SUBSCRIBE_RPC_CMD = 'blockchain.headers.subscribe'
module.exports = LocalIndexerWrapper

13
lib/remote-importer/oxt-wrapper.js

@ -109,6 +109,19 @@ class OxtWrapper extends Wrapper {
return ret
}
/**
* Retrieve the height of the chaintip for the remote source
* @returns {Promise} returns an object
* {chainTipHeight: <chaintip_height>}
*/
async getChainTipHeight() {
let chainTipHeight = null
const result = await this._get(`/lastblock`)
if (result != null && result['data'].length == 1)
chainTipHeight = parseInt(result['data'][0]['height'])
return {'chainTipHeight': chainTipHeight}
}
}
module.exports = OxtWrapper

9
lib/remote-importer/remote-importer.js

@ -465,6 +465,15 @@ class RemoteImporter {
}
}
/**
* Retrieve the height of the chaintip for the remote source
* @returns {Promise} returns an object
* {chainTipHeight: <chaintip_height>}
*/
async getChainTipHeight() {
return this.sources.getChainTipHeight()
}
}
module.exports = new RemoteImporter()

16
lib/remote-importer/sources.js

@ -81,6 +81,22 @@ class Sources {
}
}
/**
* Retrieve the height of the chaintip
* @returns {Promise} returns an object
* {chainTipHeight: <chaintip_height>}
*/
async getChainTipHeight() {
let ret = {'chainTipHeight': null}
try {
ret = await this.source.getChainTipHeight()
} catch(e) {
Logger.error(e, `Importer : Sources.getChainTipHeight() : Error while retrieving the chaintip`)
} finally {
return ret
}
}
}
module.exports = Sources

Loading…
Cancel
Save