github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/module/module.go (about) 1 package authz 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 8 gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" 9 "github.com/spf13/cobra" 10 11 modulev1 "cosmossdk.io/api/cosmos/authz/module/v1" 12 "cosmossdk.io/core/address" 13 "cosmossdk.io/core/appmodule" 14 "cosmossdk.io/core/store" 15 "cosmossdk.io/depinject" 16 "cosmossdk.io/errors" 17 18 "github.com/cosmos/cosmos-sdk/baseapp" 19 sdkclient "github.com/cosmos/cosmos-sdk/client" 20 "github.com/cosmos/cosmos-sdk/codec" 21 cdctypes "github.com/cosmos/cosmos-sdk/codec/types" 22 sdk "github.com/cosmos/cosmos-sdk/types" 23 "github.com/cosmos/cosmos-sdk/types/module" 24 simtypes "github.com/cosmos/cosmos-sdk/types/simulation" 25 "github.com/cosmos/cosmos-sdk/x/authz" 26 "github.com/cosmos/cosmos-sdk/x/authz/client/cli" 27 "github.com/cosmos/cosmos-sdk/x/authz/keeper" 28 "github.com/cosmos/cosmos-sdk/x/authz/simulation" 29 ) 30 31 var ( 32 _ module.AppModuleBasic = AppModule{} 33 _ module.AppModuleSimulation = AppModule{} 34 _ module.HasGenesis = AppModule{} 35 _ module.HasServices = AppModule{} 36 37 _ appmodule.AppModule = AppModule{} 38 _ appmodule.HasBeginBlocker = AppModule{} 39 ) 40 41 // AppModuleBasic defines the basic application module used by the authz module. 42 type AppModuleBasic struct { 43 cdc codec.Codec 44 ac address.Codec 45 } 46 47 // Name returns the authz module's name. 48 func (AppModuleBasic) Name() string { 49 return authz.ModuleName 50 } 51 52 // RegisterServices registers a gRPC query service to respond to the 53 // module-specific gRPC queries. 54 func (am AppModule) RegisterServices(cfg module.Configurator) { 55 authz.RegisterQueryServer(cfg.QueryServer(), am.keeper) 56 authz.RegisterMsgServer(cfg.MsgServer(), am.keeper) 57 m := keeper.NewMigrator(am.keeper) 58 err := cfg.RegisterMigration(authz.ModuleName, 1, m.Migrate1to2) 59 if err != nil { 60 panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", authz.ModuleName, err)) 61 } 62 } 63 64 // RegisterLegacyAminoCodec registers the authz module's types for the given codec. 65 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { 66 authz.RegisterLegacyAminoCodec(cdc) 67 } 68 69 // RegisterInterfaces registers the authz module's interface types 70 func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { 71 authz.RegisterInterfaces(registry) 72 } 73 74 // DefaultGenesis returns default genesis state as raw bytes for the authz 75 // module. 76 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 77 return cdc.MustMarshalJSON(authz.DefaultGenesisState()) 78 } 79 80 // ValidateGenesis performs genesis state validation for the authz module. 81 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config sdkclient.TxEncodingConfig, bz json.RawMessage) error { 82 var data authz.GenesisState 83 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 84 return errors.Wrapf(err, "failed to unmarshal %s genesis state", authz.ModuleName) 85 } 86 87 return authz.ValidateGenesis(data) 88 } 89 90 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the authz module. 91 func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *gwruntime.ServeMux) { 92 if err := authz.RegisterQueryHandlerClient(context.Background(), mux, authz.NewQueryClient(clientCtx)); err != nil { 93 panic(err) 94 } 95 } 96 97 // GetTxCmd returns the transaction commands for the authz module 98 func (ab AppModuleBasic) GetTxCmd() *cobra.Command { 99 return cli.GetTxCmd(ab.ac) 100 } 101 102 // AppModule implements the sdk.AppModule interface 103 type AppModule struct { 104 AppModuleBasic 105 106 keeper keeper.Keeper 107 accountKeeper authz.AccountKeeper 108 bankKeeper authz.BankKeeper 109 registry cdctypes.InterfaceRegistry 110 } 111 112 // NewAppModule creates a new AppModule object 113 func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper, bk authz.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule { 114 return AppModule{ 115 AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak.AddressCodec()}, 116 keeper: keeper.SetBankKeeper(bk), // Super ugly hack to not be api breaking in v0.50 and v0.47 117 accountKeeper: ak, 118 bankKeeper: bk, 119 registry: registry, 120 } 121 } 122 123 // IsOnePerModuleType implements the depinject.OnePerModuleType interface. 124 func (am AppModule) IsOnePerModuleType() {} 125 126 // IsAppModule implements the appmodule.AppModule interface. 127 func (am AppModule) IsAppModule() {} 128 129 // InitGenesis performs genesis initialization for the authz module. It returns 130 // no validator updates. 131 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) { 132 var genesisState authz.GenesisState 133 cdc.MustUnmarshalJSON(data, &genesisState) 134 am.keeper.InitGenesis(ctx, &genesisState) 135 } 136 137 // ExportGenesis returns the exported genesis state as raw bytes for the authz 138 // module. 139 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 140 gs := am.keeper.ExportGenesis(ctx) 141 return cdc.MustMarshalJSON(gs) 142 } 143 144 // ConsensusVersion implements AppModule/ConsensusVersion. 145 func (AppModule) ConsensusVersion() uint64 { return 2 } 146 147 // BeginBlock returns the begin blocker for the authz module. 148 func (am AppModule) BeginBlock(ctx context.Context) error { 149 c := sdk.UnwrapSDKContext(ctx) 150 return BeginBlocker(c, am.keeper) 151 } 152 153 func init() { 154 appmodule.Register( 155 &modulev1.Module{}, 156 appmodule.Provide(ProvideModule), 157 ) 158 } 159 160 type ModuleInputs struct { 161 depinject.In 162 163 Cdc codec.Codec 164 AccountKeeper authz.AccountKeeper 165 BankKeeper authz.BankKeeper 166 Registry cdctypes.InterfaceRegistry 167 MsgServiceRouter baseapp.MessageRouter 168 StoreService store.KVStoreService 169 } 170 171 type ModuleOutputs struct { 172 depinject.Out 173 174 AuthzKeeper keeper.Keeper 175 Module appmodule.AppModule 176 } 177 178 func ProvideModule(in ModuleInputs) ModuleOutputs { 179 k := keeper.NewKeeper(in.StoreService, in.Cdc, in.MsgServiceRouter, in.AccountKeeper) 180 m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) 181 return ModuleOutputs{AuthzKeeper: k.SetBankKeeper(in.BankKeeper) /* depinject ux improvement */, Module: m} 182 } 183 184 // ____________________________________________________________________________ 185 186 // AppModuleSimulation functions 187 188 // GenerateGenesisState creates a randomized GenState of the authz module. 189 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 190 simulation.RandomizedGenState(simState) 191 } 192 193 // RegisterStoreDecoder registers a decoder for authz module's types 194 func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { 195 sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc) 196 } 197 198 // WeightedOperations returns the all the gov module operations with their respective weights. 199 func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { 200 return simulation.WeightedOperations( 201 am.registry, 202 simState.AppParams, simState.Cdc, simState.TxConfig, 203 am.accountKeeper, am.bankKeeper, am.keeper, 204 ) 205 }