Skip to content

Commit

Permalink
feat: support core API genesis in module manager (#14582)
Browse files Browse the repository at this point in the history
Co-authored-by: Facundo Medica <[email protected]>
Co-authored-by: Facundo Medica <[email protected]>
Co-authored-by: Marko <[email protected]>
  • Loading branch information
4 people authored Jan 27, 2023
1 parent 8ab4389 commit aceadb0
Show file tree
Hide file tree
Showing 6 changed files with 558 additions and 43 deletions.
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.

163 changes: 163 additions & 0 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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{}
_ 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

0 comments on commit aceadb0

Please sign in to comment.