iterator.go 4.9 KB

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