From fa530a4da351674a68ec1ebeccc95fb0d3fd61b0 Mon Sep 17 00:00:00 2001 From: jeronimoalbi Date: Fri, 14 Oct 2022 18:02:28 +0200 Subject: [PATCH 1/5] fix: network commands check for latest config version before build --- changelog.md | 1 + ignite/chainconfig/chainconfig.go | 20 +++++++++++++++++++ ignite/chainconfig/config/config.go | 5 +++++ ignite/chainconfig/errors.go | 15 +++++++++++--- .../network/networkchain/networkchain.go | 16 +++++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index fe92419fba..d869682dbf 100644 --- a/changelog.md +++ b/changelog.md @@ -30,6 +30,7 @@ - Change faucet to allow C.O.R.S. preflight requests. - Fix config file migration to void leaving end of file content chunks - Handle "No records were found in keyring" message when checking keys. +- Network commands check for latest config version before building the chain binary. ### Features diff --git a/ignite/chainconfig/chainconfig.go b/ignite/chainconfig/chainconfig.go index f38ce62e60..326e195a2c 100644 --- a/ignite/chainconfig/chainconfig.go +++ b/ignite/chainconfig/chainconfig.go @@ -87,3 +87,23 @@ func LocateDefault(root string) (path string, err error) { return "", ErrConfigNotFound } + +// CheckVersion checks that the config version is the latest +// and if not a VersionError is returned. +func CheckVersion(configPath string) error { + file, err := os.Open(configPath) + if err != nil { + return err + } + + version, err := ReadConfigVersion(file) + if err != nil { + return err + } + + if version != LatestVersion { + return VersionError{version} + } + + return nil +} diff --git a/ignite/chainconfig/config/config.go b/ignite/chainconfig/config/config.go index 2cfc60ca3f..3ade1644b2 100644 --- a/ignite/chainconfig/config/config.go +++ b/ignite/chainconfig/config/config.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "io" "github.com/imdario/mergo" @@ -11,6 +12,10 @@ import ( // Version defines the type for the config version number. type Version uint +func (v Version) String() string { + return fmt.Sprintf("v%d", v) +} + // Converter defines the interface required to migrate configurations to newer versions. type Converter interface { // Clone clones the config by returning a new copy of the current one. diff --git a/ignite/chainconfig/errors.go b/ignite/chainconfig/errors.go index 792cec9fa0..642596acf0 100644 --- a/ignite/chainconfig/errors.go +++ b/ignite/chainconfig/errors.go @@ -15,7 +15,7 @@ type ValidationError struct { Message string } -func (e *ValidationError) Error() string { +func (e ValidationError) Error() string { return fmt.Sprintf("config is not valid: %s", e.Message) } @@ -24,6 +24,15 @@ type UnsupportedVersionError struct { Version config.Version } -func (e *UnsupportedVersionError) Error() string { - return fmt.Sprintf("config version %d is not supported", e.Version) +func (e UnsupportedVersionError) Error() string { + return fmt.Sprintf("config version %s is not supported", e.Version) +} + +// VersionError is returned when config version is not the latest. +type VersionError struct { + Version config.Version +} + +func (e VersionError) Error() string { + return fmt.Sprintf("config version %s is required and the current version is %s", LatestVersion, e.Version) } diff --git a/ignite/services/network/networkchain/networkchain.go b/ignite/services/network/networkchain/networkchain.go index 72d6369af1..d83d73742e 100644 --- a/ignite/services/network/networkchain/networkchain.go +++ b/ignite/services/network/networkchain/networkchain.go @@ -11,6 +11,7 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + "github.com/ignite/cli/ignite/chainconfig" sperrors "github.com/ignite/cli/ignite/errors" "github.com/ignite/cli/ignite/pkg/cache" "github.com/ignite/cli/ignite/pkg/chaincmd" @@ -279,8 +280,23 @@ func (c Chain) NodeID(ctx context.Context) (string, error) { return nodeID, nil } +// CheckConfigVersion checks that the config version is the latest. +func (c Chain) CheckConfigVersion() error { + configPath := c.chain.ConfigPath() + if configPath == "" { + return chainconfig.ErrConfigNotFound + } + + return chainconfig.CheckVersion(configPath) +} + // Build builds chain sources, also checks if source was already built func (c *Chain) Build(ctx context.Context, cacheStorage cache.Storage) (binaryName string, err error) { + // Check that the config version is the latest before building the binary + if err = c.CheckConfigVersion(); err != nil { + return + } + // if chain was already published and has launch id check binary cache if c.launchID != 0 { if binaryName, err = c.chain.Binary(); err != nil { From 661f4e1336d8dccb09fa208409157bd922337737 Mon Sep 17 00:00:00 2001 From: jeronimoalbi Date: Tue, 18 Oct 2022 09:32:32 +0200 Subject: [PATCH 2/5] chore: change network publish integration test to use example repo --- integration/network/network_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/integration/network/network_test.go b/integration/network/network_test.go index 20947e4374..1feef19a82 100644 --- a/integration/network/network_test.go +++ b/integration/network/network_test.go @@ -124,8 +124,12 @@ func TestNetworkPublish(t *testing.T) { step.Exec( envtest.IgniteApp, "network", "chain", "publish", - "https://github.com/lubtd/planet", + "https://github.com/ignite/example", "--local", + // The hash is used to be sure the test uses the right config + // version. Hash value must be updated to the latest when the + // config version in the repository is updated to a new version. + "--hash", "b8b2cc2876c982dd4a049ed16b9a6099eca000aa", ), step.Stdout(&b), )), From 1068c71a48185023ea01f02dfcd77153d389ac1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B3nimo=20Albi?= Date: Tue, 18 Oct 2022 16:07:50 +0200 Subject: [PATCH 3/5] Update changelog.md Co-authored-by: Alex Johnson --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 4b80974b64..97bcbefccb 100644 --- a/changelog.md +++ b/changelog.md @@ -39,7 +39,7 @@ - Fix config file migration to void leaving end of file content chunks - Change session print loop to block until all events are handled. - Handle "No records were found in keyring" message when checking keys. -- Network commands check for latest config version before building the chain binary. +- [#2922](https://github.com/ignite/cli/pull/2922) Network commands check for latest config version before building the chain binary. ## [`v0.24.1`](https://github.com/ignite/cli/releases/tag/v0.24.1) From 41aae83db8ab9cd9d0c85d7647ef2fb713e67373 Mon Sep 17 00:00:00 2001 From: jeronimoalbi Date: Tue, 18 Oct 2022 16:15:40 +0200 Subject: [PATCH 4/5] chore: improve config version check implementation --- ignite/chainconfig/chainconfig.go | 10 +++------- ignite/services/network/networkchain/networkchain.go | 9 ++++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ignite/chainconfig/chainconfig.go b/ignite/chainconfig/chainconfig.go index 326e195a2c..60bd12838c 100644 --- a/ignite/chainconfig/chainconfig.go +++ b/ignite/chainconfig/chainconfig.go @@ -2,6 +2,7 @@ package chainconfig import ( "fmt" + "io" "os" "path/filepath" "strings" @@ -90,13 +91,8 @@ func LocateDefault(root string) (path string, err error) { // CheckVersion checks that the config version is the latest // and if not a VersionError is returned. -func CheckVersion(configPath string) error { - file, err := os.Open(configPath) - if err != nil { - return err - } - - version, err := ReadConfigVersion(file) +func CheckVersion(configFile io.Reader) error { + version, err := ReadConfigVersion(configFile) if err != nil { return err } diff --git a/ignite/services/network/networkchain/networkchain.go b/ignite/services/network/networkchain/networkchain.go index d83d73742e..ce35fe53b6 100644 --- a/ignite/services/network/networkchain/networkchain.go +++ b/ignite/services/network/networkchain/networkchain.go @@ -287,7 +287,14 @@ func (c Chain) CheckConfigVersion() error { return chainconfig.ErrConfigNotFound } - return chainconfig.CheckVersion(configPath) + file, err := os.Open(configPath) + if err != nil { + return err + } + + defer file.Close() + + return chainconfig.CheckVersion(file) } // Build builds chain sources, also checks if source was already built From bace368c93708bf988f5faf40c1bb657225f1095 Mon Sep 17 00:00:00 2001 From: jeronimoalbi Date: Tue, 18 Oct 2022 16:29:35 +0200 Subject: [PATCH 5/5] test: add `chainconfig.CheckVersion` tests --- ignite/chainconfig/chainconfig_test.go | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ignite/chainconfig/chainconfig_test.go diff --git a/ignite/chainconfig/chainconfig_test.go b/ignite/chainconfig/chainconfig_test.go new file mode 100644 index 0000000000..95279a43ae --- /dev/null +++ b/ignite/chainconfig/chainconfig_test.go @@ -0,0 +1,38 @@ +package chainconfig_test + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/ignite/cli/ignite/chainconfig" + "github.com/ignite/cli/ignite/chainconfig/config" +) + +func TestCheckVersion(t *testing.T) { + // Arrange + cfg := bytes.NewBufferString( + fmt.Sprintf("version: %d", chainconfig.LatestVersion), + ) + + // Act + err := chainconfig.CheckVersion(cfg) + + // Assert + require.NoError(t, err) +} + +func TestCheckVersionWithOutdatedVersion(t *testing.T) { + // Arrange + cfg := bytes.NewBufferString("version: 0") + wantError := chainconfig.VersionError{} + + // Act + err := chainconfig.CheckVersion(cfg) + + // Assert + require.ErrorAs(t, err, &wantError) + require.Equal(t, wantError.Version, config.Version(0)) +}