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