Skip to content

Commit

Permalink
chore: add Packet parse test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 26, 2024
1 parent e34ed6a commit 8321fe1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/messages/packet/interchain_accounts_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package packet

import (
"fmt"
cosmosBankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
configTypes "main/pkg/config/types"
"main/pkg/types"
"main/pkg/types/event"
"strconv"

cosmosBankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/std"

Expand Down
3 changes: 2 additions & 1 deletion pkg/messages/packet/interchain_accounts_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package packet

import (
"fmt"
icaTypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
aliasManagerPkg "main/pkg/alias_manager"
configPkg "main/pkg/config"
configTypes "main/pkg/config/types"
Expand All @@ -15,6 +14,8 @@ import (
"math/big"
"testing"

icaTypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"

types2 "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
Expand Down
91 changes: 91 additions & 0 deletions pkg/messages/packet/packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package packet

import (
configTypes "main/pkg/config/types"
"testing"

types2 "github.com/cosmos/cosmos-sdk/codec/types"
icaTypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibcTypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcChannelTypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
)

func TestPacketFungibleParseOk(t *testing.T) {
t.Parallel()

msg := &ibcTypes.FungibleTokenPacketData{
Denom: "uatom",
Amount: "100",
Sender: "sender",
Receiver: "receiver",
Memo: "memo",
}

msgBytes, err := ibcTypes.ModuleCdc.MarshalJSON(msg)
require.NoError(t, err)

packet := ibcChannelTypes.Packet{
SourceChannel: "src_channel",
SourcePort: "src_port",
DestinationChannel: "dst_channel",
DestinationPort: "dst_port",
Data: msgBytes,
}

parsed, err := ParsePacket(packet, &configTypes.Chain{Name: "chain"})
require.NoError(t, err)
require.NotNil(t, parsed)

_, ok := parsed.(*FungibleTokenPacket)
require.True(t, ok)
}

func TestPacketInterchainParseOk(t *testing.T) {
t.Parallel()

txInternal := &icaTypes.CosmosTx{
Messages: []*types2.Any{},
}
txBytes, err := proto.Marshal(txInternal)
require.NoError(t, err)

icaPacket := &icaTypes.InterchainAccountPacketData{
Type: icaTypes.EXECUTE_TX,
Data: txBytes,
}
icaPacketBytes, err := ibcTypes.ModuleCdc.MarshalJSON(icaPacket)
require.NoError(t, err)

packet := ibcChannelTypes.Packet{
SourceChannel: "src_channel",
SourcePort: "src_port",
DestinationChannel: "dst_channel",
DestinationPort: "dst_port",
Data: icaPacketBytes,
}

parsed, err := ParsePacket(packet, &configTypes.Chain{Name: "chain"})
require.NoError(t, err)
require.NotNil(t, parsed)

_, ok := parsed.(*InterchainAccountsPacket)
require.True(t, ok)
}

func TestPacketParseFail(t *testing.T) {
t.Parallel()

packet := ibcChannelTypes.Packet{
SourceChannel: "src_channel",
SourcePort: "src_port",
DestinationChannel: "dst_channel",
DestinationPort: "dst_port",
Data: []byte("invalid"),
}

parsed, err := ParsePacket(packet, &configTypes.Chain{Name: "chain"})
require.Error(t, err)
require.Nil(t, parsed)
}

0 comments on commit 8321fe1

Please sign in to comment.