Skip to content
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

refactor: memo parsing logic in ibc_packet_handlers.go #321

Merged
merged 6 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 14 additions & 35 deletions x/interchainstaking/keeper/ibc_packet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"

"cosmossdk.io/math"
"github.com/golang/protobuf/proto" //nolint:staticcheck

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"

"github.com/cosmos/cosmos-sdk/telemetry"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
"github.com/golang/protobuf/proto" //nolint:staticcheck
lsmstakingtypes "github.com/iqlusioninc/liquidity-staking-module/x/staking/types"

"github.com/ingenuity-build/quicksilver/utils"
Expand All @@ -34,7 +31,6 @@ import (

const (
transferPort = "transfer"
withdrawal = "withdrawal"
)

type TypedMsg struct {
Expand Down Expand Up @@ -578,14 +574,10 @@ func (k *Keeper) HandleBeginRedelegate(ctx sdk.Context, msg sdk.Msg, completion
if completion.IsZero() {
return errors.New("invalid zero nil completion time")
}
parts := strings.Split(memo, "/")
if len(parts) != 2 || parts[0] != "rebalance" {
return errors.New("unexpected epoch rebalance memo format")
}

epochNumber, err := strconv.ParseInt(parts[1], 10, 64)
epochNumber, err := types.ParseMsgMemo(memo, types.MsgTypeRebalance)
if err != nil {
return errors.New("unexpected epoch rebalance memo format (2)")
return err
}

k.Logger(ctx).Info("Received MsgBeginRedelegate acknowledgement")
Expand Down Expand Up @@ -615,14 +607,9 @@ func (k *Keeper) HandleBeginRedelegate(ctx sdk.Context, msg sdk.Msg, completion
}

func (k *Keeper) HandleFailedBeginRedelegate(ctx sdk.Context, msg sdk.Msg, memo string) error {
parts := strings.Split(memo, "/")
if len(parts) != 2 || parts[0] != "rebalance" {
return errors.New("unexpected epoch rebalance memo format")
}

epochNumber, err := strconv.ParseInt(parts[1], 10, 64)
epochNumber, err := types.ParseMsgMemo(memo, types.MsgTypeRebalance)
if err != nil {
return errors.New("unexpected epoch rebalance memo format (2)")
return err
}

k.Logger(ctx).Error("Received MsgBeginRedelegate acknowledgement error")
Expand All @@ -649,15 +636,12 @@ func (k *Keeper) HandleUndelegate(ctx sdk.Context, msg sdk.Msg, completion time.
k.Logger(ctx).Error("unable to cast source message to MsgUndelegate")
return errors.New("unable to cast source message to MsgUndelegate")
}
memoParts := strings.Split(memo, "/")
if len(memoParts) != 2 || memoParts[0] != withdrawal {
return errors.New("unexpected memo form")
}

epochNumber, err := strconv.ParseInt(memoParts[1], 10, 64)
epochNumber, err := types.ParseMsgMemo(memo, types.MsgTypeWithdrawal)
if err != nil {
return err
}

zone := k.GetZoneForDelegateAccount(ctx, undelegateMsg.DelegatorAddress)

ubr, found := k.GetUnbondingRecord(ctx, zone.ChainId, undelegateMsg.ValidatorAddress, epochNumber)
Expand Down Expand Up @@ -707,14 +691,9 @@ func (k *Keeper) HandleUndelegate(ctx sdk.Context, msg sdk.Msg, completion time.
}

func (k *Keeper) HandleFailedUndelegate(ctx sdk.Context, msg sdk.Msg, memo string) error {
parts := strings.Split(memo, "/")
if len(parts) != 2 || parts[0] != withdrawal {
return errors.New("unexpected epoch undelegate memo format")
}

epochNumber, err := strconv.ParseInt(parts[1], 10, 64)
epochNumber, err := types.ParseMsgMemo(memo, types.MsgTypeWithdrawal)
if err != nil {
return errors.New("unexpected epoch undelegate memo format (2)")
return err
}

k.Logger(ctx).Error("Received MsgUndelegate acknowledgement error")
Expand Down
50 changes: 50 additions & 0 deletions x/interchainstaking/types/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package types_test

import (
"strconv"
"testing"

"github.com/stretchr/testify/require"

"github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
)

func FuzzParseMsgMemo(f *testing.F) {
// seed corpus
tests := []struct {
memo string
msgType string
}{
{
memo: types.MsgTypeRebalance + "/" + "10",
msgType: types.MsgTypeRebalance,
},
{
memo: types.MsgTypeWithdrawal + "/" + "10",
msgType: types.MsgTypeWithdrawal,
},
{
memo: "invalid" + "/" + "10",
msgType: types.MsgTypeWithdrawal,
},
{
memo: types.MsgTypeWithdrawal + "/" + "A",
msgType: types.MsgTypeWithdrawal,
},
}
for _, tt := range tests {
f.Add(tt.memo, tt.msgType)
}

f.Fuzz(func(t *testing.T, memo, msgType string) {
epochNumber, err := types.ParseMsgMemo(memo, msgType)
if err != nil {
return
}

newMemo := msgType + "/" + strconv.FormatInt(epochNumber, 10)
epochNumber2, err := types.ParseMsgMemo(newMemo, msgType)
require.NoError(t, err)
require.Equal(t, epochNumber, epochNumber2)
})
}
26 changes: 26 additions & 0 deletions x/interchainstaking/types/ibc_packet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

import (
"fmt"
"strconv"
"strings"
)

const (
MsgTypeWithdrawal = "withdrawal"
MsgTypeRebalance = "rebalance"
)

func ParseMsgMemo(memo, msgType string) (epochNumber int64, err error) {
parts := strings.Split(memo, "/")
if len(parts) != 2 || parts[0] != msgType {
return 0, fmt.Errorf("unexpected epoch %s memo format", msgType)
}

epochNumber, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return 0, fmt.Errorf("unexpected epoch %s memo format: %w", msgType, err)
}

return
}
58 changes: 58 additions & 0 deletions x/interchainstaking/types/ibc_packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
)

func TestParseMsgMemo(t *testing.T) {
tests := []struct {
name string
memo string
msgType string
wantErr bool
expectedEpochNumber int64
}{
{
name: "valid rebalance",
memo: types.MsgTypeRebalance + "/" + "10",
msgType: types.MsgTypeRebalance,
wantErr: false,
expectedEpochNumber: 10,
},
{
name: "valid withdrawal",
memo: types.MsgTypeWithdrawal + "/" + "10",
msgType: types.MsgTypeWithdrawal,
wantErr: false,
expectedEpochNumber: 10,
},
{
name: "invalid msg type",
memo: "invalid" + "/" + "10",
msgType: types.MsgTypeWithdrawal,
wantErr: true,
},
{
name: "invalid epoch number",
memo: types.MsgTypeWithdrawal + "/" + "A",
msgType: types.MsgTypeWithdrawal,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
epochNumber, err := types.ParseMsgMemo(tt.memo, tt.msgType)
if tt.wantErr {
t.Logf("Error:\n%v\n", err)
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.expectedEpochNumber, epochNumber)
})
}
}