Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

evm: various fixes #319

Merged
merged 12 commits into from
Jun 4, 2020
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (`x/evm`) [\#255](https://github.com/ChainSafe/ethermint/pull/255) Add missing `GenesisState` fields and support `ExportGenesis` functionality.
* [\#272](https://github.com/ChainSafe/ethermint/pull/272) Add `Logger` for evm module.
* [\#317](https://github.com/ChainSafe/ethermint/pull/317) `GenesisAccount` validation.
* (`x/evm`) [\#319](https://github.com/ChainSafe/ethermint/pull/319) Verious evm improvements:
* Add transaction `[]*ethtypes.Logs` to evm's `GenesisState` to persist logs after an upgrade.
* Remove evm `CodeKey` and `BlockKey`in favor of a prefix `Store`.
* Set `BlockBloom` during `EndBlock` instead of `BeginBlock`.
* `Commit` state object and `Finalize` storage after `InitGenesis` setup.

### Features

Expand All @@ -73,5 +78,6 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (`x/evm`) [\#319](https://github.com/ChainSafe/ethermint/pull/319) Fix `SetBlockHash` that was setting the incorrect height during `BeginBlock`.
* (x/evm) [\#176](https://github.com/ChainSafe/ethermint/issues/176) Updated Web3 transaction hash from using RLP hash. Now all transaction hashes exposed are amino hashes.
* Removes `Hash()` (RLP) function from `MsgEthereumTx` to avoid confusion or misuse in future.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ test-rpc:
@${GO_MOD} go test -v --vet=off ./tests/rpc_test

it-tests:
./scripts/integration-test-all.sh -q 1 -z 1 -s 10
./scripts/integration-test-all.sh -q 1 -z 1 -s 2

godocs:
@echo "--> Wait a few seconds and visit http://localhost:6060/pkg/github.com/cosmos/ethermint"
Expand Down
9 changes: 2 additions & 7 deletions app/ethermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ func NewEthermintApp(
keys := sdk.NewKVStoreKeys(
bam.MainStoreKey, auth.StoreKey, bank.StoreKey, staking.StoreKey,
supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
gov.StoreKey, params.StoreKey, evidence.StoreKey, evm.CodeKey, evm.StoreKey,
gov.StoreKey, params.StoreKey, evidence.StoreKey, evm.StoreKey,
faucet.StoreKey,
)
blockKey := sdk.NewKVStoreKey(evm.BlockKey)

tkeys := sdk.NewTransientStoreKeys(params.TStoreKey)

Expand Down Expand Up @@ -203,7 +202,7 @@ func NewEthermintApp(
app.subspaces[crisis.ModuleName], invCheckPeriod, app.SupplyKeeper, auth.FeeCollectorName,
)
app.EvmKeeper = evm.NewKeeper(
app.cdc, blockKey, keys[evm.CodeKey], keys[evm.StoreKey], app.AccountKeeper,
app.cdc, keys[evm.StoreKey], app.AccountKeeper,
app.BankKeeper,
)
// TODO: use protobuf
Expand Down Expand Up @@ -299,10 +298,6 @@ func NewEthermintApp(
app.MountKVStores(keys)
app.MountTransientStores(tkeys)

// Mount block hash mapping key as DB (no need for historical queries)
// TODO: why does this need to be always StoreTypeDB?
app.MountStore(blockKey, sdk.StoreTypeDB)

// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
Expand Down
7 changes: 3 additions & 4 deletions importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ var (

accKey = sdk.NewKVStoreKey(auth.StoreKey)
storeKey = sdk.NewKVStoreKey(evmtypes.StoreKey)
codeKey = sdk.NewKVStoreKey(evmtypes.CodeKey)

logger = tmlog.NewNopLogger()

Expand Down Expand Up @@ -107,7 +106,7 @@ func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak auth.Accoun
ms := cms.CacheMultiStore()
ctx := sdk.NewContext(ms, abci.Header{}, false, logger)

stateDB := evmtypes.NewCommitStateDB(ctx, codeKey, storeKey, ak, bk)
stateDB := evmtypes.NewCommitStateDB(ctx, storeKey, ak, bk)

// sort the addresses and insertion of key/value pairs matters
genAddrs := make([]string, len(genBlock.Alloc))
Expand Down Expand Up @@ -189,7 +188,7 @@ func TestImportBlocks(t *testing.T) {
bk := bank.NewBaseKeeper(appCodec, bankKey, ak, bankSubspace, nil)

// mount stores
keys := []*sdk.KVStoreKey{accKey, bankKey, storeKey, codeKey}
keys := []*sdk.KVStoreKey{accKey, bankKey, storeKey}
for _, key := range keys {
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil)
}
Expand Down Expand Up @@ -280,7 +279,7 @@ func TestImportBlocks(t *testing.T) {

// nolint: interfacer
func createStateDB(ctx sdk.Context, ak auth.AccountKeeper, bk bank.Keeper) *evmtypes.CommitStateDB {
return evmtypes.NewCommitStateDB(ctx, codeKey, storeKey, ak, bk)
return evmtypes.NewCommitStateDB(ctx, storeKey, ak, bk)
}

// accumulateRewards credits the coinbase of the given block with the mining
Expand Down
2 changes: 1 addition & 1 deletion rpc/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (e *EthermintBackend) getEthBlockByNumber(height int64, fullTx bool) (map[s
}
}

res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryLogsBloom, strconv.FormatInt(block.Block.Height, 10)))
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryBloom, strconv.FormatInt(block.Block.Height, 10)))
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions tests/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,12 @@ func TestEth_GetTransactionReceipt(t *testing.T) {

param := []string{hash.String()}
rpcRes := call(t, "eth_getTransactionReceipt", param)
require.Nil(t, rpcRes.Error)

receipt := make(map[string]interface{})
err := json.Unmarshal(rpcRes.Result, &receipt)
require.NoError(t, err)
require.NotEmpty(t, receipt)
require.Equal(t, "0x1", receipt["status"].(string))
require.Equal(t, []interface{}{}, receipt["logs"].([]interface{}))
}
Expand Down
17 changes: 10 additions & 7 deletions x/evm/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

// BeginBlock sets the Bloom and Hash mappings and resets the Bloom filter and
// BeginBlock sets the block hash -> block height map and resets the Bloom filter and
// the transaction count to 0.
func BeginBlock(k Keeper, ctx sdk.Context, req abci.RequestBeginBlock) {
if req.Header.LastBlockId.GetHash() == nil || req.Header.GetHeight() < 1 {
return
}

// Consider removing this when using evm as module without web3 API
bloom := ethtypes.BytesToBloom(k.Bloom.Bytes())
k.SetBlockBloomMapping(ctx, bloom, req.Header.GetHeight())
k.SetBlockHashMapping(ctx, req.Header.LastBlockId.GetHash(), req.Header.GetHeight())
k.SetBlockHash(ctx, req.Header.LastBlockId.GetHash(), req.Header.GetHeight()-1)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

// reset counters
k.Bloom = big.NewInt(0)
k.TxCount = 0
}

// EndBlock updates the accounts and commits states objects to the KV Store
func EndBlock(k Keeper, ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
// EndBlock updates the accounts and commits states objects to the KV Store.
//
func EndBlock(k Keeper, ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
// Gas costs are handled within msg handler so costs should be ignored
ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())

Expand All @@ -42,5 +42,8 @@ func EndBlock(k Keeper, ctx sdk.Context, _ abci.RequestEndBlock) []abci.Validato
// Clear accounts cache after account data has been committed
k.CommitStateDB.ClearStateObjects()

bloom := ethtypes.BytesToBloom(k.Bloom.Bytes())
k.SetBlockBloom(ctx, ctx.BlockHeight(), bloom)

return []abci.ValidatorUpdate{}
}
4 changes: 1 addition & 3 deletions x/evm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
const (
ModuleName = types.ModuleName
StoreKey = types.StoreKey
CodeKey = types.StoreKey
BlockKey = types.BlockKey
RouterKey = types.RouterKey
QueryProtocolVersion = types.QueryProtocolVersion
QueryBalance = types.QueryBalance
Expand All @@ -20,7 +18,7 @@ const (
QueryNonce = types.QueryNonce
QueryHashToHeight = types.QueryHashToHeight
QueryTransactionLogs = types.QueryTransactionLogs
QueryLogsBloom = types.QueryLogsBloom
QueryBloom = types.QueryBloom
QueryLogs = types.QueryLogs
QueryAccount = types.QueryAccount
)
Expand Down
33 changes: 27 additions & 6 deletions x/evm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@ import (
// InitGenesis initializes genesis state based on exported genesis
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) []abci.ValidatorUpdate {
for _, account := range data.Accounts {
csdb := k.CommitStateDB.WithContext(ctx)
// FIXME: this will override bank InitGenesis balance!
csdb.SetBalance(account.Address, account.Balance)
csdb.SetCode(account.Address, account.Code)
k.SetBalance(ctx, account.Address, account.Balance)
k.SetCode(ctx, account.Address, account.Code)
for _, storage := range account.Storage {
csdb.SetState(account.Address, storage.Key, storage.Value)
k.SetState(ctx, account.Address, storage.Key, storage.Value)
}
}
// TODO: Commit?

var err error
for _, txLog := range data.TxsLogs {
err = k.SetLogs(ctx, txLog.Hash, txLog.Logs)
if err != nil {
panic(err)
}
}

// set state objects and code to store
_, err = k.Commit(ctx, false)
if err != nil {
panic(err)
}

// set storage to store
err = k.Finalise(ctx, true)
if err != nil {
panic(err)
}
return []abci.ValidatorUpdate{}
}

Expand Down Expand Up @@ -59,5 +77,8 @@ func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisSta
ethGenAccounts = append(ethGenAccounts, genAccount)
}

return GenesisState{Accounts: ethGenAccounts}
return GenesisState{
Accounts: ethGenAccounts,
TxsLogs: k.GetAllTxLogs(ctx),
}
}
10 changes: 8 additions & 2 deletions x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func handleMsgEthereumTx(ctx sdk.Context, k Keeper, msg types.MsgEthereumTx) (*s
k.Bloom.Or(k.Bloom, executionResult.Bloom)

// update transaction logs in KVStore
k.SetTransactionLogs(ctx, txHash, executionResult.Logs)
err = k.SetLogs(ctx, common.BytesToHash(txHash), executionResult.Logs)
if err != nil {
panic(err)
}

// log successful execution
k.Logger(ctx).Info(executionResult.Result.Log)
Expand Down Expand Up @@ -147,7 +150,10 @@ func handleMsgEthermint(ctx sdk.Context, k Keeper, msg types.MsgEthermint) (*sdk
k.Bloom.Or(k.Bloom, executionResult.Bloom)

// update transaction logs in KVStore
k.SetTransactionLogs(ctx, txHash, executionResult.Logs)
err = k.SetLogs(ctx, common.BytesToHash(txHash), executionResult.Logs)
if err != nil {
panic(err)
}

// log successful execution
k.Logger(ctx).Info(executionResult.Result.Log)
Expand Down
7 changes: 4 additions & 3 deletions x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ func (suite *EvmTestSuite) TestHandlerLogs() {
suite.Require().Equal(len(resultData.Logs[0].Topics), 2)

hash := []byte{1}
suite.app.EvmKeeper.SetTransactionLogs(suite.ctx, hash, resultData.Logs)
err = suite.app.EvmKeeper.SetLogs(suite.ctx, ethcmn.BytesToHash(hash), resultData.Logs)
suite.Require().NoError(err)

logs, err := suite.app.EvmKeeper.GetTransactionLogs(suite.ctx, hash)
logs, err := suite.app.EvmKeeper.GetLogs(suite.ctx, ethcmn.BytesToHash(hash))
suite.Require().NoError(err, "failed to get logs")

suite.Require().Equal(logs, resultData.Logs)
Expand Down Expand Up @@ -273,7 +274,7 @@ func (suite *EvmTestSuite) TestQueryTxLogs() {
// get logs by tx hash
hash := resultData.TxHash.Bytes()

logs, err := suite.app.EvmKeeper.GetTransactionLogs(suite.ctx, hash)
logs, err := suite.app.EvmKeeper.GetLogs(suite.ctx, ethcmn.BytesToHash(hash))
suite.Require().NoError(err, "failed to get logs")

suite.Require().Equal(logs, resultData.Logs)
Expand Down
Loading