evm: ForEachStorage semantic not compatible with go-ethereum (#798)

* 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>
This commit is contained in:
yihuang
2021-11-30 10:34:33 +00:00
committed by GitHub
co-authored by Federico Kunze Küllmer
parent 91b042b653
commit e6d0eff957
5 changed files with 10 additions and 9 deletions
+2 -2
View File
@@ -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
})
}
+2 -1
View File
@@ -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
}
}
+3 -3
View File
@@ -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")),