Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add option to only deposit #26

Merged
merged 6 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

## Unreleased

### Features

- [#26](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/26) Enable just depositing with the binary

## [v0.3.0](https://github.com/MalteHerrmann/upgrade-local-node-go/releases/tag/v0.3.0) - 2023-08-30

### Features
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/MalteHerrmann/upgrade-local-node-go
go 1.21

require (
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.4
github.com/evmos/evmos/v14 v14.0.0-rc3
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -43,6 +42,7 @@ require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/coinbase/rosetta-sdk-go/types v1.0.0 // indirect
github.com/cometbft/cometbft v0.37.2 // indirect
github.com/cometbft/cometbft-db v0.8.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
Expand Down
68 changes: 68 additions & 0 deletions gov/deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package gov

import (
"fmt"
"regexp"
"strconv"

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

// DepositForProposal deposits the given amount for the proposal with the given proposalID
// from the given account.
func DepositForProposal(bin *utils.Binary, proposalID int, sender, deposit string) (string, error) {
out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{
Subcommand: []string{
"tx", "gov", "deposit", strconv.Itoa(proposalID), deposit,
},
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
}

// GetMinDeposit returns the minimum deposit necessary for a proposal from the governance parameters of
// the running chain.
func GetMinDeposit(bin *utils.Binary) (sdk.Coins, error) {
out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{
Subcommand: []string{"q", "gov", "param", "deposit", "--output=json"},
Quiet: true,
})
if err != nil {
return sdk.Coins{}, errors.Wrap(err, "failed to query governance parameters")
}

return ParseMinDepositFromResponse(out)
}

// ParseMinDepositFromResponse parses the minimum deposit from the given output of the governance
// parameters query.
//
// FIXME: It wasn't possible to unmarshal the JSON output of the query because of a missing unit in the max_deposit_period

Check failure on line 48 in gov/deposit.go

View workflow job for this annotation

GitHub Actions / lint

gov/deposit.go:48: Line contains TODO/BUG/FIXME: "FIXME: It wasn't possible to unmarshal t..." (godox)
// parameter. This should rather be done using GRPC.
func ParseMinDepositFromResponse(out string) (sdk.Coins, error) {
// FIXME: This is a workaround for the missing unit in the max_deposit_period parameter. Should be done with gRPC.

Check failure on line 51 in gov/deposit.go

View workflow job for this annotation

GitHub Actions / lint

gov/deposit.go:51: Line contains TODO/BUG/FIXME: "FIXME: This is a workaround for the miss..." (godox)
depositPatternRaw := `min_deposit":\[{"denom":"(\w+)","amount":"(\d+)`
depositPattern := regexp.MustCompile(depositPatternRaw)

minDepositMatch := depositPattern.FindStringSubmatch(out)
if len(minDepositMatch) == 0 {
return sdk.Coins{}, fmt.Errorf("failed to find min deposit in params output: %q", out)
}

minDepositDenom := minDepositMatch[1]

minDepositAmount, err := strconv.Atoi(minDepositMatch[2])
if err != nil {
return sdk.Coins{}, fmt.Errorf("failed to find min deposit in params output: %q", out)
}

return sdk.Coins{sdk.NewInt64Coin(minDepositDenom, int64(minDepositAmount))}, nil
}
49 changes: 49 additions & 0 deletions gov/deposit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gov_test

import (
"testing"

"github.com/MalteHerrmann/upgrade-local-node-go/gov"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestParseMinDepositFromResponse(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
out string
expMinDeposit sdk.Coins
expError bool
errContains string
}{
{
name: "pass",
out: `{"min_deposit":[{"denom":"aevmos","amount":"10000000"}],"max_deposit_period":"30000000000"}`,
expMinDeposit: sdk.Coins{sdk.NewInt64Coin("aevmos", 10000000)},
},
{
name: "fail - no min deposit",
out: "invalid output",
expError: true,
errContains: "failed to find min deposit in params output",
},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

minDeposit, err := gov.ParseMinDepositFromResponse(tc.out)
if tc.expError {
require.Error(t, err, "expected error parsing min deposit")
require.ErrorContains(t, err, tc.errContains, "expected different error")
} else {
require.NoError(t, err, "unexpected error parsing min deposit")
require.Equal(t, tc.expMinDeposit, minDeposit, "expected different min deposit")
}
})
}
}
54 changes: 1 addition & 53 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 All @@ -18,7 +17,7 @@ func buildUpgradeProposalCommand(targetVersion string, upgradeHeight int) []stri
"tx", "gov", "submit-legacy-proposal", "software-upgrade", targetVersion,
"--title", fmt.Sprintf("'Upgrade to %s'", targetVersion),
"--description", fmt.Sprintf("'Upgrade to %s'", targetVersion),
"--upgrade-height", fmt.Sprintf("%d", upgradeHeight),
"--upgrade-height", strconv.Itoa(upgradeHeight),
"--deposit", "100000000000000000000aevmos",
"--output", "json",
"--no-validate",
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,18 +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", fmt.Sprintf("%d", 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
}
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
}
Loading
Loading