Skip to content

Commit

Permalink
chore: test converter
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Mar 3, 2024
1 parent 13ca1db commit 88dc3b7
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"main/pkg/types"
"strings"

abciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cometbft/cometbft/libs/json"
coreTypes "github.com/cometbft/cometbft/rpc/core/types"
Expand Down Expand Up @@ -115,7 +114,7 @@ func (c *Converter) ParseEvent(event jsonRpcTypes.RPCResponse, nodeURL string) t
txMessages := []types.Message{}

for _, message := range txProto.GetBody().Messages {
if msgParsed := c.ParseMessage(message, txResult); msgParsed != nil {
if msgParsed := c.ParseMessage(message, txResult.Height); msgParsed != nil {
txMessages = append(txMessages, msgParsed)
}
}
Expand All @@ -141,14 +140,14 @@ func (c *Converter) ParseEvent(event jsonRpcTypes.RPCResponse, nodeURL string) t

func (c *Converter) ParseMessage(
message *codecTypes.Any,
txResult abciTypes.TxResult,
height int64,
) types.Message {
parser, ok := c.Parsers[message.TypeUrl]
if !ok {
return &messages.MsgUnsupportedMessage{MsgType: message.TypeUrl}
}

msgParsed, err := parser(message.Value, c.Chain, txResult.Height)
msgParsed, err := parser(message.Value, c.Chain, height)
if err != nil {
return &messages.MsgUnparsedMessage{
MsgType: message.TypeUrl,
Expand All @@ -161,7 +160,7 @@ func (c *Converter) ParseMessage(

// Processing internal messages (such as ones in MsgExec
for _, internalMessage := range msgParsed.GetRawMessages() {
if internalMessageParsed := c.ParseMessage(internalMessage, txResult); internalMessageParsed != nil {
if internalMessageParsed := c.ParseMessage(internalMessage, height); internalMessageParsed != nil {
msgParsed.AddParsedMessage(internalMessageParsed)
}
}
Expand Down
181 changes: 181 additions & 0 deletions pkg/converter/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package converter_test

import (
configTypes "main/pkg/config/types"
converterPkg "main/pkg/converter"
loggerPkg "main/pkg/logger"
"main/pkg/messages"
"main/pkg/types"
"testing"

jsonRpcTypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
cosmosAuthzTypes "github.com/cosmos/cosmos-sdk/x/authz"
cosmosBankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
)

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Error: &jsonRpcTypes.RPCError{Message: "test message"},
}
result := converter.ParseEvent(event, "example")
require.NotNil(t, result)
require.IsType(t, &types.TxError{}, result)

event2 := jsonRpcTypes.RPCResponse{
Error: &jsonRpcTypes.RPCError{Message: "client is already subscribed"},
}
result2 := converter.ParseEvent(event2, "example")
require.Nil(t, result2)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Result: []byte("test"),
}
result := converter.ParseEvent(event, "example")
require.NotNil(t, result)
require.IsType(t, &types.TxError{}, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Result: []byte("{\"data\":null}"),
}
result := converter.ParseEvent(event, "example")
require.Nil(t, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Result: []byte("{\"data\":{\"type\":\"tendermint/event/NewBlock\",\"value\":{}}}"),
}
result := converter.ParseEvent(event, "example")
require.Nil(t, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

event := jsonRpcTypes.RPCResponse{
Result: []byte("{\"data\":{\"type\":\"tendermint/event/Tx\",\"value\":{\"TxResult\":{\"height\":\"1\",\"index\":9,\"tx\":\"CmEKXwooL3NlbnRpbmVsLm5vZGUudjIuTXNnVXBkYXRlU3RhdHVzUmVxdWVzdBIzCi9zZW50bm9kZTFmdGNycnU0MDdmbGdhZTB0cm4wbjRja2RtY2w1aDZsZ3V4ZHIydBABEmcKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQIgzmHYcht/wBxkUOilsMRa2qUxPhHn8smOT1eFQBxlmRIECgIIARheEhMKDQoFdWR2cG4SBDk1MjYQk+gFGkB20FDj4l1Btj7avEltQAB3KH63PHg+52nXfcshadIwZmDErlv5dzF1Jz/d2NIs4gRj/5/twPFCabAffMlLsYlm\",\"result\":{\"data\":\"CisKKS9zZW50aW5lbC5ub2RlLnYyLk1zZ1VwZGF0ZURldGFpbHNSZXF1ZXN0\",\"log\":\"\",\"gas_wanted\":\"106365\",\"gas_used\":\"102726\",\"events\":[]}}}},\"events\":{}}"),
}
result := converter.ParseEvent(event, "example")
require.NotNil(t, result)
require.IsType(t, &types.Tx{}, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

message := &codecTypes.Any{TypeUrl: "unsupported"}
result := converter.ParseMessage(message, 123)
require.NotNil(t, result)
require.IsType(t, &messages.MsgUnsupportedMessage{}, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

// msgSend := &cosmosBankTypes.MsgSend{}
// bytes, err := msgSend.Marshal()
// require.NoError(t, err)

message := &codecTypes.Any{
TypeUrl: "/cosmos.bank.v1beta1.MsgSend",
Value: []byte("unparsed"),
}
result := converter.ParseMessage(message, 123)
require.NotNil(t, result)
require.IsType(t, &messages.MsgUnparsedMessage{}, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

msgSend := &cosmosBankTypes.MsgSend{}
bytes, err := msgSend.Marshal()
require.NoError(t, err)

message := &codecTypes.Any{
TypeUrl: "/cosmos.bank.v1beta1.MsgSend",
Value: bytes,
}
result := converter.ParseMessage(message, 123)
require.NotNil(t, result)
require.IsType(t, &messages.MsgSend{}, result)
}

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

logger := loggerPkg.GetDefaultLogger()
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

msgSend := &cosmosBankTypes.MsgSend{}
msgSendBytes, err := msgSend.Marshal()
require.NoError(t, err)

msgExec := &cosmosAuthzTypes.MsgExec{
Msgs: []*codecTypes.Any{
{
TypeUrl: "/cosmos.bank.v1beta1.MsgSend",
Value: msgSendBytes,
},
},
}
bytes, err := msgExec.Marshal()
require.NoError(t, err)

message := &codecTypes.Any{
TypeUrl: "/cosmos.authz.v1beta1.MsgExec",
Value: bytes,
}
result := converter.ParseMessage(message, 123)
require.NotNil(t, result)
require.IsType(t, &messages.MsgExec{}, result)
require.Len(t, result.GetParsedMessages(), 1)
require.IsType(t, &messages.MsgSend{}, result.GetParsedMessages()[0])
}

0 comments on commit 88dc3b7

Please sign in to comment.