From 36d03c506b3dcbc9cacf00b140235d86ec9e3148 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 2 May 2019 02:41:54 +0100 Subject: [PATCH 1/6] Remove baseapp dependency on the version package The version package is meant to be a convenience utility that provides SDK consumers with a ready-to-use version command that produces app's versioning information from flags passed at compile time. It will not make sense anymore for the baseapp package to depend on the version package once gaia will have been migrated away from the SDK main repository as we neither want to make assumptions nor set expectations on downstream apps buildsystems. Thus BaseApp now provides SetAppVersion() and AppVersion() to to allow SDK consumers to set BaseApp's version information string once the struct is initialised. --- Makefile | 3 ++- baseapp/baseapp.go | 12 ++++++++++-- baseapp/baseapp_test.go | 20 ++++++++++++++++++++ cmd/gaia/Makefile | 3 ++- cmd/gaia/app/app.go | 2 ++ version/command.go | 5 +++-- version/version.go | 21 +++++++++++++++------ 7 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index ff5f742e6f69..bc61031d2c99 100644 --- a/Makefile +++ b/Makefile @@ -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)" diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 119450dc5b91..2281ce711087 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -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. @@ -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) @@ -131,6 +133,12 @@ func (app *BaseApp) SetCommitMultiStoreTracer(w io.Writer) { app.cms.SetTracer(w) } +// AppVersion returns the application's version string. +func (app *BaseApp) AppVersion() string { return app.appVersion } + +// SetAppVersion sets the application's version string. +func (app *BaseApp) SetAppVersion(v string) { app.appVersion = v } + // MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp // multistore. func (app *BaseApp) MountStores(keys ...sdk.StoreKey) { @@ -439,7 +447,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: diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index c3d435f9f272..4ac2ae3ce324 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -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) diff --git a/cmd/gaia/Makefile b/cmd/gaia/Makefile index f966ee43619d..1a5e33a8cb57 100644 --- a/cmd/gaia/Makefile +++ b/cmd/gaia/Makefile @@ -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)" diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index de910348c7f1..d389ebf24517 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -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" @@ -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, diff --git a/version/command.go b/version/command.go index c3ff71df10e9..a5ed5951e70b 100644 --- a/version/command.go +++ b/version/command.go @@ -16,7 +16,8 @@ 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", @@ -24,7 +25,7 @@ var ( verInfo := newVersionInfo() if !viper.GetBool(flagLong) { - fmt.Println(verInfo.CosmosSDK) + fmt.Println(verInfo.Version) return nil } diff --git a/version/version.go b/version/version.go index aad02d1904da..75e1269d6d63 100644 --- a/version/version.go +++ b/version/version.go @@ -6,16 +6,24 @@ import ( "runtime" ) -// Variables set by build flags +// Variables representin application's versioning +// information set at build time. var ( - Commit = "" - Version = "" + // Application's name + Name = "" + // 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"` @@ -23,15 +31,16 @@ type versionInfo struct { } 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, From 9362ea576b10224f6807a42017c7d51d5bba0a22 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 2 May 2019 03:09:01 +0100 Subject: [PATCH 2/6] Add pending entries --- .pending/breaking/sdk/4250-BaseApp-Query-r | 3 +++ .pending/features/sdk/4250-New-BaseApp-Set | 1 + 2 files changed, 4 insertions(+) create mode 100644 .pending/breaking/sdk/4250-BaseApp-Query-r create mode 100644 .pending/features/sdk/4250-New-BaseApp-Set diff --git a/.pending/breaking/sdk/4250-BaseApp-Query-r b/.pending/breaking/sdk/4250-BaseApp-Query-r new file mode 100644 index 000000000000..0adee63c8dad --- /dev/null +++ b/.pending/breaking/sdk/4250-BaseApp-Query-r @@ -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. diff --git a/.pending/features/sdk/4250-New-BaseApp-Set b/.pending/features/sdk/4250-New-BaseApp-Set new file mode 100644 index 000000000000..11edde4a00b0 --- /dev/null +++ b/.pending/features/sdk/4250-New-BaseApp-Set @@ -0,0 +1 @@ +#4250 New BaseApp.{,Set}AppVersion() methods to get/set app's version string. From d4feb47232e224bdffe717d8bfebd49df22d3209 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 2 May 2019 03:15:17 +0100 Subject: [PATCH 3/6] Don't append \n to version.GoVersion --- version/command.go | 2 +- version/version.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/version/command.go b/version/command.go index a5ed5951e70b..b886a9f24e45 100644 --- a/version/command.go +++ b/version/command.go @@ -30,7 +30,7 @@ var ( } if viper.GetString(cli.OutputFlag) != "json" { - fmt.Print(verInfo) + fmt.Println(verInfo) return nil } diff --git a/version/version.go b/version/version.go index 75e1269d6d63..6be591196d37 100644 --- a/version/version.go +++ b/version/version.go @@ -45,5 +45,5 @@ func newVersionInfo() versionInfo { 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)} } From 66668fa53ac7c5e05085a27ea184684f9c6b6679 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 2 May 2019 12:36:12 +0100 Subject: [PATCH 4/6] Better package docs --- version/version.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/version/version.go b/version/version.go index 6be591196d37..c46f9deca633 100644 --- a/version/version.go +++ b/version/version.go @@ -1,4 +1,19 @@ -//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 ( @@ -6,8 +21,6 @@ import ( "runtime" ) -// Variables representin application's versioning -// information set at build time. var ( // Application's name Name = "" From 571a49df79a64b0dab6bdf459ae52d43d32e0aae Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 2 May 2019 19:18:11 +0100 Subject: [PATCH 5/6] Move SetAppVersion() to options.go --- baseapp/baseapp.go | 11 +++++------ baseapp/options.go | 8 ++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 2281ce711087..0ad9f2309d4d 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -122,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 @@ -133,12 +138,6 @@ func (app *BaseApp) SetCommitMultiStoreTracer(w io.Writer) { app.cms.SetTracer(w) } -// AppVersion returns the application's version string. -func (app *BaseApp) AppVersion() string { return app.appVersion } - -// SetAppVersion sets the application's version string. -func (app *BaseApp) SetAppVersion(v string) { app.appVersion = v } - // MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp // multistore. func (app *BaseApp) MountStores(keys ...sdk.StoreKey) { diff --git a/baseapp/options.go b/baseapp/options.go index d78f59958a7f..1062c45b4487 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -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") From 9d5d10164a343b8bff1cda27b354c2c4c70f71c0 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 2 May 2019 19:19:31 +0100 Subject: [PATCH 6/6] Add sealing test for appVersion --- baseapp/baseapp_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 4ac2ae3ce324..c552ed4d64ea 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -246,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) })