Skip to content

Commit

Permalink
feat(cosmovisor): extend add upgrade to support non governance upgrade (
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jul 3, 2023
1 parent 9b237c7 commit c6d5b7c
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 103 deletions.
2 changes: 1 addition & 1 deletion tools/cosmovisor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Features

* [#16413](https://github.com/cosmos/cosmos-sdk/issues/16413) Add `cosmovisor pre-upgrade` command to manually add an upgrade to cosmovisor.
* [#16573](https://github.com/cosmos/cosmos-sdk/pull/16573) Extend `cosmovisor` configuration with new log format options
* [#16550](https://github.com/cosmos/cosmos-sdk/pull/16550) Add COSMOVISOR_CUSTOM_PREUPGRADE to cosmovisor to execute custom pre-upgrade scripts (separate from daemon pre-upgrade).
* [#15361](https://github.com/cosmos/cosmos-sdk/pull/15361) Add `cosmovisor config` command to display the configuration used by cosmovisor.
* [#12457](https://github.com/cosmos/cosmos-sdk/issues/12457) Add `cosmovisor pre-upgrade` command to manually add an upgrade to cosmovisor.

## Improvements

Expand Down
6 changes: 3 additions & 3 deletions tools/cosmovisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ To install the latest version of `cosmovisor`, run the following command:
go install cosmossdk.io/tools/cosmovisor/cmd/cosmovisor@latest
```

To install a previous version, you can specify the version. IMPORTANT: Chains that use Cosmos SDK v0.44.3 or earlier (eg v0.44.2) and want to use auto-download feature MUST use `cosmovisor v0.1.0`
To install a previous version, you can specify the version:

```shell
go install github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor@v0.1.0
go install github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor@v1.5.0
```

Run `cosmovisor version` to check the cosmovisor version.
Expand Down Expand Up @@ -367,7 +367,7 @@ Open a new terminal window and submit an upgrade proposal along with a deposit a
**>= v0.50+**:

```shell
./build/simd tx upgrade software-upgrade test1 --title upgrade --summary upgrade --upgrade-height 200 --from validator --yes
./build/simd tx upgrade software-upgrade test1 --title upgrade --summary upgrade --upgrade-height 200 --upgrade-info "{}" --no-validate --from validator --yes
./build/simd tx gov deposit 1 10000000stake --from validator --yes
./build/simd tx gov vote 1 yes --from validator --yes
```
Expand Down
9 changes: 2 additions & 7 deletions tools/cosmovisor/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"strings"
"time"

"github.com/rs/zerolog"

"cosmossdk.io/log"
"cosmossdk.io/x/upgrade/plan"
upgradetypes "cosmossdk.io/x/upgrade/types"
Expand Down Expand Up @@ -44,9 +42,6 @@ const (
currentLink = "current"
)

// must be the same as x/upgrade/types.UpgradeInfoFilename
const defaultFilename = "upgrade-info.json"

// Config is the information passed in to control the daemon
type Config struct {
Home string
Expand Down Expand Up @@ -96,7 +91,7 @@ func (cfg *Config) BaseUpgradeDir() string {

// UpgradeInfoFilePath is the expected upgrade-info filename created by `x/upgrade/keeper`.
func (cfg *Config) UpgradeInfoFilePath() string {
return filepath.Join(cfg.Home, "data", defaultFilename)
return filepath.Join(cfg.Home, "data", upgradetypes.UpgradeInfoFilename)
}

// SymLinkToGenesis creates a symbolic link from "./current" to the genesis directory.
Expand Down Expand Up @@ -224,7 +219,7 @@ func (cfg *Config) Logger(dst io.Writer) log.Logger {
var logger log.Logger

if cfg.DisableLogs {
logger = log.NewCustomLogger(zerolog.Nop())
logger = log.NewNopLogger()
} else {
logger = log.NewLogger(dst,
log.ColorOption(cfg.ColorLogs),
Expand Down
59 changes: 48 additions & 11 deletions tools/cosmovisor/cmd/cosmovisor/add_upgrade.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path"
"strings"

"github.com/spf13/cobra"

"cosmossdk.io/tools/cosmovisor"
upgradetypes "cosmossdk.io/x/upgrade/types"
)

func NewAddUpgradeCmd() *cobra.Command {
Expand All @@ -19,7 +22,8 @@ func NewAddUpgradeCmd() *cobra.Command {
RunE: AddUpgrade,
}

addUpgrade.Flags().Bool(cosmovisor.FlagForce, false, "overwrite existing upgrade binary")
addUpgrade.Flags().Bool(cosmovisor.FlagForce, false, "overwrite existing upgrade binary / upgrade-info.json file")
addUpgrade.Flags().Int64(cosmovisor.FlagUpgradeHeight, 0, "define a height at which to upgrade the binary automatically (without governance proposal)")

return addUpgrade
}
Expand All @@ -33,7 +37,7 @@ func AddUpgrade(cmd *cobra.Command, args []string) error {

logger := cfg.Logger(os.Stdout)

upgradeName := args[0]
upgradeName := strings.ToLower(args[0])
if len(upgradeName) == 0 {
return fmt.Errorf("upgrade name cannot be empty")
}
Expand All @@ -59,22 +63,55 @@ func AddUpgrade(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to read binary: %w", err)
}

if _, err := os.Stat(cfg.UpgradeBin(upgradeName)); err == nil {
if force, _ := cmd.Flags().GetBool(cosmovisor.FlagForce); !force {
return fmt.Errorf("upgrade binary already exists at %s", cfg.UpgradeBin(upgradeName))
force, err := cmd.Flags().GetBool(cosmovisor.FlagForce)
if err != nil {
return fmt.Errorf("failed to get force flag: %w", err)
}

if err := saveOrAbort(cfg.UpgradeBin(upgradeName), executableData, force); err != nil {
return err
}

logger.Info(fmt.Sprintf("Using %s for %s upgrade", executablePath, upgradeName))
logger.Info(fmt.Sprintf("Upgrade binary located at %s", cfg.UpgradeBin(upgradeName)))

if upgradeHeight, err := cmd.Flags().GetInt64(cosmovisor.FlagUpgradeHeight); err != nil {
return fmt.Errorf("failed to get upgrade-height flag: %w", err)
} else if upgradeHeight > 0 {
plan := upgradetypes.Plan{Name: upgradeName, Height: upgradeHeight}
if err := plan.ValidateBasic(); err != nil {
panic(fmt.Errorf("something is wrong with cosmovisor: %w", err))
}

// create upgrade-info.json file
planData, err := json.Marshal(plan)
if err != nil {
return fmt.Errorf("failed to marshal upgrade plan: %w", err)
}

logger.Info(fmt.Sprintf("Overwriting %s for %s upgrade", executablePath, upgradeName))
if err := saveOrAbort(cfg.UpgradeInfoFilePath(), planData, force); err != nil {
return err
}

logger.Info(fmt.Sprintf("%s created, %s upgrade binary will switch at height %d", upgradetypes.UpgradeInfoFilename, upgradeName, upgradeHeight))
}

return nil
}

// saveOrAbort saves data to path or aborts if file exists and force is false
func saveOrAbort(path string, data []byte, force bool) error {
if _, err := os.Stat(path); err == nil {
if !force {
return fmt.Errorf("file already exists at %s", path)
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("failed to check if upgrade binary exists: %w", err)
return fmt.Errorf("failed to check if file exists: %w", err)
}

if err := os.WriteFile(cfg.UpgradeBin(upgradeName), executableData, 0o600); err != nil {
if err := os.WriteFile(path, data, 0o600); err != nil {
return fmt.Errorf("failed to write binary to location: %w", err)
}

logger.Info(fmt.Sprintf("Using %s for %s upgrade", executablePath, upgradeName))
logger.Info(fmt.Sprintf("Upgrade binary located at %s", cfg.UpgradeBin(upgradeName)))

return nil
}
7 changes: 4 additions & 3 deletions tools/cosmovisor/cmd/cosmovisor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
)

var initCmd = &cobra.Command{
Use: "init <path to executable>",
Short: "Initialize a cosmovisor daemon home directory.",
Args: cobra.ExactArgs(1),
Use: "init <path to executable>",
Short: "Initialize a cosmovisor daemon home directory.",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return InitializeCosmovisor(nil, args)
},
Expand Down
4 changes: 1 addition & 3 deletions tools/cosmovisor/cmd/cosmovisor/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -244,8 +243,7 @@ func (p *BufferedPipe) panicIfStarted(msg string) {
func (s *InitTestSuite) NewCapturingLogger() (*BufferedPipe, log.Logger) {
bufferedStdOut, err := StartNewBufferedPipe("stdout", os.Stdout)
s.Require().NoError(err, "creating stdout buffered pipe")
output := zerolog.ConsoleWriter{Out: bufferedStdOut, TimeFormat: time.RFC3339Nano}
logger := log.NewCustomLogger(zerolog.New(output).With().Str("module", "cosmovisor").Timestamp().Logger())
logger := log.NewLogger(bufferedStdOut, log.ColorOption(false), log.TimeFormatOption(time.RFC3339Nano)).With(log.ModuleKey, "cosmovisor")
return &bufferedStdOut, logger
}

Expand Down
9 changes: 5 additions & 4 deletions tools/cosmovisor/cmd/cosmovisor/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (

func NewVersionCmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Display cosmovisor and APP version.",
Use: "version",
Short: "Display cosmovisor and APP version.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
noAppVersion, _ := cmd.Flags().GetBool(cosmovisor.FlagNoAppVersion)
noAppVersion, _ := cmd.Flags().GetBool(cosmovisor.FlagCosmovisorOnly)
if val, err := cmd.Flags().GetString(cosmovisor.FlagOutput); val == "json" && err == nil {
return printVersionJSON(cmd, args, noAppVersion)
}
Expand All @@ -26,7 +27,7 @@ func NewVersionCmd() *cobra.Command {
}

versionCmd.Flags().StringP(cosmovisor.FlagOutput, "o", "text", "Output format (text|json)")
versionCmd.Flags().Bool(cosmovisor.FlagNoAppVersion, false, "Don't print APP version")
versionCmd.Flags().Bool(cosmovisor.FlagCosmovisorOnly, false, "Print cosmovisor version only")

return versionCmd
}
Expand Down
3 changes: 2 additions & 1 deletion tools/cosmovisor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cosmovisor
const (
FlagOutput = "output"
FlagSkipUpgradeHeight = "unsafe-skip-upgrades"
FlagNoAppVersion = "no-app-version"
FlagCosmovisorOnly = "cosmovisor-only"
FlagForce = "force"
FlagUpgradeHeight = "upgrade-height"
)
2 changes: 1 addition & 1 deletion tools/cosmovisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
cosmossdk.io/log v1.1.0
cosmossdk.io/x/upgrade v0.0.0-20230614103911-b3da8bb4e801
github.com/otiai10/copy v1.12.0
github.com/rs/zerolog v1.29.1
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
)
Expand Down Expand Up @@ -133,6 +132,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
Expand Down
Loading

0 comments on commit c6d5b7c

Please sign in to comment.