proof.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "fmt"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Prove constructs a merkle proof for key. The result contains all encoded nodes
  26. // on the path to the value at key. The value itself is also included in the last
  27. // node and can be retrieved by verifying the proof.
  28. //
  29. // If the trie does not contain a value for key, the returned proof contains all
  30. // nodes of the longest existing prefix of the key (at least the root node), ending
  31. // with the node that proves the absence of the key.
  32. func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
  33. // Collect all nodes on the path to key.
  34. key = keybytesToHex(key)
  35. var nodes []node
  36. tn := t.root
  37. for len(key) > 0 && tn != nil {
  38. switch n := tn.(type) {
  39. case *shortNode:
  40. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  41. // The trie doesn't contain the key.
  42. tn = nil
  43. } else {
  44. tn = n.Val
  45. key = key[len(n.Key):]
  46. }
  47. nodes = append(nodes, n)
  48. case *fullNode:
  49. tn = n.Children[key[0]]
  50. key = key[1:]
  51. nodes = append(nodes, n)
  52. case hashNode:
  53. var err error
  54. tn, err = t.resolveHash(n, nil)
  55. if err != nil {
  56. log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
  57. return err
  58. }
  59. default:
  60. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  61. }
  62. }
  63. hasher := newHasher(false)
  64. defer returnHasherToPool(hasher)
  65. for i, n := range nodes {
  66. if fromLevel > 0 {
  67. fromLevel--
  68. continue
  69. }
  70. var hn node
  71. n, hn = hasher.proofHash(n)
  72. if hash, ok := hn.(hashNode); ok || i == 0 {
  73. // If the node's database encoding is a hash (or is the
  74. // root node), it becomes a proof element.
  75. enc, _ := rlp.EncodeToBytes(n)
  76. if !ok {
  77. hash = hasher.hashData(enc)
  78. }
  79. proofDb.Put(hash, enc)
  80. }
  81. }
  82. return nil
  83. }
  84. // Prove constructs a merkle proof for key. The result contains all encoded nodes
  85. // on the path to the value at key. The value itself is also included in the last
  86. // node and can be retrieved by verifying the proof.
  87. //
  88. // If the trie does not contain a value for key, the returned proof contains all
  89. // nodes of the longest existing prefix of the key (at least the root node), ending
  90. // with the node that proves the absence of the key.
  91. func (t *SecureTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
  92. return t.trie.Prove(key, fromLevel, proofDb)
  93. }
  94. // VerifyProof checks merkle proofs. The given proof must contain the value for
  95. // key in a trie with the given root hash. VerifyProof returns an error if the
  96. // proof contains invalid trie nodes or the wrong value.
  97. func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.KeyValueReader) (value []byte, nodes int, err error) {
  98. key = keybytesToHex(key)
  99. wantHash := rootHash
  100. for i := 0; ; i++ {
  101. buf, _ := proofDb.Get(wantHash[:])
  102. if buf == nil {
  103. return nil, i, fmt.Errorf("proof node %d (hash %064x) missing", i, wantHash)
  104. }
  105. n, err := decodeNode(wantHash[:], buf)
  106. if err != nil {
  107. return nil, i, fmt.Errorf("bad proof node %d: %v", i, err)
  108. }
  109. keyrest, cld := get(n, key)
  110. switch cld := cld.(type) {
  111. case nil:
  112. // The trie doesn't contain the key.
  113. return nil, i, nil
  114. case hashNode:
  115. key = keyrest
  116. copy(wantHash[:], cld)
  117. case valueNode:
  118. return cld, i + 1, nil
  119. }
  120. }
  121. }
  122. func get(tn node, key []byte) ([]byte, node) {
  123. for {
  124. switch n := tn.(type) {
  125. case *shortNode:
  126. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  127. return nil, nil
  128. }
  129. tn = n.Val
  130. key = key[len(n.Key):]
  131. case *fullNode:
  132. tn = n.Children[key[0]]
  133. key = key[1:]
  134. case hashNode:
  135. return key, n
  136. case nil:
  137. return key, nil
  138. case valueNode:
  139. return nil, n
  140. default:
  141. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  142. }
  143. }
  144. }