iterator.go 5.0 KB

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