github.com/Finschia/finschia-sdk@v0.48.1/x/foundation/module/module.go (about)

     1  package module
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	ocabci "github.com/Finschia/ostracon/abci/types"
     9  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    10  	"github.com/spf13/cobra"
    11  	abci "github.com/tendermint/tendermint/abci/types"
    12  
    13  	"github.com/Finschia/finschia-sdk/client"
    14  	"github.com/Finschia/finschia-sdk/codec"
    15  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    16  	sdk "github.com/Finschia/finschia-sdk/types"
    17  	"github.com/Finschia/finschia-sdk/types/module"
    18  	"github.com/Finschia/finschia-sdk/x/foundation"
    19  	"github.com/Finschia/finschia-sdk/x/foundation/client/cli"
    20  	"github.com/Finschia/finschia-sdk/x/foundation/keeper"
    21  )
    22  
    23  const (
    24  	consensusVersion uint64 = 2
    25  )
    26  
    27  var (
    28  	_ module.AppModule           = AppModule{}
    29  	_ module.AppModuleBasic      = AppModuleBasic{}
    30  	_ module.BeginBlockAppModule = AppModule{}
    31  	_ module.EndBlockAppModule   = AppModule{}
    32  )
    33  
    34  // AppModuleBasic defines the basic application module used by the foundation module.
    35  type AppModuleBasic struct{}
    36  
    37  // Name returns the ModuleName
    38  func (AppModuleBasic) Name() string {
    39  	return foundation.ModuleName
    40  }
    41  
    42  // RegisterLegacyAminoCodec registers the foundation types on the LegacyAmino codec
    43  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
    44  
    45  // DefaultGenesis returns default genesis state as raw bytes for the foundation
    46  // module.
    47  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    48  	return cdc.MustMarshalJSON(foundation.DefaultGenesisState())
    49  }
    50  
    51  // ValidateGenesis performs genesis state validation for the foundation module.
    52  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
    53  	var data foundation.GenesisState
    54  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    55  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", foundation.ModuleName, err)
    56  	}
    57  
    58  	return foundation.ValidateGenesis(data)
    59  }
    60  
    61  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the foundation module.
    62  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
    63  	if err := foundation.RegisterQueryHandlerClient(context.Background(), mux, foundation.NewQueryClient(clientCtx)); err != nil {
    64  		panic(err)
    65  	}
    66  }
    67  
    68  // GetQueryCmd returns the cli query commands for this module
    69  func (AppModuleBasic) GetQueryCmd() *cobra.Command {
    70  	return cli.NewQueryCmd()
    71  }
    72  
    73  // GetTxCmd returns the transaction commands for this module
    74  func (AppModuleBasic) GetTxCmd() *cobra.Command {
    75  	return cli.NewTxCmd()
    76  }
    77  
    78  func (b AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
    79  	foundation.RegisterInterfaces(registry)
    80  }
    81  
    82  //____________________________________________________________________________
    83  
    84  // AppModule implements an application module for the foundation module.
    85  type AppModule struct {
    86  	AppModuleBasic
    87  
    88  	keeper keeper.Keeper
    89  }
    90  
    91  // NewAppModule creates a new AppModule object
    92  func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
    93  	return AppModule{
    94  		keeper: keeper,
    95  	}
    96  }
    97  
    98  // RegisterInvariants does nothing, there are no invariants to enforce
    99  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
   100  	keeper.RegisterInvariants(ir, am.keeper)
   101  }
   102  
   103  // Route is empty, as we do not handle Messages (just proposals)
   104  func (AppModule) Route() sdk.Route { return sdk.Route{} }
   105  
   106  // QuerierRoute returns the route we respond to for abci queries
   107  func (AppModule) QuerierRoute() string { return "" }
   108  
   109  // LegacyQuerierHandler registers a query handler to respond to the module-specific queries
   110  func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
   111  	return nil
   112  }
   113  
   114  // RegisterServices registers a GRPC query service to respond to the
   115  // module-specific GRPC queries.
   116  func (am AppModule) RegisterServices(cfg module.Configurator) {
   117  	foundation.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper))
   118  	foundation.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper))
   119  	keeper.NewMigrator(am.keeper).Register(cfg.RegisterMigration)
   120  }
   121  
   122  // InitGenesis performs genesis initialization for the foundation module. It returns
   123  // no validator updates.
   124  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   125  	var genesisState foundation.GenesisState
   126  	cdc.MustUnmarshalJSON(data, &genesisState)
   127  	if err := am.keeper.InitGenesis(ctx, &genesisState); err != nil {
   128  		panic(err)
   129  	}
   130  	return []abci.ValidatorUpdate{}
   131  }
   132  
   133  // ExportGenesis returns the exported genesis state as raw bytes for the foundation
   134  // module.
   135  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   136  	gs := am.keeper.ExportGenesis(ctx)
   137  	return cdc.MustMarshalJSON(gs)
   138  }
   139  
   140  // ConsensusVersion implements AppModule/ConsensusVersion.
   141  func (AppModule) ConsensusVersion() uint64 { return consensusVersion }
   142  
   143  // BeginBlock performs a no-op.
   144  func (am AppModule) BeginBlock(ctx sdk.Context, _ ocabci.RequestBeginBlock) {
   145  	keeper.BeginBlocker(ctx, am.keeper)
   146  }
   147  
   148  // EndBlock performs a no-op.
   149  func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   150  	keeper.EndBlocker(ctx, am.keeper)
   151  	return []abci.ValidatorUpdate{}
   152  }
   153  
   154  //____________________________________________________________________________
   155  
   156  // AppModuleSimulation functions
   157  
   158  // // GenerateGenesisState creates a randomized GenState of the foundation module.
   159  // func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   160  // 	simulation.RandomizedGenState(simState)
   161  // }
   162  
   163  // // ProposalContents returns all the foundation content functions used to
   164  // // simulate foundation proposals.
   165  // func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
   166  // 	return simulation.ProposalContents()
   167  // }
   168  
   169  // // RandomizedParams creates randomized foundation param changes for the simulator.
   170  // func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
   171  // 	return simulation.ParamChanges(r)
   172  // }
   173  
   174  // // RegisterStoreDecoder registers a decoder for foundation module's types
   175  // func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   176  // 	sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
   177  // }
   178  
   179  // // WeightedOperations returns the all the foundation module operations with their respective weights.
   180  // func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
   181  // 	return simulation.WeightedOperations(
   182  // 		simState.AppParams, simState.Cdc,
   183  // 		am.stakingKeeper, am.slashingKeeper, am.keeper, simState.Contents,
   184  // 	)
   185  // }