github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/29-fee/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 5 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 6 capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types" 7 paramtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params" 8 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/29-fee/types" 9 channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 10 host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host" 11 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 12 ) 13 14 // Middleware must implement types.ChannelKeeper and types.PortKeeper expected interfaces 15 // so that it can wrap IBC channel and port logic for underlying application. 16 var ( 17 _ types.ChannelKeeper = Keeper{} 18 _ types.PortKeeper = Keeper{} 19 ) 20 21 // Keeper defines the IBC fungible transfer keeper 22 type Keeper struct { 23 storeKey sdk.StoreKey 24 cdc *codec.CodecProxy 25 26 authKeeper types.AccountKeeper 27 ics4Wrapper types.ICS4Wrapper 28 channelKeeper types.ChannelKeeper 29 portKeeper types.PortKeeper 30 bankKeeper types.BankKeeper 31 } 32 33 // NewKeeper creates a new 29-fee Keeper instance 34 func NewKeeper( 35 cdc *codec.CodecProxy, key sdk.StoreKey, paramSpace paramtypes.Subspace, 36 ics4Wrapper types.ICS4Wrapper, channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper, authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, 37 ) Keeper { 38 return Keeper{ 39 cdc: cdc, 40 storeKey: key, 41 ics4Wrapper: ics4Wrapper, 42 channelKeeper: channelKeeper, 43 portKeeper: portKeeper, 44 authKeeper: authKeeper, 45 bankKeeper: bankKeeper, 46 } 47 } 48 49 // Logger returns a module-specific logger. 50 func (k Keeper) Logger(ctx sdk.Context) log.Logger { 51 return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName) 52 } 53 54 // BindPort defines a wrapper function for the port Keeper's function in 55 // order to expose it to module's InitGenesis function 56 func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability { 57 return k.portKeeper.BindPort(ctx, portID) 58 } 59 60 // GetChannel wraps IBC ChannelKeeper's GetChannel function 61 func (k Keeper) GetChannel(ctx sdk.Context, portID, channelID string) (channeltypes.Channel, bool) { 62 return k.channelKeeper.GetChannel(ctx, portID, channelID) 63 } 64 65 // GetPacketCommitment wraps IBC ChannelKeeper's GetPacketCommitment function 66 func (k Keeper) GetPacketCommitment(ctx sdk.Context, portID, channelID string, sequence uint64) []byte { 67 return k.channelKeeper.GetPacketCommitment(ctx, portID, channelID, sequence) 68 } 69 70 // GetNextSequenceSend wraps IBC ChannelKeeper's GetNextSequenceSend function 71 func (k Keeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) { 72 return k.channelKeeper.GetNextSequenceSend(ctx, portID, channelID) 73 } 74 75 // GetFeeModuleAddress returns the ICS29 Fee ModuleAccount address 76 func (k Keeper) GetFeeModuleAddress() sdk.AccAddress { 77 return k.authKeeper.GetModuleAddress(types.ModuleName) 78 } 79 80 // EscrowAccountHasBalance verifies if the escrow account has the provided fee. 81 func (k Keeper) EscrowAccountHasBalance(ctx sdk.Context, coins sdk.CoinAdapters) bool { 82 sdkCoin := coins.ToCoins() 83 for _, coin := range sdkCoin { 84 if !k.bankKeeper.HasBalance(ctx, k.GetFeeModuleAddress(), coin) { 85 return false 86 } 87 } 88 89 return true 90 } 91 92 // lockFeeModule sets a flag to determine if fee handling logic should run for the given channel 93 // identified by channel and port identifiers. 94 // Please see ADR 004 for more information. 95 func (k Keeper) lockFeeModule(ctx sdk.Context) { 96 store := ctx.KVStore(k.storeKey) 97 store.Set(types.KeyLocked(), []byte{1}) 98 } 99 100 // IsLocked indicates if the fee module is locked 101 // Please see ADR 004 for more information. 102 func (k Keeper) IsLocked(ctx sdk.Context) bool { 103 store := ctx.KVStore(k.storeKey) 104 return store.Has(types.KeyLocked()) 105 } 106 107 // SetFeeEnabled sets a flag to determine if fee handling logic should run for the given channel 108 // identified by channel and port identifiers. 109 func (k Keeper) SetFeeEnabled(ctx sdk.Context, portID, channelID string) { 110 store := ctx.KVStore(k.storeKey) 111 store.Set(types.KeyFeeEnabled(portID, channelID), []byte{1}) 112 } 113 114 // DeleteFeeEnabled deletes the fee enabled flag for a given portID and channelID 115 func (k Keeper) DeleteFeeEnabled(ctx sdk.Context, portID, channelID string) { 116 store := ctx.KVStore(k.storeKey) 117 store.Delete(types.KeyFeeEnabled(portID, channelID)) 118 } 119 120 // IsFeeEnabled returns whether fee handling logic should be run for the given port. It will check the 121 // fee enabled flag for the given port and channel identifiers 122 func (k Keeper) IsFeeEnabled(ctx sdk.Context, portID, channelID string) bool { 123 store := ctx.KVStore(k.storeKey) 124 return store.Get(types.KeyFeeEnabled(portID, channelID)) != nil 125 } 126 127 // GetAllFeeEnabledChannels returns a list of all ics29 enabled channels containing portID & channelID that are stored in state 128 func (k Keeper) GetAllFeeEnabledChannels(ctx sdk.Context) []types.FeeEnabledChannel { 129 store := ctx.KVStore(k.storeKey) 130 iterator := sdk.KVStorePrefixIterator(store, []byte(types.FeeEnabledKeyPrefix)) 131 defer iterator.Close() 132 133 var enabledChArr []types.FeeEnabledChannel 134 for ; iterator.Valid(); iterator.Next() { 135 portID, channelID, err := types.ParseKeyFeeEnabled(string(iterator.Key())) 136 if err != nil { 137 panic(err) 138 } 139 ch := types.FeeEnabledChannel{ 140 PortId: portID, 141 ChannelId: channelID, 142 } 143 144 enabledChArr = append(enabledChArr, ch) 145 } 146 147 return enabledChArr 148 } 149 150 // GetPayeeAddress retrieves the fee payee address stored in state given the provided channel identifier and relayer address 151 func (k Keeper) GetPayeeAddress(ctx sdk.Context, relayerAddr, channelID string) (string, bool) { 152 store := ctx.KVStore(k.storeKey) 153 key := types.KeyPayee(relayerAddr, channelID) 154 155 if !store.Has(key) { 156 return "", false 157 } 158 159 return string(store.Get(key)), true 160 } 161 162 // SetPayeeAddress stores the fee payee address in state keyed by the provided channel identifier and relayer address 163 func (k Keeper) SetPayeeAddress(ctx sdk.Context, relayerAddr, payeeAddr, channelID string) { 164 store := ctx.KVStore(k.storeKey) 165 store.Set(types.KeyPayee(relayerAddr, channelID), []byte(payeeAddr)) 166 } 167 168 // GetAllPayees returns all registered payees addresses 169 func (k Keeper) GetAllPayees(ctx sdk.Context) []types.RegisteredPayee { 170 store := ctx.KVStore(k.storeKey) 171 iterator := sdk.KVStorePrefixIterator(store, []byte(types.PayeeKeyPrefix)) 172 defer iterator.Close() 173 174 var registeredPayees []types.RegisteredPayee 175 for ; iterator.Valid(); iterator.Next() { 176 relayerAddr, channelID, err := types.ParseKeyPayeeAddress(string(iterator.Key())) 177 if err != nil { 178 panic(err) 179 } 180 181 payee := types.RegisteredPayee{ 182 Relayer: relayerAddr, 183 Payee: string(iterator.Value()), 184 ChannelId: channelID, 185 } 186 187 registeredPayees = append(registeredPayees, payee) 188 } 189 190 return registeredPayees 191 } 192 193 // SetCounterpartyPayeeAddress maps the destination chain counterparty payee address to the source relayer address 194 // The receiving chain must store the mapping from: address -> counterpartyPayeeAddress for the given channel 195 func (k Keeper) SetCounterpartyPayeeAddress(ctx sdk.Context, address, counterpartyAddress, channelID string) { 196 store := ctx.KVStore(k.storeKey) 197 store.Set(types.KeyCounterpartyPayee(address, channelID), []byte(counterpartyAddress)) 198 } 199 200 // GetCounterpartyPayeeAddress gets the counterparty payee address given a destination relayer address 201 func (k Keeper) GetCounterpartyPayeeAddress(ctx sdk.Context, address, channelID string) (string, bool) { 202 store := ctx.KVStore(k.storeKey) 203 key := types.KeyCounterpartyPayee(address, channelID) 204 205 if !store.Has(key) { 206 return "", false 207 } 208 209 addr := string(store.Get(key)) 210 return addr, true 211 } 212 213 // GetAllCounterpartyPayees returns all registered counterparty payee addresses 214 func (k Keeper) GetAllCounterpartyPayees(ctx sdk.Context) []types.RegisteredCounterpartyPayee { 215 store := ctx.KVStore(k.storeKey) 216 iterator := sdk.KVStorePrefixIterator(store, []byte(types.CounterpartyPayeeKeyPrefix)) 217 defer iterator.Close() 218 219 var registeredCounterpartyPayees []types.RegisteredCounterpartyPayee 220 for ; iterator.Valid(); iterator.Next() { 221 relayerAddr, channelID, err := types.ParseKeyCounterpartyPayee(string(iterator.Key())) 222 if err != nil { 223 panic(err) 224 } 225 226 counterpartyPayee := types.RegisteredCounterpartyPayee{ 227 Relayer: relayerAddr, 228 CounterpartyPayee: string(iterator.Value()), 229 ChannelId: channelID, 230 } 231 232 registeredCounterpartyPayees = append(registeredCounterpartyPayees, counterpartyPayee) 233 } 234 235 return registeredCounterpartyPayees 236 } 237 238 // SetRelayerAddressForAsyncAck sets the forward relayer address during OnRecvPacket in case of async acknowledgement 239 func (k Keeper) SetRelayerAddressForAsyncAck(ctx sdk.Context, packetID channeltypes.PacketId, address string) { 240 store := ctx.KVStore(k.storeKey) 241 store.Set(types.KeyRelayerAddressForAsyncAck(packetID), []byte(address)) 242 } 243 244 // GetRelayerAddressForAsyncAck gets forward relayer address for a particular packet 245 func (k Keeper) GetRelayerAddressForAsyncAck(ctx sdk.Context, packetID channeltypes.PacketId) (string, bool) { 246 store := ctx.KVStore(k.storeKey) 247 key := types.KeyRelayerAddressForAsyncAck(packetID) 248 if !store.Has(key) { 249 return "", false 250 } 251 252 addr := string(store.Get(key)) 253 return addr, true 254 } 255 256 // GetAllForwardRelayerAddresses returns all forward relayer addresses stored for async acknowledgements 257 func (k Keeper) GetAllForwardRelayerAddresses(ctx sdk.Context) []types.ForwardRelayerAddress { 258 store := ctx.KVStore(k.storeKey) 259 iterator := sdk.KVStorePrefixIterator(store, []byte(types.ForwardRelayerPrefix)) 260 defer iterator.Close() 261 262 var forwardRelayerAddr []types.ForwardRelayerAddress 263 for ; iterator.Valid(); iterator.Next() { 264 packetID, err := types.ParseKeyRelayerAddressForAsyncAck(string(iterator.Key())) 265 if err != nil { 266 panic(err) 267 } 268 269 addr := types.ForwardRelayerAddress{ 270 Address: string(iterator.Value()), 271 PacketId: packetID, 272 } 273 274 forwardRelayerAddr = append(forwardRelayerAddr, addr) 275 } 276 277 return forwardRelayerAddr 278 } 279 280 // Deletes the forwardRelayerAddr associated with the packetID 281 func (k Keeper) DeleteForwardRelayerAddress(ctx sdk.Context, packetID channeltypes.PacketId) { 282 store := ctx.KVStore(k.storeKey) 283 key := types.KeyRelayerAddressForAsyncAck(packetID) 284 store.Delete(key) 285 } 286 287 // GetFeesInEscrow returns all escrowed packet fees for a given packetID 288 func (k Keeper) GetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) (types.PacketFees, bool) { 289 store := ctx.KVStore(k.storeKey) 290 key := types.KeyFeesInEscrow(packetID) 291 bz := store.Get(key) 292 if bz == nil { 293 return types.PacketFees{}, false 294 } 295 296 return k.MustUnmarshalFees(bz), true 297 } 298 299 // HasFeesInEscrow returns true if packet fees exist for the provided packetID 300 func (k Keeper) HasFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) bool { 301 store := ctx.KVStore(k.storeKey) 302 key := types.KeyFeesInEscrow(packetID) 303 304 return store.Has(key) 305 } 306 307 // SetFeesInEscrow sets the given packet fees in escrow keyed by the packetID 308 func (k Keeper) SetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId, fees types.PacketFees) { 309 store := ctx.KVStore(k.storeKey) 310 bz := k.MustMarshalFees(fees) 311 store.Set(types.KeyFeesInEscrow(packetID), bz) 312 } 313 314 // DeleteFeesInEscrow deletes the fee associated with the given packetID 315 func (k Keeper) DeleteFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) { 316 store := ctx.KVStore(k.storeKey) 317 key := types.KeyFeesInEscrow(packetID) 318 store.Delete(key) 319 } 320 321 // GetIdentifiedPacketFeesForChannel returns all the currently escrowed fees on a given channel. 322 func (k Keeper) GetIdentifiedPacketFeesForChannel(ctx sdk.Context, portID, channelID string) []types.IdentifiedPacketFees { 323 var identifiedPacketFees []types.IdentifiedPacketFees 324 325 store := ctx.KVStore(k.storeKey) 326 iterator := sdk.KVStorePrefixIterator(store, types.KeyFeesInEscrowChannelPrefix(portID, channelID)) 327 328 defer iterator.Close() 329 for ; iterator.Valid(); iterator.Next() { 330 packetID, err := types.ParseKeyFeesInEscrow(string(iterator.Key())) 331 if err != nil { 332 panic(err) 333 } 334 335 packetFees := k.MustUnmarshalFees(iterator.Value()) 336 337 identifiedFee := types.NewIdentifiedPacketFees(packetID, packetFees.PacketFees) 338 identifiedPacketFees = append(identifiedPacketFees, identifiedFee) 339 } 340 341 return identifiedPacketFees 342 } 343 344 // GetAllIdentifiedPacketFees returns a list of all IdentifiedPacketFees that are stored in state 345 func (k Keeper) GetAllIdentifiedPacketFees(ctx sdk.Context) []types.IdentifiedPacketFees { 346 store := ctx.KVStore(k.storeKey) 347 iterator := sdk.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix)) 348 defer iterator.Close() 349 350 var identifiedFees []types.IdentifiedPacketFees 351 for ; iterator.Valid(); iterator.Next() { 352 packetID, err := types.ParseKeyFeesInEscrow(string(iterator.Key())) 353 if err != nil { 354 panic(err) 355 } 356 357 feesInEscrow := k.MustUnmarshalFees(iterator.Value()) 358 359 identifiedFee := types.IdentifiedPacketFees{ 360 PacketId: packetID, 361 PacketFees: feesInEscrow.PacketFees, 362 } 363 364 identifiedFees = append(identifiedFees, identifiedFee) 365 } 366 367 return identifiedFees 368 } 369 370 // MustMarshalFees attempts to encode a Fee object and returns the 371 // raw encoded bytes. It panics on error. 372 func (k Keeper) MustMarshalFees(fees types.PacketFees) []byte { 373 return k.cdc.GetProtocMarshal().MustMarshal(&fees) 374 } 375 376 // MustUnmarshalFees attempts to decode and return a Fee object from 377 // raw encoded bytes. It panics on error. 378 func (k Keeper) MustUnmarshalFees(bz []byte) types.PacketFees { 379 var fees types.PacketFees 380 k.cdc.GetProtocMarshal().MustUnmarshal(bz, &fees) 381 return fees 382 }