From f1efb3e6dfb2a8330d45acca99b6dc76c06445c3 Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Fri, 3 May 2024 21:32:41 +0800 Subject: [PATCH] Fix Test_ValidatorRewards with correct await and validation blocks issuance --- tools/docker-network/tests/rewards_test.go | 27 ++++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/tools/docker-network/tests/rewards_test.go b/tools/docker-network/tests/rewards_test.go index 87b53c040..eb87f2d45 100644 --- a/tools/docker-network/tests/rewards_test.go +++ b/tools/docker-network/tests/rewards_test.go @@ -53,14 +53,13 @@ func Test_ValidatorRewards(t *testing.T) { latestCommitmentSlot := blockIssuance.LatestCommitment.Slot stakingStartEpoch := d.defaultWallet.StakingStartEpochFromSlot(latestCommitmentSlot) - currentEpoch := clt.CommittedAPI().TimeProvider().EpochFromSlot(latestCommitmentSlot) slotsDuration := clt.CommittedAPI().ProtocolParameters().SlotDurationInSeconds() // Set end epoch so the staking feature can be removed as soon as possible. endEpoch := stakingStartEpoch + clt.CommittedAPI().ProtocolParameters().StakingUnbondingPeriod() + 1 // The earliest epoch in which we can remove the staking feature and claim rewards. - claimingSlot := clt.CommittedAPI().TimeProvider().EpochStart(endEpoch + 1) + goodClaimingSlot := clt.CommittedAPI().TimeProvider().EpochStart(endEpoch + 1) // create accounts and continue issuing candidacy payload for account in the background goodWallet, goodAccountData := d.CreateAccount(WithStakingFeature(100, 1, stakingStartEpoch, endEpoch)) @@ -69,16 +68,19 @@ func Test_ValidatorRewards(t *testing.T) { d, goodWallet, clt.CommittedAPI().TimeProvider().CurrentSlot(), - claimingSlot, + goodClaimingSlot, slotsDuration) lazyWallet, lazyAccountData := d.CreateAccount(WithStakingFeature(100, 1, stakingStartEpoch, endEpoch)) + // The StartEpoch and EndEpoch may alter, so we need to recalculate the claiming slot. + // Note that we can only change the StartEpoch and EndEpoch in the wallet during account transition, because we wait for implicit account to be usable in CreateAccount, changing those in advance may still be outdated. + lazyClaimingSlot := clt.CommittedAPI().TimeProvider().EpochStart(lazyAccountData.Output.FeatureSet().Staking().EndEpoch + 1) lazyInitialMana := lazyAccountData.Output.StoredMana() issueCandidacyPayloadInBackground(ctx, d, lazyWallet, clt.CommittedAPI().TimeProvider().CurrentSlot(), - claimingSlot, + lazyClaimingSlot, slotsDuration) // make sure the account is in the committee, so it can issue validation blocks @@ -87,20 +89,15 @@ func Test_ValidatorRewards(t *testing.T) { d.AssertCommittee(currentEpoch+2, append(d.AccountsFromNodes(d.Nodes("V1", "V3", "V2", "V4")...), goodAccountAddrBech32, lazyAccountAddrBech32)) // issue validation blocks to have performance - if currentSlot := clt.CommittedAPI().TimeProvider().CurrentSlot(); currentSlot < claimingSlot { - slotToWait := claimingSlot - currentSlot - secToWait := time.Duration(slotToWait) * time.Duration(slotsDuration) * time.Second - fmt.Println("Wait for ", secToWait, "until expected slot: ", claimingSlot) - - var wg sync.WaitGroup - issueValidationBlockInBackground(ctx, &wg, goodWallet, currentSlot, claimingSlot, 5) - issueValidationBlockInBackground(ctx, &wg, lazyWallet, currentSlot, claimingSlot, 1) + currentSlot := clt.CommittedAPI().TimeProvider().CurrentSlot() + var wg sync.WaitGroup + issueValidationBlockInBackground(ctx, &wg, goodWallet, currentSlot, goodClaimingSlot, 5) + issueValidationBlockInBackground(ctx, &wg, lazyWallet, currentSlot, lazyClaimingSlot, 1) - wg.Wait() - } + wg.Wait() // claim rewards that put to the account output - d.AwaitCommitment(claimingSlot) + d.AwaitCommitment(lazyClaimingSlot) d.ClaimRewardsForValidator(ctx, goodWallet) d.ClaimRewardsForValidator(ctx, lazyWallet)