Skip to content

Commit b4ed29e

Browse files
author
Braydon Fuller
committed
Merge pull request #193 from kleetus/reindex
Reindex logic
2 parents d405edc + 18aff3d commit b4ed29e

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

lib/services/bitcoind.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function Bitcoin(options) {
2222
return new Bitcoin(options);
2323
}
2424

25+
this._reindex = false;
26+
this._reindexWait = 1000;
2527
Service.call(this, options);
2628
$.checkState(this.node.datadir, 'Node is missing datadir property');
2729
}
@@ -69,6 +71,15 @@ Bitcoin.prototype._loadConfiguration = function() {
6971
'Please add "txindex=1" to your configuration and reindex an existing database if ' +
7072
'necessary with reindex=1'
7173
);
74+
75+
if (this.configuration.reindex && this.configuration.reindex === 1) {
76+
log.warn('Reindex option is currently enabled. This means that bitcoind is undergoing a reindex. ' +
77+
'The reindex flag will start the index from beginning every time the node is started, so it ' +
78+
'should be removed after the reindex has been initiated. Once the reindex is complete, the rest ' +
79+
'of bitcore-node services will start.');
80+
this._reindex = true;
81+
}
82+
7283
};
7384

7485
Bitcoin.prototype._onTipUpdate = function(result) {
@@ -139,7 +150,21 @@ Bitcoin.prototype.start = function(callback) {
139150
if (err) {
140151
return callback(err);
141152
}
142-
self._onReady(result, callback);
153+
if (self._reindex) {
154+
var interval = setInterval(function() {
155+
var percentSynced = bindings.syncPercentage();
156+
log.info("Bitcoin Core Daemon Reindex Percentage: " + percentSynced);
157+
if (percentSynced >= 100) {
158+
self._reindex = false;
159+
self._onReady(result, callback);
160+
clearInterval(interval);
161+
}
162+
}, self._reindexWait);
163+
164+
}
165+
else {
166+
self._onReady(result, callback);
167+
}
143168
});
144169
});
145170
};

test/services/bitcoind.unit.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ describe('Bitcoin Service', function() {
7575
bitcoind._loadConfiguration({datadir: './test'});
7676
}).should.throw('Txindex option');
7777
});
78+
describe('reindex', function() {
79+
var log = require('../../lib/').log;
80+
var stub;
81+
beforeEach(function() {
82+
stub = sinon.stub(log, 'warn');
83+
});
84+
after(function() {
85+
stub.restore();
86+
});
87+
it('should warn the user if reindex is set to 1 in the bitcoin.conf file', function() {
88+
var readFileSync = function() {
89+
return "txindex=1\nreindex=1";
90+
};
91+
var testbitcoin = proxyquire('../../lib/services/bitcoind', {
92+
fs: {
93+
readFileSync: readFileSync,
94+
existsSync: sinon.stub().returns(true)
95+
},
96+
mkdirp: {
97+
sync: sinon.stub()
98+
},
99+
});
100+
var bitcoind = new testbitcoin(baseConfig);
101+
bitcoind._loadConfiguration();
102+
stub.callCount.should.equal(1);
103+
});
104+
});
78105
});
79106
describe('#_registerEventHandlers', function() {
80107
it('will emit tx with transactions from bindings', function(done) {
@@ -252,6 +279,48 @@ describe('Bitcoin Service', function() {
252279
done();
253280
});
254281
});
282+
describe('reindex', function() {
283+
var log = require('../../lib/').log;
284+
var info;
285+
beforeEach(function() {
286+
info = sinon.stub(log, 'info');
287+
});
288+
afterEach(function() {
289+
info.restore();
290+
});
291+
it('will wait for a reindex to complete before calling the callback.', function(done) {
292+
var start = sinon.stub().callsArgWith(1, null);
293+
var onBlocksReady = sinon.stub().callsArg(0);
294+
var percentage = 98;
295+
var TestBitcoin = proxyquire('../../lib/services/bitcoind', {
296+
fs: {
297+
readFileSync: readFileSync
298+
},
299+
bindings: function(name) {
300+
return {
301+
start: start,
302+
onBlocksReady: onBlocksReady,
303+
syncPercentage: function() {
304+
return percentage;
305+
}
306+
};
307+
}
308+
});
309+
var bitcoind = new TestBitcoin(baseConfig);
310+
bitcoind._reindex = true;
311+
bitcoind._reindexWait = 1;
312+
bitcoind._onReady = sinon.stub().callsArg(1);
313+
bitcoind._loadConfiguration = sinon.stub();
314+
bitcoind.start(function() {
315+
info.callCount.should.be.within(2,3);
316+
bitcoind._reindex.should.be.false;
317+
done();
318+
});
319+
setTimeout(function() {
320+
percentage = 100;
321+
}, 2);
322+
});
323+
});
255324
});
256325
describe('#stop', function() {
257326
it('will call bindings stop', function() {

0 commit comments

Comments
 (0)