forked from sei-protocol/sei-iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_range.go
584 lines (506 loc) · 16.9 KB
/
proof_range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package iavl
import (
"bytes"
"crypto/sha256"
"fmt"
"sort"
"strings"
"github.com/pkg/errors"
iavlproto "github.com/cosmos/iavl/proto"
)
type RangeProof struct {
// You don't need the right path because
// it can be derived from what we have.
LeftPath PathToLeaf `json:"left_path"`
InnerNodes []PathToLeaf `json:"inner_nodes"`
Leaves []ProofLeafNode `json:"leaves"`
// memoize
rootHash []byte // valid iff rootVerified is true
rootVerified bool
treeEnd bool // valid iff rootVerified is true
}
// Keys returns all the keys in the RangeProof. NOTE: The keys here may
// include more keys than provided by tree.GetRangeWithProof or
// MutableTree.GetVersionedRangeWithProof. The keys returned there are only
// in the provided [startKey,endKey){limit} range. The keys returned here may
// include extra keys, such as:
// - the key before startKey if startKey is provided and doesn't exist;
// - the key after a queried key with tree.GetWithProof, when the key is absent.
func (proof *RangeProof) Keys() (keys [][]byte) {
if proof == nil {
return nil
}
for _, leaf := range proof.Leaves {
keys = append(keys, leaf.Key)
}
return keys
}
// String returns a string representation of the proof.
func (proof *RangeProof) String() string {
if proof == nil {
return "<nil-RangeProof>"
}
return proof.StringIndented("")
}
func (proof *RangeProof) StringIndented(indent string) string {
istrs := make([]string, 0, len(proof.InnerNodes))
for _, ptl := range proof.InnerNodes {
istrs = append(istrs, ptl.stringIndented(indent+" "))
}
lstrs := make([]string, 0, len(proof.Leaves))
for _, leaf := range proof.Leaves {
lstrs = append(lstrs, leaf.stringIndented(indent+" "))
}
return fmt.Sprintf(`RangeProof{
%s LeftPath: %v
%s InnerNodes:
%s %v
%s Leaves:
%s %v
%s (rootVerified): %v
%s (rootHash): %X
%s (treeEnd): %v
%s}`,
indent, proof.LeftPath.stringIndented(indent+" "),
indent,
indent, strings.Join(istrs, "\n"+indent+" "),
indent,
indent, strings.Join(lstrs, "\n"+indent+" "),
indent, proof.rootVerified,
indent, proof.rootHash,
indent, proof.treeEnd,
indent)
}
// The index of the first leaf (of the whole tree).
// Returns -1 if the proof is nil.
func (proof *RangeProof) LeftIndex() int64 {
if proof == nil {
return -1
}
return proof.LeftPath.Index()
}
// Also see LeftIndex().
// Verify that a key has some value.
// Does not assume that the proof itself is valid, call Verify() first.
func (proof *RangeProof) VerifyItem(key, value []byte) error {
if proof == nil {
return errors.Wrap(ErrInvalidProof, "proof is nil")
}
if !proof.rootVerified {
return errors.New("must call Verify(root) first")
}
leaves := proof.Leaves
i := sort.Search(len(leaves), func(i int) bool {
return bytes.Compare(key, leaves[i].Key) <= 0
})
if i >= len(leaves) || !bytes.Equal(leaves[i].Key, key) {
return errors.Wrap(ErrInvalidProof, "leaf key not found in proof")
}
h := sha256.Sum256(value)
valueHash := h[:]
if !bytes.Equal(leaves[i].ValueHash, valueHash) {
return errors.Wrap(ErrInvalidProof, "leaf value hash not same")
}
return nil
}
// Verify that proof is valid absence proof for key.
// Does not assume that the proof itself is valid.
// For that, use Verify(root).
func (proof *RangeProof) VerifyAbsence(key []byte) error {
if proof == nil {
return errors.Wrap(ErrInvalidProof, "proof is nil")
}
if !proof.rootVerified {
return errors.New("must call Verify(root) first")
}
cmp := bytes.Compare(key, proof.Leaves[0].Key)
if cmp < 0 {
if proof.LeftPath.isLeftmost() {
return nil
}
return errors.New("absence not proved by left path")
} else if cmp == 0 {
return errors.New("absence disproved via first item #0")
}
if len(proof.LeftPath) == 0 {
return nil // proof ok
}
if proof.LeftPath.isRightmost() {
return nil
}
// See if any of the leaves are greater than key.
for i := 1; i < len(proof.Leaves); i++ {
leaf := proof.Leaves[i]
cmp := bytes.Compare(key, leaf.Key)
switch {
case cmp < 0:
return nil // proof ok
case cmp == 0:
return fmt.Errorf("absence disproved via item #%v", i)
default:
// if i == len(proof.Leaves)-1 {
// If last item, check whether
// it's the last item in the tree.
// }
continue
}
}
// It's still a valid proof if our last leaf is the rightmost child.
if proof.treeEnd {
return nil // OK!
}
// It's not a valid absence proof.
if len(proof.Leaves) < 2 {
return errors.New("absence not proved by right leaf (need another leaf?)")
}
return errors.New("absence not proved by right leaf")
}
// Verify that proof is valid.
func (proof *RangeProof) Verify(root []byte) error {
if proof == nil {
return errors.Wrap(ErrInvalidProof, "proof is nil")
}
err := proof.verify(root)
return err
}
func (proof *RangeProof) verify(root []byte) (err error) {
rootHash := proof.rootHash
if rootHash == nil {
derivedHash, err := proof.computeRootHash()
if err != nil {
return err
}
rootHash = derivedHash
}
if !bytes.Equal(rootHash, root) {
return errors.Wrap(ErrInvalidRoot, "root hash doesn't match")
}
proof.rootVerified = true
return nil
}
// ComputeRootHash computes the root hash with leaves.
// Returns nil if error or proof is nil.
// Does not verify the root hash.
func (proof *RangeProof) ComputeRootHash() []byte {
if proof == nil {
return nil
}
rootHash, _ := proof.computeRootHash()
return rootHash
}
func (proof *RangeProof) computeRootHash() (rootHash []byte, err error) {
rootHash, treeEnd, err := proof._computeRootHash()
if err == nil {
proof.rootHash = rootHash // memoize
proof.treeEnd = treeEnd // memoize
}
return rootHash, err
}
func (proof *RangeProof) _computeRootHash() (rootHash []byte, treeEnd bool, err error) {
if len(proof.Leaves) == 0 {
return nil, false, errors.Wrap(ErrInvalidProof, "no leaves")
}
if len(proof.InnerNodes)+1 != len(proof.Leaves) {
return nil, false, errors.Wrap(ErrInvalidProof, "InnerNodes vs Leaves length mismatch, leaves should be 1 more.") //nolint:revive
}
// Start from the left path and prove each leaf.
// shared across recursive calls
var leaves = proof.Leaves
var innersq = proof.InnerNodes
var COMPUTEHASH func(path PathToLeaf, rightmost bool) (hash []byte, treeEnd bool, done bool, err error)
// rightmost: is the root a rightmost child of the tree?
// treeEnd: true iff the last leaf is the last item of the tree.
// Returns the (possibly intermediate, possibly root) hash.
COMPUTEHASH = func(path PathToLeaf, rightmost bool) (hash []byte, treeEnd bool, done bool, err error) {
// Pop next leaf.
nleaf, rleaves := leaves[0], leaves[1:]
leaves = rleaves
// Compute hash.
hash, err = (pathWithLeaf{
Path: path,
Leaf: nleaf,
}).computeRootHash()
if err != nil {
return nil, treeEnd, false, err
}
// If we don't have any leaves left, we're done.
if len(leaves) == 0 {
rightmost = rightmost && path.isRightmost()
return hash, rightmost, true, nil
}
// Prove along path (until we run out of leaves).
for len(path) > 0 {
// Drop the leaf-most (last-most) inner nodes from path
// until we encounter one with a left hash.
// We assume that the left side is already verified.
// rpath: rest of path
// lpath: last path item
rpath, lpath := path[:len(path)-1], path[len(path)-1]
path = rpath
if len(lpath.Right) == 0 {
continue
}
// Pop next inners, a PathToLeaf (e.g. []ProofInnerNode).
inners, rinnersq := innersq[0], innersq[1:]
innersq = rinnersq
// Recursively verify inners against remaining leaves.
derivedRoot, treeEnd, done, err := COMPUTEHASH(inners, rightmost && rpath.isRightmost())
if err != nil {
return nil, treeEnd, false, errors.Wrap(err, "recursive COMPUTEHASH call")
}
if !bytes.Equal(derivedRoot, lpath.Right) {
return nil, treeEnd, false, errors.Wrapf(ErrInvalidRoot, "intermediate root hash %X doesn't match, got %X", lpath.Right, derivedRoot)
}
if done {
return hash, treeEnd, true, nil
}
}
// We're not done yet (leaves left over). No error, not done either.
// Technically if rightmost, we know there's an error "left over leaves
// -- malformed proof", but we return that at the top level, below.
return hash, false, false, nil
}
// Verify!
path := proof.LeftPath
rootHash, treeEnd, done, err := COMPUTEHASH(path, true)
if err != nil {
return nil, treeEnd, errors.Wrap(err, "root COMPUTEHASH call")
} else if !done {
return nil, treeEnd, errors.Wrap(ErrInvalidProof, "left over leaves -- malformed proof")
}
// Ok!
return rootHash, treeEnd, nil
}
// toProto converts the proof to a Protobuf representation, for use in ValueOp and AbsenceOp.
func (proof *RangeProof) ToProto() *iavlproto.RangeProof {
pb := &iavlproto.RangeProof{
LeftPath: make([]*iavlproto.ProofInnerNode, 0, len(proof.LeftPath)),
InnerNodes: make([]*iavlproto.PathToLeaf, 0, len(proof.InnerNodes)),
Leaves: make([]*iavlproto.ProofLeafNode, 0, len(proof.Leaves)),
}
for _, inner := range proof.LeftPath {
pb.LeftPath = append(pb.LeftPath, inner.toProto())
}
for _, path := range proof.InnerNodes {
pbPath := make([]*iavlproto.ProofInnerNode, 0, len(path))
for _, inner := range path {
pbPath = append(pbPath, inner.toProto())
}
pb.InnerNodes = append(pb.InnerNodes, &iavlproto.PathToLeaf{Inners: pbPath})
}
for _, leaf := range proof.Leaves {
pb.Leaves = append(pb.Leaves, leaf.toProto())
}
return pb
}
// rangeProofFromProto generates a RangeProof from a Protobuf RangeProof.
func RangeProofFromProto(pbProof *iavlproto.RangeProof) (RangeProof, error) {
proof := RangeProof{}
for _, pbInner := range pbProof.LeftPath {
inner, err := proofInnerNodeFromProto(pbInner)
if err != nil {
return proof, err
}
proof.LeftPath = append(proof.LeftPath, inner)
}
for _, pbPath := range pbProof.InnerNodes {
var path PathToLeaf // leave as nil unless populated, for Amino compatibility
if pbPath != nil {
for _, pbInner := range pbPath.Inners {
inner, err := proofInnerNodeFromProto(pbInner)
if err != nil {
return proof, err
}
path = append(path, inner)
}
}
proof.InnerNodes = append(proof.InnerNodes, path)
}
for _, pbLeaf := range pbProof.Leaves {
leaf, err := proofLeafNodeFromProto(pbLeaf)
if err != nil {
return proof, err
}
proof.Leaves = append(proof.Leaves, leaf)
}
return proof, nil
}
// keyStart is inclusive and keyEnd is exclusive.
// If keyStart or keyEnd don't exist, the leaf before keyStart
// or after keyEnd will also be included, but not be included in values.
// If keyEnd-1 exists, no later leaves will be included.
// If keyStart >= keyEnd and both not nil, errors out.
// Limit is never exceeded.
func (t *ImmutableTree) getRangeProof(keyStart, keyEnd []byte, limit int) (proof *RangeProof, keys, values [][]byte, err error) {
if keyStart != nil && keyEnd != nil && bytes.Compare(keyStart, keyEnd) >= 0 {
return nil, nil, nil, fmt.Errorf("if keyStart and keyEnd are present, need keyStart < keyEnd")
}
if limit < 0 {
return nil, nil, nil, fmt.Errorf("limit must be greater or equal to 0 -- 0 means no limit")
}
if t.root == nil {
return nil, nil, nil, nil
}
_, _, err = t.root.hashWithCount() // Ensure that all hashes are calculated.
if err != nil {
return nil, nil, nil, err
}
// Get the first key/value pair proof, which provides us with the left key.
path, left, err := t.root.PathToLeaf(t, keyStart)
if err != nil {
// Key doesn't exist, but instead we got the prev leaf (or the
// first or last leaf), which provides proof of absence).
err = nil
}
startOK := keyStart == nil || bytes.Compare(keyStart, left.GetNodeKey()) <= 0
endOK := keyEnd == nil || bytes.Compare(left.GetNodeKey(), keyEnd) < 0
// If left.key is in range, add it to key/values.
if startOK && endOK {
keys = append(keys, left.GetNodeKey()) // == keyStart
values = append(values, left.GetValue())
}
h := sha256.Sum256(left.GetValue())
var leaves = []ProofLeafNode{
{
Key: left.GetNodeKey(),
ValueHash: h[:],
Version: left.GetVersion(),
},
}
// 1: Special case if limit is 1.
// 2: Special case if keyEnd is left.key+1.
_stop := false
if limit == 1 {
_stop = true // case 1
} else if keyEnd != nil && bytes.Compare(cpIncr(left.GetNodeKey()), keyEnd) >= 0 {
_stop = true // case 2
}
if _stop {
return &RangeProof{
LeftPath: path,
Leaves: leaves,
}, keys, values, nil
}
// Get the key after left.key to iterate from.
afterLeft := cpIncr(left.GetNodeKey())
// Traverse starting from afterLeft, until keyEnd or the next leaf
// after keyEnd.
var allPathToLeafs = []PathToLeaf(nil)
var currentPathToLeaf = PathToLeaf(nil)
var leafCount = 1 // from left above.
var pathCount = 0
t.root.traverseInRange(t, afterLeft, nil, true, false, false,
func(node *Node) (stop bool) {
// Track when we diverge from path, or when we've exhausted path,
// since the first allPathToLeafs shouldn't include it.
if pathCount != -1 {
if len(path) <= pathCount {
// We're done with path counting.
pathCount = -1
} else {
pn := path[pathCount]
if pn.Height != node.GetHeight() ||
pn.Left != nil && !bytes.Equal(pn.Left, node.GetLeftHash()) ||
pn.Right != nil && !bytes.Equal(pn.Right, node.GetRightHash()) {
// We've diverged, so start appending to allPathToLeaf.
pathCount = -1
} else {
pathCount++
}
}
}
if node.GetHeight() == 0 { // Leaf node
// Append all paths that we tracked so far to get to this leaf node.
allPathToLeafs = append(allPathToLeafs, currentPathToLeaf)
// Start a new one to track as we traverse the tree.
currentPathToLeaf = PathToLeaf(nil)
h := sha256.Sum256(node.GetValue())
leaves = append(leaves, ProofLeafNode{
Key: node.GetNodeKey(),
ValueHash: h[:],
Version: node.GetVersion(),
})
leafCount++
// Maybe terminate because we found enough leaves.
if limit > 0 && limit <= leafCount {
return true
}
// Terminate if we've found keyEnd or after.
if keyEnd != nil && bytes.Compare(node.GetNodeKey(), keyEnd) >= 0 {
return true
}
// Value is in range, append to keys and values.
keys = append(keys, node.GetNodeKey())
values = append(values, node.GetValue())
// Terminate if we've found keyEnd-1 or after.
// We don't want to fetch any leaves for it.
if keyEnd != nil && bytes.Compare(cpIncr(node.GetNodeKey()), keyEnd) >= 0 {
return true
}
} else if pathCount < 0 { // Inner node.
// Only store if the node is not stored in currentPathToLeaf already. We track if we are
// still going through PathToLeaf using pathCount. When pathCount goes to -1, we
// start storing the other paths we took to get to the leaf nodes. Also we skip
// storing the left node, since we are traversing the tree starting from the left
// and don't need to store unnecessary info as we only need to go down the right
// path.
currentPathToLeaf = append(currentPathToLeaf, ProofInnerNode{
Height: node.GetHeight(),
Size: node.GetSize(),
Version: node.GetVersion(),
Left: nil,
Right: node.GetRightHash(),
})
}
return false
},
)
return &RangeProof{
LeftPath: path,
InnerNodes: allPathToLeafs,
Leaves: leaves,
}, keys, values, nil
}
//----------------------------------------
// GetWithProof gets the value under the key if it exists, or returns nil.
// A proof of existence or absence is returned alongside the value.
func (t *ImmutableTree) GetWithProof(key []byte) (value []byte, proof *RangeProof, err error) {
proof, _, values, err := t.getRangeProof(key, cpIncr(key), 2)
if err != nil {
return nil, nil, errors.Wrap(err, "constructing range proof")
}
if len(values) > 0 && bytes.Equal(proof.Leaves[0].Key, key) {
return values[0], proof, nil
}
return nil, proof, nil
}
// GetRangeWithProof gets key/value pairs within the specified range and limit.
func (t *ImmutableTree) GetRangeWithProof(startKey []byte, endKey []byte, limit int) (keys, values [][]byte, proof *RangeProof, err error) {
proof, keys, values, err = t.getRangeProof(startKey, endKey, limit)
return
}
// GetVersionedWithProof gets the value under the key at the specified version
// if it exists, or returns nil.
func (tree *MutableTree) GetVersionedWithProof(key []byte, version int64) ([]byte, *RangeProof, error) {
if tree.VersionExists(version) {
t, err := tree.GetImmutable(version)
if err != nil {
return nil, nil, err
}
return t.GetWithProof(key)
}
return nil, nil, errors.Wrap(ErrVersionDoesNotExist, "")
}
// GetVersionedRangeWithProof gets key/value pairs within the specified range
// and limit.
func (tree *MutableTree) GetVersionedRangeWithProof(startKey, endKey []byte, limit int, version int64) (
keys, values [][]byte, proof *RangeProof, err error) {
if tree.VersionExists(version) {
t, err := tree.GetImmutable(version)
if err != nil {
return nil, nil, nil, err
}
return t.GetRangeWithProof(startKey, endKey, limit)
}
return nil, nil, nil, errors.Wrap(ErrVersionDoesNotExist, "")
}