only pass db as option to module because other params aren't available until init

This commit is contained in:
Patrick Nagurny 2015-07-23 13:13:06 -06:00
parent 1aee45e423
commit 8290d61a09
5 changed files with 12 additions and 20 deletions

View File

@ -222,9 +222,7 @@ DB.prototype.getAPIMethods = function() {
DB.prototype.addModule = function(Module) { DB.prototype.addModule = function(Module) {
var module = new Module({ var module = new Module({
db: this, db: this
bitcoind: this.bitcoind,
network: this.network
}); });
$.checkArgumentType(module, BaseModule); $.checkArgumentType(module, BaseModule);
this.modules.push(module); this.modules.push(module);

View File

@ -2,8 +2,6 @@
var Module = function(options) { var Module = function(options) {
this.db = options.db; this.db = options.db;
this.bitcoind = options.bitcoind;
this.network = options.network;
}; };
Module.prototype.blockHandler = function(block, add, callback) { Module.prototype.blockHandler = function(block, add, callback) {

View File

@ -67,15 +67,15 @@ AddressModule.prototype.blockHandler = function(block, addOutput, callback) {
if(script.isPublicKeyOut()) { if(script.isPublicKeyOut()) {
var pubkey = script.chunks[0].buf; var pubkey = script.chunks[0].buf;
address = Address.fromPublicKey(new PublicKey(pubkey), this.network); address = Address.fromPublicKey(new PublicKey(pubkey), this.db.network);
} else { } else {
address = output.script.toAddress(this.network); address = output.script.toAddress(this.db.network);
} }
var outputIndex = j; var outputIndex = j;
var timestamp = block.timestamp.getTime(); var timestamp = block.timestamp.getTime();
var height = block.height; var height = block.__height;
operations.push({ operations.push({
type: action, type: action,
@ -156,7 +156,7 @@ AddressModule.prototype.getOutputs = function(address, queryMempool, callback) {
} }
if(queryMempool) { if(queryMempool) {
outputs = outputs.concat(self.bitcoind.getMempoolOutputs(address)); outputs = outputs.concat(self.db.bitcoind.getMempoolOutputs(address));
} }
callback(null, outputs); callback(null, outputs);
@ -198,7 +198,7 @@ AddressModule.prototype.isSpent = function(output, queryMempool, callback) {
var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid; var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid;
setImmediate(function() { setImmediate(function() {
callback(self.bitcoind.isSpent(txid, output.outputIndex)); callback(self.db.bitcoind.isSpent(txid, output.outputIndex));
}); });
}; };

View File

@ -301,15 +301,11 @@ describe('Bitcoin DB', function() {
inherits(Module1, BaseModule); inherits(Module1, BaseModule);
var db = new DB({store: memdown}); var db = new DB({store: memdown});
db.bitcoind = {};
db.network = {};
db.modules = []; db.modules = [];
db.addModule(Module1); db.addModule(Module1);
db.modules.length.should.equal(1); db.modules.length.should.equal(1);
should.exist(db.modules[0].db); 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() { it('should throw an error if module is not an instance of BaseModule', function() {

View File

@ -77,7 +77,7 @@ describe('AddressModule', function() {
var value64 = data[2].value; var value64 = data[2].value;
it('should create the correct operations when updating/adding outputs', function(done) { 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); should.not.exist(err);
operations.length.should.equal(11); operations.length.should.equal(11);
operations[0].type.should.equal('put'); operations[0].type.should.equal('put');
@ -88,7 +88,7 @@ describe('AddressModule', function() {
}); });
}); });
it('should create the correct operations when removing outputs', function(done) { 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); should.not.exist(err);
operations.length.should.equal(11); operations.length.should.equal(11);
operations[0].type.should.equal('del'); operations[0].type.should.equal('del');
@ -118,7 +118,7 @@ describe('AddressModule', function() {
var am = new AddressModule({db: db, network: 'livenet'}); 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); should.not.exist(err);
operations.length.should.equal(0); operations.length.should.equal(0);
done(); done();
@ -170,7 +170,7 @@ describe('AddressModule', function() {
blockHeight: 352532 blockHeight: 352532
} }
]; ];
am.bitcoind = { am.db.bitcoind = {
getMempoolOutputs: sinon.stub().returns(mempoolOutputs) getMempoolOutputs: sinon.stub().returns(mempoolOutputs)
}; };
@ -313,8 +313,8 @@ describe('AddressModule', function() {
}); });
describe('#isSpent', function() { describe('#isSpent', function() {
var am = new AddressModule({}); var am = new AddressModule({db: {}});
am.bitcoind = { am.db.bitcoind = {
isSpent: sinon.stub().returns(true) isSpent: sinon.stub().returns(true)
}; };