proof.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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
  213. prefix, pos := prefixLen(left, right), 0
  214. var parent node
  215. for {
  216. if pos >= prefix {
  217. break
  218. }
  219. switch rn := (n).(type) {
  220. case *shortNode:
  221. if len(right)-pos < len(rn.Key) || !bytes.Equal(rn.Key, right[pos:pos+len(rn.Key)]) {
  222. return errors.New("invalid edge path")
  223. }
  224. // Special case, the non-existent proof points to the same path
  225. // as the existent proof, but the path of existent proof is longer.
  226. // In this case, truncate the extra path(it should be recovered
  227. // by node insertion).
  228. if len(left)-pos < len(rn.Key) || !bytes.Equal(rn.Key, left[pos:pos+len(rn.Key)]) {
  229. fn := parent.(*fullNode)
  230. fn.Children[left[pos-1]] = nil
  231. return nil
  232. }
  233. rn.flags = nodeFlag{dirty: true}
  234. parent = n
  235. n, pos = rn.Val, pos+len(rn.Key)
  236. case *fullNode:
  237. rn.flags = nodeFlag{dirty: true}
  238. parent = n
  239. n, pos = rn.Children[right[pos]], pos+1
  240. default:
  241. panic(fmt.Sprintf("%T: invalid node: %v", n, n))
  242. }
  243. }
  244. fn, ok := n.(*fullNode)
  245. if !ok {
  246. return errors.New("the fork point must be a fullnode")
  247. }
  248. // Find the fork point! Unset all intermediate references
  249. for i := left[prefix] + 1; i < right[prefix]; i++ {
  250. fn.Children[i] = nil
  251. }
  252. fn.flags = nodeFlag{dirty: true}
  253. if err := unset(fn, fn.Children[left[prefix]], left[prefix:], 1, false); err != nil {
  254. return err
  255. }
  256. if err := unset(fn, fn.Children[right[prefix]], right[prefix:], 1, true); err != nil {
  257. return err
  258. }
  259. return nil
  260. }
  261. // unset removes all internal node references either the left most or right most.
  262. // If we try to unset all right most references, it can meet these scenarios:
  263. //
  264. // - The given path is existent in the trie, unset the associated shortnode
  265. // - The given path is non-existent in the trie
  266. // - the fork point is a fullnode, the corresponding child pointed by path
  267. // is nil, return
  268. // - the fork point is a shortnode, the key of shortnode is less than path,
  269. // keep the entire branch and return.
  270. // - the fork point is a shortnode, the key of shortnode is greater than path,
  271. // unset the entire branch.
  272. //
  273. // If we try to unset all left most references, then the given path should
  274. // be existent.
  275. func unset(parent node, child node, key []byte, pos int, removeLeft bool) error {
  276. switch cld := child.(type) {
  277. case *fullNode:
  278. if removeLeft {
  279. for i := 0; i < int(key[pos]); i++ {
  280. cld.Children[i] = nil
  281. }
  282. cld.flags = nodeFlag{dirty: true}
  283. } else {
  284. for i := key[pos] + 1; i < 16; i++ {
  285. cld.Children[i] = nil
  286. }
  287. cld.flags = nodeFlag{dirty: true}
  288. }
  289. return unset(cld, cld.Children[key[pos]], key, pos+1, removeLeft)
  290. case *shortNode:
  291. if len(key[pos:]) < len(cld.Key) || !bytes.Equal(cld.Key, key[pos:pos+len(cld.Key)]) {
  292. // Find the fork point, it's an non-existent branch.
  293. if removeLeft {
  294. return errors.New("invalid right edge proof")
  295. }
  296. if bytes.Compare(cld.Key, key[pos:]) > 0 {
  297. // The key of fork shortnode is greater than the
  298. // path(it belongs to the range), unset the entrie
  299. // branch. The parent must be a fullnode.
  300. fn := parent.(*fullNode)
  301. fn.Children[key[pos-1]] = nil
  302. } else {
  303. // The key of fork shortnode is less than the
  304. // path(it doesn't belong to the range), keep
  305. // it with the cached hash available.
  306. return nil
  307. }
  308. }
  309. if _, ok := cld.Val.(valueNode); ok {
  310. fn := parent.(*fullNode)
  311. fn.Children[key[pos-1]] = nil
  312. return nil
  313. }
  314. cld.flags = nodeFlag{dirty: true}
  315. return unset(cld, cld.Val, key, pos+len(cld.Key), removeLeft)
  316. case nil:
  317. // If the node is nil, it's a child of the fork point
  318. // fullnode(it's an non-existent branch).
  319. if removeLeft {
  320. return errors.New("invalid right edge proof")
  321. }
  322. return nil
  323. default:
  324. panic("it shouldn't happen") // hashNode, valueNode
  325. }
  326. }
  327. // VerifyRangeProof checks whether the given leaf nodes and edge proofs
  328. // can prove the given trie leaves range is matched with given root hash
  329. // and the range is consecutive(no gap inside).
  330. //
  331. // Note the given first edge proof can be non-existing proof. For example
  332. // the first proof is for an non-existent values 0x03. The given batch
  333. // leaves are [0x04, 0x05, .. 0x09]. It's still feasible to prove. But the
  334. // last edge proof should always be an existent proof.
  335. //
  336. // The firstKey is paired with firstProof, not necessarily the same as keys[0]
  337. // (unless firstProof is an existent proof).
  338. //
  339. // Expect the normal case, this function can also be used to verify the following
  340. // range proofs:
  341. //
  342. // - All elements proof. In this case the left and right proof can be nil, but the
  343. // range should be all the leaves in the trie.
  344. //
  345. // - Zero element proof(left edge proof should be a non-existent proof). In this
  346. // case if there are still some other leaves available on the right side, then
  347. // an error will be returned.
  348. //
  349. // - One element proof. In this case no matter the left edge proof is a non-existent
  350. // proof or not, we can always verify the correctness of the proof.
  351. func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, values [][]byte, firstProof ethdb.KeyValueReader, lastProof ethdb.KeyValueReader) error {
  352. if len(keys) != len(values) {
  353. return fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
  354. }
  355. // Special case, there is no edge proof at all. The given range is expected
  356. // to be the whole leaf-set in the trie.
  357. if firstProof == nil && lastProof == nil {
  358. emptytrie, err := New(common.Hash{}, NewDatabase(memorydb.New()))
  359. if err != nil {
  360. return err
  361. }
  362. for index, key := range keys {
  363. emptytrie.TryUpdate(key, values[index])
  364. }
  365. if emptytrie.Hash() != rootHash {
  366. return fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, emptytrie.Hash())
  367. }
  368. return nil
  369. }
  370. // Special case, there is a provided non-existence proof and zero key/value
  371. // pairs, meaning there are no more accounts / slots in the trie.
  372. if len(keys) == 0 {
  373. // Recover the non-existent proof to a path, ensure there is nothing left
  374. root, err := proofToPath(rootHash, nil, firstKey, firstProof, true)
  375. if err != nil {
  376. return err
  377. }
  378. node, pos, firstKey := root, 0, keybytesToHex(firstKey)
  379. for node != nil {
  380. switch rn := node.(type) {
  381. case *fullNode:
  382. for i := firstKey[pos] + 1; i < 16; i++ {
  383. if rn.Children[i] != nil {
  384. return errors.New("more leaves available")
  385. }
  386. }
  387. node, pos = rn.Children[firstKey[pos]], pos+1
  388. case *shortNode:
  389. if len(firstKey)-pos < len(rn.Key) || !bytes.Equal(rn.Key, firstKey[pos:pos+len(rn.Key)]) {
  390. if bytes.Compare(rn.Key, firstKey[pos:]) < 0 {
  391. node = nil
  392. continue
  393. } else {
  394. return errors.New("more leaves available")
  395. }
  396. }
  397. node, pos = rn.Val, pos+len(rn.Key)
  398. case valueNode, hashNode:
  399. return errors.New("more leaves available")
  400. }
  401. }
  402. // Yeah, although we receive nothing, but we can prove
  403. // there is no more leaf in the trie, return nil.
  404. return nil
  405. }
  406. // Special case, there is only one element and left edge
  407. // proof is an existent one.
  408. if len(keys) == 1 && bytes.Equal(keys[0], firstKey) {
  409. value, err := VerifyProof(rootHash, keys[0], firstProof)
  410. if err != nil {
  411. return err
  412. }
  413. if !bytes.Equal(value, values[0]) {
  414. return fmt.Errorf("correct proof but invalid data")
  415. }
  416. return nil
  417. }
  418. // Convert the edge proofs to edge trie paths. Then we can
  419. // have the same tree architecture with the original one.
  420. // For the first edge proof, non-existent proof is allowed.
  421. root, err := proofToPath(rootHash, nil, firstKey, firstProof, true)
  422. if err != nil {
  423. return err
  424. }
  425. // Pass the root node here, the second path will be merged
  426. // with the first one. For the last edge proof, non-existent
  427. // proof is not allowed.
  428. root, err = proofToPath(rootHash, root, keys[len(keys)-1], lastProof, false)
  429. if err != nil {
  430. return err
  431. }
  432. // Remove all internal references. All the removed parts should
  433. // be re-filled(or re-constructed) by the given leaves range.
  434. if err := unsetInternal(root, firstKey, keys[len(keys)-1]); err != nil {
  435. return err
  436. }
  437. // Rebuild the trie with the leave stream, the shape of trie
  438. // should be same with the original one.
  439. newtrie := &Trie{root: root, db: NewDatabase(memorydb.New())}
  440. for index, key := range keys {
  441. newtrie.TryUpdate(key, values[index])
  442. }
  443. if newtrie.Hash() != rootHash {
  444. return fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, newtrie.Hash())
  445. }
  446. return nil
  447. }
  448. // get returns the child of the given node. Return nil if the
  449. // node with specified key doesn't exist at all.
  450. //
  451. // There is an additional flag `skipResolved`. If it's set then
  452. // all resolved nodes won't be returned.
  453. func get(tn node, key []byte, skipResolved bool) ([]byte, node) {
  454. for {
  455. switch n := tn.(type) {
  456. case *shortNode:
  457. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  458. return nil, nil
  459. }
  460. tn = n.Val
  461. key = key[len(n.Key):]
  462. if !skipResolved {
  463. return key, tn
  464. }
  465. case *fullNode:
  466. tn = n.Children[key[0]]
  467. key = key[1:]
  468. if !skipResolved {
  469. return key, tn
  470. }
  471. case hashNode:
  472. return key, n
  473. case nil:
  474. return key, nil
  475. case valueNode:
  476. return nil, n
  477. default:
  478. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  479. }
  480. }
  481. }