evm: fix types unit tests (#154)

* tests: fix evm unit tests

* lint
This commit is contained in:
Federico Kunze 2021-06-21 09:09:23 -04:00 committed by GitHub
parent 91ad6387bf
commit 8a2a8d377f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 17 deletions

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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)