diff --git a/tools/docker-network/tests/api_core_test.go b/tools/docker-network/tests/api_core_test.go index 8de31ea4c..d9ad30c4c 100644 --- a/tools/docker-network/tests/api_core_test.go +++ b/tools/docker-network/tests/api_core_test.go @@ -319,7 +319,7 @@ func Test_ValidatorsAPI(t *testing.T) { } wg.Wait() - d.AwaitCommitment(clt.CommittedAPI().TimeProvider().EpochEnd(fullAccountCreationEpoch)) + d.AwaitCommittedSlot(clt.CommittedAPI().TimeProvider().EpochEnd(fullAccountCreationEpoch)) // check if all validators are returned from the validators API with pageSize 10 actualValidators := getAllValidatorsOnEpoch(t, clt, 0, 10) @@ -327,7 +327,7 @@ func Test_ValidatorsAPI(t *testing.T) { // wait until fullAccountCreationEpoch+1 and check the results again targetSlot := clt.CommittedAPI().TimeProvider().EpochEnd(fullAccountCreationEpoch + 1) - d.AwaitCommitment(targetSlot) + d.AwaitCommittedSlot(targetSlot) actualValidators = getAllValidatorsOnEpoch(t, clt, fullAccountCreationEpoch+1, 10) require.ElementsMatch(t, expectedValidators, actualValidators) } @@ -352,7 +352,7 @@ func Test_CoreAPI_ValidRequests(t *testing.T) { assetsPerSlot, lastSlot := prepareAssets(d, 5) fmt.Println("Await finalisation of slot", lastSlot) - d.AwaitFinalization(lastSlot) + d.AwaitFinalizedSlot(lastSlot) tests := []struct { name string diff --git a/tools/docker-network/tests/api_management_test.go b/tools/docker-network/tests/api_management_test.go index 48f8cdf85..945944a68 100644 --- a/tools/docker-network/tests/api_management_test.go +++ b/tools/docker-network/tests/api_management_test.go @@ -46,7 +46,7 @@ func Test_ManagementAPI_Peers_ValidRequests(t *testing.T) { d.WaitUntilNetworkReady() // wait longer for autopeering - d.AwaitCommitment(d.DefaultWallet().CurrentSlot()) + d.AwaitCommittedSlot(d.DefaultWallet().CurrentSlot()) // get the management client managementClient, err := d.Client("V1").Management(getContextWithTimeout(5 * time.Second)) diff --git a/tools/docker-network/tests/dockertestframework/accounts.go b/tools/docker-network/tests/dockertestframework/accounts.go index bbdec050b..82fc7f2ef 100644 --- a/tools/docker-network/tests/dockertestframework/accounts.go +++ b/tools/docker-network/tests/dockertestframework/accounts.go @@ -44,7 +44,7 @@ func (d *DockerTestFramework) CheckAccountStatus(ctx context.Context, blkID iota d.AwaitTransactionPayloadAccepted(ctx, txID) // wait for the account to be committed - d.AwaitCommitment(slot) + d.AwaitCommittedSlot(slot) // Check the indexer if len(checkIndexer) > 0 && checkIndexer[0] { @@ -209,7 +209,7 @@ func (d *DockerTestFramework) CreateNativeToken(fromWallet *mock.Wallet, mintedA fmt.Println("Create native tokens transaction sent, blkID:", block.ID().ToHex(), ", txID:", signedTx.Transaction.MustID().ToHex(), ", slot:", block.ID().Slot()) // wait for the account to be committed - d.AwaitCommitment(block.ID().Slot()) + d.AwaitCommittedSlot(block.ID().Slot()) d.AssertIndexerAccount(fromWallet.BlockIssuer.AccountData) //nolint:forcetypeassert diff --git a/tools/docker-network/tests/dockertestframework/awaits.go b/tools/docker-network/tests/dockertestframework/awaits.go index 747d9940b..f90af9bfd 100644 --- a/tools/docker-network/tests/dockertestframework/awaits.go +++ b/tools/docker-network/tests/dockertestframework/awaits.go @@ -69,8 +69,8 @@ func (d *DockerTestFramework) AwaitTransactionFailure(ctx context.Context, txID }) } -func (d *DockerTestFramework) AwaitSlot(targetSlot iotago.SlotIndex) { - currentSlot := d.NodeStatus("V1").LatestAcceptedBlockSlot +func (d *DockerTestFramework) awaitSlot(targetSlot iotago.SlotIndex, slotName string, getCurrentSlotFunc func() iotago.SlotIndex, offsetDeadline ...time.Duration) { + currentSlot := getCurrentSlotFunc() if targetSlot <= currentSlot { return @@ -83,64 +83,39 @@ func (d *DockerTestFramework) AwaitSlot(targetSlot iotago.SlotIndex) { } // give some extra time for peering etc - deadline += 30 * time.Second - - d.EventuallyWithDurations(func() error { - latestAcceptedSlot := d.NodeStatus("V1").LatestAcceptedBlockSlot - if targetSlot > latestAcceptedSlot { - return ierrors.Errorf("current slot %d is not reached yet, current slot %d", targetSlot, latestAcceptedSlot) - } - - return nil - }, deadline, 1*time.Second) -} - -func (d *DockerTestFramework) AwaitCommitment(targetSlot iotago.SlotIndex) { - currentCommittedSlot := d.NodeStatus("V1").LatestCommitmentID.Slot() - - if targetSlot <= currentCommittedSlot { - return - } - - // we wait at max "targetSlot - currentCommittedSlot" times * slot duration - deadline := time.Duration(d.defaultWallet.Client.CommittedAPI().ProtocolParameters().SlotDurationInSeconds()) * time.Second - if currentCommittedSlot < targetSlot { - deadline *= time.Duration(targetSlot - currentCommittedSlot) + if len(offsetDeadline) > 0 { + deadline += offsetDeadline[0] + } else { + // add 30 seconds as default + deadline += 30 * time.Second } - // give some extra time for peering etc - deadline += 30 * time.Second - d.EventuallyWithDurations(func() error { - latestCommittedSlot := d.NodeStatus("V1").LatestCommitmentID.Slot() - if targetSlot > latestCommittedSlot { - return ierrors.Errorf("committed slot %d is not reached yet, current committed slot %d", targetSlot, latestCommittedSlot) + currentSlot := getCurrentSlotFunc() + if targetSlot > currentSlot { + return ierrors.Errorf("%s slot %d is not reached yet, %s slot %d", slotName, targetSlot, slotName, currentSlot) } return nil }, deadline, 1*time.Second) } -func (d *DockerTestFramework) AwaitFinalization(targetSlot iotago.SlotIndex) { - currentFinalizedSlot := d.NodeStatus("V1").LatestFinalizedSlot - - // we wait at max "targetSlot - currentFinalizedSlot" times * slot duration - deadline := time.Duration(d.defaultWallet.Client.CommittedAPI().ProtocolParameters().SlotDurationInSeconds()) * time.Second - if currentFinalizedSlot < targetSlot { - deadline *= time.Duration(targetSlot - currentFinalizedSlot) - } - - // give some extra time for peering etc - deadline += 30 * time.Second +func (d *DockerTestFramework) AwaitLatestAcceptedBlockSlot(targetSlot iotago.SlotIndex, offsetDeadline ...time.Duration) { + d.awaitSlot(targetSlot, "latest accepted block", func() iotago.SlotIndex { + return d.NodeStatus("V1").LatestAcceptedBlockSlot + }, offsetDeadline...) +} - d.EventuallyWithDurations(func() error { - currentFinalisedSlot := d.NodeStatus("V1").LatestFinalizedSlot - if targetSlot > currentFinalisedSlot { - return ierrors.Errorf("finalized slot %d is not reached yet", targetSlot) - } +func (d *DockerTestFramework) AwaitCommittedSlot(targetSlot iotago.SlotIndex, offsetDeadline ...time.Duration) { + d.awaitSlot(targetSlot, "committed", func() iotago.SlotIndex { + return d.NodeStatus("V1").LatestCommitmentID.Slot() + }, offsetDeadline...) +} - return nil - }, deadline, 1*time.Second) +func (d *DockerTestFramework) AwaitFinalizedSlot(targetSlot iotago.SlotIndex, offsetDeadline ...time.Duration) { + d.awaitSlot(targetSlot, "finalized", func() iotago.SlotIndex { + return d.NodeStatus("V1").LatestFinalizedSlot + }, offsetDeadline...) } func (d *DockerTestFramework) AwaitEpochFinalized() { @@ -153,7 +128,7 @@ func (d *DockerTestFramework) AwaitEpochFinalized() { currentEpoch := d.defaultWallet.Client.CommittedAPI().TimeProvider().EpochFromSlot(info.Status.LatestFinalizedSlot) // await the start slot of the next epoch - d.AwaitFinalization(d.defaultWallet.Client.CommittedAPI().TimeProvider().EpochStart(currentEpoch + 1)) + d.AwaitFinalizedSlot(d.defaultWallet.Client.CommittedAPI().TimeProvider().EpochStart(currentEpoch + 1)) } func (d *DockerTestFramework) AwaitAddressUnspentOutputAccepted(ctx context.Context, wallet *mock.Wallet, addr iotago.Address) (outputID iotago.OutputID, output iotago.Output, err error) { diff --git a/tools/docker-network/tests/dockertestframework/faucet.go b/tools/docker-network/tests/dockertestframework/faucet.go index 52c04f9b7..19e5c1722 100644 --- a/tools/docker-network/tests/dockertestframework/faucet.go +++ b/tools/docker-network/tests/dockertestframework/faucet.go @@ -116,7 +116,7 @@ func (d *DockerTestFramework) RequestFaucetFundsAndAllotManaTo(fromWallet *mock. d.AwaitTransactionPayloadAccepted(ctx, signedTx.Transaction.MustID()) // allotment is updated when the transaction is committed - d.AwaitCommitment(block.ID().Slot()) + d.AwaitCommittedSlot(block.ID().Slot()) // check if the mana is allotted toCongestionResp, err := clt.Congestion(ctx, to.Address, 0, preAllotmentCommitmentID) diff --git a/tools/docker-network/tests/rewards_test.go b/tools/docker-network/tests/rewards_test.go index 0ddf89338..979d22cb9 100644 --- a/tools/docker-network/tests/rewards_test.go +++ b/tools/docker-network/tests/rewards_test.go @@ -124,7 +124,7 @@ func Test_ValidatorRewards(t *testing.T) { wg.Wait() // claim rewards that put to the account output - d.AwaitCommitment(validationBlocksEndSlot) + d.AwaitCommittedSlot(validationBlocksEndSlot) d.ClaimRewardsForValidator(ctx, goodValidator) d.ClaimRewardsForValidator(ctx, lazyValidator) @@ -173,7 +173,7 @@ func Test_DelegatorRewards(t *testing.T) { // delegate funds to V2 delegationOutputData := d.DelegateToValidator(delegatorWallet, d.Node("V2").AccountAddress(t)) - d.AwaitCommitment(delegationOutputData.ID.CreationSlot()) + d.AwaitCommittedSlot(delegationOutputData.ID.CreationSlot()) // check if V2 received the delegator stake v2Resp, err := clt.Validator(ctx, d.Node("V2").AccountAddress(t)) @@ -236,7 +236,7 @@ func Test_DelayedClaimingRewards(t *testing.T) { { // delegate funds to V2 delegationOutputData := d.DelegateToValidator(delegatorWallet, d.Node("V2").AccountAddress(t)) - d.AwaitCommitment(delegationOutputData.ID.CreationSlot()) + d.AwaitCommittedSlot(delegationOutputData.ID.CreationSlot()) // check if V2 received the delegator stake v2Resp, err := clt.Validator(ctx, d.Node("V2").AccountAddress(t)) @@ -249,7 +249,7 @@ func Test_DelayedClaimingRewards(t *testing.T) { latestCommitmentSlot := delegatorWallet.GetNewBlockIssuanceResponse().LatestCommitment.Slot delegationEndEpoch := dockertestframework.GetDelegationEndEpoch(apiForSlot, currentSlot, latestCommitmentSlot) delegationOutputData = d.DelayedClaimingTransition(ctx, delegatorWallet, delegationOutputData) - d.AwaitCommitment(delegationOutputData.ID.CreationSlot()) + d.AwaitCommittedSlot(delegationOutputData.ID.CreationSlot()) // the delegated stake should be removed from the validator, so the pool stake should equal to the validator stake v2Resp, err = clt.Validator(ctx, d.Node("V2").AccountAddress(t)) @@ -274,7 +274,7 @@ func Test_DelayedClaimingRewards(t *testing.T) { // delay claiming rewards in the same slot of delegation delegationOutputData = d.DelayedClaimingTransition(ctx, delegatorWallet, delegationOutputData) - d.AwaitCommitment(delegationOutputData.ID.CreationSlot()) + d.AwaitCommittedSlot(delegationOutputData.ID.CreationSlot()) // the delegated stake should be 0, thus poolStake should be equal to validatorStake v2Resp, err := clt.Validator(ctx, d.Node("V2").AccountAddress(t)) @@ -299,7 +299,7 @@ func issueCandidacyAnnouncementsInBackground(ctx context.Context, d *dockertestf } // wait until the epoch start is reached - d.AwaitSlot(d.DefaultWallet().Client.CommittedAPI().TimeProvider().EpochStart(epoch)) + d.AwaitLatestAcceptedBlockSlot(d.DefaultWallet().Client.CommittedAPI().TimeProvider().EpochStart(epoch)) if ctx.Err() != nil { // context is canceled return @@ -337,7 +337,7 @@ func issueValidationBlocksInBackground(ctx context.Context, d *dockertestframewo } // wait until the slot is reached - d.AwaitSlot(slot) + d.AwaitLatestAcceptedBlockSlot(slot) if ctx.Err() != nil { // context is canceled return diff --git a/tools/docker-network/tests/sync_snapshot_test.go b/tools/docker-network/tests/sync_snapshot_test.go index 9731d5c51..b4d114362 100644 --- a/tools/docker-network/tests/sync_snapshot_test.go +++ b/tools/docker-network/tests/sync_snapshot_test.go @@ -46,7 +46,7 @@ func Test_SyncFromSnapshot(t *testing.T) { // delegate funds to receiver delegationOutputData := d.DelegateToValidator(delegatorWallet, receiver.AccountAddress(t)) - d.AwaitCommitment(delegationOutputData.ID.CreationSlot()) + d.AwaitCommittedSlot(delegationOutputData.ID.CreationSlot()) // check if receiver received the delegator stake resp, err := clt.Validator(ctx, receiver.AccountAddress(t))