github.com/cosmos/cosmos-sdk@v0.50.10/x/group/module/module.go (about) 1 package module 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/group/module/v1" 12 "cosmossdk.io/core/address" 13 "cosmossdk.io/core/appmodule" 14 "cosmossdk.io/depinject" 15 store "cosmossdk.io/store/types" 16 17 "github.com/cosmos/cosmos-sdk/baseapp" 18 sdkclient "github.com/cosmos/cosmos-sdk/client" 19 "github.com/cosmos/cosmos-sdk/codec" 20 cdctypes "github.com/cosmos/cosmos-sdk/codec/types" 21 sdk "github.com/cosmos/cosmos-sdk/types" 22 "github.com/cosmos/cosmos-sdk/types/module" 23 simtypes "github.com/cosmos/cosmos-sdk/types/simulation" 24 "github.com/cosmos/cosmos-sdk/x/group" 25 "github.com/cosmos/cosmos-sdk/x/group/client/cli" 26 "github.com/cosmos/cosmos-sdk/x/group/keeper" 27 "github.com/cosmos/cosmos-sdk/x/group/simulation" 28 ) 29 30 // ConsensusVersion defines the current x/group module consensus version. 31 const ConsensusVersion = 2 32 33 var ( 34 _ module.AppModuleBasic = AppModule{} 35 _ module.AppModuleSimulation = AppModule{} 36 _ module.HasGenesis = AppModule{} 37 _ module.HasServices = AppModule{} 38 _ module.HasInvariants = AppModule{} 39 40 _ appmodule.AppModule = AppModule{} 41 _ appmodule.HasEndBlocker = AppModule{} 42 ) 43 44 type AppModule struct { 45 AppModuleBasic 46 keeper keeper.Keeper 47 bankKeeper group.BankKeeper 48 accKeeper group.AccountKeeper 49 registry cdctypes.InterfaceRegistry 50 } 51 52 // NewAppModule creates a new AppModule object 53 func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak group.AccountKeeper, bk group.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule { 54 return AppModule{ 55 AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak.AddressCodec()}, 56 keeper: keeper, 57 bankKeeper: bk, 58 accKeeper: ak, 59 registry: registry, 60 } 61 } 62 63 // IsOnePerModuleType implements the depinject.OnePerModuleType interface. 64 func (am AppModule) IsOnePerModuleType() {} 65 66 // IsAppModule implements the appmodule.AppModule interface. 67 func (am AppModule) IsAppModule() {} 68 69 type AppModuleBasic struct { 70 cdc codec.Codec 71 ac address.Codec 72 } 73 74 // Name returns the group module's name. 75 func (AppModuleBasic) Name() string { 76 return group.ModuleName 77 } 78 79 // DefaultGenesis returns default genesis state as raw bytes for the group 80 // module. 81 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 82 return cdc.MustMarshalJSON(group.NewGenesisState()) 83 } 84 85 // ValidateGenesis performs genesis state validation for the group module. 86 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config sdkclient.TxEncodingConfig, bz json.RawMessage) error { 87 var data group.GenesisState 88 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 89 return fmt.Errorf("failed to unmarshal %s genesis state: %w", group.ModuleName, err) 90 } 91 return data.Validate() 92 } 93 94 // GetTxCmd returns the transaction commands for the group module 95 func (a AppModuleBasic) GetTxCmd() *cobra.Command { 96 return cli.TxCmd(a.Name(), a.ac) 97 } 98 99 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the group module. 100 func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *gwruntime.ServeMux) { 101 if err := group.RegisterQueryHandlerClient(context.Background(), mux, group.NewQueryClient(clientCtx)); err != nil { 102 panic(err) 103 } 104 } 105 106 // RegisterInterfaces registers the group module's interface types 107 func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { 108 group.RegisterInterfaces(registry) 109 } 110 111 // RegisterLegacyAminoCodec registers the group module's types for the given codec. 112 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { 113 group.RegisterLegacyAminoCodec(cdc) 114 } 115 116 // RegisterInvariants does nothing, there are no invariants to enforce 117 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { 118 keeper.RegisterInvariants(ir, am.keeper) 119 } 120 121 // InitGenesis performs genesis initialization for the group module. It returns 122 // no validator updates. 123 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) { 124 am.keeper.InitGenesis(ctx, cdc, data) 125 } 126 127 // ExportGenesis returns the exported genesis state as raw bytes for the group 128 // module. 129 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 130 gs := am.keeper.ExportGenesis(ctx, cdc) 131 return cdc.MustMarshalJSON(gs) 132 } 133 134 // RegisterServices registers a gRPC query service to respond to the 135 // module-specific gRPC queries. 136 func (am AppModule) RegisterServices(cfg module.Configurator) { 137 group.RegisterMsgServer(cfg.MsgServer(), am.keeper) 138 group.RegisterQueryServer(cfg.QueryServer(), am.keeper) 139 140 m := keeper.NewMigrator(am.keeper) 141 if err := cfg.RegisterMigration(group.ModuleName, 1, m.Migrate1to2); err != nil { 142 panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", group.ModuleName, err)) 143 } 144 } 145 146 // ConsensusVersion implements AppModule/ConsensusVersion. 147 func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } 148 149 // EndBlock implements the group module's EndBlock. 150 func (am AppModule) EndBlock(ctx context.Context) error { 151 c := sdk.UnwrapSDKContext(ctx) 152 return EndBlocker(c, am.keeper) 153 } 154 155 // ____________________________________________________________________________ 156 157 // AppModuleSimulation functions 158 159 // GenerateGenesisState creates a randomized GenState of the group module. 160 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 161 simulation.RandomizedGenState(simState) 162 } 163 164 // RegisterStoreDecoder registers a decoder for group module's types 165 func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { 166 sdr[group.StoreKey] = simulation.NewDecodeStore(am.cdc) 167 } 168 169 // WeightedOperations returns the all the gov module operations with their respective weights. 170 func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { 171 return simulation.WeightedOperations( 172 am.registry, 173 simState.AppParams, simState.Cdc, simState.TxConfig, 174 am.accKeeper, am.bankKeeper, am.keeper, am.cdc, 175 ) 176 } 177 178 // 179 // App Wiring Setup 180 // 181 182 func init() { 183 appmodule.Register( 184 &modulev1.Module{}, 185 appmodule.Provide(ProvideModule), 186 ) 187 } 188 189 type GroupInputs struct { 190 depinject.In 191 192 Config *modulev1.Module 193 Key *store.KVStoreKey 194 Cdc codec.Codec 195 AccountKeeper group.AccountKeeper 196 BankKeeper group.BankKeeper 197 Registry cdctypes.InterfaceRegistry 198 MsgServiceRouter baseapp.MessageRouter 199 } 200 201 type GroupOutputs struct { 202 depinject.Out 203 204 GroupKeeper keeper.Keeper 205 Module appmodule.AppModule 206 } 207 208 func ProvideModule(in GroupInputs) GroupOutputs { 209 /* 210 Example of setting group params: 211 in.Config.MaxMetadataLen = 1000 212 in.Config.MaxExecutionPeriod = "1209600s" 213 */ 214 215 k := keeper.NewKeeper(in.Key, in.Cdc, in.MsgServiceRouter, in.AccountKeeper, group.Config{MaxExecutionPeriod: in.Config.MaxExecutionPeriod.AsDuration(), MaxMetadataLen: in.Config.MaxMetadataLen}) 216 m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) 217 return GroupOutputs{GroupKeeper: k, Module: m} 218 }