iterator.go 4.9 KB

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