iterator.go 3.8 KB

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