Skip to content

Commit

Permalink
feat: implement OnTimeoutPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Nov 16, 2024
1 parent 77f6e7d commit f3e4e2e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 33 deletions.
48 changes: 21 additions & 27 deletions x/oracle/ibc_module.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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)
}
4 changes: 4 additions & 0 deletions x/oracle/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 7 additions & 2 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions x/oracle/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions x/oracle/services/v1/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
7 changes: 5 additions & 2 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
1 change: 1 addition & 0 deletions x/subscription/services/v3/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v3

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down

0 comments on commit f3e4e2e

Please sign in to comment.