Skip to content

Commit

Permalink
Merge pull request #20 from Stride-Labs/sam/sttia-ibc-middleware
Browse files Browse the repository at this point in the history
added staketia to middleware stack
  • Loading branch information
sampocs authored Jan 19, 2024
2 parents 7567958 + 38843da commit c44cb59
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ func NewStrideApp(
// - core ibc
// - autopilot
// - records
// - staketia
// - ratelimit
// - pfm
// - transfer
Expand All @@ -763,6 +764,7 @@ func NewStrideApp(
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, // refund timeout
)
transferStack = ratelimitmodule.NewIBCMiddleware(app.RatelimitKeeper, transferStack)
transferStack = staketia.NewIBCMiddleware(app.StakeTiaKeeper, transferStack)
transferStack = recordsmodule.NewIBCModule(app.RecordsKeeper, transferStack)
transferStack = autopilot.NewIBCModule(app.AutopilotKeeper, transferStack)

Expand Down
4 changes: 4 additions & 0 deletions x/records/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ func (im IBCModule) GetAppVersion(ctx sdk.Context, portID, channelID string) (st
return ibctransfertypes.Version, true // im.keeper.GetAppVersion(ctx, portID, channelID)
}

// TODO [cleanup]: We probably don't need these AppModule callbacks,
// but we should dig into it more
// before removing

// APP MODULE IMPLEMENTATION
// OnChanOpenInit implements the IBCModule interface
func (am AppModule) OnChanOpenInit(
Expand Down
192 changes: 192 additions & 0 deletions x/staketia/ibc_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package staketia

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

"github.com/Stride-Labs/stride/v17/x/staketia/keeper"
)

var _ porttypes.Middleware = &IBCMiddleware{}

type IBCMiddleware struct {
app porttypes.IBCModule
keeper keeper.Keeper
}

// NewIBCMiddleware creates a new IBCMiddleware given the keeper
func NewIBCMiddleware(k keeper.Keeper, app porttypes.IBCModule) IBCMiddleware {
return IBCMiddleware{
app: app,
keeper: k,
}
}

// No custom logic needed for OnChanOpenInit - passes through to next middleware
func (im IBCMiddleware) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
channelCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnChanOpenAck (Staketia): portID %s, channelID %s", portID, channelID))
return im.app.OnChanOpenInit(
ctx,
order,
connectionHops,
portID,
channelID,
channelCap,
counterparty,
version,
)
}

// No custom logic needed for OnChanOpenAck - passes through to next middleware
func (im IBCMiddleware) OnChanOpenAck(
ctx sdk.Context,
portID string,
channelID string,
counterpartyChannelID string,
counterpartyVersion string,
) error {
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnChanOpenAck (Staketia): portID %s, channelID %s, counterpartyChannelID %s, counterpartyVersion %s",
portID, channelID, counterpartyChannelID, counterpartyVersion))
return im.app.OnChanOpenAck(
ctx,
portID,
channelID,
counterpartyChannelID,
counterpartyVersion,
)
}

// No custom logic needed for OnChanCloseConfirm - passes through to next middleware
func (im IBCMiddleware) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnChanCloseConfirm (Staketia): portID %s, channelID %s", portID, channelID))
return im.app.OnChanCloseConfirm(ctx, portID, channelID)
}

// OnAcknowledgementPacket must check the ack for outbound transfers of native tokens
// and update record keeping based on whether it succeeded
func (im IBCMiddleware) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnAcknowledgementPacket (Staketia): SourcePort %s, SourceChannel %s, DestinationPort %s, DestinationChannel %s",
packet.SourcePort, packet.SourceChannel, packet.DestinationPort, packet.DestinationChannel))

// TODO [sttia]: Handle transfer OnAckPacket

return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
}

// OnTimeoutPacket must check if an outbound transfer of native tokens timed out,
// and, if so, adjust record keeping
func (im IBCMiddleware) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error {
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnTimeoutPacket (Staketia): packet %v, relayer %v", packet, relayer))

// TODO [sttia]: Handle transfer OnTimeoutPacket

return im.app.OnTimeoutPacket(ctx, packet, relayer)
}

// No custom logic needed for OnChanOpenTry - passes through to next middleware
func (im IBCMiddleware) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID,
channelID string,
channelCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
return im.app.OnChanOpenTry(
ctx,
order,
connectionHops,
portID,
channelID,
channelCap,
counterparty,
counterpartyVersion,
)
}

// No custom logic needed for OnChanOpenConfirm - passes through to next middleware
func (im IBCMiddleware) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return im.app.OnChanOpenConfirm(ctx, portID, channelID)
}

// No custom logic needed for OnChanCloseInit - passes through to next middleware
func (im IBCMiddleware) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
) error {
return im.app.OnChanCloseInit(ctx, portID, channelID)
}

// No custom logic needed for OnRecvPacket - passes through to next middleware
func (im IBCMiddleware) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
return im.app.OnRecvPacket(ctx, packet, relayer)
}

// Send implements the ICS4Wrapper interface
// Staketia sits above where ICS4 traffic routes in the transfer stack
// so this should never get called
func (im IBCMiddleware) SendPacket(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (sequence uint64, err error) {
panic("Unexpected ICS4Wrapper route to staketia module")
}

// WriteAcknowledgement implements the ICS4Wrapper interface
// Staketia sits above where ICS4 traffic routes in the transfer stack
// so this should never get called
func (im IBCMiddleware) WriteAcknowledgement(
ctx sdk.Context,
channelCap *capabilitytypes.Capability,
packet ibcexported.PacketI,
ack ibcexported.Acknowledgement,
) error {
panic("Unexpected ICS4Wrapper route to staketia module")
}

// GetAppVersion implements the ICS4Wrapper interface
// Staketia sits above where ICS4 traffic routes in the transfer stack
// so this should never get called
func (im IBCMiddleware) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
panic("Unexpected ICS4Wrapper route to staketia module")
}

0 comments on commit c44cb59

Please sign in to comment.