From 550179595d21526a58387281441507367eabc680 Mon Sep 17 00:00:00 2001 From: MalteHerrmann Date: Tue, 7 May 2024 20:46:28 +0200 Subject: [PATCH] wait for number of blocks instead of seconds and check successful votes --- gov/vote.go | 15 +++++++++++++-- utils/utils.go | 28 +++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/gov/vote.go b/gov/vote.go index d1df414..195972a 100644 --- a/gov/vote.go +++ b/gov/vote.go @@ -30,10 +30,16 @@ func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error { return errors.New("no accounts with delegations found") } - utils.Wait(1) + if err := utils.WaitNBlocks(bin, 1); err != nil { + return err + } + bin.Logger.Info().Msgf("voting for proposal %d", proposalID) - var out string + var ( + out string + successfulVotes int + ) for _, acc := range accsWithDelegations { out, err = VoteForProposal(bin, proposalID, acc.Name) @@ -49,9 +55,14 @@ func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error { bin.Logger.Error().Msgf("could not vote using key %s: %v", acc.Name, err) } else { bin.Logger.Info().Msgf("voted using key %s", acc.Name) + successfulVotes += 1 } } + if successfulVotes == 0 { + return errors.New("there were no successful votes for the proposal, please check logs") + } + return nil } diff --git a/utils/utils.go b/utils/utils.go index 8982be9..ffa1031 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -80,6 +80,9 @@ func ExecuteBinaryCmd(bin *Binary, args BinaryCmdArgs) (string, error) { } // GetCurrentHeight returns the current block height of the node. +// +// NOTE: Because the response contains uint64 values encoded as strings, this cannot be unmarshalled +// from the BlockResult type. Instead, we use a regex to extract the height from the response. func GetCurrentHeight(bin *Binary) (int, error) { output, err := ExecuteQuery(bin, QueryArgs{ Subcommand: []string{"q", "block"}, @@ -187,7 +190,26 @@ func GetTxHashFromTxResponse(cdc *codec.ProtoCodec, out string) (string, error) return txHash.TxHash, nil } -// Wait waits for the specified amount of seconds. -func Wait(seconds int) { - time.Sleep(time.Duration(seconds) * time.Second) +// WaitNBlocks waits for the specified amount of blocks being produced +// on the connected network. +func WaitNBlocks(bin *Binary, n int) error { + currentHeight, err := GetCurrentHeight(bin) + if err != nil { + return err + } + + for { + bin.Logger.Debug().Msgf("waiting for %d blocks\n", n) + time.Sleep(2 * time.Second) + height, err := GetCurrentHeight(bin) + if err != nil { + return err + } + + if height >= currentHeight+n { + break + } + } + + return nil }