forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
proof.go
692 lines (614 loc) · 23 KB
/
proof.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package merkledb
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/database/memdb"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/trace"
"github.com/MetalBlockchain/metalgo/utils/hashing"
)
const verificationCacheSize = 2_000
var (
ErrInvalidProof = errors.New("proof obtained an invalid root ID")
ErrInvalidMaxLength = errors.New("expected max length to be > 0")
ErrNonIncreasingValues = errors.New("keys sent are not in increasing order")
ErrStateFromOutsideOfRange = errors.New("state key falls outside of the start->end range")
ErrNonIncreasingProofNodes = errors.New("each proof node key must be a strict prefix of the next")
ErrExtraProofNodes = errors.New("extra proof nodes in path")
ErrDataInMissingRootProof = errors.New("there should be no state or deleted keys in a change proof that had a missing root")
ErrNoMerkleProof = errors.New("empty key response must include merkle proof")
ErrShouldJustBeRoot = errors.New("end proof should only contain root")
ErrNoStartProof = errors.New("no start proof")
ErrNoEndProof = errors.New("no end proof")
ErrNoProof = errors.New("proof has no nodes")
ErrProofNodeNotForKey = errors.New("the provided node has a key that is not a prefix of the specified key")
ErrProofValueDoesntMatch = errors.New("the provided value does not match the proof node for the provided key's value")
ErrProofNodeHasUnincludedValue = errors.New("the provided proof has a value for a key within the range that is not present in the provided key/values")
)
type ProofNode struct {
KeyPath SerializedPath
// Nothing if this is an intermediate node.
// The value in this node if its length < [HashLen].
// The hash of the value in this node otherwise.
ValueOrHash Maybe[[]byte]
Children map[byte]ids.ID
}
// An inclusion/exclustion proof of a key.
type Proof struct {
// Nodes in the proof path from root --> target key
// (or node that would be where key is if it doesn't exist).
// Must always be non-empty (i.e. have the root node).
Path []ProofNode
// This is a proof that [key] exists/doesn't exist.
Key []byte
// Nothing if [Key] isn't in the trie.
// Otherwise the value corresponding to [Key].
Value Maybe[[]byte]
}
// Returns nil if the trie given in [proof] has root [expectedRootID].
// That is, this is a valid proof that [proof.Key] exists/doesn't exist
// in the trie with root [expectedRootID].
func (proof *Proof) Verify(ctx context.Context, expectedRootID ids.ID) error {
// Make sure the proof is well-formed.
if len(proof.Path) == 0 {
return ErrNoProof
}
if err := verifyProofPath(proof.Path, newPath(proof.Key)); err != nil {
return err
}
// Confirm that the last proof node's value matches the claimed proof value
lastNode := proof.Path[len(proof.Path)-1]
// If the last proof node's key is [proof.Key] (i.e. this is an inclusion proof)
// then the value of the last proof node must match [proof.Value].
// Note odd length keys can never match the [proof.Key] since it's bytes,
// and thus an even number of nibbles.
if !lastNode.KeyPath.hasOddLength() &&
bytes.Equal(proof.Key, lastNode.KeyPath.Value) &&
!valueOrHashMatches(proof.Value, lastNode.ValueOrHash) {
return ErrProofValueDoesntMatch
}
// If the last proof node has an odd length or a different key than [proof.Key]
// then this is an exclusion proof and should prove that [proof.Key] isn't in the trie..
// Note odd length keys can never match the [proof.Key] since it's bytes,
// and thus an even number of nibbles.
if (lastNode.KeyPath.hasOddLength() || !bytes.Equal(proof.Key, lastNode.KeyPath.Value)) &&
!proof.Value.IsNothing() {
return ErrProofValueDoesntMatch
}
view, err := getEmptyTrieView(ctx)
if err != nil {
return err
}
// Insert all of the proof nodes.
// [provenPath] is the path that we are proving exists, or the path
// that is where the path we are proving doesn't exist should be.
provenPath := proof.Path[len(proof.Path)-1].KeyPath.deserialize()
// Don't bother locking [db] and [view] -- nobody else has a reference to them.
if err = addPathInfo(view, proof.Path, provenPath, provenPath); err != nil {
return err
}
gotRootID, err := view.GetMerkleRoot(ctx)
if err != nil {
return err
}
if expectedRootID != gotRootID {
return fmt.Errorf("%w:[%s], expected:[%s]", ErrInvalidProof, gotRootID, expectedRootID)
}
return nil
}
type KeyValue struct {
Key []byte
Value []byte
}
// A proof that a given set of key-value pairs are in a trie.
type RangeProof struct {
// A proof that the smallest key in the requested range does/doesn't exist.
// Note that this may not be an entire proof -- nodes are omitted if
// they are also in [EndProof].
StartProof []ProofNode
// A proof of the greatest key in [KeyValues], or, if this proof contains
// no [KeyValues], just the root.
// Empty if the request for this range proof gave no upper bound
// on the range to fetch, unless this proof contains no [KeyValues]
// and [StartProof] is empty.
EndProof []ProofNode
// This proof proves that the key-value pairs in [KeyValues] are in the trie.
// Sorted by increasing key.
KeyValues []KeyValue
}
// Returns nil iff all the following hold:
// - [start] <= [end].
// - [proof] is non-empty.
// - All keys in [proof.KeyValues] are in the range [start, end].
// If [start] is empty, all keys are considered > [start].
// If [end] is empty, all keys are considered < [end].
// - [proof.KeyValues] is sorted by increasing key.
// - [proof.StartProof] and [proof.EndProof] are well-formed.
// - One of the following holds:
// [end] and [proof.EndProof] are empty.
// [proof.StartProof], [start], [end], and [proof.KeyValues] are empty and
// [proof.EndProof] is just the root.
// [end] is non-empty and [proof.EndProof] is a valid proof of a key <= [end].
// - [expectedRootID] is the root of the trie containing the given key-value
// pairs and start/end proofs.
func (proof *RangeProof) Verify(
ctx context.Context,
start []byte,
end []byte,
expectedRootID ids.ID,
) error {
switch {
case len(end) > 0 && bytes.Compare(start, end) > 0:
return ErrStartAfterEnd
case len(proof.KeyValues) == 0 && len(proof.StartProof) == 0 && len(proof.EndProof) == 0:
return ErrNoMerkleProof
case len(start) == 0 && len(end) == 0 && len(proof.KeyValues) == 0 && len(proof.EndProof) != 1:
return ErrShouldJustBeRoot
case len(proof.EndProof) == 0 && len(end) > 0:
return ErrNoEndProof
}
// Make sure the key-value pairs are sorted and in [start, end].
if err := verifyKeyValues(proof.KeyValues, start, end); err != nil {
return err
}
largestkey := end
if len(proof.KeyValues) > 0 {
// If [proof] has key-value pairs, we should insert children
// greater than [end] to ancestors of the node containing [end]
// so that we get the expected root ID.
largestkey = proof.KeyValues[len(proof.KeyValues)-1].Key
}
// The key-value pairs (allegedly) proven by [proof].
keyValues := make(map[path][]byte, len(proof.KeyValues))
for _, keyValue := range proof.KeyValues {
keyValues[newPath(keyValue.Key)] = keyValue.Value
}
smallestPath := newPath(start)
largestPath := newPath(largestkey)
// Ensure that the start proof is valid and contains values that
// match the key/values that were sent.
if err := verifyProofPath(proof.StartProof, smallestPath); err != nil {
return err
}
if err := verifyAllRangeProofKeyValuesPresent(proof.StartProof, smallestPath, largestPath, keyValues); err != nil {
return err
}
// Ensure that the end proof is valid and contains values that
// match the key/values that were sent.
if err := verifyProofPath(proof.EndProof, largestPath); err != nil {
return err
}
if err := verifyAllRangeProofKeyValuesPresent(proof.EndProof, smallestPath, largestPath, keyValues); err != nil {
return err
}
// Don't need to lock [view] because nobody else has a reference to it.
view, err := getEmptyTrieView(ctx)
if err != nil {
return err
}
// Insert all key-value pairs into the trie.
for _, kv := range proof.KeyValues {
if _, err := view.insertIntoTrie(newPath(kv.Key), Some(kv.Value)); err != nil {
return err
}
}
// For all the nodes along the edges of the proof, insert children < [start] and > [end]
// into the trie so that we get the expected root ID (if this proof is valid).
// By inserting all children < [start], we prove that there are no keys
// > [start] but less than the first key given. That is, the peer who
// gave us this proof is not omitting nodes.
if err := addPathInfo(view, proof.StartProof, smallestPath, largestPath); err != nil {
return err
}
if err := addPathInfo(view, proof.EndProof, smallestPath, largestPath); err != nil {
return err
}
calculatedRoot, err := view.GetMerkleRoot(ctx)
if err != nil {
return err
}
if expectedRootID != calculatedRoot {
return fmt.Errorf("%w:[%s], expected:[%s]", ErrInvalidProof, calculatedRoot, expectedRootID)
}
return nil
}
// Verify that all non-intermediate nodes in [proof] which have keys
// in [[start], [end]] have the value given for that key in [keysValues].
func verifyAllRangeProofKeyValuesPresent(proof []ProofNode, start, end path, keysValues map[path][]byte) error {
for i := 0; i < len(proof); i++ {
var (
node = proof[i]
nodeKey = node.KeyPath
nodePath = nodeKey.deserialize()
)
// Skip odd length keys since they cannot have a value (enforced by [verifyProofPath]).
if !nodeKey.hasOddLength() && nodePath.Compare(start) >= 0 && nodePath.Compare(end) <= 0 {
value, ok := keysValues[nodePath]
if !ok && !node.ValueOrHash.IsNothing() {
// We didn't get a key-value pair for this key, but the proof node has a value.
return ErrProofNodeHasUnincludedValue
}
if ok && !valueOrHashMatches(Some(value), node.ValueOrHash) {
// We got a key-value pair for this key, but the value in the proof
// node doesn't match the value we got for this key.
return ErrProofValueDoesntMatch
}
}
}
return nil
}
type KeyChange struct {
Key []byte
Value Maybe[[]byte]
}
type ChangeProof struct {
// If false, the node that created this doesn't have
// sufficient history to generate a change proof and
// all other fields must be empty.
// Otherwise at least one other field is non-empty.
HadRootsInHistory bool
// A proof that the smallest key in the requested range does/doesn't
// exist in the trie with the requested start root.
// Empty if no lower bound on the requested range was given.
// Note that this may not be an entire proof -- nodes are omitted if
// they are also in [EndProof].
StartProof []ProofNode
// A proof that the largest key in [KeyValues] and [DeletedKeys]
// does/doesn't exist in the trie with the requested start root.
// Empty iff no upper bound on the requested range was given
// and [KeyValues] and [DeletedKeys] are empty.
EndProof []ProofNode
// A subset of key-values that were added, removed, or had their values modified
// between the requested start root (exclusive) and the requested
// end root (inclusive).
// Sorted by increasing key.
KeyChanges []KeyChange
}
// Returns nil iff all of the following hold:
// - [start] <= [end].
// - [proof] is non-empty iff [proof.HadRootsInHistory].
// - All keys in [proof.KeyValues] and [proof.DeletedKeys] are in [start, end].
// If [start] is empty, all keys are considered > [start].
// If [end] is empty, all keys are considered < [end].
// - [proof.KeyValues] and [proof.DeletedKeys] are sorted in order of increasing key.
// - [proof.StartProof] and [proof.EndProof] are well-formed.
// - When the keys in [proof.KeyValues] are added to [db] and the keys in [proof.DeletedKeys]
// are removed from [db], the root ID of [db] is [expectedEndRootID].
//
// Assumes [db.lock] isn't held.
func (proof *ChangeProof) Verify(
ctx context.Context,
db *Database,
start []byte,
end []byte,
expectedEndRootID ids.ID,
) error {
if len(end) > 0 && bytes.Compare(start, end) > 0 {
return ErrStartAfterEnd
}
if !proof.HadRootsInHistory {
// The node we requested the proof from didn't have sufficient
// history to fulfill this request.
if !proof.Empty() {
// cannot have any changes if the root was missing
return ErrDataInMissingRootProof
}
return nil
}
switch {
case proof.Empty():
return ErrNoMerkleProof
case len(end) > 0 && len(proof.EndProof) == 0:
// We requested an end proof but didn't get one.
return ErrNoEndProof
case len(start) > 0 && len(proof.StartProof) == 0 && len(proof.EndProof) == 0:
// We requested a start proof but didn't get one.
// Note that we also have to check that [proof.EndProof] is empty
// to handle the case that the start proof is empty because all
// its nodes are also in the end proof, and those nodes are omitted.
return ErrNoStartProof
}
// Make sure the key-value pairs are sorted and in [start, end].
if err := verifyKeyChanges(proof.KeyChanges, start, end); err != nil {
return err
}
smallestPath := newPath(start)
// Make sure the start proof, if given, is well-formed.
if err := verifyProofPath(proof.StartProof, smallestPath); err != nil {
return err
}
// Find the greatest key in [proof.KeyChanges]
// Note that [proof.EndProof] is a proof for this key.
// [largestPath] is also used when we add children of proof nodes to [trie] below.
largestKey := end
if len(proof.KeyChanges) > 0 {
// If [proof] has key-value pairs, we should insert children
// greater than [end] to ancestors of the node containing [end]
// so that we get the expected root ID.
largestKey = proof.KeyChanges[len(proof.KeyChanges)-1].Key
}
largestPath := newPath(largestKey)
// Make sure the end proof, if given, is well-formed.
if err := verifyProofPath(proof.EndProof, largestPath); err != nil {
return err
}
keyValues := make(map[path]Maybe[[]byte], len(proof.KeyChanges))
for _, keyValue := range proof.KeyChanges {
keyValues[newPath(keyValue.Key)] = keyValue.Value
}
// want to prevent commit writes to DB, but not prevent db reads
db.commitLock.RLock()
defer db.commitLock.RUnlock()
if err := verifyAllChangeProofKeyValuesPresent(
ctx,
db,
proof.StartProof,
smallestPath,
largestPath,
keyValues,
); err != nil {
return err
}
if err := verifyAllChangeProofKeyValuesPresent(
ctx,
db,
proof.EndProof,
smallestPath,
largestPath,
keyValues,
); err != nil {
return err
}
// Don't need to lock [view] because nobody else has a reference to it.
view, err := db.newUntrackedView(len(proof.KeyChanges))
if err != nil {
return err
}
// Insert the key-value pairs into the trie.
for _, kv := range proof.KeyChanges {
if kv.Value.IsNothing() {
if err := view.removeFromTrie(newPath(kv.Key)); err != nil {
return err
}
} else if _, err := view.insertIntoTrie(newPath(kv.Key), kv.Value); err != nil {
return err
}
}
// For all the nodes along the edges of the proof, insert children < [start] and > [largestKey]
// into the trie so that we get the expected root ID (if this proof is valid).
if err := addPathInfo(view, proof.StartProof, smallestPath, largestPath); err != nil {
return err
}
if err := addPathInfo(view, proof.EndProof, smallestPath, largestPath); err != nil {
return err
}
// Make sure we get the expected root.
calculatedRoot, err := view.getMerkleRoot(ctx)
if err != nil {
return err
}
if expectedEndRootID != calculatedRoot {
return fmt.Errorf("%w:[%s], expected:[%s]", ErrInvalidProof, calculatedRoot, expectedEndRootID)
}
return nil
}
// Verifies that all values present in the [proof]:
// - Are nothing when deleted, not in the db, or the node has an odd path length.
// - if the node's path is within the key range, that has a value that matches the value passed in the change list or in the db
func verifyAllChangeProofKeyValuesPresent(
ctx context.Context,
db *Database,
proof []ProofNode,
start path,
end path,
keysValues map[path]Maybe[[]byte],
) error {
for i := 0; i < len(proof); i++ {
var (
node = proof[i]
nodeKey = node.KeyPath
nodePath = nodeKey.deserialize()
)
// Check the value of any node with a key that is within the range.
// Skip odd length keys since they cannot have a value (enforced by [verifyProofPath]).
if !nodeKey.hasOddLength() && nodePath.Compare(start) >= 0 && nodePath.Compare(end) <= 0 {
value, ok := keysValues[nodePath]
if !ok {
// This value isn't in the list of key-value pairs we got.
dbValue, err := db.GetValue(ctx, nodeKey.Value)
if err != nil {
if err != database.ErrNotFound {
return err
}
// This key isn't in the database so proof node should have Nothing.
value = Nothing[[]byte]()
} else {
// This key is in the database so proof node should have matching value.
value = Some(dbValue)
}
}
if !valueOrHashMatches(value, node.ValueOrHash) {
return ErrProofValueDoesntMatch
}
}
}
return nil
}
func (proof *ChangeProof) Empty() bool {
return len(proof.KeyChanges) == 0 &&
len(proof.StartProof) == 0 && len(proof.EndProof) == 0
}
// Returns nil iff both hold:
// 1. [kvs] is sorted by key in increasing order.
// 2. All keys in [kvs] are in the range [start, end].
// If [start] is nil, there is no lower bound on acceptable keys.
// If [end] is nil, there is no upper bound on acceptable keys.
// If [kvs] is empty, returns nil.
func verifyKeyChanges(kvs []KeyChange, start, end []byte) error {
if len(kvs) == 0 {
return nil
}
// ensure that the keys are in increasing order
for i := 0; i < len(kvs)-1; i++ {
if bytes.Compare(kvs[i].Key, kvs[i+1].Key) >= 0 {
return ErrNonIncreasingValues
}
}
// ensure that the keys are within the range [start, end]
if (len(start) > 0 && bytes.Compare(kvs[0].Key, start) < 0) ||
(len(end) > 0 && bytes.Compare(kvs[len(kvs)-1].Key, end) > 0) {
return ErrStateFromOutsideOfRange
}
return nil
}
// Returns nil iff both hold:
// 1. [kvs] is sorted by key in increasing order.
// 2. All keys in [kvs] are in the range [start, end].
// If [start] is nil, there is no lower bound on acceptable keys.
// If [end] is nil, there is no upper bound on acceptable keys.
// If [kvs] is empty, returns nil.
func verifyKeyValues(kvs []KeyValue, start, end []byte) error {
hasLowerBound := len(start) > 0
hasUpperBound := len(end) > 0
for i := 0; i < len(kvs); i++ {
if i < len(kvs)-1 && bytes.Compare(kvs[i].Key, kvs[i+1].Key) >= 0 {
return ErrNonIncreasingValues
}
if (hasLowerBound && bytes.Compare(kvs[i].Key, start) < 0) ||
(hasUpperBound && bytes.Compare(kvs[i].Key, end) > 0) {
return ErrStateFromOutsideOfRange
}
}
return nil
}
// Returns nil iff all the following hold:
// - Any node with an odd nibble length, should not have a value associated with it
// since all keys with values are written in bytes, so have even nibble length.
// - Each key in [proof] is a strict prefix of the following key.
// - Each key in [proof] is a strict prefix of [keyBytes], except possibly the last.
// - If the last element in [proof] is [keyBytes], this is an inclusion proof.
// Otherwise, this is an exclusion proof and [keyBytes] must not be in [proof].
func verifyProofPath(proof []ProofNode, keyPath path) error {
provenKey := keyPath.Serialize()
// loop over all but the last node since it will not have the prefix in exclusion proofs
for i := 0; i < len(proof)-1; i++ {
nodeKey := proof[i].KeyPath
// intermediate nodes (nodes with odd nibble length) should never have a value associated with them
if nodeKey.hasOddLength() && !proof[i].ValueOrHash.IsNothing() {
return ErrOddLengthWithValue
}
// each node should have a key that has the proven key as a prefix
if !provenKey.HasStrictPrefix(nodeKey) {
return ErrProofNodeNotForKey
}
// each node should have a key that is a prefix of the next node's key
nextKey := proof[i+1].KeyPath
if !nextKey.HasStrictPrefix(nodeKey) {
return ErrNonIncreasingProofNodes
}
}
// check the last node for a value since the above loop doesn't check the last node
if len(proof) > 0 {
lastNode := proof[len(proof)-1]
if lastNode.KeyPath.hasOddLength() && !lastNode.ValueOrHash.IsNothing() {
return ErrOddLengthWithValue
}
}
return nil
}
// Returns true if [value] and [valueDigest] match.
// [valueOrHash] should be the [ValueOrHash] field of a [ProofNode].
func valueOrHashMatches(value Maybe[[]byte], valueOrHash Maybe[[]byte]) bool {
var (
valueIsNothing = value.IsNothing()
digestIsNothing = valueOrHash.IsNothing()
)
switch {
case valueIsNothing != digestIsNothing:
// One is nothing and the other isn't -- no match.
return false
case valueIsNothing:
// Both are nothing -- match.
return true
case len(value.value) < HashLength:
return bytes.Equal(value.value, valueOrHash.value)
default:
valueHash := hashing.ComputeHash256(value.value)
return bytes.Equal(valueHash, valueOrHash.value)
}
}
// Adds each key/value pair in [proofPath] to [t].
// For each proof node, adds the children that are < [start] or > [end].
// If [start] is empty, no children are < [start].
// If [end] is empty, no children are > [end].
// Assumes [t.lock] is held.
func addPathInfo(
t *trieView,
proofPath []ProofNode,
startPath path,
endPath path,
) error {
var (
hasLowerBound = len(startPath) > 0
hasUpperBound = len(endPath) > 0
)
for i := len(proofPath) - 1; i >= 0; i-- {
proofNode := proofPath[i]
keyPath := proofNode.KeyPath.deserialize()
if len(keyPath)&1 == 1 && !proofNode.ValueOrHash.IsNothing() {
// a value cannot have an odd number of nibbles in its key
return ErrOddLengthWithValue
}
// load the node associated with the key or create a new one
// pass nothing because we are going to overwrite the value digest below
n, err := t.insertIntoTrie(keyPath, Nothing[[]byte]())
if err != nil {
return err
}
// We overwrite the valueDigest to be the hash provided in the proof
// node because we may not know the pre-image of the valueDigest.
n.valueDigest = proofNode.ValueOrHash
if !hasLowerBound && !hasUpperBound {
// No children of proof nodes are outside the range.
// No need to add any children to [n].
continue
}
// Add [proofNode]'s children which are outside the range [start, end].
compressedPath := EmptyPath
for index, childID := range proofNode.Children {
if existingChild, ok := n.children[index]; ok {
compressedPath = existingChild.compressedPath
}
childPath := keyPath.Append(index) + compressedPath
if (hasLowerBound && childPath.Compare(startPath) < 0) ||
(hasUpperBound && childPath.Compare(endPath) > 0) {
n.addChildWithoutNode(index, compressedPath, childID)
}
}
}
return nil
}
func getEmptyTrieView(ctx context.Context) (*trieView, error) {
tracer, err := trace.New(trace.Config{Enabled: false})
if err != nil {
return nil, err
}
db, err := newDatabase(
ctx,
memdb.New(),
Config{
Tracer: tracer,
NodeCacheSize: verificationCacheSize,
},
&mockMetrics{},
)
if err != nil {
return nil, err
}
return db.newUntrackedView(defaultPreallocationSize)
}