hasher.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // calculator is a utility used by the hasher to calculate the hash value of the tree node.
  26. type calculator struct {
  27. sha hash.Hash
  28. buffer *bytes.Buffer
  29. }
  30. // calculatorPool is a set of temporary calculators that may be individually saved and retrieved.
  31. var calculatorPool = sync.Pool{
  32. New: func() interface{} {
  33. return &calculator{buffer: new(bytes.Buffer), sha: sha3.NewKeccak256()}
  34. },
  35. }
  36. // hasher hasher is used to calculate the hash value of the whole tree.
  37. type hasher struct {
  38. cachegen uint16
  39. cachelimit uint16
  40. threaded bool
  41. mu sync.Mutex
  42. }
  43. func newHasher(cachegen, cachelimit uint16) *hasher {
  44. h := &hasher{
  45. cachegen: cachegen,
  46. cachelimit: cachelimit,
  47. }
  48. return h
  49. }
  50. // newCalculator retrieves a cleaned calculator from calculator pool.
  51. func (h *hasher) newCalculator() *calculator {
  52. calculator := calculatorPool.Get().(*calculator)
  53. calculator.buffer.Reset()
  54. calculator.sha.Reset()
  55. return calculator
  56. }
  57. // returnCalculator returns a no longer used calculator to the pool.
  58. func (h *hasher) returnCalculator(calculator *calculator) {
  59. calculatorPool.Put(calculator)
  60. }
  61. // hash collapses a node down into a hash node, also returning a copy of the
  62. // original node initialized with the computed hash to replace the original one.
  63. func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, node, error) {
  64. // If we're not storing the node, just hashing, use available cached data
  65. if hash, dirty := n.cache(); hash != nil {
  66. if db == nil {
  67. return hash, n, nil
  68. }
  69. if n.canUnload(h.cachegen, h.cachelimit) {
  70. // Unload the node from cache. All of its subnodes will have a lower or equal
  71. // cache generation number.
  72. cacheUnloadCounter.Inc(1)
  73. return hash, hash, nil
  74. }
  75. if !dirty {
  76. return hash, n, nil
  77. }
  78. }
  79. // Trie not processed yet or needs storage, walk the children
  80. collapsed, cached, err := h.hashChildren(n, db)
  81. if err != nil {
  82. return hashNode{}, n, err
  83. }
  84. hashed, err := h.store(collapsed, db, force)
  85. if err != nil {
  86. return hashNode{}, n, err
  87. }
  88. // Cache the hash of the node for later reuse and remove
  89. // the dirty flag in commit mode. It's fine to assign these values directly
  90. // without copying the node first because hashChildren copies it.
  91. cachedHash, _ := hashed.(hashNode)
  92. switch cn := cached.(type) {
  93. case *shortNode:
  94. cn.flags.hash = cachedHash
  95. if db != nil {
  96. cn.flags.dirty = false
  97. }
  98. case *fullNode:
  99. cn.flags.hash = cachedHash
  100. if db != nil {
  101. cn.flags.dirty = false
  102. }
  103. }
  104. return hashed, cached, nil
  105. }
  106. // hashChildren replaces the children of a node with their hashes if the encoded
  107. // size of the child is larger than a hash, returning the collapsed node as well
  108. // as a replacement for the original node with the child hashes cached in.
  109. func (h *hasher) hashChildren(original node, db DatabaseWriter) (node, node, error) {
  110. var err error
  111. switch n := original.(type) {
  112. case *shortNode:
  113. // Hash the short node's child, caching the newly hashed subtree
  114. collapsed, cached := n.copy(), n.copy()
  115. collapsed.Key = hexToCompact(n.Key)
  116. cached.Key = common.CopyBytes(n.Key)
  117. if _, ok := n.Val.(valueNode); !ok {
  118. collapsed.Val, cached.Val, err = h.hash(n.Val, db, false)
  119. if err != nil {
  120. return original, original, err
  121. }
  122. }
  123. if collapsed.Val == nil {
  124. collapsed.Val = valueNode(nil) // Ensure that nil children are encoded as empty strings.
  125. }
  126. return collapsed, cached, nil
  127. case *fullNode:
  128. // Hash the full node's children, caching the newly hashed subtrees
  129. collapsed, cached := n.copy(), n.copy()
  130. // hashChild is a helper to hash a single child, which is called either on the
  131. // same thread as the caller or in a goroutine for the toplevel branching.
  132. hashChild := func(index int, wg *sync.WaitGroup) {
  133. if wg != nil {
  134. defer wg.Done()
  135. }
  136. // Ensure that nil children are encoded as empty strings.
  137. if collapsed.Children[index] == nil {
  138. collapsed.Children[index] = valueNode(nil)
  139. return
  140. }
  141. // Hash all other children properly
  142. var herr error
  143. collapsed.Children[index], cached.Children[index], herr = h.hash(n.Children[index], db, false)
  144. if herr != nil {
  145. h.mu.Lock() // rarely if ever locked, no congenstion
  146. err = herr
  147. h.mu.Unlock()
  148. }
  149. }
  150. // If we're not running in threaded mode yet, span a goroutine for each child
  151. if !h.threaded {
  152. // Disable further threading
  153. h.threaded = true
  154. // Hash all the children concurrently
  155. var wg sync.WaitGroup
  156. for i := 0; i < 16; i++ {
  157. wg.Add(1)
  158. go hashChild(i, &wg)
  159. }
  160. wg.Wait()
  161. // Reenable threading for subsequent hash calls
  162. h.threaded = false
  163. } else {
  164. for i := 0; i < 16; i++ {
  165. hashChild(i, nil)
  166. }
  167. }
  168. if err != nil {
  169. return original, original, err
  170. }
  171. cached.Children[16] = n.Children[16]
  172. if collapsed.Children[16] == nil {
  173. collapsed.Children[16] = valueNode(nil)
  174. }
  175. return collapsed, cached, nil
  176. default:
  177. // Value and hash nodes don't have children so they're left as were
  178. return n, original, nil
  179. }
  180. }
  181. func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
  182. // Don't store hashes or empty nodes.
  183. if _, isHash := n.(hashNode); n == nil || isHash {
  184. return n, nil
  185. }
  186. calculator := h.newCalculator()
  187. defer h.returnCalculator(calculator)
  188. // Generate the RLP encoding of the node
  189. if err := rlp.Encode(calculator.buffer, n); err != nil {
  190. panic("encode error: " + err.Error())
  191. }
  192. if calculator.buffer.Len() < 32 && !force {
  193. return n, nil // Nodes smaller than 32 bytes are stored inside their parent
  194. }
  195. // Larger nodes are replaced by their hash and stored in the database.
  196. hash, _ := n.cache()
  197. if hash == nil {
  198. calculator.sha.Write(calculator.buffer.Bytes())
  199. hash = hashNode(calculator.sha.Sum(nil))
  200. }
  201. if db != nil {
  202. // db might be a leveldb batch, which is not safe for concurrent writes
  203. h.mu.Lock()
  204. err := db.Put(hash, calculator.buffer.Bytes())
  205. h.mu.Unlock()
  206. return hash, err
  207. }
  208. return hash, nil
  209. }