From e6d0eff9575e844d6e3824bc6b3a46c97fa28fe7 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 30 Nov 2021 18:34:33 +0800 Subject: [PATCH] evm: `ForEachStorage` semantic not compatible with go-ethereum (#798) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Problem: ForEachStorage sematic not compatible with go-ethereum Solution: - reversed the semantic of return value of the callback function. * changelog Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 1 + testutil/network/util.go | 5 ++--- x/evm/keeper/keeper.go | 4 ++-- x/evm/keeper/statedb.go | 3 ++- x/evm/keeper/statedb_test.go | 6 +++--- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2329d45..bed7702c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [tharsis#769](https://github.com/tharsis/ethermint/pull/769) Fix default Ethereum signer for JSON-RPC. * (rpc) [tharsis#781](https://github.com/tharsis/ethermint/pull/781) Fix get block invalid transactions filter. * (rpc) [tharsis#782](https://github.com/tharsis/ethermint/pull/782) Fix wrong block gas limit returned by JSON-RPC. +* (evm) [tharsis#798](https://github.com/tharsis/ethermint/pull/798) Fix the semantic of `ForEachStorage` callback's return value ## [v0.8.0] - 2021-11-17 diff --git a/testutil/network/util.go b/testutil/network/util.go index fe3e138c..1c4c8f50 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -251,10 +251,9 @@ func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalance } func WriteFile(name string, dir string, contents []byte) error { - writePath := filepath.Join(dir) - file := filepath.Join(writePath, name) + file := filepath.Join(dir, name) - err := tmos.EnsureDir(writePath, 0o755) + err := tmos.EnsureDir(dir, 0o755) if err != nil { return err } diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 750554b7..9c6945c0 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -295,7 +295,7 @@ func (k Keeper) GetAccountStorage(ctx sdk.Context, address common.Address) (type err := k.ForEachStorage(address, func(key, value common.Hash) bool { storage = append(storage, types.NewState(key, value)) - return false + return true }) if err != nil { return types.Storage{}, err @@ -317,7 +317,7 @@ func (k Keeper) DeleteState(addr common.Address, key common.Hash) { func (k Keeper) DeleteAccountStorage(addr common.Address) { _ = k.ForEachStorage(addr, func(key, _ common.Hash) bool { k.DeleteState(addr, key) - return false + return true }) } diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index 76a42116..10fb78c7 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -795,6 +795,7 @@ func (k *Keeper) AddPreimage(_ common.Hash, _ []byte) {} // ForEachStorage uses the store iterator to iterate over all the state keys and perform a callback // function on each of them. +// The callback should return `true` to continue, return `false` to break early. func (k *Keeper) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error { if k.HasStateError() { return k.stateErr @@ -814,7 +815,7 @@ func (k *Keeper) ForEachStorage(addr common.Address, cb func(key, value common.H value := common.BytesToHash(iterator.Value()) // check if iteration stops - if cb(key, value) { + if !cb(key, value) { return nil } } diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index 82a2966f..7c69072b 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -747,7 +747,7 @@ func (suite *KeeperTestSuite) TestForEachStorage() { }, func(key, value common.Hash) bool { storage = append(storage, types.NewState(key, value)) - return false + return true }, []common.Hash{ common.BytesToHash([]byte("value0")), @@ -766,9 +766,9 @@ func (suite *KeeperTestSuite) TestForEachStorage() { func(key, value common.Hash) bool { if value == common.BytesToHash([]byte("filtervalue")) { storage = append(storage, types.NewState(key, value)) - return true + return false } - return false + return true }, []common.Hash{ common.BytesToHash([]byte("filtervalue")),