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: unify version modifier for v2 (backport #21508) #21578

Merged
merged 5 commits into from
Sep 6, 2024
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
15 changes: 5 additions & 10 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/header"
"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
Expand Down Expand Up @@ -89,6 +90,7 @@ type BaseApp struct {
verifyVoteExt sdk.VerifyVoteExtensionHandler // ABCI VerifyVoteExtension handler
prepareCheckStater sdk.PrepareCheckStater // logic to run during commit using the checkState
precommiter sdk.Precommiter // logic to run during commit using the deliverState
versionModifier server.VersionModifier // interface to get and set the app version

addrPeerFilter sdk.PeerFilter // filter peers by address and port
idPeerFilter sdk.PeerFilter // filter peers by node ID
Expand Down Expand Up @@ -249,18 +251,11 @@ func (app *BaseApp) Name() string {

// AppVersion returns the application's protocol version.
func (app *BaseApp) AppVersion(ctx context.Context) (uint64, error) {
if app.paramStore == nil {
return 0, errors.New("app.paramStore is nil")
if app.versionModifier == nil {
return 0, errors.New("app.versionModifier is nil")
}

cp, err := app.paramStore.Get(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get consensus params: %w", err)
}
if cp.Version == nil {
return 0, nil
}
return cp.Version.App, nil
return app.versionModifier.AppVersion(ctx)
}

// Version returns the application's version string.
Expand Down
1 change: 1 addition & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
app.SetParamStore(paramStore{db: dbm.NewMemDB()})
app.SetTxDecoder(txConfig.TxDecoder())
app.SetTxEncoder(txConfig.TxEncoder())
app.SetVersionModifier(newMockedVersionModifier(0))

// mount stores and seal
require.Nil(t, app.LoadLatestVersion())
Expand Down
27 changes: 13 additions & 14 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"math"

"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/metrics"
pruningtypes "cosmossdk.io/store/pruning/types"
Expand Down Expand Up @@ -146,22 +147,11 @@ func (app *BaseApp) SetVersion(v string) {
// SetAppVersion sets the application's version this is used as part of the
// header in blocks and is returned to the consensus engine in EndBlock.
func (app *BaseApp) SetAppVersion(ctx context.Context, v uint64) error {
if app.paramStore == nil {
return errors.New("param store must be set to set app version")
if app.versionModifier == nil {
return errors.New("version modifier must be set to set app version")
}

cp, err := app.paramStore.Get(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus params: %w", err)
}
if cp.Version == nil {
return errors.New("version is not set in param store")
}
cp.Version.App = v
if err := app.paramStore.Set(ctx, cp); err != nil {
return err
}
return nil
return app.versionModifier.SetAppVersion(ctx, v)
}

func (app *BaseApp) SetDB(db corestore.KVStoreWithBatch) {
Expand Down Expand Up @@ -323,6 +313,15 @@ func (app *BaseApp) SetTxEncoder(txEncoder sdk.TxEncoder) {
app.txEncoder = txEncoder
}

// SetVersionModifier sets the version modifier for the BaseApp that allows to set the app version.
func (app *BaseApp) SetVersionModifier(versionModifier server.VersionModifier) {
if app.sealed {
panic("SetVersionModifier() on sealed BaseApp")
}

app.versionModifier = versionModifier
}

// SetQueryMultiStore set a alternative MultiStore implementation to support grpc query service.
//
// Ref: https://github.com/cosmos/cosmos-sdk/issues/13317
Expand Down
18 changes: 18 additions & 0 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/server"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -375,3 +376,20 @@ func wonkyMsg(t *testing.T, cfg client.TxConfig, tx signing.Tx) signing.Tx {
require.NoError(t, err)
return builder.GetTx()
}

func newMockedVersionModifier(startingVersion uint64) server.VersionModifier {
return &mockedVersionModifier{version: startingVersion}
}

type mockedVersionModifier struct {
version uint64
}

func (m *mockedVersionModifier) SetAppVersion(ctx context.Context, u uint64) error {
m.version = u
return nil
}

func (m *mockedVersionModifier) AppVersion(ctx context.Context) (uint64, error) {
return m.version, nil
}
6 changes: 0 additions & 6 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
Expand Down Expand Up @@ -103,7 +102,6 @@ func init() {
ProvideEnvironment,
ProvideTransientStoreService,
ProvideModuleManager,
ProvideAppVersionModifier,
ProvideCometService,
),
appconfig.Invoke(SetupAppBuilder),
Expand Down Expand Up @@ -293,10 +291,6 @@ func ProvideTransientStoreService(
return transientStoreService{key: storeKey}
}

func ProvideAppVersionModifier(app *AppBuilder) server.VersionModifier {
return app.app
}

func ProvideCometService() comet.Service {
return NewContextAwareCometInfoService()
}
2 changes: 2 additions & 0 deletions simapp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Always refer to the [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/mai
* [#20490](https://github.com/cosmos/cosmos-sdk/pull/20490) Refactor simulations to make use of `testutil/sims` instead of `runsims`.
* [#19726](https://github.com/cosmos/cosmos-sdk/pull/19726) Update APIs to match CometBFT v1.
* [#21466](https://github.com/cosmos/cosmos-sdk/pull/21466) Allow chains to plug in their own public key types in `base.Account`
* [#21508](https://github.com/cosmos/cosmos-sdk/pull/21508) Abstract the way we update the version of the app state in `app.go` using the interface `VersionModifier`.

<!-- TODO: move changelog.md elements to here -->

## v0.47 to v0.50
Expand Down
3 changes: 3 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func NewSimApp(
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[consensustypes.StoreKey]), logger.With(log.ModuleKey, "x/consensus")), govModuleAddr)
bApp.SetParamStore(app.ConsensusParamsKeeper.ParamsStore)

// set the version modifier
bApp.SetVersionModifier(consensus.ProvideAppVersionModifier(app.ConsensusParamsKeeper))

// add keepers
accountsKeeper, err := accounts.NewKeeper(
appCodec,
Expand Down
2 changes: 1 addition & 1 deletion simapp/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
cosmossdk.io/depinject v1.0.0
cosmossdk.io/log v1.4.1
cosmossdk.io/math v1.3.0
cosmossdk.io/runtime/v2 v2.0.0-20240905114452-a57b25418a59 // main
cosmossdk.io/runtime/v2 v2.0.0-20240906155629-dce0365c234b // main
cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b // main
cosmossdk.io/server/v2/cometbft v0.0.0-00010101000000-000000000000
cosmossdk.io/store/v2 v2.0.0-20240906090851-36d9b25e8981 // main
Expand Down
4 changes: 2 additions & 2 deletions simapp/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ cosmossdk.io/log v1.4.1 h1:wKdjfDRbDyZRuWa8M+9nuvpVYxrEOwbD/CA8hvhU8QM=
cosmossdk.io/log v1.4.1/go.mod h1:k08v0Pyq+gCP6phvdI6RCGhLf/r425UT6Rk/m+o74rU=
cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
cosmossdk.io/runtime/v2 v2.0.0-20240905114452-a57b25418a59 h1:9p9X7mTme7w0dznm6Zqr5APd7qhakJZx7CqGJyAYtYE=
cosmossdk.io/runtime/v2 v2.0.0-20240905114452-a57b25418a59/go.mod h1:yrRIRz4pLdpntieuC6DrCx+7AZGcy6zVI1kOEYzZTmo=
cosmossdk.io/runtime/v2 v2.0.0-20240906155629-dce0365c234b h1:jxpSMaMYYbkD7Ydt6YgbBev4txSyeSQhJJyMQ9ZNZrs=
cosmossdk.io/runtime/v2 v2.0.0-20240906155629-dce0365c234b/go.mod h1:dS/uElxFZjZUgwEjjRh8qE84OoavL8Yd6gEdUOGtkjQ=
cosmossdk.io/schema v0.2.0 h1:UH5CR1DqUq8yP+5Np8PbvG4YX0zAUsTN2Qk6yThmfMk=
cosmossdk.io/schema v0.2.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
cosmossdk.io/server/v2 v2.0.0-20240829074658-81a225e6a29b h1:FFixNVq2SbtRlYvr1fB/WikfYTRrA0o/35ImIhhZzrE=
Expand Down
39 changes: 39 additions & 0 deletions x/consensus/depinject.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package consensus

import (
"context"

modulev1 "cosmossdk.io/api/cosmos/consensus/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/server"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
"cosmossdk.io/x/consensus/keeper"
Expand All @@ -23,6 +26,7 @@ func init() {
appconfig.RegisterModule(
&modulev1.Module{},
appconfig.Provide(ProvideModule),
appconfig.Provide(ProvideAppVersionModifier),
)
}

Expand Down Expand Up @@ -64,6 +68,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
m := NewAppModule(in.Cdc, k)
baseappOpt := func(app *baseapp.BaseApp) {
app.SetParamStore(k.ParamsStore)
app.SetVersionModifier(versionModifier{Keeper: k})
}

return ModuleOutputs{
Expand All @@ -72,3 +77,37 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
BaseAppOption: baseappOpt,
}
}

type versionModifier struct {
Keeper keeper.Keeper
}

func (v versionModifier) SetAppVersion(ctx context.Context, version uint64) error {
params, err := v.Keeper.Params(ctx, nil)
if err != nil {
return err
}

updatedParams := params.Params
updatedParams.Version.App = version

err = v.Keeper.ParamsStore.Set(ctx, *updatedParams)
if err != nil {
return err
}

return nil
}

func (v versionModifier) AppVersion(ctx context.Context) (uint64, error) {
params, err := v.Keeper.Params(ctx, nil)
if err != nil {
return 0, err
}

return params.Params.Version.GetApp(), nil
}

func ProvideAppVersionModifier(k keeper.Keeper) server.VersionModifier {
return versionModifier{Keeper: k}
}
19 changes: 19 additions & 0 deletions x/upgrade/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/header"
"cosmossdk.io/core/server"
coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -132,6 +133,7 @@ func setupTest(t *testing.T, height int64, skip map[int64]bool) *TestSuite {
s.env = runtime.NewEnvironment(storeService, coretesting.NewNopLogger(), runtime.EnvWithMsgRouterService(s.baseApp.MsgServiceRouter()), runtime.EnvWithQueryRouterService(s.baseApp.GRPCQueryRouter()))

s.baseApp.SetParamStore(&paramStore{params: cmtproto.ConsensusParams{Version: &cmtproto.VersionParams{App: 1}}})
s.baseApp.SetVersionModifier(newMockedVersionModifier(1))

authority, err := addresscodec.NewBech32Codec("cosmos").BytesToString(authtypes.NewModuleAddress(govModuleName))
require.NoError(t, err)
Expand All @@ -144,6 +146,23 @@ func setupTest(t *testing.T, height int64, skip map[int64]bool) *TestSuite {
return &s
}

func newMockedVersionModifier(startingVersion uint64) server.VersionModifier {
return &mockedVersionModifier{version: startingVersion}
}

type mockedVersionModifier struct {
version uint64
}

func (m *mockedVersionModifier) SetAppVersion(ctx context.Context, u uint64) error {
m.version = u
return nil
}

func (m *mockedVersionModifier) AppVersion(ctx context.Context) (uint64, error) {
return m.version, nil
}

func TestRequireFutureBlock(t *testing.T) {
s := setupTest(t, 10, map[int64]bool{})
err := s.keeper.ScheduleUpgrade(s.ctx, types.Plan{Name: "test", Height: s.ctx.HeaderInfo().Height - 1})
Expand Down
2 changes: 2 additions & 0 deletions x/upgrade/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (s *KeeperTestSuite) SetupTest() {
s.encCfg.TxConfig.TxDecoder(),
)
s.baseApp.SetParamStore(&paramStore{params: cmttypes.DefaultConsensusParams().ToProto()})
s.baseApp.SetVersionModifier(newMockedVersionModifier(0))

appVersion, err := s.baseApp.AppVersion(context.Background())
s.Require().NoError(err)
s.Require().Equal(uint64(0), appVersion)
Expand Down
Loading