github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/trie/proof.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package trie 19 20 import ( 21 "bytes" 22 "fmt" 23 24 "github.com/AigarNetwork/aigar/common" 25 "github.com/AigarNetwork/aigar/ethdb" 26 "github.com/AigarNetwork/aigar/log" 27 "github.com/AigarNetwork/aigar/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.KeyValueWriter) error { 38 // Collect all nodes on the path to key. 39 key = keybytesToHex(key) 40 var 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(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 = hasher.makeHashNode(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.KeyValueWriter) 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 ethdb.KeyValueReader) (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) 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 }