From daf87aabc574422e1331955815a8a915c75807a9 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 1 Feb 2024 22:33:45 +0100 Subject: [PATCH 01/10] feat(core): add migrations registering --- core/appmodule/migrations.go | 16 ++++++++++++++++ core/appmodule/module.go | 26 +++++++++++++++++--------- runtime/services/autocli.go | 2 ++ types/module/configurator.go | 10 ++++++++++ x/staking/go.mod | 11 ++++++----- x/staking/go.sum | 21 ++++++++------------- x/staking/module.go | 33 ++++++++++++++++++++------------- 7 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 core/appmodule/migrations.go diff --git a/core/appmodule/migrations.go b/core/appmodule/migrations.go new file mode 100644 index 000000000000..63c724108b65 --- /dev/null +++ b/core/appmodule/migrations.go @@ -0,0 +1,16 @@ +package appmodule + +import "context" + +type MigrationRegistrar interface { + // Register registers an in-place store migration for a module. The + // handler is a migration script to perform in-place migrations from version + // `fromVersion` to version `fromVersion+1`. + // + // EACH TIME a module's ConsensusVersion increments, a new migration MUST + // be registered using this function. If a migration handler is missing for + // a particular function, the upgrade logic (see RunMigrations function) + // will panic. If the ConsensusVersion bump does not introduce any store + // changes, then a no-op function must be registered here. + Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error +} diff --git a/core/appmodule/module.go b/core/appmodule/module.go index 192dbaacc3e0..60e6f540b3ce 100644 --- a/core/appmodule/module.go +++ b/core/appmodule/module.go @@ -38,17 +38,12 @@ type HasServices interface { RegisterServices(grpc.ServiceRegistrar) error } -// HasPrepareCheckState is an extension interface that contains information about the AppModule -// and PrepareCheckState. -type HasPrepareCheckState interface { +// HasMigrations is the extension interface that modules should implement to register migrations. +type HasMigrations interface { AppModule - PrepareCheckState(context.Context) error -} -// HasPrecommit is an extension interface that contains information about the AppModule and Precommit. -type HasPrecommit interface { - AppModule - Precommit(context.Context) error + // RegisterMigrations registers the module's migrations with the app's migrator. + RegisterMigrations(MigrationRegistrar) error } // ResponsePreBlock represents the response from the PreBlock method. @@ -86,3 +81,16 @@ type HasEndBlocker interface { // a block. EndBlock(context.Context) error } + +// HasPrepareCheckState is an extension interface that contains information about the AppModule +// and PrepareCheckState. +type HasPrepareCheckState interface { + AppModule + PrepareCheckState(context.Context) error +} + +// HasPrecommit is an extension interface that contains information about the AppModule and Precommit. +type HasPrecommit interface { + AppModule + Precommit(context.Context) error +} diff --git a/runtime/services/autocli.go b/runtime/services/autocli.go index 66c70b31e7ee..5c58c2cd62b2 100644 --- a/runtime/services/autocli.go +++ b/runtime/services/autocli.go @@ -113,6 +113,8 @@ func (a *autocliConfigurator) RegisterMigration(string, uint64, module.Migration return nil } +func (a *autocliConfigurator) Register(string, uint64, func(context.Context) error) error { return nil } + func (a *autocliConfigurator) RegisterService(sd *grpc.ServiceDesc, ss interface{}) { if a.registryCache == nil { a.registryCache, a.err = proto.MergedRegistry() diff --git a/types/module/configurator.go b/types/module/configurator.go index 970f868b3697..6c566dd9ca65 100644 --- a/types/module/configurator.go +++ b/types/module/configurator.go @@ -1,6 +1,7 @@ package module import ( + "context" "fmt" "github.com/cosmos/gogoproto/grpc" @@ -45,6 +46,8 @@ type Configurator interface { // will panic. If the ConsensusVersion bump does not introduce any store // changes, then a no-op function must be registered here. RegisterMigration(moduleName string, fromVersion uint64, handler MigrationHandler) error + + Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error } type configurator struct { @@ -119,6 +122,13 @@ func (c *configurator) RegisterMigration(moduleName string, fromVersion uint64, return nil } +// Register implements the Configurator.Register method +func (c *configurator) Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error { + return c.RegisterMigration(moduleName, fromVersion, func(sdkCtx sdk.Context) error { + return handler(sdkCtx) + }) +} + // runModuleMigrations runs all in-place store migrations for one given module from a // version to another version. func (c *configurator) runModuleMigrations(ctx sdk.Context, moduleName string, fromVersion, toVersion uint64) error { diff --git a/x/staking/go.mod b/x/staking/go.mod index 151f1c728c0f..c1d11afb6400 100644 --- a/x/staking/go.mod +++ b/x/staking/go.mod @@ -29,14 +29,13 @@ require ( gotest.tools/v3 v3.5.1 ) -require cosmossdk.io/x/bank v0.0.0-00010101000000-000000000000 // indirect - require ( cosmossdk.io/x/auth v0.0.0-00010101000000-000000000000 + cosmossdk.io/x/bank v0.0.0-00010101000000-000000000000 // indirect cosmossdk.io/x/tx v0.13.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect - github.com/99designs/keyring v1.2.1 // indirect + github.com/99designs/keyring v1.2.2 // indirect github.com/DataDog/datadog-go v4.8.3+incompatible // indirect github.com/DataDog/zstd v1.5.5 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect @@ -58,7 +57,7 @@ require ( github.com/cosmos/iavl v1.0.0 // indirect github.com/cosmos/ics23/go v0.10.0 // indirect github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect - github.com/danieljoos/wincred v1.1.2 // indirect + github.com/danieljoos/wincred v1.2.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect @@ -72,7 +71,7 @@ require ( github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/getsentry/sentry-go v0.26.0 // indirect - github.com/go-kit/kit v0.12.0 // indirect + github.com/go-kit/kit v0.13.0 // indirect github.com/go-kit/log v0.2.1 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect @@ -165,7 +164,9 @@ replace github.com/cosmos/cosmos-sdk => ../../. // TODO remove post spinning out all modules replace ( + cosmossdk.io/core => ../../core cosmossdk.io/depinject => ../../depinject cosmossdk.io/x/auth => ../auth cosmossdk.io/x/bank => ../bank + cosmossdk.io/x/tx => ../tx ) diff --git a/x/staking/go.sum b/x/staking/go.sum index 4d2159b607f1..e83c10c711ad 100644 --- a/x/staking/go.sum +++ b/x/staking/go.sum @@ -4,8 +4,6 @@ cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a h1:Zr++x1RCJWi+K8bTZsQKdjt cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a/go.mod h1:7B/5XWh1HYwJk3DzWeNoxOSI+nGx1m5UyYfHLFuKzkw= cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s= cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0= -cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7 h1:hOzi4yo2Fc7h3mod+xX4m4QA4+Uq+PkFRjY/yalZ0B8= -cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7/go.mod h1:3v0JJNNd8ye0cOvJ+wUUvE7Ke0d2qxnNIDcXd5mziHk= cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0= cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U= cosmossdk.io/log v1.3.0 h1:L0Z0XstClo2kOU4h3V1iDoE5Ji64sg5HLOogzGg67Oo= @@ -14,15 +12,13 @@ cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig= cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0= cosmossdk.io/store v1.0.2 h1:lSg5BTvJBHUDwswNNyeh4K/CbqiHER73VU4nDNb8uk0= cosmossdk.io/store v1.0.2/go.mod h1:EFtENTqVTuWwitGW1VwaBct+yDagk7oG/axBMPH+FXs= -cosmossdk.io/x/tx v0.13.0 h1:8lzyOh3zONPpZv2uTcUmsv0WTXy6T1/aCVDCqShmpzU= -cosmossdk.io/x/tx v0.13.0/go.mod h1:CpNQtmoqbXa33/DVxWQNx5Dcnbkv2xGUhL7tYQ5wUsY= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= -github.com/99designs/keyring v1.2.1 h1:tYLp1ULvO7i3fI5vE21ReQuj99QFSs7lGm0xWyJo87o= -github.com/99designs/keyring v1.2.1/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA= +github.com/99designs/keyring v1.2.2 h1:pZd3neh/EmUzWONb35LxQfvuY7kiSXAq3HQd97+XBn0= +github.com/99designs/keyring v1.2.2/go.mod h1:wes/FrByc8j7lFOAGLGSNEg8f/PaI3cgTBqhFkHUrPk= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -76,8 +72,8 @@ github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipus github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= -github.com/bufbuild/protocompile v0.6.0 h1:Uu7WiSQ6Yj9DbkdnOe7U4mNKp58y9WDMKDn28/ZlunY= -github.com/bufbuild/protocompile v0.6.0/go.mod h1:YNP35qEYoYGme7QMtz5SBCoN4kL4g12jTtjuzRNdjpE= +github.com/bufbuild/protocompile v0.5.1 h1:mixz5lJX4Hiz4FpqFREJHIXLfaLBntfaJv1h+/jS+Qg= +github.com/bufbuild/protocompile v0.5.1/go.mod h1:G5iLmavmF4NsYtpZFvE3B/zFch2GIY8+wjsYLR/lc40= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= @@ -152,8 +148,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0= -github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0= +github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE= +github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -228,8 +224,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2 github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o= -github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4= -github.com/go-kit/kit v0.12.0/go.mod h1:lHd+EkCZPIwYItmGDDRdhinkzX2A1sj+M9biaEaizzs= +github.com/go-kit/kit v0.13.0 h1:OoneCcHKHQ03LfBpoQCUfCluwd2Vt3ohz+kvbJneZAU= +github.com/go-kit/kit v0.13.0/go.mod h1:phqEHMMUbyrCFCTgH48JueqrM3md2HcAZ8N3XE4FKDg= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= @@ -853,7 +849,6 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/x/staking/module.go b/x/staking/module.go index bc2841106ef1..ed28a6c68fc9 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -8,6 +8,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" @@ -29,7 +30,6 @@ const ( var ( _ module.AppModuleBasic = AppModuleBasic{} _ module.AppModuleSimulation = AppModule{} - _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} _ module.HasABCIGenesis = AppModule{} _ module.HasABCIEndBlock = AppModule{} @@ -37,6 +37,8 @@ var ( _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the staking module. @@ -120,24 +122,29 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - querier := keeper.Querier{Keeper: am.keeper} - types.RegisterQueryServer(cfg.QueryServer(), querier) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, keeper.NewQuerier(am.keeper)) + return nil +} + +func (am AppModule) RegisterMigrations(mh appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) + if err := mh.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) + if err := mh.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) + if err := mh.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err)) + if err := mh.Register(types.ModuleName, 4, m.Migrate4to5); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err) } + + return nil } // InitGenesis performs genesis initialization for the staking module. From 72b2176cb72488145f18121dca7ae1ef60b689c2 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 7 Feb 2024 13:44:45 +0100 Subject: [PATCH 02/10] updates --- types/module/configurator.go | 3 +++ x/staking/keeper/migrations.go | 13 +++++++------ x/staking/migrations/v5/migrations_test.go | 7 +++++-- x/staking/migrations/v5/store.go | 7 ++++--- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/types/module/configurator.go b/types/module/configurator.go index 6c566dd9ca65..df335c6461c7 100644 --- a/types/module/configurator.go +++ b/types/module/configurator.go @@ -21,6 +21,8 @@ import ( // their services in the RegisterServices method. It is designed to eventually // support module object capabilities isolation as described in // https://github.com/cosmos/cosmos-sdk/issues/7093 +// Deprecated: The Configurator is deprecated. +// Preferrably use core services for registering msg/query server and migrations. type Configurator interface { grpc.Server @@ -123,6 +125,7 @@ func (c *configurator) RegisterMigration(moduleName string, fromVersion uint64, } // Register implements the Configurator.Register method +// It permis to register modules migrations that have migrated to serverv2 but still be compatible with baseapp. func (c *configurator) Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error { return c.RegisterMigration(moduleName, fromVersion, func(sdkCtx sdk.Context) error { return handler(sdkCtx) diff --git a/x/staking/keeper/migrations.go b/x/staking/keeper/migrations.go index 623f16a23045..f04dc799976f 100644 --- a/x/staking/keeper/migrations.go +++ b/x/staking/keeper/migrations.go @@ -1,10 +1,11 @@ package keeper import ( + "context" + v5 "cosmossdk.io/x/staking/migrations/v5" "github.com/cosmos/cosmos-sdk/runtime" - sdk "github.com/cosmos/cosmos-sdk/types" ) // Migrator is a struct for handling in-place store migrations. @@ -20,22 +21,22 @@ func NewMigrator(keeper *Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } // Migrate2to3 migrates x/staking state from consensus version 2 to 3. -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return nil } // Migrate3to4 migrates x/staking state from consensus version 3 to 4. -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { return nil } // Migrate4to5 migrates x/staking state from consensus version 4 to 5. -func (m Migrator) Migrate4to5(ctx sdk.Context) error { +func (m Migrator) Migrate4to5(ctx context.Context) error { store := runtime.KVStoreAdapter(m.keeper.storeService.OpenKVStore(ctx)) - return v5.MigrateStore(ctx, store, m.keeper.cdc) + return v5.MigrateStore(ctx, store, m.keeper.cdc, m.keeper.Logger(ctx)) } diff --git a/x/staking/migrations/v5/migrations_test.go b/x/staking/migrations/v5/migrations_test.go index 537ae6983270..c21f1c53dd8a 100644 --- a/x/staking/migrations/v5/migrations_test.go +++ b/x/staking/migrations/v5/migrations_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "cosmossdk.io/log" sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/staking" @@ -28,6 +29,7 @@ func TestHistoricalKeysMigration(t *testing.T) { tKey := storetypes.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) + logger := log.NewTestLogger(t) type testCase struct { oldKey, newKey []byte @@ -63,7 +65,7 @@ func TestHistoricalKeysMigration(t *testing.T) { } // migrate store to new key format - require.NoErrorf(t, v5.MigrateStore(ctx, store, cdc), "v5.MigrateStore failed, seed: %d", seed) + require.NoErrorf(t, v5.MigrateStore(ctx, store, cdc, logger), "v5.MigrateStore failed, seed: %d", seed) // check results for _, tc := range testCases { @@ -83,6 +85,7 @@ func TestDelegationsByValidatorMigrations(t *testing.T) { tKey := storetypes.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) + logger := log.NewTestLogger(t) accAddrs := sims.CreateIncrementalAccounts(11) valAddrs := sims.ConvertAddrsToValAddrs(accAddrs[0:1]) @@ -98,7 +101,7 @@ func TestDelegationsByValidatorMigrations(t *testing.T) { dels := getValDelegations(ctx, cdc, storeKey, valAddrs[0]) assert.Len(t, dels, 0) - err := v5.MigrateStore(ctx, store, cdc) + err := v5.MigrateStore(ctx, store, cdc, logger) assert.NoError(t, err) // after migration the state of delegations by val index should not be empty diff --git a/x/staking/migrations/v5/store.go b/x/staking/migrations/v5/store.go index a572d35d8f9b..38ff590b3271 100644 --- a/x/staking/migrations/v5/store.go +++ b/x/staking/migrations/v5/store.go @@ -1,6 +1,7 @@ package v5 import ( + "context" "fmt" "strconv" @@ -12,7 +13,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func migrateDelegationsByValidatorIndex(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error { +func migrateDelegationsByValidatorIndex(ctx context.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error { iterator := storetypes.KVStorePrefixIterator(store, DelegationKey) for ; iterator.Valid(); iterator.Next() { @@ -29,11 +30,11 @@ func migrateDelegationsByValidatorIndex(ctx sdk.Context, store storetypes.KVStor } // MigrateStore performs in-place store migrations from v4 to v5. -func MigrateStore(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error { +func MigrateStore(ctx context.Context, store storetypes.KVStore, cdc codec.BinaryCodec, logger log.Logger) error { if err := migrateDelegationsByValidatorIndex(ctx, store, cdc); err != nil { return err } - return migrateHistoricalInfoKeys(store, ctx.Logger()) + return migrateHistoricalInfoKeys(store, logger) } // migrateHistoricalInfoKeys migrate HistoricalInfo keys to binary format From a4e7719db887a91819ff81b2413510e8a839031b Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 7 Feb 2024 17:51:18 +0100 Subject: [PATCH 03/10] updates --- docs/build/building-modules/13-upgrade.md | 2 ++ types/module/configurator.go | 2 +- x/slashing/keeper/migrations.go | 9 ++++--- x/slashing/migrations/v4/migrate.go | 14 ++++++----- x/slashing/module.go | 30 +++++++++++++++-------- x/staking/module.go | 10 ++++---- x/upgrade/keeper/migrations.go | 10 ++++---- x/upgrade/module.go | 27 ++++++++++++-------- 8 files changed, 63 insertions(+), 41 deletions(-) diff --git a/docs/build/building-modules/13-upgrade.md b/docs/build/building-modules/13-upgrade.md index 908a6a06eeb0..932b88f82649 100644 --- a/docs/build/building-modules/13-upgrade.md +++ b/docs/build/building-modules/13-upgrade.md @@ -49,6 +49,8 @@ Since these migrations are functions that need access to a Keeper's store, use a https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/x/bank/keeper/migrations.go ``` + + ## Writing Migration Scripts To define the functionality that takes place during an upgrade, write a migration script and place the functions in a `migrations/` directory. For example, to write migration scripts for the bank module, place the functions in `x/bank/migrations/`. Use the recommended naming convention for these functions. For example, `v2bank` is the script that migrates the package `x/bank/migrations/v2`: diff --git a/types/module/configurator.go b/types/module/configurator.go index df335c6461c7..3cf2945ffc10 100644 --- a/types/module/configurator.go +++ b/types/module/configurator.go @@ -22,7 +22,7 @@ import ( // support module object capabilities isolation as described in // https://github.com/cosmos/cosmos-sdk/issues/7093 // Deprecated: The Configurator is deprecated. -// Preferrably use core services for registering msg/query server and migrations. +// Preferably use core services for registering msg/query server and migrations. type Configurator interface { grpc.Server diff --git a/x/slashing/keeper/migrations.go b/x/slashing/keeper/migrations.go index 62102332e9bc..735eede4de08 100644 --- a/x/slashing/keeper/migrations.go +++ b/x/slashing/keeper/migrations.go @@ -1,10 +1,11 @@ package keeper import ( + "context" + v4 "cosmossdk.io/x/slashing/migrations/v4" "github.com/cosmos/cosmos-sdk/runtime" - sdk "github.com/cosmos/cosmos-sdk/types" ) // Migrator is a struct for handling in-place store migrations. @@ -18,7 +19,7 @@ func NewMigrator(keeper Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } @@ -26,14 +27,14 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error { // version 2 to version 3. Specifically, it takes the parameters that are currently stored // and managed by the x/params modules and stores them directly into the x/slashing // module state. -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return nil } // Migrate3to4 migrates the x/slashing module state from the consensus // version 3 to version 4. Specifically, it migrates the validator missed block // bitmap. -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { store := runtime.KVStoreAdapter(m.keeper.storeService.OpenKVStore(ctx)) params, err := m.keeper.Params.Get(ctx) if err != nil { diff --git a/x/slashing/migrations/v4/migrate.go b/x/slashing/migrations/v4/migrate.go index 43dd2128d922..f81566996e68 100644 --- a/x/slashing/migrations/v4/migrate.go +++ b/x/slashing/migrations/v4/migrate.go @@ -1,6 +1,8 @@ package v4 import ( + "context" + "github.com/bits-and-blooms/bitset" gogotypes "github.com/cosmos/gogoproto/types" @@ -15,7 +17,7 @@ import ( // Migrate migrates state to consensus version 4. Specifically, the migration // deletes all existing validator bitmap entries and replaces them with a real // "chunked" bitmap. -func Migrate(ctx sdk.Context, cdc codec.BinaryCodec, store storetypes.KVStore, params types.Params) error { +func Migrate(ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, params types.Params) error { // Get all the missed blocks for each validator, based on the existing signing // info. var missedBlocks []types.ValidatorMissedBlocks @@ -57,7 +59,7 @@ func Migrate(ctx sdk.Context, cdc codec.BinaryCodec, store storetypes.KVStore, p } func iterateValidatorSigningInfos( - ctx sdk.Context, + ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, cb func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool), @@ -77,7 +79,7 @@ func iterateValidatorSigningInfos( } func iterateValidatorMissedBlockBitArray( - ctx sdk.Context, + ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, addr sdk.ConsAddress, @@ -99,7 +101,7 @@ func iterateValidatorMissedBlockBitArray( } func GetValidatorMissedBlocks( - ctx sdk.Context, + ctx context.Context, cdc codec.BinaryCodec, store storetypes.KVStore, addr sdk.ConsAddress, @@ -114,7 +116,7 @@ func GetValidatorMissedBlocks( return missedBlocks } -func deleteValidatorMissedBlockBitArray(ctx sdk.Context, store storetypes.KVStore, addr sdk.ConsAddress) { +func deleteValidatorMissedBlockBitArray(ctx context.Context, store storetypes.KVStore, addr sdk.ConsAddress) { iter := storetypes.KVStorePrefixIterator(store, validatorMissedBlockBitArrayPrefixKey(addr)) defer iter.Close() @@ -123,7 +125,7 @@ func deleteValidatorMissedBlockBitArray(ctx sdk.Context, store storetypes.KVStor } } -func setMissedBlockBitmapValue(ctx sdk.Context, store storetypes.KVStore, addr sdk.ConsAddress, index int64, missed bool) error { +func setMissedBlockBitmapValue(ctx context.Context, store storetypes.KVStore, addr sdk.ConsAddress, index int64, missed bool) error { // get the chunk or "word" in the logical bitmap chunkIndex := index / MissedBlockBitmapChunkSize key := ValidatorMissedBlockBitmapKey(addr, chunkIndex) diff --git a/x/slashing/module.go b/x/slashing/module.go index ea2b4c9ad947..6dbbb135c22d 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -6,6 +6,7 @@ import ( "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/slashing/keeper" @@ -26,10 +27,11 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the slashing module. @@ -109,21 +111,29 @@ func NewAppModule( func (am AppModule) IsAppModule() {} // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper)) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, keeper.NewQuerier(am.keeper)) + return nil +} + +func (am AppModule) RegisterMigrations(mh appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) + + if err := mh.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) + if err := mh.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) + + if err := mh.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %w", types.ModuleName, err) } + + return nil } // InitGenesis performs genesis initialization for the slashing module. It returns diff --git a/x/staking/module.go b/x/staking/module.go index ed28a6c68fc9..0883ca5ad842 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -129,18 +129,18 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { return nil } -func (am AppModule) RegisterMigrations(mh appmodule.MigrationRegistrar) error { +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := mh.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err) } - if err := mh.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err) } - if err := mh.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { + if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err) } - if err := mh.Register(types.ModuleName, 4, m.Migrate4to5); err != nil { + if err := mr.Register(types.ModuleName, 4, m.Migrate4to5); err != nil { return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err) } diff --git a/x/upgrade/keeper/migrations.go b/x/upgrade/keeper/migrations.go index f6a514654c90..ada16e6eb0f2 100644 --- a/x/upgrade/keeper/migrations.go +++ b/x/upgrade/keeper/migrations.go @@ -1,6 +1,7 @@ package keeper import ( + "context" "encoding/binary" "fmt" @@ -9,7 +10,6 @@ import ( "cosmossdk.io/x/upgrade/types" "github.com/cosmos/cosmos-sdk/runtime" - sdk "github.com/cosmos/cosmos-sdk/types" ) const ( @@ -28,11 +28,11 @@ func NewMigrator(keeper *Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return migrateDoneUpgradeKeys(ctx, m.keeper.storeService) } -func migrateDoneUpgradeKeys(ctx sdk.Context, storeService storetypes.KVStoreService) error { +func migrateDoneUpgradeKeys(ctx context.Context, storeService storetypes.KVStoreService) error { store := storeService.OpenKVStore(ctx) oldDoneStore := prefix.NewStore(runtime.KVStoreAdapter(store), []byte{types.DoneByte}) oldDoneStoreIter := oldDoneStore.Iterator(nil, nil) @@ -57,11 +57,11 @@ func migrateDoneUpgradeKeys(ctx sdk.Context, storeService storetypes.KVStoreServ // Migrate2to3 migrates from version 2 to 3. // It takes the legacy protocol version and if it exists, uses it to set // the app version (of the baseapp) -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return migrateAppVersion(ctx, m.keeper) } -func migrateAppVersion(ctx sdk.Context, keeper *Keeper) error { +func migrateAppVersion(ctx context.Context, keeper *Keeper) error { if keeper.versionModifier == nil { return fmt.Errorf("version modifier is not set") } diff --git a/x/upgrade/module.go b/x/upgrade/module.go index ea647b5e1d94..4b11f44a811e 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -7,6 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" @@ -30,10 +31,11 @@ const ConsensusVersion uint64 = 3 var ( _ module.AppModuleBasic = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasPreBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic implements the sdk.AppModuleBasic interface @@ -84,19 +86,24 @@ func NewAppModule(keeper *keeper.Keeper, ac address.Codec) AppModule { func (am AppModule) IsAppModule() {} // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, am.keeper) + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2) - if err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } - err = cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3) - if err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) + + if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err) } + + return nil } // InitGenesis is ignored, no sense in serializing future upgrades From 53dc459f95ee0cae251136652c7895590bd21e86 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 7 Feb 2024 23:55:54 +0100 Subject: [PATCH 04/10] updates --- x/mint/keeper/migrator.go | 6 ++---- x/mint/module.go | 24 ++++++++++++++++-------- x/params/module.go | 14 ++++++++------ x/protocolpool/module.go | 17 ++++++++++------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/x/mint/keeper/migrator.go b/x/mint/keeper/migrator.go index f0161568dc5e..855f3cd7154c 100644 --- a/x/mint/keeper/migrator.go +++ b/x/mint/keeper/migrator.go @@ -1,8 +1,6 @@ package keeper -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) +import "context" // Migrator is a struct for handling in-place state migrations. type Migrator struct { @@ -20,6 +18,6 @@ func NewMigrator(k Keeper) Migrator { // version 2. Specifically, it takes the parameters that are currently stored // and managed by the x/params modules and stores them directly into the x/mint // module state. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } diff --git a/x/mint/module.go b/x/mint/module.go index 0f1e3492cac1..d2fc4358ce5a 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -6,6 +6,7 @@ import ( "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/mint/keeper" @@ -26,10 +27,11 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the mint module. @@ -110,17 +112,23 @@ func NewAppModule( // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} -// RegisterServices registers a gRPC query service to respond to the -// module-specific gRPC queries. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper)) +// RegisterServices registers module services. +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, keeper.NewQueryServerImpl(am.keeper)) + return nil +} + +// RegisterMigrations registers module migrations. +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } + + return nil } // InitGenesis performs genesis initialization for the mint module. It returns diff --git a/x/params/module.go b/x/params/module.go index e2b2ac61f539..9ec3095c56a4 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -4,6 +4,7 @@ import ( "context" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/params/keeper" @@ -19,9 +20,9 @@ import ( var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} - _ module.HasServices = AppModule{} - _ appmodule.AppModule = AppModule{} + _ appmodule.AppModule = AppModule{} + _ appmodule.HasServices = AppModule{} ) // ConsensusVersion defines the current x/params module consensus version. @@ -72,10 +73,11 @@ func (am AppModule) IsAppModule() {} // GenerateGenesisState performs a no-op. func (AppModule) GenerateGenesisState(simState *module.SimulationState) {} -// RegisterServices registers a gRPC query service to respond to the -// module-specific gRPC queries. -func (am AppModule) RegisterServices(cfg module.Configurator) { - proposal.RegisterQueryServer(cfg.QueryServer(), am.keeper) +// RegisterServices registers module services. +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + proposal.RegisterQueryServer(registrar, am.keeper) + + return nil } // RegisterStoreDecoder doesn't register any type. diff --git a/x/protocolpool/module.go b/x/protocolpool/module.go index ae4efdc4e9a4..35d36b027fe2 100644 --- a/x/protocolpool/module.go +++ b/x/protocolpool/module.go @@ -6,6 +6,7 @@ import ( "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/protocolpool/keeper" @@ -21,11 +22,13 @@ import ( const ConsensusVersion = 1 var ( - _ module.AppModuleBasic = AppModule{} - + _ module.AppModuleBasic = AppModule{} _ module.AppModule = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} + + _ appmodule.AppModule = AppModule{} + _ appmodule.HasServices = AppModule{} ) // AppModuleBasic defines the basic application module used by the pool module. @@ -76,15 +79,15 @@ type AppModule struct { bankKeeper types.BankKeeper } -var _ appmodule.AppModule = AppModule{} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper)) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, keeper.NewQuerier(am.keeper)) + + return nil } // NewAppModule creates a new AppModule object From 3f40376cb7ea5e3e44704c959cccb897e86dba11 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Feb 2024 00:02:27 +0100 Subject: [PATCH 05/10] updates --- x/circuit/module.go | 13 ++++++++----- x/crisis/keeper/migrator.go | 6 ++---- x/crisis/module.go | 19 ++++++++++++++----- x/feegrant/keeper/migrations.go | 6 +++--- x/feegrant/module/module.go | 26 +++++++++++++++++--------- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/x/circuit/module.go b/x/circuit/module.go index b2e61cb19548..c39000ce1676 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -7,6 +7,7 @@ import ( "time" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/circuit/keeper" @@ -25,9 +26,9 @@ const ConsensusVersion = 1 var ( _ module.AppModuleBasic = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} - _ appmodule.AppModule = AppModule{} + _ appmodule.AppModule = AppModule{} + _ appmodule.HasServices = AppModule{} ) // AppModuleBasic defines the basic application module used by the circuit module. @@ -81,9 +82,11 @@ type AppModule struct { func (am AppModule) IsAppModule() {} // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper)) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, keeper.NewQueryServer(am.keeper)) + + return nil } // NewAppModule creates a new AppModule object diff --git a/x/crisis/keeper/migrator.go b/x/crisis/keeper/migrator.go index fe4c0d8cf599..7929d86054fd 100644 --- a/x/crisis/keeper/migrator.go +++ b/x/crisis/keeper/migrator.go @@ -1,8 +1,6 @@ package keeper -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) +import "context" // Migrator is a struct for handling in-place state migrations. type Migrator struct { @@ -20,6 +18,6 @@ func NewMigrator(k *Keeper) Migrator { // version 2. Specifically, it takes the parameters that are currently stored // and managed by the x/params modules and stores them directly into the x/crisis // module state. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } diff --git a/x/crisis/module.go b/x/crisis/module.go index a82b9ab3ff4d..ddc1510efe20 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -8,6 +8,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" @@ -26,10 +27,11 @@ const ConsensusVersion = 2 var ( _ module.AppModuleBasic = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // Module init related flags @@ -109,13 +111,20 @@ func AddModuleInitFlags(startCmd *cobra.Command) { } // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), am.keeper) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, am.keeper) + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) + + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } + + return nil } // InitGenesis performs genesis initialization for the crisis module. It returns diff --git a/x/feegrant/keeper/migrations.go b/x/feegrant/keeper/migrations.go index ccb583197179..0d2b9a05509e 100644 --- a/x/feegrant/keeper/migrations.go +++ b/x/feegrant/keeper/migrations.go @@ -1,9 +1,9 @@ package keeper import ( - v2 "cosmossdk.io/x/feegrant/migrations/v2" + "context" - sdk "github.com/cosmos/cosmos-sdk/types" + v2 "cosmossdk.io/x/feegrant/migrations/v2" ) // Migrator is a struct for handling in-place store migrations. @@ -17,6 +17,6 @@ func NewMigrator(keeper Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return v2.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc) } diff --git a/x/feegrant/module/module.go b/x/feegrant/module/module.go index 82f1f52a2e8f..b67d1e17f5b9 100644 --- a/x/feegrant/module/module.go +++ b/x/feegrant/module/module.go @@ -7,6 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/errors" @@ -23,11 +24,12 @@ import ( var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} - _ module.HasServices = AppModule{} _ module.HasGenesis = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // ---------------------------------------------------------------------------- @@ -44,16 +46,22 @@ func (ab AppModuleBasic) Name() string { return feegrant.ModuleName } -// RegisterServices registers a gRPC query service to respond to the -// module-specific gRPC queries. -func (am AppModule) RegisterServices(cfg module.Configurator) { - feegrant.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - feegrant.RegisterQueryServer(cfg.QueryServer(), am.keeper) +// RegisterServices registers module services. +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + feegrant.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + feegrant.RegisterQueryServer(registrar, am.keeper) + + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - err := cfg.RegisterMigration(feegrant.ModuleName, 1, m.Migrate1to2) - if err != nil { - panic(fmt.Sprintf("failed to migrate x/feegrant from version 1 to 2: %v", err)) + + if err := mr.Register(feegrant.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/feegrant from version 1 to 2: %w", err) } + + return nil } // RegisterLegacyAminoCodec registers the feegrant module's types for the given codec. From 6153444317780f00fc791189b1226373e9f05b3c Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Feb 2024 00:56:39 +0100 Subject: [PATCH 06/10] updates --- docs/build/building-modules/13-upgrade.md | 2 +- types/module/configurator.go | 2 + types/module/core_module.go | 7 ++++ types/module/module.go | 7 ++++ x/auth/keeper/migrations.go | 19 ++++----- x/auth/module.go | 40 ++++++++++-------- x/authz/keeper/migrations.go | 6 +-- x/authz/module/module.go | 25 +++++++---- x/bank/keeper/migrations.go | 10 ++--- x/bank/module.go | 30 +++++++++----- x/distribution/keeper/migrations.go | 12 +++--- x/distribution/migrations/v4/migrate.go | 2 +- x/distribution/migrations/v4/migrate_funds.go | 3 +- x/distribution/module.go | 28 ++++++++----- x/gov/keeper/migrations.go | 14 +++---- x/gov/migrations/v5/store.go | 5 ++- x/gov/migrations/v6/store.go | 5 +-- x/gov/module.go | 41 +++++++++++-------- x/group/keeper/migrations.go | 1 - x/group/module/module.go | 31 +++++++------- 20 files changed, 171 insertions(+), 119 deletions(-) diff --git a/docs/build/building-modules/13-upgrade.md b/docs/build/building-modules/13-upgrade.md index 932b88f82649..fc3dc384b3bc 100644 --- a/docs/build/building-modules/13-upgrade.md +++ b/docs/build/building-modules/13-upgrade.md @@ -49,7 +49,7 @@ Since these migrations are functions that need access to a Keeper's store, use a https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/x/bank/keeper/migrations.go ``` - + ## Writing Migration Scripts diff --git a/types/module/configurator.go b/types/module/configurator.go index 3cf2945ffc10..2423e8d98d4f 100644 --- a/types/module/configurator.go +++ b/types/module/configurator.go @@ -49,6 +49,8 @@ type Configurator interface { // changes, then a no-op function must be registered here. RegisterMigration(moduleName string, fromVersion uint64, handler MigrationHandler) error + // Register registers an in-place store migration for a module. + // It permits to register modules migrations that have migrated to serverv2 but still be compatible with baseapp. Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error } diff --git a/types/module/core_module.go b/types/module/core_module.go index 128fa0c51410..de2d816ab1cf 100644 --- a/types/module/core_module.go +++ b/types/module/core_module.go @@ -200,6 +200,13 @@ func (c coreAppModuleBasicAdaptor) RegisterServices(cfg Configurator) { panic(err) } } + + if module, ok := c.module.(appmodule.HasMigrations); ok { + err := module.RegisterMigrations(cfg) + if err != nil { + panic(err) + } + } } func (c coreAppModuleBasicAdaptor) IsOnePerModuleType() {} diff --git a/types/module/module.go b/types/module/module.go index 9fd7bc1c7750..91ccd7ab3dae 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -467,6 +467,13 @@ func (m *Manager) RegisterServices(cfg Configurator) error { } } + if module, ok := module.(appmodule.HasMigrations); ok { + err := module.RegisterMigrations(cfg) + if err != nil { + return err + } + } + if cfg.Error() != nil { return cfg.Error() } diff --git a/x/auth/keeper/migrations.go b/x/auth/keeper/migrations.go index f7017d20ad81..2951a9d54b35 100644 --- a/x/auth/keeper/migrations.go +++ b/x/auth/keeper/migrations.go @@ -1,7 +1,7 @@ package keeper import ( - "github.com/cosmos/gogoproto/grpc" + "context" v5 "cosmossdk.io/x/auth/migrations/v5" "cosmossdk.io/x/auth/types" @@ -11,23 +11,22 @@ import ( // Migrator is a struct for handling in-place store migrations. type Migrator struct { - keeper AccountKeeper - queryServer grpc.Server + keeper AccountKeeper } // NewMigrator returns a new Migrator. -func NewMigrator(keeper AccountKeeper, queryServer grpc.Server) Migrator { - return Migrator{keeper: keeper, queryServer: queryServer} +func NewMigrator(keeper AccountKeeper) Migrator { + return Migrator{keeper: keeper} } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } // Migrate2to3 migrates from consensus version 2 to version 3. Specifically, for each account // we index the account's ID to their address. -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return nil } @@ -35,14 +34,14 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error { // version 4. Specifically, it takes the parameters that are currently stored // and managed by the x/params modules and stores them directly into the x/auth // module state. -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { return nil } // Migrate4To5 migrates the x/auth module state from the consensus version 4 to 5. // It migrates the GlobalAccountNumber from being a protobuf defined value to a // big-endian encoded uint64, it also migrates it to use a more canonical prefix. -func (m Migrator) Migrate4To5(ctx sdk.Context) error { +func (m Migrator) Migrate4To5(ctx context.Context) error { return v5.Migrate(ctx, m.keeper.storeService, m.keeper.AccountNumber) } @@ -50,7 +49,7 @@ func (m Migrator) Migrate4To5(ctx sdk.Context) error { // set the account without map to accAddr to accNumber. // // NOTE: This is used for testing purposes only. -func (m Migrator) V45SetAccount(ctx sdk.Context, acc sdk.AccountI) error { +func (m Migrator) V45SetAccount(ctx context.Context, acc sdk.AccountI) error { addr := acc.GetAddress() store := m.keeper.storeService.OpenKVStore(ctx) diff --git a/x/auth/module.go b/x/auth/module.go index 1421f8ffd652..8646f4096fee 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -6,6 +6,7 @@ import ( "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "google.golang.org/grpc" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" @@ -30,9 +31,10 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} - _ appmodule.AppModule = AppModule{} + _ appmodule.AppModule = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the auth module. @@ -98,27 +100,31 @@ func NewAppModule(cdc codec.Codec, accountKeeper keeper.AccountKeeper, randGenAc } } -// RegisterServices registers a GRPC query service to respond to the -// module-specific GRPC queries. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.accountKeeper)) - types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.accountKeeper)) +// RegisterServices registers module services. +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.accountKeeper)) + types.RegisterQueryServer(registrar, keeper.NewQueryServer(am.accountKeeper)) - m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer()) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) - } + return nil +} - if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { + m := keeper.NewMigrator(am.accountKeeper) + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) + if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4To5); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5", types.ModuleName)) + if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %w", types.ModuleName, err) } + if err := mr.Register(types.ModuleName, 4, m.Migrate4To5); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %w", types.ModuleName, err) + } + + return nil } // InitGenesis performs genesis initialization for the auth module. It returns diff --git a/x/authz/keeper/migrations.go b/x/authz/keeper/migrations.go index 8d4ea3c2303c..b67e16433b26 100644 --- a/x/authz/keeper/migrations.go +++ b/x/authz/keeper/migrations.go @@ -1,9 +1,9 @@ package keeper import ( - v2 "cosmossdk.io/x/authz/migrations/v2" + "context" - sdk "github.com/cosmos/cosmos-sdk/types" + v2 "cosmossdk.io/x/authz/migrations/v2" ) // Migrator is a struct for handling in-place store migrations. @@ -17,6 +17,6 @@ func NewMigrator(keeper Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return v2.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc) } diff --git a/x/authz/module/module.go b/x/authz/module/module.go index 089b7e2c55dc..7298d4c97abc 100644 --- a/x/authz/module/module.go +++ b/x/authz/module/module.go @@ -7,6 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/errors" @@ -26,10 +27,11 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the authz module. @@ -42,16 +44,21 @@ func (AppModuleBasic) Name() string { return authz.ModuleName } -// RegisterServices registers a gRPC query service to respond to the -// module-specific gRPC queries. -func (am AppModule) RegisterServices(cfg module.Configurator) { - authz.RegisterQueryServer(cfg.QueryServer(), am.keeper) - authz.RegisterMsgServer(cfg.MsgServer(), am.keeper) +// RegisterServices registers module services. +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + authz.RegisterQueryServer(registrar, am.keeper) + authz.RegisterMsgServer(registrar, am.keeper) + + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - err := cfg.RegisterMigration(authz.ModuleName, 1, m.Migrate1to2) - if err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", authz.ModuleName, err)) + if err := mr.Register(authz.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", authz.ModuleName, err) } + + return nil } // RegisterLegacyAminoCodec registers the authz module's types for the given codec. diff --git a/x/bank/keeper/migrations.go b/x/bank/keeper/migrations.go index 843462d3c765..5ac819c27236 100644 --- a/x/bank/keeper/migrations.go +++ b/x/bank/keeper/migrations.go @@ -1,8 +1,6 @@ package keeper -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) +import "context" // Migrator is a struct for handling in-place store migrations. type Migrator struct { @@ -15,16 +13,16 @@ func NewMigrator(keeper BaseKeeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } // Migrate2to3 migrates x/bank storage from version 2 to 3. -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return nil } // Migrate3to4 migrates x/bank storage from version 3 to 4. -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { return nil } diff --git a/x/bank/module.go b/x/bank/module.go index 3b5541e0101f..7e9556fe6699 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -8,6 +8,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/bank/client/cli" @@ -31,10 +32,11 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} - _ appmodule.AppModule = AppModule{} + _ appmodule.AppModule = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the bank module. @@ -95,23 +97,29 @@ type AppModule struct { func (am AppModule) IsAppModule() {} // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, am.keeper) + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper.(keeper.BaseKeeper)) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/bank from version 1 to 2: %v", err)) + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/bank from version 1 to 2: %w", err) } - if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { - panic(fmt.Sprintf("failed to migrate x/bank from version 2 to 3: %v", err)) + if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/bank from version 2 to 3: %w", err) } - if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { - panic(fmt.Sprintf("failed to migrate x/bank from version 3 to 4: %v", err)) + if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { + return fmt.Errorf("failed to migrate x/bank from version 3 to 4: %w", err) } + + return nil } // NewAppModule creates a new AppModule object diff --git a/x/distribution/keeper/migrations.go b/x/distribution/keeper/migrations.go index 4152a9a6faea..6d32eae61ebe 100644 --- a/x/distribution/keeper/migrations.go +++ b/x/distribution/keeper/migrations.go @@ -1,10 +1,10 @@ package keeper import ( + "context" + v4 "cosmossdk.io/x/distribution/migrations/v4" "cosmossdk.io/x/distribution/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) // Migrator is a struct for handling in-place store migrations. @@ -18,7 +18,7 @@ func NewMigrator(keeper Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } @@ -26,13 +26,13 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error { // version 2 to version 3. Specifically, it takes the parameters that are currently stored // and managed by the x/params module and stores them directly into the x/distribution // module state. -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return nil } // Migrate3to4 migrates the x/distribution module state to use collections // Additionally it migrates distribution fee pool to use protocol pool module account -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { if err := v4.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc); err != nil { return err } @@ -40,7 +40,7 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error { return m.migrateFunds(ctx) } -func (m Migrator) migrateFunds(ctx sdk.Context) error { +func (m Migrator) migrateFunds(ctx context.Context) error { macc := m.keeper.GetDistributionAccount(ctx) poolMacc := m.keeper.authKeeper.GetModuleAccount(ctx, types.ProtocolPoolModuleName) diff --git a/x/distribution/migrations/v4/migrate.go b/x/distribution/migrations/v4/migrate.go index d5abfe43d200..b09902b0dd22 100644 --- a/x/distribution/migrations/v4/migrate.go +++ b/x/distribution/migrations/v4/migrate.go @@ -19,7 +19,7 @@ var ( NewProposerKey = collections.NewPrefix(1) ) -func MigrateStore(ctx sdk.Context, storeService store.KVStoreService, cdc codec.BinaryCodec) error { +func MigrateStore(ctx context.Context, storeService store.KVStoreService, cdc codec.BinaryCodec) error { store := storeService.OpenKVStore(ctx) bz, err := store.Get(OldProposerKey) if err != nil { diff --git a/x/distribution/migrations/v4/migrate_funds.go b/x/distribution/migrations/v4/migrate_funds.go index bf2df7c82294..135fc5296c71 100644 --- a/x/distribution/migrations/v4/migrate_funds.go +++ b/x/distribution/migrations/v4/migrate_funds.go @@ -1,6 +1,7 @@ package v4 import ( + "context" "fmt" "cosmossdk.io/x/distribution/types" @@ -9,7 +10,7 @@ import ( ) // MigrateFunds migrates the distribution module funds to pool module -func MigrateFunds(ctx sdk.Context, bankKeeper types.BankKeeper, feePool types.FeePool, macc, poolMacc sdk.ModuleAccountI) (types.FeePool, error) { +func MigrateFunds(ctx context.Context, bankKeeper types.BankKeeper, feePool types.FeePool, macc, poolMacc sdk.ModuleAccountI) (types.FeePool, error) { poolBal, remainder := feePool.CommunityPool.TruncateDecimal() distrbalances := bankKeeper.GetAllBalances(ctx, macc.GetAddress()) diff --git a/x/distribution/module.go b/x/distribution/module.go index d5fa23d054c1..e965bc54506c 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -7,6 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/distribution/client/cli" @@ -29,11 +30,12 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the distribution module. @@ -119,22 +121,28 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper)) +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) + types.RegisterQueryServer(registrar, keeper.NewQuerier(am.keeper)) + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) + if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) + if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err) } - if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) + if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { + return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %w", types.ModuleName, err) } + + return nil } // InitGenesis performs genesis initialization for the distribution module. It returns diff --git a/x/gov/keeper/migrations.go b/x/gov/keeper/migrations.go index 945b44ffbc25..5d7226a3b563 100644 --- a/x/gov/keeper/migrations.go +++ b/x/gov/keeper/migrations.go @@ -1,10 +1,10 @@ package keeper import ( + "context" + v5 "cosmossdk.io/x/gov/migrations/v5" v6 "cosmossdk.io/x/gov/migrations/v6" - - sdk "github.com/cosmos/cosmos-sdk/types" ) // Migrator is a struct for handling in-place store migrations. @@ -20,26 +20,26 @@ func NewMigrator(keeper *Keeper) Migrator { } // Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { +func (m Migrator) Migrate1to2(ctx context.Context) error { return nil } // Migrate2to3 migrates from version 2 to 3. -func (m Migrator) Migrate2to3(ctx sdk.Context) error { +func (m Migrator) Migrate2to3(ctx context.Context) error { return nil } // Migrate3to4 migrates from version 3 to 4. -func (m Migrator) Migrate3to4(ctx sdk.Context) error { +func (m Migrator) Migrate3to4(ctx context.Context) error { return nil } // Migrate4to5 migrates from version 4 to 5. -func (m Migrator) Migrate4to5(ctx sdk.Context) error { +func (m Migrator) Migrate4to5(ctx context.Context) error { return v5.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc, m.keeper.Constitution) } // Migrate4to5 migrates from version 5 to 6. -func (m Migrator) Migrate5to6(ctx sdk.Context) error { +func (m Migrator) Migrate5to6(ctx context.Context) error { return v6.MigrateStore(ctx, m.keeper.Params, m.keeper.Proposals) } diff --git a/x/gov/migrations/v5/store.go b/x/gov/migrations/v5/store.go index 62f7328bba0f..b4c86f51e6b9 100644 --- a/x/gov/migrations/v5/store.go +++ b/x/gov/migrations/v5/store.go @@ -1,12 +1,13 @@ package v5 import ( + "context" + "cosmossdk.io/collections" corestoretypes "cosmossdk.io/core/store" govv1 "cosmossdk.io/x/gov/types/v1" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" ) var ( @@ -21,7 +22,7 @@ var ( // // Addition of the new proposal expedited parameters that are set to 0 by default. // Set of default chain constitution. -func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec, constitutionCollection collections.Item[string]) error { +func MigrateStore(ctx context.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec, constitutionCollection collections.Item[string]) error { store := storeService.OpenKVStore(ctx) paramsBz, err := store.Get(ParamsKey) if err != nil { diff --git a/x/gov/migrations/v6/store.go b/x/gov/migrations/v6/store.go index 2485d21a1aac..4676511700c7 100644 --- a/x/gov/migrations/v6/store.go +++ b/x/gov/migrations/v6/store.go @@ -1,12 +1,11 @@ package v6 import ( + "context" "fmt" "cosmossdk.io/collections" v1 "cosmossdk.io/x/gov/types/v1" - - sdk "github.com/cosmos/cosmos-sdk/types" ) // MigrateStore performs in-place store migrations from v5 (v0.50) to v6 (v0.51). The @@ -14,7 +13,7 @@ import ( // // Addition of new field in params to store types of proposals that can be submitted. // Addition of gov params for optimistic proposals. -func MigrateStore(ctx sdk.Context, paramsCollection collections.Item[v1.Params], proposalCollection collections.Map[uint64, v1.Proposal]) error { +func MigrateStore(ctx context.Context, paramsCollection collections.Item[v1.Params], proposalCollection collections.Map[uint64, v1.Proposal]) error { // Migrate proposals err := proposalCollection.Walk(ctx, nil, func(key uint64, proposal v1.Proposal) (bool, error) { if proposal.Expedited { diff --git a/x/gov/module.go b/x/gov/module.go index 52fb641283bd..8f42af98a43b 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -7,6 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" @@ -32,11 +33,12 @@ var ( _ module.AppModuleBasic = AppModuleBasic{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) // AppModuleBasic defines the basic application module used by the gov module. @@ -144,35 +146,40 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // RegisterServices registers module services. -func (am AppModule) RegisterServices(cfg module.Configurator) { +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { msgServer := keeper.NewMsgServerImpl(am.keeper) - v1beta1.RegisterMsgServer(cfg.MsgServer(), keeper.NewLegacyMsgServerImpl(am.accountKeeper.GetModuleAddress(govtypes.ModuleName).String(), msgServer)) - v1.RegisterMsgServer(cfg.MsgServer(), msgServer) + v1beta1.RegisterMsgServer(registrar, keeper.NewLegacyMsgServerImpl(am.accountKeeper.GetModuleAddress(govtypes.ModuleName).String(), msgServer)) + v1.RegisterMsgServer(registrar, msgServer) - legacyQueryServer := keeper.NewLegacyQueryServer(am.keeper) - v1beta1.RegisterQueryServer(cfg.QueryServer(), legacyQueryServer) - v1.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper)) + v1beta1.RegisterQueryServer(registrar, keeper.NewLegacyQueryServer(am.keeper)) + v1.RegisterQueryServer(registrar, keeper.NewQueryServer(am.keeper)) + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(govtypes.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/gov from version 1 to 2: %v", err)) + if err := mr.Register(govtypes.ModuleName, 1, m.Migrate1to2); err != nil { + return fmt.Errorf("failed to migrate x/gov from version 1 to 2: %w", err) } - if err := cfg.RegisterMigration(govtypes.ModuleName, 2, m.Migrate2to3); err != nil { - panic(fmt.Sprintf("failed to migrate x/gov from version 2 to 3: %v", err)) + if err := mr.Register(govtypes.ModuleName, 2, m.Migrate2to3); err != nil { + return fmt.Errorf("failed to migrate x/gov from version 2 to 3: %w", err) } - if err := cfg.RegisterMigration(govtypes.ModuleName, 3, m.Migrate3to4); err != nil { - panic(fmt.Sprintf("failed to migrate x/gov from version 3 to 4: %v", err)) + if err := mr.Register(govtypes.ModuleName, 3, m.Migrate3to4); err != nil { + return fmt.Errorf("failed to migrate x/gov from version 3 to 4: %w", err) } - if err := cfg.RegisterMigration(govtypes.ModuleName, 4, m.Migrate4to5); err != nil { - panic(fmt.Sprintf("failed to migrate x/gov from version 4 to 5: %v", err)) + if err := mr.Register(govtypes.ModuleName, 4, m.Migrate4to5); err != nil { + return fmt.Errorf("failed to migrate x/gov from version 4 to 5: %w", err) } - if err := cfg.RegisterMigration(govtypes.ModuleName, 5, m.Migrate5to6); err != nil { - panic(fmt.Sprintf("failed to migrate x/gov from version 5 to 6: %v", err)) + if err := mr.Register(govtypes.ModuleName, 5, m.Migrate5to6); err != nil { + return fmt.Errorf("failed to migrate x/gov from version 5 to 6: %w", err) } + + return nil } // InitGenesis performs genesis initialization for the gov module. It returns diff --git a/x/group/keeper/migrations.go b/x/group/keeper/migrations.go index 0f5b40e6c6fa..ee60cec421ca 100644 --- a/x/group/keeper/migrations.go +++ b/x/group/keeper/migrations.go @@ -2,7 +2,6 @@ package keeper import ( v2 "cosmossdk.io/x/group/migrations/v2" - sdk "github.com/cosmos/cosmos-sdk/types" ) diff --git a/x/group/module/module.go b/x/group/module/module.go index a91c618f58bc..4863877dd511 100644 --- a/x/group/module/module.go +++ b/x/group/module/module.go @@ -7,6 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "google.golang.org/grpc" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" @@ -30,11 +31,12 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} - _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} + _ appmodule.HasServices = AppModule{} + _ appmodule.HasMigrations = AppModule{} ) type AppModule struct { @@ -124,16 +126,21 @@ func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json return cdc.MustMarshalJSON(gs) } -// RegisterServices registers a gRPC query service to respond to the -// module-specific gRPC queries. -func (am AppModule) RegisterServices(cfg module.Configurator) { - group.RegisterMsgServer(cfg.MsgServer(), am.keeper) - group.RegisterQueryServer(cfg.QueryServer(), am.keeper) +// RegisterServices registers module services. +func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { + group.RegisterMsgServer(registrar, am.keeper) + group.RegisterQueryServer(registrar, am.keeper) - m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(group.ModuleName, 1, m.Migrate1to2); err != nil { - panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", group.ModuleName, err)) - } + return nil +} + +func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { // TODO, group doesn't use kvstore service, so we cannot migrate it + // m := keeper.NewMigrator(am.keeper) + // if err := mr.Register(group.ModuleName, 1, m.Migrate1to2); err != nil { + // return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", group.ModuleName, err) + // } + + return nil } // ConsensusVersion implements AppModule/ConsensusVersion. @@ -145,10 +152,6 @@ func (am AppModule) EndBlock(ctx context.Context) error { return EndBlocker(c, am.keeper) } -// ____________________________________________________________________________ - -// AppModuleSimulation functions - // GenerateGenesisState creates a randomized GenState of the group module. func (AppModule) GenerateGenesisState(simState *module.SimulationState) { simulation.RandomizedGenState(simState) From ea80fb4eb4ee2b3e545c28efaa4ac01eb2f148b0 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Feb 2024 01:07:49 +0100 Subject: [PATCH 07/10] docs --- CHANGELOG.md | 9 ++--- UPGRADING.md | 92 +++++++++++++++++++++++------------------------ core/CHANGELOG.md | 3 +- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8c604af528..a88078e162b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,10 +74,6 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i * (crypto | x/auth) [#14372](https://github.com/cosmos/cosmos-sdk/pull/18194) Key checks on signatures antehandle. * (types) [#18963](https://github.com/cosmos/cosmos-sdk/pull/18963) Swap out amino json encoding of `ABCIMessageLogs` for std lib json encoding -### Deprecated - -* (simapp) [#19146](https://github.com/cosmos/cosmos-sdk/pull/19146) Replace `--v` CLI option with `--validator-count`/`-n`. - ### Bug Fixes * (baseapp) [#19338](https://github.com/cosmos/cosmos-sdk/pull/19338) Set HeaderInfo in context when calling `setState`. @@ -144,6 +140,11 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i * (server) [#18303](https://github.com/cosmos/cosmos-sdk/pull/18303) `appd export` has moved with other genesis commands, use `appd genesis export` instead. +### Deprecated + +* (simapp) [#19146](https://github.com/cosmos/cosmos-sdk/pull/19146) Replace `--v` CLI option with `--validator-count`/`-n`. +* (module) [#19370](https://github.com/cosmos/cosmos-sdk/pull/19370) Depcreate ` module.Configurator`, use `appmodule.HasMigrations` and `appmodule.HasServicecs` instead from Core API. + ## [v0.50.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.3) - 2023-01-15 ### Features diff --git a/UPGRADING.md b/UPGRADING.md index 3c481857b056..697a073891b0 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -5,7 +5,45 @@ Note, always read the **SimApp** section for more information on application wir ## [Unreleased] -### Unordered Transactions +### SimApp + +In this section we describe the changes made in Cosmos SDK' SimApp. +**These changes are directly applicable to your application wiring.** + +#### AnteHandlers + +The GasConsumptionDecorator and IncreaseSequenceDecorator have been merged with the SigVerificationDecorator, so you'll +need to remove them both from your app.go code, they will yield to unresolvable symbols when compiling. + +#### Client (`root.go`) + +The `client` package has been refactored to make use of the address codecs (address, validator address, consensus address, etc.). +This is part of the work of abstracting the SDK from the global bech32 config. + +This means the address codecs must be provided in the `client.Context` in the application client (usually `root.go`). + +```diff +clientCtx = clientCtx. ++ WithAddressCodec(addressCodec). ++ WithValidatorAddressCodec(validatorAddressCodec). ++ WithConsensusAddressCodec(consensusAddressCodec) +``` + +**When using `depinject` / `app v2`, the client codecs can be provided directly from application config.** + +Refer to SimApp `root_v2.go` and `root.go` for an example with an app v2 and a legacy app. + +### Core + +`appmodule.Environment` interface was introduced to fetch different services from the application. This can be used as an alternative to using `sdk.UnwrapContext(ctx)` to fetch the services. It needs to be passed into a module at instantiation. + +Circuit Breaker is used as an example. + +```go +app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment((keys[circuittypes.StoreKey]), nil), appCodec, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec()) +``` + +#### Unordered Transactions The Cosmos SDK now supports unordered transactions. This means that transactions can be executed in any order and doesn't require the client to deal with or manage @@ -80,57 +118,19 @@ used as a TTL for the transaction and is used to provide replay protection. See [ADR-070](https://github.com/cosmos/cosmos-sdk/blob/main/docs/architecture/adr-070-unordered-transactions.md) for more details. -### Params - -* Params migrations were removed. It is required to migrate to 0.50 prior to upgrading to v0.51. - -### SimApp - -In this section we describe the changes made in Cosmos SDK' SimApp. -**These changes are directly applicable to your application wiring.** - -#### AnteHandlers - -The GasConsumptionDecorator and IncreaseSequenceDecorator have been merged with the SigVerificationDecorator, so you'll -need to remove them both from your app.go code, they will yield to unresolvable symbols when compiling. - -#### Client (`root.go`) - -The `client` package has been refactored to make use of the address codecs (address, validator address, consensus address, etc.). -This is part of the work of abstracting the SDK from the global bech32 config. - -This means the address codecs must be provided in the `client.Context` in the application client (usually `root.go`). - -```diff -clientCtx = clientCtx. -+ WithAddressCodec(addressCodec). -+ WithValidatorAddressCodec(validatorAddressCodec). -+ WithConsensusAddressCodec(consensusAddressCodec) -``` - -**When using `depinject` / `app v2`, the client codecs can be provided directly from application config.** - -Refer to SimApp `root_v2.go` and `root.go` for an example with an app v2 and a legacy app. - -#### Dependency Injection - - - -### Core +### Modules -`appmodule.Environment` interface was introduced to fetch different services from the application. This can be used as an alternative to using `sdk.UnwrapContext(ctx)` to fetch the services. It needs to be passed into a module at instantiation. +#### `**all**` -Circuit Breaker is used as an example. +##### Params -```go -app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment((keys[circuittypes.StoreKey]), nil), appCodec, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec()) -``` +Old module migrations have been removed. It is required to migrate to v0.50 prior to upgrading to v0.51 for not missing any module migrations. -### Modules +##### Core API -#### `**all**` +Core API has been introduces for modules in v0.47. With the deprecation of `sdk.Context`, we strongly recommend to use the `cosmossdk.io/core/appmodule` interfaces for the modules. This will allow the modules to work out of the box with server/v2 and baseapp, as well as limit their dependencies on the SDK. -##### Dependency Injection +##### Dependency Injection Previously `cosmossdk.io/core` held functions `Invoke`, `Provide` and `Register` were moved to `cosmossdk.io/depinject/appconfig`. All modules using dependency injection must update their imports. diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 4f5d609d73c3..27ee7b8be825 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -41,8 +41,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#18379](https://github.com/cosmos/cosmos-sdk/pull/18379) Add branch service. * [#18457](https://github.com/cosmos/cosmos-sdk/pull/18457) Add branch.ExecuteWithGasLimit. * [#19041](https://github.com/cosmos/cosmos-sdk/pull/19041) Add `appmodule.Environment` interface to fetch different services +* [#19370](https://github.com/cosmos/cosmos-sdk/pull/19370) Add `appmodule.Migrations` interface to handle migrations -### API Breaking +### API Breaking Changes * [#18857](https://github.com/cosmos/cosmos-sdk/pull/18857) Moved `FormatCoins` to `x/tx`. * [#18861](httpes://github.com/cosmos/cosmos-sdk/pull/18861) Moved `coin.ParseCoin` to `client/v2/internal`. From ac089cd2b44000caa8096139dee32b54ef4a8eb4 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Feb 2024 09:00:07 +0100 Subject: [PATCH 08/10] revert group as not supported --- x/group/keeper/migrations.go | 1 + x/group/module/module.go | 27 ++++++++++----------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/x/group/keeper/migrations.go b/x/group/keeper/migrations.go index ee60cec421ca..0f5b40e6c6fa 100644 --- a/x/group/keeper/migrations.go +++ b/x/group/keeper/migrations.go @@ -2,6 +2,7 @@ package keeper import ( v2 "cosmossdk.io/x/group/migrations/v2" + sdk "github.com/cosmos/cosmos-sdk/types" ) diff --git a/x/group/module/module.go b/x/group/module/module.go index 4863877dd511..e9823ccd7ca1 100644 --- a/x/group/module/module.go +++ b/x/group/module/module.go @@ -7,7 +7,6 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - "google.golang.org/grpc" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" @@ -31,12 +30,11 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} + _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} - _ appmodule.HasServices = AppModule{} - _ appmodule.HasMigrations = AppModule{} ) type AppModule struct { @@ -126,21 +124,16 @@ func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json return cdc.MustMarshalJSON(gs) } -// RegisterServices registers module services. -func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { - group.RegisterMsgServer(registrar, am.keeper) - group.RegisterQueryServer(registrar, am.keeper) +// RegisterServices registers a gRPC query service to respond to the +// module-specific gRPC queries. +func (am AppModule) RegisterServices(cfg module.Configurator) { + group.RegisterMsgServer(cfg.MsgServer(), am.keeper) + group.RegisterQueryServer(cfg.QueryServer(), am.keeper) - return nil -} - -func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { // TODO, group doesn't use kvstore service, so we cannot migrate it - // m := keeper.NewMigrator(am.keeper) - // if err := mr.Register(group.ModuleName, 1, m.Migrate1to2); err != nil { - // return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", group.ModuleName, err) - // } - - return nil + m := keeper.NewMigrator(am.keeper) + if err := cfg.RegisterMigration(group.ModuleName, 1, m.Migrate1to2); err != nil { + panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", group.ModuleName, err)) + } } // ConsensusVersion implements AppModule/ConsensusVersion. From 2e0b1d2206f72ba274f87f6aab4375f7d0a38e25 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Feb 2024 09:07:56 +0100 Subject: [PATCH 09/10] updates --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a88078e162b1..ad97108cf7c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -143,7 +143,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Deprecated * (simapp) [#19146](https://github.com/cosmos/cosmos-sdk/pull/19146) Replace `--v` CLI option with `--validator-count`/`-n`. -* (module) [#19370](https://github.com/cosmos/cosmos-sdk/pull/19370) Depcreate ` module.Configurator`, use `appmodule.HasMigrations` and `appmodule.HasServicecs` instead from Core API. +* (module) [#19370](https://github.com/cosmos/cosmos-sdk/pull/19370) Deprecate `module.Configurator`, use `appmodule.HasMigrations` and `appmodule.HasServicecs` instead from Core API. ## [v0.50.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.3) - 2023-01-15 From 394a1e7b1c6e7f16b199848d82d04c91b72e62ea Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 8 Feb 2024 10:44:41 +0100 Subject: [PATCH 10/10] feedback Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com> --- types/module/configurator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/module/configurator.go b/types/module/configurator.go index 2423e8d98d4f..f396b289b1ac 100644 --- a/types/module/configurator.go +++ b/types/module/configurator.go @@ -127,7 +127,7 @@ func (c *configurator) RegisterMigration(moduleName string, fromVersion uint64, } // Register implements the Configurator.Register method -// It permis to register modules migrations that have migrated to serverv2 but still be compatible with baseapp. +// It allows to register modules migrations that have migrated to server/v2 but still be compatible with baseapp. func (c *configurator) Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error { return c.RegisterMigration(moduleName, fromVersion, func(sdkCtx sdk.Context) error { return handler(sdkCtx)