Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc(core-blockchain): log the reason for discarding a block #2903

Merged
merged 7 commits into from
Sep 6, 2019
Merged
10 changes: 7 additions & 3 deletions packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
State,
TransactionPool,
} from "@arkecosystem/core-interfaces";
import { Blocks, Crypto, Interfaces, Managers } from "@arkecosystem/crypto";
import { Blocks, Crypto, Interfaces, Managers, Utils } from "@arkecosystem/crypto";

import { isBlockChained, roundCalculator } from "@arkecosystem/core-utils";
import async from "async";
Expand Down Expand Up @@ -386,8 +386,12 @@ export class Blockchain implements blockchain.IBlockchain {
const acceptedBlocks: Interfaces.IBlock[] = [];
let lastProcessResult: BlockProcessorResult;

if (blocks[0] && !isBlockChained(this.getLastBlock().data, blocks[0].data)) {
this.clearQueue(); // Discard remaining blocks as it won't go anywhere anyway.
if (blocks[0] &&
!isBlockChained(this.getLastBlock().data, blocks[0].data, logger) &&
!Utils.isException(blocks[0].data)) {
// Discard remaining blocks as it won't go anywhere anyway.
this.clearQueue();
this.resetLastDownloadedBlock();
return callback();
}

Expand Down
32 changes: 30 additions & 2 deletions packages/core-utils/src/is-block-chained.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import { Logger } from "@arkecosystem/core-interfaces";
import { Crypto, Interfaces } from "@arkecosystem/crypto";

export const isBlockChained = (previousBlock: Interfaces.IBlockData, nextBlock: Interfaces.IBlockData): boolean => {
export const isBlockChained = (
previousBlock: Interfaces.IBlockData,
nextBlock: Interfaces.IBlockData,
logger?: Logger.ILogger): boolean => {

const followsPrevious: boolean = nextBlock.previousBlock === previousBlock.id;
const isPlusOne: boolean = nextBlock.height === previousBlock.height + 1;

const previousSlot: number = Crypto.Slots.getSlotNumber(previousBlock.timestamp);
const nextSlot: number = Crypto.Slots.getSlotNumber(nextBlock.timestamp);
const isAfterPreviousSlot: boolean = previousSlot < nextSlot;

return followsPrevious && isPlusOne && isAfterPreviousSlot;
const isChained: boolean = followsPrevious && isPlusOne && isAfterPreviousSlot;

if (logger && !isChained) {
const messagePrefix: string =
`Block { height: ${nextBlock.height}, id: ${nextBlock.id}, ` +
`previousBlock: ${nextBlock.previousBlock} } is not chained to the ` +
`previous block { height: ${previousBlock.height}, id: ${previousBlock.id} }`;

let messageDetail: string;

if (!followsPrevious) {
messageDetail = `previous block id mismatch`;
} else if (!isPlusOne) {
messageDetail = `height is not plus one`;
} else if (!isAfterPreviousSlot) {
messageDetail = `previous slot is not smaller: ` +
`${previousSlot} (derived from timestamp ${previousBlock.timestamp}) VS ` +
`${nextSlot} (derived from timestamp ${nextBlock.timestamp})`;
}

logger.warn(`${messagePrefix}: ${messageDetail}`);
}

return isChained;
};