Skip to content

Commit

Permalink
fix(core-forger): log warning when active delegates are under the req…
Browse files Browse the repository at this point in the history
…uired delegate count (#3611)
  • Loading branch information
bertiespell authored Mar 23, 2020
1 parent 6e5fead commit 937ffa0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
21 changes: 6 additions & 15 deletions __tests__/unit/core-forger/delegate-tracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { mockLastBlock, setup } from "./setup";

let delegateTracker: DelegateTracker;
let loggerDebug: jest.SpyInstance;
let loggerWarning: jest.SpyInstance;
let activeDelegates;

beforeEach(async () => {
activeDelegates = calculateActiveDelegates();
const initialEnv = await setup(activeDelegates);
delegateTracker = initialEnv.sandbox.app.resolve<DelegateTracker>(DelegateTracker);
loggerDebug = initialEnv.spies.logger.debug;
loggerWarning = initialEnv.spies.logger.warning;
});

beforeEach(() => {
Expand Down Expand Up @@ -119,7 +121,7 @@ describe("DelegateTracker", () => {
}
});

it("should handle cases where there are less active delegates than the required delegate count", async () => {
it("should log warning when there are less active delegates than the required delegate count", async () => {
const mockMileStoneData = {
blocktime: 2,
activeDelegates: 80,
Expand All @@ -130,20 +132,9 @@ describe("DelegateTracker", () => {
delegateTracker.initialize(activeDelegates);
await delegateTracker.handle();

/**
* TODO: check this is desired behaviour
* When there are less activeDelegates than required this behaves differently.
* In this case, the first entry in nextDelegates is calculated as forging next (as opposed to the second delegate in the test above).
* We also don't calculate (or log) the time until forging for any delegate.
*/
for (let i = 0; i < activeDelegates.length; i++) {
const nextToForge = activeDelegates[i];
if (i === 0) {
expect(loggerDebug).toHaveBeenCalledWith(`${nextToForge.publicKey} will forge next.`);
} else {
expect(loggerDebug).toHaveBeenNthCalledWith(i + 2, `${nextToForge.publicKey} has already forged.`);
}
}
expect(loggerWarning).toHaveBeenCalledWith(
`Tracker only has ${activeDelegates.length} active delegates from a required ${mockMileStoneData.activeDelegates}`,
);
});
});
});
2 changes: 2 additions & 0 deletions __tests__/unit/core-forger/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export const setup = async activeDelegates => {

const error: jest.SpyInstance = jest.fn();
const debug: jest.SpyInstance = jest.fn();
const warning: jest.SpyInstance = jest.fn();

const logger = {
error,
debug,
warning,
};

sandbox.app.bind(Container.Identifiers.LogService).toConstantValue(logger);
Expand Down
10 changes: 10 additions & 0 deletions packages/core-forger/src/delegate-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export class DelegateTracker {
}
}

if (activeDelegates.length < delegatesCount) {
return this.logger.warning(
`Tracker only has ${Utils.pluralize(
"active delegate",
activeDelegates.length,
true,
)} from a required ${delegatesCount}`,
);
}

// Determine Next Forger Usernames...
this.logger.debug(
`Next Forgers: ${JSON.stringify(
Expand Down

0 comments on commit 937ffa0

Please sign in to comment.