use mempool spent index

This commit is contained in:
Patrick Nagurny 2015-09-17 15:41:05 -04:00
parent 8b1a2aa001
commit bc39a5e480

View File

@ -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 {