Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(uibc): handle params.ics20_hooks switch #2461

Merged
merged 15 commits into from
Mar 16, 2024
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### Features

- [2459](https://github.com/umee-network/umee/pull/2459), [2461](https://github.com/umee-network/umee/pull/2461) uibc: handle `params.ics20_hooks` switch (enabled / disabled).

## v6.4.0-beta1 - 2024-03-11

### Features
Expand Down
4 changes: 4 additions & 0 deletions x/uibc/gmp/gmp_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func NewHandler() *Handler {

func (h Handler) OnRecvPacket(ctx sdk.Context, coinReceived sdk.Coin, memo string, receiver sdk.AccAddress,
) error {
if len(memo) == 0 {
return nil
}

logger := ctx.Logger().With("handler", "gmp_handler")
var msg Message
var err error
Expand Down
4 changes: 3 additions & 1 deletion x/uibc/uics20/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@
return ackResp
}

params := quotaKeeper.GetParams()

Check warning on line 66 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L66

Added line #L66 was not covered by tests

// NOTE: IBC hooks must be the last middleware - just the transfer app.
// MemoHandler may update amoount in the message, because the received token amount may be
// smaller than the amount originally sent (various fees). We need to be sure that there is
// no other middleware that can change packet data or amounts.

mh := MemoHandler{cdc: im.cdc, leverage: im.leverage}
mh := MemoHandler{executeEnabled: params.Ics20Hooks, cdc: im.cdc, leverage: im.leverage}

Check warning on line 73 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L73

Added line #L73 was not covered by tests
events, err := mh.onRecvPacketPrepare(&ctx, packet, ftData)
if err != nil {
if !errors.Is(err, errMemoValidation{}) {
Expand Down
31 changes: 19 additions & 12 deletions x/uibc/uics20/memo_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ import (
"github.com/umee-network/umee/v6/x/uibc/gmp"
)

// error messages
var (
errWrongSigner = errors.New("msg signer doesn't match the ICS20 receiver")
errNoSubCoins = errors.New("message must use only coins sent from the transfer")
errHooksDisabled = errors.New("ics20-hooks: execute is disabled but valid hook memo is set")
errMsg0Type = errors.New("only MsgSupply, MsgSupplyCollateral and MsgLiquidate are supported as messages[0]")
// errMsg1Type = errors.New("only MsgBorrow is supported as messages[1]")
)

type MemoHandler struct {
cdc codec.JSONCodec
leverage ltypes.MsgServer

isGMP bool
msgs []sdk.Msg
memo string
received sdk.Coin
executeEnabled bool
isGMP bool
msgs []sdk.Msg
memo string
received sdk.Coin

receiver sdk.AccAddress
fallbackReceiver sdk.AccAddress
Expand Down Expand Up @@ -85,9 +95,14 @@ func (mh *MemoHandler) onRecvPacketPrepare(
}

// runs messages encoded in the ICS20 memo.
// Returns list of
// NOTE: we fork the store and only commit if all messages pass. Otherwise the fork store
// is discarded.
func (mh MemoHandler) execute(ctx *sdk.Context) error {
if !mh.executeEnabled && (mh.isGMP || len(mh.msgs) != 0) {
return errHooksDisabled
}

logger := recvPacketLogger(ctx)
if mh.isGMP {
gh := gmp.NewHandler()
Expand All @@ -107,14 +122,6 @@ func (mh MemoHandler) execute(ctx *sdk.Context) error {
return nil
}

// error messages used in validateMemoMsg
var (
errWrongSigner = errors.New("msg signer doesn't match the ICS20 receiver")
errNoSubCoins = errors.New("message must use only coins sent from the transfer")
errMsg0Type = errors.New("only MsgSupply, MsgSupplyCollateral and MsgLiquidate are supported as messages[0]")
// errMsg1Type = errors.New("only MsgBorrow is supported as messages[1]")
)

// We only support the following message combinations:
// - [MsgSupply]
// - [MsgSupplyCollateral]
Expand Down
31 changes: 31 additions & 0 deletions x/uibc/uics20/memo_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uics20
import (
"testing"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -160,3 +161,33 @@ func TestAdjustOperatedCoin(t *testing.T) {
assert.Equal(t, tc.expectedAmount, tc.operated.Amount.Int64())
}
}

func TestMemoExecute(t *testing.T) {
lvg := mocks.NewLvgNoopMsgSrv()
mh := MemoHandler{leverage: lvg}
ctx, _ := tsdk.NewCtxOneStore(t, storetypes.NewMemoryStoreKey("quota"))
msgs := []sdk.Msg{&ltypes.MsgSupply{}}

tcs := []struct {
enabled bool
gmp bool
msgs []sdk.Msg
err error
}{
{true, true, msgs, nil},
{true, false, msgs, nil},
{true, false, nil, nil},

{false, true, nil, errHooksDisabled},
{false, false, msgs, errHooksDisabled},
{false, true, msgs, errHooksDisabled},
}
for i, tc := range tcs {
mh.executeEnabled = tc.enabled
mh.isGMP = tc.gmp
mh.msgs = tc.msgs
err := mh.execute(&ctx)
assert.ErrorIs(t, err, tc.err, i)
}

}
Loading