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

fix(core-forger): also check if still in same slot #2792

Merged
merged 5 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
vasild marked this conversation as resolved.
Show resolved Hide resolved
});

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