forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
trieview.go
1384 lines (1172 loc) · 39.9 KB
/
trieview.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package merkledb
import (
"bytes"
"context"
"errors"
"fmt"
"runtime"
"sync"
"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/buffer"
"github.com/MetalBlockchain/metalgo/utils/set"
)
const (
defaultPreallocationSize = 100
minNodeCountForConcurrentHashing = 500
initialProofPathSize = 16
)
var (
ErrCommitted = errors.New("view has been committed")
ErrChangedBaseRoot = errors.New("the trie this view was based on has changed its root")
ErrEditLocked = errors.New(
"view has been edit locked. Any view generated from this view would be corrupted by edits",
)
ErrOddLengthWithValue = errors.New(
"the underlying db only supports whole number of byte keys, so cannot record changes with odd nibble length",
)
ErrGetClosestNodeFailure = errors.New("GetClosestNode failed to return the closest node")
ErrStartAfterEnd = errors.New("start key > end key")
_ TrieView = &trieView{}
)
// Editable view of a trie, collects changes on top of a base trie.
// Delays adding key/value pairs to the trie.
type trieView struct {
// Must be held when reading/writing fields.
lock sync.Mutex
changes *changeSummary
// Key/value pairs we've already fetched from [baseTrie].
// A Nothing value indicates that the key has been removed.
baseValuesCache map[path]Maybe[[]byte]
// Key/value pairs that have been inserted/removed but not
// yet reflected in the trie's structure. This allows us to
// defer the cost of updating the trie until we calculate node IDs.
// A Nothing value indicates that the key has been removed.
unappliedValueChanges map[path]Maybe[[]byte]
// The trie below this one in the current view stack.
// This is either [baseView] or [db].
// Used to get information missing from the local view.
baseTrie Trie
// The root of [db] when this view was created.
basedOnRoot ids.ID
db *Database
// the view that this view is based upon (if it exists, nil otherwise).
// If non-nil, is [baseTrie].
baseView *trieView
// The root of the trie represented by this view.
root *node
// Nodes we've already fetched from [baseTrie].
// A nil value indicates that the node isn't in [baseTrie].
baseNodesCache map[path]*node
// Key --> Parent of the node with that key.
parents map[path]*node
// True if the IDs of nodes in this view need to be recalculated.
needsRecalculation bool
// If true, this view has been committed and cannot be edited.
// Calls to Insert and Remove will return ErrCommitted.
committed bool
// If true, this view has been edit locked because another view
// exists atop it.
// Calls to Insert and Remove will return ErrEditLocked.
changeLocked bool
estimatedSize int
}
// Returns a new view on top of this one.
// Assumes this view stack is unlocked.
func (t *trieView) NewView(ctx context.Context) (TrieView, error) {
return t.NewPreallocatedView(ctx, defaultPreallocationSize)
}
// Returns a new view on top of this one with memory allocated to store the
// [estimatedChanges] number of key/value changes.
// Assumes this view stack is unlocked.
func (t *trieView) NewPreallocatedView(
ctx context.Context,
estimatedChanges int,
) (TrieView, error) {
t.lockStack()
defer t.unlockStack()
return newTrieView(ctx, t.db, t, nil, estimatedChanges)
}
// Creates a new view atop the given [baseView].
// If [baseView] is nil, the view is created atop [db].
// If [baseView] isn't nil, sets [baseView.changeLocked] to true.
// If [changes] is nil, a new changeSummary is created.
// Assumes [db.lock] is read locked.
// Assumes [baseView] is nil or locked.
func newTrieView(
ctx context.Context,
db *Database,
baseView *trieView,
changes *changeSummary,
estimatedSize int,
) (*trieView, error) {
if changes == nil {
changes = newChangeSummary(estimatedSize)
}
baseTrie := Trie(db)
if baseView != nil {
baseTrie = baseView
baseView.changeLocked = true
}
baseRoot := db.getMerkleRoot()
result := &trieView{
db: db,
baseView: baseView,
baseTrie: baseTrie,
basedOnRoot: baseRoot,
changes: changes,
estimatedSize: estimatedSize,
baseNodesCache: make(map[path]*node, defaultPreallocationSize),
baseValuesCache: make(map[path]Maybe[[]byte], defaultPreallocationSize),
parents: make(map[path]*node, 2*estimatedSize),
unappliedValueChanges: make(map[path]Maybe[[]byte], estimatedSize),
}
var err error
result.root, err = result.getNodeWithID(ctx, ids.Empty, RootPath)
return result, err
}
// Write locks this view and read locks all views/the database below it.
func (t *trieView) lockStack() {
t.lock.Lock()
t.baseTrie.lockStack()
}
func (t *trieView) unlockStack() {
t.baseTrie.unlockStack()
t.lock.Unlock()
}
// Calculates the IDs of all nodes in this trie.
func (t *trieView) CalculateIDs(ctx context.Context) error {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.CalculateIDs")
defer span.End()
t.lockStack()
defer t.unlockStack()
return t.calculateIDs(ctx)
}
// Recalculates the node IDs for all changed nodes in the trie.
// Assumes this view stack is locked.
func (t *trieView) calculateIDs(ctx context.Context) error {
if !t.needsRecalculation {
return nil
}
if t.committed {
// Note that this should never happen. If a view is committed, it should
// never be edited, so [t.needsRecalculation] should always be false.
return ErrCommitted
}
// We wait to create the span until after checking that we need to actually
// calculateIDs to make traces more useful (otherwise there may be a span
// per key modified even though IDs are not re-calculated).
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.calculateIDs")
defer span.End()
// ensure that the view under this one is up to date before potentially pulling in nodes from it
if t.baseView != nil {
if err := t.baseView.calculateIDs(ctx); err != nil {
return err
}
}
if err := t.applyChangedValuesToTrie(ctx); err != nil {
return err
}
_, topoSpan := t.db.tracer.Start(ctx, "MerkleDB.trieview.calculateIDs.topologicalSort")
seen := set.NewSet[path](len(t.changes.nodes) * 2)
dependencyCounts := make(map[path]int, len(t.changes.nodes))
readyNodes := make(map[path]*node, len(t.changes.nodes))
// determine all changed node's ancestors and gather dependency data
for key, nodeChange := range t.changes.nodes {
if seen.Contains(key) || nodeChange.after == nil {
continue
}
if _, ok := dependencyCounts[key]; !ok {
readyNodes[key] = nodeChange.after
}
currentNodeKey := key
parent := t.parents[nodeChange.after.key]
// all ancestors of a modified node need to have their ID updated
// if the ancestors have already been seen or there is no parent, we can stop
for !seen.Contains(currentNodeKey) && parent != nil {
// mark the previous node as handled
seen.Add(currentNodeKey)
// move on to the parent of the previous node
currentNodeKey = parent.key
// this node depends on the hash of the previous node, so add one to the dependency count
dependencyCounts[currentNodeKey]++
// this node has a dependency, so it cannot be ready
delete(readyNodes, currentNodeKey)
// move on to the next ancestor
parent = t.parents[parent.key]
}
}
topoSpan.End()
// perform hashing in topological order
var err error
if seen.Len() >= minNodeCountForConcurrentHashing {
err = t.calculateIDsConcurrent(ctx, readyNodes, dependencyCounts)
} else {
err = t.calculateIDsSync(ctx, readyNodes, dependencyCounts)
}
if err != nil {
return err
}
t.needsRecalculation = false
return nil
}
// Returns a proof that [bytesPath] is in or not in trie [t].
func (t *trieView) GetProof(ctx context.Context, key []byte) (*Proof, error) {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.GetProof")
defer span.End()
t.lockStack()
defer t.unlockStack()
if err := t.calculateIDs(ctx); err != nil {
return nil, err
}
return t.getProof(ctx, key)
}
// Returns a proof that [bytesPath] is in or not in trie [t].
// Assumes this view stack is locked.
func (t *trieView) getProof(ctx context.Context, key []byte) (*Proof, error) {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.getProof")
defer span.End()
proof := &Proof{
Key: key,
}
// Get the node at the given path, or the node closest to it.
keyPath := newPath(key)
closestNode, exact, err := t.getClosestNode(ctx, keyPath)
if err != nil {
return nil, err
}
proofPath := buffer.NewUnboundedDeque[ProofNode](initialProofPathSize)
currentNode := closestNode
for currentNode != nil {
proofPath.PushLeft(currentNode.asProofNode())
currentNode = t.parents[currentNode.key]
}
// From root --> node from left --> right.
proof.Path = proofPath.List()
if exact {
// There is a node with the given [key].
return proof, nil
}
// There is no node with the given [key].
// If there is a child at the index where the node would be
// if it existed, include that child in the proof.
nextIndex := keyPath[len(closestNode.key)]
child, ok := closestNode.children[nextIndex]
if !ok {
return proof, nil
}
childPath := closestNode.key + path(nextIndex) + child.compressedPath
childNode, err := t.getNodeFromParent(ctx, closestNode, childPath)
if err != nil {
return nil, err
}
proof.Path = append(proof.Path, childNode.asProofNode())
return proof, nil
}
// Returns a range proof for (at least part of) the key range [start, end].
// The returned proof's [KeyValues] has at most [maxLength] values.
// [maxLength] must be > 0.
func (t *trieView) GetRangeProof(
ctx context.Context,
start, end []byte,
maxLength int,
) (*RangeProof, error) {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.GetRangeProof")
defer span.End()
t.lockStack()
defer t.unlockStack()
return t.getRangeProof(ctx, start, end, maxLength)
}
// Returns a range proof for (at least part of) the key range [start, end].
// The returned proof's [KeyValues] has at most [maxLength] values.
// [maxLength] must be > 0.
// Assumes this view stack is locked.
func (t *trieView) getRangeProof(
ctx context.Context,
start, end []byte,
maxLength int,
) (*RangeProof, error) {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.getRangeProof")
defer span.End()
if len(end) > 0 && bytes.Compare(start, end) == 1 {
return nil, ErrStartAfterEnd
}
if maxLength <= 0 {
return nil, fmt.Errorf("%w but was %d", ErrInvalidMaxLength, maxLength)
}
if err := t.calculateIDs(ctx); err != nil {
return nil, err
}
var (
result RangeProof
err error
)
result.KeyValues, err = t.getKeyValues(ctx, start, end, maxLength, set.Set[string]{})
if err != nil {
return nil, err
}
// This proof may not contain all key-value pairs in [start, end] due to size limitations.
// The end proof we provide should be for the last key-value pair in the proof, not for
// the last key-value pair requested, which may not be in this proof.
if len(result.KeyValues) > 0 {
end = result.KeyValues[len(result.KeyValues)-1].Key
}
if len(end) > 0 {
endProof, err := t.getProof(ctx, end)
if err != nil {
return nil, err
}
result.EndProof = endProof.Path
}
if len(start) > 0 {
startProof, err := t.getProof(ctx, start)
if err != nil {
return nil, err
}
result.StartProof = startProof.Path
// strip out any common nodes to reduce proof size
i := 0
for ; i < len(result.StartProof) &&
i < len(result.EndProof) &&
result.StartProof[i].KeyPath.Equal(result.EndProof[i].KeyPath); i++ {
}
result.StartProof = result.StartProof[i:]
}
if len(result.StartProof) == 0 && len(result.EndProof) == 0 && len(result.KeyValues) == 0 {
// If the range is empty, return the root proof.
rootProof, err := t.getProof(ctx, rootKey)
if err != nil {
return nil, err
}
result.EndProof = rootProof.Path
}
return &result, nil
}
// Removes from the view stack views that have been committed or whose
// changes are already in the database.
// Returns true if [t]'s changes are already in the database.
// Assumes this view stack is locked.
func (t *trieView) cleanupCommittedViews(ctx context.Context) (bool, error) {
if t.committed {
return true, nil
}
root, err := t.getMerkleRoot(ctx)
if err != nil {
return false, err
}
if root == t.db.getMerkleRoot() {
// this view's root matches the db's root, so the changes in it are already in the db.
t.markViewStackCommitted()
return true, nil
}
if t.baseView == nil {
// There are no views under this one so we're done cleaning the view stack.
return false, nil
}
inDatabase, err := t.baseView.cleanupCommittedViews(ctx)
if err != nil {
return false, err
}
if !inDatabase {
// [t.baseView]'s changes aren't in the database yet
// so we can't remove our reference to it.
return false, nil
}
// [t.baseView]'s changes are in the database, so we can remove our reference to it.
// We don't need to commit it to the database.
t.baseView = nil
// There's no view under this one, so we should read/write changes to the database.
t.baseTrie = t.db
return false, nil
}
// Commits changes from this trie to the underlying DB.
func (t *trieView) Commit(ctx context.Context) error {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.Commit")
defer span.End()
t.lock.Lock()
defer t.lock.Unlock()
t.db.lock.Lock()
defer t.db.lock.Unlock()
// Note that we don't call lockStack() here because that would grab
// [t.db]'s read lock, but we want its write lock because we're going
// to modify [t.db]. No other view's call to lockStack() can proceed
// until this method returns because we hold [t.db]'s write lock.
if err := t.validateDBRoot(ctx); err != nil {
return err
}
return t.commit(ctx)
}
// Commits the changes from this trie to the underlying DB.
// Assumes [t.lock] and [t.db.lock] are held.
func (t *trieView) commit(ctx context.Context) error {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.triview.commit", oteltrace.WithAttributes(
attribute.Int("changeCount", len(t.changes.values)),
))
defer span.End()
if t.committed {
return ErrCommitted
}
if err := t.calculateIDs(ctx); err != nil {
return err
}
// ensure we don't recommit any committed tries
if alreadyCommitted, err := t.cleanupCommittedViews(ctx); alreadyCommitted || err != nil {
return err
}
// commit [t.baseView] before committing the current view
if t.baseView != nil {
// We have [db.lock] here so [t.baseView] can't be changing.
if err := t.baseView.commit(ctx); err != nil {
return err
}
t.baseView = nil
t.baseTrie = t.db
}
if err := t.db.commitChanges(ctx, t.changes); err != nil {
return err
}
t.committed = true
return nil
}
// Returns the ID of the root of this trie.
func (t *trieView) GetMerkleRoot(ctx context.Context) (ids.ID, error) {
t.lockStack()
defer t.unlockStack()
return t.getMerkleRoot(ctx)
}
// Returns the ID of the root node of this trie.
// Assumes this view stack is locked.
func (t *trieView) getMerkleRoot(ctx context.Context) (ids.ID, error) {
if err := t.calculateIDs(ctx); err != nil {
return ids.Empty, err
}
return t.root.id, nil
}
// Returns up to [maxLength] key/values from keys in closed range [start, end].
// Acts similarly to the merge step of a merge sort to combine state from the view
// with state from the base trie.
// Assumes this view stack is locked.
func (t *trieView) getKeyValues(
ctx context.Context,
start []byte,
end []byte,
maxLength int,
keysToIgnore set.Set[string],
) ([]KeyValue, error) {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieView.getKeyValues")
defer span.End()
if maxLength <= 0 {
return nil, fmt.Errorf("%w but was %d", ErrInvalidMaxLength, maxLength)
}
// collect all values that have changed or been deleted
changes := make([]KeyValue, 0, len(t.changes.values))
for key, change := range t.changes.values {
if change.after.IsNothing() {
// This was deleted
keysToIgnore.Add(string(key.Serialize().Value))
} else {
changes = append(changes, KeyValue{
Key: key.Serialize().Value,
Value: change.after.value,
})
}
}
// sort [changes] so they can be merged with the base trie's state
slices.SortFunc(changes, func(a, b KeyValue) bool {
return bytes.Compare(a.Key, b.Key) == -1
})
baseKeyValues, err := t.baseTrie.getKeyValues(ctx, start, end, maxLength, keysToIgnore)
if err != nil {
return nil, err
}
var (
// True if there are more key/value pairs from [baseKeyValues] to add to result
baseKeyValuesFinished = false
// True if there are more key/value pairs from [changes] to add to result
changesFinished = false
// The index of the next key/value pair to add from [baseKeyValues].
baseKeyValuesIndex = 0
// The index of the next key/value pair to add from [changes].
changesIndex = 0
remainingLength = maxLength
hasUpperBound = len(end) > 0
result = make([]KeyValue, 0, len(baseKeyValues))
)
// keep adding key/value pairs until one of the following:
// * a key that is lexicographically larger than the end key is hit
// * the maxLength is hit
// * no more values are available to add
for remainingLength > 0 {
// the baseKeyValues iterator is finished when we have run out of keys or hit a key greater than the end key
baseKeyValuesFinished = baseKeyValuesFinished ||
(baseKeyValuesIndex >= len(baseKeyValues) || (hasUpperBound && bytes.Compare(baseKeyValues[baseKeyValuesIndex].Key, end) == 1))
// the changes iterator is finished when we have run out of keys or hit a key greater than the end key
changesFinished = changesFinished ||
(changesIndex >= len(changes) || (hasUpperBound && bytes.Compare(changes[changesIndex].Key, end) == 1))
// if both the base state and changes are finished, return the result of the merge
if baseKeyValuesFinished && changesFinished {
return result, nil
}
// one or both iterators still have values, so one will be added to the result
remainingLength--
// both still have key/values available, so add the smallest key
if !changesFinished && !baseKeyValuesFinished {
currentChangeState := changes[changesIndex]
currentKeyValues := baseKeyValues[baseKeyValuesIndex]
switch bytes.Compare(currentChangeState.Key, currentKeyValues.Key) {
case -1:
result = append(result, currentChangeState)
changesIndex++
case 0:
// the keys are the same, so override the base value with the changed value
result = append(result, currentChangeState)
changesIndex++
baseKeyValuesIndex++
case 1:
result = append(result, currentKeyValues)
baseKeyValuesIndex++
}
continue
}
// the base state is not finished, but the changes is finished.
// add the next base state value.
if !baseKeyValuesFinished {
currentBaseState := baseKeyValues[baseKeyValuesIndex]
result = append(result, currentBaseState)
baseKeyValuesIndex++
continue
}
// the base state is finished, but the changes is not finished.
// add the next changes value.
currentChangeState := changes[changesIndex]
result = append(result, currentChangeState)
changesIndex++
}
return result, nil
}
func (t *trieView) GetValues(ctx context.Context, keys [][]byte) ([][]byte, []error) {
t.lockStack()
defer t.unlockStack()
results := make([][]byte, len(keys))
errors := make([]error, len(keys))
if err := t.validateDBRoot(ctx); err != nil {
for i := range keys {
errors[i] = err
}
return results, errors
}
for i, key := range keys {
results[i], errors[i] = t.getValue(ctx, newPath(key))
}
return results, errors
}
// Returns the value for the given [key].
// Returns database.ErrNotFound if it doesn't exist.
func (t *trieView) GetValue(ctx context.Context, key []byte) ([]byte, error) {
t.lockStack()
defer t.unlockStack()
if err := t.validateDBRoot(ctx); err != nil {
return nil, err
}
return t.getValue(ctx, newPath(key))
}
// Assumes this view stack is locked.
func (t *trieView) getValue(ctx context.Context, key path) ([]byte, error) {
value, hasLocal, err := t.getCachedValue(key)
if hasLocal {
return value, err
}
// if we don't have local copy of the key, then grab a copy from the base trie
value, err = t.baseTrie.getValue(ctx, key)
if err != nil {
if err == database.ErrNotFound {
// Cache the miss.
t.baseValuesCache[key] = Nothing[[]byte]()
}
return nil, err
}
t.baseValuesCache[key] = Some(value)
return value, nil
}
// Upserts the key/value pair into the trie.
func (t *trieView) Insert(ctx context.Context, key []byte, value []byte) error {
t.lockStack()
defer t.unlockStack()
return t.insert(ctx, key, value)
}
// Assumes this view stack is locked.
func (t *trieView) insert(ctx context.Context, key []byte, value []byte) error {
if t.committed {
return ErrCommitted
}
if t.changeLocked {
return ErrEditLocked
}
valCopy := slices.Clone(value)
return t.recordValueChange(ctx, newPath(key), Some(valCopy))
}
// Removes the value associated with [key] from this trie.
func (t *trieView) Remove(ctx context.Context, key []byte) error {
t.lockStack()
defer t.unlockStack()
return t.remove(ctx, key)
}
// Assumes this view stack is locked.
func (t *trieView) remove(ctx context.Context, key []byte) error {
if t.committed {
return ErrCommitted
}
if t.changeLocked {
return ErrEditLocked
}
return t.recordValueChange(ctx, newPath(key), Nothing[[]byte]())
}
// Returns nil iff at least one of the following is true:
// - The root of the db hasn't changed since this view was created.
// - This view's root is the same as the db's root.
// - This method returns nil for the view under this one.
//
// Assumes this view stack is locked.
func (t *trieView) validateDBRoot(ctx context.Context) error {
dbRoot := t.db.getMerkleRoot()
// the root has not changed, so the trieview is still valid
if dbRoot == t.basedOnRoot {
return nil
}
if t.baseView != nil {
// if the view that this view is based on is valid,
// then this view is valid too.
if err := t.baseView.validateDBRoot(ctx); err == nil {
return nil
}
}
// this view has no base view or an invalid base view.
// calculate the current view's root and check if it matches the db.
localRoot, err := t.getMerkleRoot(ctx)
if err != nil {
return err
}
// the roots don't match, which means that that the changes
// in this view aren't already represented in the db
if localRoot != dbRoot {
return ErrChangedBaseRoot
}
return nil
}
// Assumes this view stack is locked.
func (t *trieView) applyChangedValuesToTrie(ctx context.Context) error {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.applyChangedValuesToTrie")
defer span.End()
unappliedValues := t.unappliedValueChanges
t.unappliedValueChanges = make(map[path]Maybe[[]byte], t.estimatedSize)
for key, change := range unappliedValues {
if change.IsNothing() {
if err := t.removeFromTrie(ctx, key); err != nil {
return err
}
} else {
if _, err := t.insertIntoTrie(ctx, key, change); err != nil {
return err
}
}
}
return nil
}
// Merges together nodes in the inclusive descendants of [node] that
// have no value and a single child into one node with a compressed
// path until a node that doesn't meet those criteria is reached.
// Assumes at least one of the following is true:
// * [node] has a value.
// * [node] has children.
// Assumes this view stack is locked.
func (t *trieView) compressNodePath(ctx context.Context, node *node) error {
parent := t.parents[node.key]
// don't collapse into this node if it's the root, doesn't have 1 child, or has a value
if parent == nil || len(node.children) != 1 || node.hasValue() {
return nil
}
// delete all empty nodes with a single child under [node]
for len(node.children) == 1 && !node.hasValue() {
delete(t.parents, node.key)
if err := t.recordNodeDeleted(ctx, node); err != nil {
return err
}
nextNode, err := t.getNodeFromParent(ctx, node, node.getSingleChildPath())
if err != nil {
return err
}
node = nextNode
}
// [node] is the first node with multiple children or with a value under [n].
// combine it with [n].
t.parents[node.key] = parent
parent.addChild(node)
return t.recordNodeChange(ctx, parent)
}
// Deletes each node in the inclusive ancestry of [node] that has no
// value and no children.
// Assumes this view stack is locked.
func (t *trieView) deleteEmptyNodes(ctx context.Context, node *node) error {
for node != nil && len(node.children) == 0 && !node.hasValue() {
if err := t.recordNodeDeleted(ctx, node); err != nil {
return err
}
parent := t.parents[node.key]
if parent != nil {
delete(t.parents, node.key)
parent.removeChild(node)
if err := t.recordNodeChange(ctx, parent); err != nil {
return err
}
}
node = parent
}
if node == nil {
// The last processed node was the root.
// No need to call [t.compressNodePath] because the
// root has no parent that it can be merged with.
return nil
}
return t.compressNodePath(ctx, node)
}
// Gets the node furthest along a path if any exist.
// Returns:
// 1. The node closest to matching the [fullPath].
// 2. True if the node is an exact match with the [fullPath].
// 3. Any error that occurred while following the path.
// Assumes this view stack is locked.
func (t *trieView) getClosestNode(
ctx context.Context,
fullPath path,
) (closestNode *node, exactMatch bool, err error) {
// all paths start at the root
currentNode := t.root
matchedPathIndex := 0
var previousNode *node
// while the entire path hasn't been matched
for matchedPathIndex < len(fullPath) {
// confirm that a child exists and grab its ID before attempting to load it
nextChildEntry, hasChild := currentNode.children[fullPath[matchedPathIndex]]
// the nibble for the child entry has now been handled, so increment the matchedPathIndex
matchedPathIndex += 1
if !hasChild || !fullPath[matchedPathIndex:].HasPrefix(nextChildEntry.compressedPath) {
// there was no child along the path or the child that was there doesn't match the remaining path
return currentNode, false, nil
}
// the compressed path of the entry there matched the path, so increment the matched index
matchedPathIndex += len(nextChildEntry.compressedPath)
previousNode = currentNode
// grab the child node
currentNode, err = t.getNodeWithID(ctx, nextChildEntry.id, fullPath[:matchedPathIndex])
if err != nil {
// trouble retrieving the next node
// return the last node that was able to be retrieved
return previousNode, false, err
}
// record that the node just loaded has the previous node as its parent
t.parents[currentNode.key] = previousNode
}
// the entire path was matched entirely, so return the node and indicate it was an exact match
return currentNode, true, nil
}
func getLengthOfCommonPrefix(first, second path) int {
commonIndex := 0
for len(first) > commonIndex && len(second) > commonIndex && first[commonIndex] == second[commonIndex] {
commonIndex++
}
return commonIndex
}
// Assumes this view stack is locked.
func (t *trieView) getNode(ctx context.Context, key path) (*node, error) {
if err := t.calculateIDs(ctx); err != nil {
return nil, err
}
n, err := t.getNodeWithID(ctx, ids.Empty, key)
if err != nil {
return nil, err
}
return n.clone(), nil
}
// Returns:
// 1. The value at [key] iff the following return value is true.
// 2. True if the value at [key] exists in the caches.
// If false, the [key] may be in the trie, just not in the caches.
// 3. database.ErrNotFound if the value isn't in the trie at all (not just the caches).
//
// Assumes this view stack is locked.
func (t *trieView) getCachedValue(key path) ([]byte, bool, error) {
if change, ok := t.changes.values[key]; ok {
t.db.metrics.ViewValueCacheHit()
if change.after.IsNothing() {
return nil, true, database.ErrNotFound
}
return change.after.value, true, nil
}
if maybeVal, ok := t.baseValuesCache[key]; ok {
t.db.metrics.ViewValueCacheHit()
if maybeVal.IsNothing() {
return nil, true, database.ErrNotFound
}
return maybeVal.value, true, nil
}
t.db.metrics.ViewValueCacheMiss()
return nil, false, nil
}
// Hashes all nodes concurrently.
// Assumes this view stack is locked.
func (t *trieView) calculateIDsConcurrent(
ctx context.Context,
readyNodes map[path]*node,
dependencyCounts map[path]int,
) error {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.hashingConcurrent")
defer span.End()
eg, ctx := errgroup.WithContext(ctx)
numThreads := runtime.NumCPU()
readyNodesChan := make(chan *node, len(readyNodes)) // nodes that are ready to be hashed
updateParentChan := make(chan *node, len(readyNodes)) // parent nodes that need to be updated
// This iteration is guaranteed not to block because [readyNodesChan] is the
// exact right size.
//
// Invariant: There must be at least one node ready to hash.
for _, n := range readyNodes {
readyNodesChan <- n
}