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" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"

View File

@ -1,11 +1,12 @@
package types package types
import ( import (
"bytes"
"fmt" "fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common" 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. // Validate performs a basic validation of the State fields.
func (s State) Validate() error { 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") return sdkerrors.Wrap(ErrInvalidState, "state key hash cannot be empty")
} }
// NOTE: state value can be empty // NOTE: state value can be empty

View File

@ -80,6 +80,6 @@ func TestStorageCopy(t *testing.T) {
func TestStorageString(t *testing.T) { func TestStorageString(t *testing.T) {
storage := Storage{NewState(ethcmn.BytesToHash([]byte("key")), ethcmn.BytesToHash([]byte("value")))} 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()) require.Equal(t, str, storage.String())
} }

View File

@ -4,21 +4,15 @@ import (
log "github.com/xlab/suplog" log "github.com/xlab/suplog"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
var EmptyCodeHash = crypto.Keccak256(nil) 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 // DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse
func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) { func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) {
var txMsgData sdk.TxMsgData var txMsgData sdk.TxMsgData
@ -27,17 +21,16 @@ func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) {
return nil, err return nil, err
} }
dataList := txMsgData.GetData() data := txMsgData.GetData()
if len(dataList) == 0 { if len(data) == 0 {
return &MsgEthereumTxResponse{}, nil return &MsgEthereumTxResponse{}, nil
} }
var res MsgEthereumTxResponse var res MsgEthereumTxResponse
err := proto.Unmarshal(dataList[0].GetData(), &res) err := proto.Unmarshal(data[0].GetData(), &res)
if err != nil { if err != nil {
err = errors.Wrap(err, "proto.Unmarshal failed") return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data")
return nil, err
} }
return &res, nil return &res, nil

View File

@ -3,10 +3,13 @@ package types
import ( import (
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
proto "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
) )
@ -25,6 +28,7 @@ func TestEvmDataEncoding(t *testing.T) {
ret := []byte{0x5, 0x8} ret := []byte{0x5, 0x8}
data := &MsgEthereumTxResponse{ data := &MsgEthereumTxResponse{
Hash: common.BytesToHash([]byte("hash")).String(),
Logs: []*Log{{ Logs: []*Log{{
Data: []byte{1, 2, 3, 4}, Data: []byte{1, 2, 3, 4},
BlockNumber: 17, BlockNumber: 17,
@ -32,10 +36,17 @@ func TestEvmDataEncoding(t *testing.T) {
Ret: ret, Ret: ret,
} }
enc, err := EncodeTxResponse(data) enc, err := proto.Marshal(data)
require.NoError(t, err) 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.NoError(t, err)
require.NotNil(t, res) require.NotNil(t, res)
require.Equal(t, data.Logs, res.Logs) require.Equal(t, data.Logs, res.Logs)