github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/module.go (about) 1 package staking 2 3 import ( 4 "encoding/json" 5 "math/rand" 6 7 sim "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation" 8 "github.com/fibonacci-chain/fbc/x/staking/simulation" 9 10 "github.com/fibonacci-chain/fbc/x/staking/keeper" 11 12 "github.com/gorilla/mux" 13 "github.com/spf13/cobra" 14 flag "github.com/spf13/pflag" 15 16 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 17 cfg "github.com/fibonacci-chain/fbc/libs/tendermint/config" 18 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 19 20 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 21 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 22 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 23 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module" 24 authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types" 25 "github.com/fibonacci-chain/fbc/x/staking/client/cli" 26 "github.com/fibonacci-chain/fbc/x/staking/client/rest" 27 "github.com/fibonacci-chain/fbc/x/staking/types" 28 ) 29 30 var ( 31 _ module.AppModule = AppModule{} 32 _ module.AppModuleBasic = AppModuleBasic{} 33 ) 34 35 // AppModuleBasic is a struct of app module basics object 36 type AppModuleBasic struct{} 37 38 // Name returns the staking module's name. 39 func (AppModuleBasic) Name() string { 40 return ModuleName 41 } 42 43 // RegisterCodec registers module codec 44 func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { 45 RegisterCodec(cdc) 46 } 47 48 // DefaultGenesis returns default genesis state 49 func (AppModuleBasic) DefaultGenesis() json.RawMessage { 50 return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) 51 } 52 53 // ValidateGenesis give a validity check to module genesis 54 func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { 55 var data GenesisState 56 err := ModuleCdc.UnmarshalJSON(bz, &data) 57 if err != nil { 58 return err 59 } 60 return ValidateGenesis(data) 61 } 62 63 // RegisterRESTRoutes registers rest routes 64 func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { 65 rest.RegisterRoutes(ctx, rtr) 66 } 67 68 // GetTxCmd gets the root tx command of this module 69 func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { 70 return cli.GetTxCmd(StoreKey, cdc) 71 } 72 73 // GetQueryCmd gets the root query command of this module 74 func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { 75 return cli.GetQueryCmd(StoreKey, cdc) 76 } 77 78 //_____________________________________ 79 // extra helpers 80 81 // CreateValidatorMsgHelpers is used for gen-tx 82 func (AppModuleBasic) CreateValidatorMsgHelpers(ipDefault string) ( 83 fs *flag.FlagSet, nodeIDFlag, pubkeyFlag, amountFlag, defaultsDesc string) { 84 return cli.CreateValidatorMsgHelpers(ipDefault) 85 } 86 87 // PrepareFlagsForTxCreateValidator is used for gen-tx 88 func (AppModuleBasic) PrepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, 89 chainID string, valPubKey crypto.PubKey) { 90 cli.PrepareFlagsForTxCreateValidator(config, nodeID, chainID, valPubKey) 91 } 92 93 // BuildCreateValidatorMsg is used for gen-tx 94 func (AppModuleBasic) BuildCreateValidatorMsg(cliCtx context.CLIContext, 95 txBldr authtypes.TxBuilder) (authtypes.TxBuilder, sdk.Msg, error) { 96 return cli.BuildCreateValidatorMsg(cliCtx, txBldr) 97 } 98 99 // AppModule is a struct of app module 100 type AppModule struct { 101 AppModuleBasic 102 keeper Keeper 103 accKeeper types.AccountKeeper 104 supplyKeeper types.SupplyKeeper 105 } 106 107 // NewAppModule creates a new AppModule object 108 func NewAppModule(keeper Keeper, accKeeper types.AccountKeeper, 109 supplyKeeper types.SupplyKeeper) AppModule { 110 111 return AppModule{ 112 AppModuleBasic: AppModuleBasic{}, 113 keeper: keeper, 114 accKeeper: accKeeper, 115 supplyKeeper: supplyKeeper, 116 } 117 } 118 119 // RegisterInvariants registers invariants 120 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { 121 // required by fbchain 122 keeper.RegisterInvariantsCustom(ir, am.keeper) 123 } 124 125 // Route returns the message routing key for the staking module. 126 func (AppModule) Route() string { 127 return RouterKey 128 } 129 130 // NewHandler returns module handler 131 func (am AppModule) NewHandler() sdk.Handler { 132 return NewHandler(am.keeper) 133 } 134 135 // QuerierRoute returns the staking module's querier route name. 136 func (AppModule) QuerierRoute() string { 137 return QuerierRoute 138 } 139 140 // NewQuerierHandler returns module querier 141 func (am AppModule) NewQuerierHandler() sdk.Querier { 142 return NewQuerier(am.keeper) 143 } 144 145 // InitGenesis initializes module genesis 146 func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 147 var genesisState GenesisState 148 ModuleCdc.MustUnmarshalJSON(data, &genesisState) 149 return InitGenesis(ctx, am.keeper, am.accKeeper, am.supplyKeeper, genesisState) 150 } 151 152 // ExportGenesis exports module genesis 153 func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { 154 gs := ExportGenesis(ctx, am.keeper) 155 return ModuleCdc.MustMarshalJSON(gs) 156 } 157 158 // BeginBlock is invoked on the beginning of each block 159 func (am AppModule) BeginBlock(ctx sdk.Context, b abci.RequestBeginBlock) { 160 BeginBlocker(ctx, am.keeper) 161 } 162 163 // EndBlock is invoked on the end of each block 164 func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { 165 return EndBlocker(ctx, am.keeper) 166 } 167 168 // GenerateGenesisState performs a no-op. 169 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 170 } 171 172 // ProposalContents returns all the params content functions used to 173 // simulate governance proposals. 174 func (am AppModule) ProposalContents(simState module.SimulationState) []sim.WeightedProposalContent { 175 return simulation.ProposalContents(simState.ParamChanges) 176 } 177 178 // RandomizedParams creates randomized distribution param changes for the simulator. 179 func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange { 180 return nil 181 } 182 183 // RegisterStoreDecoder doesn't register any type. 184 func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {} 185 186 // WeightedOperations returns the all the gov module operations with their respective weights. 187 func (am AppModule) WeightedOperations(_ module.SimulationState) []sim.WeightedOperation { 188 return nil 189 }