From 8290d61a09a09a87dfc3a73c66251c2347bb9caa Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Thu, 23 Jul 2015 13:13:06 -0600 Subject: [PATCH] only pass db as option to module because other params aren't available until init --- lib/db.js | 4 +--- lib/module.js | 2 -- lib/modules/address.js | 10 +++++----- test/db.unit.js | 4 ---- test/modules/address.unit.js | 12 ++++++------ 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/db.js b/lib/db.js index 4f6ee98a..14983950 100644 --- a/lib/db.js +++ b/lib/db.js @@ -222,9 +222,7 @@ DB.prototype.getAPIMethods = function() { DB.prototype.addModule = function(Module) { var module = new Module({ - db: this, - bitcoind: this.bitcoind, - network: this.network + db: this }); $.checkArgumentType(module, BaseModule); this.modules.push(module); diff --git a/lib/module.js b/lib/module.js index 896f8a95..9624e96c 100644 --- a/lib/module.js +++ b/lib/module.js @@ -2,8 +2,6 @@ var Module = function(options) { this.db = options.db; - this.bitcoind = options.bitcoind; - this.network = options.network; }; Module.prototype.blockHandler = function(block, add, callback) { diff --git a/lib/modules/address.js b/lib/modules/address.js index 1c77b761..4b21e05b 100644 --- a/lib/modules/address.js +++ b/lib/modules/address.js @@ -67,15 +67,15 @@ AddressModule.prototype.blockHandler = function(block, addOutput, callback) { if(script.isPublicKeyOut()) { var pubkey = script.chunks[0].buf; - address = Address.fromPublicKey(new PublicKey(pubkey), this.network); + address = Address.fromPublicKey(new PublicKey(pubkey), this.db.network); } else { - address = output.script.toAddress(this.network); + address = output.script.toAddress(this.db.network); } var outputIndex = j; var timestamp = block.timestamp.getTime(); - var height = block.height; + var height = block.__height; operations.push({ type: action, @@ -156,7 +156,7 @@ AddressModule.prototype.getOutputs = function(address, queryMempool, callback) { } if(queryMempool) { - outputs = outputs.concat(self.bitcoind.getMempoolOutputs(address)); + outputs = outputs.concat(self.db.bitcoind.getMempoolOutputs(address)); } callback(null, outputs); @@ -198,7 +198,7 @@ AddressModule.prototype.isSpent = function(output, queryMempool, callback) { var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid; setImmediate(function() { - callback(self.bitcoind.isSpent(txid, output.outputIndex)); + callback(self.db.bitcoind.isSpent(txid, output.outputIndex)); }); }; diff --git a/test/db.unit.js b/test/db.unit.js index c74ca0b2..96a75339 100644 --- a/test/db.unit.js +++ b/test/db.unit.js @@ -301,15 +301,11 @@ describe('Bitcoin DB', function() { inherits(Module1, BaseModule); var db = new DB({store: memdown}); - db.bitcoind = {}; - db.network = {}; db.modules = []; db.addModule(Module1); db.modules.length.should.equal(1); should.exist(db.modules[0].db); - should.exist(db.modules[0].bitcoind); - should.exist(db.modules[0].network); }); it('should throw an error if module is not an instance of BaseModule', function() { diff --git a/test/modules/address.unit.js b/test/modules/address.unit.js index d0fb446d..d5c10499 100644 --- a/test/modules/address.unit.js +++ b/test/modules/address.unit.js @@ -77,7 +77,7 @@ describe('AddressModule', function() { var value64 = data[2].value; it('should create the correct operations when updating/adding outputs', function(done) { - am.blockHandler({height: 345003, timestamp: new Date(1424836934000)}, true, function(err, operations) { + am.blockHandler({__height: 345003, timestamp: new Date(1424836934000)}, true, function(err, operations) { should.not.exist(err); operations.length.should.equal(11); operations[0].type.should.equal('put'); @@ -88,7 +88,7 @@ describe('AddressModule', function() { }); }); it('should create the correct operations when removing outputs', function(done) { - am.blockHandler({height: 345003, timestamp: new Date(1424836934000)}, false, function(err, operations) { + am.blockHandler({__height: 345003, timestamp: new Date(1424836934000)}, false, function(err, operations) { should.not.exist(err); operations.length.should.equal(11); operations[0].type.should.equal('del'); @@ -118,7 +118,7 @@ describe('AddressModule', function() { var am = new AddressModule({db: db, network: 'livenet'}); - am.blockHandler({height: 345003, timestamp: new Date(1424836934000)}, false, function(err, operations) { + am.blockHandler({__height: 345003, timestamp: new Date(1424836934000)}, false, function(err, operations) { should.not.exist(err); operations.length.should.equal(0); done(); @@ -170,7 +170,7 @@ describe('AddressModule', function() { blockHeight: 352532 } ]; - am.bitcoind = { + am.db.bitcoind = { getMempoolOutputs: sinon.stub().returns(mempoolOutputs) }; @@ -313,8 +313,8 @@ describe('AddressModule', function() { }); describe('#isSpent', function() { - var am = new AddressModule({}); - am.bitcoind = { + var am = new AddressModule({db: {}}); + am.db.bitcoind = { isSpent: sinon.stub().returns(true) };