diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index 06a0e893..9fb0f4e5 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -38,7 +38,10 @@ import ( ethcrypto "github.com/ethereum/go-ethereum/crypto" abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/tmhash" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmversion "github.com/tendermint/tendermint/proto/tendermint/version" + "github.com/tendermint/tendermint/version" ) var ( @@ -104,6 +107,23 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { ChainID: "ethermint_9000-1", Time: time.Now().UTC(), ProposerAddress: suite.consAddress.Bytes(), + Version: tmversion.Consensus{ + Block: version.BlockProtocol, + }, + LastBlockId: tmproto.BlockID{ + Hash: tmhash.Sum([]byte("block_id")), + PartSetHeader: tmproto.PartSetHeader{ + Total: 11, + Hash: tmhash.Sum([]byte("partset_header")), + }, + }, + AppHash: tmhash.Sum([]byte("app")), + DataHash: tmhash.Sum([]byte("data")), + EvidenceHash: tmhash.Sum([]byte("evidence")), + ValidatorsHash: tmhash.Sum([]byte("validators")), + NextValidatorsHash: tmhash.Sum([]byte("next_validators")), + ConsensusHash: tmhash.Sum([]byte("consensus")), + LastResultsHash: tmhash.Sum([]byte("last_result")), }) suite.app.EvmKeeper.WithContext(suite.ctx) @@ -151,7 +171,7 @@ func (suite *KeeperTestSuite) EvmDenom() string { // Commit and begin new block func (suite *KeeperTestSuite) Commit() { - suite.app.Commit() + _ = suite.app.Commit() header := suite.ctx.BlockHeader() header.Height += 1 suite.app.BeginBlock(abci.RequestBeginBlock{ diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index bf6aa91f..bd3bfb7a 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -3,13 +3,112 @@ package keeper_test import ( "fmt" - "github.com/tharsis/ethermint/tests" + "github.com/ethereum/go-ethereum/common" + + "github.com/tendermint/tendermint/crypto/tmhash" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmtypes "github.com/tendermint/tendermint/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/tharsis/ethermint/tests" ) +func (suite *KeeperTestSuite) TestGetHashFn() { + header := suite.ctx.BlockHeader() + h, _ := tmtypes.HeaderFromProto(&header) + hash := h.Hash() + + testCases := []struct { + msg string + height uint64 + malleate func() + expHash common.Hash + }{ + { + "case 1.1: context hash cached", + uint64(suite.ctx.BlockHeight()), + func() { + suite.ctx = suite.ctx.WithHeaderHash(tmhash.Sum([]byte("header"))) + suite.app.EvmKeeper.WithContext(suite.ctx) + }, + common.BytesToHash(tmhash.Sum([]byte("header"))), + }, + { + "case 1.2: failed to cast Tendermint header", + uint64(suite.ctx.BlockHeight()), + func() { + header := tmproto.Header{} + header.Height = suite.ctx.BlockHeight() + suite.ctx = suite.ctx.WithBlockHeader(header) + suite.app.EvmKeeper.WithContext(suite.ctx) + }, + common.Hash{}, + }, + { + "case 1.3: hash calculated from Tendermint header", + uint64(suite.ctx.BlockHeight()), + func() { + + suite.ctx = suite.ctx.WithBlockHeader(header) + suite.app.EvmKeeper.WithContext(suite.ctx) + }, + common.BytesToHash(hash), + }, + { + "case 2.1: height lower than current one, hist info not found", + 1, + func() { + suite.ctx = suite.ctx.WithBlockHeight(10) + suite.app.EvmKeeper.WithContext(suite.ctx) + }, + common.Hash{}, + }, + { + "case 2.2: height lower than current one, invalid hist info header", + 1, + func() { + suite.app.StakingKeeper.SetHistoricalInfo(suite.ctx, 1, &stakingtypes.HistoricalInfo{}) + suite.ctx = suite.ctx.WithBlockHeight(10) + suite.app.EvmKeeper.WithContext(suite.ctx) + }, + common.Hash{}, + }, + { + "case 2.3: height lower than current one, calculated from hist info header", + 1, + func() { + histInfo := &stakingtypes.HistoricalInfo{ + Header: header, + } + suite.app.StakingKeeper.SetHistoricalInfo(suite.ctx, 1, histInfo) + suite.ctx = suite.ctx.WithBlockHeight(10) + suite.app.EvmKeeper.WithContext(suite.ctx) + }, + common.BytesToHash(hash), + }, + { + "case 3: height greater than current one", + 200, + func() {}, + common.Hash{}, + }, + } + + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() // reset + + tc.malleate() + + hash := suite.app.EvmKeeper.GetHashFn()(tc.height) + suite.Require().Equal(tc.expHash, hash) + }) + } +} + func (suite *KeeperTestSuite) TestGetCoinbaseAddress() { valOpAddr := tests.GenerateAddress() @@ -74,5 +173,4 @@ func (suite *KeeperTestSuite) TestGetCoinbaseAddress() { } }) } - }