2015-12-28 13:20:37 +00: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"
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
"errors"
|
2015-12-28 13:20:37 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-09-28 08:48:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-12-28 13:20:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
2023-05-11 07:15:44 +00:00
|
|
|
// nodeIterator is an iterator to traverse the entire state trie post-order,
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// including all of the contract code and contract state tries. Preimage is
|
|
|
|
// required in order to resolve the contract address.
|
2023-05-11 07:15:44 +00:00
|
|
|
type nodeIterator struct {
|
2015-12-28 13:20:37 +00:00
|
|
|
state *StateDB // State being iterated
|
|
|
|
|
2017-02-22 22:49:34 +00: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 13:20:37 +00:00
|
|
|
|
2016-01-08 11:46:45 +00: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 10:11:56 +00:00
|
|
|
|
2016-01-08 11:46:45 +00: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 10:37:00 +00:00
|
|
|
|
|
|
|
Error error // Failure set in case of an internal error in the iterator
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 07:15:44 +00:00
|
|
|
// newNodeIterator creates an post-order state node iterator.
|
|
|
|
func newNodeIterator(state *StateDB) *nodeIterator {
|
|
|
|
return &nodeIterator{
|
2015-12-28 13:20:37 +00:00
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves the iterator to the next node, returning whether there are any
|
2016-02-16 10:37:00 +00:00
|
|
|
// further nodes. In case of an internal error this method returns false and
|
|
|
|
// sets the Error field to the encountered failure.
|
2023-05-11 07:15:44 +00:00
|
|
|
func (it *nodeIterator) Next() bool {
|
2016-02-16 10:37:00 +00: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 13:20:37 +00:00
|
|
|
return it.retrieve()
|
|
|
|
}
|
|
|
|
|
|
|
|
// step moves the iterator to the next entry of the state trie.
|
2023-05-11 07:15:44 +00:00
|
|
|
func (it *nodeIterator) step() error {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Abort if we reached the end of the iteration
|
|
|
|
if it.state == nil {
|
2016-02-16 10:37:00 +00:00
|
|
|
return nil
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// Initialize the iterator if we've just started
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
var err error
|
2015-12-28 13:20:37 +00:00
|
|
|
if it.stateIt == nil {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
it.stateIt, err = it.state.trie.NodeIterator(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// If we had data nodes previously, we surely have at least state nodes
|
|
|
|
if it.dataIt != nil {
|
2017-02-22 22:49:34 +00:00
|
|
|
if cont := it.dataIt.Next(true); !cont {
|
|
|
|
if it.dataIt.Error() != nil {
|
|
|
|
return it.dataIt.Error()
|
2016-02-16 10:37:00 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
it.dataIt = nil
|
|
|
|
}
|
2016-02-16 10:37:00 +00:00
|
|
|
return nil
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// If we had source code previously, discard that
|
|
|
|
if it.code != nil {
|
|
|
|
it.code = nil
|
2016-02-16 10:37:00 +00:00
|
|
|
return nil
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// Step to the next state trie node, terminating if we're out of nodes
|
2017-02-22 22:49:34 +00:00
|
|
|
if cont := it.stateIt.Next(true); !cont {
|
|
|
|
if it.stateIt.Error() != nil {
|
|
|
|
return it.stateIt.Error()
|
2016-02-16 10:37:00 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
it.state, it.stateIt = nil, nil
|
2016-02-16 10:37:00 +00:00
|
|
|
return nil
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// If the state trie node is an internal entry, leave as is
|
2017-02-22 22:49:34 +00:00
|
|
|
if !it.stateIt.Leaf() {
|
2016-02-16 10:37:00 +00:00
|
|
|
return nil
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// Otherwise we've reached an account node, initiate data iteration
|
2021-09-28 08:48:07 +00:00
|
|
|
var account types.StateAccount
|
2023-08-24 08:47:42 +00:00
|
|
|
if err := rlp.DecodeBytes(it.stateIt.LeafBlob(), &account); err != nil {
|
2016-02-16 10:37:00 +00:00
|
|
|
return err
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// Lookup the preimage of account hash
|
|
|
|
preimage := it.state.trie.GetKey(it.stateIt.LeafKey())
|
|
|
|
if preimage == nil {
|
|
|
|
return errors.New("account address is not available")
|
|
|
|
}
|
|
|
|
address := common.BytesToAddress(preimage)
|
|
|
|
|
|
|
|
// Traverse the storage slots belong to the account
|
2023-11-14 12:09:40 +00:00
|
|
|
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root, it.state.trie)
|
2015-12-28 13:20:37 +00:00
|
|
|
if err != nil {
|
2016-02-16 10:37:00 +00:00
|
|
|
return err
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
it.dataIt, err = dataTrie.NodeIterator(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-22 22:49:34 +00:00
|
|
|
if !it.dataIt.Next(true) {
|
2015-12-28 13:20:37 +00:00
|
|
|
it.dataIt = nil
|
|
|
|
}
|
2023-02-21 11:12:27 +00:00
|
|
|
if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
|
2016-01-06 10:11:56 +00:00
|
|
|
it.codeHash = common.BytesToHash(account.CodeHash)
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
it.code, err = it.state.db.ContractCode(address, common.BytesToHash(account.CodeHash))
|
2015-12-28 13:20:37 +00:00
|
|
|
if err != nil {
|
2016-02-16 10:37:00 +00:00
|
|
|
return fmt.Errorf("code %x: %v", account.CodeHash, err)
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 22:49:34 +00:00
|
|
|
it.accountHash = it.stateIt.Parent()
|
2016-02-16 10:37:00 +00:00
|
|
|
return nil
|
2015-12-28 13:20:37 +00: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.
|
2023-05-11 07:15:44 +00:00
|
|
|
func (it *nodeIterator) retrieve() bool {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Clear out any previously set values
|
2017-02-22 22:49:34 +00:00
|
|
|
it.Hash = common.Hash{}
|
2015-12-28 13:20:37 +00: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-22 22:49:34 +00:00
|
|
|
it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
|
2016-01-08 11:46:45 +00:00
|
|
|
if it.Parent == (common.Hash{}) {
|
|
|
|
it.Parent = it.accountHash
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
case it.code != nil:
|
2017-02-22 22:49:34 +00:00
|
|
|
it.Hash, it.Parent = it.codeHash, it.accountHash
|
2015-12-28 13:20:37 +00:00
|
|
|
case it.stateIt != nil:
|
2017-02-22 22:49:34 +00:00
|
|
|
it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|