Skip to content

Commit

Permalink
Address Service: Removed event listeners prior to stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Jan 18, 2016
1 parent 687400e commit af72d40
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/services/address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ var AddressService = function(options) {
this.subscriptions['address/transaction'] = {};
this.subscriptions['address/balance'] = {};

this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
this.node.services.bitcoind.on('txleave', this.transactionLeaveHandler.bind(this));
this._bitcoindTransactionListener = this.transactionHandler.bind(this);
this._bitcoindTransactionLeaveListener = this.transactionLeaveHandler.bind(this);
this.node.services.bitcoind.on('tx', this._bitcoindTransactionListener);
this.node.services.bitcoind.on('txleave', this._bitcoindTransactionLeaveListener);

this.maxInputsQueryLength = options.maxInputsQueryLength || constants.MAX_INPUTS_QUERY_LENGTH;
this.maxOutputsQueryLength = options.maxOutputsQueryLength || constants.MAX_OUTPUTS_QUERY_LENGTH;
Expand Down Expand Up @@ -103,6 +105,8 @@ AddressService.prototype.start = function(callback) {

AddressService.prototype.stop = function(callback) {
// TODO Keep track of ongoing db requests before shutting down
this.node.services.bitcoind.removeListener('tx', this._bitcoindTransactionListener);
this.node.services.bitcoind.removeListener('txleave', this._bitcoindTransactionLeaveListener);
this.mempoolIndex.close(callback);
};

Expand Down Expand Up @@ -227,6 +231,10 @@ AddressService.prototype.transactionLeaveHandler = function(txInfo) {
AddressService.prototype.transactionHandler = function(txInfo, callback) {
var self = this;

if (this.node.stopping) {
return callback();
}

// Basic transaction format is handled by the daemon
// and we can safely assume the buffer is properly formatted.
var tx = bitcore.Transaction().fromBuffer(txInfo.buffer);
Expand Down Expand Up @@ -760,11 +768,6 @@ AddressService.prototype.createInputsStream = function(addressStr, options) {
inputStream.end();
}).pipe(inputStream);


inputStream.on('end', function() {
stream.end();
});

return stream;

};
Expand Down Expand Up @@ -967,7 +970,6 @@ AddressService.prototype._getSpentMempool = function(txidBuffer, outputIndex, ca
};

AddressService.prototype.createOutputsStream = function(addressStr, options) {

var outputStream = new OutputsTransformStream({
address: new Address(addressStr, this.node.network),
tipHeight: this.node.services.db.tip.__height
Expand All @@ -981,10 +983,6 @@ AddressService.prototype.createOutputsStream = function(addressStr, options) {
})
.pipe(outputStream);

outputStream.on('end', function() {
stream.end();
});

return stream;

};
Expand Down

0 comments on commit af72d40

Please sign in to comment.