mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-07-07 05:02:30 +00:00
224 lines
7.6 KiB
Go
224 lines
7.6 KiB
Go
package machine
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"cosmossdk.io/core/appmodule"
|
|
"cosmossdk.io/core/store"
|
|
"cosmossdk.io/depinject"
|
|
"cosmossdk.io/log"
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
|
|
// this line is used by starport scaffolding # 1
|
|
|
|
"github.com/planetmint/planetmint-go/x/machine/types"
|
|
|
|
"github.com/planetmint/planetmint-go/x/machine/keeper"
|
|
|
|
modulev1 "github.com/planetmint/planetmint-go/api/planetmintgo/machine/module"
|
|
)
|
|
|
|
var (
|
|
_ module.AppModuleBasic = (*AppModule)(nil)
|
|
_ module.AppModuleSimulation = (*AppModule)(nil)
|
|
_ module.HasGenesis = (*AppModule)(nil)
|
|
_ module.HasInvariants = (*AppModule)(nil)
|
|
_ module.HasConsensusVersion = (*AppModule)(nil)
|
|
|
|
_ appmodule.AppModule = (*AppModule)(nil)
|
|
_ appmodule.HasBeginBlocker = (*AppModule)(nil)
|
|
_ appmodule.HasEndBlocker = (*AppModule)(nil)
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// AppModuleBasic
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// AppModuleBasic implements the AppModuleBasic interface that defines the
|
|
// independent methods a Cosmos SDK module needs to implement.
|
|
type AppModuleBasic struct {
|
|
cdc codec.BinaryCodec
|
|
}
|
|
|
|
func NewAppModuleBasic(cdc codec.BinaryCodec) AppModuleBasic {
|
|
return AppModuleBasic{cdc: cdc}
|
|
}
|
|
|
|
// Name returns the name of the module as a string.
|
|
func (AppModuleBasic) Name() string {
|
|
return types.ModuleName
|
|
}
|
|
|
|
// RegisterLegacyAminoCodec registers the amino codec for the module, which is used
|
|
// to marshal and unmarshal structs to/from []byte in order to persist them in the module's KVStore.
|
|
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
|
|
|
|
// RegisterInterfaces registers a module's interface types and their concrete implementations as proto.Message.
|
|
func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {
|
|
types.RegisterInterfaces(reg)
|
|
}
|
|
|
|
// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage.
|
|
// The default GenesisState need to be defined by the module developer and is primarily used for testing.
|
|
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
|
|
return cdc.MustMarshalJSON(types.DefaultGenesis())
|
|
}
|
|
|
|
// ValidateGenesis used to validate the GenesisState, given in its json.RawMessage form.
|
|
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
|
|
var genState types.GenesisState
|
|
if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
|
|
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
|
|
}
|
|
return genState.Validate()
|
|
}
|
|
|
|
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
|
|
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
|
|
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// AppModule
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// AppModule implements the AppModule interface that defines the inter-dependent methods that modules need to implement
|
|
type AppModule struct {
|
|
AppModuleBasic
|
|
|
|
keeper keeper.Keeper
|
|
accountKeeper types.AccountKeeper
|
|
bankKeeper types.BankKeeper
|
|
}
|
|
|
|
func NewAppModule(
|
|
cdc codec.Codec,
|
|
keeper keeper.Keeper,
|
|
accountKeeper types.AccountKeeper,
|
|
bankKeeper types.BankKeeper,
|
|
) AppModule {
|
|
return AppModule{
|
|
AppModuleBasic: NewAppModuleBasic(cdc),
|
|
keeper: keeper,
|
|
accountKeeper: accountKeeper,
|
|
bankKeeper: bankKeeper,
|
|
}
|
|
}
|
|
|
|
// 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(), am.keeper)
|
|
|
|
// m := keeper.NewMigrator(am.keeper)
|
|
// if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate1to2); err != nil {
|
|
// panic(fmt.Errorf("failed to register migration of %s to v2: %w", types.ModuleName, err))
|
|
// }
|
|
}
|
|
|
|
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
|
|
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
|
|
|
|
// InitGenesis performs the module's genesis initialization. It returns no validator updates.
|
|
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) {
|
|
var genState types.GenesisState
|
|
// Initialize global index to index in genesis state
|
|
cdc.MustUnmarshalJSON(gs, &genState)
|
|
|
|
InitGenesis(ctx, am.keeper, genState)
|
|
}
|
|
|
|
// ExportGenesis returns the module's exported genesis state as raw JSON bytes.
|
|
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
|
|
genState := ExportGenesis(ctx, am.keeper)
|
|
return cdc.MustMarshalJSON(genState)
|
|
}
|
|
|
|
// ConsensusVersion is a sequence number for state-breaking change of the module.
|
|
// It should be incremented on each consensus-breaking change introduced by the module.
|
|
// To avoid wrong/empty versions, the initial version should be set to 1.
|
|
func (AppModule) ConsensusVersion() uint64 { return 2 }
|
|
|
|
// BeginBlock contains the logic that is automatically triggered at the beginning of each block.
|
|
// The begin block implementation is optional.
|
|
func (am AppModule) BeginBlock(_ context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// EndBlock contains the logic that is automatically triggered at the end of each block.
|
|
// The end block implementation is optional.
|
|
func (am AppModule) EndBlock(_ context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
|
|
func (am AppModule) IsOnePerModuleType() {}
|
|
|
|
// IsAppModule implements the appmodule.AppModule interface.
|
|
func (am AppModule) IsAppModule() {}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// App Wiring Setup
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func init() {
|
|
appmodule.Register(
|
|
&modulev1.Module{},
|
|
appmodule.Provide(ProvideModule),
|
|
)
|
|
}
|
|
|
|
type ModuleInputs struct {
|
|
depinject.In
|
|
|
|
StoreService store.KVStoreService
|
|
Cdc codec.Codec
|
|
Config *modulev1.Module
|
|
Logger log.Logger
|
|
|
|
AccountKeeper types.AccountKeeper
|
|
BankKeeper types.BankKeeper
|
|
// rootDir string TODO: find out if this should be put in ModuleInputs or read from cfg when ProvideModule is called
|
|
}
|
|
|
|
type ModuleOutputs struct {
|
|
depinject.Out
|
|
|
|
MachineKeeper keeper.Keeper
|
|
Module appmodule.AppModule
|
|
}
|
|
|
|
func ProvideModule(in ModuleInputs) ModuleOutputs {
|
|
// default to governance authority if not provided
|
|
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
|
|
if in.Config.Authority != "" {
|
|
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
|
|
}
|
|
k := keeper.NewKeeper(
|
|
in.Cdc,
|
|
in.StoreService,
|
|
in.Logger,
|
|
authority.String(),
|
|
"rootDir", // TODO: hardcoded because of setting up e2e testnetwork see comment in line 192
|
|
)
|
|
m := NewAppModule(
|
|
in.Cdc,
|
|
k,
|
|
in.AccountKeeper,
|
|
in.BankKeeper,
|
|
)
|
|
|
|
return ModuleOutputs{MachineKeeper: k, Module: m}
|
|
}
|