2018-11-28 22:19:22 +00:00
package types
import (
"math/big"
"testing"
2021-05-12 13:08:31 +00:00
"github.com/stretchr/testify/suite"
2020-04-23 15:49:25 +00:00
2021-05-12 13:08:31 +00:00
"github.com/cosmos/cosmos-sdk/crypto/keyring"
2020-04-23 15:49:25 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2021-06-22 10:49:18 +00:00
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
"github.com/tharsis/ethermint/tests"
2021-04-17 10:00:07 +00:00
2018-11-28 22:19:22 +00:00
ethcmn "github.com/ethereum/go-ethereum/common"
2021-06-11 13:38:51 +00:00
"github.com/ethereum/go-ethereum/core/types"
2021-05-12 13:08:31 +00:00
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
2018-11-28 22:19:22 +00:00
)
2021-05-12 13:08:31 +00:00
type MsgsTestSuite struct {
suite . Suite
2020-04-23 15:49:25 +00:00
2021-05-12 13:08:31 +00:00
signer keyring . Signer
from ethcmn . Address
to ethcmn . Address
chainID * big . Int
}
func TestMsgsTestSuite ( t * testing . T ) {
suite . Run ( t , new ( MsgsTestSuite ) )
}
func ( suite * MsgsTestSuite ) SetupTest ( ) {
privFrom , err := ethsecp256k1 . GenerateKey ( )
suite . Require ( ) . NoError ( err )
privTo , err := ethsecp256k1 . GenerateKey ( )
suite . Require ( ) . NoError ( err )
suite . signer = tests . NewSigner ( privFrom )
suite . from = crypto . PubkeyToAddress ( privFrom . ToECDSA ( ) . PublicKey )
suite . to = crypto . PubkeyToAddress ( privTo . ToECDSA ( ) . PublicKey )
suite . chainID = big . NewInt ( 1 )
}
func ( suite * MsgsTestSuite ) TestMsgEthereumTx_Constructor ( ) {
msg := NewMsgEthereumTx ( nil , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , nil )
2021-05-14 06:52:18 +00:00
suite . Require ( ) . Equal ( msg . Data . To , suite . to . Hex ( ) )
2021-05-12 13:08:31 +00:00
suite . Require ( ) . Equal ( msg . Route ( ) , RouterKey )
suite . Require ( ) . Equal ( msg . Type ( ) , TypeMsgEthereumTx )
suite . Require ( ) . NotNil ( msg . To ( ) )
suite . Require ( ) . Equal ( msg . GetMsgs ( ) , [ ] sdk . Msg { msg } )
suite . Require ( ) . Panics ( func ( ) { msg . GetSigners ( ) } )
suite . Require ( ) . Panics ( func ( ) { msg . GetSignBytes ( ) } )
2020-04-23 15:49:25 +00:00
2021-05-10 16:34:00 +00:00
msg = NewMsgEthereumTxContract ( nil , 0 , nil , 100000 , nil , [ ] byte ( "test" ) , nil )
2021-05-12 13:08:31 +00:00
suite . Require ( ) . NotNil ( msg )
2021-05-14 06:52:18 +00:00
suite . Require ( ) . Empty ( msg . Data . To )
2021-05-12 13:08:31 +00:00
suite . Require ( ) . Nil ( msg . To ( ) )
2018-11-28 22:19:22 +00:00
}
2021-05-12 13:08:31 +00:00
func ( suite * MsgsTestSuite ) TestMsgEthereumTx_ValidateBasic ( ) {
2018-11-28 22:19:22 +00:00
testCases := [ ] struct {
2020-04-23 15:49:25 +00:00
msg string
2021-05-14 06:52:18 +00:00
to * ethcmn . Address
2018-11-28 22:19:22 +00:00
amount * big . Int
gasPrice * big . Int
2021-06-11 13:38:51 +00:00
from string
accessList * ethtypes . AccessList
chainID * big . Int
2018-11-28 22:19:22 +00:00
expectPass bool
} {
2021-06-11 13:38:51 +00:00
{ msg : "pass with recipient - Legacy Tx" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( 100000 ) , expectPass : true } ,
{ msg : "pass with recipient - AccessList Tx" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( 0 ) , accessList : & ethtypes . AccessList { } , chainID : big . NewInt ( 1 ) , expectPass : true } ,
{ msg : "pass contract - Legacy Tx" , to : nil , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( 100000 ) , expectPass : true } ,
2021-05-31 14:54:59 +00:00
{ msg : "invalid recipient" , to : & ethcmn . Address { } , amount : big . NewInt ( - 1 ) , gasPrice : big . NewInt ( 1000 ) , expectPass : false } ,
2021-06-14 12:42:34 +00:00
{ msg : "nil amount" , to : & suite . to , amount : nil , gasPrice : big . NewInt ( 1000 ) , expectPass : true } ,
2021-05-14 06:52:18 +00:00
{ msg : "negative amount" , to : & suite . to , amount : big . NewInt ( - 1 ) , gasPrice : big . NewInt ( 1000 ) , expectPass : true } ,
2021-06-11 13:38:51 +00:00
{ msg : "nil gas price" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : nil , expectPass : false } ,
2021-05-14 06:52:18 +00:00
{ msg : "negative gas price" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( - 1 ) , expectPass : true } ,
{ msg : "zero gas price" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( 0 ) , expectPass : true } ,
2021-06-11 13:38:51 +00:00
{ msg : "invalid from address" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( 0 ) , from : ethcmn . Address { } . Hex ( ) , expectPass : false } ,
{ msg : "chain ID not set on AccessListTx" , to : & suite . to , amount : big . NewInt ( 100 ) , gasPrice : big . NewInt ( 0 ) , accessList : & ethtypes . AccessList { } , chainID : nil , expectPass : false } ,
2018-11-28 22:19:22 +00:00
}
for i , tc := range testCases {
2021-06-11 13:38:51 +00:00
msg := NewMsgEthereumTx ( tc . chainID , 0 , tc . to , tc . amount , 0 , tc . gasPrice , nil , tc . accessList )
msg . From = tc . from
2021-05-12 13:08:31 +00:00
err := msg . ValidateBasic ( )
2018-11-28 22:19:22 +00:00
if tc . expectPass {
2021-06-11 13:38:51 +00:00
suite . Require ( ) . NoError ( err , "valid test %d failed: %s, %v" , i , tc . msg , msg )
2018-11-28 22:19:22 +00:00
} else {
2021-06-11 13:38:51 +00:00
suite . Require ( ) . Error ( err , "invalid test %d passed: %s, %v" , i , tc . msg , msg . Data )
2018-11-28 22:19:22 +00:00
}
}
}
2021-05-12 13:08:31 +00:00
func ( suite * MsgsTestSuite ) TestMsgEthereumTx_Sign ( ) {
testCases := [ ] struct {
msg string
2021-06-11 13:38:51 +00:00
tx * MsgEthereumTx
2021-06-01 17:14:33 +00:00
ethSigner ethtypes . Signer
2021-06-11 13:38:51 +00:00
malleate func ( tx * MsgEthereumTx )
2021-05-12 13:08:31 +00:00
expectPass bool
} {
{
2021-06-01 17:14:33 +00:00
"pass - EIP2930 signer" ,
2021-06-11 13:38:51 +00:00
NewMsgEthereumTx ( suite . chainID , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , & types . AccessList { } ) ,
2021-06-01 17:14:33 +00:00
ethtypes . NewEIP2930Signer ( suite . chainID ) ,
2021-06-11 13:38:51 +00:00
func ( tx * MsgEthereumTx ) { tx . From = suite . from . Hex ( ) } ,
2021-05-12 13:08:31 +00:00
true ,
} ,
2021-06-01 17:14:33 +00:00
{
2021-06-11 13:38:51 +00:00
"pass - EIP155 signer" ,
NewMsgEthereumTx ( suite . chainID , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , nil ) ,
2021-06-01 17:14:33 +00:00
ethtypes . NewEIP155Signer ( suite . chainID ) ,
2021-06-11 13:38:51 +00:00
func ( tx * MsgEthereumTx ) { tx . From = suite . from . Hex ( ) } ,
true ,
2021-06-01 17:14:33 +00:00
} ,
{
2021-06-11 13:38:51 +00:00
"pass - Homestead signer" ,
NewMsgEthereumTx ( suite . chainID , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , nil ) ,
2021-06-01 17:14:33 +00:00
ethtypes . HomesteadSigner { } ,
2021-06-11 13:38:51 +00:00
func ( tx * MsgEthereumTx ) { tx . From = suite . from . Hex ( ) } ,
true ,
2021-06-01 17:14:33 +00:00
} ,
{
2021-06-11 13:38:51 +00:00
"pass - Frontier signer" ,
NewMsgEthereumTx ( suite . chainID , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , nil ) ,
2021-06-01 17:14:33 +00:00
ethtypes . FrontierSigner { } ,
2021-06-11 13:38:51 +00:00
func ( tx * MsgEthereumTx ) { tx . From = suite . from . Hex ( ) } ,
true ,
2021-06-01 17:14:33 +00:00
} ,
2021-05-12 13:08:31 +00:00
{
"no from address " ,
2021-06-11 13:38:51 +00:00
NewMsgEthereumTx ( suite . chainID , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , & types . AccessList { } ) ,
2021-06-01 17:14:33 +00:00
ethtypes . NewEIP2930Signer ( suite . chainID ) ,
2021-06-11 13:38:51 +00:00
func ( tx * MsgEthereumTx ) { tx . From = "" } ,
2021-05-12 13:08:31 +00:00
false ,
} ,
{
"from address ≠ signer address" ,
2021-06-11 13:38:51 +00:00
NewMsgEthereumTx ( suite . chainID , 0 , & suite . to , nil , 100000 , nil , [ ] byte ( "test" ) , & types . AccessList { } ) ,
2021-06-01 17:14:33 +00:00
ethtypes . NewEIP2930Signer ( suite . chainID ) ,
2021-06-11 13:38:51 +00:00
func ( tx * MsgEthereumTx ) { tx . From = suite . to . Hex ( ) } ,
2021-05-12 13:08:31 +00:00
false ,
} ,
}
for i , tc := range testCases {
2021-06-11 13:38:51 +00:00
tc . malleate ( tc . tx )
2021-06-01 17:14:33 +00:00
2021-06-11 13:38:51 +00:00
err := tc . tx . Sign ( tc . ethSigner , suite . signer )
2021-05-12 13:08:31 +00:00
if tc . expectPass {
suite . Require ( ) . NoError ( err , "valid test %d failed: %s" , i , tc . msg )
2021-06-11 13:38:51 +00:00
tx := tc . tx . AsTransaction ( )
2021-05-12 13:08:31 +00:00
2021-06-11 13:38:51 +00:00
sender , err := ethtypes . Sender ( tc . ethSigner , tx )
2021-05-12 13:08:31 +00:00
suite . Require ( ) . NoError ( err , tc . msg )
2021-06-11 13:38:51 +00:00
suite . Require ( ) . Equal ( tc . tx . From , sender . Hex ( ) , tc . msg )
2021-05-12 13:08:31 +00:00
} else {
suite . Require ( ) . Error ( err , "invalid test %d passed: %s" , i , tc . msg )
}
}
}