separate MAX_TX_QUERY_LIMIT for each query type

This commit is contained in:
sairajzero 2023-04-19 01:03:45 +05:30
parent 4afb0dfaaa
commit 8b07a1e4a5

View File

@ -16,7 +16,9 @@ var utils = require('../../utils');
var LRU = require('lru-cache'); var LRU = require('lru-cache');
var XXHash = require('xxhash'); 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 = 5000;
// See rationale about this cache at function getTxList(next) // See rationale about this cache at function getTxList(next)
const TXID_LIST_CACHE_ITEMS = 250; // nr of items (this translates to: consecutive const TXID_LIST_CACHE_ITEMS = 250; // nr of items (this translates to: consecutive
@ -198,7 +200,7 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
if( !_.isUndefined(options.from) || !_.isUndefined(options.to)) { if( !_.isUndefined(options.from) || !_.isUndefined(options.to)) {
old_support = true; old_support = true;
options.from = options.from || 0; options.from = options.from || 0;
options.to = options.to || 0xffffffff; //Max value of to will actually be MAX_TX_QUERY_LIMIT options.to = options.to || 0xffffffff; //Max value of to will actually be MAX_TX_QUERY_LIMIT_HISTORY
} }
if (_.isString(addresses)) { if (_.isString(addresses)) {
@ -224,15 +226,15 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream
if(!results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array if(!results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array
results.items.unshift(tx); //using unshift, so that recent tx (low) are at front results.items.unshift(tx); //using unshift, so that recent tx (low) are at front
if(results.items.length > MAX_TX_QUERY_LIMIT) { //remove items from array when overflown 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())); 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; let del_count = results.items.length - MAX_TX_QUERY_LIMIT_HISTORY;
let start_index = old_support ? MAX_TX_QUERY_LIMIT : 0; let start_index = old_support ? MAX_TX_QUERY_LIMIT_HISTORY : 0;
results.items.splice(start_index, del_count); results.items.splice(start_index, del_count);
results.incomplete = true; results.incomplete = true;
if(!old_support && addr_count >= MAX_TX_QUERY_LIMIT) if(!old_support && addr_count >= MAX_TX_QUERY_LIMIT_HISTORY)
addr_options.flag_stop = true; //limit has reached, stop quering db for more tx addr_options.flag_stop = true; //limit has reached, stop quering db for more tx
} }
@ -362,7 +364,7 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
result.lastItem = tx.txid(); result.lastItem = tx.txid();
} }
if(count >= MAX_TX_QUERY_LIMIT) {//stop quering db when limit reached if(count >= MAX_TX_QUERY_LIMIT_SUMMARY) {//stop quering db when limit reached
options.flag_stop = true; options.flag_stop = true;
result.incomplete = true; result.incomplete = true;
} }
@ -649,7 +651,7 @@ AddressService.prototype._aggregateAddressSummaryResult = function (tx, address,
result.transactions.unshift(txid); //using unshift, so that recent tx (low confirmation) are at front result.transactions.unshift(txid); //using unshift, so that recent tx (low confirmation) are at front
if(result.transactions.length > MAX_TX_QUERY_LIMIT) if(result.transactions.length > MAX_TX_QUERY_LIMIT_SUMMARY)
result.transactions.pop(); //pop the oldest tx in list (when list limit is maxed out) result.transactions.pop(); //pop the oldest tx in list (when list limit is maxed out)
} }
@ -740,7 +742,7 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
utxoStream.on('data', function(data) { 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'); utxoStream.emit('end');
return; return;
} }
@ -941,7 +943,7 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
txIdTransformStream._transform = function(chunk, enc, callback) { txIdTransformStream._transform = function(chunk, enc, callback) {
var txInfo = self._encoding.decodeAddressIndexKey(chunk); 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'); txIdTransformStream.emit('end');
return; return;
} }