github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/keeper/abci_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"math/big"
     5  	"os"
     6  	"time"
     7  
     8  	ethcmn "github.com/ethereum/go-ethereum/common"
     9  	ethtypes "github.com/ethereum/go-ethereum/core/types"
    10  	ethcrypto "github.com/ethereum/go-ethereum/crypto"
    11  	"github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1"
    12  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    13  	"github.com/fibonacci-chain/fbc/x/evm/types"
    14  	"github.com/fibonacci-chain/fbc/x/evm/watcher"
    15  	"github.com/spf13/viper"
    16  )
    17  
    18  func (suite *KeeperTestSuite) TestBeginBlock() {
    19  	req := abci.RequestBeginBlock{
    20  		Header: abci.Header{
    21  			LastBlockId: abci.BlockID{
    22  				Hash: ethcmn.FromHex(hex),
    23  			},
    24  			Height: 10,
    25  		},
    26  	}
    27  
    28  	// get the initial consumption
    29  	initialConsumed := suite.ctx.GasMeter().GasConsumed()
    30  
    31  	// update the counters
    32  	suite.app.EvmKeeper.Bloom.SetInt64(10)
    33  	suite.app.EvmKeeper.TxCount = 10
    34  
    35  	suite.app.EvmKeeper.BeginBlock(suite.ctx, abci.RequestBeginBlock{})
    36  	suite.Require().NotZero(suite.app.EvmKeeper.Bloom.Int64())
    37  	suite.Require().NotZero(suite.app.EvmKeeper.TxCount)
    38  
    39  	suite.Require().Equal(int64(initialConsumed), int64(suite.ctx.GasMeter().GasConsumed()))
    40  
    41  	suite.app.EvmKeeper.BeginBlock(suite.ctx, req)
    42  	suite.Require().Zero(suite.app.EvmKeeper.Bloom.Int64())
    43  	suite.Require().Zero(suite.app.EvmKeeper.TxCount)
    44  
    45  	suite.Require().Equal(int64(initialConsumed), int64(suite.ctx.GasMeter().GasConsumed()))
    46  
    47  	blockHash := ethcmn.BytesToHash(req.Header.LastBlockId.Hash)
    48  	lastHeight, found := suite.app.EvmKeeper.GetBlockHeight(suite.ctx, blockHash)
    49  	suite.Require().True(found)
    50  	suite.Require().Equal(int64(9), lastHeight)
    51  }
    52  
    53  func (suite *KeeperTestSuite) TestEndBlock() {
    54  	// update the counters
    55  	suite.app.EvmKeeper.Bloom.SetInt64(10)
    56  
    57  	// set gas limit to 1 to ensure no gas is consumed during the operation
    58  	initialConsumed := suite.ctx.GasMeter().GasConsumed()
    59  
    60  	_ = suite.app.EvmKeeper.EndBlock(suite.ctx, abci.RequestEndBlock{Height: 100})
    61  
    62  	suite.Require().Equal(int64(initialConsumed), int64(suite.ctx.GasMeter().GasConsumed()))
    63  
    64  	bloom := suite.app.EvmKeeper.GetBlockBloom(suite.ctx, 100)
    65  	suite.Require().Equal(int64(10), bloom.Big().Int64())
    66  }
    67  
    68  func (suite *KeeperTestSuite) TestEndBlockWatcher() {
    69  	// update the counters
    70  	suite.app.EvmKeeper.Bloom.SetInt64(10)
    71  	suite.app.EvmKeeper.Watcher.SetFirstUse(true)
    72  
    73  	store := suite.ctx.KVStore(suite.app.EvmKeeper.GetStoreKey())
    74  	store.Set(types.GetContractDeploymentWhitelistMemberKey(suite.address.Bytes()), []byte(""))
    75  	store.Set(types.GetContractBlockedListMemberKey(suite.address.Bytes()), []byte(""))
    76  	viper.Set(watcher.FlagFastQueryLru, 100)
    77  	_ = suite.app.EvmKeeper.EndBlock(suite.ctx, abci.RequestEndBlock{Height: 10})
    78  	suite.app.Commit(abci.RequestCommit{})
    79  	time.Sleep(100 * time.Millisecond)
    80  	querier := watcher.NewQuerier()
    81  	res1 := querier.HasContractDeploymentWhitelist(suite.address.Bytes())
    82  	res2 := querier.HasContractBlockedList(suite.address.Bytes())
    83  	os.RemoveAll(watcher.WatchDbDir)
    84  
    85  	suite.Require().True(res1)
    86  	suite.Require().True(res2)
    87  }
    88  
    89  func (suite *KeeperTestSuite) TestResetCache() {
    90  	// fill journal
    91  	suite.stateDB.AddAddressToAccessList(suite.address)
    92  	// fill refund
    93  	suite.stateDB.AddRefund(100)
    94  	// fill validRevisions
    95  	suite.stateDB.Snapshot()
    96  
    97  	// fill txIndex,thash,bhash
    98  	thash := ethcmn.BytesToHash([]byte("thash"))
    99  	bhash := ethcmn.BytesToHash([]byte("bhash"))
   100  	txi := 2
   101  	suite.stateDB.Prepare(thash, bhash, txi)
   102  
   103  	// fill logSize
   104  	contractAddress := ethcmn.BigToAddress(big.NewInt(1))
   105  	log := ethtypes.Log{Address: contractAddress}
   106  	suite.stateDB.AddLog(&log)
   107  
   108  	// fill preimages, hashToPreimageIndex
   109  	hash := ethcmn.BytesToHash([]byte("hash"))
   110  	preimage := []byte("preimage")
   111  	suite.stateDB.AddPreimage(hash, preimage)
   112  
   113  	// fill stateObjects, addressToObjectIndex, stateObjectsDirty
   114  	priv, err := ethsecp256k1.GenerateKey()
   115  	suite.Require().NoError(err)
   116  	addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)
   117  	suite.stateDB.CreateAccount(addr)
   118  
   119  	_ = suite.app.EvmKeeper.EndBlock(suite.ctx, abci.RequestEndBlock{Height: 1})
   120  
   121  	err = suite.stateDB.Reset(ethcmn.Hash{})
   122  	suite.Require().Nil(err)
   123  
   124  	suite.Require().Zero(suite.stateDB.TxIndex())
   125  	suite.Require().Equal(ethcmn.Hash{}, suite.stateDB.BlockHash())
   126  
   127  	suite.Require().Zero(suite.app.EvmKeeper.Bloom.Int64())
   128  	suite.Require().Zero(suite.app.EvmKeeper.TxCount)
   129  	suite.Require().Zero(len(suite.stateDB.WithContext(suite.ctx).Preimages()))
   130  	suite.Require().Zero(suite.stateDB.GetRefund())
   131  }