Skip to content

Commit 937ffa0

Browse files
authored
fix(core-forger): log warning when active delegates are under the required delegate count (#3611)
1 parent 6e5fead commit 937ffa0

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

__tests__/unit/core-forger/delegate-tracker.test.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import { mockLastBlock, setup } from "./setup";
1212

1313
let delegateTracker: DelegateTracker;
1414
let loggerDebug: jest.SpyInstance;
15+
let loggerWarning: jest.SpyInstance;
1516
let activeDelegates;
1617

1718
beforeEach(async () => {
1819
activeDelegates = calculateActiveDelegates();
1920
const initialEnv = await setup(activeDelegates);
2021
delegateTracker = initialEnv.sandbox.app.resolve<DelegateTracker>(DelegateTracker);
2122
loggerDebug = initialEnv.spies.logger.debug;
23+
loggerWarning = initialEnv.spies.logger.warning;
2224
});
2325

2426
beforeEach(() => {
@@ -119,7 +121,7 @@ describe("DelegateTracker", () => {
119121
}
120122
});
121123

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

133-
/**
134-
* TODO: check this is desired behaviour
135-
* When there are less activeDelegates than required this behaves differently.
136-
* In this case, the first entry in nextDelegates is calculated as forging next (as opposed to the second delegate in the test above).
137-
* We also don't calculate (or log) the time until forging for any delegate.
138-
*/
139-
for (let i = 0; i < activeDelegates.length; i++) {
140-
const nextToForge = activeDelegates[i];
141-
if (i === 0) {
142-
expect(loggerDebug).toHaveBeenCalledWith(`${nextToForge.publicKey} will forge next.`);
143-
} else {
144-
expect(loggerDebug).toHaveBeenNthCalledWith(i + 2, `${nextToForge.publicKey} has already forged.`);
145-
}
146-
}
135+
expect(loggerWarning).toHaveBeenCalledWith(
136+
`Tracker only has ${activeDelegates.length} active delegates from a required ${mockMileStoneData.activeDelegates}`,
137+
);
147138
});
148139
});
149140
});

__tests__/unit/core-forger/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ export const setup = async activeDelegates => {
1515

1616
const error: jest.SpyInstance = jest.fn();
1717
const debug: jest.SpyInstance = jest.fn();
18+
const warning: jest.SpyInstance = jest.fn();
1819

1920
const logger = {
2021
error,
2122
debug,
23+
warning,
2224
};
2325

2426
sandbox.app.bind(Container.Identifiers.LogService).toConstantValue(logger);

packages/core-forger/src/delegate-tracker.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ export class DelegateTracker {
8787
}
8888
}
8989

90+
if (activeDelegates.length < delegatesCount) {
91+
return this.logger.warning(
92+
`Tracker only has ${Utils.pluralize(
93+
"active delegate",
94+
activeDelegates.length,
95+
true,
96+
)} from a required ${delegatesCount}`,
97+
);
98+
}
99+
90100
// Determine Next Forger Usernames...
91101
this.logger.debug(
92102
`Next Forgers: ${JSON.stringify(

0 commit comments

Comments
 (0)