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 0219a3e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 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

Check failure on line 19 in pkg/constant/version.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to version (ineffassign)
}
return NormalizeVersion(Version)

Check warning on line 21 in pkg/constant/version.go

View check run for this annotation

Codecov / codecov/patch

pkg/constant/version.go#L16-L21

Added lines #L16 - L21 were not covered by tests
}

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

Check warning on line 26 in pkg/constant/version.go

View check run for this annotation

Codecov / codecov/patch

pkg/constant/version.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}

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

Check warning on line 33 in pkg/constant/version.go

View check run for this annotation

Codecov / codecov/patch

pkg/constant/version.go#L29-L33

Added lines #L29 - L33 were not covered by tests
}
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()

Check warning on line 166 in zetaclient/maintenance/shutdown_listener.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/maintenance/shutdown_listener.go#L166

Added line #L166 was not covered by tests
}
4 changes: 2 additions & 2 deletions zetaclient/maintenance/shutdown_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestShutdownListener(t *testing.T) {

listener := NewShutdownListener(client, logger)
listener.getVersion = func() string {
return "1.1.2"
return "v1.1.2"
}

client.Mock.On("GetOperationalFlags", ctx).Return(observertypes.OperationalFlags{
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestShutdownListener(t *testing.T) {

listener := NewShutdownListener(client, logger)
listener.getVersion = func() string {
return "1.1.1"
return "v1.1.1"
}

client.Mock.On("GetOperationalFlags", ctx).Return(observertypes.OperationalFlags{
Expand Down

0 comments on commit 0219a3e

Please sign in to comment.