Merge pull request #193 from kleetus/reindex

Reindex logic
This commit is contained in:
Braydon Fuller 2015-09-08 10:15:34 -04:00
commit b4ed29eabe
2 changed files with 95 additions and 1 deletions

View File

@ -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,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) {
@ -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);
}
});
});
};

View File

@ -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() {