Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
tests: improve x/evm/types coverage (#1302)
Browse files Browse the repository at this point in the history
* modifying x/evm/types tests v1

* modifying x/evm/types tests v2

* modifying x/evm/types test v3

Co-authored-by: Freddy Caceres <facs95@gmail.com>
  • Loading branch information
adisaran64 and facs95 committed Sep 1, 2022
1 parent ca070e2 commit 3e4544d
Show file tree
Hide file tree
Showing 8 changed files with 632 additions and 2 deletions.
41 changes: 41 additions & 0 deletions x/evm/types/access_list_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ func (suite *TxDataTestSuite) TestAccessListTxGetGasFeeCap() {
}
}

func (suite *TxDataTestSuite) TestEmptyAccessList() {
testCases := []struct {
name string
tx AccessListTx
}{
{
"empty access list tx",
AccessListTx{
Accesses: nil,
},
},
}
for _, tc := range testCases {
actual := tc.tx.GetAccessList()

suite.Require().Nil(actual, tc.name)
}
}

func (suite *TxDataTestSuite) TestAccessListTxCost() {
testCases := []struct {
name string
Expand All @@ -81,6 +100,28 @@ func (suite *TxDataTestSuite) TestAccessListTxCost() {
}
}

func (suite *TxDataTestSuite) TestAccessListEffectiveGasPrice() {
testCases := []struct {
name string
tx AccessListTx
baseFee *big.Int
}{
{
"non-empty access list tx",
AccessListTx{
GasPrice: &suite.sdkInt,
},
(&suite.sdkInt).BigInt(),
},
}

for _, tc := range testCases {
actual := tc.tx.EffectiveGasPrice(tc.baseFee)

suite.Require().Equal(tc.tx.GetGasPrice(), actual, tc.name)
}
}

func (suite *TxDataTestSuite) TestAccessListTxEffectiveCost() {
testCases := []struct {
name string
Expand Down
63 changes: 63 additions & 0 deletions x/evm/types/chain_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,69 @@ func TestChainConfigValidate(t *testing.T) {
},
true,
},
{
"invalid ArrowGlacierBlock",
ChainConfig{
HomesteadBlock: newIntPtr(0),
DAOForkBlock: newIntPtr(0),
EIP150Block: newIntPtr(0),
EIP150Hash: defaultEIP150Hash,
EIP155Block: newIntPtr(0),
EIP158Block: newIntPtr(0),
ByzantiumBlock: newIntPtr(0),
ConstantinopleBlock: newIntPtr(0),
PetersburgBlock: newIntPtr(0),
IstanbulBlock: newIntPtr(0),
MuirGlacierBlock: newIntPtr(0),
BerlinBlock: newIntPtr(0),
LondonBlock: newIntPtr(0),
ArrowGlacierBlock: newIntPtr(-1),
},
true,
},
{
"invalid GrayGlacierBlock",
ChainConfig{
HomesteadBlock: newIntPtr(0),
DAOForkBlock: newIntPtr(0),
EIP150Block: newIntPtr(0),
EIP150Hash: defaultEIP150Hash,
EIP155Block: newIntPtr(0),
EIP158Block: newIntPtr(0),
ByzantiumBlock: newIntPtr(0),
ConstantinopleBlock: newIntPtr(0),
PetersburgBlock: newIntPtr(0),
IstanbulBlock: newIntPtr(0),
MuirGlacierBlock: newIntPtr(0),
BerlinBlock: newIntPtr(0),
LondonBlock: newIntPtr(0),
ArrowGlacierBlock: newIntPtr(0),
GrayGlacierBlock: newIntPtr(-1),
},
true,
},
{
"invalid MergeNetsplitBlock",
ChainConfig{
HomesteadBlock: newIntPtr(0),
DAOForkBlock: newIntPtr(0),
EIP150Block: newIntPtr(0),
EIP150Hash: defaultEIP150Hash,
EIP155Block: newIntPtr(0),
EIP158Block: newIntPtr(0),
ByzantiumBlock: newIntPtr(0),
ConstantinopleBlock: newIntPtr(0),
PetersburgBlock: newIntPtr(0),
IstanbulBlock: newIntPtr(0),
MuirGlacierBlock: newIntPtr(0),
BerlinBlock: newIntPtr(0),
LondonBlock: newIntPtr(0),
ArrowGlacierBlock: newIntPtr(0),
GrayGlacierBlock: newIntPtr(0),
MergeNetsplitBlock: newIntPtr(-1),
},
true,
},
{
"invalid fork order - skip HomesteadBlock",
ChainConfig{
Expand Down
87 changes: 87 additions & 0 deletions x/evm/types/dynamic_fee_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/tests"
"github.com/stretchr/testify/suite"
Expand All @@ -18,25 +19,33 @@ type TxDataTestSuite struct {

sdkInt sdkmath.Int
uint64 uint64
hexUint64 hexutil.Uint64
bigInt *big.Int
hexBigInt hexutil.Big
overflowBigInt *big.Int
sdkZeroInt sdkmath.Int
sdkMinusOneInt sdkmath.Int
invalidAddr string
addr common.Address
hexAddr string
hexDataBytes hexutil.Bytes
hexInputBytes hexutil.Bytes
}

func (suite *TxDataTestSuite) SetupTest() {
suite.sdkInt = sdkmath.NewInt(100)
suite.uint64 = suite.sdkInt.Uint64()
suite.hexUint64 = hexutil.Uint64(100)
suite.bigInt = big.NewInt(1)
suite.hexBigInt = hexutil.Big(*big.NewInt(1))
suite.overflowBigInt = big.NewInt(0).Exp(big.NewInt(10), big.NewInt(256), nil)
suite.sdkZeroInt = sdk.ZeroInt()
suite.sdkMinusOneInt = sdkmath.NewInt(-1)
suite.invalidAddr = "123456"
suite.addr = tests.GenerateAddress()
suite.hexAddr = suite.addr.Hex()
suite.hexDataBytes = hexutil.Bytes([]byte("data"))
suite.hexInputBytes = hexutil.Bytes([]byte("input"))
}

func TestTxDataTestSuite(t *testing.T) {
Expand Down Expand Up @@ -581,6 +590,84 @@ func (suite *TxDataTestSuite) TestDynamicFeeTxValidate() {
}
}

func (suite *TxDataTestSuite) TestDynamicFeeTxEffectiveGasPrice() {
testCases := []struct {
name string
tx DynamicFeeTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty dynamic fee tx",
DynamicFeeTx{
GasTipCap: &suite.sdkInt,
GasFeeCap: &suite.sdkInt,
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
},
}

for _, tc := range testCases {
actual := tc.tx.EffectiveGasPrice(tc.baseFee)

suite.Require().Equal(tc.exp, actual, tc.name)
}
}

func (suite *TxDataTestSuite) TestDynamicFeeTxEffectiveFee() {
testCases := []struct {
name string
tx DynamicFeeTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty dynamic fee tx",
DynamicFeeTx{
GasTipCap: &suite.sdkInt,
GasFeeCap: &suite.sdkInt,
GasLimit: uint64(1),
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
},
}

for _, tc := range testCases {
actual := tc.tx.EffectiveFee(tc.baseFee)

suite.Require().Equal(tc.exp, actual, tc.name)
}
}

func (suite *TxDataTestSuite) TestDynamicFeeTxEffectiveCost() {
testCases := []struct {
name string
tx DynamicFeeTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty dynamic fee tx",
DynamicFeeTx{
GasTipCap: &suite.sdkInt,
GasFeeCap: &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) TestDynamicFeeTxFeeCost() {
tx := &DynamicFeeTx{}
suite.Require().Panics(func() { tx.Fee() }, "should panic")
Expand Down
5 changes: 5 additions & 0 deletions x/evm/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (suite *GenesisTestSuite) TestValidateGenesis() {
genState: &GenesisState{},
expPass: false,
},
{
name: "copied genesis",
genState: NewGenesisState(DefaultGenesisState().Params, DefaultGenesisState().Accounts),
expPass: true,
},
{
name: "invalid genesis",
genState: &GenesisState{
Expand Down
79 changes: 77 additions & 2 deletions x/evm/types/legacy_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,84 @@ func (suite *TxDataTestSuite) TestLegacyTxValidate() {
}
}

func (suite *TxDataTestSuite) TestLegacyTxEffectiveGasPrice() {
testCases := []struct {
name string
tx LegacyTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty legacy tx",
LegacyTx{
GasPrice: &suite.sdkInt,
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
},
}

for _, tc := range testCases {
actual := tc.tx.EffectiveGasPrice(tc.baseFee)

suite.Require().Equal(tc.exp, actual, tc.name)
}
}

func (suite *TxDataTestSuite) TestLegacyTxEffectiveFee() {
testCases := []struct {
name string
tx LegacyTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty legacy tx",
LegacyTx{
GasPrice: &suite.sdkInt,
GasLimit: uint64(1),
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
},
}

for _, tc := range testCases {
actual := tc.tx.EffectiveFee(tc.baseFee)

suite.Require().Equal(tc.exp, actual, tc.name)
}
}

func (suite *TxDataTestSuite) TestLegacyTxEffectiveCost() {
testCases := []struct {
name string
tx LegacyTx
baseFee *big.Int
exp *big.Int
}{
{
"non-empty legacy tx",
LegacyTx{
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) TestLegacyTxFeeCost() {
tx := &LegacyTx{}

suite.Require().Panics(func() { tx.Fee() }, "should panice")
suite.Require().Panics(func() { tx.Cost() }, "should panice")
suite.Require().Panics(func() { tx.Fee() }, "should panic")
suite.Require().Panics(func() { tx.Cost() }, "should panic")
}
Loading

0 comments on commit 3e4544d

Please sign in to comment.