Skip to content

Commit

Permalink
fix: upgrade Cosmos SDK app compatibility check (ignite#3523)
Browse files Browse the repository at this point in the history
* refactor: unify cosmos module path constant

* chore: add new COmos SDK versions to `comosover` pkg

* refactor: add assert for min supported Cosmos SDK version

* refactor: change scaffolder to assert minimum supported SDK version

* fix: correct SDK version error message

The format was improved for better readability and the warning icon
removed because it was not rendered properly.

* chore: add changelog entry

* fix: correct error string to pass CI

* feat: add chain option to check for supported Cosmos SDK version

* chore: change variable name for error message constant

* chore: correct error message
  • Loading branch information
jeronimoalbi authored Jun 5, 2023
1 parent 615c855 commit 379b4e7
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 31 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [#3497](https://github.com/ignite/cli/pull/3497) Use corret bank balance query url in faucet openapi
- [#3481](https://github.com/ignite/cli/pull/3481) Use correct checksum format in release checksum file
- [#3470](https://github.com/ignite/cli/pull/3470) Prevent overriding minimum-gas-prices with default value
- [#3523](https://github.com/ignite/cli/pull/3523) Upgrade Cosmos SDK compatibility check for scaffolded apps

## [`v0.26.1`](https://github.com/ignite/cli/releases/tag/v0.26.1)

Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/chain_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func chainBuildHandler(cmd *cobra.Command, _ []string) error {
chain.KeyringBackend(chaincmd.KeyringBackendTest),
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.CheckCosmosSDKVersion(),
}

if flagGetCheckDependencies(cmd) {
Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/chain_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func chainInitHandler(cmd *cobra.Command, _ []string) error {
chain.KeyringBackend(chaincmd.KeyringBackendTest),
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.CheckCosmosSDKVersion(),
}

if flagGetCheckDependencies(cmd) {
Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/chain_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func chainServe(cmd *cobra.Command, session *cliui.Session) error {
chainOption := []chain.Option{
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.CheckCosmosSDKVersion(),
}

if flagGetCheckDependencies(cmd) {
Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func generateGoHandler(cmd *cobra.Command, _ []string) error {
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.CheckCosmosSDKVersion(),
)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions ignite/pkg/cosmosanalysis/cosmosanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"github.com/pkg/errors"
"golang.org/x/mod/modfile"

"github.com/ignite/cli/ignite/pkg/cosmosver"
"github.com/ignite/cli/ignite/pkg/gomodule"
)

const (
cosmosModulePath = "github.com/cosmos/cosmos-sdk"
tendermintModulePath = "github.com/cometbft/cometbft"
appFileName = "app.go"
defaultAppFilePath = "app/" + appFileName
Expand Down Expand Up @@ -193,8 +193,8 @@ func IsChainPath(path string) error {
// ValidateGoMod check if the cosmos-sdk and the tendermint packages are imported.
func ValidateGoMod(module *modfile.File) error {
moduleCheck := map[string]bool{
cosmosModulePath: true,
tendermintModulePath: true,
cosmosver.CosmosModulePath: true,
tendermintModulePath: true,
}
for _, r := range module.Require {
delete(moduleCheck, r.Mod.Path)
Expand Down
6 changes: 3 additions & 3 deletions ignite/pkg/cosmosgen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/ignite/cli/ignite/pkg/cmdrunner"
"github.com/ignite/cli/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/ignite/pkg/cosmosver"
"github.com/ignite/cli/ignite/pkg/gomodule"
"github.com/ignite/cli/ignite/pkg/xfilepath"
)

const (
defaultSDKImport = "github.com/cosmos/cosmos-sdk"
moduleCacheNamespace = "generate.setup.module"
)

Expand Down Expand Up @@ -54,12 +54,12 @@ func (g *generator) setup() (err error) {
return err
}

g.sdkImport = defaultSDKImport
g.sdkImport = cosmosver.CosmosModulePath

// Check if the Cosmos SDK import path points to a different path
// and if so change the default one to the new location.
for _, r := range modFile.Replace {
if r.Old.Path == defaultSDKImport {
if r.Old.Path == cosmosver.CosmosModulePath {
g.sdkImport = r.New.Path
break
}
Expand Down
3 changes: 3 additions & 0 deletions ignite/pkg/cosmosver/cosmosver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ var (
StargateFortyVersion = newVersion("0.40.0")
StargateFortyFourVersion = newVersion("0.44.0-alpha")
StargateFortyFiveThreeVersion = newVersion("0.45.3")
StargateFortySevenTwoVersion = newVersion("0.47.2")
)

var (
// Versions is a list of known, sorted Cosmos-SDK versions.
Versions = []Version{
StargateFortyVersion,
StargateFortyFourVersion,
StargateFortyFiveThreeVersion,
StargateFortySevenTwoVersion,
}

// Latest is the latest known version of the Cosmos-SDK.
Expand Down
5 changes: 3 additions & 2 deletions ignite/pkg/cosmosver/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
)

const (
cosmosModulePath = "github.com/cosmos/cosmos-sdk"
// CosmosModulePath defines Cosmos SDK import path.
CosmosModulePath = "github.com/cosmos/cosmos-sdk"
)

// Detect detects major version of Cosmos.
Expand All @@ -18,7 +19,7 @@ func Detect(appPath string) (version Version, err error) {
for _, r := range parsed.Require {
v := r.Mod

if v.Path == cosmosModulePath {
if v.Path == CosmosModulePath {
if version, err = Parse(v.Version); err != nil {
return version, err
}
Expand Down
19 changes: 19 additions & 0 deletions ignite/services/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ignite/cli/ignite/pkg/repoversion"
"github.com/ignite/cli/ignite/pkg/xexec"
"github.com/ignite/cli/ignite/pkg/xurl"
igniteversion "github.com/ignite/cli/ignite/version"
)

var appBackendSourceWatchPaths = []string{
Expand Down Expand Up @@ -68,6 +69,10 @@ type chainOptions struct {
// been modified since they were downloaded.
checkDependencies bool

// checkCosmosSDKVersion checks that the app was scaffolded with version of
// the Cosmos SDK that is supported by Ignite CLI.
checkCosmosSDKVersion bool

// printGeneratedPaths prints the output paths of the generated code
printGeneratedPaths bool

Expand Down Expand Up @@ -129,6 +134,14 @@ func CheckDependencies() Option {
}
}

// CheckCosmosSDKVersion checks that the app was scaffolded with a version of
// the Cosmos SDK that is supported by Ignite CLI.
func CheckCosmosSDKVersion() Option {
return func(c *Chain) {
c.options.checkCosmosSDKVersion = true
}
}

// PrintGeneratedPaths prints the output paths of the generated code.
func PrintGeneratedPaths() Option {
return func(c *Chain) {
Expand Down Expand Up @@ -163,6 +176,12 @@ func New(path string, options ...Option) (*Chain, error) {
return nil, err
}

if c.options.checkCosmosSDKVersion {
if err := igniteversion.AssertSupportedCosmosSDKVersion(c.Version); err != nil {
return nil, err
}
}

return c, nil
}

Expand Down
32 changes: 10 additions & 22 deletions ignite/services/scaffolder/scaffolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package scaffolder

import (
"context"
"fmt"
"path/filepath"

chainconfig "github.com/ignite/cli/ignite/config/chain"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/ignite/cli/ignite/pkg/cosmosver"
"github.com/ignite/cli/ignite/pkg/gocmd"
"github.com/ignite/cli/ignite/pkg/gomodulepath"
"github.com/ignite/cli/ignite/version"
)

// Scaffolder is Ignite CLI app scaffolder.
Expand All @@ -30,44 +30,32 @@ type Scaffolder struct {

// New creates a new scaffold app.
func New(appPath string) (Scaffolder, error) {
sc, err := newScaffolder(appPath)
path, err := filepath.Abs(appPath)
if err != nil {
return sc, err
}

if sc.Version.LT(cosmosver.StargateFortyFourVersion) {
return sc, fmt.Errorf(
`⚠️ Your chain has been scaffolded with an old version of Cosmos SDK: %[1]v.
Please, follow the migration guide to upgrade your chain to the latest version:
https://docs.ignite.com/migration`, sc.Version.String(),
)
return Scaffolder{}, err
}
return sc, nil
}

// newScaffolder creates a new Scaffolder for an existent app.
func newScaffolder(path string) (Scaffolder, error) {
path, err := filepath.Abs(path)
modpath, path, err := gomodulepath.Find(path)
if err != nil {
return Scaffolder{}, err
}

modpath, path, err := gomodulepath.Find(path)
ver, err := cosmosver.Detect(path)
if err != nil {
return Scaffolder{}, err
}
if err := cosmosanalysis.IsChainPath(path); err != nil {

// Make sure that the app was scaffolded with a supported Cosmos SDK version
if err := version.AssertSupportedCosmosSDKVersion(ver); err != nil {
return Scaffolder{}, err
}

version, err := cosmosver.Detect(path)
if err != nil {
if err := cosmosanalysis.IsChainPath(path); err != nil {
return Scaffolder{}, err
}

s := Scaffolder{
Version: version,
Version: ver,
path: path,
modpath: modpath,
}
Expand Down
15 changes: 14 additions & 1 deletion ignite/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import (
chainconfig "github.com/ignite/cli/ignite/config/chain"
"github.com/ignite/cli/ignite/pkg/cmdrunner/exec"
"github.com/ignite/cli/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/ignite/pkg/cosmosver"
"github.com/ignite/cli/ignite/pkg/gitpod"
"github.com/ignite/cli/ignite/pkg/xexec"
)

const (
errOldCosmosSDKVersion = `your chain has been scaffolded with an older version of Cosmos SDK: %s
Please, follow the migration guide to upgrade your chain to the latest version at https://docs.ignite.com/migration`

versionDev = "development"
versionNightly = "nightly"
)
Expand Down Expand Up @@ -105,7 +110,7 @@ func Long(ctx context.Context) string {
)
if info, ok := debug.ReadBuildInfo(); ok {
for _, dep := range info.Deps {
if dep.Path == "github.com/cosmos/cosmos-sdk" {
if dep.Path == cosmosver.CosmosModulePath {
sdkVersion = dep.Version
break
}
Expand Down Expand Up @@ -181,3 +186,11 @@ func Long(ctx context.Context) string {

return b.String()
}

// AssertSupportedCosmosSDKVersion asserts that a Cosmos SDK version is supported by Ignite CLI.
func AssertSupportedCosmosSDKVersion(v cosmosver.Version) error {
if v.LT(cosmosver.StargateFortySevenTwoVersion) {
return fmt.Errorf(errOldCosmosSDKVersion, v)
}
return nil
}

0 comments on commit 379b4e7

Please sign in to comment.