2021-07-12 17:42:53 +00:00
|
|
|
package types_test
|
2019-10-22 16:40:34 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-12 17:42:53 +00:00
|
|
|
"math/big"
|
2019-10-22 16:40:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-06-21 13:09:23 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-07-12 17:42:53 +00:00
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2021-06-21 13:09:23 +00:00
|
|
|
proto "github.com/gogo/protobuf/proto"
|
2021-07-12 17:42:53 +00:00
|
|
|
"github.com/tharsis/ethermint/app"
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
2021-07-12 17:42:53 +00:00
|
|
|
"github.com/tharsis/ethermint/encoding"
|
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2020-07-02 15:19:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-06-21 13:09:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-10-22 16:40:34 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2021-04-18 15:54:18 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2019-10-22 16:40:34 +00:00
|
|
|
)
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
// GenerateEthAddress generates an Ethereum address.
|
|
|
|
func GenerateEthAddress() ethcmn.Address {
|
|
|
|
priv, err := ethsecp256k1.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)
|
|
|
|
}
|
|
|
|
|
2019-10-22 16:40:34 +00:00
|
|
|
func TestEvmDataEncoding(t *testing.T) {
|
|
|
|
ret := []byte{0x5, 0x8}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
data := &evmtypes.MsgEthereumTxResponse{
|
2021-06-21 13:09:23 +00:00
|
|
|
Hash: common.BytesToHash([]byte("hash")).String(),
|
2021-07-12 17:42:53 +00:00
|
|
|
Logs: []*evmtypes.Log{{
|
2021-06-08 11:11:37 +00:00
|
|
|
Data: []byte{1, 2, 3, 4},
|
|
|
|
BlockNumber: 17,
|
|
|
|
}},
|
2020-04-01 18:49:21 +00:00
|
|
|
Ret: ret,
|
|
|
|
}
|
2019-10-22 16:40:34 +00:00
|
|
|
|
2021-06-21 13:09:23 +00:00
|
|
|
enc, err := proto.Marshal(data)
|
2020-04-01 18:49:21 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-22 16:40:34 +00:00
|
|
|
|
2021-06-21 13:09:23 +00:00
|
|
|
txData := &sdk.TxMsgData{
|
2021-07-12 17:42:53 +00:00
|
|
|
Data: []*sdk.MsgData{{MsgType: evmtypes.TypeMsgEthereumTx, Data: enc}},
|
2021-06-21 13:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txDataBz, err := proto.Marshal(txData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
res, err := evmtypes.DecodeTxResponse(txDataBz)
|
2019-10-22 16:40:34 +00:00
|
|
|
require.NoError(t, err)
|
2021-04-17 10:00:07 +00:00
|
|
|
require.NotNil(t, res)
|
2021-06-08 11:11:37 +00:00
|
|
|
require.Equal(t, data.Logs, res.Logs)
|
2020-04-01 18:49:21 +00:00
|
|
|
require.Equal(t, ret, res.Ret)
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
2021-07-12 17:42:53 +00:00
|
|
|
|
|
|
|
func TestUnwrapEthererumMsg(t *testing.T) {
|
|
|
|
_, err := evmtypes.UnwrapEthereumMsg(nil)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
clientCtx := client.Context{}.WithTxConfig(encodingConfig.TxConfig)
|
|
|
|
builder, _ := clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder)
|
|
|
|
|
|
|
|
tx := builder.GetTx().(sdk.Tx)
|
|
|
|
_, err = evmtypes.UnwrapEthereumMsg(&tx)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
msg := evmtypes.NewTx(big.NewInt(1), 0, &common.Address{}, big.NewInt(0), 0, big.NewInt(0), []byte{}, nil)
|
|
|
|
err = builder.SetMsgs(msg)
|
|
|
|
|
|
|
|
tx = builder.GetTx().(sdk.Tx)
|
|
|
|
msg_, err := evmtypes.UnwrapEthereumMsg(&tx)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, msg_, msg)
|
|
|
|
}
|