Skip to content

Commit

Permalink
fix(core-forger): also check if still in same slot (#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed Jul 10, 2019
1 parent 766870e commit ec68d39
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
18 changes: 16 additions & 2 deletions __tests__/unit/core-forger/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Forger Manager", () => {
const del = new Delegate("a secret", testnet.network);
const round = {
lastBlock: { id: sampleBlock.data.id, height: sampleBlock.data.height },
timestamp: 1,
timestamp: Crypto.Slots.getTime(),
reward: 2 * 1e8,
};

Expand Down Expand Up @@ -92,7 +92,7 @@ describe("Forger Manager", () => {
const del = new Delegate("a secret", testnet.network);
const round = {
lastBlock: { id: sampleBlock.data.id, height: sampleBlock.data.height },
timestamp: 1,
timestamp: Crypto.Slots.getTime(),
reward: 2 * 1e8,
};

Expand All @@ -115,6 +115,20 @@ describe("Forger Manager", () => {
});

expect(forgeManager.client.broadcastBlock).not.toHaveBeenCalled();

jest.resetAllMocks();

jest.spyOn(Crypto.Slots, "getTimeInMsUntilNextSlot").mockReturnValueOnce(2000);
jest.spyOn(Crypto.Slots, "getSlotNumber").mockImplementation(timestamp => {
return timestamp === undefined ? 1 : 2;
});

await forgeManager.forgeNewBlock(del, round, {
lastBlockId: round.lastBlock.id,
nodeHeight: round.lastBlock.height,
});

expect(forgeManager.client.broadcastBlock).not.toHaveBeenCalled();
});
});

Expand Down
24 changes: 13 additions & 11 deletions packages/core-forger/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ export class ForgerManager {

const minimumMs: number = 2000;
const timeLeftInMs: number = Crypto.Slots.getTimeInMsUntilNextSlot();
if (timeLeftInMs >= minimumMs) {
this.logger.info(
`Forged new block ${block.data.id} by delegate ${this.usernames[delegate.publicKey]} (${
delegate.publicKey
})`,
);
const currentSlot: number = Crypto.Slots.getSlotNumber();
const roundSlot: number = Crypto.Slots.getSlotNumber(round.timestamp);
const prettyName: string = `${this.usernames[delegate.publicKey]} (${delegate.publicKey})`;

if (timeLeftInMs >= minimumMs && currentSlot === roundSlot) {
this.logger.info(`Forged new block ${block.data.id} by delegate ${prettyName}`);

await this.client.broadcastBlock(block.toJson());

Expand All @@ -170,11 +170,13 @@ export class ForgerManager {
this.client.emitEvent(ApplicationEvents.TransactionForged, transaction);
}
} else {
this.logger.warn(
`Failed to forge new block by delegate ${this.usernames[delegate.publicKey]} (${
delegate.publicKey
}, because there were less than ${minimumMs}ms left`,
);
if (currentSlot !== roundSlot) {
this.logger.warn(`Failed to forge new block by delegate ${prettyName}, because already in next slot.`);
} else {
this.logger.warn(
`Failed to forge new block by delegate ${prettyName}, because there were ${timeLeftInMs}ms left in the current slot (less than ${minimumMs}ms).`,
);
}
}
}

Expand Down

0 comments on commit ec68d39

Please sign in to comment.