laconicd/x/evm/keeper/keeper_test.go

155 lines
5.0 KiB
Go
Raw Normal View History

package keeper_test
import (
"math/big"
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
2021-04-17 10:00:07 +00:00
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/ethermint/app"
ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/types"
ethcmn "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
2021-04-17 10:00:07 +00:00
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
x/evm: unit tests and fixes (#223) * evm: move Keeper and Querier to /keeper package * keeper: update keeper_test.go * fix format * evm: use aliased types * bump SDK version to v0.38.1 * app: updates from new version * errors: switch sdk.Error -> error * errors: switch sdk.Error -> error. Continuation * more fixes * update app/ * update keys and client pkgs * build * fix tests * lint * minor changes * changelog * address @austinbell comments * Fix keyring usage in rpc API and CLI * fix keyring * break line * Misc cleanup (#188) * evm: move Begin and EndBlock to abci.go * evm: use expected keeper interfaces * app: use EthermintApp for integration and unit test setup * evm: remove count type; update codec * go mod verify * evm: rename msgs for consistency * evm: events * minor cleanup * lint * ante: update tests * changelog * nolint * evm: update statedb to create ethermint Account instead of BaseAccount * fix importer test * address @austinabell comments * update README * changelog * evm: update codec * rename GasLimit->Gas and Price ->GasPrice * msg cleanup and tests * cleanup TxData * fix marshaling * revert rename * move types * evm/keeper: querier tests * switch MarshalLengthPrefixed -> BinaryBare; remove panics * fix event sender * fix panic * try fix txDecoder error * evm: handler tests * evm: handler MsgEthermint test * fix tests * store logs in keeper after transition (#210) * add some comments * begin log handler test * update TransitionCSDB to return ReturnData * use rlp for result data encode/decode * update tests * implement SetBlockLogs * implement GetBlockLogs * test log set/get * update keeper get/set logs to use hash as key * fix test * move logsKey to csdb * attempt to fix test * attempt to fix test * attempt to fix test * lint * lint * lint * save logs after handling msg * update k.Logs * cleanup * remove unused * fix issues * comment out handler test * address comments * lint * fix handler test * address comments * use amino * lint * address comments * merge * fix encoding bug * minor fix * rpc: error handling * rpc: simulate only returns gasConsumed * rpc: error ineffassign * evm: handler test * go: bump version to 1.14 and SDK version to latest master * rpc: fix simulation return value * breaking changes from SDK * sdk: breaking changes; build * tests: fixes * minor fix * proto: ethermint types attempt * proto: define EthAccount proto type and extend sdk std.Codec * evm: fix panic on handler test * evm: minor state object changes * cleanup * tests: update test-importer * fix evm test * fix pubkey registration * lint * cleanup * more test checks for importer * minor change * codec fixes * rm init func * fix importer test build * fixes * test fixes * fix bloom key * rm unnecesary func * remove comment Co-authored-by: austinabell <austinabell8@gmail.com> Co-authored-by: noot <36753753+noot@users.noreply.github.com>
2020-04-23 15:49:25 +00:00
const addrHex = "0x756F45E3FA69347A9A973A725E3C98bC4db0b4c1"
const hex = "0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"
x/evm: unit tests and fixes (#223) * evm: move Keeper and Querier to /keeper package * keeper: update keeper_test.go * fix format * evm: use aliased types * bump SDK version to v0.38.1 * app: updates from new version * errors: switch sdk.Error -> error * errors: switch sdk.Error -> error. Continuation * more fixes * update app/ * update keys and client pkgs * build * fix tests * lint * minor changes * changelog * address @austinbell comments * Fix keyring usage in rpc API and CLI * fix keyring * break line * Misc cleanup (#188) * evm: move Begin and EndBlock to abci.go * evm: use expected keeper interfaces * app: use EthermintApp for integration and unit test setup * evm: remove count type; update codec * go mod verify * evm: rename msgs for consistency * evm: events * minor cleanup * lint * ante: update tests * changelog * nolint * evm: update statedb to create ethermint Account instead of BaseAccount * fix importer test * address @austinabell comments * update README * changelog * evm: update codec * rename GasLimit->Gas and Price ->GasPrice * msg cleanup and tests * cleanup TxData * fix marshaling * revert rename * move types * evm/keeper: querier tests * switch MarshalLengthPrefixed -> BinaryBare; remove panics * fix event sender * fix panic * try fix txDecoder error * evm: handler tests * evm: handler MsgEthermint test * fix tests * store logs in keeper after transition (#210) * add some comments * begin log handler test * update TransitionCSDB to return ReturnData * use rlp for result data encode/decode * update tests * implement SetBlockLogs * implement GetBlockLogs * test log set/get * update keeper get/set logs to use hash as key * fix test * move logsKey to csdb * attempt to fix test * attempt to fix test * attempt to fix test * lint * lint * lint * save logs after handling msg * update k.Logs * cleanup * remove unused * fix issues * comment out handler test * address comments * lint * fix handler test * address comments * use amino * lint * address comments * merge * fix encoding bug * minor fix * rpc: error handling * rpc: simulate only returns gasConsumed * rpc: error ineffassign * evm: handler test * go: bump version to 1.14 and SDK version to latest master * rpc: fix simulation return value * breaking changes from SDK * sdk: breaking changes; build * tests: fixes * minor fix * proto: ethermint types attempt * proto: define EthAccount proto type and extend sdk std.Codec * evm: fix panic on handler test * evm: minor state object changes * cleanup * tests: update test-importer * fix evm test * fix pubkey registration * lint * cleanup * more test checks for importer * minor change * codec fixes * rm init func * fix importer test build * fixes * test fixes * fix bloom key * rm unnecesary func * remove comment Co-authored-by: austinabell <austinabell8@gmail.com> Co-authored-by: noot <36753753+noot@users.noreply.github.com>
2020-04-23 15:49:25 +00:00
var (
hash = ethcmn.FromHex(hex)
)
type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
app *app.EthermintApp
queryClient types.QueryClient
address ethcmn.Address
}
func (suite *KeeperTestSuite) SetupTest() {
checkTx := false
suite.app = app.Setup(checkTx)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: "ethermint-1", Time: time.Now().UTC()})
suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx)
suite.address = ethcmn.HexToAddress(addrHex)
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())
acc := &ethermint.EthAccount{
2021-04-17 10:00:07 +00:00
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0),
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)
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
func (suite *KeeperTestSuite) TestTransactionLogs() {
ethHash := ethcmn.BytesToHash(hash)
log := &ethtypes.Log{
Address: suite.address,
Data: []byte("log"),
BlockNumber: 10,
}
log2 := &ethtypes.Log{
Address: suite.address,
Data: []byte("log2"),
BlockNumber: 11,
}
expLogs := []*ethtypes.Log{log}
err := suite.app.EvmKeeper.CommitStateDB.SetLogs(ethHash, expLogs)
suite.Require().NoError(err)
logs, err := suite.app.EvmKeeper.CommitStateDB.GetLogs(ethHash)
suite.Require().NoError(err)
suite.Require().Equal(expLogs, logs)
expLogs = []*ethtypes.Log{log2, log}
// add another log under the zero hash
suite.app.EvmKeeper.CommitStateDB.AddLog(log2)
logs = suite.app.EvmKeeper.CommitStateDB.AllLogs()
suite.Require().Equal(expLogs, logs)
// add another log under the zero hash
log3 := &ethtypes.Log{
Address: suite.address,
Data: []byte("log3"),
BlockNumber: 10,
}
suite.app.EvmKeeper.CommitStateDB.AddLog(log3)
txLogs := suite.app.EvmKeeper.GetAllTxLogs(suite.ctx)
suite.Require().Equal(2, len(txLogs))
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)
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)
}
func (suite *KeeperTestSuite) TestDBStorage() {
// Perform state transitions
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})
// Test block height mapping functionality
testBloom := ethtypes.BytesToBloom([]byte{0x1, 0x3})
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 4, testBloom)
// Get those state transitions
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})
bloom, found := suite.app.EvmKeeper.GetBlockBloom(suite.ctx, 4)
suite.Require().True(found)
suite.Require().Equal(bloom, testBloom)
// commit stateDB
_, err := suite.app.EvmKeeper.CommitStateDB.Commit(false)
suite.Require().NoError(err, "failed to commit StateDB")
// simulate BaseApp EndBlocker commitment
suite.app.Commit()
}
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)
}