github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/module.go (about)

     1  package slashing
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
     9  
    10  	modulev1 "cosmossdk.io/api/cosmos/slashing/module/v1"
    11  	"cosmossdk.io/core/appmodule"
    12  	store "cosmossdk.io/core/store"
    13  	"cosmossdk.io/depinject"
    14  
    15  	"github.com/cosmos/cosmos-sdk/client"
    16  	"github.com/cosmos/cosmos-sdk/codec"
    17  	cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
    18  	sdk "github.com/cosmos/cosmos-sdk/types"
    19  	"github.com/cosmos/cosmos-sdk/types/module"
    20  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    21  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
    22  	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
    23  	"github.com/cosmos/cosmos-sdk/x/slashing/exported"
    24  	"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
    25  	"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
    26  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    27  	staking "github.com/cosmos/cosmos-sdk/x/staking/types"
    28  )
    29  
    30  // ConsensusVersion defines the current x/slashing module consensus version.
    31  const ConsensusVersion = 4
    32  
    33  var (
    34  	_ module.AppModuleBasic      = AppModule{}
    35  	_ module.AppModuleSimulation = AppModule{}
    36  	_ module.HasGenesis          = AppModule{}
    37  	_ module.HasServices         = AppModule{}
    38  
    39  	_ appmodule.AppModule       = AppModule{}
    40  	_ appmodule.HasBeginBlocker = AppModule{}
    41  )
    42  
    43  // AppModuleBasic defines the basic application module used by the slashing module.
    44  type AppModuleBasic struct {
    45  	cdc codec.Codec
    46  }
    47  
    48  // Name returns the slashing module's name.
    49  func (AppModuleBasic) Name() string {
    50  	return types.ModuleName
    51  }
    52  
    53  // RegisterLegacyAminoCodec registers the slashing module's types for the given codec.
    54  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    55  	types.RegisterLegacyAminoCodec(cdc)
    56  }
    57  
    58  // RegisterInterfaces registers the module's interface types
    59  func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
    60  	types.RegisterInterfaces(registry)
    61  }
    62  
    63  // DefaultGenesis returns default genesis state as raw bytes for the slashing
    64  // module.
    65  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    66  	return cdc.MustMarshalJSON(types.DefaultGenesisState())
    67  }
    68  
    69  // ValidateGenesis performs genesis state validation for the slashing module.
    70  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
    71  	var data types.GenesisState
    72  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    73  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    74  	}
    75  
    76  	return types.ValidateGenesis(data)
    77  }
    78  
    79  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the slashig module.
    80  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
    81  	if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
    82  		panic(err)
    83  	}
    84  }
    85  
    86  // AppModule implements an application module for the slashing module.
    87  type AppModule struct {
    88  	AppModuleBasic
    89  
    90  	registry cdctypes.InterfaceRegistry
    91  
    92  	keeper        keeper.Keeper
    93  	accountKeeper types.AccountKeeper
    94  	bankKeeper    types.BankKeeper
    95  	stakingKeeper types.StakingKeeper
    96  
    97  	// legacySubspace is used solely for migration of x/slashing managed parameters
    98  	legacySubspace exported.Subspace
    99  }
   100  
   101  // NewAppModule creates a new AppModule object
   102  func NewAppModule(
   103  	cdc codec.Codec,
   104  	keeper keeper.Keeper,
   105  	ak types.AccountKeeper,
   106  	bk types.BankKeeper,
   107  	sk types.StakingKeeper,
   108  	ss exported.Subspace,
   109  	registry cdctypes.InterfaceRegistry,
   110  ) AppModule {
   111  	return AppModule{
   112  		AppModuleBasic: AppModuleBasic{cdc: cdc},
   113  		keeper:         keeper,
   114  		accountKeeper:  ak,
   115  		bankKeeper:     bk,
   116  		stakingKeeper:  sk,
   117  		legacySubspace: ss,
   118  	}
   119  }
   120  
   121  // IsOnePerModuleType implements the depinject.OnePerModuleType interface.
   122  func (am AppModule) IsOnePerModuleType() {}
   123  
   124  // IsAppModule implements the appmodule.AppModule interface.
   125  func (am AppModule) IsAppModule() {}
   126  
   127  // RegisterServices registers module services.
   128  func (am AppModule) RegisterServices(cfg module.Configurator) {
   129  	types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
   130  	types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
   131  
   132  	m := keeper.NewMigrator(am.keeper, am.legacySubspace)
   133  	if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
   134  		panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
   135  	}
   136  
   137  	if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
   138  		panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err))
   139  	}
   140  
   141  	if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil {
   142  		panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err))
   143  	}
   144  }
   145  
   146  // InitGenesis performs genesis initialization for the slashing module. It returns
   147  // no validator updates.
   148  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
   149  	var genesisState types.GenesisState
   150  	cdc.MustUnmarshalJSON(data, &genesisState)
   151  	am.keeper.InitGenesis(ctx, am.stakingKeeper, &genesisState)
   152  }
   153  
   154  // ExportGenesis returns the exported genesis state as raw bytes for the slashing
   155  // module.
   156  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   157  	gs := am.keeper.ExportGenesis(ctx)
   158  	return cdc.MustMarshalJSON(gs)
   159  }
   160  
   161  // ConsensusVersion implements AppModule/ConsensusVersion.
   162  func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
   163  
   164  // BeginBlock returns the begin blocker for the slashing module.
   165  func (am AppModule) BeginBlock(ctx context.Context) error {
   166  	return BeginBlocker(ctx, am.keeper)
   167  }
   168  
   169  // AppModuleSimulation functions
   170  
   171  // GenerateGenesisState creates a randomized GenState of the slashing module.
   172  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   173  	simulation.RandomizedGenState(simState)
   174  }
   175  
   176  // ProposalMsgs returns msgs used for governance proposals for simulations.
   177  func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
   178  	return simulation.ProposalMsgs()
   179  }
   180  
   181  // RegisterStoreDecoder registers a decoder for slashing module's types
   182  func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
   183  	sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
   184  }
   185  
   186  // WeightedOperations returns the all the slashing module operations with their respective weights.
   187  func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
   188  	return simulation.WeightedOperations(
   189  		am.registry, simState.AppParams, simState.Cdc, simState.TxConfig,
   190  		am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper,
   191  	)
   192  }
   193  
   194  //
   195  // App Wiring Setup
   196  //
   197  
   198  func init() {
   199  	appmodule.Register(
   200  		&modulev1.Module{},
   201  		appmodule.Provide(ProvideModule),
   202  	)
   203  }
   204  
   205  type ModuleInputs struct {
   206  	depinject.In
   207  
   208  	Config       *modulev1.Module
   209  	StoreService store.KVStoreService
   210  	Cdc          codec.Codec
   211  	LegacyAmino  *codec.LegacyAmino
   212  	Registry     cdctypes.InterfaceRegistry
   213  
   214  	AccountKeeper types.AccountKeeper
   215  	BankKeeper    types.BankKeeper
   216  	StakingKeeper types.StakingKeeper
   217  
   218  	// LegacySubspace is used solely for migration of x/params managed parameters
   219  	LegacySubspace exported.Subspace `optional:"true"`
   220  }
   221  
   222  type ModuleOutputs struct {
   223  	depinject.Out
   224  
   225  	Keeper keeper.Keeper
   226  	Module appmodule.AppModule
   227  	Hooks  staking.StakingHooksWrapper
   228  }
   229  
   230  func ProvideModule(in ModuleInputs) ModuleOutputs {
   231  	// default to governance authority if not provided
   232  	authority := authtypes.NewModuleAddress(govtypes.ModuleName)
   233  	if in.Config.Authority != "" {
   234  		authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
   235  	}
   236  
   237  	k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.StoreService, in.StakingKeeper, authority.String())
   238  	m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.LegacySubspace, in.Registry)
   239  	return ModuleOutputs{
   240  		Keeper: k,
   241  		Module: m,
   242  		Hooks:  staking.StakingHooksWrapper{StakingHooks: k.Hooks()},
   243  	}
   244  }