Skip to content

Commit

Permalink
wait for number of blocks instead of seconds and check successful votes
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed May 7, 2024
1 parent 47d59a8 commit 5501795
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
15 changes: 13 additions & 2 deletions gov/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 34 in gov/vote.go

View workflow job for this annotation

GitHub Actions / lint

error returned from external package is unwrapped: sig: func github.com/MalteHerrmann/evmos-utils/utils.WaitNBlocks(bin *github.com/MalteHerrmann/evmos-utils/utils.Binary, n int) error (wrapcheck)
}

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)
Expand All @@ -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

Check warning on line 58 in gov/vote.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace successfulVotes += 1 with successfulVotes++ (revive)
}
}

if successfulVotes == 0 {
return errors.New("there were no successful votes for the proposal, please check logs")
}

return nil
}

Expand Down
28 changes: 25 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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 {

Check failure on line 195 in utils/utils.go

View workflow job for this annotation

GitHub Actions / lint

parameter name 'n' is too short for the scope of its usage (varnamelen)
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)

Check failure on line 203 in utils/utils.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 2, in <argument> detected (mnd)
height, err := GetCurrentHeight(bin)

Check failure on line 204 in utils/utils.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)
if err != nil {

Check failure on line 205 in utils/utils.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
return err
}

if height >= currentHeight+n {
break
}
}

return nil
}

0 comments on commit 5501795

Please sign in to comment.