From 730dd373184e1ce3f5b0d7552db6f2a75e152678 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 4 Sep 2015 11:39:38 -0400 Subject: [PATCH 1/2] Reindex logic - If the reindex option is set in bitcoin.conf, then when start is called and onBlocksReady's callback is fired: - start's callback will not be fired until the reindex takes place. - along the way the sync percentage is display once per second --- lib/services/bitcoind.js | 26 ++++++++++++- test/services/bitcoind.unit.js | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 30413eff..1880b285 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -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'); } @@ -69,6 +71,14 @@ 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. ' + + 'Once the reindex is complete, the rest of bitcore-node services will start. ' + + 'WARNING!!! Be sure to remove \'reindex=1\' from your bitcoin.conf -BEFORE- restarting bitcore-node.'); + this._reindex = true; + } + }; Bitcoin.prototype._onTipUpdate = function(result) { @@ -139,7 +149,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); + } }); }); }; diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 837fd97c..7d6d8fd4 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -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) { @@ -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() { From 18aff3de0cac61c9476da294b4e2455a176d59d9 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 4 Sep 2015 16:59:04 -0400 Subject: [PATCH 2/2] Fixed the warning message to the user on a reindex. --- lib/services/bitcoind.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 1880b285..9585f69d 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -74,8 +74,9 @@ Bitcoin.prototype._loadConfiguration = function() { if (this.configuration.reindex && this.configuration.reindex === 1) { log.warn('Reindex option is currently enabled. This means that bitcoind is undergoing a reindex. ' + - 'Once the reindex is complete, the rest of bitcore-node services will start. ' + - 'WARNING!!! Be sure to remove \'reindex=1\' from your bitcoin.conf -BEFORE- restarting bitcore-node.'); + '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; }