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