Skip to content

Commit

Permalink
evm: GetHashFn test (evmos#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored Aug 31, 2021
1 parent 87c4ea2 commit 516ffe2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
22 changes: 21 additions & 1 deletion x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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{
Expand Down
102 changes: 100 additions & 2 deletions x/evm/keeper/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -74,5 +173,4 @@ func (suite *KeeperTestSuite) TestGetCoinbaseAddress() {
}
})
}

}

0 comments on commit 516ffe2

Please sign in to comment.