Skip to content

Commit 7fa7af2

Browse files
authored
fix(core-database): initializeActiveDelegates just use current round (#3827)
1 parent 8d64939 commit 7fa7af2

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

__tests__/unit/core-database/database-service.test.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,12 @@ describe("DatabaseService.restoreCurrentRound", () => {
308308
stateStore.getLastBlocksByHeight.mockReturnValueOnce(lastBlocksByHeight);
309309
blockRepository.findByHeightRangeWithTransactions.mockReturnValueOnce(lastBlocksByHeight);
310310

311-
const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn() };
311+
const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn(), revert: jest.fn() };
312312
getDposPreviousRoundState.mockReturnValueOnce(prevRoundState);
313313

314-
const prevRoundDelegateWallet = { getAttribute: jest.fn() };
315-
const prevRoundDposStateAllDelegates = [prevRoundDelegateWallet];
316-
prevRoundState.getAllDelegates.mockReturnValueOnce(prevRoundDposStateAllDelegates);
317-
318-
const prevRoundDelegateUsername = "test_delegate";
319-
prevRoundDelegateWallet.getAttribute.mockReturnValueOnce(prevRoundDelegateUsername);
320-
321314
const delegateWallet = { setAttribute: jest.fn(), getAttribute: jest.fn() };
322315
walletRepository.findByUsername.mockReturnValueOnce(delegateWallet);
323316

324-
const prevRoundDelegateRank = 1;
325-
prevRoundDelegateWallet.getAttribute.mockReturnValueOnce(prevRoundDelegateRank);
326-
327-
const prevRoundDposStateRoundDelegates = [prevRoundDelegateWallet];
328-
prevRoundState.getRoundDelegates.mockReturnValueOnce(prevRoundDposStateRoundDelegates);
329-
330317
const dposStateRoundDelegates = [delegateWallet];
331318
dposState.getRoundDelegates.mockReturnValueOnce(dposStateRoundDelegates);
332319
dposState.getRoundDelegates.mockReturnValueOnce(dposStateRoundDelegates);
@@ -336,9 +323,9 @@ describe("DatabaseService.restoreCurrentRound", () => {
336323

337324
await databaseService.restoreCurrentRound(1760000);
338325

339-
expect(getDposPreviousRoundState).toBeCalled();
340-
expect(walletRepository.findByUsername).toBeCalledWith(prevRoundDelegateUsername);
341-
expect(delegateWallet.setAttribute).toBeCalledWith("delegate.rank", prevRoundDelegateRank);
326+
expect(getDposPreviousRoundState).not.toBeCalled(); // restoring current round should not need previous round state
327+
// important: getActiveDelegates should be called with only roundInfo (restoreCurrentRound does *not* provide delegates to it)
328+
expect(triggers.call).toHaveBeenLastCalledWith("getActiveDelegates", { roundInfo: expect.anything(), delegates: undefined });
342329
expect(databaseService.forgingDelegates).toEqual(forgingDelegates);
343330
});
344331
});
@@ -985,8 +972,8 @@ describe("DatabaseService.revertRound", () => {
985972
stateStore.getLastBlocksByHeight.mockReturnValueOnce([lastBlock.data]);
986973
blockRepository.findByHeightRangeWithTransactions.mockReturnValueOnce([lastBlock.data]);
987974

988-
const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn() };
989-
getDposPreviousRoundState.mockReturnValueOnce(prevRoundState);
975+
const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn(), revert: jest.fn() };
976+
getDposPreviousRoundState.mockReturnValueOnce(prevRoundState).mockReturnValueOnce(prevRoundState);
990977

991978
const prevRoundDelegateWallet = { getAttribute: jest.fn() };
992979
const prevRoundDposStateAllDelegates = [prevRoundDelegateWallet];

packages/core-database/src/database-service.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,7 @@ export class DatabaseService {
744744
this.forgingDelegates = undefined;
745745

746746
const roundInfo: Contracts.Shared.RoundInfo = AppUtils.roundCalculator.calculateRound(height);
747-
const prevRoundDelegates: Contracts.State.Wallet[] = await this.calcPreviousActiveDelegates(roundInfo);
748-
749-
await this.setForgingDelegatesOfRound(roundInfo, prevRoundDelegates);
747+
await this.setForgingDelegatesOfRound(roundInfo);
750748
}
751749

752750
private async setForgingDelegatesOfRound(

0 commit comments

Comments
 (0)