Skip to content

Commit

Permalink
linters and restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Dec 17, 2023
1 parent 473208b commit 4f93ce5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 93 deletions.
27 changes: 27 additions & 0 deletions gov/deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gov

import (
"fmt"
"strconv"

"github.com/MalteHerrmann/upgrade-local-node-go/utils"
"github.com/pkg/errors"
)

// DepositForProposal deposits the given amount of Evmos for the proposal with the given proposalID
// from the given account.
func DepositForProposal(bin *utils.Binary, proposalID int, sender string, amount int) (string, error) {
out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{
Subcommand: []string{
"tx", "gov", "deposit", strconv.Itoa(proposalID), strconv.Itoa(amount) + "aevmos",
},
From: sender,
UseDefaults: true,
Quiet: true,
})
if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("failed to deposit for proposal %d", proposalID))
}

return out, nil
}
71 changes: 0 additions & 71 deletions gov/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gov

import (
"fmt"
"log"
"strconv"
"strings"

Expand Down Expand Up @@ -78,42 +77,6 @@ func QueryLatestProposalID(bin *utils.Binary) (int, error) {
return int(res.Proposals[len(res.Proposals)-1].Id), nil
}

// SubmitAllVotesForProposal submits a vote for the given proposal ID using all testing accounts.
func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error {
accsWithDelegations, err := utils.FilterAccountsWithDelegations(bin)
if err != nil {
return errors.Wrap(err, "Error filtering accounts")
}

if len(accsWithDelegations) == 0 {
return errors.New("No accounts with delegations found")
}

utils.Wait(1)
log.Printf("Voting for proposal %d...\n", proposalID)

var out string

for _, acc := range accsWithDelegations {
out, err = VoteForProposal(bin, proposalID, acc.Name)
if err != nil {
if strings.Contains(out, fmt.Sprintf("%d: unknown proposal", proposalID)) {
return fmt.Errorf("no proposal with ID %d found", proposalID)
}

if strings.Contains(out, fmt.Sprintf("%d: inactive proposal", proposalID)) {
return fmt.Errorf("proposal with ID %d is inactive", proposalID)
}

log.Printf(" - could NOT vote using key: %s\n", acc.Name)
} else {
log.Printf(" - voted using key: %s\n", acc.Name)
}
}

return nil
}

// SubmitUpgradeProposal submits a software upgrade proposal with the given target version and upgrade height.
func SubmitUpgradeProposal(bin *utils.Binary, targetVersion string, upgradeHeight int) (int, error) {
upgradeProposal := buildUpgradeProposalCommand(targetVersion, upgradeHeight)
Expand Down Expand Up @@ -145,37 +108,3 @@ func SubmitUpgradeProposal(bin *utils.Binary, targetVersion string, upgradeHeigh

return GetProposalIDFromSubmitEvents(events)
}

// VoteForProposal votes for the proposal with the given ID using the given account.
func VoteForProposal(bin *utils.Binary, proposalID int, sender string) (string, error) {
out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{
Subcommand: []string{"tx", "gov", "vote", strconv.Itoa(proposalID), "yes"},
From: sender,
UseDefaults: true,
Quiet: true,
})
if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("failed to vote for proposal %d", proposalID))
}

return out, nil
}

// DepositForProposal deposits the given amount of Evmos for the proposal with the given proposalID
// from the given account.
func DepositForProposal(bin *utils.Binary, proposalID int, sender string, amount int) (string, error) {
out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{
Subcommand: []string{
"tx", "gov", "deposit", strconv.Itoa(proposalID), strconv.Itoa(amount) + "aevmos",
},
From: sender,
UseDefaults: true,
Quiet: true,
})

if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("failed to deposit for proposal %d", proposalID))
}

return out, nil
}
62 changes: 62 additions & 0 deletions gov/vote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package gov

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/MalteHerrmann/upgrade-local-node-go/utils"
"github.com/pkg/errors"
)

// SubmitAllVotesForProposal submits a vote for the given proposal ID using all testing accounts.
func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error {
accsWithDelegations, err := utils.FilterAccountsWithDelegations(bin)
if err != nil {
return errors.Wrap(err, "error filtering accounts")
}

if len(accsWithDelegations) == 0 {
return errors.New("no accounts with delegations found")
}

utils.Wait(1)
log.Printf("Voting for proposal %d...\n", proposalID)

var out string

for _, acc := range accsWithDelegations {
out, err = VoteForProposal(bin, proposalID, acc.Name)
if err != nil {
if strings.Contains(out, fmt.Sprintf("%d: unknown proposal", proposalID)) {
return fmt.Errorf("no proposal with ID %d found", proposalID)
}

if strings.Contains(out, fmt.Sprintf("%d: inactive proposal", proposalID)) {
return fmt.Errorf("proposal with ID %d is inactive", proposalID)
}

log.Printf(" - could NOT vote using key: %s\n", acc.Name)
} else {
log.Printf(" - voted using key: %s\n", acc.Name)
}
}

return nil
}

// VoteForProposal votes for the proposal with the given ID using the given account.
func VoteForProposal(bin *utils.Binary, proposalID int, sender string) (string, error) {
out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{
Subcommand: []string{"tx", "gov", "vote", strconv.Itoa(proposalID), "yes"},
From: sender,
UseDefaults: true,
Quiet: true,
})
if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("failed to vote for proposal %d", proposalID))
}

return out, nil
}
40 changes: 18 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,44 @@ func main() {

bin, err := utils.NewEvmosTestingBinary()
if err != nil {
log.Fatalf("Error creating binary: %v", err)
log.Fatalf("error creating binary: %v", err)
}

err = bin.GetAccounts()
if err != nil {
log.Fatalf("Error getting accounts: %v", err)
if err = bin.GetAccounts(); err != nil {
log.Fatalf("error getting accounts: %v", err)

Check failure on line 34 in main.go

View workflow job for this annotation

GitHub Actions / lint

main.go:34: Line contains TODO/BUG/FIXME: "TODO: use with Cobra CLI" (godox)
}

// TODO: use with Cobra CLI
switch os.Args[1] {
case "vote":
proposalID, err := getProposalIDFromInput(bin, os.Args)
if err != nil {
log.Fatalf("Error getting proposal ID: %v", err)
log.Fatalf("error getting proposal ID: %v", err)
}

err = gov.SubmitAllVotesForProposal(bin, proposalID)
if err != nil {
log.Fatalf("Error submitting votes for proposal %d: %v", proposalID, err)
if err = gov.SubmitAllVotesForProposal(bin, proposalID); err != nil {
log.Fatalf("error submitting votes for proposal %d: %v", proposalID, err)
}

case "deposit":
proposalID, err := getProposalIDFromInput(bin, os.Args)
if err != nil {
log.Fatalf("Error getting proposal ID: %v", err)
log.Fatalf("error getting proposal ID: %v", err)

Check failure on line 52 in main.go

View workflow job for this annotation

GitHub Actions / lint

main.go:52: Line contains TODO/BUG/FIXME: "TODO: replace fixed amount with min depo..." (godox)
}

// TODO: replace fixed amount with min deposit from chain params
if _, err = gov.DepositForProposal(bin, proposalID, bin.Accounts[0].Name, 1e9); err != nil {
log.Fatalf("Error depositing for proposal %d: %v", proposalID, err)
log.Fatalf("error depositing for proposal %d: %v", proposalID, err)
}

default:
targetVersion := os.Args[1]
if matched, _ := regexp.MatchString(`v\d+\.\d+\.\d(-rc\d+)?`, targetVersion); !matched {
log.Fatalf("Invalid target version: %s. Please use the format vX.Y.Z(-rc*).\n", targetVersion)
log.Fatalf("invalid target version: %s; please use the format vX.Y.Z(-rc*).\n", targetVersion)
}

err := upgradeLocalNode(bin, targetVersion)
if err != nil {
log.Fatalf("Error upgrading local node: %v", err)
if err := upgradeLocalNode(bin, targetVersion); err != nil {
log.Fatalf("error upgrading local node: %v", err)
}
}
}
Expand All @@ -83,15 +80,15 @@ func getProposalIDFromInput(bin *utils.Binary, args []string) (int, error) {
case 2:
proposalID, err = gov.QueryLatestProposalID(bin)
if err != nil {
return 0, errors.Wrap(err, "Error querying latest proposal ID")
return 0, errors.Wrap(err, "error querying latest proposal ID")
}
case 3:
proposalID, err = strconv.Atoi(args[2])
if err != nil {
return 0, errors.Wrapf(err, "Error converting proposal ID %s to integer", args[2])
return 0, errors.Wrapf(err, "error converting proposal ID %s to integer", args[2])
}
default:
return 0, errors.New("Invalid number of arguments")
return 0, errors.New("invalid number of arguments")
}

return proposalID, nil
Expand All @@ -102,7 +99,7 @@ func getProposalIDFromInput(bin *utils.Binary, args []string) (int, error) {
func upgradeLocalNode(bin *utils.Binary, targetVersion string) error {
currentHeight, err := utils.GetCurrentHeight(bin)
if err != nil {
return errors.Wrap(err, "Error getting current height")
return errors.Wrap(err, "error getting current height")
}

upgradeHeight := currentHeight + deltaHeight
Expand All @@ -111,14 +108,13 @@ func upgradeLocalNode(bin *utils.Binary, targetVersion string) error {

proposalID, err := gov.SubmitUpgradeProposal(bin, targetVersion, upgradeHeight)
if err != nil {
return errors.Wrap(err, "Error executing upgrade proposal")
return errors.Wrap(err, "error executing upgrade proposal")
}

log.Printf("Scheduled upgrade to %s at height %d.\n", targetVersion, upgradeHeight)

err = gov.SubmitAllVotesForProposal(bin, proposalID)
if err != nil {
return errors.Wrapf(err, "Error submitting votes for proposal %d", proposalID)
if err = gov.SubmitAllVotesForProposal(bin, proposalID); err != nil {
return errors.Wrapf(err, "error submitting votes for proposal %d", proposalID)
}

return nil
Expand Down

0 comments on commit 4f93ce5

Please sign in to comment.