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