From d2cfe932665caa256ce41c3f4c35f4ce468472ca Mon Sep 17 00:00:00 2001 From: Neeharika-Sompalli Date: Fri, 10 Jan 2025 10:27:37 -0600 Subject: [PATCH] fix unit tests Signed-off-by: Neeharika-Sompalli --- .../steps/PlatformStateUpdatesTest.java | 10 +++- .../EndOfStakingPeriodUpdaterTest.java | 54 +++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/workflows/handle/steps/PlatformStateUpdatesTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/workflows/handle/steps/PlatformStateUpdatesTest.java index a4ef34e8e6f..4fc78b65983 100644 --- a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/workflows/handle/steps/PlatformStateUpdatesTest.java +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/workflows/handle/steps/PlatformStateUpdatesTest.java @@ -315,8 +315,14 @@ void updatesCandidateRosterWeightsWhenNotExportingAndRosterLifecycleEnabled() { StakingNodeInfo.newBuilder().stake(1000).weight(1).build()); subject.handleTxBody(state, txBody.build(), configWith(true, true, false)); - - final var candidateRoster = rosters.get(rosterStateBackingStore.get().candidateRosterHash()); + final var candidateRosterHash = state.getWritableStates(RosterService.NAME) + .getSingleton("ROSTER_STATE") + .get() + .candidateRosterHash(); + final var candidateRoster = state.getWritableStates(RosterService.NAME) + .get("ROSTERS") + .get(new ProtoBytes(candidateRosterHash)); + assertEquals(candidateRoster.rosterEntries().size(), 2); assertEquals(candidateRoster.rosterEntries().get(0).weight(), 1000); assertEquals(candidateRoster.rosterEntries().get(1).weight(), 0); } diff --git a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/staking/EndOfStakingPeriodUpdaterTest.java b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/staking/EndOfStakingPeriodUpdaterTest.java index 510dee2fdc1..86ffbdbd2f7 100644 --- a/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/staking/EndOfStakingPeriodUpdaterTest.java +++ b/hedera-node/hedera-token-service-impl/src/test/java/com/hedera/node/app/service/token/impl/test/handlers/staking/EndOfStakingPeriodUpdaterTest.java @@ -328,6 +328,48 @@ void calculatesNewEndOfPeriodStakingFieldsAsExpected() { .isLessThanOrEqualTo(SUM_OF_CONSENSUS_WEIGHTS); } + @Test + void doesntUpdateWeightWhenUseRosterLifecycleIsTrue() { + commonSetup(1_000_000_000L, STAKING_INFO_1, STAKING_INFO_2, STAKING_INFO_3); + + // Assert preconditions + assertThat(STAKING_INFO_1.weight()).isZero(); + assertThat(STAKING_INFO_2.weight()).isZero(); + assertThat(STAKING_INFO_3.weight()).isZero(); + assertThat(STAKING_INFO_1.pendingRewards()).isZero(); + assertThat(STAKING_INFO_2.pendingRewards()).isZero(); + assertThat(STAKING_INFO_3.pendingRewards()).isZero(); + given(nodeStakeUpdateRecordBuilder.transaction(any())).willReturn(nodeStakeUpdateRecordBuilder); + given(nodeStakeUpdateRecordBuilder.memo(any())).willReturn(nodeStakeUpdateRecordBuilder); + given(nodeStakeUpdateRecordBuilder.exchangeRate(ExchangeRateSet.DEFAULT)) + .willReturn(nodeStakeUpdateRecordBuilder); + + subject.updateNodes(context, ExchangeRateSet.DEFAULT, weightUpdates, true); + + assertThat(stakingRewardsStore.totalStakeRewardStart()) + .isEqualTo(STAKE_TO_REWARD_1 + STAKE_TO_REWARD_2 + STAKE_TO_REWARD_3); + assertThat(stakingRewardsStore.totalStakedStart()).isEqualTo(130000000000L); + final var resultStakingInfo1 = stakingInfoStore.get(NODE_NUM_1.number()); + final var resultStakingInfo2 = stakingInfoStore.get(NODE_NUM_2.number()); + final var resultStakingInfo3 = stakingInfoStore.get(NODE_NUM_3.number()); + assertThat(resultStakingInfo1.stake()).isEqualTo(80000000000L); + assertThat(resultStakingInfo2.stake()).isEqualTo(50000000000L); + assertThat(resultStakingInfo3.stake()).isZero(); + assertThat(resultStakingInfo1.unclaimedStakeRewardStart()).isZero(); + assertThat(resultStakingInfo2.unclaimedStakeRewardStart()).isZero(); + assertThat(resultStakingInfo3.unclaimedStakeRewardStart()).isZero(); + assertThat(resultStakingInfo1.rewardSumHistory()).isEqualTo(List.of(86L, 6L, 5L)); + assertThat(resultStakingInfo2.rewardSumHistory()).isEqualTo(List.of(101L, 1L, 1L)); + assertThat(resultStakingInfo3.rewardSumHistory()).isEqualTo(List.of(11L, 3L, 1L)); + // Weight will not be updated, stake will be used as weight in roster + assertThat(resultStakingInfo1.weight()).isZero(); + assertThat(resultStakingInfo2.weight()).isZero(); + assertThat(resultStakingInfo3.weight()).isZero(); + assertThat(resultStakingInfo1.pendingRewards()).isEqualTo(72000); + assertThat(resultStakingInfo2.pendingRewards()).isEqualTo(63000L); + assertThat(resultStakingInfo3.pendingRewards()).isEqualTo(72000L); + } + @Test void calculatesNewEndOfPeriodStakingFieldsAsExpectedWhenMaxStakeIsLessThanTotalStake() { commonSetup(1_000_000_000L, STAKING_INFO_1, STAKING_INFO_2, STAKING_INFO_3); @@ -510,7 +552,9 @@ private void commonSetup( */ public static final EntityNumber NODE_NUM_8 = EntityNumber.newBuilder().number(8).build(); - /** Staking info for node 1. */ + /** + * Staking info for node 1. + */ public static final StakingNodeInfo STAKING_INFO_1 = StakingNodeInfo.newBuilder() .nodeNumber(NODE_NUM_1.number()) .minStake(MIN_STAKE) @@ -524,7 +568,9 @@ private void commonSetup( .deleted(false) .weight(0) .build(); - /** Staking info for node 2. */ + /** + * Staking info for node 2. + */ public static final StakingNodeInfo STAKING_INFO_2 = StakingNodeInfo.newBuilder() .nodeNumber(NODE_NUM_2.number()) .minStake(MIN_STAKE) @@ -538,7 +584,9 @@ private void commonSetup( .deleted(false) .weight(0) .build(); - /** Staking info for node 3. */ + /** + * Staking info for node 3. + */ public static final StakingNodeInfo STAKING_INFO_3 = StakingNodeInfo.newBuilder() .nodeNumber(NODE_NUM_3.number()) .minStake(MIN_STAKE)