Address Service: Limit the length of outputs that can be queried at a time

This commit is contained in:
Braydon Fuller 2016-01-05 16:47:29 -05:00
parent 40eb4f50ae
commit cef2f7686d
2 changed files with 22 additions and 2 deletions

View File

@ -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;

View File

@ -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);
}