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