2020-04-01 18:49:21 +00:00
|
|
|
package keeper_test
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
2020-04-01 18:49:21 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
"github.com/cosmos/ethermint/app"
|
|
|
|
"github.com/cosmos/ethermint/x/evm/keeper"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2019-10-04 19:32:56 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
const addrHex = "0x756F45E3FA69347A9A973A725E3C98bC4db0b4c1"
|
2020-07-15 10:36:55 +00:00
|
|
|
const hex = "0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"
|
2020-04-23 15:49:25 +00:00
|
|
|
|
2020-05-03 01:56:18 +00:00
|
|
|
var (
|
2020-07-15 10:36:55 +00:00
|
|
|
hash = ethcmn.FromHex(hex)
|
2020-05-03 01:56:18 +00:00
|
|
|
)
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
querier sdk.Querier
|
|
|
|
app *app.EthermintApp
|
2020-07-15 10:36:55 +00:00
|
|
|
address ethcmn.Address
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
|
|
|
checkTx := false
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
suite.app = app.Setup(checkTx)
|
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: "3", Time: time.Now().UTC()})
|
|
|
|
suite.querier = keeper.NewQuerier(suite.app.EvmKeeper)
|
2020-07-15 10:36:55 +00:00
|
|
|
suite.address = ethcmn.HexToAddress(addrHex)
|
2019-09-26 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-05-03 01:56:18 +00:00
|
|
|
func (suite *KeeperTestSuite) TestTransactionLogs() {
|
2020-06-04 10:40:21 +00:00
|
|
|
ethHash := ethcmn.BytesToHash(hash)
|
2020-05-03 01:56:18 +00:00
|
|
|
log := ðtypes.Log{
|
2020-07-15 10:36:55 +00:00
|
|
|
Address: suite.address,
|
2020-05-03 01:56:18 +00:00
|
|
|
Data: []byte("log"),
|
|
|
|
BlockNumber: 10,
|
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
log2 := ðtypes.Log{
|
2020-07-15 10:36:55 +00:00
|
|
|
Address: suite.address,
|
2020-06-04 10:40:21 +00:00
|
|
|
Data: []byte("log2"),
|
|
|
|
BlockNumber: 11,
|
|
|
|
}
|
2020-05-03 01:56:18 +00:00
|
|
|
expLogs := []*ethtypes.Log{log}
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
err := suite.app.EvmKeeper.SetLogs(suite.ctx, ethHash, expLogs)
|
|
|
|
suite.Require().NoError(err)
|
2020-05-03 01:56:18 +00:00
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
logs, err := suite.app.EvmKeeper.GetLogs(suite.ctx, ethHash)
|
2020-05-03 01:56:18 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(expLogs, logs)
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
expLogs = []*ethtypes.Log{log2, log}
|
|
|
|
|
|
|
|
// add another log under the zero hash
|
|
|
|
suite.app.EvmKeeper.AddLog(suite.ctx, log2)
|
2020-05-03 01:56:18 +00:00
|
|
|
logs = suite.app.EvmKeeper.AllLogs(suite.ctx)
|
|
|
|
suite.Require().Equal(expLogs, logs)
|
2020-06-04 10:40:21 +00:00
|
|
|
|
|
|
|
// add another log under the zero hash
|
|
|
|
log3 := ðtypes.Log{
|
2020-07-15 10:36:55 +00:00
|
|
|
Address: suite.address,
|
2020-06-04 10:40:21 +00:00
|
|
|
Data: []byte("log3"),
|
|
|
|
BlockNumber: 10,
|
|
|
|
}
|
|
|
|
suite.app.EvmKeeper.AddLog(suite.ctx, log3)
|
|
|
|
|
|
|
|
txLogs := suite.app.EvmKeeper.GetAllTxLogs(suite.ctx)
|
|
|
|
suite.Require().Equal(2, len(txLogs))
|
|
|
|
|
|
|
|
suite.Require().Equal(ethcmn.Hash{}.String(), txLogs[0].Hash.String())
|
|
|
|
suite.Require().Equal([]*ethtypes.Log{log2, log3}, txLogs[0].Logs)
|
|
|
|
|
|
|
|
suite.Require().Equal(ethHash.String(), txLogs[1].Hash.String())
|
|
|
|
suite.Require().Equal([]*ethtypes.Log{log}, txLogs[1].Logs)
|
2020-05-03 01:56:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
func (suite *KeeperTestSuite) TestDBStorage() {
|
2019-09-26 15:54:23 +00:00
|
|
|
// Perform state transitions
|
2020-07-15 10:36:55 +00:00
|
|
|
suite.app.EvmKeeper.CreateAccount(suite.ctx, suite.address)
|
|
|
|
suite.app.EvmKeeper.SetBalance(suite.ctx, suite.address, big.NewInt(5))
|
|
|
|
suite.app.EvmKeeper.SetNonce(suite.ctx, suite.address, 4)
|
|
|
|
suite.app.EvmKeeper.SetState(suite.ctx, suite.address, ethcmn.HexToHash("0x2"), ethcmn.HexToHash("0x3"))
|
|
|
|
suite.app.EvmKeeper.SetCode(suite.ctx, suite.address, []byte{0x1})
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2019-09-27 14:08:45 +00:00
|
|
|
// Test block hash mapping functionality
|
2020-06-04 10:40:21 +00:00
|
|
|
suite.app.EvmKeeper.SetBlockHash(suite.ctx, hash, 7)
|
|
|
|
height, found := suite.app.EvmKeeper.GetBlockHash(suite.ctx, hash)
|
|
|
|
suite.Require().True(found)
|
2020-04-23 15:49:25 +00:00
|
|
|
suite.Require().Equal(int64(7), height)
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
suite.app.EvmKeeper.SetBlockHash(suite.ctx, []byte{0x43, 0x32}, 8)
|
2019-09-27 14:08:45 +00:00
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
// Test block height mapping functionality
|
|
|
|
testBloom := ethtypes.BytesToBloom([]byte{0x1, 0x3})
|
2020-06-04 10:40:21 +00:00
|
|
|
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 4, testBloom)
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// Get those state transitions
|
2020-07-15 10:36:55 +00:00
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.GetBalance(suite.ctx, suite.address).Cmp(big.NewInt(5)), 0)
|
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address), uint64(4))
|
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.GetState(suite.ctx, suite.address, ethcmn.HexToHash("0x2")), ethcmn.HexToHash("0x3"))
|
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.GetCode(suite.ctx, suite.address), []byte{0x1})
|
2019-09-27 14:08:45 +00:00
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
height, found = suite.app.EvmKeeper.GetBlockHash(suite.ctx, hash)
|
|
|
|
suite.Require().True(found)
|
2020-04-23 15:49:25 +00:00
|
|
|
suite.Require().Equal(height, int64(7))
|
2020-06-04 10:40:21 +00:00
|
|
|
height, found = suite.app.EvmKeeper.GetBlockHash(suite.ctx, []byte{0x43, 0x32})
|
|
|
|
suite.Require().True(found)
|
2020-04-23 15:49:25 +00:00
|
|
|
suite.Require().Equal(height, int64(8))
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
bloom, found := suite.app.EvmKeeper.GetBlockBloom(suite.ctx, 4)
|
|
|
|
suite.Require().True(found)
|
2020-04-01 18:49:21 +00:00
|
|
|
suite.Require().Equal(bloom, testBloom)
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// commit stateDB
|
2020-06-04 10:40:21 +00:00
|
|
|
_, err := suite.app.EvmKeeper.Commit(suite.ctx, false)
|
2020-04-01 18:49:21 +00:00
|
|
|
suite.Require().NoError(err, "failed to commit StateDB")
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
// simulate BaseApp EndBlocker commitment
|
2020-04-01 18:49:21 +00:00
|
|
|
suite.app.Commit()
|
2019-09-26 15:54:23 +00:00
|
|
|
}
|