utils.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2022 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 trie
  17. // tracer tracks the changes of trie nodes. During the trie operations,
  18. // some nodes can be deleted from the trie, while these deleted nodes
  19. // won't be captured by trie.Hasher or trie.Committer. Thus, these deleted
  20. // nodes won't be removed from the disk at all. Tracer is an auxiliary tool
  21. // used to track all insert and delete operations of trie and capture all
  22. // deleted nodes eventually.
  23. //
  24. // The changed nodes can be mainly divided into two categories: the leaf
  25. // node and intermediate node. The former is inserted/deleted by callers
  26. // while the latter is inserted/deleted in order to follow the rule of trie.
  27. // This tool can track all of them no matter the node is embedded in its
  28. // parent or not, but valueNode is never tracked.
  29. //
  30. // Besides, it's also used for recording the original value of the nodes
  31. // when they are resolved from the disk. The pre-value of the nodes will
  32. // be used to construct reverse-diffs in the future.
  33. //
  34. // Note tracer is not thread-safe, callers should be responsible for handling
  35. // the concurrency issues by themselves.
  36. type tracer struct {
  37. insert map[string]struct{}
  38. delete map[string]struct{}
  39. origin map[string][]byte
  40. }
  41. // newTracer initializes the tracer for capturing trie changes.
  42. func newTracer() *tracer {
  43. return &tracer{
  44. insert: make(map[string]struct{}),
  45. delete: make(map[string]struct{}),
  46. origin: make(map[string][]byte),
  47. }
  48. }
  49. /*
  50. // onRead tracks the newly loaded trie node and caches the rlp-encoded blob internally.
  51. // Don't change the value outside of function since it's not deep-copied.
  52. func (t *tracer) onRead(key []byte, val []byte) {
  53. // Tracer isn't used right now, remove this check later.
  54. if t == nil {
  55. return
  56. }
  57. t.origin[string(key)] = val
  58. }
  59. */
  60. // onInsert tracks the newly inserted trie node. If it's already in the deletion set
  61. // (resurrected node), then just wipe it from the deletion set as the "untouched".
  62. func (t *tracer) onInsert(key []byte) {
  63. // Tracer isn't used right now, remove this check later.
  64. if t == nil {
  65. return
  66. }
  67. if _, present := t.delete[string(key)]; present {
  68. delete(t.delete, string(key))
  69. return
  70. }
  71. t.insert[string(key)] = struct{}{}
  72. }
  73. // onDelete tracks the newly deleted trie node. If it's already
  74. // in the addition set, then just wipe it from the addition set
  75. // as it's untouched.
  76. func (t *tracer) onDelete(key []byte) {
  77. // Tracer isn't used right now, remove this check later.
  78. if t == nil {
  79. return
  80. }
  81. if _, present := t.insert[string(key)]; present {
  82. delete(t.insert, string(key))
  83. return
  84. }
  85. t.delete[string(key)] = struct{}{}
  86. }
  87. // insertList returns the tracked inserted trie nodes in list format.
  88. func (t *tracer) insertList() [][]byte {
  89. // Tracer isn't used right now, remove this check later.
  90. if t == nil {
  91. return nil
  92. }
  93. var ret [][]byte
  94. for key := range t.insert {
  95. ret = append(ret, []byte(key))
  96. }
  97. return ret
  98. }
  99. // deleteList returns the tracked deleted trie nodes in list format.
  100. func (t *tracer) deleteList() [][]byte {
  101. // Tracer isn't used right now, remove this check later.
  102. if t == nil {
  103. return nil
  104. }
  105. var ret [][]byte
  106. for key := range t.delete {
  107. ret = append(ret, []byte(key))
  108. }
  109. return ret
  110. }
  111. /*
  112. // getPrev returns the cached original value of the specified node.
  113. func (t *tracer) getPrev(key []byte) []byte {
  114. // Don't panic on uninitialized tracer, it's possible in testing.
  115. if t == nil {
  116. return nil
  117. }
  118. return t.origin[string(key)]
  119. }
  120. */
  121. // reset clears the content tracked by tracer.
  122. func (t *tracer) reset() {
  123. // Tracer isn't used right now, remove this check later.
  124. if t == nil {
  125. return
  126. }
  127. t.insert = make(map[string]struct{})
  128. t.delete = make(map[string]struct{})
  129. t.origin = make(map[string][]byte)
  130. }
  131. // copy returns a deep copied tracer instance.
  132. func (t *tracer) copy() *tracer {
  133. // Tracer isn't used right now, remove this check later.
  134. if t == nil {
  135. return nil
  136. }
  137. var (
  138. insert = make(map[string]struct{})
  139. delete = make(map[string]struct{})
  140. origin = make(map[string][]byte)
  141. )
  142. for key := range t.insert {
  143. insert[key] = struct{}{}
  144. }
  145. for key := range t.delete {
  146. delete[key] = struct{}{}
  147. }
  148. for key, val := range t.origin {
  149. origin[key] = val
  150. }
  151. return &tracer{
  152. insert: insert,
  153. delete: delete,
  154. origin: origin,
  155. }
  156. }