2019-05-16 08:25:32 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
2020-09-04 08:07:02 -07:00
|
|
|
"context"
|
2019-05-16 08:25:32 -07:00
|
|
|
"encoding/json"
|
2020-01-09 12:14:23 -08:00
|
|
|
"fmt"
|
2019-08-28 07:58:25 -07:00
|
|
|
"math/rand"
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2020-06-06 00:59:57 -07:00
|
|
|
"github.com/gogo/protobuf/grpc"
|
2020-08-25 08:44:13 -07:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2020-06-06 00:59:57 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-07-23 19:45:34 -07:00
|
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2019-05-16 08:25:32 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-03-23 04:55:44 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
|
2020-06-12 07:25:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
|
2020-02-19 16:04:13 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2019-12-05 01:29:54 -08:00
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2019-05-16 08:25:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-13 15:16:03 -07:00
|
|
|
_ module.AppModule = AppModule{}
|
|
|
|
_ module.AppModuleBasic = AppModuleBasic{}
|
2019-12-05 01:29:54 -08:00
|
|
|
_ module.AppModuleSimulation = AppModule{}
|
2019-05-16 08:25:32 -07:00
|
|
|
)
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// AppModuleBasic defines the basic application module used by the slashing module.
|
2020-04-21 14:33:56 -07:00
|
|
|
type AppModuleBasic struct {
|
|
|
|
cdc codec.Marshaler
|
|
|
|
}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
var _ module.AppModuleBasic = AppModuleBasic{}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the slashing module's name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModuleBasic) Name() string {
|
2019-06-05 16:26:17 -07:00
|
|
|
return types.ModuleName
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-09-07 07:47:12 -07:00
|
|
|
// RegisterLegacyAminoCodec registers the slashing module's types for the given codec.
|
|
|
|
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
|
|
|
|
types.RegisterLegacyAminoCodec(cdc)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-07-23 19:45:34 -07:00
|
|
|
// RegisterInterfaces registers the module's interface types
|
|
|
|
func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
|
|
|
|
types.RegisterInterfaces(registry)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// DefaultGenesis returns default genesis state as raw bytes for the slashing
|
|
|
|
// module.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
|
2020-06-12 07:25:12 -07:00
|
|
|
return cdc.MustMarshalJSON(types.DefaultGenesisState())
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ValidateGenesis performs genesis state validation for the slashing module.
|
2020-07-25 01:10:04 -07:00
|
|
|
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error {
|
2020-06-12 07:25:12 -07:00
|
|
|
var data types.GenesisState
|
2020-02-18 04:50:13 -08:00
|
|
|
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
|
2020-01-09 12:14:23 -08:00
|
|
|
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
2020-01-09 12:14:23 -08:00
|
|
|
|
2020-06-12 07:25:12 -07:00
|
|
|
return types.ValidateGenesis(data)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterRESTRoutes registers the REST routes for the slashing module.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) {
|
|
|
|
rest.RegisterHandlers(clientCtx, rtr)
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 08:44:13 -07:00
|
|
|
// RegisterGRPCRoutes registers the gRPC Gateway routes for the slashig module.
|
2020-09-04 08:07:02 -07:00
|
|
|
func (AppModuleBasic) RegisterGRPCRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
|
|
|
|
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
|
2020-08-25 08:44:13 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetTxCmd returns the root tx command for the slashing module.
|
2020-07-21 08:28:43 -07:00
|
|
|
func (AppModuleBasic) GetTxCmd() *cobra.Command {
|
2020-07-14 08:55:19 -07:00
|
|
|
return cli.NewTxCmd()
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetQueryCmd returns no root query command for the slashing module.
|
2020-07-21 08:28:43 -07:00
|
|
|
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
|
2020-07-14 08:55:19 -07:00
|
|
|
return cli.GetQueryCmd()
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
//____________________________________________________________________________
|
|
|
|
|
|
|
|
// AppModule implements an application module for the slashing module.
|
2019-05-16 08:25:32 -07:00
|
|
|
type AppModule struct {
|
|
|
|
AppModuleBasic
|
2019-08-13 15:16:03 -07:00
|
|
|
|
2020-06-12 07:25:12 -07:00
|
|
|
keeper keeper.Keeper
|
2019-12-05 01:29:54 -08:00
|
|
|
accountKeeper types.AccountKeeper
|
2020-01-30 13:31:16 -08:00
|
|
|
bankKeeper types.BankKeeper
|
2019-12-05 01:29:54 -08:00
|
|
|
stakingKeeper stakingkeeper.Keeper
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppModule creates a new AppModule object
|
2020-06-12 07:25:12 -07:00
|
|
|
func NewAppModule(cdc codec.Marshaler, keeper keeper.Keeper, ak types.AccountKeeper, bk types.BankKeeper, sk stakingkeeper.Keeper) AppModule {
|
2019-05-16 08:25:32 -07:00
|
|
|
return AppModule{
|
2020-04-21 14:33:56 -07:00
|
|
|
AppModuleBasic: AppModuleBasic{cdc: cdc},
|
2019-12-05 01:29:54 -08:00
|
|
|
keeper: keeper,
|
2020-01-30 13:31:16 -08:00
|
|
|
accountKeeper: ak,
|
|
|
|
bankKeeper: bk,
|
|
|
|
stakingKeeper: sk,
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the slashing module's name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) Name() string {
|
2020-06-12 07:25:12 -07:00
|
|
|
return types.ModuleName
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterInvariants registers the slashing module invariants.
|
2019-06-06 13:32:38 -07:00
|
|
|
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Route returns the message routing key for the slashing module.
|
2020-06-11 08:37:23 -07:00
|
|
|
func (am AppModule) Route() sdk.Route {
|
2020-06-12 07:25:12 -07:00
|
|
|
return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper))
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// QuerierRoute returns the slashing module's querier route name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) QuerierRoute() string {
|
2020-06-12 07:25:12 -07:00
|
|
|
return types.QuerierRoute
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-07-24 12:04:29 -07:00
|
|
|
// LegacyQuerierHandler returns the slashing module sdk.Querier.
|
2020-08-26 02:39:38 -07:00
|
|
|
func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
2020-07-25 01:03:58 -07:00
|
|
|
return keeper.NewQuerier(am.keeper, legacyQuerierCdc)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:16:57 -07:00
|
|
|
// RegisterQueryService registers a GRPC query service to respond to the
|
|
|
|
// module-specific GRPC queries.
|
|
|
|
func (am AppModule) RegisterQueryService(server grpc.Server) {
|
|
|
|
types.RegisterQueryServer(server, am.keeper)
|
|
|
|
}
|
2020-06-06 00:59:57 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// InitGenesis performs genesis initialization for the slashing module. It returns
|
|
|
|
// no validator updates.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
|
2020-06-12 07:25:12 -07:00
|
|
|
var genesisState types.GenesisState
|
2020-08-11 07:22:30 -07:00
|
|
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
|
|
|
InitGenesis(ctx, am.keeper, am.stakingKeeper, &genesisState)
|
2019-05-16 08:25:32 -07:00
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the slashing
|
|
|
|
// module.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
|
2019-05-16 08:25:32 -07:00
|
|
|
gs := ExportGenesis(ctx, am.keeper)
|
2020-02-18 04:50:13 -08:00
|
|
|
return cdc.MustMarshalJSON(gs)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// BeginBlock returns the begin blocker for the slashing module.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
|
|
|
|
BeginBlocker(ctx, req, am.keeper)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// EndBlock returns the end blocker for the slashing module. It returns no validator
|
|
|
|
// updates.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
|
|
|
|
return []abci.ValidatorUpdate{}
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
2019-12-05 01:29:54 -08:00
|
|
|
|
|
|
|
//____________________________________________________________________________
|
|
|
|
|
|
|
|
// AppModuleSimulation functions
|
|
|
|
|
|
|
|
// GenerateGenesisState creates a randomized GenState of the slashing module.
|
|
|
|
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
|
|
|
|
simulation.RandomizedGenState(simState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProposalContents doesn't return any content functions for governance proposals.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
|
2019-12-05 01:29:54 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomizedParams creates randomized slashing param changes for the simulator.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
|
2019-12-05 01:29:54 -08:00
|
|
|
return simulation.ParamChanges(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStoreDecoder registers a decoder for slashing module's types
|
2020-04-21 14:33:56 -07:00
|
|
|
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
|
2020-06-12 07:25:12 -07:00
|
|
|
sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WeightedOperations returns the all the slashing module operations with their respective weights.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
|
2020-01-30 13:31:16 -08:00
|
|
|
return simulation.WeightedOperations(
|
|
|
|
simState.AppParams, simState.Cdc,
|
|
|
|
am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper,
|
|
|
|
)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|