Skip to content

Commit

Permalink
fix(core-database): initializeActiveDelegates just use current round (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one authored Jun 25, 2020
1 parent 8d64939 commit 7fa7af2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
25 changes: 6 additions & 19 deletions __tests__/unit/core-database/database-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,25 +308,12 @@ describe("DatabaseService.restoreCurrentRound", () => {
stateStore.getLastBlocksByHeight.mockReturnValueOnce(lastBlocksByHeight);
blockRepository.findByHeightRangeWithTransactions.mockReturnValueOnce(lastBlocksByHeight);

const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn() };
const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn(), revert: jest.fn() };
getDposPreviousRoundState.mockReturnValueOnce(prevRoundState);

const prevRoundDelegateWallet = { getAttribute: jest.fn() };
const prevRoundDposStateAllDelegates = [prevRoundDelegateWallet];
prevRoundState.getAllDelegates.mockReturnValueOnce(prevRoundDposStateAllDelegates);

const prevRoundDelegateUsername = "test_delegate";
prevRoundDelegateWallet.getAttribute.mockReturnValueOnce(prevRoundDelegateUsername);

const delegateWallet = { setAttribute: jest.fn(), getAttribute: jest.fn() };
walletRepository.findByUsername.mockReturnValueOnce(delegateWallet);

const prevRoundDelegateRank = 1;
prevRoundDelegateWallet.getAttribute.mockReturnValueOnce(prevRoundDelegateRank);

const prevRoundDposStateRoundDelegates = [prevRoundDelegateWallet];
prevRoundState.getRoundDelegates.mockReturnValueOnce(prevRoundDposStateRoundDelegates);

const dposStateRoundDelegates = [delegateWallet];
dposState.getRoundDelegates.mockReturnValueOnce(dposStateRoundDelegates);
dposState.getRoundDelegates.mockReturnValueOnce(dposStateRoundDelegates);
Expand All @@ -336,9 +323,9 @@ describe("DatabaseService.restoreCurrentRound", () => {

await databaseService.restoreCurrentRound(1760000);

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

const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn() };
getDposPreviousRoundState.mockReturnValueOnce(prevRoundState);
const prevRoundState = { getAllDelegates: jest.fn(), getRoundDelegates: jest.fn(), revert: jest.fn() };
getDposPreviousRoundState.mockReturnValueOnce(prevRoundState).mockReturnValueOnce(prevRoundState);

const prevRoundDelegateWallet = { getAttribute: jest.fn() };
const prevRoundDposStateAllDelegates = [prevRoundDelegateWallet];
Expand Down
4 changes: 1 addition & 3 deletions packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,7 @@ export class DatabaseService {
this.forgingDelegates = undefined;

const roundInfo: Contracts.Shared.RoundInfo = AppUtils.roundCalculator.calculateRound(height);
const prevRoundDelegates: Contracts.State.Wallet[] = await this.calcPreviousActiveDelegates(roundInfo);

await this.setForgingDelegatesOfRound(roundInfo, prevRoundDelegates);
await this.setForgingDelegatesOfRound(roundInfo);
}

private async setForgingDelegatesOfRound(
Expand Down

0 comments on commit 7fa7af2

Please sign in to comment.