hasher.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2016 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. import (
  18. "bytes"
  19. "hash"
  20. "sync"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto/sha3"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. type hasher struct {
  26. tmp *bytes.Buffer
  27. sha hash.Hash
  28. cachegen uint16
  29. cachelimit uint16
  30. onleaf LeafCallback
  31. }
  32. // hashers live in a global db.
  33. var hasherPool = sync.Pool{
  34. New: func() interface{} {
  35. return &hasher{tmp: new(bytes.Buffer), sha: sha3.NewKeccak256()}
  36. },
  37. }
  38. func newHasher(cachegen, cachelimit uint16, onleaf LeafCallback) *hasher {
  39. h := hasherPool.Get().(*hasher)
  40. h.cachegen, h.cachelimit, h.onleaf = cachegen, cachelimit, onleaf
  41. return h
  42. }
  43. func returnHasherToPool(h *hasher) {
  44. hasherPool.Put(h)
  45. }
  46. // hash collapses a node down into a hash node, also returning a copy of the
  47. // original node initialized with the computed hash to replace the original one.
  48. func (h *hasher) hash(n node, db *Database, force bool) (node, node, error) {
  49. // If we're not storing the node, just hashing, use available cached data
  50. if hash, dirty := n.cache(); hash != nil {
  51. if db == nil {
  52. return hash, n, nil
  53. }
  54. if n.canUnload(h.cachegen, h.cachelimit) {
  55. // Unload the node from cache. All of its subnodes will have a lower or equal
  56. // cache generation number.
  57. cacheUnloadCounter.Inc(1)
  58. return hash, hash, nil
  59. }
  60. if !dirty {
  61. return hash, n, nil
  62. }
  63. }
  64. // Trie not processed yet or needs storage, walk the children
  65. collapsed, cached, err := h.hashChildren(n, db)
  66. if err != nil {
  67. return hashNode{}, n, err
  68. }
  69. hashed, err := h.store(collapsed, db, force)
  70. if err != nil {
  71. return hashNode{}, n, err
  72. }
  73. // Cache the hash of the node for later reuse and remove
  74. // the dirty flag in commit mode. It's fine to assign these values directly
  75. // without copying the node first because hashChildren copies it.
  76. cachedHash, _ := hashed.(hashNode)
  77. switch cn := cached.(type) {
  78. case *shortNode:
  79. cn.flags.hash = cachedHash
  80. if db != nil {
  81. cn.flags.dirty = false
  82. }
  83. case *fullNode:
  84. cn.flags.hash = cachedHash
  85. if db != nil {
  86. cn.flags.dirty = false
  87. }
  88. }
  89. return hashed, cached, nil
  90. }
  91. // hashChildren replaces the children of a node with their hashes if the encoded
  92. // size of the child is larger than a hash, returning the collapsed node as well
  93. // as a replacement for the original node with the child hashes cached in.
  94. func (h *hasher) hashChildren(original node, db *Database) (node, node, error) {
  95. var err error
  96. switch n := original.(type) {
  97. case *shortNode:
  98. // Hash the short node's child, caching the newly hashed subtree
  99. collapsed, cached := n.copy(), n.copy()
  100. collapsed.Key = hexToCompact(n.Key)
  101. cached.Key = common.CopyBytes(n.Key)
  102. if _, ok := n.Val.(valueNode); !ok {
  103. collapsed.Val, cached.Val, err = h.hash(n.Val, db, false)
  104. if err != nil {
  105. return original, original, err
  106. }
  107. }
  108. if collapsed.Val == nil {
  109. collapsed.Val = valueNode(nil) // Ensure that nil children are encoded as empty strings.
  110. }
  111. return collapsed, cached, nil
  112. case *fullNode:
  113. // Hash the full node's children, caching the newly hashed subtrees
  114. collapsed, cached := n.copy(), n.copy()
  115. for i := 0; i < 16; i++ {
  116. if n.Children[i] != nil {
  117. collapsed.Children[i], cached.Children[i], err = h.hash(n.Children[i], db, false)
  118. if err != nil {
  119. return original, original, err
  120. }
  121. } else {
  122. collapsed.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings.
  123. }
  124. }
  125. cached.Children[16] = n.Children[16]
  126. if collapsed.Children[16] == nil {
  127. collapsed.Children[16] = valueNode(nil)
  128. }
  129. return collapsed, cached, nil
  130. default:
  131. // Value and hash nodes don't have children so they're left as were
  132. return n, original, nil
  133. }
  134. }
  135. // store hashes the node n and if we have a storage layer specified, it writes
  136. // the key/value pair to it and tracks any node->child references as well as any
  137. // node->external trie references.
  138. func (h *hasher) store(n node, db *Database, force bool) (node, error) {
  139. // Don't store hashes or empty nodes.
  140. if _, isHash := n.(hashNode); n == nil || isHash {
  141. return n, nil
  142. }
  143. // Generate the RLP encoding of the node
  144. h.tmp.Reset()
  145. if err := rlp.Encode(h.tmp, n); err != nil {
  146. panic("encode error: " + err.Error())
  147. }
  148. if h.tmp.Len() < 32 && !force {
  149. return n, nil // Nodes smaller than 32 bytes are stored inside their parent
  150. }
  151. // Larger nodes are replaced by their hash and stored in the database.
  152. hash, _ := n.cache()
  153. if hash == nil {
  154. h.sha.Reset()
  155. h.sha.Write(h.tmp.Bytes())
  156. hash = hashNode(h.sha.Sum(nil))
  157. }
  158. if db != nil {
  159. // We are pooling the trie nodes into an intermediate memory cache
  160. db.lock.Lock()
  161. hash := common.BytesToHash(hash)
  162. db.insert(hash, h.tmp.Bytes())
  163. // Track all direct parent->child node references
  164. switch n := n.(type) {
  165. case *shortNode:
  166. if child, ok := n.Val.(hashNode); ok {
  167. db.reference(common.BytesToHash(child), hash)
  168. }
  169. case *fullNode:
  170. for i := 0; i < 16; i++ {
  171. if child, ok := n.Children[i].(hashNode); ok {
  172. db.reference(common.BytesToHash(child), hash)
  173. }
  174. }
  175. }
  176. db.lock.Unlock()
  177. // Track external references from account->storage trie
  178. if h.onleaf != nil {
  179. switch n := n.(type) {
  180. case *shortNode:
  181. if child, ok := n.Val.(valueNode); ok && child != nil {
  182. h.onleaf(child, hash)
  183. }
  184. case *fullNode:
  185. for i := 0; i < 16; i++ {
  186. if child, ok := n.Children[i].(valueNode); ok && child != nil {
  187. h.onleaf(child, hash)
  188. }
  189. }
  190. }
  191. }
  192. }
  193. return hash, nil
  194. }