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 23 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
105 changes: 105 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.

161 changes: 161 additions & 0 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package module

import (
"encoding/json"

"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"

storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ AppModuleBasic = coreAppModuleBasicAdapator{}
var _ HasGenesis = coreAppModuleBasicAdapator{}

// CoreAppModuleBasicAdaptor wraps the core API module as an AppModule that this version
// of the SDK can use.
func CoreAppModuleBasicAdaptor(name string, module appmodule.AppModule) AppModuleBasic {
return coreAppModuleBasicAdapator{
name: name,
module: module,
}
}

type coreAppModuleBasicAdapator struct {
name string
module appmodule.AppModule
}

// DefaultGenesis implements HasGenesis
func (c coreAppModuleBasicAdapator) DefaultGenesis(codec.JSONCodec) json.RawMessage {
if mod, ok := c.module.(appmodule.HasGenesis); ok {
target := genesis.RawJSONTarget{}
err := mod.DefaultGenesis(target.Target())
if err != nil {
panic(err)
}

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

return res
}
return nil
}

// ValidateGenesis implements HasGenesis
func (c coreAppModuleBasicAdapator) ValidateGenesis(cdc codec.JSONCodec, txConfig client.TxEncodingConfig, bz json.RawMessage) error {
if mod, ok := c.module.(appmodule.HasGenesis); ok {
source, err := genesis.SourceFromRawJSON(bz)
if err != nil {
return err
}

if err := mod.ValidateGenesis(source); err != nil {
return err
}
}

return nil
}

// ExportGenesis implements HasGenesis
func (c coreAppModuleBasicAdapator) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
if module, ok := c.module.(appmodule.HasGenesis); ok {
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)
}

return rawJSON
}
return nil
}

// InitGenesis implements HasGenesis
func (c coreAppModuleBasicAdapator) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate {
if module, ok := c.module.(appmodule.HasGenesis); ok {
// core API genesis
source, err := genesis.SourceFromRawJSON(bz)
if err != nil {
panic(err)
}

err = module.InitGenesis(ctx, source)
if err != nil {
panic(err)
}
}
return nil
}

// Name implements AppModuleBasic
func (c coreAppModuleBasicAdapator) Name() string {
return c.name
}

// GetQueryCmd implements AppModuleBasic
func (c coreAppModuleBasicAdapator) GetQueryCmd() *cobra.Command {
if mod, ok := c.module.(interface {
GetQueryCmd() *cobra.Command
}); ok {
return mod.GetQueryCmd()
}

return nil
}

// GetTxCmd implements AppModuleBasic
func (c coreAppModuleBasicAdapator) GetTxCmd() *cobra.Command {
if mod, ok := c.module.(interface {
GetTxCmd() *cobra.Command
}); ok {
return mod.GetTxCmd()
}

return nil
}

// RegisterGRPCGatewayRoutes implements AppModuleBasic
func (c coreAppModuleBasicAdapator) RegisterGRPCGatewayRoutes(ctx client.Context, mux *runtime.ServeMux) {
if mod, ok := c.module.(interface {
RegisterGRPCGatewayRoutes(context client.Context, mux *runtime.ServeMux)
}); ok {
mod.RegisterGRPCGatewayRoutes(ctx, mux)
}
}

// RegisterInterfaces implements AppModuleBasic
func (c coreAppModuleBasicAdapator) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
if mod, ok := c.module.(interface {
RegisterInterfaces(registry codectypes.InterfaceRegistry)
}); ok {
mod.RegisterInterfaces(registry)
}
}

// RegisterLegacyAminoCodec implements AppModuleBasic
func (c coreAppModuleBasicAdapator) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) {
if mod, ok := c.module.(interface {
RegisterLegacyAminoCodec(amino *codec.LegacyAmino)
}); ok {
mod.RegisterLegacyAminoCodec(amino)
}
}
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
13 changes: 12 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,10 @@ 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 {
appmodule.AppModule
appmodule.HasGenesis
}
Loading