Skip to content

Commit

Permalink
chore: add MsgTimeout test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 26, 2024
1 parent 8321fe1 commit 3aa82ef
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 3 deletions.
4 changes: 1 addition & 3 deletions pkg/messages/msg_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ func ParseMsgTimeout(data []byte, chain *configTypes.Chain, height int64) (types
parsedPacket, err := packet.ParsePacket(parsedMessage.Packet, chain)
if err != nil {
return nil, err
} else if parsedPacket == nil {
return nil, nil
}

return &MsgTimeout{
Expand All @@ -40,7 +38,7 @@ func ParseMsgTimeout(data []byte, chain *configTypes.Chain, height int64) (types
}, nil
}

func (m MsgTimeout) Type() string {
func (m *MsgTimeout) Type() string {
return "/ibc.core.channel.v1.MsgTimeout"
}

Expand Down
208 changes: 208 additions & 0 deletions pkg/messages/msg_timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package messages

import (
aliasManagerPkg "main/pkg/alias_manager"
configPkg "main/pkg/config"
configTypes "main/pkg/config/types"
"main/pkg/data_fetcher"
"main/pkg/fs"
loggerPkg "main/pkg/logger"
packet2 "main/pkg/messages/packet"
"main/pkg/metrics"
"main/pkg/types"
"main/pkg/types/event"
"testing"

cosmosTypes "github.com/cosmos/cosmos-sdk/types"
cosmosBankTypes "github.com/cosmos/cosmos-sdk/x/bank/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 TestMsgTimeoutParseFail(t *testing.T) {
t.Parallel()

parsed, err := ParseMsgTimeout([]byte("bytes"), &configTypes.Chain{Name: "chain"}, 100)
require.Error(t, err)
require.Nil(t, parsed)
}

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

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

msg := &ibcChannelTypes.MsgTimeout{
Signer: "signer",
Packet: packet,
}
msgBytes, err := proto.Marshal(msg)
require.NoError(t, err)

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

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

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

msgInternalBytes, err := ibcTypes.ModuleCdc.MarshalJSON(msgInternal)
require.NoError(t, err)

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

msg := &ibcChannelTypes.MsgTimeout{
Signer: "signer",
Packet: packet,
}
msgBytes, err := proto.Marshal(msg)
require.NoError(t, err)

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

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

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

msgInternalBytes, err := ibcTypes.ModuleCdc.MarshalJSON(msgInternal)
require.NoError(t, err)

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

msg := &ibcChannelTypes.MsgTimeout{
Signer: "signer",
Packet: packet,
}
msgBytes, err := proto.Marshal(msg)
require.NoError(t, err)

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

require.Equal(t, "/ibc.core.channel.v1.MsgTimeout", parsed.Type())

values := parsed.GetValues()

require.Equal(t, event.EventValues{
event.From(cosmosTypes.EventTypeMessage, cosmosTypes.AttributeKeyAction, "/ibc.core.channel.v1.MsgTimeout"),
event.From(cosmosTypes.EventTypeMessage, cosmosTypes.AttributeKeySender, "signer"),
event.From(ibcTypes.EventTypePacket, cosmosBankTypes.AttributeKeyReceiver, "receiver"),
event.From(ibcTypes.EventTypePacket, cosmosTypes.AttributeKeyAmount, "100"),
event.From(cosmosTypes.EventTypeMessage, cosmosTypes.AttributeKeySender, "sender"),
}, values)

parsed.AddParsedMessage(nil)
parsed.SetParsedMessages([]types.Message{})
require.Empty(t, parsed.GetParsedMessages())
require.Empty(t, parsed.GetRawMessages())
}

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

config := &configPkg.AppConfig{
Chains: configTypes.Chains{
{
Name: "chain",
ChainID: "chain-id",
Denoms: configTypes.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6, CoingeckoCurrency: "cosmos"},
},
},
},
Metrics: configPkg.MetricsConfig{Enabled: false},
AliasesPath: "path.toml",
}

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

msgInternalBytes, err := ibcTypes.ModuleCdc.MarshalJSON(msgInternal)
require.NoError(t, err)

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

msg := &ibcChannelTypes.MsgTimeout{
Signer: "signer",
Packet: packet,
}
msgBytes, err := proto.Marshal(msg)
require.NoError(t, err)

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

filesystem := &fs.MockFs{}
logger := loggerPkg.GetNopLogger()
aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem)
metricsManager := metrics.NewManager(logger, config.Metrics)
dataFetcher := data_fetcher.NewDataFetcher(logger, config, aliasManager, metricsManager)

err = aliasManager.Set("subscription", "chain", "signer", "signer_alias")
require.NoError(t, err)
err = aliasManager.Set("subscription", "chain", "receiver", "receiver_alias")
require.NoError(t, err)

parsed.GetAdditionalData(dataFetcher, "subscription")

message, ok := parsed.(*MsgTimeout)
require.True(t, ok)
require.Equal(t, "signer_alias", message.Signer.Title)

packetParsed, ok := message.Packet.(*packet2.FungibleTokenPacket)
require.True(t, ok)
require.Equal(t, "receiver_alias", packetParsed.Receiver.Title)
}

0 comments on commit 3aa82ef

Please sign in to comment.