Skip to content

Commit

Permalink
refactor: consolidate version normalization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Jan 31, 2025
1 parent 1375375 commit c3815d0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// migrations needto be run in different environment
func GetDefaultUpgradeHandlerVersion() string {
// semver must have v prefix, but we store without prefix
vVersion := "v" + constant.Version
vVersion := constant.GetNormalizedVersion()

// development builds always use the full version in the release handlers
if semver.Build(vVersion) != "" || semver.Prerelease(vVersion) != "" {
Expand Down
11 changes: 2 additions & 9 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -39,6 +38,7 @@ import (
tonrunner "github.com/zeta-chain/node/e2e/runner/ton"
"github.com/zeta-chain/node/e2e/txserver"
"github.com/zeta-chain/node/e2e/utils"
"github.com/zeta-chain/node/pkg/constant"
"github.com/zeta-chain/node/pkg/contracts/testdappv2"
toncontracts "github.com/zeta-chain/node/pkg/contracts/ton"
"github.com/zeta-chain/node/pkg/contracts/uniswap/v2-core/contracts/uniswapv2factory.sol"
Expand Down Expand Up @@ -437,13 +437,6 @@ func (r *E2ERunner) GetZetacoredVersion() string {
}
nodeInfo, err := r.Clients.Zetacore.GetNodeInfo(r.Ctx)
require.NoError(r, err, "get node info")
r.zetacoredVersion = ensurePrefix(nodeInfo.ApplicationVersion.Version, "v")
r.zetacoredVersion = constant.NormalizeVersion(nodeInfo.ApplicationVersion.Version)
return r.zetacoredVersion
}

func ensurePrefix(s, prefix string) string {
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
}
26 changes: 26 additions & 0 deletions pkg/constant/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
package constant

import "strings"

var (
Name = ""
Version = ""
CommitHash = ""
BuildTime = ""
)

const versionWhenBuiltWithoutMake = "v0.0.0+nomake"

// GetNormalizedVersion applies NormalizeVersion to the Version constant
// and ensures that it is semver compliant
func GetNormalizedVersion() string {
version := Version
if version == "" {
version = versionWhenBuiltWithoutMake
}
return NormalizeVersion(Version)
}

// NormalizeVersion ensures that the version string starts with a v
func NormalizeVersion(version string) string {
return ensurePrefix(version, "v")
}

func ensurePrefix(s, prefix string) string {
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
}
13 changes: 2 additions & 11 deletions zetaclient/maintenance/shutdown_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package maintenance
import (
"context"
"fmt"
"strings"
"time"

"cosmossdk.io/errors"
Expand Down Expand Up @@ -151,8 +150,7 @@ func (o *ShutdownListener) handleNewFlags(ctx context.Context, f observertypes.O

func (o *ShutdownListener) checkMinimumVersion(f observertypes.OperationalFlags) error {
if f.MinimumVersion != "" {
// we typically store the version without the required v prefix
currentVersion := ensurePrefix(o.getVersion(), "v")
currentVersion := o.getVersion()
if semver.Compare(currentVersion, f.MinimumVersion) == -1 {
return fmt.Errorf(
"current version (%s) is less than minimum version (%s)",
Expand All @@ -165,12 +163,5 @@ func (o *ShutdownListener) checkMinimumVersion(f observertypes.OperationalFlags)
}

func getVersionDefault() string {
return constant.Version
}

func ensurePrefix(s, prefix string) string {
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
return constant.GetNormalizedVersion()
}

0 comments on commit c3815d0

Please sign in to comment.