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 62934b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 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
14 changes: 13 additions & 1 deletion test/services/address/index.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,26 @@ describe('Address Service', function() {

describe('#stop', function() {
it('will close mempool levelup', function(done) {
var testnode = {
network: Networks.testnet,
datadir: 'testdir',
db: mockdb,
services: {
bitcoind: {
on: sinon.stub(),
removeListener: sinon.stub()
}
}
};
var am = new AddressService({
mempoolMemoryIndex: true,
node: mocknode
node: testnode
});
am.mempoolIndex = {};
am.mempoolIndex.close = sinon.stub().callsArg(0);
am.stop(function() {
am.mempoolIndex.close.callCount.should.equal(1);
am.node.services.bitcoind.removeListener.callCount.should.equal(2);
done();
});
});
Expand Down

0 comments on commit 62934b4

Please sign in to comment.