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