proof.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // Copyright 2015 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. "bytes"
  19. "errors"
  20. "fmt"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // Prove constructs a merkle proof for key. The result contains all encoded nodes
  28. // on the path to the value at key. The value itself is also included in the last
  29. // node and can be retrieved by verifying the proof.
  30. //
  31. // If the trie does not contain a value for key, the returned proof contains all
  32. // nodes of the longest existing prefix of the key (at least the root node), ending
  33. // with the node that proves the absence of the key.
  34. func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
  35. // Collect all nodes on the path to key.
  36. key = keybytesToHex(key)
  37. var nodes []node
  38. tn := t.root
  39. for len(key) > 0 && tn != nil {
  40. switch n := tn.(type) {
  41. case *shortNode:
  42. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  43. // The trie doesn't contain the key.
  44. tn = nil
  45. } else {
  46. tn = n.Val
  47. key = key[len(n.Key):]
  48. }
  49. nodes = append(nodes, n)
  50. case *fullNode:
  51. tn = n.Children[key[0]]
  52. key = key[1:]
  53. nodes = append(nodes, n)
  54. case hashNode:
  55. var err error
  56. tn, err = t.resolveHash(n, nil)
  57. if err != nil {
  58. log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
  59. return err
  60. }
  61. default:
  62. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  63. }
  64. }
  65. hasher := newHasher(false)
  66. defer returnHasherToPool(hasher)
  67. for i, n := range nodes {
  68. if fromLevel > 0 {
  69. fromLevel--
  70. continue
  71. }
  72. var hn node
  73. n, hn = hasher.proofHash(n)
  74. if hash, ok := hn.(hashNode); ok || i == 0 {
  75. // If the node's database encoding is a hash (or is the
  76. // root node), it becomes a proof element.
  77. enc, _ := rlp.EncodeToBytes(n)
  78. if !ok {
  79. hash = hasher.hashData(enc)
  80. }
  81. proofDb.Put(hash, enc)
  82. }
  83. }
  84. return nil
  85. }
  86. // Prove constructs a merkle proof for key. The result contains all encoded nodes
  87. // on the path to the value at key. The value itself is also included in the last
  88. // node and can be retrieved by verifying the proof.
  89. //
  90. // If the trie does not contain a value for key, the returned proof contains all
  91. // nodes of the longest existing prefix of the key (at least the root node), ending
  92. // with the node that proves the absence of the key.
  93. func (t *SecureTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
  94. return t.trie.Prove(key, fromLevel, proofDb)
  95. }
  96. // VerifyProof checks merkle proofs. The given proof must contain the value for
  97. // key in a trie with the given root hash. VerifyProof returns an error if the
  98. // proof contains invalid trie nodes or the wrong value.
  99. func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.KeyValueReader) (value []byte, err error) {
  100. key = keybytesToHex(key)
  101. wantHash := rootHash
  102. for i := 0; ; i++ {
  103. buf, _ := proofDb.Get(wantHash[:])
  104. if buf == nil {
  105. return nil, fmt.Errorf("proof node %d (hash %064x) missing", i, wantHash)
  106. }
  107. n, err := decodeNode(wantHash[:], buf)
  108. if err != nil {
  109. return nil, fmt.Errorf("bad proof node %d: %v", i, err)
  110. }
  111. keyrest, cld := get(n, key, true)
  112. switch cld := cld.(type) {
  113. case nil:
  114. // The trie doesn't contain the key.
  115. return nil, nil
  116. case hashNode:
  117. key = keyrest
  118. copy(wantHash[:], cld)
  119. case valueNode:
  120. return cld, nil
  121. }
  122. }
  123. }
  124. // proofToPath converts a merkle proof to trie node path.
  125. // The main purpose of this function is recovering a node
  126. // path from the merkle proof stream. All necessary nodes
  127. // will be resolved and leave the remaining as hashnode.
  128. func proofToPath(rootHash common.Hash, root node, key []byte, proofDb ethdb.KeyValueReader, allowNonExistent bool) (node, error) {
  129. // resolveNode retrieves and resolves trie node from merkle proof stream
  130. resolveNode := func(hash common.Hash) (node, error) {
  131. buf, _ := proofDb.Get(hash[:])
  132. if buf == nil {
  133. return nil, fmt.Errorf("proof node (hash %064x) missing", hash)
  134. }
  135. n, err := decodeNode(hash[:], buf)
  136. if err != nil {
  137. return nil, fmt.Errorf("bad proof node %v", err)
  138. }
  139. return n, err
  140. }
  141. // If the root node is empty, resolve it first.
  142. // Root node must be included in the proof.
  143. if root == nil {
  144. n, err := resolveNode(rootHash)
  145. if err != nil {
  146. return nil, err
  147. }
  148. root = n
  149. }
  150. var (
  151. err error
  152. child, parent node
  153. keyrest []byte
  154. terminate bool
  155. )
  156. key, parent = keybytesToHex(key), root
  157. for {
  158. keyrest, child = get(parent, key, false)
  159. switch cld := child.(type) {
  160. case nil:
  161. // The trie doesn't contain the key. It's possible
  162. // the proof is a non-existing proof, but at least
  163. // we can prove all resolved nodes are correct, it's
  164. // enough for us to prove range.
  165. if allowNonExistent {
  166. return root, nil
  167. }
  168. return nil, errors.New("the node is not contained in trie")
  169. case *shortNode:
  170. key, parent = keyrest, child // Already resolved
  171. continue
  172. case *fullNode:
  173. key, parent = keyrest, child // Already resolved
  174. continue
  175. case hashNode:
  176. child, err = resolveNode(common.BytesToHash(cld))
  177. if err != nil {
  178. return nil, err
  179. }
  180. case valueNode:
  181. terminate = true
  182. }
  183. // Link the parent and child.
  184. switch pnode := parent.(type) {
  185. case *shortNode:
  186. pnode.Val = child
  187. case *fullNode:
  188. pnode.Children[key[0]] = child
  189. default:
  190. panic(fmt.Sprintf("%T: invalid node: %v", pnode, pnode))
  191. }
  192. if terminate {
  193. return root, nil // The whole path is resolved
  194. }
  195. key, parent = keyrest, child
  196. }
  197. }
  198. // unsetInternal removes all internal node references(hashnode, embedded node).
  199. // It should be called after a trie is constructed with two edge proofs. Also
  200. // the given boundary keys must be the one used to construct the edge proofs.
  201. //
  202. // It's the key step for range proof. All visited nodes should be marked dirty
  203. // since the node content might be modified. Besides it can happen that some
  204. // fullnodes only have one child which is disallowed. But if the proof is valid,
  205. // the missing children will be filled, otherwise it will be thrown anyway.
  206. func unsetInternal(n node, left []byte, right []byte) error {
  207. left, right = keybytesToHex(left), keybytesToHex(right)
  208. // todo(rjl493456442) different length edge keys should be supported
  209. if len(left) != len(right) {
  210. return errors.New("inconsistent edge path")
  211. }
  212. // Step down to the fork point. There are two scenarios can happen:
  213. // - the fork point is a shortnode: the left proof MUST point to a
  214. // non-existent key and the key doesn't match with the shortnode
  215. // - the fork point is a fullnode: the left proof can point to an
  216. // existent key or not.
  217. var (
  218. pos = 0
  219. parent node
  220. )
  221. findFork:
  222. for {
  223. switch rn := (n).(type) {
  224. case *shortNode:
  225. // The right proof must point to an existent key.
  226. if len(right)-pos < len(rn.Key) || !bytes.Equal(rn.Key, right[pos:pos+len(rn.Key)]) {
  227. return errors.New("invalid edge path")
  228. }
  229. rn.flags = nodeFlag{dirty: true}
  230. // Special case, the non-existent proof points to the same path
  231. // as the existent proof, but the path of existent proof is longer.
  232. // In this case, the fork point is this shortnode.
  233. if len(left)-pos < len(rn.Key) || !bytes.Equal(rn.Key, left[pos:pos+len(rn.Key)]) {
  234. break findFork
  235. }
  236. parent = n
  237. n, pos = rn.Val, pos+len(rn.Key)
  238. case *fullNode:
  239. leftnode, rightnode := rn.Children[left[pos]], rn.Children[right[pos]]
  240. // The right proof must point to an existent key.
  241. if rightnode == nil {
  242. return errors.New("invalid edge path")
  243. }
  244. rn.flags = nodeFlag{dirty: true}
  245. if leftnode != rightnode {
  246. break findFork
  247. }
  248. parent = n
  249. n, pos = rn.Children[left[pos]], pos+1
  250. default:
  251. panic(fmt.Sprintf("%T: invalid node: %v", n, n))
  252. }
  253. }
  254. switch rn := n.(type) {
  255. case *shortNode:
  256. if _, ok := rn.Val.(valueNode); ok {
  257. parent.(*fullNode).Children[right[pos-1]] = nil
  258. return nil
  259. }
  260. return unset(rn, rn.Val, right[pos:], len(rn.Key), true)
  261. case *fullNode:
  262. for i := left[pos] + 1; i < right[pos]; i++ {
  263. rn.Children[i] = nil
  264. }
  265. if err := unset(rn, rn.Children[left[pos]], left[pos:], 1, false); err != nil {
  266. return err
  267. }
  268. if err := unset(rn, rn.Children[right[pos]], right[pos:], 1, true); err != nil {
  269. return err
  270. }
  271. return nil
  272. default:
  273. panic(fmt.Sprintf("%T: invalid node: %v", n, n))
  274. }
  275. }
  276. // unset removes all internal node references either the left most or right most.
  277. // If we try to unset all right most references, it can meet these scenarios:
  278. //
  279. // - The given path is existent in the trie, unset the associated shortnode
  280. // - The given path is non-existent in the trie
  281. // - the fork point is a fullnode, the corresponding child pointed by path
  282. // is nil, return
  283. // - the fork point is a shortnode, the key of shortnode is less than path,
  284. // keep the entire branch and return.
  285. // - the fork point is a shortnode, the key of shortnode is greater than path,
  286. // unset the entire branch.
  287. //
  288. // If we try to unset all left most references, then the given path should
  289. // be existent.
  290. func unset(parent node, child node, key []byte, pos int, removeLeft bool) error {
  291. switch cld := child.(type) {
  292. case *fullNode:
  293. if removeLeft {
  294. for i := 0; i < int(key[pos]); i++ {
  295. cld.Children[i] = nil
  296. }
  297. cld.flags = nodeFlag{dirty: true}
  298. } else {
  299. for i := key[pos] + 1; i < 16; i++ {
  300. cld.Children[i] = nil
  301. }
  302. cld.flags = nodeFlag{dirty: true}
  303. }
  304. return unset(cld, cld.Children[key[pos]], key, pos+1, removeLeft)
  305. case *shortNode:
  306. if len(key[pos:]) < len(cld.Key) || !bytes.Equal(cld.Key, key[pos:pos+len(cld.Key)]) {
  307. // Find the fork point, it's an non-existent branch.
  308. if removeLeft {
  309. return errors.New("invalid right edge proof")
  310. }
  311. if bytes.Compare(cld.Key, key[pos:]) > 0 {
  312. // The key of fork shortnode is greater than the
  313. // path(it belongs to the range), unset the entrie
  314. // branch. The parent must be a fullnode.
  315. fn := parent.(*fullNode)
  316. fn.Children[key[pos-1]] = nil
  317. } else {
  318. // The key of fork shortnode is less than the
  319. // path(it doesn't belong to the range), keep
  320. // it with the cached hash available.
  321. }
  322. return nil
  323. }
  324. if _, ok := cld.Val.(valueNode); ok {
  325. fn := parent.(*fullNode)
  326. fn.Children[key[pos-1]] = nil
  327. return nil
  328. }
  329. cld.flags = nodeFlag{dirty: true}
  330. return unset(cld, cld.Val, key, pos+len(cld.Key), removeLeft)
  331. case nil:
  332. // If the node is nil, it's a child of the fork point
  333. // fullnode(it's an non-existent branch).
  334. if removeLeft {
  335. return errors.New("invalid right edge proof")
  336. }
  337. return nil
  338. default:
  339. panic("it shouldn't happen") // hashNode, valueNode
  340. }
  341. }
  342. // VerifyRangeProof checks whether the given leaf nodes and edge proofs
  343. // can prove the given trie leaves range is matched with given root hash
  344. // and the range is consecutive(no gap inside).
  345. //
  346. // Note the given first edge proof can be non-existing proof. For example
  347. // the first proof is for an non-existent values 0x03. The given batch
  348. // leaves are [0x04, 0x05, .. 0x09]. It's still feasible to prove. But the
  349. // last edge proof should always be an existent proof.
  350. //
  351. // The firstKey is paired with firstProof, not necessarily the same as keys[0]
  352. // (unless firstProof is an existent proof).
  353. //
  354. // Expect the normal case, this function can also be used to verify the following
  355. // range proofs:
  356. //
  357. // - All elements proof. In this case the left and right proof can be nil, but the
  358. // range should be all the leaves in the trie.
  359. //
  360. // - Zero element proof(left edge proof should be a non-existent proof). In this
  361. // case if there are still some other leaves available on the right side, then
  362. // an error will be returned.
  363. //
  364. // - One element proof. In this case no matter the left edge proof is a non-existent
  365. // proof or not, we can always verify the correctness of the proof.
  366. func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, values [][]byte, firstProof ethdb.KeyValueReader, lastProof ethdb.KeyValueReader) error {
  367. if len(keys) != len(values) {
  368. return fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
  369. }
  370. // Special case, there is no edge proof at all. The given range is expected
  371. // to be the whole leaf-set in the trie.
  372. if firstProof == nil && lastProof == nil {
  373. emptytrie, err := New(common.Hash{}, NewDatabase(memorydb.New()))
  374. if err != nil {
  375. return err
  376. }
  377. for index, key := range keys {
  378. emptytrie.TryUpdate(key, values[index])
  379. }
  380. if emptytrie.Hash() != rootHash {
  381. return fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, emptytrie.Hash())
  382. }
  383. return nil
  384. }
  385. // Special case, there is a provided non-existence proof and zero key/value
  386. // pairs, meaning there are no more accounts / slots in the trie.
  387. if len(keys) == 0 {
  388. // Recover the non-existent proof to a path, ensure there is nothing left
  389. root, err := proofToPath(rootHash, nil, firstKey, firstProof, true)
  390. if err != nil {
  391. return err
  392. }
  393. node, pos, firstKey := root, 0, keybytesToHex(firstKey)
  394. for node != nil {
  395. switch rn := node.(type) {
  396. case *fullNode:
  397. for i := firstKey[pos] + 1; i < 16; i++ {
  398. if rn.Children[i] != nil {
  399. return errors.New("more leaves available")
  400. }
  401. }
  402. node, pos = rn.Children[firstKey[pos]], pos+1
  403. case *shortNode:
  404. if len(firstKey)-pos < len(rn.Key) || !bytes.Equal(rn.Key, firstKey[pos:pos+len(rn.Key)]) {
  405. if bytes.Compare(rn.Key, firstKey[pos:]) < 0 {
  406. node = nil
  407. continue
  408. } else {
  409. return errors.New("more leaves available")
  410. }
  411. }
  412. node, pos = rn.Val, pos+len(rn.Key)
  413. case valueNode, hashNode:
  414. return errors.New("more leaves available")
  415. }
  416. }
  417. // Yeah, although we receive nothing, but we can prove
  418. // there is no more leaf in the trie, return nil.
  419. return nil
  420. }
  421. // Special case, there is only one element and left edge
  422. // proof is an existent one.
  423. if len(keys) == 1 && bytes.Equal(keys[0], firstKey) {
  424. value, err := VerifyProof(rootHash, keys[0], firstProof)
  425. if err != nil {
  426. return err
  427. }
  428. if !bytes.Equal(value, values[0]) {
  429. return fmt.Errorf("correct proof but invalid data")
  430. }
  431. return nil
  432. }
  433. // Convert the edge proofs to edge trie paths. Then we can
  434. // have the same tree architecture with the original one.
  435. // For the first edge proof, non-existent proof is allowed.
  436. root, err := proofToPath(rootHash, nil, firstKey, firstProof, true)
  437. if err != nil {
  438. return err
  439. }
  440. // Pass the root node here, the second path will be merged
  441. // with the first one. For the last edge proof, non-existent
  442. // proof is not allowed.
  443. root, err = proofToPath(rootHash, root, keys[len(keys)-1], lastProof, false)
  444. if err != nil {
  445. return err
  446. }
  447. // Remove all internal references. All the removed parts should
  448. // be re-filled(or re-constructed) by the given leaves range.
  449. if err := unsetInternal(root, firstKey, keys[len(keys)-1]); err != nil {
  450. return err
  451. }
  452. // Rebuild the trie with the leave stream, the shape of trie
  453. // should be same with the original one.
  454. newtrie := &Trie{root: root, db: NewDatabase(memorydb.New())}
  455. for index, key := range keys {
  456. newtrie.TryUpdate(key, values[index])
  457. }
  458. if newtrie.Hash() != rootHash {
  459. return fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, newtrie.Hash())
  460. }
  461. return nil
  462. }
  463. // get returns the child of the given node. Return nil if the
  464. // node with specified key doesn't exist at all.
  465. //
  466. // There is an additional flag `skipResolved`. If it's set then
  467. // all resolved nodes won't be returned.
  468. func get(tn node, key []byte, skipResolved bool) ([]byte, node) {
  469. for {
  470. switch n := tn.(type) {
  471. case *shortNode:
  472. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  473. return nil, nil
  474. }
  475. tn = n.Val
  476. key = key[len(n.Key):]
  477. if !skipResolved {
  478. return key, tn
  479. }
  480. case *fullNode:
  481. tn = n.Children[key[0]]
  482. key = key[1:]
  483. if !skipResolved {
  484. return key, tn
  485. }
  486. case hashNode:
  487. return key, n
  488. case nil:
  489. return key, nil
  490. case valueNode:
  491. return nil, n
  492. default:
  493. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  494. }
  495. }
  496. }