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