bitcoind: add mempool to address txid results
This commit is contained in:
parent
ab70aa666e
commit
60333bcb0e
@ -394,28 +394,69 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Bitcoin.prototype._getBalanceFromMempool = function(deltas) {
|
||||||
|
var satoshis = 0;
|
||||||
|
for (var i = 0; i < deltas.length; i++) {
|
||||||
|
satoshis += deltas[i].satoshis;
|
||||||
|
}
|
||||||
|
return satoshis;
|
||||||
|
};
|
||||||
|
|
||||||
|
Bitcoin.prototype._getTxidsFromMempool = function(deltas) {
|
||||||
|
var mempoolTxids = [];
|
||||||
|
var mempoolTxidsKnown = {};
|
||||||
|
for (var i = 0; i < deltas.length; i++) {
|
||||||
|
var txid = deltas[i].txid;
|
||||||
|
if (!mempoolTxidsKnown[txid]) {
|
||||||
|
mempoolTxids.push(txid);
|
||||||
|
mempoolTxidsKnown[txid] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mempoolTxids;
|
||||||
|
};
|
||||||
|
|
||||||
Bitcoin.prototype.getAddressTxids = function(addressArg, options, callback) {
|
Bitcoin.prototype.getAddressTxids = function(addressArg, options, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool;
|
||||||
var addresses = [addressArg];
|
var addresses = [addressArg];
|
||||||
if (Array.isArray(addressArg)) {
|
if (Array.isArray(addressArg)) {
|
||||||
addresses = addressArg;
|
addresses = addressArg;
|
||||||
}
|
}
|
||||||
var cacheKey = addresses.join('');
|
var cacheKey = addresses.join('');
|
||||||
|
var mempoolTxids = [];
|
||||||
var txids = self.txidsCache.get(cacheKey);
|
var txids = self.txidsCache.get(cacheKey);
|
||||||
if (txids) {
|
|
||||||
return setImmediate(function() {
|
function finish() {
|
||||||
callback(null, txids);
|
if (txids) {
|
||||||
});
|
var allTxids = mempoolTxids.reverse().concat(txids);
|
||||||
} else {
|
return setImmediate(function() {
|
||||||
self.client.getAddressTxids({addresses: addresses}, function(err, response) {
|
callback(null, allTxids);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
self.client.getAddressTxids({addresses: addresses}, function(err, response) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
response.result.reverse();
|
||||||
|
self.txidsCache.set(cacheKey, response.result);
|
||||||
|
var allTxids = mempoolTxids.reverse().concat(response.result);
|
||||||
|
return callback(null, allTxids);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queryMempool) {
|
||||||
|
self.client.getAddressMempool({addresses: addresses}, function(err, response) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
response.result.reverse();
|
mempoolTxids = self._getTxidsFromMempool(response.result);
|
||||||
self.txidsCache.set(cacheKey, response.result);
|
finish();
|
||||||
return callback(null, response.result);
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Bitcoin.prototype._getConfirmationsDetail = function(transaction) {
|
Bitcoin.prototype._getConfirmationsDetail = function(transaction) {
|
||||||
@ -585,10 +626,11 @@ Bitcoin.prototype.getAddressHistory = function(addressArg, options, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Bitcoin.prototype.getAddressSummary = function(addressArg, options, callback) {
|
Bitcoin.prototype.getAddressSummary = function(addressArg, options, callback) {
|
||||||
// TODO: optional mempool
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var summary = {};
|
var summary = {};
|
||||||
|
var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool;
|
||||||
var summaryTxids = [];
|
var summaryTxids = [];
|
||||||
|
var mempoolTxids = [];
|
||||||
|
|
||||||
var addresses = [addressArg];
|
var addresses = [addressArg];
|
||||||
if (Array.isArray(addressArg)) {
|
if (Array.isArray(addressArg)) {
|
||||||
@ -597,14 +639,10 @@ Bitcoin.prototype.getAddressSummary = function(addressArg, options, callback) {
|
|||||||
|
|
||||||
var cacheKey = addresses.join('');
|
var cacheKey = addresses.join('');
|
||||||
|
|
||||||
if (_.isUndefined(options.queryMempool)) {
|
|
||||||
options.queryMempool = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function querySummary() {
|
function querySummary() {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function getTxList(done) {
|
function getTxList(done) {
|
||||||
self.getAddressTxids(addressArg, options, function(err, txids) {
|
self.getAddressTxids(addressArg, {queryMempool: false}, function(err, txids) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
}
|
}
|
||||||
@ -623,14 +661,29 @@ Bitcoin.prototype.getAddressSummary = function(addressArg, options, callback) {
|
|||||||
summary.balance = data.balance;
|
summary.balance = data.balance;
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
function getMempool(done) {
|
||||||
|
if (!queryMempool) {
|
||||||
|
return done();
|
||||||
|
}
|
||||||
|
self.client.getAddressMempool({'addresses': [addressArg]}, function(err, response) {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
mempoolTxids = self._getTxidsFromMempool(response.result);
|
||||||
|
summary.unconfirmedAppearances = mempoolTxids.length;
|
||||||
|
summary.unconfirmedBalance = self._getBalanceFromMempool(response.result);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
},
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
self.summaryCache.set(cacheKey, summary);
|
self.summaryCache.set(cacheKey, summary);
|
||||||
if (!options.noTxList) {
|
if (!options.noTxList) {
|
||||||
summary.txids = summaryTxids;
|
var allTxids = mempoolTxids.reverse().concat(summaryTxids);
|
||||||
|
summary.txids = allTxids;
|
||||||
}
|
}
|
||||||
callback(null, summary);
|
callback(null, summary);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user