Skip to content

Commit

Permalink
Issue validation blocks with proper delay and await slot more precisely
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Apr 15, 2024
1 parent 2074bed commit cdda133
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
5 changes: 4 additions & 1 deletion tools/docker-network/tests/dockerframework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
35 changes: 16 additions & 19 deletions tools/docker-network/tests/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

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

0 comments on commit cdda133

Please sign in to comment.