use mempool spent index
This commit is contained in:
parent
8b1a2aa001
commit
bc39a5e480
@ -28,6 +28,7 @@ var AddressService = function(options) {
|
|||||||
|
|
||||||
this.mempoolOutputIndex = {};
|
this.mempoolOutputIndex = {};
|
||||||
this.mempoolInputIndex = {};
|
this.mempoolInputIndex = {};
|
||||||
|
this.mempoolSpentIndex = {};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -177,6 +178,11 @@ AddressService.prototype.updateMempoolIndex = function(tx) {
|
|||||||
for (var inputIndex = 0; inputIndex < inputLength; inputIndex++) {
|
for (var inputIndex = 0; inputIndex < inputLength; inputIndex++) {
|
||||||
|
|
||||||
var input = tx.inputs[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);
|
var address = input.script.toAddress(this.node.network);
|
||||||
if (!address) {
|
if (!address) {
|
||||||
continue;
|
continue;
|
||||||
@ -198,6 +204,7 @@ AddressService.prototype.resetMempoolIndex = function(callback) {
|
|||||||
var transactionBuffers = self.node.services.bitcoind.getMempoolTransactions();
|
var transactionBuffers = self.node.services.bitcoind.getMempoolTransactions();
|
||||||
this.mempoolInputIndex = {};
|
this.mempoolInputIndex = {};
|
||||||
this.mempoolOutputIndex = {};
|
this.mempoolOutputIndex = {};
|
||||||
|
this.mempoolSpentIndex = {};
|
||||||
async.each(transactionBuffers, function(txBuffer, next) {
|
async.each(transactionBuffers, function(txBuffer, next) {
|
||||||
var tx = Transaction().fromBuffer(txBuffer);
|
var tx = Transaction().fromBuffer(txBuffer);
|
||||||
self.updateMempoolIndex(tx);
|
self.updateMempoolIndex(tx);
|
||||||
@ -894,8 +901,9 @@ AddressService.prototype.getAddressSummary = function(address, options, callback
|
|||||||
|
|
||||||
var outputs;
|
var outputs;
|
||||||
var inputs;
|
var inputs;
|
||||||
|
var mempoolInputs;
|
||||||
|
|
||||||
async.waterfall(
|
async.series(
|
||||||
[
|
[
|
||||||
function(next) {
|
function(next) {
|
||||||
if(options.noTxList) {
|
if(options.noTxList) {
|
||||||
@ -928,23 +936,25 @@ AddressService.prototype.getAddressSummary = function(address, options, callback
|
|||||||
var txids = [];
|
var txids = [];
|
||||||
|
|
||||||
for(var i = 0; i < outputs.length; i++) {
|
for(var i = 0; i < outputs.length; i++) {
|
||||||
var spent = self.node.services.bitcoind.isSpent(outputs[i].txid, outputs[i].outputIndex);
|
// Bitcoind's isSpent at the moment only works for confirmed transactions
|
||||||
var spentConfirmed = true; // TODO
|
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]);
|
txids.push(outputs[i]);
|
||||||
totalReceived += outputs[i].satoshis;
|
|
||||||
unconfirmedBalance += outputs[i].satoshis;
|
unconfirmedBalance += outputs[i].satoshis;
|
||||||
if(outputs[i].confirmations) {
|
if(outputs[i].confirmations) {
|
||||||
|
totalReceived += outputs[i].satoshis;
|
||||||
balance += outputs[i].satoshis;
|
balance += outputs[i].satoshis;
|
||||||
appearances++;
|
appearances++;
|
||||||
} else {
|
} else {
|
||||||
unconfirmedAppearances++;
|
unconfirmedAppearances++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(spent) {
|
if(spentDB || spentMempool) {
|
||||||
totalSpent += outputs[i].satoshis;
|
|
||||||
unconfirmedBalance -= outputs[i].satoshis;
|
unconfirmedBalance -= outputs[i].satoshis;
|
||||||
if(spentConfirmed) {
|
if(spentDB) {
|
||||||
|
totalSpent += outputs[i].satoshis;
|
||||||
balance -= outputs[i].satoshis;
|
balance -= outputs[i].satoshis;
|
||||||
appearances++;
|
appearances++;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user