github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/module.go (about) 1 package ibc 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 8 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/upgrade" 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params" 10 ibcclient "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client" 11 12 "math/rand" 13 14 clientCtx "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 15 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 16 codectypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types" 17 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 18 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module" 19 simulation2 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation" 20 clienttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types" 21 connectiontypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/03-connection/types" 22 channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 23 host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host" 24 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/base" 25 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/client/cli" 26 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/keeper" 27 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/simulation" 28 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/types" 29 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 30 tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types" 31 "github.com/gorilla/mux" 32 "github.com/grpc-ecosystem/grpc-gateway/runtime" 33 "github.com/spf13/cobra" 34 ) 35 36 var ( 37 _ module.AppModuleAdapter = AppModule{} 38 _ module.AppModuleBasicAdapter = AppModuleBasic{} 39 _ module.AppModuleSimulation = AppModule{} 40 ) 41 42 // AppModuleBasic defines the basic application module used by the ibc module. 43 type AppModuleBasic struct{} 44 45 var _ module.AppModuleBasic = AppModuleBasic{} 46 47 // Name returns the ibc module's name. 48 func (AppModuleBasic) Name() string { 49 return host.ModuleName 50 } 51 52 // DefaultGenesis returns default genesis state as raw bytes for the ibc 53 // module. 54 func (AppModuleBasic) DefaultGenesis() json.RawMessage { 55 return nil 56 } 57 58 // ValidateGenesis performs genesis state validation for the mint module. 59 func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { 60 return nil 61 } 62 63 // RegisterRESTRoutes does nothing. IBC does not support legacy REST routes. 64 func (AppModuleBasic) RegisterRESTRoutes(ctx clientCtx.CLIContext, rtr *mux.Router) {} 65 66 func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { 67 types.RegisterCodec(cdc) 68 } 69 70 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the ibc module. 71 func (AppModuleBasic) RegisterGRPCGatewayRoutes(ctx clientCtx.CLIContext, mux *runtime.ServeMux) { 72 clienttypes.RegisterQueryHandlerClient(context.Background(), mux, clienttypes.NewQueryClient(ctx)) 73 connectiontypes.RegisterQueryHandlerClient(context.Background(), mux, connectiontypes.NewQueryClient(ctx)) 74 channeltypes.RegisterQueryHandlerClient(context.Background(), mux, channeltypes.NewQueryClient(ctx)) 75 } 76 77 // GetTxCmd returns the root tx command for the ibc module. 78 func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { 79 return nil 80 } 81 82 func (am AppModuleBasic) GetTxCmdV2(cdc *codec.CodecProxy, reg codectypes.InterfaceRegistry) *cobra.Command { 83 return cli.GetTxCmd(cdc, reg) 84 } 85 86 // GetQueryCmd returns no root query command for the ibc module. 87 func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { 88 return nil 89 } 90 91 func (AppModuleBasic) GetQueryCmdV2(cdc *codec.CodecProxy, reg codectypes.InterfaceRegistry) *cobra.Command { 92 return cli.GetQueryCmd(cdc, reg) 93 } 94 95 // RegisterInterfaces registers module concrete types into protobuf Any. 96 func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { 97 types.RegisterInterfaces(registry) 98 } 99 100 // AppModule implements an application module for the ibc module. 101 type AppModule struct { 102 AppModuleBasic 103 *base.BaseIBCUpgradeModule 104 keeper *keeper.FacadedKeeper 105 106 // create localhost by default 107 createLocalhost bool 108 } 109 110 // NewAppModule creates a new AppModule object 111 func NewAppModule(k *keeper.FacadedKeeper) AppModule { 112 ret := AppModule{ 113 keeper: k, 114 } 115 ret.BaseIBCUpgradeModule = base.NewBaseIBCUpgradeModule(ret) 116 return ret 117 } 118 119 func (a AppModule) NewQuerierHandler() sdk.Querier { 120 return nil 121 } 122 123 func (a AppModule) NewHandler() sdk.Handler { 124 return NewHandler(*a.keeper) 125 } 126 127 // Name returns the ibc module's name. 128 func (AppModule) Name() string { 129 return host.ModuleName 130 } 131 132 // RegisterInvariants registers the ibc module invariants. 133 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {} 134 135 // Route returns the message routing key for the ibc module. 136 func (am AppModule) Route() string { 137 return host.RouterKey 138 } 139 140 // QuerierRoute returns the ibc module's querier route name. 141 func (AppModule) QuerierRoute() string { 142 return host.QuerierRoute 143 } 144 145 // RegisterServices registers module services. 146 func (am AppModule) RegisterServices(cfg module.Configurator) { 147 clienttypes.RegisterMsgServer(cfg.MsgServer(), am.keeper) 148 connectiontypes.RegisterMsgServer(cfg.MsgServer(), am.keeper) 149 channeltypes.RegisterMsgServer(cfg.MsgServer(), am.keeper) 150 types.RegisterQueryService(cfg.QueryServer(), am.keeper.V2Keeper) 151 } 152 153 // InitGenesis performs genesis initialization for the ibc module. It returns 154 // no validator updates. 155 // func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, bz json.RawMessage) []abci.ValidatorUpdate { 156 func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 157 return nil 158 } 159 160 func (am AppModule) initGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 161 var gs types.GenesisState 162 err := ModuleCdc.UnmarshalJSON(data, &gs) 163 if err != nil { 164 panic(fmt.Sprintf("failed to unmarshal %s genesis state: %s", host.ModuleName, err)) 165 } 166 InitGenesis(ctx, *am.keeper.V2Keeper, am.createLocalhost, &gs) 167 return []abci.ValidatorUpdate{} 168 } 169 170 // ExportGenesis returns the exported genesis state as raw bytes for the ibc 171 // module. 172 func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { 173 return nil 174 } 175 176 func (am AppModule) exportGenesis(ctx sdk.Context) json.RawMessage { 177 return ModuleCdc.MustMarshalJSON(ExportGenesis(ctx, *am.keeper.V2Keeper)) 178 } 179 180 func lazyGenesis() json.RawMessage { 181 ret := DefaultGenesisState() 182 return ModuleCdc.MustMarshalJSON(&ret) 183 } 184 185 // BeginBlock returns the begin blocker for the ibc module. 186 func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { 187 if !tmtypes.HigherThanVenus1(req.Header.Height) { 188 return 189 } 190 ibcclient.BeginBlocker(ctx, am.keeper.V2Keeper.ClientKeeper) 191 } 192 193 // EndBlock returns the end blocker for the ibc module. It returns no validator 194 // updates. 195 func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate { 196 return []abci.ValidatorUpdate{} 197 } 198 199 func (am AppModuleBasic) RegisterRouterForGRPC(cliCtx clientCtx.CLIContext, r *mux.Router) { 200 201 } 202 203 // ____________________________________________________________________________ 204 205 // AppModuleSimulation functions 206 207 // GenerateGenesisState creates a randomized GenState of the ibc module. 208 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 209 simulation.RandomizedGenState(simState) 210 } 211 212 // ProposalContents doesn't return any content functions for governance proposals. 213 func (AppModule) ProposalContents(_ module.SimulationState) []simulation2.WeightedProposalContent { 214 return nil 215 } 216 217 // RandomizedParams returns nil since IBC doesn't register parameter changes. 218 func (AppModule) RandomizedParams(_ *rand.Rand) []simulation2.ParamChange { 219 return nil 220 } 221 222 // RegisterStoreDecoder registers a decoder for ibc module's types 223 func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 224 sdr[host.StoreKey] = simulation.NewDecodeStore(*am.keeper.V2Keeper) 225 } 226 227 // WeightedOperations returns the all the ibc module operations with their respective weights. 228 func (am AppModule) WeightedOperations(_ module.SimulationState) []simulation2.WeightedOperation { 229 return nil 230 } 231 232 func (am AppModule) RegisterTask() upgrade.HeightTask { 233 return upgrade.NewHeightTask(4, func(ctx sdk.Context) error { 234 data := lazyGenesis() 235 am.initGenesis(ctx, data) 236 return nil 237 }) 238 } 239 240 func (am AppModule) RegisterParam() params.ParamSet { 241 return nil 242 }