diff --git a/lib/services/db.js b/lib/services/db.js index 042c7bab..732e4fc5 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -366,15 +366,22 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) { async.eachSeries( this.node.services, function(mod, next) { - mod.blockHandler.call(mod, block, add, function(err, ops) { - if (err) { - return next(err); - } - if (ops) { - operations = operations.concat(ops); - } - next(); - }); + if(mod.blockHandler) { + $.checkArgument(typeof mod.blockHandler === 'function', 'blockHandler must be a function'); + + mod.blockHandler.call(mod, block, add, function(err, ops) { + if (err) { + return next(err); + } + if (ops) { + $.checkArgument(Array.isArray(ops), 'blockHandler for ' + mod.name + ' returned non-array'); + operations = operations.concat(ops); + } + next(); + }); + } else { + setImmediate(next); + } }, function(err) { if (err) { diff --git a/test/services/db.unit.js b/test/services/db.unit.js index a2293df4..867f49cb 100644 --- a/test/services/db.unit.js +++ b/test/services/db.unit.js @@ -630,6 +630,9 @@ describe('DB Service', function() { Service1.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op1', 'op2', 'op3']); var Service2 = function() {}; Service2.prototype.blockHandler = sinon.stub().callsArgWith(2, null, ['op4', 'op5']); + var Service3 = function() {}; + var Service4 = function() {}; + Service4.prototype.blockHandler = sinon.stub().callsArgWith(2, null, 'bad-value'); db.node = {}; db.node.services = { service1: new Service1(), @@ -657,6 +660,31 @@ describe('DB Service', function() { done(); }); }); + + it('should not give an error if a service does not have blockHandler', function(done) { + db.node = {}; + db.node.services = { + service3: new Service3() + }; + + db.runAllBlockHandlers('block', true, function(err) { + should.not.exist(err); + done(); + }); + }); + + it('should throw an error if blockHandler gives unexpected result', function() { + db.node = {}; + db.node.services = { + service4: new Service4() + }; + + (function() { + db.runAllBlockHandlers('block', true, function(err) { + should.not.exist(err); + }); + }).should.throw('bitcore.ErrorInvalidArgument'); + }); }); describe('#getAPIMethods', function() {