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