github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/distribution/module.go (about) 1 package distribution 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/rand" 7 8 "github.com/gorilla/mux" 9 "github.com/spf13/cobra" 10 11 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 12 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 14 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 15 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 16 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module" 17 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/client/cli" 18 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/client/rest" 19 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/simulation" 20 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/types" 21 sim "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation" 22 stakingkeeper "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/keeper" 23 ) 24 25 var ( 26 _ module.AppModule = AppModule{} 27 _ module.AppModuleBasic = AppModuleBasic{} 28 _ module.AppModuleSimulation = AppModule{} 29 ) 30 31 // AppModuleBasic defines the basic application module used by the distribution module. 32 type AppModuleBasic struct{} 33 34 // Name returns the distribution module's name. 35 func (AppModuleBasic) Name() string { 36 return ModuleName 37 } 38 39 // RegisterCodec registers the distribution module's types for the given codec. 40 func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { 41 RegisterCodec(cdc) 42 } 43 44 // DefaultGenesis returns default genesis state as raw bytes for the distribution 45 // module. 46 func (AppModuleBasic) DefaultGenesis() json.RawMessage { 47 return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) 48 } 49 50 // ValidateGenesis performs genesis state validation for the distribution module. 51 func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { 52 var data GenesisState 53 if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { 54 return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) 55 } 56 57 return ValidateGenesis(data) 58 } 59 60 // RegisterRESTRoutes registers the REST routes for the distribution module. 61 func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { 62 rest.RegisterRoutes(ctx, rtr, StoreKey) 63 } 64 65 // GetTxCmd returns the root tx command for the distribution module. 66 func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { 67 return cli.GetTxCmd(StoreKey, cdc) 68 } 69 70 // GetQueryCmd returns the root query command for the distribution module. 71 func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { 72 return cli.GetQueryCmd(StoreKey, cdc) 73 } 74 75 //____________________________________________________________________________ 76 77 // AppModule implements an application module for the distribution module. 78 type AppModule struct { 79 AppModuleBasic 80 81 keeper Keeper 82 accountKeeper types.AccountKeeper 83 stakingKeeper stakingkeeper.Keeper 84 supplyKeeper types.SupplyKeeper 85 } 86 87 // NewAppModule creates a new AppModule object 88 func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, 89 supplyKeeper types.SupplyKeeper, stakingKeeper stakingkeeper.Keeper) AppModule { 90 return AppModule{ 91 AppModuleBasic: AppModuleBasic{}, 92 keeper: keeper, 93 accountKeeper: accountKeeper, 94 supplyKeeper: supplyKeeper, 95 stakingKeeper: stakingKeeper, 96 } 97 } 98 99 // Name returns the distribution module's name. 100 func (AppModule) Name() string { 101 return ModuleName 102 } 103 104 // RegisterInvariants registers the distribution module invariants. 105 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { 106 RegisterInvariants(ir, am.keeper) 107 } 108 109 // Route returns the message routing key for the distribution module. 110 func (AppModule) Route() string { 111 return RouterKey 112 } 113 114 // NewHandler returns an sdk.Handler for the distribution module. 115 func (am AppModule) NewHandler() sdk.Handler { 116 return NewHandler(am.keeper) 117 } 118 119 // QuerierRoute returns the distribution module's querier route name. 120 func (AppModule) QuerierRoute() string { 121 return QuerierRoute 122 } 123 124 // NewQuerierHandler returns the distribution module sdk.Querier. 125 func (am AppModule) NewQuerierHandler() sdk.Querier { 126 return NewQuerier(am.keeper) 127 } 128 129 // InitGenesis performs genesis initialization for the distribution module. It returns 130 // no validator updates. 131 func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 132 var genesisState GenesisState 133 ModuleCdc.MustUnmarshalJSON(data, &genesisState) 134 InitGenesis(ctx, am.keeper, am.supplyKeeper, genesisState) 135 return []abci.ValidatorUpdate{} 136 } 137 138 // ExportGenesis returns the exported genesis state as raw bytes for the distribution 139 // module. 140 func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { 141 gs := ExportGenesis(ctx, am.keeper) 142 return ModuleCdc.MustMarshalJSON(gs) 143 } 144 145 // BeginBlock returns the begin blocker for the distribution module. 146 func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { 147 BeginBlocker(ctx, req, am.keeper) 148 } 149 150 // EndBlock returns the end blocker for the distribution module. It returns no validator 151 // updates. 152 func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { 153 return []abci.ValidatorUpdate{} 154 } 155 156 //____________________________________________________________________________ 157 158 // AppModuleSimulation functions 159 160 // GenerateGenesisState creates a randomized GenState of the distribution module. 161 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 162 simulation.RandomizedGenState(simState) 163 } 164 165 // ProposalContents returns all the distribution content functions used to 166 // simulate governance proposals. 167 func (am AppModule) ProposalContents(_ module.SimulationState) []sim.WeightedProposalContent { 168 return simulation.ProposalContents(am.keeper) 169 } 170 171 // RandomizedParams creates randomized distribution param changes for the simulator. 172 func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange { 173 return simulation.ParamChanges(r) 174 } 175 176 // RegisterStoreDecoder registers a decoder for distribution module's types 177 func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 178 sdr[StoreKey] = simulation.DecodeStore 179 } 180 181 // WeightedOperations returns the all the gov module operations with their respective weights. 182 func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation { 183 return simulation.WeightedOperations(simState.AppParams, simState.Cdc, 184 am.accountKeeper, am.keeper, am.stakingKeeper) 185 }