stacktrie.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // Copyright 2020 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. "bufio"
  19. "bytes"
  20. "encoding/gob"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "sync"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/rlp"
  29. )
  30. var ErrCommitDisabled = errors.New("no database for committing")
  31. var stPool = sync.Pool{
  32. New: func() interface{} {
  33. return NewStackTrie(nil)
  34. },
  35. }
  36. func stackTrieFromPool(db ethdb.KeyValueWriter) *StackTrie {
  37. st := stPool.Get().(*StackTrie)
  38. st.db = db
  39. return st
  40. }
  41. func returnToPool(st *StackTrie) {
  42. st.Reset()
  43. stPool.Put(st)
  44. }
  45. // StackTrie is a trie implementation that expects keys to be inserted
  46. // in order. Once it determines that a subtree will no longer be inserted
  47. // into, it will hash it and free up the memory it uses.
  48. type StackTrie struct {
  49. nodeType uint8 // node type (as in branch, ext, leaf)
  50. val []byte // value contained by this node if it's a leaf
  51. key []byte // key chunk covered by this (full|ext) node
  52. keyOffset int // offset of the key chunk inside a full key
  53. children [16]*StackTrie // list of children (for fullnodes and exts)
  54. db ethdb.KeyValueWriter // Pointer to the commit db, can be nil
  55. }
  56. // NewStackTrie allocates and initializes an empty trie.
  57. func NewStackTrie(db ethdb.KeyValueWriter) *StackTrie {
  58. return &StackTrie{
  59. nodeType: emptyNode,
  60. db: db,
  61. }
  62. }
  63. // NewFromBinary initialises a serialized stacktrie with the given db.
  64. func NewFromBinary(data []byte, db ethdb.KeyValueWriter) (*StackTrie, error) {
  65. var st StackTrie
  66. if err := st.UnmarshalBinary(data); err != nil {
  67. return nil, err
  68. }
  69. // If a database is used, we need to recursively add it to every child
  70. if db != nil {
  71. st.setDb(db)
  72. }
  73. return &st, nil
  74. }
  75. // MarshalBinary implements encoding.BinaryMarshaler
  76. func (st *StackTrie) MarshalBinary() (data []byte, err error) {
  77. var (
  78. b bytes.Buffer
  79. w = bufio.NewWriter(&b)
  80. )
  81. if err := gob.NewEncoder(w).Encode(struct {
  82. Nodetype uint8
  83. Val []byte
  84. Key []byte
  85. KeyOffset uint8
  86. }{
  87. st.nodeType,
  88. st.val,
  89. st.key,
  90. uint8(st.keyOffset),
  91. }); err != nil {
  92. return nil, err
  93. }
  94. for _, child := range st.children {
  95. if child == nil {
  96. w.WriteByte(0)
  97. continue
  98. }
  99. w.WriteByte(1)
  100. if childData, err := child.MarshalBinary(); err != nil {
  101. return nil, err
  102. } else {
  103. w.Write(childData)
  104. }
  105. }
  106. w.Flush()
  107. return b.Bytes(), nil
  108. }
  109. // UnmarshalBinary implements encoding.BinaryUnmarshaler
  110. func (st *StackTrie) UnmarshalBinary(data []byte) error {
  111. r := bytes.NewReader(data)
  112. return st.unmarshalBinary(r)
  113. }
  114. func (st *StackTrie) unmarshalBinary(r io.Reader) error {
  115. var dec struct {
  116. Nodetype uint8
  117. Val []byte
  118. Key []byte
  119. KeyOffset uint8
  120. }
  121. gob.NewDecoder(r).Decode(&dec)
  122. st.nodeType = dec.Nodetype
  123. st.val = dec.Val
  124. st.key = dec.Key
  125. st.keyOffset = int(dec.KeyOffset)
  126. var hasChild = make([]byte, 1)
  127. for i := range st.children {
  128. if _, err := r.Read(hasChild); err != nil {
  129. return err
  130. } else if hasChild[0] == 0 {
  131. continue
  132. }
  133. var child StackTrie
  134. child.unmarshalBinary(r)
  135. st.children[i] = &child
  136. }
  137. return nil
  138. }
  139. func (st *StackTrie) setDb(db ethdb.KeyValueWriter) {
  140. st.db = db
  141. for _, child := range st.children {
  142. if child != nil {
  143. child.setDb(db)
  144. }
  145. }
  146. }
  147. func newLeaf(ko int, key, val []byte, db ethdb.KeyValueWriter) *StackTrie {
  148. st := stackTrieFromPool(db)
  149. st.nodeType = leafNode
  150. st.keyOffset = ko
  151. st.key = append(st.key, key[ko:]...)
  152. st.val = val
  153. return st
  154. }
  155. func newExt(ko int, key []byte, child *StackTrie, db ethdb.KeyValueWriter) *StackTrie {
  156. st := stackTrieFromPool(db)
  157. st.nodeType = extNode
  158. st.keyOffset = ko
  159. st.key = append(st.key, key[ko:]...)
  160. st.children[0] = child
  161. return st
  162. }
  163. // List all values that StackTrie#nodeType can hold
  164. const (
  165. emptyNode = iota
  166. branchNode
  167. extNode
  168. leafNode
  169. hashedNode
  170. )
  171. // TryUpdate inserts a (key, value) pair into the stack trie
  172. func (st *StackTrie) TryUpdate(key, value []byte) error {
  173. k := keybytesToHex(key)
  174. if len(value) == 0 {
  175. panic("deletion not supported")
  176. }
  177. st.insert(k[:len(k)-1], value)
  178. return nil
  179. }
  180. func (st *StackTrie) Update(key, value []byte) {
  181. if err := st.TryUpdate(key, value); err != nil {
  182. log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
  183. }
  184. }
  185. func (st *StackTrie) Reset() {
  186. st.db = nil
  187. st.key = st.key[:0]
  188. st.val = nil
  189. for i := range st.children {
  190. st.children[i] = nil
  191. }
  192. st.nodeType = emptyNode
  193. st.keyOffset = 0
  194. }
  195. // Helper function that, given a full key, determines the index
  196. // at which the chunk pointed by st.keyOffset is different from
  197. // the same chunk in the full key.
  198. func (st *StackTrie) getDiffIndex(key []byte) int {
  199. diffindex := 0
  200. for ; diffindex < len(st.key) && st.key[diffindex] == key[st.keyOffset+diffindex]; diffindex++ {
  201. }
  202. return diffindex
  203. }
  204. // Helper function to that inserts a (key, value) pair into
  205. // the trie.
  206. func (st *StackTrie) insert(key, value []byte) {
  207. switch st.nodeType {
  208. case branchNode: /* Branch */
  209. idx := int(key[st.keyOffset])
  210. // Unresolve elder siblings
  211. for i := idx - 1; i >= 0; i-- {
  212. if st.children[i] != nil {
  213. if st.children[i].nodeType != hashedNode {
  214. st.children[i].hash()
  215. }
  216. break
  217. }
  218. }
  219. // Add new child
  220. if st.children[idx] == nil {
  221. st.children[idx] = stackTrieFromPool(st.db)
  222. st.children[idx].keyOffset = st.keyOffset + 1
  223. }
  224. st.children[idx].insert(key, value)
  225. case extNode: /* Ext */
  226. // Compare both key chunks and see where they differ
  227. diffidx := st.getDiffIndex(key)
  228. // Check if chunks are identical. If so, recurse into
  229. // the child node. Otherwise, the key has to be split
  230. // into 1) an optional common prefix, 2) the fullnode
  231. // representing the two differing path, and 3) a leaf
  232. // for each of the differentiated subtrees.
  233. if diffidx == len(st.key) {
  234. // Ext key and key segment are identical, recurse into
  235. // the child node.
  236. st.children[0].insert(key, value)
  237. return
  238. }
  239. // Save the original part. Depending if the break is
  240. // at the extension's last byte or not, create an
  241. // intermediate extension or use the extension's child
  242. // node directly.
  243. var n *StackTrie
  244. if diffidx < len(st.key)-1 {
  245. n = newExt(diffidx+1, st.key, st.children[0], st.db)
  246. } else {
  247. // Break on the last byte, no need to insert
  248. // an extension node: reuse the current node
  249. n = st.children[0]
  250. }
  251. // Convert to hash
  252. n.hash()
  253. var p *StackTrie
  254. if diffidx == 0 {
  255. // the break is on the first byte, so
  256. // the current node is converted into
  257. // a branch node.
  258. st.children[0] = nil
  259. p = st
  260. st.nodeType = branchNode
  261. } else {
  262. // the common prefix is at least one byte
  263. // long, insert a new intermediate branch
  264. // node.
  265. st.children[0] = stackTrieFromPool(st.db)
  266. st.children[0].nodeType = branchNode
  267. st.children[0].keyOffset = st.keyOffset + diffidx
  268. p = st.children[0]
  269. }
  270. // Create a leaf for the inserted part
  271. o := newLeaf(st.keyOffset+diffidx+1, key, value, st.db)
  272. // Insert both child leaves where they belong:
  273. origIdx := st.key[diffidx]
  274. newIdx := key[diffidx+st.keyOffset]
  275. p.children[origIdx] = n
  276. p.children[newIdx] = o
  277. st.key = st.key[:diffidx]
  278. case leafNode: /* Leaf */
  279. // Compare both key chunks and see where they differ
  280. diffidx := st.getDiffIndex(key)
  281. // Overwriting a key isn't supported, which means that
  282. // the current leaf is expected to be split into 1) an
  283. // optional extension for the common prefix of these 2
  284. // keys, 2) a fullnode selecting the path on which the
  285. // keys differ, and 3) one leaf for the differentiated
  286. // component of each key.
  287. if diffidx >= len(st.key) {
  288. panic("Trying to insert into existing key")
  289. }
  290. // Check if the split occurs at the first nibble of the
  291. // chunk. In that case, no prefix extnode is necessary.
  292. // Otherwise, create that
  293. var p *StackTrie
  294. if diffidx == 0 {
  295. // Convert current leaf into a branch
  296. st.nodeType = branchNode
  297. p = st
  298. st.children[0] = nil
  299. } else {
  300. // Convert current node into an ext,
  301. // and insert a child branch node.
  302. st.nodeType = extNode
  303. st.children[0] = NewStackTrie(st.db)
  304. st.children[0].nodeType = branchNode
  305. st.children[0].keyOffset = st.keyOffset + diffidx
  306. p = st.children[0]
  307. }
  308. // Create the two child leaves: the one containing the
  309. // original value and the one containing the new value
  310. // The child leave will be hashed directly in order to
  311. // free up some memory.
  312. origIdx := st.key[diffidx]
  313. p.children[origIdx] = newLeaf(diffidx+1, st.key, st.val, st.db)
  314. p.children[origIdx].hash()
  315. newIdx := key[diffidx+st.keyOffset]
  316. p.children[newIdx] = newLeaf(p.keyOffset+1, key, value, st.db)
  317. // Finally, cut off the key part that has been passed
  318. // over to the children.
  319. st.key = st.key[:diffidx]
  320. st.val = nil
  321. case emptyNode: /* Empty */
  322. st.nodeType = leafNode
  323. st.key = key[st.keyOffset:]
  324. st.val = value
  325. case hashedNode:
  326. panic("trying to insert into hash")
  327. default:
  328. panic("invalid type")
  329. }
  330. }
  331. // hash() hashes the node 'st' and converts it into 'hashedNode', if possible.
  332. // Possible outcomes:
  333. // 1. The rlp-encoded value was >= 32 bytes:
  334. // - Then the 32-byte `hash` will be accessible in `st.val`.
  335. // - And the 'st.type' will be 'hashedNode'
  336. // 2. The rlp-encoded value was < 32 bytes
  337. // - Then the <32 byte rlp-encoded value will be accessible in 'st.val'.
  338. // - And the 'st.type' will be 'hashedNode' AGAIN
  339. //
  340. // This method will also:
  341. // set 'st.type' to hashedNode
  342. // clear 'st.key'
  343. func (st *StackTrie) hash() {
  344. /* Shortcut if node is already hashed */
  345. if st.nodeType == hashedNode {
  346. return
  347. }
  348. // The 'hasher' is taken from a pool, but we don't actually
  349. // claim an instance until all children are done with their hashing,
  350. // and we actually need one
  351. var h *hasher
  352. switch st.nodeType {
  353. case branchNode:
  354. var nodes [17]node
  355. for i, child := range st.children {
  356. if child == nil {
  357. nodes[i] = nilValueNode
  358. continue
  359. }
  360. child.hash()
  361. if len(child.val) < 32 {
  362. nodes[i] = rawNode(child.val)
  363. } else {
  364. nodes[i] = hashNode(child.val)
  365. }
  366. st.children[i] = nil // Reclaim mem from subtree
  367. returnToPool(child)
  368. }
  369. nodes[16] = nilValueNode
  370. h = newHasher(false)
  371. defer returnHasherToPool(h)
  372. h.tmp.Reset()
  373. if err := rlp.Encode(&h.tmp, nodes); err != nil {
  374. panic(err)
  375. }
  376. case extNode:
  377. st.children[0].hash()
  378. h = newHasher(false)
  379. defer returnHasherToPool(h)
  380. h.tmp.Reset()
  381. var valuenode node
  382. if len(st.children[0].val) < 32 {
  383. valuenode = rawNode(st.children[0].val)
  384. } else {
  385. valuenode = hashNode(st.children[0].val)
  386. }
  387. n := struct {
  388. Key []byte
  389. Val node
  390. }{
  391. Key: hexToCompact(st.key),
  392. Val: valuenode,
  393. }
  394. if err := rlp.Encode(&h.tmp, n); err != nil {
  395. panic(err)
  396. }
  397. returnToPool(st.children[0])
  398. st.children[0] = nil // Reclaim mem from subtree
  399. case leafNode:
  400. h = newHasher(false)
  401. defer returnHasherToPool(h)
  402. h.tmp.Reset()
  403. st.key = append(st.key, byte(16))
  404. sz := hexToCompactInPlace(st.key)
  405. n := [][]byte{st.key[:sz], st.val}
  406. if err := rlp.Encode(&h.tmp, n); err != nil {
  407. panic(err)
  408. }
  409. case emptyNode:
  410. st.val = emptyRoot.Bytes()
  411. st.key = st.key[:0]
  412. st.nodeType = hashedNode
  413. return
  414. default:
  415. panic("Invalid node type")
  416. }
  417. st.key = st.key[:0]
  418. st.nodeType = hashedNode
  419. if len(h.tmp) < 32 {
  420. st.val = common.CopyBytes(h.tmp)
  421. return
  422. }
  423. // Write the hash to the 'val'. We allocate a new val here to not mutate
  424. // input values
  425. st.val = make([]byte, 32)
  426. h.sha.Reset()
  427. h.sha.Write(h.tmp)
  428. h.sha.Read(st.val)
  429. if st.db != nil {
  430. // TODO! Is it safe to Put the slice here?
  431. // Do all db implementations copy the value provided?
  432. st.db.Put(st.val, h.tmp)
  433. }
  434. }
  435. // Hash returns the hash of the current node
  436. func (st *StackTrie) Hash() (h common.Hash) {
  437. st.hash()
  438. if len(st.val) != 32 {
  439. // If the node's RLP isn't 32 bytes long, the node will not
  440. // be hashed, and instead contain the rlp-encoding of the
  441. // node. For the top level node, we need to force the hashing.
  442. ret := make([]byte, 32)
  443. h := newHasher(false)
  444. defer returnHasherToPool(h)
  445. h.sha.Reset()
  446. h.sha.Write(st.val)
  447. h.sha.Read(ret)
  448. return common.BytesToHash(ret)
  449. }
  450. return common.BytesToHash(st.val)
  451. }
  452. // Commit will firstly hash the entrie trie if it's still not hashed
  453. // and then commit all nodes to the associated database. Actually most
  454. // of the trie nodes MAY have been committed already. The main purpose
  455. // here is to commit the root node.
  456. //
  457. // The associated database is expected, otherwise the whole commit
  458. // functionality should be disabled.
  459. func (st *StackTrie) Commit() (common.Hash, error) {
  460. if st.db == nil {
  461. return common.Hash{}, ErrCommitDisabled
  462. }
  463. st.hash()
  464. if len(st.val) != 32 {
  465. // If the node's RLP isn't 32 bytes long, the node will not
  466. // be hashed (and committed), and instead contain the rlp-encoding of the
  467. // node. For the top level node, we need to force the hashing+commit.
  468. ret := make([]byte, 32)
  469. h := newHasher(false)
  470. defer returnHasherToPool(h)
  471. h.sha.Reset()
  472. h.sha.Write(st.val)
  473. h.sha.Read(ret)
  474. st.db.Put(ret, st.val)
  475. return common.BytesToHash(ret), nil
  476. }
  477. return common.BytesToHash(st.val), nil
  478. }