github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/core/state/iterator.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package state 18 19 import ( 20 "bytes" 21 "fmt" 22 23 "github.com/tirogen/go-ethereum/common" 24 "github.com/tirogen/go-ethereum/core/types" 25 "github.com/tirogen/go-ethereum/rlp" 26 "github.com/tirogen/go-ethereum/trie" 27 ) 28 29 // NodeIterator is an iterator to traverse the entire state trie post-order, 30 // including all of the contract code and contract state tries. 31 type NodeIterator struct { 32 state *StateDB // State being iterated 33 34 stateIt trie.NodeIterator // Primary iterator for the global state trie 35 dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract 36 37 accountHash common.Hash // Hash of the node containing the account 38 codeHash common.Hash // Hash of the contract source code 39 code []byte // Source code associated with a contract 40 41 Hash common.Hash // Hash of the current entry being iterated (nil if not standalone) 42 Parent common.Hash // Hash of the first full ancestor node (nil if current is the root) 43 44 Error error // Failure set in case of an internal error in the iterator 45 } 46 47 // NewNodeIterator creates an post-order state node iterator. 48 func NewNodeIterator(state *StateDB) *NodeIterator { 49 return &NodeIterator{ 50 state: state, 51 } 52 } 53 54 // Next moves the iterator to the next node, returning whether there are any 55 // further nodes. In case of an internal error this method returns false and 56 // sets the Error field to the encountered failure. 57 func (it *NodeIterator) Next() bool { 58 // If the iterator failed previously, don't do anything 59 if it.Error != nil { 60 return false 61 } 62 // Otherwise step forward with the iterator and report any errors 63 if err := it.step(); err != nil { 64 it.Error = err 65 return false 66 } 67 return it.retrieve() 68 } 69 70 // step moves the iterator to the next entry of the state trie. 71 func (it *NodeIterator) step() error { 72 // Abort if we reached the end of the iteration 73 if it.state == nil { 74 return nil 75 } 76 // Initialize the iterator if we've just started 77 if it.stateIt == nil { 78 it.stateIt = it.state.trie.NodeIterator(nil) 79 } 80 // If we had data nodes previously, we surely have at least state nodes 81 if it.dataIt != nil { 82 if cont := it.dataIt.Next(true); !cont { 83 if it.dataIt.Error() != nil { 84 return it.dataIt.Error() 85 } 86 it.dataIt = nil 87 } 88 return nil 89 } 90 // If we had source code previously, discard that 91 if it.code != nil { 92 it.code = nil 93 return nil 94 } 95 // Step to the next state trie node, terminating if we're out of nodes 96 if cont := it.stateIt.Next(true); !cont { 97 if it.stateIt.Error() != nil { 98 return it.stateIt.Error() 99 } 100 it.state, it.stateIt = nil, nil 101 return nil 102 } 103 // If the state trie node is an internal entry, leave as is 104 if !it.stateIt.Leaf() { 105 return nil 106 } 107 // Otherwise we've reached an account node, initiate data iteration 108 var account types.StateAccount 109 if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil { 110 return err 111 } 112 dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, common.BytesToHash(it.stateIt.LeafKey()), account.Root) 113 if err != nil { 114 return err 115 } 116 it.dataIt = dataTrie.NodeIterator(nil) 117 if !it.dataIt.Next(true) { 118 it.dataIt = nil 119 } 120 if !bytes.Equal(account.CodeHash, emptyCodeHash) { 121 it.codeHash = common.BytesToHash(account.CodeHash) 122 addrHash := common.BytesToHash(it.stateIt.LeafKey()) 123 it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash)) 124 if err != nil { 125 return fmt.Errorf("code %x: %v", account.CodeHash, err) 126 } 127 } 128 it.accountHash = it.stateIt.Parent() 129 return nil 130 } 131 132 // retrieve pulls and caches the current state entry the iterator is traversing. 133 // The method returns whether there are any more data left for inspection. 134 func (it *NodeIterator) retrieve() bool { 135 // Clear out any previously set values 136 it.Hash = common.Hash{} 137 138 // If the iteration's done, return no available data 139 if it.state == nil { 140 return false 141 } 142 // Otherwise retrieve the current entry 143 switch { 144 case it.dataIt != nil: 145 it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent() 146 if it.Parent == (common.Hash{}) { 147 it.Parent = it.accountHash 148 } 149 case it.code != nil: 150 it.Hash, it.Parent = it.codeHash, it.accountHash 151 case it.stateIt != nil: 152 it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent() 153 } 154 return true 155 }