committer.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2019 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. "errors"
  19. "fmt"
  20. "sync"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "golang.org/x/crypto/sha3"
  24. )
  25. // leafChanSize is the size of the leafCh. It's a pretty arbitrary number, to allow
  26. // some parallelism but not incur too much memory overhead.
  27. const leafChanSize = 200
  28. // leaf represents a trie leaf value
  29. type leaf struct {
  30. size int // size of the rlp data (estimate)
  31. hash common.Hash // hash of rlp data
  32. node node // the node to commit
  33. }
  34. // committer is a type used for the trie Commit operation. A committer has some
  35. // internal preallocated temp space, and also a callback that is invoked when
  36. // leaves are committed. The leafs are passed through the `leafCh`, to allow
  37. // some level of parallelism.
  38. // By 'some level' of parallelism, it's still the case that all leaves will be
  39. // processed sequentially - onleaf will never be called in parallel or out of order.
  40. type committer struct {
  41. tmp sliceBuffer
  42. sha crypto.KeccakState
  43. onleaf LeafCallback
  44. leafCh chan *leaf
  45. }
  46. // committers live in a global sync.Pool
  47. var committerPool = sync.Pool{
  48. New: func() interface{} {
  49. return &committer{
  50. tmp: make(sliceBuffer, 0, 550), // cap is as large as a full fullNode.
  51. sha: sha3.NewLegacyKeccak256().(crypto.KeccakState),
  52. }
  53. },
  54. }
  55. // newCommitter creates a new committer or picks one from the pool.
  56. func newCommitter() *committer {
  57. return committerPool.Get().(*committer)
  58. }
  59. func returnCommitterToPool(h *committer) {
  60. h.onleaf = nil
  61. h.leafCh = nil
  62. committerPool.Put(h)
  63. }
  64. // commit collapses a node down into a hash node and inserts it into the database
  65. func (c *committer) Commit(n node, db *Database) (hashNode, error) {
  66. if db == nil {
  67. return nil, errors.New("no db provided")
  68. }
  69. h, err := c.commit(n, db)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return h.(hashNode), nil
  74. }
  75. // commit collapses a node down into a hash node and inserts it into the database
  76. func (c *committer) commit(n node, db *Database) (node, error) {
  77. // if this path is clean, use available cached data
  78. hash, dirty := n.cache()
  79. if hash != nil && !dirty {
  80. return hash, nil
  81. }
  82. // Commit children, then parent, and remove remove the dirty flag.
  83. switch cn := n.(type) {
  84. case *shortNode:
  85. // Commit child
  86. collapsed := cn.copy()
  87. // If the child is fullnode, recursively commit.
  88. // Otherwise it can only be hashNode or valueNode.
  89. if _, ok := cn.Val.(*fullNode); ok {
  90. childV, err := c.commit(cn.Val, db)
  91. if err != nil {
  92. return nil, err
  93. }
  94. collapsed.Val = childV
  95. }
  96. // The key needs to be copied, since we're delivering it to database
  97. collapsed.Key = hexToCompact(cn.Key)
  98. hashedNode := c.store(collapsed, db)
  99. if hn, ok := hashedNode.(hashNode); ok {
  100. return hn, nil
  101. }
  102. return collapsed, nil
  103. case *fullNode:
  104. hashedKids, err := c.commitChildren(cn, db)
  105. if err != nil {
  106. return nil, err
  107. }
  108. collapsed := cn.copy()
  109. collapsed.Children = hashedKids
  110. hashedNode := c.store(collapsed, db)
  111. if hn, ok := hashedNode.(hashNode); ok {
  112. return hn, nil
  113. }
  114. return collapsed, nil
  115. case hashNode:
  116. return cn, nil
  117. default:
  118. // nil, valuenode shouldn't be committed
  119. panic(fmt.Sprintf("%T: invalid node: %v", n, n))
  120. }
  121. }
  122. // commitChildren commits the children of the given fullnode
  123. func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, error) {
  124. var children [17]node
  125. for i := 0; i < 16; i++ {
  126. child := n.Children[i]
  127. if child == nil {
  128. continue
  129. }
  130. // If it's the hashed child, save the hash value directly.
  131. // Note: it's impossible that the child in range [0, 15]
  132. // is a valuenode.
  133. if hn, ok := child.(hashNode); ok {
  134. children[i] = hn
  135. continue
  136. }
  137. // Commit the child recursively and store the "hashed" value.
  138. // Note the returned node can be some embedded nodes, so it's
  139. // possible the type is not hashnode.
  140. hashed, err := c.commit(child, db)
  141. if err != nil {
  142. return children, err
  143. }
  144. children[i] = hashed
  145. }
  146. // For the 17th child, it's possible the type is valuenode.
  147. if n.Children[16] != nil {
  148. children[16] = n.Children[16]
  149. }
  150. return children, nil
  151. }
  152. // store hashes the node n and if we have a storage layer specified, it writes
  153. // the key/value pair to it and tracks any node->child references as well as any
  154. // node->external trie references.
  155. func (c *committer) store(n node, db *Database) node {
  156. // Larger nodes are replaced by their hash and stored in the database.
  157. var (
  158. hash, _ = n.cache()
  159. size int
  160. )
  161. if hash == nil {
  162. // This was not generated - must be a small node stored in the parent.
  163. // In theory we should apply the leafCall here if it's not nil(embedded
  164. // node usually contains value). But small value(less than 32bytes) is
  165. // not our target.
  166. return n
  167. } else {
  168. // We have the hash already, estimate the RLP encoding-size of the node.
  169. // The size is used for mem tracking, does not need to be exact
  170. size = estimateSize(n)
  171. }
  172. // If we're using channel-based leaf-reporting, send to channel.
  173. // The leaf channel will be active only when there an active leaf-callback
  174. if c.leafCh != nil {
  175. c.leafCh <- &leaf{
  176. size: size,
  177. hash: common.BytesToHash(hash),
  178. node: n,
  179. }
  180. } else if db != nil {
  181. // No leaf-callback used, but there's still a database. Do serial
  182. // insertion
  183. db.lock.Lock()
  184. db.insert(common.BytesToHash(hash), size, n)
  185. db.lock.Unlock()
  186. }
  187. return hash
  188. }
  189. // commitLoop does the actual insert + leaf callback for nodes.
  190. func (c *committer) commitLoop(db *Database) {
  191. for item := range c.leafCh {
  192. var (
  193. hash = item.hash
  194. size = item.size
  195. n = item.node
  196. )
  197. // We are pooling the trie nodes into an intermediate memory cache
  198. db.lock.Lock()
  199. db.insert(hash, size, n)
  200. db.lock.Unlock()
  201. if c.onleaf != nil {
  202. switch n := n.(type) {
  203. case *shortNode:
  204. if child, ok := n.Val.(valueNode); ok {
  205. c.onleaf(nil, nil, child, hash)
  206. }
  207. case *fullNode:
  208. // For children in range [0, 15], it's impossible
  209. // to contain valuenode. Only check the 17th child.
  210. if n.Children[16] != nil {
  211. c.onleaf(nil, nil, n.Children[16].(valueNode), hash)
  212. }
  213. }
  214. }
  215. }
  216. }
  217. func (c *committer) makeHashNode(data []byte) hashNode {
  218. n := make(hashNode, c.sha.Size())
  219. c.sha.Reset()
  220. c.sha.Write(data)
  221. c.sha.Read(n)
  222. return n
  223. }
  224. // estimateSize estimates the size of an rlp-encoded node, without actually
  225. // rlp-encoding it (zero allocs). This method has been experimentally tried, and with a trie
  226. // with 1000 leafs, the only errors above 1% are on small shortnodes, where this
  227. // method overestimates by 2 or 3 bytes (e.g. 37 instead of 35)
  228. func estimateSize(n node) int {
  229. switch n := n.(type) {
  230. case *shortNode:
  231. // A short node contains a compacted key, and a value.
  232. return 3 + len(n.Key) + estimateSize(n.Val)
  233. case *fullNode:
  234. // A full node contains up to 16 hashes (some nils), and a key
  235. s := 3
  236. for i := 0; i < 16; i++ {
  237. if child := n.Children[i]; child != nil {
  238. s += estimateSize(child)
  239. } else {
  240. s++
  241. }
  242. }
  243. return s
  244. case valueNode:
  245. return 1 + len(n)
  246. case hashNode:
  247. return 1 + len(n)
  248. default:
  249. panic(fmt.Sprintf("node type %T", n))
  250. }
  251. }