Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support core API genesis in module manager #14582

Merged
merged 28 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e9ffd7f
feat!: implement core API genesis
aaronc Dec 15, 2022
2efcf6b
Merge branch 'main' into aaronc/core-api-genesis-impl
aaronc Dec 15, 2022
3233d21
WIP on tests
aaronc Dec 15, 2022
c0cf8fe
add core API impl
aaronc Dec 15, 2022
bacaf7f
Merge branch 'main' of github.com:cosmos/cosmos-sdk into aaronc/core-…
aaronc Jan 11, 2023
46dfbcd
updates
aaronc Jan 11, 2023
50a354b
lint
aaronc Jan 11, 2023
b5e4c21
Merge branch 'main' into aaronc/core-api-genesis-impl2
aaronc Jan 11, 2023
0491d68
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into aaro…
facundomedica Jan 16, 2023
79ff1b1
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into aaro…
facundomedica Jan 19, 2023
ff21ea5
add some tests + TODO: core app module implements AppModuleBasic
facundomedica Jan 20, 2023
09d0e5c
progress with adapter, sorry for hijacking your PR Aaron
facundomedica Jan 24, 2023
b788b4a
add another test
facundomedica Jan 25, 2023
c4f9248
add more checks
facundomedica Jan 25, 2023
58e8fdc
implement HasGenesis
facundomedica Jan 25, 2023
e23bdc5
add specific tests for core api modules
facundomedica Jan 25, 2023
6b1bd73
merge main
facundomedica Jan 25, 2023
6969a0e
fix
facundomedica Jan 25, 2023
8db2de0
fix import
facundomedica Jan 26, 2023
14e5cbb
Merge branch 'main' into aaronc/core-api-genesis-impl2
facundomedica Jan 26, 2023
88abfd0
fix non deterministic initgenesis slice
facundomedica Jan 26, 2023
6431dc4
Merge branch 'aaronc/core-api-genesis-impl2' of https://github.com/co…
facundomedica Jan 26, 2023
7a1dda9
Merge branch 'main' into aaronc/core-api-genesis-impl2
facundomedica Jan 26, 2023
cd89bfd
fix lint
facundomedica Jan 26, 2023
da0bf09
replace
facundomedica Jan 26, 2023
93e5e20
Merge branch 'aaronc/core-api-genesis-impl2' of https://github.com/co…
facundomedica Jan 26, 2023
7356796
Merge branch 'main' into aaronc/core-api-genesis-impl2
facundomedica Jan 27, 2023
4de2d23
Merge branch 'main' into aaronc/core-api-genesis-impl2
tac0turtle Jan 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions testutil/mock/types_mock_appmodule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions types/module/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package module
15 changes: 14 additions & 1 deletion types/module/mock_appmodule_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package module_test

import "github.com/cosmos/cosmos-sdk/types/module"
import (
"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/types/module"
)

// AppModuleWithAllExtensions is solely here for the purpose of generating
// mocks to be used in module tests.
Expand All @@ -13,3 +17,12 @@ type AppModuleWithAllExtensions interface {
module.BeginBlockAppModule
module.EndBlockAppModule
}

// CoreAppModule is solely here for the purpose of generating
// mocks to be used in module tests.
type CoreAppModule interface {
// TODO: remove module.AppModuleBasic
module.AppModuleBasic
appmodule.AppModule
appmodule.HasGenesis
}
76 changes: 65 additions & 11 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sort"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/genesis"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -100,21 +101,41 @@ func (bm BasicManager) RegisterInterfaces(registry codectypes.InterfaceRegistry)

// DefaultGenesis provides default genesis information for all modules
func (bm BasicManager) DefaultGenesis(cdc codec.JSONCodec) map[string]json.RawMessage {
genesis := make(map[string]json.RawMessage)
genesisData := make(map[string]json.RawMessage)
for _, b := range bm {
if mod, ok := b.(HasGenesisBasics); ok {
genesis[b.Name()] = mod.DefaultGenesis(cdc)
if mod, ok := b.(appmodule.HasGenesis); ok {
target := genesis.RawJSONTarget{}
err := mod.DefaultGenesis(target.Target())
if err != nil {
panic(err)
}

genesisData[b.Name()], err = target.JSON()
if err != nil {
panic(err)
}
} else if mod, ok := b.(HasGenesisBasics); ok {
genesisData[b.Name()] = mod.DefaultGenesis(cdc)
}
}

return genesis
return genesisData
}

// ValidateGenesis performs genesis state validation for all modules
func (bm BasicManager) ValidateGenesis(cdc codec.JSONCodec, txEncCfg client.TxEncodingConfig, genesis map[string]json.RawMessage) error {
func (bm BasicManager) ValidateGenesis(cdc codec.JSONCodec, txEncCfg client.TxEncodingConfig, genesisData map[string]json.RawMessage) error {
for _, b := range bm {
if mod, ok := b.(HasGenesisBasics); ok {
if err := mod.ValidateGenesis(cdc, txEncCfg, genesis[b.Name()]); err != nil {
if mod, ok := b.(appmodule.HasGenesis); ok {
source, err := genesis.SourceFromRawJSON(genesisData[b.Name()])
if err != nil {
return err
}

if err := mod.ValidateGenesis(source); err != nil {
return err
}
} else if mod, ok := b.(HasGenesisBasics); ok {
if err := mod.ValidateGenesis(cdc, txEncCfg, genesisData[b.Name()]); err != nil {
return err
}
}
Expand Down Expand Up @@ -370,9 +391,22 @@ func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, genesisData
continue
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

if module, ok := m.Modules[moduleName].(HasGenesis); ok {
ctx.Logger().Debug("running initialization for module", "module", moduleName)
mod := m.Modules[moduleName]

ctx.Logger().Debug("running initialization for module", "module", moduleName)

if module, ok := mod.(appmodule.HasGenesis); ok {
// core API genesis
source, err := genesis.SourceFromRawJSON(genesisData[moduleName])
if err != nil {
panic(err)
}

err = module.InitGenesis(ctx, source)
if err != nil {
panic(err)
}
} else if module, ok := mod.(HasGenesis); ok {
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
moduleValUpdates := module.InitGenesis(ctx, cdc, genesisData[moduleName])

// use these validator updates if provided, the module manager assumes
Expand Down Expand Up @@ -406,15 +440,34 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec,
if len(modulesToExport) == 0 {
modulesToExport = m.OrderExportGenesis
}

// verify modules exists in app, so that we don't panic in the middle of an export
if err := m.checkModulesExists(modulesToExport); err != nil {
panic(err)
}

channels := make(map[string]chan json.RawMessage)
for _, moduleName := range modulesToExport {
if module, ok := m.Modules[moduleName].(HasGenesis); ok {
mod := m.Modules[moduleName]

if module, ok := mod.(appmodule.HasGenesis); ok {
// core API genesis
channels[moduleName] = make(chan json.RawMessage)
go func(module appmodule.HasGenesis, ch chan json.RawMessage) {
ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions
target := genesis.RawJSONTarget{}
err := module.ExportGenesis(ctx, target.Target())
if err != nil {
panic(err)
}

rawJSON, err := target.JSON()
if err != nil {
panic(err)
}

channels[moduleName] <- rawJSON
}(module, channels[moduleName])
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
} else if module, ok := mod.(HasGenesis); ok {
channels[moduleName] = make(chan json.RawMessage)
go func(module HasGenesis, ch chan json.RawMessage) {
ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions
Expand All @@ -426,6 +479,7 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec,

genesisData := make(map[string]json.RawMessage)
for moduleName := range channels {

genesisData[moduleName] = <-channels[moduleName]
}

Expand Down
Loading