github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/slashing/module.go (about)

     1  package slashing
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  
     8  	"github.com/gorilla/mux"
     9  	"github.com/spf13/cobra"
    10  
    11  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    12  
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    15  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    16  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    17  	sim "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
    18  	"github.com/fibonacci-chain/fbc/x/slashing/client/cli"
    19  	"github.com/fibonacci-chain/fbc/x/slashing/client/rest"
    20  	"github.com/fibonacci-chain/fbc/x/slashing/internal/types"
    21  	"github.com/fibonacci-chain/fbc/x/slashing/simulation"
    22  	stakingkeeper "github.com/fibonacci-chain/fbc/x/staking/keeper"
    23  )
    24  
    25  var (
    26  	_ module.AppModule           = AppModule{}
    27  	_ module.AppModuleBasic      = AppModuleBasic{}
    28  	_ module.AppModuleSimulation = AppModule{}
    29  )
    30  
    31  // AppModuleBasic defines the basic application module used by the slashing module.
    32  type AppModuleBasic struct{}
    33  
    34  var _ module.AppModuleBasic = AppModuleBasic{}
    35  
    36  // Name returns the slashing module's name.
    37  func (AppModuleBasic) Name() string {
    38  	return types.ModuleName
    39  }
    40  
    41  // RegisterCodec registers the slashing module's types for the given codec.
    42  func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
    43  	RegisterCodec(cdc)
    44  }
    45  
    46  // DefaultGenesis returns default genesis state as raw bytes for the slashing
    47  // module.
    48  func (AppModuleBasic) DefaultGenesis() json.RawMessage {
    49  	return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
    50  }
    51  
    52  // ValidateGenesis performs genesis state validation for the slashing module.
    53  func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
    54  	var data GenesisState
    55  	if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil {
    56  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    57  	}
    58  
    59  	return ValidateGenesis(data)
    60  }
    61  
    62  // RegisterRESTRoutes registers the REST routes for the slashing module.
    63  func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
    64  	rest.RegisterRoutes(ctx, rtr)
    65  }
    66  
    67  // GetTxCmd returns the root tx command for the slashing module.
    68  func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
    69  	return cli.GetTxCmd(cdc)
    70  }
    71  
    72  // GetQueryCmd returns no root query command for the slashing module.
    73  func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    74  	return cli.GetQueryCmd(StoreKey, cdc)
    75  }
    76  
    77  //____________________________________________________________________________
    78  
    79  // AppModule implements an application module for the slashing module.
    80  type AppModule struct {
    81  	AppModuleBasic
    82  
    83  	keeper        Keeper
    84  	accountKeeper types.AccountKeeper
    85  	stakingKeeper stakingkeeper.Keeper
    86  }
    87  
    88  // NewAppModule creates a new AppModule object
    89  func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, stakingKeeper stakingkeeper.Keeper) AppModule {
    90  	return AppModule{
    91  		AppModuleBasic: AppModuleBasic{},
    92  		keeper:         keeper,
    93  		accountKeeper:  accountKeeper,
    94  		stakingKeeper:  stakingKeeper,
    95  	}
    96  }
    97  
    98  // Name returns the slashing module's name.
    99  func (AppModule) Name() string {
   100  	return ModuleName
   101  }
   102  
   103  // RegisterInvariants registers the slashing module invariants.
   104  func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
   105  
   106  // Route returns the message routing key for the slashing module.
   107  func (AppModule) Route() string {
   108  	return RouterKey
   109  }
   110  
   111  // NewHandler returns an sdk.Handler for the slashing module.
   112  func (am AppModule) NewHandler() sdk.Handler {
   113  	return NewHandler(am.keeper)
   114  }
   115  
   116  // QuerierRoute returns the slashing module's querier route name.
   117  func (AppModule) QuerierRoute() string {
   118  	return QuerierRoute
   119  }
   120  
   121  // NewQuerierHandler returns the slashing module sdk.Querier.
   122  func (am AppModule) NewQuerierHandler() sdk.Querier {
   123  	return NewQuerier(am.keeper)
   124  }
   125  
   126  // InitGenesis performs genesis initialization for the slashing module. It returns
   127  // no validator updates.
   128  func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
   129  	var genesisState GenesisState
   130  	ModuleCdc.MustUnmarshalJSON(data, &genesisState)
   131  	InitGenesis(ctx, am.keeper, am.stakingKeeper, genesisState)
   132  	return []abci.ValidatorUpdate{}
   133  }
   134  
   135  // ExportGenesis returns the exported genesis state as raw bytes for the slashing
   136  // module.
   137  func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
   138  	gs := ExportGenesis(ctx, am.keeper)
   139  	return ModuleCdc.MustMarshalJSON(gs)
   140  }
   141  
   142  // BeginBlock returns the begin blocker for the slashing module.
   143  func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
   144  	BeginBlocker(ctx, req, am.keeper)
   145  }
   146  
   147  // EndBlock returns the end blocker for the slashing module. It returns no validator
   148  // updates.
   149  func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   150  	return []abci.ValidatorUpdate{}
   151  }
   152  
   153  //____________________________________________________________________________
   154  
   155  // AppModuleSimulation functions
   156  
   157  // GenerateGenesisState creates a randomized GenState of the slashing module.
   158  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   159  	simulation.RandomizedGenState(simState)
   160  }
   161  
   162  // ProposalContents doesn't return any content functions for governance proposals.
   163  func (AppModule) ProposalContents(_ module.SimulationState) []sim.WeightedProposalContent {
   164  	return nil
   165  }
   166  
   167  // RandomizedParams creates randomized slashing param changes for the simulator.
   168  func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange {
   169  	return simulation.ParamChanges(r)
   170  }
   171  
   172  // RegisterStoreDecoder registers a decoder for slashing module's types
   173  func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   174  	sdr[StoreKey] = simulation.DecodeStore
   175  }
   176  
   177  // WeightedOperations returns the all the slashing module operations with their respective weights.
   178  func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation {
   179  	return simulation.WeightedOperations(simState.AppParams, simState.Cdc,
   180  		am.accountKeeper, am.keeper, am.stakingKeeper)
   181  }