Skip to content

Commit 53e144f

Browse files
chore: several minor cleanups and restructuring of packages (#6)
* cleanup and restructure * add changelog entry * more cleanup * update usage comment * update usage comment * fix behavior when trying to query tx that failed * remove unused struct
1 parent 6e0470b commit 53e144f

File tree

9 files changed

+160
-104
lines changed

9 files changed

+160
-104
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939

4040
### Improvements
4141

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

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

proposal.go renamed to gov/proposal.go

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
package main
1+
package gov
22

33
import (
44
"fmt"
5-
abcitypes "github.com/cometbft/cometbft/abci/types"
6-
"github.com/evmos/evmos/v14/app"
7-
"github.com/evmos/evmos/v14/encoding"
85
"strconv"
96
"strings"
10-
)
117

12-
var (
13-
// cdc is the codec to be used for the client
14-
cdc = encodingConfig.Codec
15-
// encodingConfig specifies the encoding configuration to be used for the client
16-
encodingConfig = encoding.MakeConfig(app.ModuleBasics)
8+
"github.com/MalteHerrmann/upgrade-local-node-go/utils"
9+
abcitypes "github.com/cometbft/cometbft/abci/types"
1710
)
1811

19-
// submitUpgradeProposal submits a software upgrade proposal with the given target version and upgrade height.
20-
func submitUpgradeProposal(targetVersion string, upgradeHeight int) (int, error) {
12+
// SubmitUpgradeProposal submits a software upgrade proposal with the given target version and upgrade height.
13+
func SubmitUpgradeProposal(targetVersion string, upgradeHeight int) (int, error) {
2114
upgradeProposal := buildUpgradeProposalCommand(targetVersion, upgradeHeight)
22-
out, err := executeShellCommand(upgradeProposal, evmosdHome, "dev0", true, false)
15+
out, err := utils.ExecuteBinaryCmd(utils.BinaryCmdArgs{
16+
Subcommand: upgradeProposal,
17+
From: "dev0",
18+
UseDefaults: true,
19+
})
2320
if err != nil {
2421
return 0, err
2522
}
@@ -29,9 +26,9 @@ func submitUpgradeProposal(targetVersion string, upgradeHeight int) (int, error)
2926
lines := strings.Split(out, "\n")
3027
out = lines[len(lines)-1] // last line is json output
3128

32-
events, err := getTxEvents(out)
29+
events, err := utils.GetTxEvents(out)
3330
if err != nil {
34-
panic(err)
31+
return 0, fmt.Errorf("error getting tx events: %w", err)
3532
}
3633

3734
return getProposalID(events)
@@ -71,13 +68,13 @@ func buildUpgradeProposalCommand(targetVersion string, upgradeHeight int) []stri
7168
}
7269
}
7370

74-
// voteForProposal votes for the proposal with the given ID using the given account.
75-
func voteForProposal(proposalID int, sender string) error {
76-
_, err := executeShellCommand(
77-
[]string{"tx", "gov", "vote", fmt.Sprintf("%d", proposalID), "yes"},
78-
evmosdHome,
79-
sender,
80-
true, true,
81-
)
71+
// VoteForProposal votes for the proposal with the given ID using the given account.
72+
func VoteForProposal(proposalID int, sender string) error {
73+
_, err := utils.ExecuteBinaryCmd(utils.BinaryCmdArgs{
74+
Subcommand: []string{"tx", "gov", "vote", fmt.Sprintf("%d", proposalID), "yes"},
75+
From: sender,
76+
UseDefaults: true,
77+
Quiet: true,
78+
})
8279
return err
8380
}

proposal_test.go renamed to gov/proposal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package gov
22

33
import (
44
"testing"

main.go

+16-37
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,17 @@ import (
66
"os"
77
"regexp"
88
"time"
9-
)
109

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

22-
// evmosdHome is the home directory of the local node.
23-
var evmosdHome string
24-
25-
var (
26-
// The default flags that will be used for all commands related to governance.
27-
defaultFlags = []string{
28-
"--chain-id", chainID,
29-
"--keyring-backend", "test",
30-
"--gas", "auto",
31-
"--fees", fmt.Sprintf("%d%s", defaultFees, denom),
32-
"--gas-adjustment", "1.3",
33-
"-b", "sync",
34-
"-y",
35-
}
36-
)
14+
// The amount of blocks in the future that the upgrade will be scheduled.
15+
const deltaHeight = 15
3716

3817
func main() {
39-
homeDir, err := os.UserHomeDir()
40-
if err != nil {
41-
log.Fatalf("Error getting home directory: %v", err)
42-
}
43-
evmosdHome = fmt.Sprintf("%s/.tmp-evmosd", homeDir)
44-
4518
if len(os.Args) < 2 {
46-
fmt.Println("Usage: go run upgrade-local-node.go <target_version>")
19+
fmt.Println("Usage: upgrade-local-node-go <target_version>")
4720
os.Exit(1)
4821
}
4922

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

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

75-
availableKeys, err := getAccounts()
48+
availableAccounts, err := utils.GetAccounts()
7649
if err != nil {
7750
log.Fatalf("Error getting available keys: %v", err)
7851
}
52+
53+
accsWithDelegations, err := utils.FilterAccountsWithDelegations(availableAccounts)
54+
if err != nil {
55+
log.Fatalf("Error filtering accounts: %v", err)
56+
}
7957
wait(1)
58+
8059
fmt.Println("Voting for upgrade...")
81-
for _, acc := range availableKeys {
82-
if err = voteForProposal(proposalID, acc.Name); err != nil {
60+
for _, acc := range accsWithDelegations {
61+
if err = gov.VoteForProposal(proposalID, acc.Name); err != nil {
8362
fmt.Printf(" - could NOT vote using key: %s\n", acc.Name)
8463
} else {
8564
fmt.Printf(" - voted using key: %s\n", acc.Name)

utils/constants.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/evmos/evmos/v14/app"
7+
"github.com/evmos/evmos/v14/encoding"
8+
evmosutils "github.com/evmos/evmos/v14/utils"
9+
)
10+
11+
const (
12+
// The amount of fees to be sent with a default transaction.
13+
defaultFees int = 1e18 // 1 aevmos
14+
// The denomination used for the local node.
15+
denom = evmosutils.BaseDenom
16+
)
17+
18+
var (
19+
// cdc is the codec to be used for the client
20+
cdc = encodingConfig.Codec
21+
// encodingConfig specifies the encoding configuration to be used for the client
22+
encodingConfig = encoding.MakeConfig(app.ModuleBasics)
23+
24+
// The chain ID of the node that will be upgraded.
25+
chainID = evmosutils.TestnetChainID + "-1"
26+
// defaultFlags are the default flags to be used for the client.
27+
defaultFlags = []string{
28+
"--chain-id", chainID,
29+
"--keyring-backend", "test",
30+
"--gas", "auto",
31+
"--fees", fmt.Sprintf("%d%s", defaultFees, denom),
32+
"--gas-adjustment", "1.3",
33+
"-b", "sync",
34+
"-y",
35+
}
36+
)

keys.go renamed to utils/keys.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package utils
22

33
import (
44
"encoding/json"
@@ -17,9 +17,11 @@ type Account struct {
1717
Delegations []stakingtypes.Delegation `json:"delegations"`
1818
}
1919

20-
// getAccounts returns the list of keys from the current running local node
21-
func getAccounts() ([]Account, error) {
22-
out, err := executeShellCommand([]string{"keys", "list", "--output=json"}, evmosdHome, "", false, false)
20+
// GetAccounts returns the list of keys from the current running local node
21+
func GetAccounts() ([]Account, error) {
22+
out, err := ExecuteBinaryCmd(BinaryCmdArgs{
23+
Subcommand: []string{"keys", "list", "--output=json"},
24+
})
2325
if err != nil {
2426
return nil, err
2527
}
@@ -29,15 +31,17 @@ func getAccounts() ([]Account, error) {
2931
return nil, err
3032
}
3133

32-
return stakingAccounts(accounts)
34+
return accounts, nil
3335
}
3436

35-
// stakingAccounts filters the given list of accounts for those, which are used for staking.
36-
func stakingAccounts(accounts []Account) ([]Account, error) {
37+
// FilterAccountsWithDelegations filters the given list of accounts for those, which are used for staking.
38+
func FilterAccountsWithDelegations(accounts []Account) ([]Account, error) {
3739
var stakingAccs []Account
3840

3941
for _, acc := range accounts {
40-
out, err := executeShellCommand([]string{"query", "staking", "delegations", acc.Address, "--output=json"}, evmosdHome, "", false, false)
42+
out, err := ExecuteBinaryCmd(BinaryCmdArgs{
43+
Subcommand: []string{"query", "staking", "delegations", acc.Address, "--output=json"},
44+
})
4145
if err != nil {
4246
return nil, err
4347
}
@@ -64,7 +68,7 @@ func parseDelegationsFromResponse(out string) ([]stakingtypes.Delegation, error)
6468
return nil, fmt.Errorf("error unmarshalling delegations: %w", err)
6569
}
6670

67-
var delegations = make([]stakingtypes.Delegation, len(res.DelegationResponses))
71+
delegations := make([]stakingtypes.Delegation, len(res.DelegationResponses))
6872
for i, delegation := range res.DelegationResponses {
6973
delegations[i] = delegation.Delegation
7074
}

keys_test.go renamed to utils/keys_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package utils
22

33
import (
44
"testing"

0 commit comments

Comments
 (0)