Skip to content

Commit

Permalink
test: fix tests(finalize block, commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Dec 16, 2023
1 parent 88f9af4 commit 726aa6f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
7 changes: 7 additions & 0 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

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

signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -179,6 +180,12 @@ func (suite *AnteTestSuite) SetupTest() {
suite.ctx, err = testutil.Commit(suite.ctx, suite.app, time.Second*0, nil)
suite.Require().NoError(err)

// The BeginBlock, DeliverTx, and EndBlock logic has been combined into one FinalizeBlock logic.
// Compared to the old logic, the BaseFee has changed because testutil.commit() returns a
// ctx in the state in which BeginBlock was called, so we force it to be set to the
// initial state and proceed with the test.
suite.app.FeeMarketKeeper.SetBaseFee(suite.ctx, sdkmath.NewInt(ethparams.InitialBaseFee).BigInt())

legacytx.RegressionTestingAminoCodec = encodingConfig.Amino
}

Expand Down
21 changes: 0 additions & 21 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

sdkmath "cosmossdk.io/math"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -578,8 +577,6 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
"erc20 transfer",
func() {
contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()
transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
suite.Require().NoError(err)
Expand Down Expand Up @@ -655,8 +652,6 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
"erc20 transfer w/ enableFeemarket",
func() {
contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()
transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
suite.Require().NoError(err)
Expand Down Expand Up @@ -838,14 +833,10 @@ func (suite *KeeperTestSuite) TestTraceTx() {
suite.Require().NoError(vmdb.Commit())

contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()
// Generate token transfer transaction
firstTx := suite.TransferERC20Token(suite.T(), contractAddr, suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), sdkmath.NewIntWithDecimal(1, 18).BigInt())
txMsg = suite.TransferERC20Token(suite.T(), contractAddr, suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), sdkmath.NewIntWithDecimal(1, 18).BigInt())
_, err = suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)

predecessors = append(predecessors, firstTx)
},
Expand Down Expand Up @@ -941,13 +932,9 @@ func (suite *KeeperTestSuite) TestTraceTx() {
suite.SetupTest()
// Deploy contract
contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()
// Generate token transfer transaction
txMsg = suite.TransferERC20Token(suite.T(), contractAddr, suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), sdkmath.NewIntWithDecimal(1, 18).BigInt())
_, err = suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()

tc.malleate()
Expand Down Expand Up @@ -1065,14 +1052,10 @@ func (suite *KeeperTestSuite) TestTraceBlock() {
suite.Require().NoError(vmdb.Commit())

contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()
// create multiple transactions in the same block
firstTx := suite.TransferERC20Token(suite.T(), contractAddr, suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), sdkmath.NewIntWithDecimal(1, 18).BigInt())
secondTx := suite.TransferERC20Token(suite.T(), contractAddr, suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), sdkmath.NewIntWithDecimal(1, 18).BigInt())
_, err = suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
// overwrite txs to include only the ones on new block
txs = append([]*types.MsgEthereumTx{}, firstTx, secondTx)
},
Expand Down Expand Up @@ -1124,13 +1107,9 @@ func (suite *KeeperTestSuite) TestTraceBlock() {
suite.SetupTest()
// Deploy contract
contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt())
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()
// Generate token transfer transaction
txMsg := suite.TransferERC20Token(suite.T(), contractAddr, suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), sdkmath.NewIntWithDecimal(1, 18).BigInt())
_, err = suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.ctx.BlockHeight()})
suite.Require().NoError(err)
suite.Commit()

txs = append(txs, txMsg)
Expand Down
10 changes: 8 additions & 2 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,15 @@ func (suite *KeeperTestSuite) EvmDenom() string {
}

func (suite *KeeperTestSuite) Commit() {
_, err := suite.app.Commit()
suite.Require().NoError(err)
header := suite.ctx.BlockHeader()
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: header.Height,
})
suite.Require().NoError(err)

_, err = suite.app.Commit()
suite.Require().NoError(err)

header.Height += 1
suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: header.Height,
Expand Down
11 changes: 10 additions & 1 deletion x/feemarket/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,20 @@ var _ = Describe("Feemarket", func() {
// given a local (validator config) and a gloabl (feemarket param) minGasPrice
func setupTestWithContext(valMinGasPrice string, minGasPrice sdkmath.LegacyDec, baseFee sdkmath.Int) (*ethsecp256k1.PrivKey, banktypes.MsgSend) {
privKey, msg := setupTest(valMinGasPrice + s.denom)
// The BeginBlock, DeliverTx, and EndBlock logic has been combined into a single FinalizeBlock logic.
// Because the SetBaseFee is called in BeginBlock and Commit synchronizes the check state with
// the finalize state being committed, the SetBaseFee call in setupTestWithContext becomes meaningless.
// Therefore, s.Commit() should not be called. Also, for tests that use FinalizeEthBlock,
// we explicitly change it to call SetBaseFee in the finalize state as well.
params := types.DefaultParams()
params.MinGasPrice = minGasPrice
header := s.ctx.BlockHeader()
s.ctx = s.app.NewContextLegacy(true, header)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
s.app.FeeMarketKeeper.SetBaseFee(s.ctx, baseFee.BigInt())
s.ctx = s.app.NewContextLegacy(false, header)
s.app.FeeMarketKeeper.SetParams(s.ctx, params)
s.app.FeeMarketKeeper.SetBaseFee(s.ctx, baseFee.BigInt())
s.Commit()

return privKey, msg
}
Expand Down

0 comments on commit 726aa6f

Please sign in to comment.