From 8a2a8d377f7b7a4d4b18991f87446c1bd9c6e3aa Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Mon, 21 Jun 2021 09:09:23 -0400 Subject: [PATCH] evm: fix types unit tests (#154) * tests: fix evm unit tests * lint --- x/evm/types/msg.go | 1 + x/evm/types/storage.go | 5 +++-- x/evm/types/storage_test.go | 2 +- x/evm/types/utils.go | 17 +++++------------ x/evm/types/utils_test.go | 15 +++++++++++++-- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/x/evm/types/msg.go b/x/evm/types/msg.go index b139755a..efe246c5 100644 --- a/x/evm/types/msg.go +++ b/x/evm/types/msg.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/auth/ante" + "github.com/cosmos/ethermint/types" "github.com/ethereum/go-ethereum/common" diff --git a/x/evm/types/storage.go b/x/evm/types/storage.go index dc48fa40..2c742d77 100644 --- a/x/evm/types/storage.go +++ b/x/evm/types/storage.go @@ -1,11 +1,12 @@ package types import ( - "bytes" "fmt" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/ethermint/types" + ethcmn "github.com/ethereum/go-ethereum/common" ) @@ -50,7 +51,7 @@ func (s Storage) Copy() Storage { // Validate performs a basic validation of the State fields. func (s State) Validate() error { - if bytes.Equal(ethcmn.Hex2Bytes(s.Key), ethcmn.Hash{}.Bytes()) { + if types.IsEmptyHash(s.Key) { return sdkerrors.Wrap(ErrInvalidState, "state key hash cannot be empty") } // NOTE: state value can be empty diff --git a/x/evm/types/storage_test.go b/x/evm/types/storage_test.go index 14d7ce84..95ba19c5 100644 --- a/x/evm/types/storage_test.go +++ b/x/evm/types/storage_test.go @@ -80,6 +80,6 @@ func TestStorageCopy(t *testing.T) { func TestStorageString(t *testing.T) { storage := Storage{NewState(ethcmn.BytesToHash([]byte("key")), ethcmn.BytesToHash([]byte("value")))} - str := "key:\"0x00000000000000000000000000000000000000000000000000000000006b6579\" value:\"0x00000000000000000000000000000000000000000000000000000076616c7565\"\n" + str := "key:\"0x00000000000000000000000000000000000000000000000000000000006b6579\" value:\"0x00000000000000000000000000000000000000000000000000000076616c7565\" \n" require.Equal(t, str, storage.String()) } diff --git a/x/evm/types/utils.go b/x/evm/types/utils.go index 0679f7dc..0d87480a 100644 --- a/x/evm/types/utils.go +++ b/x/evm/types/utils.go @@ -4,21 +4,15 @@ import ( log "github.com/xlab/suplog" "github.com/gogo/protobuf/proto" - "github.com/pkg/errors" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/ethereum/go-ethereum/crypto" ) var EmptyCodeHash = crypto.Keccak256(nil) -// EncodeTxResponse takes all of the necessary data from the EVM execution -// and returns the data as a byte slice encoded with protobuf. -func EncodeTxResponse(res *MsgEthereumTxResponse) ([]byte, error) { - return proto.Marshal(res) -} - // DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) { var txMsgData sdk.TxMsgData @@ -27,17 +21,16 @@ func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) { return nil, err } - dataList := txMsgData.GetData() - if len(dataList) == 0 { + data := txMsgData.GetData() + if len(data) == 0 { return &MsgEthereumTxResponse{}, nil } var res MsgEthereumTxResponse - err := proto.Unmarshal(dataList[0].GetData(), &res) + err := proto.Unmarshal(data[0].GetData(), &res) if err != nil { - err = errors.Wrap(err, "proto.Unmarshal failed") - return nil, err + return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data") } return &res, nil diff --git a/x/evm/types/utils_test.go b/x/evm/types/utils_test.go index 91117bd5..3b43534a 100644 --- a/x/evm/types/utils_test.go +++ b/x/evm/types/utils_test.go @@ -3,10 +3,13 @@ package types import ( "testing" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ethermint/crypto/ethsecp256k1" + proto "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common" ethcrypto "github.com/ethereum/go-ethereum/crypto" ) @@ -25,6 +28,7 @@ func TestEvmDataEncoding(t *testing.T) { ret := []byte{0x5, 0x8} data := &MsgEthereumTxResponse{ + Hash: common.BytesToHash([]byte("hash")).String(), Logs: []*Log{{ Data: []byte{1, 2, 3, 4}, BlockNumber: 17, @@ -32,10 +36,17 @@ func TestEvmDataEncoding(t *testing.T) { Ret: ret, } - enc, err := EncodeTxResponse(data) + enc, err := proto.Marshal(data) require.NoError(t, err) - res, err := DecodeTxResponse(enc) + txData := &sdk.TxMsgData{ + Data: []*sdk.MsgData{{MsgType: TypeMsgEthereumTx, Data: enc}}, + } + + txDataBz, err := proto.Marshal(txData) + require.NoError(t, err) + + res, err := DecodeTxResponse(txDataBz) require.NoError(t, err) require.NotNil(t, res) require.Equal(t, data.Logs, res.Logs)