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

chore: several minor cleanups and restructuring of packages #6

Merged
merged 7 commits into from
Aug 28, 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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

- [#2](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/2) Only vote if account has delegations
- [#3](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/3) Use broadcast mode `sync` instead of `block`
- [#6](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/6) Restructuring and refactoring
- [#4](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/4) Add GH actions and Makefile for testing
- [#3](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/3) Use broadcast mode `sync` instead of `block`
- [#2](https://github.com/MalteHerrmann/upgrade-local-node-go/pull/2) Only vote if account has delegations

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

Expand Down
43 changes: 20 additions & 23 deletions proposal.go → gov/proposal.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package main
package gov

import (
"fmt"
abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/evmos/evmos/v14/app"
"github.com/evmos/evmos/v14/encoding"
"strconv"
"strings"
)

var (
// cdc is the codec to be used for the client
cdc = encodingConfig.Codec
// encodingConfig specifies the encoding configuration to be used for the client
encodingConfig = encoding.MakeConfig(app.ModuleBasics)
"github.com/MalteHerrmann/upgrade-local-node-go/utils"
abcitypes "github.com/cometbft/cometbft/abci/types"
)

// submitUpgradeProposal submits a software upgrade proposal with the given target version and upgrade height.
func submitUpgradeProposal(targetVersion string, upgradeHeight int) (int, error) {
// SubmitUpgradeProposal submits a software upgrade proposal with the given target version and upgrade height.
func SubmitUpgradeProposal(targetVersion string, upgradeHeight int) (int, error) {
upgradeProposal := buildUpgradeProposalCommand(targetVersion, upgradeHeight)
out, err := executeShellCommand(upgradeProposal, evmosdHome, "dev0", true, false)
out, err := utils.ExecuteBinaryCmd(utils.BinaryCmdArgs{
Subcommand: upgradeProposal,
From: "dev0",
UseDefaults: true,
})
if err != nil {
return 0, err
}
Expand All @@ -29,9 +26,9 @@ func submitUpgradeProposal(targetVersion string, upgradeHeight int) (int, error)
lines := strings.Split(out, "\n")
out = lines[len(lines)-1] // last line is json output

events, err := getTxEvents(out)
events, err := utils.GetTxEvents(out)
if err != nil {
panic(err)
return 0, fmt.Errorf("error getting tx events: %w", err)
}

return getProposalID(events)
Expand Down Expand Up @@ -71,13 +68,13 @@ func buildUpgradeProposalCommand(targetVersion string, upgradeHeight int) []stri
}
}

// voteForProposal votes for the proposal with the given ID using the given account.
func voteForProposal(proposalID int, sender string) error {
_, err := executeShellCommand(
[]string{"tx", "gov", "vote", fmt.Sprintf("%d", proposalID), "yes"},
evmosdHome,
sender,
true, true,
)
// VoteForProposal votes for the proposal with the given ID using the given account.
func VoteForProposal(proposalID int, sender string) error {
_, err := utils.ExecuteBinaryCmd(utils.BinaryCmdArgs{
Subcommand: []string{"tx", "gov", "vote", fmt.Sprintf("%d", proposalID), "yes"},
From: sender,
UseDefaults: true,
Quiet: true,
})
return err
}
2 changes: 1 addition & 1 deletion proposal_test.go → gov/proposal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gov

import (
"testing"
Expand Down
53 changes: 16 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,17 @@ import (
"os"
"regexp"
"time"
)

const (
// The chain ID of the node that will be upgraded.
chainID = "evmos_9000-1"
// The amount of blocks in the future that the upgrade will be scheduled.
deltaHeight = 15
// The amount of fees to be sent with a default transaction.
defaultFees int = 1e18 // 1 aevmos
// The denomination used for the local node.
denom = "aevmos"
"github.com/MalteHerrmann/upgrade-local-node-go/gov"
"github.com/MalteHerrmann/upgrade-local-node-go/utils"
)

// evmosdHome is the home directory of the local node.
var evmosdHome string

var (
// The default flags that will be used for all commands related to governance.
defaultFlags = []string{
"--chain-id", chainID,
"--keyring-backend", "test",
"--gas", "auto",
"--fees", fmt.Sprintf("%d%s", defaultFees, denom),
"--gas-adjustment", "1.3",
"-b", "sync",
"-y",
}
)
// The amount of blocks in the future that the upgrade will be scheduled.
const deltaHeight = 15

func main() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Error getting home directory: %v", err)
}
evmosdHome = fmt.Sprintf("%s/.tmp-evmosd", homeDir)

if len(os.Args) < 2 {
fmt.Println("Usage: go run upgrade-local-node.go <target_version>")
fmt.Println("Usage: upgrade-local-node-go <target_version>")
os.Exit(1)
}

Expand All @@ -59,27 +32,33 @@ func main() {
// upgradeLocalNode prepares upgrading the local node to the target version
// by submitting the upgrade proposal and voting on it using all testing accounts.
func upgradeLocalNode(targetVersion string) {
currentHeight, err := getCurrentHeight()
currentHeight, err := utils.GetCurrentHeight()
if err != nil {
log.Fatalf("Error getting current height: %v", err)
}

upgradeHeight := currentHeight + deltaHeight
fmt.Println("Submitting upgrade proposal...")
proposalID, err := submitUpgradeProposal(targetVersion, upgradeHeight)
proposalID, err := gov.SubmitUpgradeProposal(targetVersion, upgradeHeight)
if err != nil {
log.Fatalf("Error executing upgrade proposal: %v", err)
}
fmt.Printf("Scheduled upgrade to %s at height %d.\n", targetVersion, upgradeHeight)

availableKeys, err := getAccounts()
availableAccounts, err := utils.GetAccounts()
if err != nil {
log.Fatalf("Error getting available keys: %v", err)
}

accsWithDelegations, err := utils.FilterAccountsWithDelegations(availableAccounts)
if err != nil {
log.Fatalf("Error filtering accounts: %v", err)
}
wait(1)

fmt.Println("Voting for upgrade...")
for _, acc := range availableKeys {
if err = voteForProposal(proposalID, acc.Name); err != nil {
for _, acc := range accsWithDelegations {
if err = gov.VoteForProposal(proposalID, acc.Name); err != nil {
fmt.Printf(" - could NOT vote using key: %s\n", acc.Name)
} else {
fmt.Printf(" - voted using key: %s\n", acc.Name)
Expand Down
36 changes: 36 additions & 0 deletions utils/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"fmt"

"github.com/evmos/evmos/v14/app"
"github.com/evmos/evmos/v14/encoding"
evmosutils "github.com/evmos/evmos/v14/utils"
)

const (
// The amount of fees to be sent with a default transaction.
defaultFees int = 1e18 // 1 aevmos
// The denomination used for the local node.
denom = evmosutils.BaseDenom
)

var (
// cdc is the codec to be used for the client
cdc = encodingConfig.Codec
// encodingConfig specifies the encoding configuration to be used for the client
encodingConfig = encoding.MakeConfig(app.ModuleBasics)

// The chain ID of the node that will be upgraded.
chainID = evmosutils.TestnetChainID + "-1"
// defaultFlags are the default flags to be used for the client.
defaultFlags = []string{
"--chain-id", chainID,
"--keyring-backend", "test",
"--gas", "auto",
"--fees", fmt.Sprintf("%d%s", defaultFees, denom),
"--gas-adjustment", "1.3",
"-b", "sync",
"-y",
}
)
22 changes: 13 additions & 9 deletions keys.go → utils/keys.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"encoding/json"
Expand All @@ -17,9 +17,11 @@ type Account struct {
Delegations []stakingtypes.Delegation `json:"delegations"`
}

// getAccounts returns the list of keys from the current running local node
func getAccounts() ([]Account, error) {
out, err := executeShellCommand([]string{"keys", "list", "--output=json"}, evmosdHome, "", false, false)
// GetAccounts returns the list of keys from the current running local node
func GetAccounts() ([]Account, error) {
out, err := ExecuteBinaryCmd(BinaryCmdArgs{
Subcommand: []string{"keys", "list", "--output=json"},
})
if err != nil {
return nil, err
}
Expand All @@ -29,15 +31,17 @@ func getAccounts() ([]Account, error) {
return nil, err
}

return stakingAccounts(accounts)
return accounts, nil
}

// stakingAccounts filters the given list of accounts for those, which are used for staking.
func stakingAccounts(accounts []Account) ([]Account, error) {
// FilterAccountsWithDelegations filters the given list of accounts for those, which are used for staking.
func FilterAccountsWithDelegations(accounts []Account) ([]Account, error) {
var stakingAccs []Account

for _, acc := range accounts {
out, err := executeShellCommand([]string{"query", "staking", "delegations", acc.Address, "--output=json"}, evmosdHome, "", false, false)
out, err := ExecuteBinaryCmd(BinaryCmdArgs{
Subcommand: []string{"query", "staking", "delegations", acc.Address, "--output=json"},
})
if err != nil {
return nil, err
}
Expand All @@ -64,7 +68,7 @@ func parseDelegationsFromResponse(out string) ([]stakingtypes.Delegation, error)
return nil, fmt.Errorf("error unmarshalling delegations: %w", err)
}

var delegations = make([]stakingtypes.Delegation, len(res.DelegationResponses))
delegations := make([]stakingtypes.Delegation, len(res.DelegationResponses))
for i, delegation := range res.DelegationResponses {
delegations[i] = delegation.Delegation
}
Expand Down
2 changes: 1 addition & 1 deletion keys_test.go → utils/keys_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"testing"
Expand Down
Loading
Loading