From cdda1339e7d70ee7d90afdb93d0ee74c5d52a19e Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Mon, 15 Apr 2024 11:01:34 +0200 Subject: [PATCH] Issue validation blocks with proper delay and await slot more precisely --- tools/docker-network/tests/dockerframework.go | 5 ++- tools/docker-network/tests/rewards_test.go | 35 +++++++++---------- tools/docker-network/tests/wallet.go | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tools/docker-network/tests/dockerframework.go b/tools/docker-network/tests/dockerframework.go index 51e4e2f37..89074a300 100644 --- a/tools/docker-network/tests/dockerframework.go +++ b/tools/docker-network/tests/dockerframework.go @@ -403,11 +403,14 @@ func (d *DockerTestFramework) CreateTaggedDataBlock(issuerID iotago.AccountID, t }, issuerID, congestionResp, issuerResp) } -func (d *DockerTestFramework) SubmitValidationBlock(issuerID iotago.AccountID) *iotago.Block { +func (d *DockerTestFramework) SubmitValidationBlock(issuerID iotago.AccountID, issueNode ...string) *iotago.Block { issuer := d.wallet.Account(issuerID) ctx := context.TODO() issuingTime := time.Now() clt := d.wallet.DefaultClient() + if issueNode != nil { + clt = d.wallet.Clients[issueNode[0]] + } currentSlot := d.wallet.DefaultClient().LatestAPI().TimeProvider().SlotFromTime(issuingTime) apiForSlot := d.wallet.DefaultClient().APIForSlot(currentSlot) diff --git a/tools/docker-network/tests/rewards_test.go b/tools/docker-network/tests/rewards_test.go index 7b281070e..06703f1b2 100644 --- a/tools/docker-network/tests/rewards_test.go +++ b/tools/docker-network/tests/rewards_test.go @@ -52,12 +52,12 @@ func Test_ValidatorRewards(t *testing.T) { // create accounts and continue issuing candidacy payload for account in the background account := d.CreateAccount(WithStakingFeature(100, 1, currentEpoch, endEpoch)) initialMana := account.Output.StoredMana() - issueCandidacyPayload(d, account.ID, clt.CommittedAPI().TimeProvider().CurrentSlot(), claimingSlot, + issueCandidacyPayloadInBackground(d, account.ID, clt.CommittedAPI().TimeProvider().CurrentSlot(), claimingSlot, slotsDuration) lazyAccount := d.CreateAccount(WithStakingFeature(100, 1, currentEpoch, endEpoch)) lazyInitialMana := lazyAccount.Output.StoredMana() - issueCandidacyPayload(d, lazyAccount.ID, clt.CommittedAPI().TimeProvider().CurrentSlot(), claimingSlot, + issueCandidacyPayloadInBackground(d, lazyAccount.ID, clt.CommittedAPI().TimeProvider().CurrentSlot(), claimingSlot, slotsDuration) // make sure the account is in the committee, so it can issue validation blocks @@ -72,8 +72,8 @@ func Test_ValidatorRewards(t *testing.T) { fmt.Println("Wait for ", secToWait, "until expected slot: ", claimingSlot) var wg sync.WaitGroup - issueValidationBlockInBackground(&wg, d, account.ID, currentSlot, claimingSlot, 3, slotsDuration) - issueValidationBlockInBackground(&wg, d, lazyAccount.ID, currentSlot, claimingSlot, 1, slotsDuration) + issueValidationBlockInBackground(&wg, d, account.ID, currentSlot, claimingSlot, 5) + issueValidationBlockInBackground(&wg, d, lazyAccount.ID, currentSlot, claimingSlot, 1) wg.Wait() @@ -228,7 +228,7 @@ func Test_DelayedClaimingRewards(t *testing.T) { } } -func issueCandidacyPayload(d *DockerTestFramework, accountID iotago.AccountID, startSlot, endSlot iotago.SlotIndex, slotDuration uint8) { +func issueCandidacyPayloadInBackground(d *DockerTestFramework, accountID iotago.AccountID, startSlot, endSlot iotago.SlotIndex, slotDuration uint8) { go func() { fmt.Println("Issuing candidacy payloads for account", accountID, "in the background...") defer fmt.Println("Issuing candidacy payloads for account", accountID, "in the background......done") @@ -240,31 +240,28 @@ func issueCandidacyPayload(d *DockerTestFramework, accountID iotago.AccountID, s }() } -func issueValidationBlock(d *DockerTestFramework, accountID iotago.AccountID, startSlot, endSlot iotago.SlotIndex, blocksPerSlot int, slotDuration uint8) { - fmt.Println("Issuing validation block for account", accountID, "in the background...") - defer fmt.Println("Issuing validation block for account", accountID, "in the background......done") - - for i := startSlot; i < endSlot; i++ { - for range blocksPerSlot { - d.SubmitValidationBlock(accountID) - } - time.Sleep(time.Duration(slotDuration) * time.Second) - } -} - -func issueValidationBlockInBackground(wg *sync.WaitGroup, d *DockerTestFramework, accountID iotago.AccountID, startSlot, endSlot iotago.SlotIndex, blocksPerSlot int, slotDuration uint8) { +func issueValidationBlockInBackground(wg *sync.WaitGroup, d *DockerTestFramework, accountID iotago.AccountID, startSlot, endSlot iotago.SlotIndex, blocksPerSlot int) { wg.Add(1) go func() { defer wg.Done() fmt.Println("Issuing validation block for account", accountID, "in the background...") defer fmt.Println("Issuing validation block for account", accountID, "in the background......done") + clt := d.wallet.DefaultClient() for i := startSlot; i < endSlot; i++ { + // wait until the slot is reached + for { + if clt.CommittedAPI().TimeProvider().CurrentSlot() == i { + break + } + time.Sleep(2 * time.Second) + } + for range blocksPerSlot { d.SubmitValidationBlock(accountID) + time.Sleep(1 * time.Second) } - time.Sleep(time.Duration(slotDuration) * time.Second) } }() } diff --git a/tools/docker-network/tests/wallet.go b/tools/docker-network/tests/wallet.go index ebb4c3006..28c06c3d9 100644 --- a/tools/docker-network/tests/wallet.go +++ b/tools/docker-network/tests/wallet.go @@ -534,7 +534,7 @@ func (w *DockerWallet) ClaimValidatorRewards(issuerAccountID iotago.AccountID, i }). AddRewardInput(&iotago.RewardInput{Index: 0}, rewardResp.Rewards). AddBlockIssuanceCreditInput(&iotago.BlockIssuanceCreditInput{ - AccountID: accountOutput.AccountID, + AccountID: issuerAccountID, }). AddCommitmentInput(&iotago.CommitmentInput{CommitmentID: lo.Return1(issuerResp.LatestCommitment.ID())}). AddOutput(accountOutput).