committer.go 7.9 KB

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