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

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