Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,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)
if so == nil {
return
return nil
}
it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))

for it.Next() {
key := common.BytesToHash(db.trie.GetKey(it.Key))
if value, dirty := so.dirtyStorage[key]; dirty {
cb(key, value)
if !cb(key, value) {
return nil
}
continue
}
cb(key, common.BytesToHash(it.Value))

if len(it.Value) > 0 {
_, content, _, err := rlp.Split(it.Value)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to post-process the value returned by the iterator? Shouldn't the underlying iterator do this?

Copy link
Copy Markdown
Member Author

@rjl493456442 rjl493456442 Feb 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is quite weird since before putting the value into the trie, we have an additional rlp encode process https://github.com/ethereum/go-ethereum/blob/master/core/state/state_object.go#L234
And for the normal GetState operation, there is a corresponding rlp decode process https://github.com/ethereum/go-ethereum/blob/master/core/state/state_object.go#L186

While the iterator only iterate the trie node and miss the rlp decode operation.

if err != nil {
return err
}
if !cb(key, common.BytesToHash(content)) {
return nil
}
}
}
return nil
}

// Copy creates a deep, independent copy of the state.
Expand Down
2 changes: 1 addition & 1 deletion core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type StateDB interface {
AddLog(*types.Log)
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
Expand Down