stacktrie.go 13 KB

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