2015-12-28 15:20:37 +02:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-09-28 11:48:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-12-28 15:20:37 +02:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeIterator is an iterator to traverse the entire state trie post-order,
|
|
|
|
// including all of the contract code and contract state tries.
|
|
|
|
type NodeIterator struct {
|
|
|
|
state *StateDB // State being iterated
|
|
|
|
|
2017-02-23 01:49:34 +03:00
|
|
|
stateIt trie.NodeIterator // Primary iterator for the global state trie
|
|
|
|
dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract
|
2015-12-28 15:20:37 +02:00
|
|
|
|
2016-01-08 13:46:45 +02:00
|
|
|
accountHash common.Hash // Hash of the node containing the account
|
|
|
|
codeHash common.Hash // Hash of the contract source code
|
|
|
|
code []byte // Source code associated with a contract
|
2016-01-06 12:11:56 +02:00
|
|
|
|
2016-01-08 13:46:45 +02:00
|
|
|
Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
|
|
|
|
Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
|
2016-02-16 12:37:00 +02:00
|
|
|
|
|
|
|
Error error // Failure set in case of an internal error in the iterator
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeIterator creates an post-order state node iterator.
|
|
|
|
func NewNodeIterator(state *StateDB) *NodeIterator {
|
|
|
|
return &NodeIterator{
|
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves the iterator to the next node, returning whether there are any
|
2016-02-16 12:37:00 +02:00
|
|
|
// further nodes. In case of an internal error this method returns false and
|
|
|
|
// sets the Error field to the encountered failure.
|
2015-12-28 15:20:37 +02:00
|
|
|
func (it *NodeIterator) Next() bool {
|
2016-02-16 12:37:00 +02:00
|
|
|
// If the iterator failed previously, don't do anything
|
|
|
|
if it.Error != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Otherwise step forward with the iterator and report any errors
|
|
|
|
if err := it.step(); err != nil {
|
|
|
|
it.Error = err
|
|
|
|
return false
|
|
|
|
}
|
2015-12-28 15:20:37 +02:00
|
|
|
return it.retrieve()
|
|
|
|
}
|
|
|
|
|
|
|
|
// step moves the iterator to the next entry of the state trie.
|
2016-02-16 12:37:00 +02:00
|
|
|
func (it *NodeIterator) step() error {
|
2015-12-28 15:20:37 +02:00
|
|
|
// Abort if we reached the end of the iteration
|
|
|
|
if it.state == nil {
|
2016-02-16 12:37:00 +02:00
|
|
|
return nil
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
// Initialize the iterator if we've just started
|
|
|
|
if it.stateIt == nil {
|
2017-04-13 15:41:24 +03:00
|
|
|
it.stateIt = it.state.trie.NodeIterator(nil)
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
// If we had data nodes previously, we surely have at least state nodes
|
|
|
|
if it.dataIt != nil {
|
2017-02-23 01:49:34 +03:00
|
|
|
if cont := it.dataIt.Next(true); !cont {
|
|
|
|
if it.dataIt.Error() != nil {
|
|
|
|
return it.dataIt.Error()
|
2016-02-16 12:37:00 +02:00
|
|
|
}
|
2015-12-28 15:20:37 +02:00
|
|
|
it.dataIt = nil
|
|
|
|
}
|
2016-02-16 12:37:00 +02:00
|
|
|
return nil
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
// If we had source code previously, discard that
|
|
|
|
if it.code != nil {
|
|
|
|
it.code = nil
|
2016-02-16 12:37:00 +02:00
|
|
|
return nil
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
// Step to the next state trie node, terminating if we're out of nodes
|
2017-02-23 01:49:34 +03:00
|
|
|
if cont := it.stateIt.Next(true); !cont {
|
|
|
|
if it.stateIt.Error() != nil {
|
|
|
|
return it.stateIt.Error()
|
2016-02-16 12:37:00 +02:00
|
|
|
}
|
2015-12-28 15:20:37 +02:00
|
|
|
it.state, it.stateIt = nil, nil
|
2016-02-16 12:37:00 +02:00
|
|
|
return nil
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
// If the state trie node is an internal entry, leave as is
|
2017-02-23 01:49:34 +03:00
|
|
|
if !it.stateIt.Leaf() {
|
2016-02-16 12:37:00 +02:00
|
|
|
return nil
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
// Otherwise we've reached an account node, initiate data iteration
|
2021-09-28 11:48:07 +03:00
|
|
|
var account types.StateAccount
|
2017-02-23 01:49:34 +03:00
|
|
|
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
|
2016-02-16 12:37:00 +02:00
|
|
|
return err
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, common.BytesToHash(it.stateIt.LeafKey()), account.Root)
|
2015-12-28 15:20:37 +02:00
|
|
|
if err != nil {
|
2016-02-16 12:37:00 +02:00
|
|
|
return err
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
2017-04-13 15:41:24 +03:00
|
|
|
it.dataIt = dataTrie.NodeIterator(nil)
|
2017-02-23 01:49:34 +03:00
|
|
|
if !it.dataIt.Next(true) {
|
2015-12-28 15:20:37 +02:00
|
|
|
it.dataIt = nil
|
|
|
|
}
|
2023-02-21 14:12:27 +03:00
|
|
|
if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
|
2016-01-06 12:11:56 +02:00
|
|
|
it.codeHash = common.BytesToHash(account.CodeHash)
|
2017-06-27 16:57:06 +03:00
|
|
|
addrHash := common.BytesToHash(it.stateIt.LeafKey())
|
|
|
|
it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
|
2015-12-28 15:20:37 +02:00
|
|
|
if err != nil {
|
2016-02-16 12:37:00 +02:00
|
|
|
return fmt.Errorf("code %x: %v", account.CodeHash, err)
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 01:49:34 +03:00
|
|
|
it.accountHash = it.stateIt.Parent()
|
2016-02-16 12:37:00 +02:00
|
|
|
return nil
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve pulls and caches the current state entry the iterator is traversing.
|
|
|
|
// The method returns whether there are any more data left for inspection.
|
|
|
|
func (it *NodeIterator) retrieve() bool {
|
|
|
|
// Clear out any previously set values
|
2017-02-23 01:49:34 +03:00
|
|
|
it.Hash = common.Hash{}
|
2015-12-28 15:20:37 +02:00
|
|
|
|
|
|
|
// If the iteration's done, return no available data
|
|
|
|
if it.state == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Otherwise retrieve the current entry
|
|
|
|
switch {
|
|
|
|
case it.dataIt != nil:
|
2017-02-23 01:49:34 +03:00
|
|
|
it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
|
2016-01-08 13:46:45 +02:00
|
|
|
if it.Parent == (common.Hash{}) {
|
|
|
|
it.Parent = it.accountHash
|
|
|
|
}
|
2015-12-28 15:20:37 +02:00
|
|
|
case it.code != nil:
|
2017-02-23 01:49:34 +03:00
|
|
|
it.Hash, it.Parent = it.codeHash, it.accountHash
|
2015-12-28 15:20:37 +02:00
|
|
|
case it.stateIt != nil:
|
2017-02-23 01:49:34 +03:00
|
|
|
it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
|
2015-12-28 15:20:37 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|