Skip to content

Commit f3e4e2e

Browse files
committed
feat: implement OnTimeoutPacket
1 parent 77f6e7d commit f3e4e2e

File tree

7 files changed

+56
-33
lines changed

7 files changed

+56
-33
lines changed

x/oracle/ibc_module.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package oracle
22

33
import (
4-
"strings"
5-
64
sdkerrors "cosmossdk.io/errors"
75
"github.com/cosmos/cosmos-sdk/codec"
86
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -27,51 +25,44 @@ type IBCModule struct {
2725
}
2826

2927
func (im IBCModule) OnChanOpenInit(
30-
ctx sdk.Context, order ibcchanneltypes.Order, _ []string, portID, channelID string,
28+
ctx sdk.Context, _ ibcchanneltypes.Order, _ []string, portID, channelID string,
3129
channelCap *capabilitytypes.Capability, _ ibcchanneltypes.Counterparty, version string,
3230
) (string, error) {
33-
if strings.TrimSpace(version) == "" {
34-
version = types.Version
35-
}
3631
if version != types.Version {
3732
return "", types.NewErrorInvalidVersion(version, types.Version)
3833
}
3934

40-
if order != ibcchanneltypes.ORDERED {
41-
return "", types.NewErrorInvalidChannelOrdering(order, ibcchanneltypes.ORDERED)
42-
}
43-
4435
boundPortID := im.keeper.GetPortID(ctx)
45-
if boundPortID != portID {
36+
if portID != boundPortID {
4637
return "", types.NewErrorInvalidPort(portID, boundPortID)
4738
}
4839

49-
if err := im.keeper.ClaimCapability(ctx, channelCap, ibchost.ChannelCapabilityPath(portID, channelID)); err != nil {
40+
capPath := ibchost.ChannelCapabilityPath(portID, channelID)
41+
if err := im.keeper.ClaimCapability(ctx, channelCap, capPath); err != nil {
5042
return "", err
5143
}
5244

53-
return version, nil
45+
return types.Version, nil
5446
}
5547

5648
func (im IBCModule) OnChanOpenTry(
57-
ctx sdk.Context, order ibcchanneltypes.Order, _ []string, portID, channelID string,
49+
ctx sdk.Context, _ ibcchanneltypes.Order, _ []string, portID, channelID string,
5850
channelCap *capabilitytypes.Capability, _ ibcchanneltypes.Counterparty, counterpartyVersion string,
5951
) (string, error) {
6052
if counterpartyVersion != types.Version {
6153
return "", types.NewErrorInvalidCounterpartyVersion(counterpartyVersion, types.Version)
6254
}
6355

64-
if order != ibcchanneltypes.ORDERED {
65-
return "", types.NewErrorInvalidChannelOrdering(order, ibcchanneltypes.ORDERED)
66-
}
67-
6856
boundPortID := im.keeper.GetPortID(ctx)
69-
if boundPortID != portID {
57+
if portID != boundPortID {
7058
return "", types.NewErrorInvalidPort(portID, boundPortID)
7159
}
7260

73-
if err := im.keeper.ClaimCapability(ctx, channelCap, ibchost.ChannelCapabilityPath(portID, channelID)); err != nil {
74-
return "", err
61+
capPath := ibchost.ChannelCapabilityPath(portID, channelID)
62+
if !im.keeper.AuthenticateCapability(ctx, channelCap, capPath) {
63+
if err := im.keeper.ClaimCapability(ctx, channelCap, capPath); err != nil {
64+
return "", err
65+
}
7566
}
7667

7768
return types.Version, nil
@@ -85,17 +76,20 @@ func (im IBCModule) OnChanOpenAck(_ sdk.Context, _, _, _ string, counterpartyVer
8576
return nil
8677
}
8778

88-
func (im IBCModule) OnChanOpenConfirm(_ sdk.Context, _, _ string) error { return nil }
79+
func (im IBCModule) OnChanOpenConfirm(_ sdk.Context, _, _ string) error {
80+
return nil
81+
}
8982

9083
func (im IBCModule) OnChanCloseInit(_ sdk.Context, _, _ string) error {
9184
return sdkerrors.Wrap(ibcerrors.ErrInvalidRequest, "user cannot close the channel")
9285
}
9386

94-
func (im IBCModule) OnChanCloseConfirm(_ sdk.Context, _, _ string) error { return nil }
87+
func (im IBCModule) OnChanCloseConfirm(_ sdk.Context, _, _ string) error {
88+
return nil
89+
}
9590

9691
func (im IBCModule) OnRecvPacket(_ sdk.Context, _ ibcchanneltypes.Packet, _ sdk.AccAddress) ibcexported.Acknowledgement {
97-
err := sdkerrors.Wrap(ibcerrors.ErrInvalidRequest, "oracle module can not receive the packets")
98-
return ibcchanneltypes.NewErrorAcknowledgement(err)
92+
return ibcchanneltypes.NewErrorAcknowledgement(sdkerrors.Wrap(ibcerrors.ErrInvalidRequest, "oracle module can not receive the packets"))
9993
}
10094

10195
func (im IBCModule) OnAcknowledgementPacket(
@@ -109,6 +103,6 @@ func (im IBCModule) OnAcknowledgementPacket(
109103
return im.keeper.OnAcknowledgementPacket(ctx, packet, ack)
110104
}
111105

112-
func (im IBCModule) OnTimeoutPacket(_ sdk.Context, _ ibcchanneltypes.Packet, _ sdk.AccAddress) error {
113-
return nil
106+
func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet ibcchanneltypes.Packet, _ sdk.AccAddress) error {
107+
return im.keeper.OnTimeoutPacket(ctx, packet)
114108
}

x/oracle/keeper/alias.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
66
)
77

8+
func (k *Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
9+
return k.capability.AuthenticateCapability(ctx, cap, name)
10+
}
11+
812
func (k *Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
913
return k.capability.ClaimCapability(ctx, cap, name)
1014
}

x/oracle/keeper/keeper.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ func NewKeeper(
3030
}
3131
}
3232

33-
func (k *Keeper) GetAuthority() string { return k.authority }
34-
func (k *Keeper) Store(ctx sdk.Context) sdk.KVStore { return ctx.KVStore(k.key) }
33+
func (k *Keeper) GetAuthority() string {
34+
return k.authority
35+
}
36+
37+
func (k *Keeper) Store(ctx sdk.Context) sdk.KVStore {
38+
return ctx.KVStore(k.key)
39+
}

x/oracle/keeper/relay.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ func (k *Keeper) handleSpotPriceQueryResponse(ctx sdk.Context, asset v1.Asset, r
117117
return nil
118118
}
119119

120+
// Unmarshal the response data to extract the spot price details.
120121
var res queryproto.SpotPriceResponse
121122
if err := k.cdc.Unmarshal(resp.GetValue(), &res); err != nil {
122123
return err
123124
}
124125

126+
// Convert the spot price to a decimal value.
125127
spotPrice, err := sdkmath.LegacyNewDecFromStr(res.GetSpotPrice())
126128
if err != nil {
127129
return err
@@ -179,3 +181,15 @@ func (k *Keeper) handleProtoRevPoolQueryResponse(ctx sdk.Context, asset v1.Asset
179181

180182
return nil
181183
}
184+
185+
// OnTimeoutPacket handles the case when a packet times out before receiving an acknowledgement.
186+
func (k *Keeper) OnTimeoutPacket(ctx sdk.Context, packet ibcchanneltypes.Packet) error {
187+
// Retrieve the source port, channel, and sequence number from the packet.
188+
portID := packet.GetSourcePort()
189+
channelID := packet.GetSourceChannel()
190+
sequence := packet.GetSequence()
191+
192+
// Delete the denom mapping associated with the timed-out packet.
193+
k.DeleteDenomForPacket(ctx, portID, channelID, sequence)
194+
return nil
195+
}

x/oracle/services/v1/query_server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package v1
22

33
import (
44
"context"
5+
56
sdk "github.com/cosmos/cosmos-sdk/types"
6-
"github.com/sentinel-official/hub/v12/x/oracle/keeper"
7-
"github.com/sentinel-official/hub/v12/x/oracle/types/v1"
87
"google.golang.org/grpc/codes"
98
"google.golang.org/grpc/status"
9+
10+
"github.com/sentinel-official/hub/v12/x/oracle/keeper"
11+
"github.com/sentinel-official/hub/v12/x/oracle/types/v1"
1012
)
1113

1214
var (

x/oracle/types/keys.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package types
22

3+
import (
4+
ibcicqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v7/types"
5+
)
6+
37
const (
48
ModuleName = "oracle"
5-
PortID = ModuleName
69
StoreKey = ModuleName
7-
Version = "oracle-1"
10+
Version = ibcicqtypes.Version
811
)
912

1013
var (

x/subscription/services/v3/query_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v3
22

33
import (
44
"context"
5+
56
"google.golang.org/grpc/codes"
67
"google.golang.org/grpc/status"
78

0 commit comments

Comments
 (0)