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