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
This commit is contained in:
parent
290874a8fb
commit
730dd37318
|
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue