From f233e624f5f5c053f0f2eb1598a27fb127df1917 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Tue, 12 Mar 2024 23:03:24 +0100 Subject: [PATCH] fix: add app version in the context in InitChain (#376) --- baseapp/abci.go | 10 +++++++--- baseapp/baseapp.go | 8 +++++--- baseapp/options.go | 4 +--- x/upgrade/keeper/keeper_test.go | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index bb9fb0566ab..daad19ba165 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -14,6 +14,7 @@ import ( "github.com/gogo/protobuf/proto" abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmversion "github.com/tendermint/tendermint/proto/tendermint/version" "google.golang.org/grpc/codes" grpcstatus "google.golang.org/grpc/status" @@ -38,12 +39,15 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC // On a new chain, we consider the init chain block height as 0, even though // req.InitialHeight is 1 by default. initHeader := tmproto.Header{ChainID: req.ChainId, Time: req.Time} + if req.ConsensusParams != nil && req.ConsensusParams.Version != nil { + initHeader.Version = tmversion.Consensus{App: req.ConsensusParams.Version.AppVersion} + } // If req.InitialHeight is > 1, then we set the initial version in the // stores. if req.InitialHeight > 1 { app.initialHeight = req.InitialHeight - initHeader = tmproto.Header{ChainID: req.ChainId, Height: req.InitialHeight, Time: req.Time} + initHeader.Height = req.InitialHeight err := app.cms.SetInitialVersion(req.InitialHeight) if err != nil { panic(err) @@ -131,8 +135,8 @@ func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo { if err != nil { panic(err) } - // get and set the app version - _ = app.AppVersion(ctx) + // initialise the app version by checking if it is already in state + app.InitAppVersion(ctx) } return abci.ResponseInfo{ Data: app.name, diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index c9877ddb012..04274f31ecf 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -202,15 +202,17 @@ func (app *BaseApp) Name() string { return app.name } -// AppVersion returns the application's protocol version. -func (app *BaseApp) AppVersion(ctx sdk.Context) uint64 { +func (app *BaseApp) InitAppVersion(ctx sdk.Context) { if app.appVersion == 0 && app.paramStore.Has(ctx, ParamStoreKeyVersionParams) { var vp tmproto.VersionParams - app.paramStore.Get(ctx, ParamStoreKeyVersionParams, &vp) // set the app version app.appVersion = vp.AppVersion } +} + +// AppVersion returns the application's protocol version. +func (app *BaseApp) AppVersion() uint64 { return app.appVersion } diff --git a/baseapp/options.go b/baseapp/options.go index ba29aa977f3..953ede102e1 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -113,9 +113,7 @@ func (app *BaseApp) SetVersion(v string) { // SetAppVersion sets the application's protocol version func (app *BaseApp) SetAppVersion(ctx sdk.Context, version uint64) { - // TODO: could make this less hacky in the future since the SDK - // shouldn't have to know about the app versioning scheme - if version >= 2 { + if app.paramStore.Has(ctx, ParamStoreKeyVersionParams) { vp := &tmproto.VersionParams{AppVersion: version} app.paramStore.Set(ctx, ParamStoreKeyVersionParams, vp) } diff --git a/x/upgrade/keeper/keeper_test.go b/x/upgrade/keeper/keeper_test.go index b37ebcb6233..19fee1054ec 100644 --- a/x/upgrade/keeper/keeper_test.go +++ b/x/upgrade/keeper/keeper_test.go @@ -203,7 +203,7 @@ func (s *KeeperTestSuite) TestSetUpgradedClient() { // Test that the protocol version successfully increments after an // upgrade and is successfully set on BaseApp's appVersion. func (s *KeeperTestSuite) TestIncrementProtocolVersion() { - oldProtocolVersion := s.app.BaseApp.AppVersion(s.ctx) + oldProtocolVersion := s.app.BaseApp.AppVersion() s.app.UpgradeKeeper.SetUpgradeHandler("dummy", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) { return vm, nil }) dummyPlan := types.Plan{ Name: "dummy", @@ -211,7 +211,7 @@ func (s *KeeperTestSuite) TestIncrementProtocolVersion() { Height: 100, } s.app.UpgradeKeeper.ApplyUpgrade(s.ctx, dummyPlan) - upgradedProtocolVersion := s.app.BaseApp.AppVersion(s.ctx) + upgradedProtocolVersion := s.app.BaseApp.AppVersion() s.Require().Equal(oldProtocolVersion+1, upgradedProtocolVersion) }