Browse Source

Merge pull request #158 from Samourai-Wallet/enh_dojo_txs_caches

misc improvements in bitcoind rpc transactions class
use-env-var-docker
kenshin samourai 5 years ago
committed by GitHub
parent
commit
4c4def784a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      lib/bitcoind-rpc/transactions.js

28
lib/bitcoind-rpc/transactions.js

@ -23,18 +23,9 @@ class Transactions {
*/ */
constructor() { constructor() {
// Caches // Caches
this.txCache = LRU({
// Maximum number of transactions to store
max: 10000,
// Function used to compute length of item
length: (n, key) => 1,
// Maximum age for items in the cache. Items do not expire
maxAge: Infinity
})
this.prevCache = LRU({ this.prevCache = LRU({
// Maximum number of transactions to store // Maximum number of transactions to store
max: 100000, max: 20000,
// Function used to compute length of item // Function used to compute length of item
length: (n, key) => 1, length: (n, key) => 1,
// Maximum age for items in the cache. Items do not expire // Maximum age for items in the cache. Items do not expire
@ -85,19 +76,9 @@ class Transactions {
* @returns {Promise} * @returns {Promise}
*/ */
async getTransaction(txid, fees) { async getTransaction(txid, fees) {
const keyCache = `${txid}-${fees ? '1' : '0'}`
// Return transaction from cache when possible
if (this.txCache.has(keyCache))
return this.txCache.get(keyCache)
try { try {
const tx = await this.rpcClient.getrawtransaction(txid, true) const tx = await this.rpcClient.getrawtransaction(txid, true)
const ret = await this._prepareTxResult(tx) return this._prepareTxResult(tx, fees)
// Store the result in cache
if (ret.block && ret.block.hash)
this.txCache.set(txid, ret)
return ret
} catch(e) { } catch(e) {
Logger.error(e, 'Bitcoind RPC : Transaction.getTransaction()') Logger.error(e, 'Bitcoind RPC : Transaction.getTransaction()')
return Promise.reject(errors.generic.GEN) return Promise.reject(errors.generic.GEN)
@ -197,10 +178,7 @@ class Transactions {
ptx = this.prevCache.get(inTxid) ptx = this.prevCache.get(inTxid)
} else { } else {
ptx = await this.rpcClient.getrawtransaction(inTxid, true) ptx = await this.rpcClient.getrawtransaction(inTxid, true)
if (ptx.blockhash && ptx.confirmations && ptx.blocktime) { this.prevCache.set(inTxid, ptx)
ptx.height = rpcLatestBlock.height - ptx.confirmations + 1
this.prevCache.set(inTxid, ptx)
}
} }
const outpoint = ptx.vout[txin.outpoint.vout] const outpoint = ptx.vout[txin.outpoint.vout]

Loading…
Cancel
Save