iterator.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. }
  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.
  46. func (it *NodeIterator) Next() bool {
  47. it.step()
  48. return it.retrieve()
  49. }
  50. // step moves the iterator to the next entry of the state trie.
  51. func (it *NodeIterator) step() {
  52. // Abort if we reached the end of the iteration
  53. if it.state == nil {
  54. return
  55. }
  56. // Initialize the iterator if we've just started
  57. if it.stateIt == nil {
  58. it.stateIt = trie.NewNodeIterator(it.state.trie.Trie)
  59. }
  60. // If we had data nodes previously, we surely have at least state nodes
  61. if it.dataIt != nil {
  62. if cont := it.dataIt.Next(); !cont {
  63. it.dataIt = nil
  64. }
  65. return
  66. }
  67. // If we had source code previously, discard that
  68. if it.code != nil {
  69. it.code = nil
  70. return
  71. }
  72. // Step to the next state trie node, terminating if we're out of nodes
  73. if cont := it.stateIt.Next(); !cont {
  74. it.state, it.stateIt = nil, nil
  75. return
  76. }
  77. // If the state trie node is an internal entry, leave as is
  78. if !it.stateIt.Leaf {
  79. return
  80. }
  81. // Otherwise we've reached an account node, initiate data iteration
  82. var account struct {
  83. Nonce uint64
  84. Balance *big.Int
  85. Root common.Hash
  86. CodeHash []byte
  87. }
  88. err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account)
  89. if err != nil {
  90. panic(err)
  91. }
  92. dataTrie, err := trie.New(account.Root, it.state.db)
  93. if err != nil {
  94. panic(err)
  95. }
  96. it.dataIt = trie.NewNodeIterator(dataTrie)
  97. if !it.dataIt.Next() {
  98. it.dataIt = nil
  99. }
  100. if bytes.Compare(account.CodeHash, emptyCodeHash) != 0 {
  101. it.codeHash = common.BytesToHash(account.CodeHash)
  102. it.code, err = it.state.db.Get(account.CodeHash)
  103. if err != nil {
  104. panic(fmt.Sprintf("code %x: %v", account.CodeHash, err))
  105. }
  106. }
  107. it.accountHash = it.stateIt.Parent
  108. }
  109. // retrieve pulls and caches the current state entry the iterator is traversing.
  110. // The method returns whether there are any more data left for inspection.
  111. func (it *NodeIterator) retrieve() bool {
  112. // Clear out any previously set values
  113. it.Hash, it.Entry = common.Hash{}, nil
  114. // If the iteration's done, return no available data
  115. if it.state == nil {
  116. return false
  117. }
  118. // Otherwise retrieve the current entry
  119. switch {
  120. case it.dataIt != nil:
  121. it.Hash, it.Entry, it.Parent = it.dataIt.Hash, it.dataIt.Node, it.dataIt.Parent
  122. if it.Parent == (common.Hash{}) {
  123. it.Parent = it.accountHash
  124. }
  125. case it.code != nil:
  126. it.Hash, it.Entry, it.Parent = it.codeHash, it.code, it.accountHash
  127. case it.stateIt != nil:
  128. it.Hash, it.Entry, it.Parent = it.stateIt.Hash, it.stateIt.Node, it.stateIt.Parent
  129. }
  130. return true
  131. }