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