evm: GetHashFn test (#515)

This commit is contained in:
Federico Kunze Küllmer 2021-08-31 17:58:46 +02:00 committed by GitHub
parent 87c4ea2dc2
commit 516ffe2bf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 3 deletions

View File

@ -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{

View File

@ -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() {
}
})
}
}