Skip to content

Commit

Permalink
Ensure rate limits are not applied to packets that aren't ics20s (#7357)
Browse files Browse the repository at this point in the history
* rate limits are not applied to packets that aren't ics20s

* added changelog

* using proper ibc hooks tag

* removed unnecesary circular import

* reset go.mod

* lint

* added contains check
  • Loading branch information
nicolaslara authored Feb 1, 2024
1 parent de82581 commit f68a6dd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [#7181](https://github.com/osmosis-labs/osmosis/pull/7181) Improve errors for out of gas
* [#7376](https://github.com/osmosis-labs/osmosis/pull/7376) Add uptime validation logic for `NoLock` (CL) gauges and switch CL gauge to pool ID links to be duration-based
* [#7357](https://github.com/osmosis-labs/osmosis/pull/7357) Fix: Ensure rate limits are not applied to packets that aren't ics20s

### Bug Fixes

Expand Down
21 changes: 21 additions & 0 deletions x/ibc-rate-limit/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ibc_rate_limit_test

import (
"fmt"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -542,3 +543,23 @@ func (suite *MiddlewareTestSuite) TestUnsetRateLimitingContract() {
// N.B.: this panics if validation fails.
paramSpace.SetParamSet(suite.chainA.GetContext(), &params)
}

// Test rate limits are reverted if a "send" fails
func (suite *MiddlewareTestSuite) TestNonICS20() {
suite.initializeEscrow()
// Setup contract
suite.chainA.StoreContractCode(&suite.Suite, "./bytecode/rate_limiter.wasm")
quotas := suite.BuildChannelQuota("weekly", "channel-0", sdk.DefaultBondDenom, 604800, 1, 1)
addr := suite.chainA.InstantiateRLContract(&suite.Suite, quotas)
suite.chainA.RegisterRateLimitingContract(addr)

osmosisApp := suite.chainA.GetOsmosisApp()

data := []byte("{}")
_, err := osmosisApp.RateLimitingICS4Wrapper.SendPacket(suite.chainA.GetContext(), capabilitytypes.NewCapability(1), "wasm.osmo1873ls0d60tg7hk00976teq9ywhzv45u3hk2urw8t3eau9eusa4eqtun9xn", "channel-0", clienttypes.NewHeight(0, 0), 1, data)

suite.Require().Error(err)
// This will error out, but not because of rate limiting
suite.Require().NotContains(err.Error(), "rate limit")
suite.Require().Contains(err.Error(), "channel not found")
}
15 changes: 12 additions & 3 deletions x/ibc-rate-limit/ics4_wrapper.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package ibc_rate_limit

import (
"encoding/json"

errorsmod "cosmossdk.io/errors"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/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"
"github.com/cosmos/ibc-go/v7/modules/core/exported"

"github.com/osmosis-labs/osmosis/v22/x/ibc-rate-limit/types"
)

Expand Down Expand Up @@ -57,6 +59,13 @@ func NewICS4Middleware(
// If the contract param is not configured, or the contract doesn't have a configuration for the (channel+denom) being
// used, transfers are not prevented and handled by the wrapped IBC app
func (i *ICS4Wrapper) SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability, sourcePort, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) (uint64, error) {
var packetdata transfertypes.FungibleTokenPacketData
if err := json.Unmarshal(data, &packetdata); err != nil {
return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
if packetdata.Denom == "" || packetdata.Amount == "" {
return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
contract := i.GetContractAddress(ctx)
if contract == "" {
// The contract has not been configured. Continue as usual
Expand Down

0 comments on commit f68a6dd

Please sign in to comment.