From b32f38edbd7238b3607af8925243cbf2e6772060 Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Mon, 29 Apr 2024 12:14:03 -0400 Subject: [PATCH] Fix funding for cucumber tests by rotating sender accounts --- test/applications_integration_test.go | 16 ++++++++++++--- test/steps_test.go | 28 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/test/applications_integration_test.go b/test/applications_integration_test.go index 02466f86..9003f4a4 100644 --- a/test/applications_integration_test.go +++ b/test/applications_integration_test.go @@ -60,11 +60,17 @@ func iCreateANewTransientAccountAndFundItWithMicroalgos(microalgos int) error { } params.Fee = types.MicroAlgos(fee) - ltxn, err := transaction.MakePaymentTxn(accounts[1], transientAccount.Address.String(), uint64(microalgos), note, close, params) + + funder, err := fundingAccount(algodV2client, uint64(microalgos)) + if err != nil { + return err + } + + ltxn, err := transaction.MakePaymentTxn(funder, transientAccount.Address.String(), uint64(microalgos), note, close, params) if err != nil { return err } - lsk, err := kcl.ExportKey(handle, walletPswd, accounts[1]) + lsk, err := kcl.ExportKey(handle, walletPswd, funder) if err != nil { return err } @@ -91,7 +97,11 @@ func iFundTheCurrentApplicationsAddress(microalgos int) error { return err } - txn, err := transaction.MakePaymentTxn(accounts[0], address.String(), uint64(microalgos), nil, "", params) + funder, err := fundingAccount(algodV2client, uint64(microalgos)) + if err != nil { + return err + } + txn, err := transaction.MakePaymentTxn(funder, address.String(), uint64(microalgos), nil, "", params) if err != nil { return err } diff --git a/test/steps_test.go b/test/steps_test.go index 85779912..bd5dbd80 100644 --- a/test/steps_test.go +++ b/test/steps_test.go @@ -10,6 +10,7 @@ import ( "encoding/json" "flag" "fmt" + "math/rand" "os" "path" "reflect" @@ -148,13 +149,38 @@ func waitForAlgodInDevMode() { time.Sleep(500 * time.Millisecond) } +// fundingAccount finds an account with enough funds to fund the given amount +func fundingAccount(client *algodV2.Client, amount uint64) (string, error) { + // Random shuffle to spread the load + shuffledAccounts := make([]string, len(accounts)) + copy(shuffledAccounts, accounts) + rand.Shuffle(len(shuffledAccounts), func(i, j int) { + shuffledAccounts[i], shuffledAccounts[j] = shuffledAccounts[j], shuffledAccounts[i] + }) + for _, accountAddress := range shuffledAccounts { + res, err := client.AccountInformation(accountAddress).Do(context.Background()) + if err != nil { + return "", err + } + if res.Amount < amount+100_000 { // 100,000 microalgos is default account min balance + continue + } + return accountAddress, nil + } + return "", fmt.Errorf("no account has enough to fund %d microalgos", amount) +} + func initializeAccount(accountAddress string) error { params, err := aclv2.SuggestedParams().Do(context.Background()) if err != nil { return err } - txn, err = transaction.MakePaymentTxn(accounts[0], accountAddress, devModeInitialAmount, []byte{}, "", params) + funder, err := fundingAccount(aclv2, devModeInitialAmount) + if err != nil { + return err + } + txn, err = transaction.MakePaymentTxn(funder, accountAddress, devModeInitialAmount, []byte{}, "", params) if err != nil { return err }