|
|
|
|
@ -16,7 +16,9 @@ var utils = require('../../utils');
|
|
|
|
|
var LRU = require('lru-cache');
|
|
|
|
|
var XXHash = require('xxhash');
|
|
|
|
|
|
|
|
|
|
const MAX_TX_QUERY_LIMIT = 1000;
|
|
|
|
|
const MAX_TX_QUERY_LIMIT_HISTORY = 1000;
|
|
|
|
|
const MAX_TX_QUERY_LIMIT_UTXO = 1000;
|
|
|
|
|
const MAX_TX_QUERY_LIMIT_SUMMARY = 500;
|
|
|
|
|
|
|
|
|
|
// See rationale about this cache at function getTxList(next)
|
|
|
|
|
const TXID_LIST_CACHE_ITEMS = 250; // nr of items (this translates to: consecutive
|
|
|
|
|
@ -177,10 +179,10 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
options.from = options.from || 0;
|
|
|
|
|
options.to = options.to || 0xffffffff;
|
|
|
|
|
//options.from = options.from || 0; //Deprecated, use `after` and `before` option
|
|
|
|
|
//options.to = options.to || 0xffffffff; //Deprecated, use `after` and `before` option
|
|
|
|
|
|
|
|
|
|
if(!callback){ //if only 3 args, then streamer is callback
|
|
|
|
|
if(!_.isFunction(callback)){ //if only 3 args, then streamer is callback
|
|
|
|
|
callback = streamer;
|
|
|
|
|
streamer = () => null; //NULL fn
|
|
|
|
|
}
|
|
|
|
|
@ -189,6 +191,22 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
|
|
|
|
|
options.queryMempool = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(options.mempoolOnly)) {
|
|
|
|
|
options.mempoolOnly = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_.isUndefined(options.reverse)) {
|
|
|
|
|
options.reverse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var old_support = false;
|
|
|
|
|
//Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use)
|
|
|
|
|
if(!_.isUndefined(options.from) || !_.isUndefined(options.to)) {
|
|
|
|
|
old_support = true;
|
|
|
|
|
options.from = options.from || 0;
|
|
|
|
|
options.to = options.to || 0xffffffff; //Max value of to will actually be MAX_TX_QUERY_LIMIT_HISTORY
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isString(addresses)) {
|
|
|
|
|
addresses = [addresses];
|
|
|
|
|
}
|
|
|
|
|
@ -200,15 +218,35 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
|
|
|
|
|
|
|
|
|
|
async.eachLimit(addresses, 4, function(address, next) {
|
|
|
|
|
|
|
|
|
|
self._streamAddressSummary(address, options, function(err, tx){
|
|
|
|
|
var addr_options = Object.assign({}, options), addr_count = 0;
|
|
|
|
|
|
|
|
|
|
results.totalCount++;
|
|
|
|
|
self._streamAddressSummary(address, addr_options, function(err, tx){
|
|
|
|
|
|
|
|
|
|
if(err)
|
|
|
|
|
return log.error(err);
|
|
|
|
|
|
|
|
|
|
if(!options.txNotNeeded && results.items.length < MAX_TX_QUERY_LIMIT)
|
|
|
|
|
results.items.push(tx);
|
|
|
|
|
addr_count++;
|
|
|
|
|
|
|
|
|
|
if(!results.items.some(x => x.txid() === tx.txid())) {//add only if tx not already in array
|
|
|
|
|
if(!options.reverse)
|
|
|
|
|
results.items.unshift(tx); //using unshift, so that recent tx (low) are at front
|
|
|
|
|
else
|
|
|
|
|
results.items.push(tx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(results.items.length > MAX_TX_QUERY_LIMIT_HISTORY) { //remove items from array when overflown
|
|
|
|
|
results.items.sort((a, b) => (b.__height || 0xffffffff) - (a.__height || 0xffffffff) || b.txid().localeCompare(a.txid()));
|
|
|
|
|
let del_count = results.items.length - MAX_TX_QUERY_LIMIT_HISTORY;
|
|
|
|
|
let start_index = (old_support || options.reverse) ? MAX_TX_QUERY_LIMIT_HISTORY : 0;
|
|
|
|
|
results.items.splice(start_index, del_count);
|
|
|
|
|
|
|
|
|
|
results.incomplete = true;
|
|
|
|
|
|
|
|
|
|
if(!old_support && addr_count >= MAX_TX_QUERY_LIMIT_HISTORY)
|
|
|
|
|
addr_options.flag_stop = true; //limit has reached, stop quering db for more tx
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streamer(null, tx);
|
|
|
|
|
|
|
|
|
|
@ -220,7 +258,15 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: sorting of tx list (results.items)
|
|
|
|
|
//sort items in desc block-height, then asc txid (if same height)
|
|
|
|
|
results.items.sort((a, b) => (b.__height || 0xffffffff) - (a.__height || 0xffffffff) || b.txid().localeCompare(a.txid()));
|
|
|
|
|
results.totalCount = results.items.length ;
|
|
|
|
|
|
|
|
|
|
//Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use)
|
|
|
|
|
if(old_support) {
|
|
|
|
|
results.items = results.items.slice(options.from, options.to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, results);
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
@ -254,7 +300,7 @@ AddressService.prototype.__getAddressSummary = function(address, options, callba
|
|
|
|
|
txApperances: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.getAddressHistory(address, options, function(err, results) {
|
|
|
|
|
self.__getAddressHistory(address, options, function(err, results) { //old fn
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
@ -277,19 +323,20 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
options.from = options.from || 0;
|
|
|
|
|
options.to = options.to || 0xffffffff;
|
|
|
|
|
options.txNotNeeded = true; //no need to store tx details in result
|
|
|
|
|
//options.from = options.from || 0; //Deprecated, use `after` and `before` option
|
|
|
|
|
//options.to = options.to || 0xffffffff; //Deprecated, use `after` and `before` option
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(options.queryMempool)) {
|
|
|
|
|
options.queryMempool = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!callback){ //if only 3 args, then streamer is callback
|
|
|
|
|
if(!_.isFunction(callback)){ //if only 3 args, then streamer is callback
|
|
|
|
|
callback = streamer;
|
|
|
|
|
streamer = () => null; //NULL fn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
|
addrStr: address,
|
|
|
|
|
balance: 0,
|
|
|
|
|
@ -304,32 +351,193 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
|
|
|
|
|
txApperances: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self._aggregateAddressSummaryResult(tx, address, result);
|
|
|
|
|
var useCache = _.isUndefined(options.after) && _.isUndefined(options.before);
|
|
|
|
|
var lastTx, lastBlock;
|
|
|
|
|
|
|
|
|
|
self.getAddressHistory(address, options, function(err, tx) {
|
|
|
|
|
self._loadCache(address, result, useCache, function(err, lastCachedTx) {
|
|
|
|
|
if(err)
|
|
|
|
|
log.error(err);
|
|
|
|
|
|
|
|
|
|
if(!_.isUndefined(lastCachedTx))
|
|
|
|
|
options.after = lastCachedTx;
|
|
|
|
|
|
|
|
|
|
self._streamAddressSummary(address, options, function(err, tx) {
|
|
|
|
|
|
|
|
|
|
if(err)
|
|
|
|
|
return log.error(err);
|
|
|
|
|
|
|
|
|
|
if(tx) {
|
|
|
|
|
count++;
|
|
|
|
|
self._aggregateAddressSummaryResult(tx, address, result, options);
|
|
|
|
|
|
|
|
|
|
if(tx.confirmations) {
|
|
|
|
|
lastTx = tx.txid();
|
|
|
|
|
lastBlock = tx.blockhash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(count >= MAX_TX_QUERY_LIMIT_SUMMARY) {//stop quering db when limit reached
|
|
|
|
|
options.flag_stop = true;
|
|
|
|
|
result.incomplete = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streamer(null, tx);
|
|
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.balanceSat = parseInt(result.balanceSat.toFixed());
|
|
|
|
|
result.totalReceivedSat = parseInt(result.totalReceivedSat.toFixed());
|
|
|
|
|
result.totalSentSat = parseInt(result.totalSentSat.toFixed());
|
|
|
|
|
result.txApperances = parseInt(result.txApperances.toFixed());
|
|
|
|
|
result.unconfirmedBalanceSat = parseInt(result.unconfirmedBalanceSat.toFixed());
|
|
|
|
|
result.unconfirmedTxApperances = parseInt(result.unconfirmedTxApperances.toFixed());
|
|
|
|
|
|
|
|
|
|
result.balance = Unit.fromSatoshis(result.balanceSat).toBTC();
|
|
|
|
|
result.totalReceived = Unit.fromSatoshis(result.totalReceivedSat).toBTC();
|
|
|
|
|
result.totalSent = Unit.fromSatoshis(result.totalSentSat).toBTC();
|
|
|
|
|
result.unconfirmedBalance = Unit.fromSatoshis(result.unconfirmedBalanceSat).toBTC();
|
|
|
|
|
|
|
|
|
|
result.lastItem = lastTx;
|
|
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
|
|
|
|
|
|
//store in cache if needed
|
|
|
|
|
if(useCache) {
|
|
|
|
|
if(result.incomplete) //full summary needs to be calculated in background
|
|
|
|
|
self._cacheSummaryInBackground(address, lastTx, lastBlock, result);
|
|
|
|
|
else if (!_.isUndefined(lastCachedTx) && !_.isUndefined(lastTx)
|
|
|
|
|
&& lastTx != lastCachedTx && !self._cacheInstance.has(address)) //update cache if needed
|
|
|
|
|
self._storeCache(address, lastTx, lastBlock, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._cacheInstance = new Set();
|
|
|
|
|
AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, lastBlock, result){
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
|
|
if(self._cacheInstance.has(address))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
self._cacheInstance.add(address);
|
|
|
|
|
|
|
|
|
|
const cache = {
|
|
|
|
|
balanceSat: result.balanceSat,
|
|
|
|
|
totalReceivedSat: result.totalReceivedSat,
|
|
|
|
|
totalSentSat: result.totalSentSat,
|
|
|
|
|
txApperances: result.txApperances,
|
|
|
|
|
unconfirmedBalanceSat: 0, //unconfirmed (mempool) values should not be cached
|
|
|
|
|
unconfirmedTxApperances: 0
|
|
|
|
|
};
|
|
|
|
|
const options = { queryMempool: false, after: lastTx, noTxList: true };
|
|
|
|
|
|
|
|
|
|
self._streamAddressSummary(address, options, function(err, tx) {
|
|
|
|
|
|
|
|
|
|
if(err)
|
|
|
|
|
return log.error(err);
|
|
|
|
|
return log.error(err);
|
|
|
|
|
|
|
|
|
|
if(tx)
|
|
|
|
|
self._aggregateAddressSummaryResult(tx, address, result);
|
|
|
|
|
|
|
|
|
|
streamer(null, tx);
|
|
|
|
|
if(tx) {
|
|
|
|
|
self._aggregateAddressSummaryResult(tx, address, cache, options);
|
|
|
|
|
if(tx.confirmations){
|
|
|
|
|
lastTx = tx.txid();
|
|
|
|
|
lastBlock = tx.blockhash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
return log.error(err);
|
|
|
|
|
|
|
|
|
|
cache.balanceSat = parseInt(cache.balanceSat.toFixed());
|
|
|
|
|
cache.totalReceivedSat = parseInt(cache.totalReceivedSat.toFixed());
|
|
|
|
|
cache.totalSentSat = parseInt(cache.totalSentSat.toFixed());
|
|
|
|
|
cache.txApperances = parseInt(cache.txApperances.toFixed());
|
|
|
|
|
|
|
|
|
|
if(!_.isUndefined(lastTx))
|
|
|
|
|
self._storeCache(address, lastTx, lastBlock, cache);
|
|
|
|
|
|
|
|
|
|
self._cacheInstance.delete(address); //remove from running instance
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._storeCache = function(address, lastCacheTx, lastCacheBlock, result, callback) {
|
|
|
|
|
const self = this;
|
|
|
|
|
var key = self._encoding.encodeAddressCacheKey(address);
|
|
|
|
|
var value = self._encoding.encodeAddressCacheValue(lastCacheTx, lastCacheBlock, result.balanceSat, result.totalReceivedSat, result.totalSentSat, result.txApperances)
|
|
|
|
|
|
|
|
|
|
if(!_.isFunction(callback)) //if callback is not passed, call a empty function
|
|
|
|
|
callback = () => null;
|
|
|
|
|
|
|
|
|
|
self._db.put(key, value, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._loadCache = function(address, result, useCache, callback) {
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
|
|
if(!useCache) //skip if useCache is false (cases like 'after' and/or 'before' parameter is used by client)
|
|
|
|
|
return callback();
|
|
|
|
|
|
|
|
|
|
var key = self._encoding.encodeAddressCacheKey(address);
|
|
|
|
|
self._db.get(key, function(err, value) {
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
if (!value) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.balance = Unit.fromSatoshis(result.balanceSat).toBTC();
|
|
|
|
|
result.totalReceived = Unit.fromSatoshis(result.totalReceivedSat).toBTC();
|
|
|
|
|
result.totalSent = Unit.fromSatoshis(result.totalSentSat).toBTC();
|
|
|
|
|
result.unconfirmedBalance = Unit.fromSatoshis(result.unconfirmedBalanceSat).toBTC();
|
|
|
|
|
var addressCache = self._encoding.decodeAddressCacheValue(value);
|
|
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
|
var lastCacheTx = addressCache.lastTx, lastCacheBlock = addressCache.lastBlock
|
|
|
|
|
|
|
|
|
|
self._block.getBlock(lastCacheBlock, function(err, block) {
|
|
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!block) { //block not found, probably removed in reorg.
|
|
|
|
|
//delete the existing cache and recalc values freshly
|
|
|
|
|
self._deleteCache(address, function() {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
//values are in satoshis
|
|
|
|
|
result.balanceSat = addressCache.balance;
|
|
|
|
|
result.totalReceivedSat = addressCache.received;
|
|
|
|
|
result.totalSentSat = addressCache.sent;
|
|
|
|
|
result.txApperances = addressCache.txApperances;
|
|
|
|
|
|
|
|
|
|
callback(null, lastCacheTx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._deleteCache = function(address, callback) {
|
|
|
|
|
const self = this;
|
|
|
|
|
var key = self._encoding.encodeAddressCacheKey(address);
|
|
|
|
|
|
|
|
|
|
if(!_.isFunction(callback)) //if callback is not passed, call a empty function
|
|
|
|
|
callback = () => null;
|
|
|
|
|
|
|
|
|
|
self._db.del(key, callback);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -375,10 +583,13 @@ AddressService.prototype._setInputResults = function(tx, address, result) {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._aggregateAddressSummaryResult = function (tx, address, result, options){
|
|
|
|
|
AddressService.prototype._getAddressSummaryResult = function(txs, address, result, options) {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
for(var i = 0; i < txs.length; i++) {
|
|
|
|
|
var tx = txs[i];
|
|
|
|
|
|
|
|
|
|
self._setOutputResults(tx, address, result);
|
|
|
|
|
self._setInputResults(tx, address, result);
|
|
|
|
|
|
|
|
|
|
@ -389,20 +600,115 @@ AddressService.prototype._aggregateAddressSummaryResult = function (tx, address,
|
|
|
|
|
result.transactions.push(tx.txid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._getAddressSummaryResult = function(txs, address, result, options) {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
for(var i = 0; i < txs.length; i++) {
|
|
|
|
|
var tx = txs[i];
|
|
|
|
|
self._aggregateAddressSummaryResult(tx, address, result, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._getOccurrenceCount = function(tx, address) {
|
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
|
|
for(var i = 0; i < tx.inputs.length; i++) {
|
|
|
|
|
|
|
|
|
|
var input = tx.inputs[i];
|
|
|
|
|
|
|
|
|
|
if(utils.getAddress(input, this._network) === address)
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(var j = 0; j < tx.outputs.length; j++) {
|
|
|
|
|
|
|
|
|
|
var output = tx.outputs[j];
|
|
|
|
|
|
|
|
|
|
if(utils.getAddress(output, this._network) === address)
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._getOutputResults = function(tx, address) {
|
|
|
|
|
|
|
|
|
|
let value = 0;
|
|
|
|
|
|
|
|
|
|
for(var j = 0; j < tx.outputs.length; j++) {
|
|
|
|
|
|
|
|
|
|
var output = tx.outputs[j];
|
|
|
|
|
|
|
|
|
|
if (utils.getAddress(output, this._network) === address)
|
|
|
|
|
value += output.value;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._getInputResults = function(tx, address) {
|
|
|
|
|
|
|
|
|
|
let value = 0;
|
|
|
|
|
|
|
|
|
|
for(var i = 0; i < tx.inputs.length; i++) {
|
|
|
|
|
|
|
|
|
|
var input = tx.inputs[i];
|
|
|
|
|
|
|
|
|
|
if (utils.getAddress(input, this._network) === address)
|
|
|
|
|
value += tx.__inputValues[i];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._aggregateAddressSummaryResult = function (tx, address, result, options) {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
let output_val = self._getOutputResults(tx, address);
|
|
|
|
|
let input_val = self._getInputResults(tx, address);
|
|
|
|
|
|
|
|
|
|
//aggregate the result
|
|
|
|
|
|
|
|
|
|
if(tx.confirmations) {
|
|
|
|
|
|
|
|
|
|
result.txApperances++;
|
|
|
|
|
|
|
|
|
|
result.totalReceivedSat += output_val;
|
|
|
|
|
result.balanceSat += output_val;
|
|
|
|
|
|
|
|
|
|
result.totalSentSat += input_val;
|
|
|
|
|
result.balanceSat -= input_val;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
result.unconfirmedTxApperances++;
|
|
|
|
|
result.unconfirmedBalanceSat += output_val;
|
|
|
|
|
result.unconfirmedBalanceSat -= input_val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!options.noTxList) {
|
|
|
|
|
|
|
|
|
|
if (!result.transactions) {
|
|
|
|
|
result.transactions = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let txid = tx.txid();
|
|
|
|
|
if(!result.transactions.includes(txid)) { //push txid only if its not in the array
|
|
|
|
|
|
|
|
|
|
result.transactions.unshift(txid); //using unshift, so that recent tx (low confirmation) are at front
|
|
|
|
|
|
|
|
|
|
if(result.transactions.length > MAX_TX_QUERY_LIMIT_SUMMARY)
|
|
|
|
|
result.transactions.pop(); //pop the oldest tx in list (when list limit is maxed out)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressService.prototype.getAddressUnspentOutputs = function(address, options, callback) {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
@ -485,7 +791,7 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
|
|
|
|
|
|
|
|
|
|
utxoStream.on('data', function(data) {
|
|
|
|
|
|
|
|
|
|
if(results.length >= MAX_TX_QUERY_LIMIT) { //Max array limit reached, end response
|
|
|
|
|
if(results.length >= MAX_TX_QUERY_LIMIT_UTXO) { //Max array limit reached, end response
|
|
|
|
|
utxoStream.emit('end');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@ -565,19 +871,21 @@ AddressService.prototype.stop = function(callback) {
|
|
|
|
|
|
|
|
|
|
AddressService.prototype._getTxidStream = function(address, options) {
|
|
|
|
|
|
|
|
|
|
var start = this._encoding.encodeAddressIndexKey(address);
|
|
|
|
|
var end = Buffer.concat([
|
|
|
|
|
start.slice(0, address.length + 4),
|
|
|
|
|
options.endHeightBuf,
|
|
|
|
|
new Buffer(new Array(83).join('f'), 'hex')
|
|
|
|
|
]);
|
|
|
|
|
var criteria = {};
|
|
|
|
|
|
|
|
|
|
var criteria = {
|
|
|
|
|
gte: start,
|
|
|
|
|
lte: end,
|
|
|
|
|
reverse: true // txids stream from low confirmations to high confirmations
|
|
|
|
|
};
|
|
|
|
|
if(options.after)
|
|
|
|
|
criteria.gt = this._encoding.encodeAddressIndexKey(address, options.start, options.after, 0xffffffff, 1, 0xffffffff); //0xffffffff is for getting after the txid
|
|
|
|
|
else
|
|
|
|
|
criteria.gte = this._encoding.encodeAddressIndexKey(address, options.start);
|
|
|
|
|
|
|
|
|
|
if(options.before)
|
|
|
|
|
criteria.lt = this._encoding.encodeAddressIndexKey(address, options.end, options.before); //get before the txid
|
|
|
|
|
else
|
|
|
|
|
criteria.lte = this._encoding.encodeAddressIndexKey(address, options.end, Array(65).join('f'), 0xffffffff, 1, 0xffffffff);
|
|
|
|
|
|
|
|
|
|
//reverse option can be used explictly when latest tx are required
|
|
|
|
|
if(options.reverse)
|
|
|
|
|
criteria.reverse = true;
|
|
|
|
|
// txid stream
|
|
|
|
|
var txidStream = this._db.createKeyStream(criteria);
|
|
|
|
|
|
|
|
|
|
@ -588,6 +896,7 @@ AddressService.prototype._getTxidStream = function(address, options) {
|
|
|
|
|
return txidStream;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//(used by old fn)
|
|
|
|
|
AddressService.prototype._getAddressTxHistory = function(options, callback) {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
@ -616,6 +925,7 @@ AddressService.prototype._getAddressTxHistory = function(options, callback) {
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//(used by old fn)
|
|
|
|
|
AddressService.prototype._getAddressTxidHistory = function(address, options, callback) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
@ -625,9 +935,6 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
|
|
|
|
|
|
|
|
|
|
var results = [];
|
|
|
|
|
|
|
|
|
|
options.endHeightBuf = new Buffer(4);
|
|
|
|
|
options.endHeightBuf.writeUInt32BE(options.end);
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(options.queryMempool)) {
|
|
|
|
|
options.queryMempool = true;
|
|
|
|
|
}
|
|
|
|
|
@ -677,12 +984,14 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
|
|
|
|
|
txIdTransformStream._transform = function(chunk, enc, callback) {
|
|
|
|
|
var txInfo = self._encoding.decodeAddressIndexKey(chunk);
|
|
|
|
|
|
|
|
|
|
if(results.length >= MAX_TX_QUERY_LIMIT) { //Max array limit reached, end response
|
|
|
|
|
if(results.length >= MAX_TX_QUERY_LIMIT_HISTORY) { //Max array limit reached, end response
|
|
|
|
|
txIdTransformStream.emit('end');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.push({ txid: txInfo.txid, height: txInfo.height });
|
|
|
|
|
if(!results.some(r => r.txid == txInfo.txid)) //add txid to array only if its not already there
|
|
|
|
|
results.push({ txid: txInfo.txid, height: txInfo.height });
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@ -701,25 +1010,37 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
|
|
|
|
|
options.start = options.start || 0;
|
|
|
|
|
options.end = options.end || 0xffffffff;
|
|
|
|
|
|
|
|
|
|
options.from = options.from || 0; //TODO: check from/to options are working or not
|
|
|
|
|
options.to = options.to || 0xffffffff;
|
|
|
|
|
//options.from = options.from || 0; //Deprecated, use `after` and `before` option
|
|
|
|
|
//options.to = options.to || 0xffffffff; //Deprecated, use `after` and `before` option
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(options.queryMempool)) {
|
|
|
|
|
options.queryMempool = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.endHeightBuf = new Buffer(4);
|
|
|
|
|
options.endHeightBuf.writeUInt32BE(options.end);
|
|
|
|
|
if (_.isUndefined(options.mempoolOnly)) {
|
|
|
|
|
options.mempoolOnly = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isUndefined(options.queryMempool)) {
|
|
|
|
|
options.queryMempool = true;
|
|
|
|
|
if (_.isUndefined(options.reverse)) {
|
|
|
|
|
options.reverse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//declare the queue to process tx data
|
|
|
|
|
var tmpTxList = {}; //store processed txid temporarily to ignore duplication
|
|
|
|
|
|
|
|
|
|
var q = async.queue(function(task, cb) {
|
|
|
|
|
var q = async.queue(function(id, cb) {
|
|
|
|
|
|
|
|
|
|
let {id, options} = task;
|
|
|
|
|
//duplication finding
|
|
|
|
|
if(id.txid in tmpTxList){
|
|
|
|
|
|
|
|
|
|
tmpTxList[id.txid][0]++;
|
|
|
|
|
|
|
|
|
|
if(tmpTxList[id.txid][1] !== null && tmpTxList[id.txid][0] >= tmpTxList[id.txid][1]) //all duplications are found for this txid
|
|
|
|
|
delete tmpTxList[id.txid];
|
|
|
|
|
|
|
|
|
|
return cb();
|
|
|
|
|
|
|
|
|
|
} else tmpTxList[id.txid] = [1, null];
|
|
|
|
|
|
|
|
|
|
if (id.height === 0xffffffff) {
|
|
|
|
|
|
|
|
|
|
@ -736,46 +1057,95 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
|
|
|
|
|
|
|
|
|
|
self._transaction.getDetailedTransaction(id.txid, options, cb);
|
|
|
|
|
|
|
|
|
|
}, 4);
|
|
|
|
|
}, 1);
|
|
|
|
|
|
|
|
|
|
q.pause(); //pause and wait until queue is set
|
|
|
|
|
//q.pause(); //pause and wait until queue is set (not needed)
|
|
|
|
|
|
|
|
|
|
function process_chunk(err, tx){
|
|
|
|
|
function chunkCallback(err, tx){
|
|
|
|
|
|
|
|
|
|
if(q.killed || (!err && !tx)) //no error or tx data (duplicate calls will have empty tx value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(tx){
|
|
|
|
|
let txid = tx.txid();
|
|
|
|
|
tmpTxList[txid][1] = self._getOccurrenceCount(tx, address);
|
|
|
|
|
|
|
|
|
|
if(tmpTxList[txid][0] >= tmpTxList[txid][1]) //all duplications are found for this txid
|
|
|
|
|
delete tmpTxList[txid];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streamer(err, tx);
|
|
|
|
|
|
|
|
|
|
if(err){
|
|
|
|
|
if((err || options.flag_stop) && !q.killed){
|
|
|
|
|
|
|
|
|
|
q.kill();
|
|
|
|
|
q.killed = true;
|
|
|
|
|
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
const waterfall_array = [];
|
|
|
|
|
|
|
|
|
|
// query the mempool for relevant txs for this address
|
|
|
|
|
function(next) {
|
|
|
|
|
waterfall_array.push(
|
|
|
|
|
//Find start height if `after` option is passed
|
|
|
|
|
function parse_after_id(next){
|
|
|
|
|
|
|
|
|
|
if (!options.queryMempool) {
|
|
|
|
|
return next(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self._mempool.getTxidsByAddress(address, 'both', next);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// add the meta data such as input values, etc.
|
|
|
|
|
function(mempoolTxids, next) {
|
|
|
|
|
|
|
|
|
|
if (mempoolTxids.length <= 0) {
|
|
|
|
|
if(_.isUndefined(options.after)) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mempoolTxids.map(id => q.push(id, process_chunk));
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
// stream the rest of the confirmed txids out of the address index
|
|
|
|
|
function(next) {
|
|
|
|
|
self._transaction.getTransaction(options.after, options, function(err, tx) {
|
|
|
|
|
|
|
|
|
|
if(tx && tx.confirmations && tx.height >= options.start) {
|
|
|
|
|
|
|
|
|
|
options.start = tx.height;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
delete options.after;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waterfall_array.push(
|
|
|
|
|
//Find end height if `before` option is passed
|
|
|
|
|
function parse_before_id(next){
|
|
|
|
|
|
|
|
|
|
if(_.isUndefined(options.before)) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self._transaction.getTransaction(options.before, options, function(err, tx) {
|
|
|
|
|
|
|
|
|
|
if(tx && tx.confirmations && tx.height <= options.end) {
|
|
|
|
|
|
|
|
|
|
options.end = tx.height;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
delete options.before;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// stream the confirmed txids out of the address index
|
|
|
|
|
function query_confirmed_txids(next) {
|
|
|
|
|
|
|
|
|
|
if (options.mempoolOnly) { //Option to query from mempool only (ie, unconfirmed txs only)
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var txIdTransformStream = new Transform({ objectMode: true });
|
|
|
|
|
|
|
|
|
|
@ -790,12 +1160,16 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
txIdTransformStream.on('end', function() {
|
|
|
|
|
q.resume();
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
txIdTransformStream._transform = function(chunk, enc, cb) {
|
|
|
|
|
|
|
|
|
|
if(options.flag_stop)//stop data query
|
|
|
|
|
return txIdTransformStream.unpipe();
|
|
|
|
|
|
|
|
|
|
var txInfo = self._encoding.decodeAddressIndexKey(chunk);
|
|
|
|
|
q.push({ txid: txInfo.txid, height: txInfo.height }, process_chunk);
|
|
|
|
|
q.push({ txid: txInfo.txid, height: txInfo.height }, chunkCallback);
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
};
|
|
|
|
|
@ -803,10 +1177,48 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
|
|
|
|
|
var txidStream = self._getTxidStream(address, options);
|
|
|
|
|
txidStream.pipe(txIdTransformStream);
|
|
|
|
|
|
|
|
|
|
q.drain(next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// query the mempool for relevant txs for this address
|
|
|
|
|
function query_mempool_txids(next) {
|
|
|
|
|
|
|
|
|
|
if (!options.queryMempool || !_.isUndefined(options.before)) { //if queryMempool=false or options.before is given a valid value, then do not query mempool
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self._mempool.getTxidsByAddress(address, 'both', function(err, mempoolTxids) {
|
|
|
|
|
|
|
|
|
|
if (mempoolTxids.length <= 0) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mempoolTxids.map(id => q.push(id, chunkCallback));
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
|
|
|
|
|
if(options.reverse){ //when queried txs in reverse key order, mempool first then confirmed
|
|
|
|
|
waterfall_array.push(query_mempool_txids);
|
|
|
|
|
waterfall_array.push(query_confirmed_txids);
|
|
|
|
|
} else { //when queried tx in key order, confirmed tx 1st, then mempool
|
|
|
|
|
waterfall_array.push(query_confirmed_txids);
|
|
|
|
|
waterfall_array.push(query_mempool_txids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
waterfall_array.push(
|
|
|
|
|
//wait for queue to complete
|
|
|
|
|
function end_fall(next) {
|
|
|
|
|
|
|
|
|
|
if(!q.started || q.idle()) //No tx in query (or) already finished querying
|
|
|
|
|
return next();
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
q.drain = () => next();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async.waterfall(waterfall_array, callback);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|