-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support core API genesis in module manager (#14582)
Co-authored-by: Facundo Medica <[email protected]> Co-authored-by: Facundo Medica <[email protected]> Co-authored-by: Marko <[email protected]>
- Loading branch information
1 parent
8ab4389
commit aceadb0
Showing
6 changed files
with
558 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.