Skip to content

Commit

Permalink
Unify await slot funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed May 15, 2024
1 parent 7579323 commit 23d7729
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 65 deletions.
6 changes: 3 additions & 3 deletions tools/docker-network/tests/api_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ 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)
require.ElementsMatch(t, expectedValidators, actualValidators)

// 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)
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/docker-network/tests/api_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tools/docker-network/tests/dockertestframework/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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
Expand Down
75 changes: 25 additions & 50 deletions tools/docker-network/tests/dockertestframework/awaits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tools/docker-network/tests/dockertestframework/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions tools/docker-network/tests/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/docker-network/tests/sync_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 23d7729

Please sign in to comment.