From bc39a5e480795a67ace8d33b3a23679225332942 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Thu, 17 Sep 2015 15:41:05 -0400 Subject: [PATCH] use mempool spent index --- lib/services/address/index.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 5783b43d..f08becf5 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -28,6 +28,7 @@ var AddressService = function(options) { this.mempoolOutputIndex = {}; this.mempoolInputIndex = {}; + this.mempoolSpentIndex = {}; }; @@ -177,6 +178,11 @@ AddressService.prototype.updateMempoolIndex = function(tx) { for (var inputIndex = 0; inputIndex < inputLength; inputIndex++) { var input = tx.inputs[inputIndex]; + + // Update spent index + var spentIndexKey = [input.prevTxId.toString('hex'), input.outputIndex].join('-'); + this.mempoolSpentIndex[spentIndexKey] = true; + var address = input.script.toAddress(this.node.network); if (!address) { continue; @@ -198,6 +204,7 @@ AddressService.prototype.resetMempoolIndex = function(callback) { var transactionBuffers = self.node.services.bitcoind.getMempoolTransactions(); this.mempoolInputIndex = {}; this.mempoolOutputIndex = {}; + this.mempoolSpentIndex = {}; async.each(transactionBuffers, function(txBuffer, next) { var tx = Transaction().fromBuffer(txBuffer); self.updateMempoolIndex(tx); @@ -894,8 +901,9 @@ AddressService.prototype.getAddressSummary = function(address, options, callback var outputs; var inputs; + var mempoolInputs; - async.waterfall( + async.series( [ function(next) { if(options.noTxList) { @@ -928,23 +936,25 @@ AddressService.prototype.getAddressSummary = function(address, options, callback var txids = []; for(var i = 0; i < outputs.length; i++) { - var spent = self.node.services.bitcoind.isSpent(outputs[i].txid, outputs[i].outputIndex); - var spentConfirmed = true; // TODO + // Bitcoind's isSpent at the moment only works for confirmed transactions + var spentDB = self.node.services.bitcoind.isSpent(outputs[i].txid, outputs[i].outputIndex); + var spentIndexKey = [outputs[i].txid, outputs[i].outputIndex].join('-'); + var spentMempool = self.mempoolSpentIndex[spentIndexKey]; txids.push(outputs[i]); - totalReceived += outputs[i].satoshis; unconfirmedBalance += outputs[i].satoshis; if(outputs[i].confirmations) { + totalReceived += outputs[i].satoshis; balance += outputs[i].satoshis; appearances++; } else { unconfirmedAppearances++; } - if(spent) { - totalSpent += outputs[i].satoshis; + if(spentDB || spentMempool) { unconfirmedBalance -= outputs[i].satoshis; - if(spentConfirmed) { + if(spentDB) { + totalSpent += outputs[i].satoshis; balance -= outputs[i].satoshis; appearances++; } else {