Reset Bcoin chain if blocks are missing from mongo.

This commit is contained in:
tenthirtyone 2017-08-24 14:42:50 -04:00
parent 2ccaf2fce0
commit 333eec3dd8
3 changed files with 9 additions and 7 deletions

View File

@ -12,9 +12,12 @@ db.connect(config.mongodb.uri, config.mongodb.options);
db.connection.once('open', () => {
// DB Audit returns best height to node
db.blocks.findMissingBlocks((err, lastBestHeight) => {
db.blocks.findMissingBlocks((err, bestBlockHeight) => {
// Pass height to node to start Sync
if (config.start_node) Bcoin.start(lastBestHeight);
logger.log('debug',
`Starting Bcoin from best height: ${bestBlockHeight}`);
if (config.start_node) Bcoin.start(bestBlockHeight);
Api.listen(config.api.port, () => {
logger.log('debug',

View File

@ -50,12 +50,11 @@ function findMissingBlocks(cb) {
// Blocks are in ascending order
let lastGoodHeight = 0;
blocks.forEach((block) => {
if (lastGoodHeight !== block.height - 1) {
return lastGoodHeight;
if (lastGoodHeight === block.height - 1) {
lastGoodHeight = block.height;
}
lastGoodHeight = block.height;
});
return lastGoodHeight;
return cb(null, lastGoodHeight);
});
}

View File

@ -14,7 +14,7 @@ function start(bestBlockHeight) {
.then(() => {
node.connect()
.then(() => {
node.reset(bestBlockHeight);
node.chain.reset(bestBlockHeight);
node.startSync();
});
});