hasher.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  29. // hashers live in a global pool.
  30. var hasherPool = sync.Pool{
  31. New: func() interface{} {
  32. return &hasher{tmp: new(bytes.Buffer), sha: sha3.NewKeccak256()}
  33. },
  34. }
  35. func newHasher() *hasher {
  36. return hasherPool.Get().(*hasher)
  37. }
  38. func returnHasherToPool(h *hasher) {
  39. hasherPool.Put(h)
  40. }
  41. // hash collapses a node down into a hash node, also returning a copy of the
  42. // original node initialzied with the computed hash to replace the original one.
  43. func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, node, error) {
  44. // If we're not storing the node, just hashing, use avaialble cached data
  45. if hash, dirty := n.cache(); hash != nil && (db == nil || !dirty) {
  46. return hash, n, nil
  47. }
  48. // Trie not processed yet or needs storage, walk the children
  49. collapsed, cached, err := h.hashChildren(n, db)
  50. if err != nil {
  51. return hashNode{}, n, err
  52. }
  53. hashed, err := h.store(collapsed, db, force)
  54. if err != nil {
  55. return hashNode{}, n, err
  56. }
  57. // Cache the hash and RLP blob of the ndoe for later reuse
  58. if hash, ok := hashed.(hashNode); ok && !force {
  59. switch cached := cached.(type) {
  60. case shortNode:
  61. cached.hash = hash
  62. if db != nil {
  63. cached.dirty = false
  64. }
  65. return hashed, cached, nil
  66. case fullNode:
  67. cached.hash = hash
  68. if db != nil {
  69. cached.dirty = false
  70. }
  71. return hashed, cached, nil
  72. }
  73. }
  74. return hashed, cached, nil
  75. }
  76. // hashChildren replaces the children of a node with their hashes if the encoded
  77. // size of the child is larger than a hash, returning the collapsed node as well
  78. // as a replacement for the original node with the child hashes cached in.
  79. func (h *hasher) hashChildren(original node, db DatabaseWriter) (node, node, error) {
  80. var err error
  81. switch n := original.(type) {
  82. case shortNode:
  83. // Hash the short node's child, caching the newly hashed subtree
  84. cached := n
  85. cached.Key = common.CopyBytes(cached.Key)
  86. n.Key = compactEncode(n.Key)
  87. if _, ok := n.Val.(valueNode); !ok {
  88. if n.Val, cached.Val, err = h.hash(n.Val, db, false); err != nil {
  89. return n, original, err
  90. }
  91. }
  92. if n.Val == nil {
  93. n.Val = valueNode(nil) // Ensure that nil children are encoded as empty strings.
  94. }
  95. return n, cached, nil
  96. case fullNode:
  97. // Hash the full node's children, caching the newly hashed subtrees
  98. cached := fullNode{dirty: n.dirty}
  99. for i := 0; i < 16; i++ {
  100. if n.Children[i] != nil {
  101. if n.Children[i], cached.Children[i], err = h.hash(n.Children[i], db, false); err != nil {
  102. return n, original, err
  103. }
  104. } else {
  105. n.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings.
  106. }
  107. }
  108. cached.Children[16] = n.Children[16]
  109. if n.Children[16] == nil {
  110. n.Children[16] = valueNode(nil)
  111. }
  112. return n, cached, nil
  113. default:
  114. // Value and hash nodes don't have children so they're left as were
  115. return n, original, nil
  116. }
  117. }
  118. func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
  119. // Don't store hashes or empty nodes.
  120. if _, isHash := n.(hashNode); n == nil || isHash {
  121. return n, nil
  122. }
  123. // Generate the RLP encoding of the node
  124. h.tmp.Reset()
  125. if err := rlp.Encode(h.tmp, n); err != nil {
  126. panic("encode error: " + err.Error())
  127. }
  128. if h.tmp.Len() < 32 && !force {
  129. return n, nil // Nodes smaller than 32 bytes are stored inside their parent
  130. }
  131. // Larger nodes are replaced by their hash and stored in the database.
  132. hash, _ := n.cache()
  133. if hash == nil {
  134. h.sha.Reset()
  135. h.sha.Write(h.tmp.Bytes())
  136. hash = hashNode(h.sha.Sum(nil))
  137. }
  138. if db != nil {
  139. return hash, db.Put(hash, h.tmp.Bytes())
  140. }
  141. return hash, nil
  142. }