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
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2019-09-26 15:54:23 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
"github.com/cosmos/ethermint/app"
|
2020-08-23 21:41:54 +00:00
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
2020-09-02 19:41:05 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
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"
|
2020-08-23 21:41:54 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
queryClient types.QueryClient
|
|
|
|
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)
|
2021-05-31 09:05:32 +00:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: "ethermint-1", Time: time.Now().UTC()})
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx)
|
|
|
|
|
2020-07-15 10:36:55 +00:00
|
|
|
suite.address = ethcmn.HexToAddress(addrHex)
|
2020-08-23 21:41:54 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
balance := ethermint.NewPhotonCoin(sdk.ZeroInt())
|
2020-08-23 21:41:54 +00:00
|
|
|
acc := ðermint.EthAccount{
|
2021-04-17 10:00:07 +00:00
|
|
|
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0),
|
2020-08-23 21:41:54 +00:00
|
|
|
CodeHash: ethcrypto.Keccak256(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.app.BankKeeper.SetBalance(suite.ctx, acc.GetAddress(), balance)
|
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}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
err := suite.app.EvmKeeper.CommitStateDB.SetLogs(ethHash, expLogs)
|
2020-06-04 10:40:21 +00:00
|
|
|
suite.Require().NoError(err)
|
2020-05-03 01:56:18 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
logs, err := suite.app.EvmKeeper.CommitStateDB.GetLogs(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
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.AddLog(log2)
|
|
|
|
logs = suite.app.EvmKeeper.CommitStateDB.AllLogs()
|
2020-05-03 01:56:18 +00:00
|
|
|
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,
|
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.AddLog(log3)
|
2020-06-04 10:40:21 +00:00
|
|
|
|
|
|
|
txLogs := suite.app.EvmKeeper.GetAllTxLogs(suite.ctx)
|
|
|
|
suite.Require().Equal(2, len(txLogs))
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.Require().Equal(ethcmn.Hash{}.String(), txLogs[0].Hash)
|
2021-04-18 15:54:18 +00:00
|
|
|
suite.Require().Equal([]*ethtypes.Log{log2, log3}, txLogs[0].Logs)
|
2020-06-04 10:40:21 +00:00
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.Require().Equal(ethHash.String(), txLogs[1].Hash)
|
2021-04-18 15:54:18 +00:00
|
|
|
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
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.app.EvmKeeper.CommitStateDB.CreateAccount(suite.address)
|
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetBalance(suite.address, big.NewInt(5))
|
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetNonce(suite.address, 4)
|
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetState(suite.address, ethcmn.HexToHash("0x2"), ethcmn.HexToHash("0x3"))
|
|
|
|
suite.app.EvmKeeper.CommitStateDB.SetCode(suite.address, []byte{0x1})
|
2019-09-26 15:54:23 +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
|
2021-05-25 12:56:36 +00:00
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.CommitStateDB.GetBalance(suite.address).Cmp(big.NewInt(5)), 0)
|
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.CommitStateDB.GetNonce(suite.address), uint64(4))
|
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.CommitStateDB.GetState(suite.address, ethcmn.HexToHash("0x2")), ethcmn.HexToHash("0x3"))
|
|
|
|
suite.Require().Equal(suite.app.EvmKeeper.CommitStateDB.GetCode(suite.address), []byte{0x1})
|
2019-09-27 14:08:45 +00:00
|
|
|
|
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
|
2021-05-25 12:56:36 +00:00
|
|
|
_, err := suite.app.EvmKeeper.CommitStateDB.Commit(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
|
|
|
}
|
2020-09-02 19:41:05 +00:00
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestChainConfig() {
|
|
|
|
config, found := suite.app.EvmKeeper.GetChainConfig(suite.ctx)
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(types.DefaultChainConfig(), config)
|
|
|
|
|
|
|
|
config.EIP150Block = sdk.NewInt(100)
|
|
|
|
suite.app.EvmKeeper.SetChainConfig(suite.ctx, config)
|
|
|
|
newConfig, found := suite.app.EvmKeeper.GetChainConfig(suite.ctx)
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(config, newConfig)
|
|
|
|
}
|