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

     1  package slashing
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"math/rand"
     8  
     9  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	ocabci "github.com/Finschia/ostracon/abci/types"
    14  	abci "github.com/tendermint/tendermint/abci/types"
    15  
    16  	"github.com/Finschia/finschia-sdk/client"
    17  	"github.com/Finschia/finschia-sdk/codec"
    18  	cdctypes "github.com/Finschia/finschia-sdk/codec/types"
    19  	sdk "github.com/Finschia/finschia-sdk/types"
    20  	"github.com/Finschia/finschia-sdk/types/module"
    21  	simtypes "github.com/Finschia/finschia-sdk/types/simulation"
    22  	"github.com/Finschia/finschia-sdk/x/slashing/client/cli"
    23  	"github.com/Finschia/finschia-sdk/x/slashing/keeper"
    24  	"github.com/Finschia/finschia-sdk/x/slashing/simulation"
    25  	"github.com/Finschia/finschia-sdk/x/slashing/types"
    26  )
    27  
    28  var (
    29  	_ module.AppModule           = AppModule{}
    30  	_ module.AppModuleBasic      = AppModuleBasic{}
    31  	_ module.AppModuleSimulation = AppModule{}
    32  	_ module.BeginBlockAppModule = AppModule{}
    33  )
    34  
    35  // AppModuleBasic defines the basic application module used by the slashing module.
    36  type AppModuleBasic struct {
    37  	cdc codec.Codec
    38  }
    39  
    40  // Name returns the slashing module's name.
    41  func (AppModuleBasic) Name() string {
    42  	return types.ModuleName
    43  }
    44  
    45  // RegisterLegacyAminoCodec registers the slashing module's types for the given 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 slashing
    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 slashing 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 types.ValidateGenesis(data)
    69  }
    70  
    71  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the slashig 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 slashing module.
    79  func (AppModuleBasic) GetTxCmd() *cobra.Command {
    80  	return cli.NewTxCmd()
    81  }
    82  
    83  // GetQueryCmd returns no root query command for the slashing module.
    84  func (AppModuleBasic) GetQueryCmd() *cobra.Command {
    85  	return cli.GetQueryCmd()
    86  }
    87  
    88  // AppModule implements an application module for the slashing module.
    89  type AppModule struct {
    90  	AppModuleBasic
    91  
    92  	keeper        keeper.Keeper
    93  	accountKeeper types.AccountKeeper
    94  	bankKeeper    types.BankKeeper
    95  	stakingKeeper types.StakingKeeper
    96  }
    97  
    98  // NewAppModule creates a new AppModule object
    99  func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, bk types.BankKeeper, sk types.StakingKeeper) AppModule {
   100  	return AppModule{
   101  		AppModuleBasic: AppModuleBasic{cdc: cdc},
   102  		keeper:         keeper,
   103  		accountKeeper:  ak,
   104  		bankKeeper:     bk,
   105  		stakingKeeper:  sk,
   106  	}
   107  }
   108  
   109  // Name returns the slashing module's name.
   110  func (AppModule) Name() string {
   111  	return types.ModuleName
   112  }
   113  
   114  // RegisterInvariants registers the slashing module invariants.
   115  func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
   116  
   117  // Route returns the message routing key for the slashing module.
   118  func (am AppModule) Route() sdk.Route {
   119  	return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper))
   120  }
   121  
   122  // QuerierRoute returns the slashing module's querier route name.
   123  func (AppModule) QuerierRoute() string {
   124  	return types.QuerierRoute
   125  }
   126  
   127  // LegacyQuerierHandler returns the slashing 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  	types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
   136  
   137  	// m := keeper.NewMigrator(am.keeper)
   138  	// if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
   139  	// 	panic(fmt.Sprintf("failed to migrate x/slashing from version 1 to 2: %v", err))
   140  	// }
   141  }
   142  
   143  // InitGenesis performs genesis initialization for the slashing module. It returns
   144  // no validator updates.
   145  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   146  	var genesisState types.GenesisState
   147  	cdc.MustUnmarshalJSON(data, &genesisState)
   148  	InitGenesis(ctx, am.keeper, am.stakingKeeper, &genesisState)
   149  	return []abci.ValidatorUpdate{}
   150  }
   151  
   152  // ExportGenesis returns the exported genesis state as raw bytes for the slashing
   153  // module.
   154  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   155  	gs := ExportGenesis(ctx, am.keeper)
   156  	return cdc.MustMarshalJSON(gs)
   157  }
   158  
   159  // ConsensusVersion implements AppModule/ConsensusVersion.
   160  func (AppModule) ConsensusVersion() uint64 { return 1 }
   161  
   162  // BeginBlock returns the begin blocker for the slashing module.
   163  func (am AppModule) BeginBlock(ctx sdk.Context, req ocabci.RequestBeginBlock) {
   164  	BeginBlocker(ctx, req, am.keeper)
   165  }
   166  
   167  // AppModuleSimulation functions
   168  
   169  // GenerateGenesisState creates a randomized GenState of the slashing module.
   170  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   171  	simulation.RandomizedGenState(simState)
   172  }
   173  
   174  // ProposalContents doesn't return any content functions for governance proposals.
   175  func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
   176  	return nil
   177  }
   178  
   179  // RandomizedParams creates randomized slashing param changes for the simulator.
   180  func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
   181  	return simulation.ParamChanges(r)
   182  }
   183  
   184  // RegisterStoreDecoder registers a decoder for slashing module's types
   185  func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   186  	sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
   187  }
   188  
   189  // WeightedOperations returns the all the slashing module operations with their respective weights.
   190  func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
   191  	return simulation.WeightedOperations(
   192  		simState.AppParams, simState.Cdc,
   193  		am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper,
   194  	)
   195  }