From f3e4e2e0c3b91f88602ec8322bfe003d86c16c86 Mon Sep 17 00:00:00 2001 From: Tony Stark Date: Sat, 16 Nov 2024 22:48:13 +0530 Subject: [PATCH] feat: implement OnTimeoutPacket --- x/oracle/ibc_module.go | 48 ++++++++++------------ x/oracle/keeper/alias.go | 4 ++ x/oracle/keeper/keeper.go | 9 +++- x/oracle/keeper/relay.go | 14 +++++++ x/oracle/services/v1/query_server.go | 6 ++- x/oracle/types/keys.go | 7 +++- x/subscription/services/v3/query_server.go | 1 + 7 files changed, 56 insertions(+), 33 deletions(-) diff --git a/x/oracle/ibc_module.go b/x/oracle/ibc_module.go index 5c620bc8..5ff2909e 100644 --- a/x/oracle/ibc_module.go +++ b/x/oracle/ibc_module.go @@ -1,8 +1,6 @@ package oracle import ( - "strings" - sdkerrors "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -27,51 +25,44 @@ type IBCModule struct { } func (im IBCModule) OnChanOpenInit( - ctx sdk.Context, order ibcchanneltypes.Order, _ []string, portID, channelID string, + ctx sdk.Context, _ ibcchanneltypes.Order, _ []string, portID, channelID string, channelCap *capabilitytypes.Capability, _ ibcchanneltypes.Counterparty, version string, ) (string, error) { - if strings.TrimSpace(version) == "" { - version = types.Version - } if version != types.Version { return "", types.NewErrorInvalidVersion(version, types.Version) } - if order != ibcchanneltypes.ORDERED { - return "", types.NewErrorInvalidChannelOrdering(order, ibcchanneltypes.ORDERED) - } - boundPortID := im.keeper.GetPortID(ctx) - if boundPortID != portID { + if portID != boundPortID { return "", types.NewErrorInvalidPort(portID, boundPortID) } - if err := im.keeper.ClaimCapability(ctx, channelCap, ibchost.ChannelCapabilityPath(portID, channelID)); err != nil { + capPath := ibchost.ChannelCapabilityPath(portID, channelID) + if err := im.keeper.ClaimCapability(ctx, channelCap, capPath); err != nil { return "", err } - return version, nil + return types.Version, nil } func (im IBCModule) OnChanOpenTry( - ctx sdk.Context, order ibcchanneltypes.Order, _ []string, portID, channelID string, + ctx sdk.Context, _ ibcchanneltypes.Order, _ []string, portID, channelID string, channelCap *capabilitytypes.Capability, _ ibcchanneltypes.Counterparty, counterpartyVersion string, ) (string, error) { if counterpartyVersion != types.Version { return "", types.NewErrorInvalidCounterpartyVersion(counterpartyVersion, types.Version) } - if order != ibcchanneltypes.ORDERED { - return "", types.NewErrorInvalidChannelOrdering(order, ibcchanneltypes.ORDERED) - } - boundPortID := im.keeper.GetPortID(ctx) - if boundPortID != portID { + if portID != boundPortID { return "", types.NewErrorInvalidPort(portID, boundPortID) } - if err := im.keeper.ClaimCapability(ctx, channelCap, ibchost.ChannelCapabilityPath(portID, channelID)); err != nil { - return "", err + capPath := ibchost.ChannelCapabilityPath(portID, channelID) + if !im.keeper.AuthenticateCapability(ctx, channelCap, capPath) { + if err := im.keeper.ClaimCapability(ctx, channelCap, capPath); err != nil { + return "", err + } } return types.Version, nil @@ -85,17 +76,20 @@ func (im IBCModule) OnChanOpenAck(_ sdk.Context, _, _, _ string, counterpartyVer return nil } -func (im IBCModule) OnChanOpenConfirm(_ sdk.Context, _, _ string) error { return nil } +func (im IBCModule) OnChanOpenConfirm(_ sdk.Context, _, _ string) error { + return nil +} func (im IBCModule) OnChanCloseInit(_ sdk.Context, _, _ string) error { return sdkerrors.Wrap(ibcerrors.ErrInvalidRequest, "user cannot close the channel") } -func (im IBCModule) OnChanCloseConfirm(_ sdk.Context, _, _ string) error { return nil } +func (im IBCModule) OnChanCloseConfirm(_ sdk.Context, _, _ string) error { + return nil +} func (im IBCModule) OnRecvPacket(_ sdk.Context, _ ibcchanneltypes.Packet, _ sdk.AccAddress) ibcexported.Acknowledgement { - err := sdkerrors.Wrap(ibcerrors.ErrInvalidRequest, "oracle module can not receive the packets") - return ibcchanneltypes.NewErrorAcknowledgement(err) + return ibcchanneltypes.NewErrorAcknowledgement(sdkerrors.Wrap(ibcerrors.ErrInvalidRequest, "oracle module can not receive the packets")) } func (im IBCModule) OnAcknowledgementPacket( @@ -109,6 +103,6 @@ func (im IBCModule) OnAcknowledgementPacket( return im.keeper.OnAcknowledgementPacket(ctx, packet, ack) } -func (im IBCModule) OnTimeoutPacket(_ sdk.Context, _ ibcchanneltypes.Packet, _ sdk.AccAddress) error { - return nil +func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet ibcchanneltypes.Packet, _ sdk.AccAddress) error { + return im.keeper.OnTimeoutPacket(ctx, packet) } diff --git a/x/oracle/keeper/alias.go b/x/oracle/keeper/alias.go index a1acabc6..8833569e 100644 --- a/x/oracle/keeper/alias.go +++ b/x/oracle/keeper/alias.go @@ -5,6 +5,10 @@ import ( capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" ) +func (k *Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool { + return k.capability.AuthenticateCapability(ctx, cap, name) +} + func (k *Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error { return k.capability.ClaimCapability(ctx, cap, name) } diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index e49cbeb6..e2062023 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -30,5 +30,10 @@ func NewKeeper( } } -func (k *Keeper) GetAuthority() string { return k.authority } -func (k *Keeper) Store(ctx sdk.Context) sdk.KVStore { return ctx.KVStore(k.key) } +func (k *Keeper) GetAuthority() string { + return k.authority +} + +func (k *Keeper) Store(ctx sdk.Context) sdk.KVStore { + return ctx.KVStore(k.key) +} diff --git a/x/oracle/keeper/relay.go b/x/oracle/keeper/relay.go index d700237d..eabdde2b 100644 --- a/x/oracle/keeper/relay.go +++ b/x/oracle/keeper/relay.go @@ -117,11 +117,13 @@ func (k *Keeper) handleSpotPriceQueryResponse(ctx sdk.Context, asset v1.Asset, r return nil } + // Unmarshal the response data to extract the spot price details. var res queryproto.SpotPriceResponse if err := k.cdc.Unmarshal(resp.GetValue(), &res); err != nil { return err } + // Convert the spot price to a decimal value. spotPrice, err := sdkmath.LegacyNewDecFromStr(res.GetSpotPrice()) if err != nil { return err @@ -179,3 +181,15 @@ func (k *Keeper) handleProtoRevPoolQueryResponse(ctx sdk.Context, asset v1.Asset return nil } + +// OnTimeoutPacket handles the case when a packet times out before receiving an acknowledgement. +func (k *Keeper) OnTimeoutPacket(ctx sdk.Context, packet ibcchanneltypes.Packet) error { + // Retrieve the source port, channel, and sequence number from the packet. + portID := packet.GetSourcePort() + channelID := packet.GetSourceChannel() + sequence := packet.GetSequence() + + // Delete the denom mapping associated with the timed-out packet. + k.DeleteDenomForPacket(ctx, portID, channelID, sequence) + return nil +} diff --git a/x/oracle/services/v1/query_server.go b/x/oracle/services/v1/query_server.go index bfe11e13..c6c0707a 100644 --- a/x/oracle/services/v1/query_server.go +++ b/x/oracle/services/v1/query_server.go @@ -2,11 +2,13 @@ package v1 import ( "context" + sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/sentinel-official/hub/v12/x/oracle/keeper" - "github.com/sentinel-official/hub/v12/x/oracle/types/v1" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + + "github.com/sentinel-official/hub/v12/x/oracle/keeper" + "github.com/sentinel-official/hub/v12/x/oracle/types/v1" ) var ( diff --git a/x/oracle/types/keys.go b/x/oracle/types/keys.go index 739c99ba..32cd6443 100644 --- a/x/oracle/types/keys.go +++ b/x/oracle/types/keys.go @@ -1,10 +1,13 @@ package types +import ( + ibcicqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v7/types" +) + const ( ModuleName = "oracle" - PortID = ModuleName StoreKey = ModuleName - Version = "oracle-1" + Version = ibcicqtypes.Version ) var ( diff --git a/x/subscription/services/v3/query_server.go b/x/subscription/services/v3/query_server.go index ebe43048..c64c4811 100644 --- a/x/subscription/services/v3/query_server.go +++ b/x/subscription/services/v3/query_server.go @@ -2,6 +2,7 @@ package v3 import ( "context" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status"