From 0f48e34ba8231a540f415fd54e3cc6f4adddddae Mon Sep 17 00:00:00 2001 From: Troy Kessler Date: Fri, 6 Sep 2024 16:35:58 +0200 Subject: [PATCH] chore: log recommended version --- cmd/ksync/commands/blocksync.go | 2 + cmd/ksync/commands/heightsync.go | 2 + cmd/ksync/commands/statesync.go | 2 + sources/version.go | 82 ++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 sources/version.go diff --git a/cmd/ksync/commands/blocksync.go b/cmd/ksync/commands/blocksync.go index bca6a3c..d8b99b6 100644 --- a/cmd/ksync/commands/blocksync.go +++ b/cmd/ksync/commands/blocksync.go @@ -108,6 +108,8 @@ var blockSyncCmd = &cobra.Command{ os.Exit(1) } + sources.IsBinaryRecommendedVersion(binaryPath, registryUrl, source, continuationHeight, !y) + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, source, continuationHeight) if err := consensusEngine.OpenDBs(homePath); err != nil { diff --git a/cmd/ksync/commands/heightsync.go b/cmd/ksync/commands/heightsync.go index 101b819..6d04ea4 100644 --- a/cmd/ksync/commands/heightsync.go +++ b/cmd/ksync/commands/heightsync.go @@ -118,6 +118,8 @@ var heightSyncCmd = &cobra.Command{ os.Exit(1) } + sources.IsBinaryRecommendedVersion(binaryPath, registryUrl, source, continuationHeight, !y) + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, source, continuationHeight) if err := consensusEngine.OpenDBs(homePath); err != nil { diff --git a/cmd/ksync/commands/statesync.go b/cmd/ksync/commands/statesync.go index fad7dc6..4c7100c 100644 --- a/cmd/ksync/commands/statesync.go +++ b/cmd/ksync/commands/statesync.go @@ -83,6 +83,8 @@ var stateSyncCmd = &cobra.Command{ os.Exit(1) } + sources.IsBinaryRecommendedVersion(binaryPath, registryUrl, source, snapshotHeight, !y) + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, source, snapshotHeight) if err := consensusEngine.OpenDBs(homePath); err != nil { diff --git a/sources/version.go b/sources/version.go new file mode 100644 index 0000000..b1ef9f2 --- /dev/null +++ b/sources/version.go @@ -0,0 +1,82 @@ +package sources + +import ( + "fmt" + "github.com/KYVENetwork/ksync/sources/helpers" + log "github.com/KYVENetwork/ksync/utils" + "os" + "os/exec" + "strconv" + "strings" +) + +var ( + logger = log.KsyncLogger("sources") +) + +func IsBinaryRecommendedVersion(binaryPath, registryUrl, source string, continuationHeight int64, userInput bool) { + if source == "" || !userInput { + return + } + + cmdPath, err := exec.LookPath(binaryPath) + if err != nil { + logger.Error().Msg(fmt.Sprintf("failed to lookup binary path: %s", err.Error())) + os.Exit(1) + } + + startArgs := make([]string, 0) + + // if we run with cosmovisor we start with the cosmovisor run command + if strings.HasSuffix(binaryPath, "cosmovisor") { + startArgs = append(startArgs, "run") + } + + startArgs = append(startArgs, "version") + + out, err := exec.Command(cmdPath, startArgs...).Output() + if err != nil { + logger.Error().Msg("failed to get output of binary") + os.Exit(1) + } + + binaryVersion := fmt.Sprintf("v%s", strings.TrimSuffix(string(out), "\n")) + var recommendedVersion string + + entry, err := helpers.GetSourceRegistryEntry(registryUrl, source) + if err != nil { + logger.Error().Msg(fmt.Sprintf("failed to get source registry entry: %s", err)) + os.Exit(1) + } + + for _, upgrade := range entry.Codebase.Settings.Upgrades { + height, err := strconv.ParseInt(upgrade.Height, 10, 64) + if err != nil { + logger.Error().Msg(fmt.Sprintf("failed to parse upgrade height %s: %s", upgrade.Height, err)) + os.Exit(1) + } + + if continuationHeight < height { + break + } + + recommendedVersion = upgrade.RecommendedVersion + } + + if binaryVersion == recommendedVersion { + return + } + + fmt.Printf("\u001B[36m[KSYNC]\u001B[0m The recommended binary version for the current height %d is %s while the provided binary has the following version: %s. Proceed anyway? [y/N]: ", continuationHeight, recommendedVersion, binaryVersion) + + answer := "" + if _, err := fmt.Scan(&answer); err != nil { + logger.Error().Msg(fmt.Sprintf("failed to read in user input: %s", err)) + os.Exit(1) + } + + if strings.ToLower(answer) != "y" { + logger.Error().Msg("abort") + os.Exit(0) + } +}