Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc improvements in bitcoind rpc transactions class #158

Merged
merged 4 commits into from
May 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions lib/bitcoind-rpc/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@ class Transactions {
*/
constructor() {
// 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({
// Maximum number of transactions to store
max: 100000,
max: 20000,
// Function used to compute length of item
length: (n, key) => 1,
// Maximum age for items in the cache. Items do not expire
Expand Down Expand Up @@ -85,19 +76,9 @@ class Transactions {
* @returns {Promise}
*/
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 {
const tx = await this.rpcClient.getrawtransaction(txid, true)
const ret = await this._prepareTxResult(tx)
// Store the result in cache
if (ret.block && ret.block.hash)
this.txCache.set(txid, ret)
return ret
return this._prepareTxResult(tx, fees)
} catch(e) {
Logger.error(e, 'Bitcoind RPC : Transaction.getTransaction()')
return Promise.reject(errors.generic.GEN)
Expand Down Expand Up @@ -197,10 +178,7 @@ class Transactions {
ptx = this.prevCache.get(inTxid)
} else {
ptx = await this.rpcClient.getrawtransaction(inTxid, true)
if (ptx.blockhash && ptx.confirmations && ptx.blocktime) {
ptx.height = rpcLatestBlock.height - ptx.confirmations + 1
this.prevCache.set(inTxid, ptx)
}
this.prevCache.set(inTxid, ptx)
}

const outpoint = ptx.vout[txin.outpoint.vout]
Expand Down