2015-12-28 07:20:37 -06: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 04:11:02 -05:00
|
|
|
"errors"
|
2015-12-28 07:20:37 -06:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-09-28 03:48:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-12-28 07:20:37 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
2023-05-11 02:15:44 -05: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 04:11:02 -05:00
|
|
|
// including all of the contract code and contract state tries. Preimage is
|
|
|
|
// required in order to resolve the contract address.
|
2023-05-11 02:15:44 -05:00
|
|
|
type nodeIterator struct {
|
2015-12-28 07:20:37 -06:00
|
|
|
state *StateDB // State being iterated
|
|
|
|
|
2017-02-22 16:49:34 -06: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 07:20:37 -06:00
|
|
|
|
2016-01-08 05:46:45 -06: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 04:11:56 -06:00
|
|
|
|
2016-01-08 05:46:45 -06: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 04:37:00 -06:00
|
|
|
|
|
|
|
Error error // Failure set in case of an internal error in the iterator
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
|
2024-04-23 05:09:42 -05:00
|
|
|
// newNodeIterator creates a post-order state node iterator.
|
2023-05-11 02:15:44 -05:00
|
|
|
func newNodeIterator(state *StateDB) *nodeIterator {
|
|
|
|
return &nodeIterator{
|
2015-12-28 07:20:37 -06:00
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves the iterator to the next node, returning whether there are any
|
2016-02-16 04:37:00 -06: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 02:15:44 -05:00
|
|
|
func (it *nodeIterator) Next() bool {
|
2016-02-16 04:37:00 -06: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 07:20:37 -06:00
|
|
|
return it.retrieve()
|
|
|
|
}
|
|
|
|
|
|
|
|
// step moves the iterator to the next entry of the state trie.
|
2023-05-11 02:15:44 -05:00
|
|
|
func (it *nodeIterator) step() error {
|
2015-12-28 07:20:37 -06:00
|
|
|
// Abort if we reached the end of the iteration
|
|
|
|
if it.state == nil {
|
2016-02-16 04:37:00 -06:00
|
|
|
return nil
|
2015-12-28 07:20:37 -06: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 14:31:45 -05:00
|
|
|
var err error
|
2015-12-28 07:20:37 -06: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 14:31:45 -05:00
|
|
|
it.stateIt, err = it.state.trie.NodeIterator(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
// If we had data nodes previously, we surely have at least state nodes
|
|
|
|
if it.dataIt != nil {
|
2017-02-22 16:49:34 -06:00
|
|
|
if cont := it.dataIt.Next(true); !cont {
|
|
|
|
if it.dataIt.Error() != nil {
|
|
|
|
return it.dataIt.Error()
|
2016-02-16 04:37:00 -06:00
|
|
|
}
|
2015-12-28 07:20:37 -06:00
|
|
|
it.dataIt = nil
|
|
|
|
}
|
2016-02-16 04:37:00 -06:00
|
|
|
return nil
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
// If we had source code previously, discard that
|
|
|
|
if it.code != nil {
|
|
|
|
it.code = nil
|
2016-02-16 04:37:00 -06:00
|
|
|
return nil
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
// Step to the next state trie node, terminating if we're out of nodes
|
2017-02-22 16:49:34 -06:00
|
|
|
if cont := it.stateIt.Next(true); !cont {
|
|
|
|
if it.stateIt.Error() != nil {
|
|
|
|
return it.stateIt.Error()
|
2016-02-16 04:37:00 -06:00
|
|
|
}
|
2015-12-28 07:20:37 -06:00
|
|
|
it.state, it.stateIt = nil, nil
|
2016-02-16 04:37:00 -06:00
|
|
|
return nil
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
// If the state trie node is an internal entry, leave as is
|
2017-02-22 16:49:34 -06:00
|
|
|
if !it.stateIt.Leaf() {
|
2016-02-16 04:37:00 -06:00
|
|
|
return nil
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
// Otherwise we've reached an account node, initiate data iteration
|
2021-09-28 03:48:07 -05:00
|
|
|
var account types.StateAccount
|
2023-08-24 03:47:42 -05:00
|
|
|
if err := rlp.DecodeBytes(it.stateIt.LeafBlob(), &account); err != nil {
|
2016-02-16 04:37:00 -06:00
|
|
|
return err
|
2015-12-28 07:20:37 -06: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 04:11:02 -05: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 06:09:40 -06:00
|
|
|
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root, it.state.trie)
|
2015-12-28 07:20:37 -06:00
|
|
|
if err != nil {
|
2016-02-16 04:37:00 -06:00
|
|
|
return err
|
2015-12-28 07:20:37 -06: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 14:31:45 -05:00
|
|
|
it.dataIt, err = dataTrie.NodeIterator(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-22 16:49:34 -06:00
|
|
|
if !it.dataIt.Next(true) {
|
2015-12-28 07:20:37 -06:00
|
|
|
it.dataIt = nil
|
|
|
|
}
|
2023-02-21 05:12:27 -06:00
|
|
|
if !bytes.Equal(account.CodeHash, types.EmptyCodeHash.Bytes()) {
|
2016-01-06 04:11:56 -06:00
|
|
|
it.codeHash = common.BytesToHash(account.CodeHash)
|
core/state: introduce code reader interface (#30816)
This PR introduces a `ContractCodeReader` interface with functions defined:
type ContractCodeReader interface {
Code(addr common.Address, codeHash common.Hash) ([]byte, error)
CodeSize(addr common.Address, codeHash common.Hash) (int, error)
}
This interface can be implemented in various ways. Although the codebase
currently includes only one implementation, additional implementations
could be created for different purposes and scenarios, such as a code
reader designed for the Verkle tree approach or one that reads code from
the witness.
*Notably, this interface modifies the function’s semantics. If the
contract code is not found, no error will be returned. An error should
only be returned in the event of an unexpected issue, primarily for
future implementations.*
The original state.Reader interface is extended with ContractCodeReader
methods, it gives us more flexibility to manipulate the reader with additional
logic on top, e.g. Hooks.
type Reader interface {
ContractCodeReader
StateReader
}
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2024-11-29 08:32:45 -06:00
|
|
|
it.code, err = it.state.reader.Code(address, common.BytesToHash(account.CodeHash))
|
2015-12-28 07:20:37 -06:00
|
|
|
if err != nil {
|
2016-02-16 04:37:00 -06:00
|
|
|
return fmt.Errorf("code %x: %v", account.CodeHash, err)
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
core/state: introduce code reader interface (#30816)
This PR introduces a `ContractCodeReader` interface with functions defined:
type ContractCodeReader interface {
Code(addr common.Address, codeHash common.Hash) ([]byte, error)
CodeSize(addr common.Address, codeHash common.Hash) (int, error)
}
This interface can be implemented in various ways. Although the codebase
currently includes only one implementation, additional implementations
could be created for different purposes and scenarios, such as a code
reader designed for the Verkle tree approach or one that reads code from
the witness.
*Notably, this interface modifies the function’s semantics. If the
contract code is not found, no error will be returned. An error should
only be returned in the event of an unexpected issue, primarily for
future implementations.*
The original state.Reader interface is extended with ContractCodeReader
methods, it gives us more flexibility to manipulate the reader with additional
logic on top, e.g. Hooks.
type Reader interface {
ContractCodeReader
StateReader
}
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2024-11-29 08:32:45 -06:00
|
|
|
if len(it.code) == 0 {
|
|
|
|
return fmt.Errorf("code is not found: %x", account.CodeHash)
|
|
|
|
}
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
2017-02-22 16:49:34 -06:00
|
|
|
it.accountHash = it.stateIt.Parent()
|
2016-02-16 04:37:00 -06:00
|
|
|
return nil
|
2015-12-28 07:20:37 -06: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 02:15:44 -05:00
|
|
|
func (it *nodeIterator) retrieve() bool {
|
2015-12-28 07:20:37 -06:00
|
|
|
// Clear out any previously set values
|
2017-02-22 16:49:34 -06:00
|
|
|
it.Hash = common.Hash{}
|
2015-12-28 07:20:37 -06: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 16:49:34 -06:00
|
|
|
it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
|
2016-01-08 05:46:45 -06:00
|
|
|
if it.Parent == (common.Hash{}) {
|
|
|
|
it.Parent = it.accountHash
|
|
|
|
}
|
2015-12-28 07:20:37 -06:00
|
|
|
case it.code != nil:
|
2017-02-22 16:49:34 -06:00
|
|
|
it.Hash, it.Parent = it.codeHash, it.accountHash
|
2015-12-28 07:20:37 -06:00
|
|
|
case it.stateIt != nil:
|
2017-02-22 16:49:34 -06:00
|
|
|
it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
|
2015-12-28 07:20:37 -06:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|