github.com/Finschia/finschia-sdk@v0.48.1/x/capability/module.go (about) 1 package capability 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/rand" 7 "time" 8 9 "github.com/grpc-ecosystem/grpc-gateway/runtime" 10 "github.com/spf13/cobra" 11 12 ocabci "github.com/Finschia/ostracon/abci/types" 13 abci "github.com/tendermint/tendermint/abci/types" 14 15 "github.com/Finschia/finschia-sdk/client" 16 "github.com/Finschia/finschia-sdk/codec" 17 cdctypes "github.com/Finschia/finschia-sdk/codec/types" 18 "github.com/Finschia/finschia-sdk/telemetry" 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 "github.com/Finschia/finschia-sdk/x/capability/keeper" 23 "github.com/Finschia/finschia-sdk/x/capability/simulation" 24 "github.com/Finschia/finschia-sdk/x/capability/types" 25 ) 26 27 var ( 28 _ module.AppModule = AppModule{} 29 _ module.AppModuleBasic = AppModuleBasic{} 30 _ module.AppModuleSimulation = AppModule{} 31 _ module.BeginBlockAppModule = AppModule{} 32 ) 33 34 // ---------------------------------------------------------------------------- 35 // AppModuleBasic 36 // ---------------------------------------------------------------------------- 37 38 // AppModuleBasic implements the AppModuleBasic interface for the capability module. 39 type AppModuleBasic struct { 40 cdc codec.Codec 41 } 42 43 func NewAppModuleBasic(cdc codec.Codec) AppModuleBasic { 44 return AppModuleBasic{cdc: cdc} 45 } 46 47 // Name returns the capability module's name. 48 func (AppModuleBasic) Name() string { 49 return types.ModuleName 50 } 51 52 // RegisterLegacyAminoCodec does nothing. Capability does not support amino. 53 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} 54 55 // RegisterInterfaces registers the module's interface types 56 func (a AppModuleBasic) RegisterInterfaces(_ cdctypes.InterfaceRegistry) {} 57 58 // DefaultGenesis returns the capability module's default genesis state. 59 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 60 return cdc.MustMarshalJSON(types.DefaultGenesis()) 61 } 62 63 // ValidateGenesis performs genesis state validation for the capability module. 64 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { 65 var genState types.GenesisState 66 if err := cdc.UnmarshalJSON(bz, &genState); err != nil { 67 return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) 68 } 69 return genState.Validate() 70 } 71 72 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the capability module. 73 func (a AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *runtime.ServeMux) { 74 } 75 76 // GetTxCmd returns the capability module's root tx command. 77 func (a AppModuleBasic) GetTxCmd() *cobra.Command { return nil } 78 79 // GetQueryCmd returns the capability module's root query command. 80 func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } 81 82 // ---------------------------------------------------------------------------- 83 // AppModule 84 // ---------------------------------------------------------------------------- 85 86 // AppModule implements the AppModule interface for the capability module. 87 type AppModule struct { 88 AppModuleBasic 89 90 keeper keeper.Keeper 91 } 92 93 func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule { 94 return AppModule{ 95 AppModuleBasic: NewAppModuleBasic(cdc), 96 keeper: keeper, 97 } 98 } 99 100 // Name returns the capability module's name. 101 func (am AppModule) Name() string { 102 return am.AppModuleBasic.Name() 103 } 104 105 // Route returns the capability module's message routing key. 106 func (AppModule) Route() sdk.Route { return sdk.Route{} } 107 108 // QuerierRoute returns the capability module's query routing key. 109 func (AppModule) QuerierRoute() string { return "" } 110 111 // LegacyQuerierHandler returns the capability module's Querier. 112 func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { return nil } 113 114 // RegisterServices registers a GRPC query service to respond to the 115 // module-specific GRPC queries. 116 func (am AppModule) RegisterServices(cfg module.Configurator) { 117 // m := keeper.NewMigrator(am.keeper) 118 // if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { 119 // panic(fmt.Sprintf("failed to migrate x/capability from version 1 to 2: %v", err)) 120 // } 121 } 122 123 // RegisterInvariants registers the capability module's invariants. 124 func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} 125 126 // InitGenesis performs the capability module's genesis initialization It returns 127 // no validator updates. 128 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { 129 var genState types.GenesisState 130 // Initialize global index to index in genesis state 131 cdc.MustUnmarshalJSON(gs, &genState) 132 133 InitGenesis(ctx, am.keeper, genState) 134 135 return []abci.ValidatorUpdate{} 136 } 137 138 // ExportGenesis returns the capability module's exported genesis state as raw JSON bytes. 139 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 140 genState := ExportGenesis(ctx, am.keeper) 141 return cdc.MustMarshalJSON(genState) 142 } 143 144 // ConsensusVersion implements AppModule/ConsensusVersion. 145 func (AppModule) ConsensusVersion() uint64 { return 1 } 146 147 // BeginBlocker calls InitMemStore to assert that the memory store is initialized. 148 // It's safe to run multiple times. 149 func (am AppModule) BeginBlock(ctx sdk.Context, _ ocabci.RequestBeginBlock) { 150 defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) 151 152 am.keeper.InitMemStore(ctx) 153 } 154 155 // GenerateGenesisState creates a randomized GenState of the capability module. 156 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 157 simulation.RandomizedGenState(simState) 158 } 159 160 // ProposalContents performs a no-op 161 func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { 162 return nil 163 } 164 165 // RandomizedParams creates randomized capability param changes for the simulator. 166 func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { 167 return nil 168 } 169 170 // RegisterStoreDecoder registers a decoder for capability module's types 171 func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 172 sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) 173 } 174 175 // WeightedOperations returns the all the gov module operations with their respective weights. 176 func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { 177 return nil 178 }