From 7aeb8959d1a3fa4c4f5e47b5c128e0b771150ec5 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Fri, 24 May 2024 18:15:20 +0800 Subject: [PATCH] fix(cli): doctor check fxcored upgrade version (#512) --- cmd/doctor.go | 18 +++++++++++++----- cmd/doctor_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 224a201e7..d16443561 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -59,7 +59,7 @@ func doctorCmd() *cobra.Command { if err != nil { return err } - if err := checkUpgradeInfo(serverCtx.Config.RootDir); err != nil { + if err = checkUpgradeInfo(serverCtx.Config.RootDir); err != nil { return err } bc, err := getBlockchain(clientCtx, serverCtx) @@ -418,13 +418,14 @@ func checkCosmovisor(rootPath string, bc blockchain) error { fmt.Printf("%s%s%sWarning: %s\n", SPACE, SPACE, SPACE, err.Error()) continue } - v := string(bytes.Trim(output, "\n")) - fmt.Printf("%s%s%sfxcored version: %s\n", SPACE, SPACE, SPACE, v) - if !(strings.HasPrefix(v, "release/v"+entry.Name()[len(entry.Name())-1:]) || strings.HasPrefix(v, "release/"+entry.Name())) { + outputVersion := string(bytes.Trim(output, "\n")) + fmt.Printf("%s%s%sfxcored version: %s\n", SPACE, SPACE, SPACE, outputVersion) + + if checkVersionCompatibility(entry.Name(), outputVersion) { fmt.Printf("%s%s%sWarning: fxcored version is not match upgrade plan\n", SPACE, SPACE, SPACE) } - upgradeInfoFile := filepath.Join(upgradesPath, entry.Name(), upgradetypes.UpgradeInfoFilename) + upgradeInfoFile := filepath.Join(upgradesPath, entry.Name(), upgradetypes.UpgradeInfoFilename) upgradeInfo, err := os.ReadFile(upgradeInfoFile) if err != nil { if !os.IsNotExist(err) { @@ -449,6 +450,13 @@ func checkCosmovisor(rootPath string, bc blockchain) error { return nil } +func checkVersionCompatibility(version, outputVersion string) bool { + semVer := strings.Split(version, ".") + isFxV := len(semVer) == 1 && strings.HasPrefix(semVer[0], "fx") && strings.Contains(outputVersion, semVer[0][2:]) + isSemVer := len(semVer) > 1 && strings.Contains(outputVersion, semVer[0]+"."+semVer[1]) + return !isFxV && !isSemVer +} + func printDirectory(path string, depth int, last []bool, tab string) error { printPath := path if depth > 0 { diff --git a/cmd/doctor_test.go b/cmd/doctor_test.go index ccb98cef2..431c2bcb5 100644 --- a/cmd/doctor_test.go +++ b/cmd/doctor_test.go @@ -98,3 +98,37 @@ func Test_checkGenesis(t *testing.T) { }) } } + +func Test_checkVersionCompatibility(t *testing.T) { + type args struct { + version string + outputVersion string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "v1", + args: args{ + version: "fxv1", + outputVersion: "release/v1.0.1", + }, + want: false, + }, + { + name: "v5", + args: args{ + version: "v5.0.x", + outputVersion: "release/v5.0.1", + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, checkVersionCompatibility(tt.args.version, tt.args.outputVersion), "checkVersionCompatibility(%v, %v)", tt.args.version, tt.args.outputVersion) + }) + } +}