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

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jan 4, 2022
1 parent 133ae8f commit 42fb5e0
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 426 deletions.
4 changes: 2 additions & 2 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (suite AnteTestSuite) TestAnteHandler() {
suite.Require().NoError(acc.SetSequence(1))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(10000000000))
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt(10000000000))

suite.app.FeeMarketKeeper.SetBaseFee(suite.ctx, big.NewInt(100))

Expand Down Expand Up @@ -577,7 +577,7 @@ func (suite AnteTestSuite) TestAnteHandlerWithDynamicTxFee() {
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.ctx = suite.ctx.WithIsCheckTx(tc.checkTx).WithIsReCheckTx(tc.reCheckTx)
suite.app.EvmKeeper.AddBalance(addr, big.NewInt((ethparams.InitialBaseFee+10)*100000))
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000))
_, err := suite.anteHandler(suite.ctx, tc.txFn(), false)
if tc.expPass {
suite.Require().NoError(err)
Expand Down
52 changes: 26 additions & 26 deletions app/ante/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/tharsis/ethermint/app/ante"
"github.com/tharsis/ethermint/tests"
"github.com/tharsis/ethermint/x/evm/statedb"
evmtypes "github.com/tharsis/ethermint/x/evm/types"

ethtypes "github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -65,6 +66,8 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
tx := evmtypes.NewTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil)
tx.From = addr.Hex()

var vmdb *statedb.StateDB

testCases := []struct {
name string
tx sdk.Tx
Expand All @@ -86,7 +89,7 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
tx,
func() {
// set not as an EOA
suite.app.EvmKeeper.SetCode(addr, []byte("1"))
vmdb.SetCode(addr, []byte("1"))
},
true,
false,
Expand All @@ -96,7 +99,7 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
tx,
func() {
// reset back to EOA
suite.app.EvmKeeper.SetCode(addr, nil)
vmdb.SetCode(addr, nil)
},
true,
false,
Expand All @@ -105,7 +108,7 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
"success new account",
tx,
func() {
suite.app.EvmKeeper.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, big.NewInt(1000000))
},
true,
true,
Expand All @@ -117,7 +120,7 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, big.NewInt(1000000))
},
true,
true,
Expand All @@ -126,7 +129,10 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {

for _, tc := range testCases {
suite.Run(tc.name, func() {
vmdb = suite.StateDB()
tc.malleate()
suite.Require().NoError(vmdb.Commit())

_, err := dec.AnteHandle(suite.ctx.WithIsCheckTx(tc.checkTx), tc.tx, false, nextFn)

if tc.expPass {
Expand Down Expand Up @@ -204,6 +210,8 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
tx2 := evmtypes.NewTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 1000000, big.NewInt(1), nil, nil, nil, &ethtypes.AccessList{{Address: addr, StorageKeys: nil}})
tx2.From = addr.Hex()

var vmdb *statedb.StateDB

testCases := []struct {
name string
tx sdk.Tx
Expand All @@ -221,40 +229,28 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
{
"gas limit too low",
tx,
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
func() {},
false, false,
},
{
"not enough balance for fees",
tx2,
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
func() {},
false, false,
},
{
"not enough tx gas",
tx2,
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, big.NewInt(1000000))
},
false, true,
},
{
"not enough block gas",
tx2,
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, big.NewInt(1000000))

suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(1))
},
Expand All @@ -264,10 +260,7 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
"success",
tx2,
func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, big.NewInt(1000000))

suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(10000000000000000000))
},
Expand All @@ -277,7 +270,9 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {

for _, tc := range testCases {
suite.Run(tc.name, func() {
vmdb = suite.StateDB()
tc.malleate()
suite.Require().NoError(vmdb.Commit())

if tc.expPanic {
suite.Require().Panics(func() {
Expand Down Expand Up @@ -331,6 +326,8 @@ func (suite AnteTestSuite) TestCanTransferDecorator() {
err := tx.Sign(suite.ethSigner, tests.NewSigner(privKey))
suite.Require().NoError(err)

var vmdb *statedb.StateDB

testCases := []struct {
name string
tx sdk.Tx
Expand All @@ -355,15 +352,17 @@ func (suite AnteTestSuite) TestCanTransferDecorator() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, big.NewInt(1000000))
},
true,
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
vmdb = suite.StateDB()
tc.malleate()
suite.Require().NoError(vmdb.Commit())

_, err := dec.AnteHandle(suite.ctx.WithIsCheckTx(true), tc.tx, false, nextFn)

Expand Down Expand Up @@ -458,7 +457,8 @@ func (suite AnteTestSuite) TestEthIncrementSenderSequenceDecorator() {
txData, err := evmtypes.UnpackTxData(msg.Data)
suite.Require().NoError(err)

nonce := suite.app.EvmKeeper.GetNonce(addr)
nonce, err := suite.app.EvmKeeper.GetNonce(suite.ctx, addr)
suite.Require().NoError(err)
suite.Require().Equal(txData.GetNonce()+1, nonce)
} else {
suite.Require().Error(err)
Expand Down
9 changes: 5 additions & 4 deletions app/ante/sigs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"

"github.com/tharsis/ethermint/tests"
"github.com/tharsis/ethermint/x/evm/statedb"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
)

Expand All @@ -14,11 +15,11 @@ func (suite AnteTestSuite) TestSignatures() {
addr, privKey := tests.NewAddrKey()
to := tests.GenerateAddress()

acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.Require().NoError(acc.SetSequence(1))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
acc := statedb.NewEmptyAccount()
acc.Nonce = 1
acc.Balance = big.NewInt(10000000000)

suite.app.EvmKeeper.AddBalance(addr, big.NewInt(10000000000))
suite.app.EvmKeeper.SetAccount(suite.ctx, addr, *acc)
msgEthereumTx := evmtypes.NewTx(suite.app.EvmKeeper.ChainID(), 1, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil, nil, nil)
msgEthereumTx.From = addr.Hex()

Expand Down
6 changes: 6 additions & 0 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"

"github.com/stretchr/testify/suite"
Expand All @@ -25,6 +26,7 @@ import (
ante "github.com/tharsis/ethermint/app/ante"
"github.com/tharsis/ethermint/encoding"
"github.com/tharsis/ethermint/tests"
"github.com/tharsis/ethermint/x/evm/statedb"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types"

Expand All @@ -43,6 +45,10 @@ type AnteTestSuite struct {
enableLondonHF bool
}

func (suite *AnteTestSuite) StateDB() *statedb.StateDB {
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes())))
}

func (suite *AnteTestSuite) SetupTest() {
checkTx := false

Expand Down
33 changes: 17 additions & 16 deletions tests/importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

evmkeeper "github.com/tharsis/ethermint/x/evm/keeper"
"github.com/tharsis/ethermint/x/evm/statedb"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
Expand Down Expand Up @@ -139,23 +140,23 @@ func (suite *ImporterTestSuite) TestImportBlocks() {
})
ctx := suite.app.NewContext(false, tmheader)
ctx = ctx.WithBlockHeight(tmheader.Height)
suite.app.EvmKeeper.WithContext(ctx)
vmdb := statedb.New(ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes())))

if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
applyDAOHardFork(suite.app.EvmKeeper)
applyDAOHardFork(vmdb)
}

for _, tx := range block.Transactions() {

receipt, gas, err := applyTransaction(
chainConfig, chainContext, nil, gp, suite.app.EvmKeeper, header, tx, usedGas, vmConfig,
ctx, chainConfig, chainContext, nil, gp, suite.app.EvmKeeper, vmdb, header, tx, usedGas, vmConfig,
)
suite.Require().NoError(err, "failed to apply tx at block %d; tx: %X; gas %d; receipt:%v", block.NumberU64(), tx.Hash(), gas, receipt)
suite.Require().NotNil(receipt)
}

// apply mining rewards
accumulateRewards(chainConfig, suite.app.EvmKeeper, header, block.Uncles())
accumulateRewards(chainConfig, vmdb, header, block.Uncles())

// simulate BaseApp EndBlocker commitment
endBR := types.RequestEndBlock{Height: tmheader.Height}
Expand All @@ -173,7 +174,7 @@ func (suite *ImporterTestSuite) TestImportBlocks() {
// reward. The total reward consists of the static block reward and rewards for
// included uncles. The coinbase of each uncle block is also rewarded.
func accumulateRewards(
config *ethparams.ChainConfig, evmKeeper *evmkeeper.Keeper,
config *ethparams.ChainConfig, vmdb ethvm.StateDB,
header *ethtypes.Header, uncles []*ethtypes.Header,
) {
// select the correct block reward based on chain progression
Expand All @@ -191,12 +192,12 @@ func accumulateRewards(
r.Sub(r, header.Number)
r.Mul(r, blockReward)
r.Div(r, rewardBig8)
evmKeeper.AddBalance(uncle.Coinbase, r)
vmdb.AddBalance(uncle.Coinbase, r)
r.Div(blockReward, rewardBig32)
reward.Add(reward, r)
}

evmKeeper.AddBalance(header.Coinbase, reward)
vmdb.AddBalance(header.Coinbase, reward)
}

// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
Expand All @@ -205,15 +206,15 @@ func accumulateRewards(
// Code is pulled from go-ethereum 1.9 because the StateDB interface does not include the
// SetBalance function implementation
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/consensus/misc/dao.go#L74
func applyDAOHardFork(evmKeeper *evmkeeper.Keeper) {
func applyDAOHardFork(vmdb ethvm.StateDB) {
// Retrieve the contract to refund balances into
if !evmKeeper.Exist(ethparams.DAORefundContract) {
evmKeeper.CreateAccount(ethparams.DAORefundContract)
if !vmdb.Exist(ethparams.DAORefundContract) {
vmdb.CreateAccount(ethparams.DAORefundContract)
}

// Move every DAO account and extra-balance account funds into the refund contract
for _, addr := range ethparams.DAODrainList() {
evmKeeper.AddBalance(ethparams.DAORefundContract, evmKeeper.GetBalance(addr))
vmdb.AddBalance(ethparams.DAORefundContract, vmdb.GetBalance(addr))
}
}

Expand All @@ -224,8 +225,8 @@ func applyDAOHardFork(evmKeeper *evmkeeper.Keeper) {
// Function is also pulled from go-ethereum 1.9 because of the incompatible usage
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/core/state_processor.go#L88
func applyTransaction(
config *ethparams.ChainConfig, bc ethcore.ChainContext, author *common.Address,
gp *ethcore.GasPool, evmKeeper *evmkeeper.Keeper, header *ethtypes.Header,
ctx sdk.Context, config *ethparams.ChainConfig, bc ethcore.ChainContext, author *common.Address,
gp *ethcore.GasPool, evmKeeper *evmkeeper.Keeper, vmdb ethvm.StateDB, header *ethtypes.Header,
tx *ethtypes.Transaction, usedGas *uint64, cfg ethvm.Config,
) (*ethtypes.Receipt, uint64, error) {
msg, err := tx.AsMessage(ethtypes.MakeSigner(config, header.Number), sdk.ZeroInt().BigInt())
Expand All @@ -239,7 +240,7 @@ func applyTransaction(

// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmenv := ethvm.NewEVM(blockCtx, txCtx, evmKeeper, config, cfg)
vmenv := ethvm.NewEVM(blockCtx, txCtx, vmdb, config, cfg)

// Apply the transaction to the current state (included in the env)
execResult, err := ethcore.ApplyMessage(vmenv, msg, gp)
Expand All @@ -263,11 +264,11 @@ func applyTransaction(
}

// Set the receipt logs and create a bloom for filtering
receipt.Logs = evmKeeper.GetTxLogsTransient(tx.Hash())
receipt.Logs = nil
receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})
receipt.BlockHash = header.Hash()
receipt.BlockNumber = header.Number
receipt.TransactionIndex = uint(evmKeeper.GetTxIndexTransient())
receipt.TransactionIndex = uint(evmKeeper.GetTxIndexTransient(ctx))

return receipt, execResult.UsedGas, err
}
Loading

0 comments on commit 42fb5e0

Please sign in to comment.