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

feat!: add app version to param store #360

Merged
merged 13 commits into from
Nov 20, 2023
13 changes: 12 additions & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
// to state.
if req.ConsensusParams != nil {
app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams)
if req.ConsensusParams.Version != nil {
app.appVersion = req.ConsensusParams.Version.AppVersion
}
}

if app.initChainer == nil {
Expand Down Expand Up @@ -122,7 +125,15 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp
// Info implements the ABCI interface.
func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
lastCommitID := app.cms.LastCommitID()

// load the app version for a non zero height
if lastCommitID.Version > 0 {
ctx, err := app.createQueryContext(lastCommitID.Version, false)
if err != nil {
panic(err)
}
// get and set the app version
_ = app.AppVersion(ctx)
Comment on lines +134 to +135
Copy link
Collaborator

Choose a reason for hiding this comment

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

[question] where or how does this "set" the app version? The invoked function returns the app version but I don't know what "set" is referring to here.

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

}
return abci.ResponseInfo{
Data: app.name,
Version: app.version,
Expand Down
39 changes: 25 additions & 14 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
postHandler sdk.AnteHandler // post handler, optional, e.g. for tips

appStore
baseappVersions
peerFilters
snapshotData
abciData
Expand Down Expand Up @@ -98,6 +97,13 @@
// ResponseCommit.RetainHeight.
minRetainBlocks uint64

// application's version string
version string

// application's protocol version that increments on every upgrade
// if BaseApp is passed to the upgrade keeper's NewKeeper method.
appVersion uint64
Comment on lines +100 to +105
Copy link
Collaborator

Choose a reason for hiding this comment

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

[not blocking] can these two include an example to help further differentiate them?

If version = 2.0.0 then is appVersion = 2? Is appVersion always expected to be the major version of the version string?


// recovery handler for app.runTx method
runTxRecoveryMiddleware recoveryMiddleware

Expand Down Expand Up @@ -145,15 +151,6 @@
voteInfos []abci.VoteInfo
}

type baseappVersions struct {
// application's version string
version string

// application's protocol version that increments on every upgrade
// if BaseApp is passed to the upgrade keeper's NewKeeper method.
appVersion uint64
}

// should really get handled in some db struct
// which then has a sub-item, persistence fields
type snapshotData struct {
Expand Down Expand Up @@ -206,7 +203,14 @@
}

// AppVersion returns the application's protocol version.
func (app *BaseApp) AppVersion() uint64 {
func (app *BaseApp) AppVersion(ctx sdk.Context) uint64 {
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
}
return app.appVersion
}

Expand Down Expand Up @@ -482,7 +486,12 @@
cp.Validator = &vp
}

cp.Version = &tmproto.VersionParams{AppVersion: app.appVersion}
if app.paramStore.Has(ctx, ParamStoreKeyVersionParams) {
var vp tmproto.VersionParams

app.paramStore.Get(ctx, ParamStoreKeyVersionParams, &vp)
cp.Version = &vp
}

return cp
}
Expand All @@ -507,8 +516,10 @@
app.paramStore.Set(ctx, ParamStoreKeyBlockParams, cp.Block)
app.paramStore.Set(ctx, ParamStoreKeyEvidenceParams, cp.Evidence)
app.paramStore.Set(ctx, ParamStoreKeyValidatorParams, cp.Validator)
// We're explicitly not storing the Tendermint app_version in the param store. It's
// stored instead in the x/upgrade store, with its own bump logic.
// NOTE: we only persist the app version from v2 onwards
if cp.Version.AppVersion >=2 {

Check failure on line 520 in baseapp/baseapp.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
app.paramStore.Set(ctx, ParamStoreKeyVersionParams, cp.Version)
}
}

// getMaximumBlockGas gets the maximum gas from the consensus params. It panics
Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func TestInfo(t *testing.T) {
assert.Equal(t, t.Name(), res.GetData())
assert.Equal(t, int64(0), res.LastBlockHeight)
require.Equal(t, []uint8(nil), res.LastBlockAppHash)
require.Equal(t, app.AppVersion(), res.AppVersion)
require.EqualValues(t, 0, res.AppVersion)
// ----- test a proper response -------
// TODO
}
Expand Down
11 changes: 9 additions & 2 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -110,8 +111,14 @@ func (app *BaseApp) SetVersion(v string) {
app.version = v
}

// SetProtocolVersion sets the application's protocol version
func (app *BaseApp) SetProtocolVersion(v uint64) {
// SetAppVersion sets the application's protocol version
func (app *BaseApp) SetAppVersion(ctx sdk.Context, v uint64) {
// TODO: could make this less hacky in the future since the SDK
// shouldn't have to know about the app versioning scheme
if ctx.BlockHeader().Version.App >= 2 {
vp := &tmproto.VersionParams{AppVersion: v}
app.paramStore.Set(ctx, ParamStoreKeyVersionParams, vp)
}
app.appVersion = v
}

Expand Down
16 changes: 16 additions & 0 deletions baseapp/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
ParamStoreKeyBlockParams = []byte("BlockParams")
ParamStoreKeyEvidenceParams = []byte("EvidenceParams")
ParamStoreKeyValidatorParams = []byte("ValidatorParams")
ParamStoreKeyVersionParams = []byte("VersionParams")
)

// ParamStore defines the interface the parameter store used by the BaseApp must
Expand Down Expand Up @@ -84,3 +85,18 @@ func ValidateValidatorParams(i interface{}) error {

return nil
}

// ValidateVersionParams defines a stateless validation on VersionParams. This
// function is called whenever the parameters are updated or stored.
func ValidateVersionParams(i interface{}) error {
v, ok := i.(tmproto.VersionParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.AppVersion == 0 {
return errors.New("application version must not be zero")
}

return nil
}
3 changes: 3 additions & 0 deletions x/params/types/consensus_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ func ConsensusParamsKeyTable() KeyTable {
NewParamSetPair(
baseapp.ParamStoreKeyValidatorParams, tmproto.ValidatorParams{}, baseapp.ValidateValidatorParams,
),
NewParamSetPair(
baseapp.ParamStoreKeyVersionParams, tmproto.VersionParams{}, baseapp.ValidateVersionParams,
),
)
}
4 changes: 3 additions & 1 deletion x/upgrade/exported/exported.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package exported

import sdk "github.com/cosmos/cosmos-sdk/types"

// ProtocolVersionSetter defines the interface fulfilled by BaseApp
// which allows setting it's appVersion field.
type ProtocolVersionSetter interface {
SetProtocolVersion(uint64)
SetAppVersion(sdk.Context, uint64)
}
2 changes: 1 addition & 1 deletion x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (k Keeper) ApplyUpgrade(ctx sdk.Context, plan types.Plan) {
k.setProtocolVersion(ctx, nextProtocolVersion)
if k.versionSetter != nil {
// set protocol version on BaseApp
k.versionSetter.SetProtocolVersion(nextProtocolVersion)
k.versionSetter.SetAppVersion(ctx, nextProtocolVersion)
}

// Must clear IBC state after upgrade is applied as it is stored separately from the upgrade plan.
Expand Down
4 changes: 2 additions & 2 deletions x/upgrade/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ 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()
oldProtocolVersion := s.app.BaseApp.AppVersion(s.ctx)
s.app.UpgradeKeeper.SetUpgradeHandler("dummy", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) { return vm, nil })
dummyPlan := types.Plan{
Name: "dummy",
Info: "some text here",
Height: 100,
}
s.app.UpgradeKeeper.ApplyUpgrade(s.ctx, dummyPlan)
upgradedProtocolVersion := s.app.BaseApp.AppVersion()
upgradedProtocolVersion := s.app.BaseApp.AppVersion(s.ctx)

s.Require().Equal(oldProtocolVersion+1, upgradedProtocolVersion)
}
Expand Down
Loading