hasher.go 5.9 KB

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