Address Service: Removed event listeners prior to stopping

This commit is contained in:
Braydon Fuller 2016-01-18 14:54:58 -05:00
parent 687400eab2
commit 62934b4b66
2 changed files with 23 additions and 13 deletions

View File

@ -41,8 +41,10 @@ var AddressService = function(options) {
this.subscriptions['address/transaction'] = {}; this.subscriptions['address/transaction'] = {};
this.subscriptions['address/balance'] = {}; this.subscriptions['address/balance'] = {};
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this)); this._bitcoindTransactionListener = this.transactionHandler.bind(this);
this.node.services.bitcoind.on('txleave', this.transactionLeaveHandler.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.maxInputsQueryLength = options.maxInputsQueryLength || constants.MAX_INPUTS_QUERY_LENGTH;
this.maxOutputsQueryLength = options.maxOutputsQueryLength || constants.MAX_OUTPUTS_QUERY_LENGTH; this.maxOutputsQueryLength = options.maxOutputsQueryLength || constants.MAX_OUTPUTS_QUERY_LENGTH;
@ -103,6 +105,8 @@ AddressService.prototype.start = function(callback) {
AddressService.prototype.stop = function(callback) { AddressService.prototype.stop = function(callback) {
// TODO Keep track of ongoing db requests before shutting down // 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); this.mempoolIndex.close(callback);
}; };
@ -227,6 +231,10 @@ AddressService.prototype.transactionLeaveHandler = function(txInfo) {
AddressService.prototype.transactionHandler = function(txInfo, callback) { AddressService.prototype.transactionHandler = function(txInfo, callback) {
var self = this; var self = this;
if (this.node.stopping) {
return callback();
}
// Basic transaction format is handled by the daemon // Basic transaction format is handled by the daemon
// and we can safely assume the buffer is properly formatted. // and we can safely assume the buffer is properly formatted.
var tx = bitcore.Transaction().fromBuffer(txInfo.buffer); var tx = bitcore.Transaction().fromBuffer(txInfo.buffer);
@ -760,11 +768,6 @@ AddressService.prototype.createInputsStream = function(addressStr, options) {
inputStream.end(); inputStream.end();
}).pipe(inputStream); }).pipe(inputStream);
inputStream.on('end', function() {
stream.end();
});
return stream; return stream;
}; };
@ -967,7 +970,6 @@ AddressService.prototype._getSpentMempool = function(txidBuffer, outputIndex, ca
}; };
AddressService.prototype.createOutputsStream = function(addressStr, options) { AddressService.prototype.createOutputsStream = function(addressStr, options) {
var outputStream = new OutputsTransformStream({ var outputStream = new OutputsTransformStream({
address: new Address(addressStr, this.node.network), address: new Address(addressStr, this.node.network),
tipHeight: this.node.services.db.tip.__height tipHeight: this.node.services.db.tip.__height
@ -981,10 +983,6 @@ AddressService.prototype.createOutputsStream = function(addressStr, options) {
}) })
.pipe(outputStream); .pipe(outputStream);
outputStream.on('end', function() {
stream.end();
});
return stream; return stream;
}; };

View File

@ -192,14 +192,26 @@ describe('Address Service', function() {
describe('#stop', function() { describe('#stop', function() {
it('will close mempool levelup', function(done) { 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({ var am = new AddressService({
mempoolMemoryIndex: true, mempoolMemoryIndex: true,
node: mocknode node: testnode
}); });
am.mempoolIndex = {}; am.mempoolIndex = {};
am.mempoolIndex.close = sinon.stub().callsArg(0); am.mempoolIndex.close = sinon.stub().callsArg(0);
am.stop(function() { am.stop(function() {
am.mempoolIndex.close.callCount.should.equal(1); am.mempoolIndex.close.callCount.should.equal(1);
am.node.services.bitcoind.removeListener.callCount.should.equal(2);
done(); done();
}); });
}); });