-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CORE-834] Wire x/ratelimit methods to IBC transfer logic #962
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
70aceca
Connect `x/ratelimit` to IBC transfer logic
teddyding 91b5c70
fix test
teddyding 72269c9
Split ibc.go
teddyding ed04626
comments
teddyding de0dba2
fix unit test
teddyding 4bfc25e
lint
teddyding 6313cc9
comments
teddyding b700060
nit
teddyding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package ratelimit | ||
|
||
// TODO(CORE-854): Improve attribution message. | ||
ttl33 marked this conversation as resolved.
Show resolved
Hide resolved
teddyding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This file was written with reference to Stride's IBC Rate Limit implementation: | ||
// https://github.com/Stride-Labs/stride/blob/121f2ac5d2e5f8e406f89999410a49ea4277a552/x/ratelimit | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/keeper" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/util" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" | ||
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck | ||
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" | ||
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types" | ||
|
||
"github.com/cosmos/ibc-go/v8/modules/core/exported" | ||
) | ||
|
||
var _ porttypes.Middleware = &IBCMiddleware{} | ||
|
||
type IBCMiddleware struct { | ||
app porttypes.IBCModule | ||
keeper keeper.Keeper | ||
} | ||
|
||
// TODO(CORE-855): Add e2e testing for the x/ratelimit IBC Middlware | ||
func NewIBCMiddleware(k keeper.Keeper, app porttypes.IBCModule) IBCMiddleware { | ||
return IBCMiddleware{ | ||
app: app, | ||
keeper: k, | ||
} | ||
} | ||
|
||
// OnChanOpenInit implements the IBCMiddleware interface. | ||
// Use default implementation of the underlying IBC application, since rate-limiting logic | ||
// doesn't require customized logic at channel initialization. | ||
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) { | ||
return im.app.OnChanOpenInit( | ||
ttl33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx, | ||
order, | ||
connectionHops, | ||
portID, | ||
channelID, | ||
channelCap, | ||
counterparty, | ||
version, | ||
) | ||
} | ||
|
||
// OnChanOpenTry implements the IBCMiddleware interface | ||
// Use default implementation of the underlying IBC application, since rate-limiting logic | ||
// doesn't require customized logic at channel opening. | ||
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, | ||
) | ||
} | ||
|
||
// OnChanOpenAck implements the IBCMiddleware interface | ||
func (im IBCMiddleware) OnChanOpenAck( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
counterpartyChannelID string, | ||
counterpartyVersion string, | ||
) error { | ||
return im.app.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion) | ||
} | ||
|
||
// OnChanOpenConfirm implements the IBCMiddleware interface | ||
func (im IBCMiddleware) OnChanOpenConfirm( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) error { | ||
return im.app.OnChanOpenConfirm(ctx, portID, channelID) | ||
} | ||
|
||
// OnChanCloseInit implements the IBCMiddleware interface | ||
func (im IBCMiddleware) OnChanCloseInit( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) error { | ||
return im.app.OnChanCloseInit(ctx, portID, channelID) | ||
} | ||
|
||
// OnChanCloseConfirm implements the IBCMiddleware interface | ||
func (im IBCMiddleware) OnChanCloseConfirm( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) error { | ||
return im.app.OnChanCloseConfirm(ctx, portID, channelID) | ||
} | ||
|
||
// OnRecvPacket implements the IBCMiddleware interface | ||
ttl33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Called on the receiver chain when a relayer pick up the `SendPacket` event from sender chain and relayer | ||
// to the receiver chain. On dYdX chain, this signals an inbound IBC transfer. | ||
// Does the following: | ||
// - Call `IncrementCapacitiesForDenom` to update `capacity` for the token received. | ||
// - Invoke `OnRecvPacket` callback on IBC transfer module. | ||
func (im IBCMiddleware) OnRecvPacket( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
relayer sdk.AccAddress, | ||
) exported.Acknowledgement { | ||
ibcTransferPacketInfo, err := util.ParsePacketInfo(packet, types.PACKET_RECV) | ||
if err != nil { | ||
im.keeper.Logger(ctx).Error(fmt.Sprintf("ICS20 receive packet could not be parsed: %s", err.Error())) | ||
return channeltypes.NewErrorAcknowledgement(err) | ||
} | ||
|
||
// Process deposit of incoming asset. | ||
// If the `Recv` fails on the underlying transfer stack (e.g. Reciver is invalid), the state | ||
// change is not committed. | ||
// TODO(CORE-855): Add an E2E test for this. | ||
im.keeper.IncrementCapacitiesForDenom(ctx, ibcTransferPacketInfo.Denom, ibcTransferPacketInfo.Amount) | ||
|
||
return im.app.OnRecvPacket(ctx, packet, relayer) | ||
} | ||
|
||
// OnAcknowledgementPacket implements the IBCMiddleware interface | ||
func (im IBCMiddleware) OnAcknowledgementPacket( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
acknowledgement []byte, | ||
relayer sdk.AccAddress, | ||
) error { | ||
if err := im.keeper.AcknowledgeIBCTransferPacket(ctx, packet, acknowledgement); err != nil { | ||
im.keeper.Logger(ctx).Error(fmt.Sprintf("ICS20 OnAckPacket failed: %s", err.Error())) | ||
return err | ||
} | ||
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) | ||
} | ||
|
||
// OnTimeoutPacket implements the IBCMiddleware interface | ||
func (im IBCMiddleware) OnTimeoutPacket( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
relayer sdk.AccAddress, | ||
) error { | ||
if err := im.keeper.TimeoutIBCTransferPacket(ctx, packet); err != nil { | ||
im.keeper.Logger(ctx).Error(fmt.Sprintf("ICS20 OnTimeoutPacket failed: %s", err.Error())) | ||
return err | ||
} | ||
return im.app.OnTimeoutPacket(ctx, packet, relayer) | ||
} | ||
|
||
// SendPacket implements the ICS4 Wrapper interface | ||
// Rate-limited SendPacket found in RateLimit Keeper | ||
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) { | ||
return im.keeper.SendPacket( | ||
ctx, | ||
chanCap, | ||
sourcePort, | ||
sourceChannel, | ||
timeoutHeight, | ||
timeoutTimestamp, | ||
data, | ||
) | ||
} | ||
|
||
// WriteAcknowledgement implements the ICS4 Wrapper interface | ||
func (im IBCMiddleware) WriteAcknowledgement( | ||
ctx sdk.Context, | ||
chanCap *capabilitytypes.Capability, | ||
packet exported.PacketI, | ||
ack exported.Acknowledgement, | ||
) error { | ||
return im.keeper.WriteAcknowledgement(ctx, chanCap, packet, ack) | ||
} | ||
|
||
// GetAppVersion returns the application version of the underlying application | ||
func (i IBCMiddleware) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) { | ||
return i.keeper.GetAppVersion(ctx, portID, channelID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of the
ratelimit
module to the capabilities grant is noted. However, the scoped keeper forratelimit
is created but not stored or used. This could potentially be an oversight.Ensure that the scoped keeper for the
ratelimit
module is stored and used appropriately, or remove it if it's unnecessary.