Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/services/bitcoind.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function Bitcoin(options) {
return new Bitcoin(options);
}

this._reindex = false;
this._reindexWait = 1000;
Service.call(this, options);
$.checkState(this.node.datadir, 'Node is missing datadir property');
}
Expand Down Expand Up @@ -69,6 +71,15 @@ Bitcoin.prototype._loadConfiguration = function() {
'Please add "txindex=1" to your configuration and reindex an existing database if ' +
'necessary with reindex=1'
);

if (this.configuration.reindex && this.configuration.reindex === 1) {
log.warn('Reindex option is currently enabled. This means that bitcoind is undergoing a reindex. ' +
'The reindex flag will start the index from beginning every time the node is started, so it ' +
'should be removed after the reindex has been initiated. Once the reindex is complete, the rest ' +
'of bitcore-node services will start.');
this._reindex = true;
}

};

Bitcoin.prototype._onTipUpdate = function(result) {
Expand Down Expand Up @@ -139,7 +150,21 @@ Bitcoin.prototype.start = function(callback) {
if (err) {
return callback(err);
}
self._onReady(result, callback);
if (self._reindex) {
var interval = setInterval(function() {
var percentSynced = bindings.syncPercentage();
log.info("Bitcoin Core Daemon Reindex Percentage: " + percentSynced);
if (percentSynced >= 100) {
self._reindex = false;
self._onReady(result, callback);
clearInterval(interval);
}
}, self._reindexWait);

}
else {
self._onReady(result, callback);
}
});
});
};
Expand Down
69 changes: 69 additions & 0 deletions test/services/bitcoind.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ describe('Bitcoin Service', function() {
bitcoind._loadConfiguration({datadir: './test'});
}).should.throw('Txindex option');
});
describe('reindex', function() {
var log = require('../../lib/').log;
var stub;
beforeEach(function() {
stub = sinon.stub(log, 'warn');
});
after(function() {
stub.restore();
});
it('should warn the user if reindex is set to 1 in the bitcoin.conf file', function() {
var readFileSync = function() {
return "txindex=1\nreindex=1";
};
var testbitcoin = proxyquire('../../lib/services/bitcoind', {
fs: {
readFileSync: readFileSync,
existsSync: sinon.stub().returns(true)
},
mkdirp: {
sync: sinon.stub()
},
});
var bitcoind = new testbitcoin(baseConfig);
bitcoind._loadConfiguration();
stub.callCount.should.equal(1);
});
});
});
describe('#_registerEventHandlers', function() {
it('will emit tx with transactions from bindings', function(done) {
Expand Down Expand Up @@ -252,6 +279,48 @@ describe('Bitcoin Service', function() {
done();
});
});
describe('reindex', function() {
var log = require('../../lib/').log;
var info;
beforeEach(function() {
info = sinon.stub(log, 'info');
});
afterEach(function() {
info.restore();
});
it('will wait for a reindex to complete before calling the callback.', function(done) {
var start = sinon.stub().callsArgWith(1, null);
var onBlocksReady = sinon.stub().callsArg(0);
var percentage = 98;
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
fs: {
readFileSync: readFileSync
},
bindings: function(name) {
return {
start: start,
onBlocksReady: onBlocksReady,
syncPercentage: function() {
return percentage;
}
};
}
});
var bitcoind = new TestBitcoin(baseConfig);
bitcoind._reindex = true;
bitcoind._reindexWait = 1;
bitcoind._onReady = sinon.stub().callsArg(1);
bitcoind._loadConfiguration = sinon.stub();
bitcoind.start(function() {
info.callCount.should.be.within(2,3);
bitcoind._reindex.should.be.false;
done();
});
setTimeout(function() {
percentage = 100;
}, 2);
});
});
});
describe('#stop', function() {
it('will call bindings stop', function() {
Expand Down