diff --git a/lib/services/address/constants.js b/lib/services/address/constants.js index dc0b18b7..f11b3fe7 100644 --- a/lib/services/address/constants.js +++ b/lib/services/address/constants.js @@ -41,5 +41,11 @@ exports.SPACER_MAX = new Buffer('ff', 'hex'); // to cache the summary to disk. exports.SUMMARY_CACHE_THRESHOLD = 10000; + +// The default maximum length queries +exports.MAX_INPUTS_QUERY_LENGTH = 50000; +exports.MAX_OUTPUTS_QUERY_LENGTH = 50000; + + module.exports = exports; diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 665a32dc..1eeb0404 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -45,6 +45,8 @@ var AddressService = function(options) { this.node.services.bitcoind.on('txleave', this.transactionLeaveHandler.bind(this)); this.summaryCacheThreshold = options.summaryCacheThreshold || constants.SUMMARY_CACHE_THRESHOLD; + this.maxInputsQueryLength = options.maxInputsQueryLength || constants.MAX_INPUTS_QUERY_LENGTH; + this.maxOutputsQueryLength = options.maxOutputsQueryLength || constants.MAX_OUTPUTS_QUERY_LENGTH; this._setMempoolIndexPath(); this._setSummaryCachePath(); @@ -849,6 +851,12 @@ AddressService.prototype.getInputs = function(addressStr, options, callback) { stream.on('data', function(input) { inputs.push(input); + if (inputs.length > self.maxInputsQueryLength) { + log.warn('Tried to query too many inputs (' + self.maxInputsQueryLength + ') for address '+ addressStr); + error = new Error('Maximum number of inputs (' + self.maxInputsQueryLength + ') per query reached'); + stream.pause(); + stream.end(); + } }); var error; @@ -859,7 +867,7 @@ AddressService.prototype.getInputs = function(addressStr, options, callback) { } }); - stream.on('end', function() { + stream.on('finish', function() { if (error) { return callback(error); } @@ -1053,6 +1061,12 @@ AddressService.prototype.getOutputs = function(addressStr, options, callback) { stream.on('data', function(data) { outputs.push(data); + if (outputs.length > self.maxOutputsQueryLength) { + log.warn('Tried to query too many outputs (' + self.maxOutputsQueryLength + ') for address ' + addressStr); + error = new Error('Maximum number of outputs (' + self.maxOutputsQueryLength + ') per query reached'); + stream.pause(); + stream.end(); + } }); var error; @@ -1063,7 +1077,7 @@ AddressService.prototype.getOutputs = function(addressStr, options, callback) { } }); - stream.on('end', function() { + stream.on('finish', function() { if (error) { return callback(error); }