Sync from fork #74

Merged
0xmuralik merged 232 commits from murali/update-fork into main 2023-01-10 04:50:57 +00:00
2 changed files with 506 additions and 73 deletions
Showing only changes of commit 4d63238ab0 - Show all commits

View File

@ -0,0 +1,126 @@
package types
import (
"math/big"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
func (suite *TxDataTestSuite) TestAccessListTxCopy() {
tx := &AccessListTx{}
txCopy := tx.Copy()
suite.Require().Equal(&AccessListTx{}, txCopy)
}
func (suite *TxDataTestSuite) TestAccessListTxGetGasTipCap() {
testCases := []struct {
name string
tx AccessListTx
exp *big.Int
}{
{
"non-empty gasPrice",
AccessListTx{
GasPrice: &suite.sdkInt,
},
(&suite.sdkInt).BigInt(),
},
}
for _, tc := range testCases {
actual := tc.tx.GetGasTipCap()
suite.Require().Equal(tc.exp, actual, tc.name)
}
}
func (suite *TxDataTestSuite) TestAccessListTxGetGasFeeCap() {
testCases := []struct {
name string
tx AccessListTx
exp *big.Int
}{
{
"non-empty gasPrice",
AccessListTx{
GasPrice: &suite.sdkInt,
},
(&suite.sdkInt).BigInt(),
},
}
for _, tc := range testCases {
actual := tc.tx.GetGasFeeCap()
suite.Require().Equal(tc.exp, actual, tc.name)
}
}
func (suite *TxDataTestSuite) TestAccessListTxCost() {
testCases := []struct {
name string
tx AccessListTx
exp *big.Int
}{
{
"non-empty access list tx",
AccessListTx{
GasPrice: &suite.sdkInt,
GasLimit: uint64(1),
Amount: &suite.sdkZeroInt,
},
(&suite.sdkInt).BigInt(),
},
}
for _, tc := range testCases {
actual := tc.tx.Cost()
suite.Require().Equal(tc.exp, actual, tc.name)
}
}
func (suite *TxDataTestSuite) TestAccessListTxEffectiveCost() {
testCases := []struct {
name string
tx AccessListTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty access list tx",
AccessListTx{
GasPrice: &suite.sdkInt,
GasLimit: uint64(1),
Amount: &suite.sdkZeroInt,
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
},
}
for _, tc := range testCases {
actual := tc.tx.EffectiveCost(tc.baseFee)
suite.Require().Equal(tc.exp, actual, tc.name)
}
}
func (suite *TxDataTestSuite) TestAccessListTxType() {
testCases := []struct {
name string
tx AccessListTx
}{
{
"non-empty access list tx",
AccessListTx{},
},
}
for _, tc := range testCases {
actual := tc.tx.TxType()
suite.Require().Equal(uint8(ethtypes.AccessListTxType), actual, tc.name)
}
}

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -28,10 +29,11 @@ const invalidFromAddress = "0x0000"
type MsgsTestSuite struct { type MsgsTestSuite struct {
suite.Suite suite.Suite
signer keyring.Signer signer keyring.Signer
from common.Address from common.Address
to common.Address to common.Address
chainID *big.Int chainID *big.Int
hundredBigInt *big.Int
clientCtx client.Context clientCtx client.Context
} }
@ -47,6 +49,7 @@ func (suite *MsgsTestSuite) SetupTest() {
suite.from = from suite.from = from
suite.to = tests.GenerateAddress() suite.to = tests.GenerateAddress()
suite.chainID = big.NewInt(1) suite.chainID = big.NewInt(1)
suite.hundredBigInt = big.NewInt(100)
encodingConfig := encoding.MakeConfig(app.ModuleBasics) encodingConfig := encoding.MakeConfig(app.ModuleBasics)
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
@ -70,73 +73,273 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_Constructor() {
} }
func (suite *MsgsTestSuite) TestMsgEthereumTx_BuildTx() { func (suite *MsgsTestSuite) TestMsgEthereumTx_BuildTx() {
msg := types.NewTx(nil, 0, &suite.to, nil, 100000, big.NewInt(1), big.NewInt(1), big.NewInt(0), []byte("test"), nil) testCases := []struct {
name string
msg *types.MsgEthereumTx
expError bool
}{
{
"build tx - pass",
types.NewTx(nil, 0, &suite.to, nil, 100000, big.NewInt(1), big.NewInt(1), big.NewInt(0), []byte("test"), nil),
false,
},
{
"build tx - fail: nil data",
types.NewTx(nil, 0, &suite.to, nil, 100000, big.NewInt(1), big.NewInt(1), big.NewInt(0), []byte("test"), nil),
true,
},
}
err := msg.ValidateBasic() for _, tc := range testCases {
suite.Require().NoError(err) if strings.Contains(tc.name, "nil data") {
tc.msg.Data = nil
}
tx, err := msg.BuildTx(suite.clientCtx.TxConfig.NewTxBuilder(), "aphoton") tx, err := tc.msg.BuildTx(suite.clientCtx.TxConfig.NewTxBuilder(), "aphoton")
suite.Require().NoError(err) if tc.expError {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
suite.Require().Empty(tx.GetMemo()) suite.Require().Empty(tx.GetMemo())
suite.Require().Empty(tx.GetTimeoutHeight()) suite.Require().Empty(tx.GetTimeoutHeight())
suite.Require().Equal(uint64(100000), tx.GetGas()) suite.Require().Equal(uint64(100000), tx.GetGas())
suite.Require().Equal(sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(100000))), tx.GetFee()) suite.Require().Equal(sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(100000))), tx.GetFee())
}
}
} }
func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() { func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() {
hundredInt := sdk.NewInt(100) hundredInt := big.NewInt(100)
zeroInt := sdk.ZeroInt() zeroInt := big.NewInt(0)
minusOneInt := sdk.NewInt(-1) minusOneInt := big.NewInt(-1)
exp_2_255 := sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil)) exp_2_255 := new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil)
testCases := []struct { testCases := []struct {
msg string msg string
to string to string
amount *sdk.Int amount *big.Int
gasPrice *sdk.Int gasPrice *big.Int
gasFeeCap *big.Int
gasTipCap *big.Int
from string from string
accessList *ethtypes.AccessList accessList *ethtypes.AccessList
chainID *sdk.Int chainID *big.Int
expectPass bool expectPass bool
}{ }{
{msg: "pass with recipient - Legacy Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &hundredInt, expectPass: true}, {
{msg: "pass with recipient - AccessList Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &zeroInt, accessList: &ethtypes.AccessList{}, chainID: &hundredInt, expectPass: true}, msg: "pass with recipient - Legacy Tx",
{msg: "pass contract - Legacy Tx", to: "", amount: &hundredInt, gasPrice: &hundredInt, expectPass: true}, to: suite.to.Hex(),
// {msg: "invalid recipient", to: invalidFromAddress, amount: &minusOneInt, gasPrice: &hundredInt, expectPass: false}, amount: hundredInt,
{msg: "nil amount - Legacy Tx", to: suite.to.Hex(), amount: nil, gasPrice: &hundredInt, expectPass: true}, gasPrice: hundredInt,
{msg: "negative amount - Legacy Tx", to: suite.to.Hex(), amount: &minusOneInt, gasPrice: &hundredInt, expectPass: false}, gasFeeCap: nil,
{msg: "nil gas price - Legacy Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: nil, expectPass: false}, gasTipCap: nil,
{msg: "negative gas price - Legacy Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &minusOneInt, expectPass: false}, expectPass: true,
{msg: "zero gas price - Legacy Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &zeroInt, expectPass: true}, },
{msg: "invalid from address - Legacy Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &zeroInt, from: invalidFromAddress, expectPass: false}, {
{msg: "out of bound gas fee - Legacy Tx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &exp_2_255, expectPass: false}, msg: "pass with recipient - AccessList Tx",
{msg: "nil amount - AccessListTx", to: suite.to.Hex(), amount: nil, gasPrice: &hundredInt, accessList: &ethtypes.AccessList{}, chainID: &hundredInt, expectPass: true}, to: suite.to.Hex(),
{msg: "negative amount - AccessListTx", to: suite.to.Hex(), amount: &minusOneInt, gasPrice: &hundredInt, accessList: &ethtypes.AccessList{}, chainID: nil, expectPass: false}, amount: hundredInt,
{msg: "nil gas price - AccessListTx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: nil, accessList: &ethtypes.AccessList{}, chainID: &hundredInt, expectPass: false}, gasPrice: zeroInt,
{msg: "negative gas price - AccessListTx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &minusOneInt, accessList: &ethtypes.AccessList{}, chainID: nil, expectPass: false}, gasFeeCap: nil,
{msg: "zero gas price - AccessListTx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &zeroInt, accessList: &ethtypes.AccessList{}, chainID: &hundredInt, expectPass: true}, gasTipCap: nil,
{msg: "invalid from address - AccessListTx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &zeroInt, from: invalidFromAddress, accessList: &ethtypes.AccessList{}, chainID: &hundredInt, expectPass: false}, accessList: &ethtypes.AccessList{},
{msg: "chain ID not set on AccessListTx", to: suite.to.Hex(), amount: &hundredInt, gasPrice: &zeroInt, accessList: &ethtypes.AccessList{}, chainID: nil, expectPass: false}, chainID: hundredInt,
expectPass: true,
},
{
msg: "pass with recipient - DynamicFee Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: hundredInt,
gasTipCap: zeroInt,
accessList: &ethtypes.AccessList{},
chainID: hundredInt,
expectPass: true,
},
{
msg: "pass contract - Legacy Tx",
to: "",
amount: hundredInt,
gasPrice: hundredInt,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: true,
},
{
msg: "invalid recipient",
to: invalidFromAddress,
amount: minusOneInt,
gasPrice: hundredInt,
expectPass: false,
},
{
msg: "nil amount - Legacy Tx",
to: suite.to.Hex(),
amount: nil,
gasPrice: hundredInt,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: true,
},
{
msg: "negative amount - Legacy Tx",
to: suite.to.Hex(),
amount: minusOneInt,
gasPrice: hundredInt,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: false,
},
{
msg: "nil gas price - Legacy Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: nil,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: false,
},
{
msg: "negative gas price - Legacy Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: minusOneInt,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: false,
},
{
msg: "zero gas price - Legacy Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: true,
},
{
msg: "invalid from address - Legacy Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: nil,
gasTipCap: nil,
from: invalidFromAddress,
expectPass: false,
},
{
msg: "out of bound gas fee - Legacy Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: exp_2_255,
gasFeeCap: nil,
gasTipCap: nil,
expectPass: false,
},
{
msg: "nil amount - AccessListTx",
to: suite.to.Hex(),
amount: nil,
gasPrice: hundredInt,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: hundredInt,
expectPass: true,
},
{
msg: "negative amount - AccessListTx",
to: suite.to.Hex(),
amount: minusOneInt,
gasPrice: hundredInt,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: nil,
expectPass: false,
},
{
msg: "nil gas price - AccessListTx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: nil,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: hundredInt,
expectPass: false,
},
{
msg: "negative gas price - AccessListTx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: minusOneInt,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: nil,
expectPass: false,
},
{
msg: "zero gas price - AccessListTx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: hundredInt,
expectPass: true,
},
{
msg: "invalid from address - AccessListTx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: nil,
gasTipCap: nil,
from: invalidFromAddress,
accessList: &ethtypes.AccessList{},
chainID: hundredInt,
expectPass: false,
},
{
msg: "chain ID not set on AccessListTx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: nil,
expectPass: false,
},
{
msg: "nil tx.Data - AccessList Tx",
to: suite.to.Hex(),
amount: hundredInt,
gasPrice: zeroInt,
gasFeeCap: nil,
gasTipCap: nil,
accessList: &ethtypes.AccessList{},
chainID: hundredInt,
expectPass: false,
},
} }
for i, tc := range testCases { for i, tc := range testCases {
to := common.HexToAddress(tc.from) to := common.HexToAddress(tc.from)
var chainID, amount, gasPrice *big.Int tx := types.NewTx(tc.chainID, 1, &to, tc.amount, 1000, tc.gasPrice, tc.gasFeeCap, tc.gasTipCap, nil, tc.accessList)
if tc.chainID != nil {
chainID = tc.chainID.BigInt()
}
if tc.amount != nil {
amount = tc.amount.BigInt()
}
if tc.gasPrice != nil {
gasPrice = tc.gasPrice.BigInt()
}
tx := types.NewTx(chainID, 1, &to, amount, 1000, gasPrice, nil, nil, nil, tc.accessList)
tx.From = tc.from tx.From = tc.from
// apply nil assignment here to test ValidateBasic function instead of NewTx
if strings.Contains(tc.msg, "nil tx.Data") {
tx.Data = nil
}
err := tx.ValidateBasic() err := tx.ValidateBasic()
if tc.expectPass { if tc.expectPass {
@ -215,6 +418,69 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_Sign() {
} }
} }
func (suite *MsgsTestSuite) TestMsgEthereumTx_Getters() {
testCases := []struct {
name string
tx *types.MsgEthereumTx
ethSigner ethtypes.Signer
exp *big.Int
}{
{
"get fee - pass",
types.NewTx(suite.chainID, 0, &suite.to, nil, 50, suite.hundredBigInt, nil, nil, nil, &ethtypes.AccessList{}),
ethtypes.NewEIP2930Signer(suite.chainID),
big.NewInt(5000),
},
{
"get fee - fail: nil data",
types.NewTx(suite.chainID, 0, &suite.to, nil, 50, suite.hundredBigInt, nil, nil, nil, &ethtypes.AccessList{}),
ethtypes.NewEIP2930Signer(suite.chainID),
nil,
},
{
"get effective fee - pass",
types.NewTx(suite.chainID, 0, &suite.to, nil, 50, suite.hundredBigInt, nil, nil, nil, &ethtypes.AccessList{}),
ethtypes.NewEIP2930Signer(suite.chainID),
big.NewInt(5000),
},
{
"get effective fee - fail: nil data",
types.NewTx(suite.chainID, 0, &suite.to, nil, 50, suite.hundredBigInt, nil, nil, nil, &ethtypes.AccessList{}),
ethtypes.NewEIP2930Signer(suite.chainID),
nil,
},
{
"get gas - pass",
types.NewTx(suite.chainID, 0, &suite.to, nil, 50, suite.hundredBigInt, nil, nil, nil, &ethtypes.AccessList{}),
ethtypes.NewEIP2930Signer(suite.chainID),
big.NewInt(50),
},
{
"get gas - fail: nil data",
types.NewTx(suite.chainID, 0, &suite.to, nil, 50, suite.hundredBigInt, nil, nil, nil, &ethtypes.AccessList{}),
ethtypes.NewEIP2930Signer(suite.chainID),
big.NewInt(0),
},
}
var fee, effFee *big.Int
for _, tc := range testCases {
if strings.Contains(tc.name, "nil data") {
tc.tx.Data = nil
}
if strings.Contains(tc.name, "get fee") {
fee = tc.tx.GetFee()
suite.Require().Equal(tc.exp, fee)
} else if strings.Contains(tc.name, "get effective fee") {
effFee = tc.tx.GetEffectiveFee(big.NewInt(0))
suite.Require().Equal(tc.exp, effFee)
} else if strings.Contains(tc.name, "get gas") {
gas := tc.tx.GetGas()
suite.Require().Equal(tc.exp.Uint64(), gas)
}
}
}
func (suite *MsgsTestSuite) TestFromEthereumTx() { func (suite *MsgsTestSuite) TestFromEthereumTx() {
privkey, _ := ethsecp256k1.GenerateKey() privkey, _ := ethsecp256k1.GenerateKey()
ethPriv, err := privkey.ToECDSA() ethPriv, err := privkey.ToECDSA()
@ -229,37 +495,78 @@ func (suite *MsgsTestSuite) TestFromEthereumTx() {
buildTx func() *ethtypes.Transaction buildTx func() *ethtypes.Transaction
}{ }{
{"success, normal tx", true, func() *ethtypes.Transaction { {"success, normal tx", true, func() *ethtypes.Transaction {
tx := ethtypes.NewTransaction( tx := ethtypes.NewTx(&ethtypes.AccessListTx{
0, Nonce: 0,
common.BigToAddress(big.NewInt(1)), Data: nil,
big.NewInt(10), To: &suite.to,
21000, big.NewInt(0), Value: big.NewInt(10),
nil, GasPrice: big.NewInt(1),
) Gas: 21000,
})
tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv) tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv)
suite.Require().NoError(err) suite.Require().NoError(err)
return tx return tx
}}, }},
{"fail, value bigger than 256bits", false, func() *ethtypes.Transaction { {"success, DynamicFeeTx", true, func() *ethtypes.Transaction {
tx := ethtypes.NewTransaction( tx := ethtypes.NewTx(&ethtypes.DynamicFeeTx{
0, Nonce: 0,
common.BigToAddress(big.NewInt(1)), Data: nil,
exp_10_80, To: &suite.to,
21000, big.NewInt(0), Value: big.NewInt(10),
nil, Gas: 21000,
) })
tx, err := ethtypes.SignTx(tx, ethtypes.NewLondonSigner(suite.chainID), ethPriv)
suite.Require().NoError(err)
return tx
}},
{"fail, value bigger than 256bits - AccessListTx", false, func() *ethtypes.Transaction {
tx := ethtypes.NewTx(&ethtypes.AccessListTx{
Nonce: 0,
Data: nil,
To: &suite.to,
Value: exp_10_80,
GasPrice: big.NewInt(1),
Gas: 21000,
})
tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv) tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv)
suite.Require().NoError(err) suite.Require().NoError(err)
return tx return tx
}}, }},
{"fail, gas price bigger than 256bits", false, func() *ethtypes.Transaction { {"fail, gas price bigger than 256bits - AccessListTx", false, func() *ethtypes.Transaction {
tx := ethtypes.NewTransaction( tx := ethtypes.NewTx(&ethtypes.AccessListTx{
0, Nonce: 0,
common.BigToAddress(big.NewInt(1)), Data: nil,
big.NewInt(10), To: &suite.to,
21000, exp_10_80, Value: big.NewInt(1),
nil, GasPrice: exp_10_80,
) Gas: 21000,
})
tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv)
suite.Require().NoError(err)
return tx
}},
{"fail, value bigger than 256bits - LegacyTx", false, func() *ethtypes.Transaction {
tx := ethtypes.NewTx(&ethtypes.LegacyTx{
Nonce: 0,
Data: nil,
To: &suite.to,
Value: exp_10_80,
GasPrice: big.NewInt(1),
Gas: 21000,
})
tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv)
suite.Require().NoError(err)
return tx
}},
{"fail, gas price bigger than 256bits - LegacyTx", false, func() *ethtypes.Transaction {
tx := ethtypes.NewTx(&ethtypes.LegacyTx{
Nonce: 0,
Data: nil,
To: &suite.to,
Value: big.NewInt(1),
GasPrice: exp_10_80,
Gas: 21000,
})
tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv) tx, err := ethtypes.SignTx(tx, ethtypes.NewEIP2930Signer(suite.chainID), ethPriv)
suite.Require().NoError(err) suite.Require().NoError(err)
return tx return tx