Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove baseapp dependency on the version package #4250

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .pending/breaking/sdk/4250-BaseApp-Query-r
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#4250 BaseApp.Query() returns app's version string set via BaseApp.SetAppVersion()
when handling /app/version queries instead of the version string passed as build
flag at compile time.
1 change: 1 addition & 0 deletions .pending/features/sdk/4250-New-BaseApp-Set
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4250 New BaseApp.{,Set}AppVersion() methods to get/set app's version string.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ build_tags := $(strip $(build_tags))

# process linker flags

ldflags = -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=gaia \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)"

Expand Down
11 changes: 9 additions & 2 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
)

// Key to store the consensus params in the main store.
Expand Down Expand Up @@ -85,6 +84,9 @@ type BaseApp struct {

// height at which to halt the chain and gracefully shutdown
haltHeight uint64

// application's version string
appVersion string
}

var _ abci.Application = (*BaseApp)(nil)
Expand Down Expand Up @@ -120,6 +122,11 @@ func (app *BaseApp) Name() string {
return app.name
}

// AppVersion returns the application's version string.
func (app *BaseApp) AppVersion() string {
return app.appVersion
}

// Logger returns the logger of the BaseApp.
func (app *BaseApp) Logger() log.Logger {
return app.logger
Expand Down Expand Up @@ -439,7 +446,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc
return abci.ResponseQuery{
Code: uint32(sdk.CodeOK),
Codespace: string(sdk.CodespaceRoot),
Value: []byte(version.Version),
Value: []byte(app.appVersion),
}

default:
Expand Down
23 changes: 23 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ func TestLoadVersion(t *testing.T) {
testLoadVersionHelper(t, app, int64(2), commitID2)
}

func TestAppVersionSetterGetter(t *testing.T) {
logger := defaultLogger()
pruningOpt := SetPruning(store.PruneSyncable)
db := dbm.NewMemDB()
name := t.Name()
app := NewBaseApp(name, logger, db, nil, pruningOpt)

require.Equal(t, "", app.AppVersion())
res := app.Query(abci.RequestQuery{Path: "app/version"})
require.True(t, res.IsOK())
require.Equal(t, "", string(res.Value))

versionString := "1.0.0"
app.SetAppVersion(versionString)
require.Equal(t, versionString, app.AppVersion())
res = app.Query(abci.RequestQuery{Path: "app/version"})
require.True(t, res.IsOK())
require.Equal(t, versionString, string(res.Value))
}

func TestLoadVersionInvalid(t *testing.T) {
logger := log.NewNopLogger()
pruningOpt := SetPruning(store.PruneSyncable)
Expand Down Expand Up @@ -226,6 +246,9 @@ func TestBaseAppOptionSeal(t *testing.T) {
require.Panics(t, func() {
app.SetName("")
})
require.Panics(t, func() {
app.SetAppVersion("")
})
require.Panics(t, func() {
app.SetDB(nil)
})
Expand Down
8 changes: 8 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func (app *BaseApp) SetName(name string) {
app.name = name
}

// SetAppVersion sets the application's version string.
func (app *BaseApp) SetAppVersion(v string) {
if app.sealed {
panic("SetAppVersion() on sealed BaseApp")
}
app.appVersion = v
}

func (app *BaseApp) SetDB(db dbm.DB) {
if app.sealed {
panic("SetDB() on sealed BaseApp")
Expand Down
3 changes: 2 additions & 1 deletion cmd/gaia/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ build_tags := $(strip $(build_tags))

# process linker flags

ldflags = -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=gaia \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)"

Expand Down
2 changes: 2 additions & 0 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
Expand Down Expand Up @@ -78,6 +79,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)

var app = &GaiaApp{
BaseApp: bApp,
Expand Down
7 changes: 4 additions & 3 deletions version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ const (

var (

// VersionCmd prints out the current sdk version
// VersionCmd prints out the application's version
// information passed via build flags.
VersionCmd = &cobra.Command{
Use: "version",
Short: "Print the app version",
RunE: func(_ *cobra.Command, _ []string) error {
verInfo := newVersionInfo()

if !viper.GetBool(flagLong) {
fmt.Println(verInfo.CosmosSDK)
fmt.Println(verInfo.Version)
return nil
}

if viper.GetString(cli.OutputFlag) != "json" {
fmt.Print(verInfo)
fmt.Println(verInfo)
return nil
}

Expand Down
38 changes: 30 additions & 8 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,62 @@
//nolint
// This package is a convenience utility that provides SDK
// consumers with a ready-to-use version command that
// produces apps versioning information based on flags
// passed at compile time.
//
// Configure the version command
//
// The version command can be just added to your cobra root command.
// At build time, the variables Name, Version, Commit, GoSumHash, and
// BuildTags can be passed as build flags as shown in the following
// example:
//
// go build -X github.com/cosmos/cosmos-sdk/version.Name=dapp \
// -X github.com/cosmos/cosmos-sdk/version.Version=1.0 \
// -X github.com/cosmos/cosmos-sdk/version.Commit=f0f7b7dab7e36c20b757cebce0e8f4fc5b95de60 \
// -X "github.com/cosmos/cosmos-sdk/version.BuildTags=linux darwin amd64"
package version

import (
"fmt"
"runtime"
)

// Variables set by build flags
var (
Commit = ""
Version = ""
// Application's name
Name = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should probably show an example for how the gaia Name is set here (within the cmd/gaia) - I'm assuming you just forgot this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming you just forgot this

I did not: https://github.com/cosmos/cosmos-sdk/pull/4250/files#diff-b67911656ef5d18c4ae36cb6741b7965R47

All these variables are set via build flags. I didn't provide examples as there were any before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've improved the version package docs

// Application's version string
Version = ""
// Commit
Commit = ""
// Hash of the go.sum file
GoSumHash = ""
// Build tags
BuildTags = ""
)

type versionInfo struct {
CosmosSDK string `json:"cosmos_sdk"`
Name string `json:"name"`
Version string `json:"version"`
GitCommit string `json:"commit"`
GoSumHash string `json:"gosum_hash"`
BuildTags string `json:"build_tags"`
GoVersion string `json:"go"`
}

func (v versionInfo) String() string {
return fmt.Sprintf(`cosmos-sdk: %s
return fmt.Sprintf(`%s: %s
git commit: %s
go.sum hash: %s
build tags: %s
%s`, v.CosmosSDK, v.GitCommit, v.GoSumHash, v.BuildTags, v.GoVersion)
%s`, v.Name, v.Version, v.GitCommit, v.GoSumHash, v.BuildTags, v.GoVersion)
}

func newVersionInfo() versionInfo {
return versionInfo{
Name,
Version,
Commit,
GoSumHash,
BuildTags,
fmt.Sprintf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)}
fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)}
}