core/state: fix state iterator (#19127)

* core/state: fix state iterator

* core: fix state iterator more elegant
This commit is contained in:
gary rong 2019-04-05 14:44:02 +08:00 committed by Péter Szilágyi
parent 7dd3194710
commit 36f81118f6
2 changed files with 18 additions and 5 deletions

View File

@ -515,20 +515,33 @@ func (self *StateDB) CreateAccount(addr common.Address) {
} }
} }
func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) { func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
so := db.getStateObject(addr) so := db.getStateObject(addr)
if so == nil { if so == nil {
return return nil
} }
it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
for it.Next() { for it.Next() {
key := common.BytesToHash(db.trie.GetKey(it.Key)) key := common.BytesToHash(db.trie.GetKey(it.Key))
if value, dirty := so.dirtyStorage[key]; dirty { if value, dirty := so.dirtyStorage[key]; dirty {
cb(key, value) if !cb(key, value) {
return nil
}
continue continue
} }
cb(key, common.BytesToHash(it.Value))
if len(it.Value) > 0 {
_, content, _, err := rlp.Split(it.Value)
if err != nil {
return err
} }
if !cb(key, common.BytesToHash(content)) {
return nil
}
}
}
return nil
} }
// Copy creates a deep, independent copy of the state. // Copy creates a deep, independent copy of the state.

View File

@ -63,7 +63,7 @@ type StateDB interface {
AddLog(*types.Log) AddLog(*types.Log)
AddPreimage(common.Hash, []byte) AddPreimage(common.Hash, []byte)
ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) error
} }
// CallContext provides a basic interface for the EVM calling conventions. The EVM // CallContext provides a basic interface for the EVM calling conventions. The EVM