Improvements

This commit is contained in:
sairajzero 2023-02-05 03:14:05 +05:30
parent 729072738b
commit c712b17465

View File

@ -177,8 +177,8 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
var self = this; var self = this;
options = options || {}; options = options || {};
options.from = options.from || 0; //options.from = options.from || 0; //Deprecated, use `after` option
options.to = options.to || 0xffffffff; //options.to = options.to || 0xffffffff; //Deprecated, use `after` option
if(typeof callback !== 'function'){ //if only 3 args, then streamer is callback if(typeof callback !== 'function'){ //if only 3 args, then streamer is callback
callback = streamer; callback = streamer;
@ -207,8 +207,10 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
if(!options.txNotNeeded) { if(!options.txNotNeeded) {
results.totalCount++; results.totalCount++;
if(results.items.length < MAX_TX_QUERY_LIMIT && !results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array if(results.items.length < MAX_TX_QUERY_LIMIT && !results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array
results.items.push(tx); results.items.unshift(tx); //using unshift, so that recent tx (low) are at front
} }
streamer(null, tx); streamer(null, tx);
@ -226,6 +228,13 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
results.items.sort((a, b) => b.__height - a.__height || a.txid().localeCompare(b.txid())); results.items.sort((a, b) => b.__height - a.__height || a.txid().localeCompare(b.txid()));
results.totalCount = parseInt(results.totalCount.toFixed()); results.totalCount = parseInt(results.totalCount.toFixed());
//Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use)
if( !_.isUndefined(options.from) || !_.isUndefined(options.to)) {
options.from = options.from || 0;
options.to = options.to || 0xffffffff; //Max value of to will actually be MAX_TX_QUERY_LIMIT
results.items = results.items.slice(options.from, options.to);
}
callback(null, results); callback(null, results);
}) })
@ -259,7 +268,7 @@ AddressService.prototype.__getAddressSummary = function(address, options, callba
txApperances: 0, txApperances: 0,
}; };
self.getAddressHistory(address, options, function(err, results) { self.__getAddressHistory(address, options, function(err, results) { //old fn
if (err) { if (err) {
return callback(err); return callback(err);
@ -282,8 +291,8 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
var self = this; var self = this;
options = options || {}; options = options || {};
options.from = options.from || 0; //options.from = options.from || 0; //Deprecated
options.to = options.to || 0xffffffff; //options.to = options.to || 0xffffffff; //Deprecated
options.txNotNeeded = true; //no need to store tx details in result options.txNotNeeded = true; //no need to store tx details in result
if (_.isUndefined(options.queryMempool)) { if (_.isUndefined(options.queryMempool)) {
@ -466,7 +475,7 @@ AddressService.prototype._getInputResults = function(tx, address) {
}; };
AddressService.prototype._aggregateAddressSummaryResult = function (tx, address, result, options){ AddressService.prototype._aggregateAddressSummaryResult = function (tx, address, result, options) {
var self = this; var self = this;
@ -489,12 +498,21 @@ AddressService.prototype._aggregateAddressSummaryResult = function (tx, address,
} }
if (!options.noTxList) { if (!options.noTxList) {
if (!result.transactions) { if (!result.transactions) {
result.transactions = []; result.transactions = [];
} }
let txid = tx.txid(); let txid = tx.txid();
if(!result.transactions.includes(txid) && result.transactions.length < MAX_TX_QUERY_LIMIT) //push txid only if its not in the array (list limit not maxed out) if(!result.transactions.includes(txid)) { //push txid only if its not in the array
result.transactions.push(txid);
result.transactions.unshift(txid); //using unshift, so that recent tx (low confirmation) are at front
if(result.transactions.length > MAX_TX_QUERY_LIMIT)
result.transactions.pop(); //pop the oldest tx in list (when list limit is maxed out)
}
} }
} }
@ -679,9 +697,10 @@ AddressService.prototype._getTxidStream = function(address, options) {
var criteria = { var criteria = {
gte: start, gte: start,
lte: end, lte: end
reverse: true // txids stream from low confirmations to high confirmations //reverse: true // txids stream from low confirmations to high confirmations
}; };
//NOTE: commentted reverse to keep the order in asc when reading to preserve continuity when using `after` option
// txid stream // txid stream
var txidStream = this._db.createKeyStream(criteria); var txidStream = this._db.createKeyStream(criteria);
@ -693,6 +712,7 @@ AddressService.prototype._getTxidStream = function(address, options) {
return txidStream; return txidStream;
}; };
//(used by old fn)
AddressService.prototype._getAddressTxHistory = function(options, callback) { AddressService.prototype._getAddressTxHistory = function(options, callback) {
var self = this; var self = this;
@ -721,6 +741,7 @@ AddressService.prototype._getAddressTxHistory = function(options, callback) {
}; };
//(used by old fn)
AddressService.prototype._getAddressTxidHistory = function(address, options, callback) { AddressService.prototype._getAddressTxidHistory = function(address, options, callback) {
var self = this; var self = this;
@ -784,7 +805,9 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
return; 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(); callback();
}; };
@ -803,8 +826,8 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
options.start = options.start || 0; options.start = options.start || 0;
options.end = options.end || 0xffffffff; options.end = options.end || 0xffffffff;
options.from = options.from || 0; //TODO: check from/to options are working or not //options.from = options.from || 0; //Deprecated, use `after` option
options.to = options.to || 0xffffffff; //options.to = options.to || 0xffffffff; //Deprecated, use `after` option
if (_.isUndefined(options.queryMempool)) { if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true; options.queryMempool = true;
@ -877,34 +900,14 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
async.waterfall([ async.waterfall([
// query the mempool for relevant txs for this address //Find start height if `after` option is passed
function(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) {
return next();
}
mempoolTxids.map(id => q.push(id, chunkCallback));
next();
},
function(next){ function(next){
if(_.isUndefined(options.after)) { if(_.isUndefined(options.after)) {
return next(); return next();
} }
self._transaction.getTransaction(id.txid, options, function(err, tx) { self._transaction.getTransaction(options.after, options, function(err, tx) {
if(tx && tx.confirmations && tx.height > options.start) { if(tx && tx.confirmations && tx.height > options.start) {
@ -959,7 +962,29 @@ AddressService.prototype._streamAddressSummary = function(address, options, stre
var txidStream = self._getTxidStream(address, options); var txidStream = self._getTxidStream(address, options);
txidStream.pipe(txIdTransformStream); txidStream.pipe(txIdTransformStream);
} },
// query the mempool for relevant txs for this address
function(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) {
return next();
}
mempoolTxids.map(id => q.push(id, chunkCallback));
next();
},
], callback); ], callback);
} }