Skip missing blocks during block hash chain validation
This commit is contained in:
parent
e63aaedbc0
commit
9377faea9c
@ -397,18 +397,28 @@ class BlocksRepository {
|
|||||||
const [blocks]: any[] = await DB.query(`SELECT height, hash, previous_block_hash,
|
const [blocks]: any[] = await DB.query(`SELECT height, hash, previous_block_hash,
|
||||||
UNIX_TIMESTAMP(blockTimestamp) as timestamp FROM blocks ORDER BY height`);
|
UNIX_TIMESTAMP(blockTimestamp) as timestamp FROM blocks ORDER BY height`);
|
||||||
|
|
||||||
let currentHeight = 1;
|
let partialMsg = false;
|
||||||
while (currentHeight < blocks.length) {
|
let idx = 1;
|
||||||
if (blocks[currentHeight].previous_block_hash !== blocks[currentHeight - 1].hash) {
|
while (idx < blocks.length) {
|
||||||
logger.warn(`Chain divergence detected at block ${blocks[currentHeight - 1].height}, re-indexing newer blocks and hashrates`);
|
if (blocks[idx].height - 1 !== blocks[idx - 1].height) {
|
||||||
await this.$deleteBlocksFrom(blocks[currentHeight - 1].height);
|
if (partialMsg === false) {
|
||||||
await HashratesRepository.$deleteHashratesFromTimestamp(blocks[currentHeight - 1].timestamp - 604800);
|
logger.info('Some blocks are not indexed, skipping missing blocks during chain validation');
|
||||||
|
partialMsg = true;
|
||||||
|
}
|
||||||
|
++idx;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blocks[idx].previous_block_hash !== blocks[idx - 1].hash) {
|
||||||
|
logger.warn(`Chain divergence detected at block ${blocks[idx - 1].height}, re-indexing newer blocks and hashrates`);
|
||||||
|
await this.$deleteBlocksFrom(blocks[idx - 1].height);
|
||||||
|
await HashratesRepository.$deleteHashratesFromTimestamp(blocks[idx - 1].timestamp - 604800);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
++currentHeight;
|
++idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`${currentHeight} blocks hash validated in ${new Date().getTime() - start} ms`);
|
logger.info(`${idx} blocks hash validated in ${new Date().getTime() - start} ms`);
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.err('Cannot validate chain of block hash. Reason: ' + (e instanceof Error ? e.message : e));
|
logger.err('Cannot validate chain of block hash. Reason: ' + (e instanceof Error ? e.message : e));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user