github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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 "math/big" 23 24 "github.com/atheioschain/go-atheios/common" 25 "github.com/atheioschain/go-atheios/rlp" 26 "github.com/atheioschain/go-atheios/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 Entry interface{} // Current state entry being iterated (internal representation) 43 Parent common.Hash // Hash of the first full ancestor node (nil if current is the root) 44 45 Error error // Failure set in case of an internal error in the iterator 46 } 47 48 // NewNodeIterator creates an post-order state node iterator. 49 func NewNodeIterator(state *StateDB) *NodeIterator { 50 return &NodeIterator{ 51 state: state, 52 } 53 } 54 55 // Next moves the iterator to the next node, returning whether there are any 56 // further nodes. In case of an internal error this method returns false and 57 // sets the Error field to the encountered failure. 58 func (it *NodeIterator) Next() bool { 59 // If the iterator failed previously, don't do anything 60 if it.Error != nil { 61 return false 62 } 63 // Otherwise step forward with the iterator and report any errors 64 if err := it.step(); err != nil { 65 it.Error = err 66 return false 67 } 68 return it.retrieve() 69 } 70 71 // step moves the iterator to the next entry of the state trie. 72 func (it *NodeIterator) step() error { 73 // Abort if we reached the end of the iteration 74 if it.state == nil { 75 return nil 76 } 77 // Initialize the iterator if we've just started 78 if it.stateIt == nil { 79 it.stateIt = it.state.trie.NodeIterator() 80 } 81 // If we had data nodes previously, we surely have at least state nodes 82 if it.dataIt != nil { 83 if cont := it.dataIt.Next(); !cont { 84 if it.dataIt.Error != nil { 85 return it.dataIt.Error 86 } 87 it.dataIt = nil 88 } 89 return nil 90 } 91 // If we had source code previously, discard that 92 if it.code != nil { 93 it.code = nil 94 return nil 95 } 96 // Step to the next state trie node, terminating if we're out of nodes 97 if cont := it.stateIt.Next(); !cont { 98 if it.stateIt.Error != nil { 99 return it.stateIt.Error 100 } 101 it.state, it.stateIt = nil, nil 102 return nil 103 } 104 // If the state trie node is an internal entry, leave as is 105 if !it.stateIt.Leaf { 106 return nil 107 } 108 // Otherwise we've reached an account node, initiate data iteration 109 var account struct { 110 Nonce uint64 111 Balance *big.Int 112 Root common.Hash 113 CodeHash []byte 114 } 115 if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account); err != nil { 116 return err 117 } 118 dataTrie, err := trie.New(account.Root, it.state.db) 119 if err != nil { 120 return err 121 } 122 it.dataIt = trie.NewNodeIterator(dataTrie) 123 if !it.dataIt.Next() { 124 it.dataIt = nil 125 } 126 if !bytes.Equal(account.CodeHash, emptyCodeHash) { 127 it.codeHash = common.BytesToHash(account.CodeHash) 128 it.code, err = it.state.db.Get(account.CodeHash) 129 if err != nil { 130 return fmt.Errorf("code %x: %v", account.CodeHash, err) 131 } 132 } 133 it.accountHash = it.stateIt.Parent 134 return nil 135 } 136 137 // retrieve pulls and caches the current state entry the iterator is traversing. 138 // The method returns whether there are any more data left for inspection. 139 func (it *NodeIterator) retrieve() bool { 140 // Clear out any previously set values 141 it.Hash, it.Entry = common.Hash{}, nil 142 143 // If the iteration's done, return no available data 144 if it.state == nil { 145 return false 146 } 147 // Otherwise retrieve the current entry 148 switch { 149 case it.dataIt != nil: 150 it.Hash, it.Entry, it.Parent = it.dataIt.Hash, it.dataIt.Node, it.dataIt.Parent 151 if it.Parent == (common.Hash{}) { 152 it.Parent = it.accountHash 153 } 154 case it.code != nil: 155 it.Hash, it.Entry, it.Parent = it.codeHash, it.code, it.accountHash 156 case it.stateIt != nil: 157 it.Hash, it.Entry, it.Parent = it.stateIt.Hash, it.stateIt.Node, it.stateIt.Parent 158 } 159 return true 160 }