forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 5
/
trie_root.go
1545 lines (1405 loc) · 44.2 KB
/
trie_root.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
package trie
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"math/bits"
"time"
"github.com/ledgerwatch/log/v3"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
length2 "github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
dbutils2 "github.com/ledgerwatch/erigon-lib/kv/dbutils"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/turbo/rlphacks"
)
/*
**Theoretically:** "Merkle trie root calculation" starts from state, build from state keys - trie,
on each level of trie calculates intermediate hash of underlying data.
**Practically:** It can be implemented as "Preorder trie traversal" (Preorder - visit Root, visit Left, visit Right).
But, let's make couple observations to make traversal over huge state efficient.
**Observation 1:** `TrieOfAccounts` already stores state keys in sorted way.
Iteration over this bucket will retrieve keys in same order as "Preorder trie traversal".
**Observation 2:** each Eth block - changes not big part of state - it means most of Merkle trie intermediate hashes will not change.
It means we effectively can cache them. `TrieOfAccounts` stores "Intermediate hashes of all Merkle trie levels".
It also sorted and Iteration over `TrieOfAccounts` will retrieve keys in same order as "Preorder trie traversal".
**Implementation:** by opening 1 Cursor on state and 1 more Cursor on intermediate hashes bucket - we will receive data in
order of "Preorder trie traversal". Cursors will only do "sequential reads" and "jumps forward" - been hardware-friendly.
1 stack keeps all accumulated hashes, when sub-trie traverse ends - all hashes pulled from stack -> hashed -> new hash puts on stack - it's hash of visited sub-trie (it emulates recursive nature of "Preorder trie traversal" algo).
Imagine that account with key 0000....00 (64 zeroes, 32 bytes of zeroes) changed.
Here is an example sequence which can be seen by running 2 Cursors:
```
00 // key which came from cache, can't use it - because account with this prefix changed
0000 // key which came from cache, can't use it - because account with this prefix changed
...
{30 zero bytes}00 // key which came from cache, can't use it - because account with this prefix changed
{30 zero bytes}0000 // Account which came from state, use it - calculate hash, jump to "next sub-trie"
{30 zero bytes}01 // key which came from cache, it is "next sub-trie", use it, jump to "next sub-trie"
{30 zero bytes}02 // key which came from cache, it is "next sub-trie", use it, jump to "next sub-trie"
...
{30 zero bytes}ff // key which came from cache, it is "next sub-trie", use it, jump to "next sub-trie"
{29 zero bytes}01 // key which came from cache, it is "next sub-trie" (1 byte shorter key), use it, jump to "next sub-trie"
{29 zero bytes}02 // key which came from cache, it is "next sub-trie" (1 byte shorter key), use it, jump to "next sub-trie"
...
ff // key which came from cache, it is "next sub-trie" (1 byte shorter key), use it, jump to "next sub-trie"
nil // db returned nil - means no more keys there, done
```
On practice Trie is not full - it means after account key `{30 zero bytes}0000` may come `{5 zero bytes}01` and amount of iterations will not be big.
### Attack - by delete account with huge state
It's possible to create Account with very big storage (increase storage size during many blocks).
Then delete this account (SELFDESTRUCT).
Naive storage deletion may take several minutes - depends on Disk speed - means every Eth client
will not process any incoming block that time. To protect against this attack:
PlainState, HashedState and IntermediateTrieHash buckets have "incarnations". Account entity has field "Incarnation" -
just a digit which increasing each SELFDESTRUCT or CREATE2 opcodes. Storage key formed by:
`{account_key}{incarnation}{storage_hash}`. And [turbo/trie/trie_root.go](../../turbo/trie/trie_root.go) has logic - every time
when Account visited - we save it to `accAddrHashWithInc` variable and skip any Storage or IntermediateTrieHashes with another incarnation.
*/
// FlatDBTrieLoader reads state and intermediate trie hashes in order equal to "Preorder trie traversal"
// (Preorder - visit Root, visit Left, visit Right)
//
// It produces stream of values and send this stream to `receiver`
// It skips storage with incorrect incarnations
//
// Each intermediate hash key firstly pass to RetainDecider, only if it returns "false" - such AccTrie can be used.
type FlatDBTrieLoader struct {
logPrefix string
trace bool
rd RetainDeciderWithMarker
accAddrHashWithInc [40]byte // Concatenation of addrHash of the currently build account with its incarnation encoding
ihSeek, accSeek, storageSeek []byte
kHex, kHexS []byte
// Account item buffer
accountValue accounts.Account
receiver *RootHashAggregator
hc HashCollector2
shc StorageHashCollector2
}
// RootHashAggregator - calculates Merkle trie root hash from incoming data stream
type RootHashAggregator struct {
trace bool
wasIH bool
wasIHStorage bool
root libcommon.Hash
hc HashCollector2
shc StorageHashCollector2
currStorage bytes.Buffer // Current key for the structure generation algorithm, as well as the input tape for the hash builder
succStorage bytes.Buffer
valueStorage []byte // Current value to be used as the value tape for the hash builder
hadTreeStorage bool
hashAccount libcommon.Hash // Current value to be used as the value tape for the hash builder
hashStorage libcommon.Hash // Current value to be used as the value tape for the hash builder
curr bytes.Buffer // Current key for the structure generation algorithm, as well as the input tape for the hash builder
succ bytes.Buffer
currAccK []byte
value []byte // Current value to be used as the value tape for the hash builder
hadTreeAcc bool
groups []uint16 // `groups` parameter is the map of the stack. each element of the `groups` slice is a bitmask, one bit per element currently on the stack. See `GenStructStep` docs
hasTree []uint16
hasHash []uint16
groupsStorage []uint16 // `groups` parameter is the map of the stack. each element of the `groups` slice is a bitmask, one bit per element currently on the stack. See `GenStructStep` docs
hasTreeStorage []uint16
hasHashStorage []uint16
hb *HashBuilder
hashData GenStructStepHashData
a accounts.Account
leafData GenStructStepLeafData
accData GenStructStepAccountData
// Used to construct an Account proof while calculating the tree root.
proofRetainer *ProofRetainer
cutoff bool
}
func NewRootHashAggregator() *RootHashAggregator {
return &RootHashAggregator{
hb: NewHashBuilder(false),
}
}
func NewFlatDBTrieLoader(logPrefix string, rd RetainDeciderWithMarker, hc HashCollector2, shc StorageHashCollector2, trace bool) *FlatDBTrieLoader {
if trace {
fmt.Printf("----------\n")
fmt.Printf("CalcTrieRoot\n")
}
return &FlatDBTrieLoader{
logPrefix: logPrefix,
receiver: &RootHashAggregator{
hb: NewHashBuilder(false),
hc: hc,
shc: shc,
trace: trace,
},
ihSeek: make([]byte, 0, 128),
accSeek: make([]byte, 0, 128),
storageSeek: make([]byte, 0, 128),
kHex: make([]byte, 0, 128),
kHexS: make([]byte, 0, 128),
rd: rd,
hc: hc,
shc: shc,
}
}
func (l *FlatDBTrieLoader) SetProofRetainer(pr *ProofRetainer) {
l.receiver.proofRetainer = pr
}
// CalcTrieRoot algo:
//
// for iterateIHOfAccounts {
// if canSkipState
// goto SkipAccounts
//
// for iterateAccounts from prevIH to currentIH {
// use(account)
// for iterateIHOfStorage within accountWithIncarnation{
// if canSkipState
// goto SkipStorage
//
// for iterateStorage from prevIHOfStorage to currentIHOfStorage {
// use(storage)
// }
// SkipStorage:
// use(ihStorage)
// }
// }
// SkipAccounts:
// use(AccTrie)
// }
func (l *FlatDBTrieLoader) CalcTrieRoot(tx kv.Tx, quit <-chan struct{}) (libcommon.Hash, error) {
accC, err := tx.Cursor(kv.HashedAccounts)
if err != nil {
return EmptyRoot, err
}
defer accC.Close()
accs := NewStateCursor(accC, quit)
trieAccC, err := tx.Cursor(kv.TrieOfAccounts)
if err != nil {
return EmptyRoot, err
}
defer trieAccC.Close()
trieStorageC, err := tx.CursorDupSort(kv.TrieOfStorage)
if err != nil {
return EmptyRoot, err
}
defer trieStorageC.Close()
var canUse = func(prefix []byte) (bool, []byte) {
retain, nextCreated := l.rd.RetainWithMarker(prefix)
return !retain, nextCreated
}
accTrie := AccTrie(canUse, l.hc, trieAccC, quit)
storageTrie := StorageTrie(canUse, l.shc, trieStorageC, quit)
ss, err := tx.CursorDupSort(kv.HashedStorage)
if err != nil {
return EmptyRoot, err
}
defer ss.Close()
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
for ihK, ihV, hasTree, err := accTrie.AtPrefix(nil); ; ihK, ihV, hasTree, err = accTrie.Next() { // no loop termination is at he end of loop
if err != nil {
return EmptyRoot, err
}
var firstPrefix []byte
var done bool
if accTrie.SkipState {
goto SkipAccounts
}
firstPrefix, done = accTrie.FirstNotCoveredPrefix()
if done {
goto SkipAccounts
}
for k, kHex, v, err1 := accs.Seek(firstPrefix); k != nil; k, kHex, v, err1 = accs.Next() {
if err1 != nil {
return EmptyRoot, err1
}
if keyIsBefore(ihK, kHex) {
break
}
if err = l.accountValue.DecodeForStorage(v); err != nil {
return EmptyRoot, fmt.Errorf("fail DecodeForStorage: %w", err)
}
if err = l.receiver.Receive(AccountStreamItem, kHex, nil, &l.accountValue, nil, nil, false, 0); err != nil {
return EmptyRoot, err
}
if l.accountValue.Incarnation == 0 {
continue
}
copy(l.accAddrHashWithInc[:], k)
binary.BigEndian.PutUint64(l.accAddrHashWithInc[32:], l.accountValue.Incarnation)
accWithInc := l.accAddrHashWithInc[:]
for ihKS, ihVS, hasTreeS, err2 := storageTrie.SeekToAccount(accWithInc); ; ihKS, ihVS, hasTreeS, err2 = storageTrie.Next() {
if err2 != nil {
return EmptyRoot, err2
}
if storageTrie.skipState {
goto SkipStorage
}
firstPrefix, done = storageTrie.FirstNotCoveredPrefix()
if done {
goto SkipStorage
}
for vS, err3 := ss.SeekBothRange(accWithInc, firstPrefix); vS != nil; _, vS, err3 = ss.NextDup() {
if err3 != nil {
return EmptyRoot, err3
}
hexutil.DecompressNibbles(vS[:32], &l.kHexS)
if keyIsBefore(ihKS, l.kHexS) { // read until next AccTrie
break
}
if err = l.receiver.Receive(StorageStreamItem, accWithInc, l.kHexS, nil, vS[32:], nil, false, 0); err != nil {
return EmptyRoot, err
}
}
SkipStorage:
if ihKS == nil { // Loop termination
break
}
if err = l.receiver.Receive(SHashStreamItem, accWithInc, ihKS, nil, nil, ihVS, hasTreeS, 0); err != nil {
return EmptyRoot, err
}
if len(ihKS) == 0 { // means we just sent acc.storageRoot
break
}
}
select {
default:
case <-logEvery.C:
l.logProgress(k, ihK)
}
}
SkipAccounts:
if ihK == nil { // Loop termination
break
}
if err = l.receiver.Receive(AHashStreamItem, ihK, nil, nil, nil, ihV, hasTree, 0); err != nil {
return EmptyRoot, err
}
}
if err := l.receiver.Receive(CutoffStreamItem, nil, nil, nil, nil, nil, false, 0); err != nil {
return EmptyRoot, err
}
return l.receiver.Root(), nil
}
func (l *FlatDBTrieLoader) logProgress(accountKey, ihK []byte) {
var k string
if accountKey != nil {
k = makeCurrentKeyStr(accountKey)
} else if ihK != nil {
k = makeCurrentKeyStr(ihK)
}
log.Info(fmt.Sprintf("[%s] Calculating Merkle root", l.logPrefix), "current key", k)
}
func (r *RootHashAggregator) RetainNothing(_ []byte) bool {
return false
}
func (r *RootHashAggregator) Receive(itemType StreamItem,
accountKey []byte,
storageKey []byte,
accountValue *accounts.Account,
storageValue []byte,
hash []byte,
hasTree bool,
cutoff int,
) error {
//r.traceIf("9c3dc2561d472d125d8f87dde8f2e3758386463ade768ae1a1546d34101968bb", "00")
//if storageKey == nil {
// //if bytes.HasPrefix(accountKey, common.FromHex("08050d07")) {
// fmt.Printf("1: %d, %x, %x\n", itemType, accountKey, hash)
// //}
//} else {
// //if bytes.HasPrefix(accountKey, common.FromHex("876f5a0f54b30254d2bad26bb5a8da19cbe748fd033004095d9c96c8e667376b")) && bytes.HasPrefix(storageKey, common.FromHex("")) {
// //fmt.Printf("%x\n", storageKey)
// fmt.Printf("1: %d, %x, %x, %x\n", itemType, accountKey, storageKey, hash)
// //}
//}
//
switch itemType {
case StorageStreamItem:
if len(r.currAccK) == 0 {
r.currAccK = append(r.currAccK[:0], accountKey...)
}
r.advanceKeysStorage(storageKey, true /* terminator */)
if r.currStorage.Len() > 0 {
if err := r.genStructStorage(); err != nil {
return err
}
}
r.saveValueStorage(false, hasTree, storageValue, hash)
case SHashStreamItem:
if len(storageKey) == 0 { // this is ready-to-use storage root - no reason to call GenStructStep, also GenStructStep doesn't support empty prefixes
r.hb.hashStack = append(append(r.hb.hashStack, byte(80+length2.Hash)), hash...)
r.hb.nodeStack = append(r.hb.nodeStack, nil)
r.accData.FieldSet |= AccountFieldStorageOnly
break
}
if len(r.currAccK) == 0 {
r.currAccK = append(r.currAccK[:0], accountKey...)
}
r.advanceKeysStorage(storageKey, false /* terminator */)
if r.currStorage.Len() > 0 {
if err := r.genStructStorage(); err != nil {
return err
}
}
r.saveValueStorage(true, hasTree, storageValue, hash)
case AccountStreamItem:
r.advanceKeysAccount(accountKey, true /* terminator */)
if r.curr.Len() > 0 && !r.wasIH {
r.cutoffKeysStorage(0)
if r.currStorage.Len() > 0 {
if err := r.genStructStorage(); err != nil {
return err
}
}
if r.currStorage.Len() > 0 {
r.groupsStorage = r.groupsStorage[:0]
r.hasTreeStorage = r.hasTreeStorage[:0]
r.hasHashStorage = r.hasHashStorage[:0]
r.currStorage.Reset()
r.succStorage.Reset()
r.wasIHStorage = false
// There are some storage items
r.accData.FieldSet |= AccountFieldStorageOnly
}
}
r.currAccK = r.currAccK[:0]
if r.curr.Len() > 0 {
if err := r.genStructAccount(); err != nil {
return err
}
}
if err := r.saveValueAccount(false, hasTree, accountValue, hash); err != nil {
return err
}
case AHashStreamItem:
r.advanceKeysAccount(accountKey, false /* terminator */)
if r.curr.Len() > 0 && !r.wasIH {
r.cutoffKeysStorage(0)
if r.currStorage.Len() > 0 {
if err := r.genStructStorage(); err != nil {
return err
}
}
if r.currStorage.Len() > 0 {
r.groupsStorage = r.groupsStorage[:0]
r.hasTreeStorage = r.hasTreeStorage[:0]
r.hasHashStorage = r.hasHashStorage[:0]
r.currStorage.Reset()
r.succStorage.Reset()
r.wasIHStorage = false
// There are some storage items
r.accData.FieldSet |= AccountFieldStorageOnly
}
}
r.currAccK = r.currAccK[:0]
if r.curr.Len() > 0 {
if err := r.genStructAccount(); err != nil {
return err
}
}
if err := r.saveValueAccount(true, hasTree, accountValue, hash); err != nil {
return err
}
case CutoffStreamItem:
if r.trace {
fmt.Printf("storage cuttoff %d\n", cutoff)
}
r.cutoffKeysAccount(cutoff)
if r.curr.Len() > 0 && !r.wasIH {
r.cutoffKeysStorage(0)
if r.currStorage.Len() > 0 {
if err := r.genStructStorage(); err != nil {
return err
}
}
if r.currStorage.Len() > 0 {
r.groupsStorage = r.groupsStorage[:0]
r.hasTreeStorage = r.hasTreeStorage[:0]
r.hasHashStorage = r.hasHashStorage[:0]
r.currStorage.Reset()
r.succStorage.Reset()
r.wasIHStorage = false
// There are some storage items
r.accData.FieldSet |= AccountFieldStorageOnly
}
}
// Used for optional GetProof calculation to trigger inclusion of the top-level node
r.cutoff = true
if r.curr.Len() > 0 {
if err := r.genStructAccount(); err != nil {
return err
}
}
if r.curr.Len() > 0 {
if len(r.groups) > cutoff {
r.groups = r.groups[:cutoff]
r.hasTree = r.hasTree[:cutoff]
r.hasHash = r.hasHash[:cutoff]
}
}
if r.hb.hasRoot() {
r.root = r.hb.rootHash()
} else {
r.root = EmptyRoot
}
r.groups = r.groups[:0]
r.hasTree = r.hasTree[:0]
r.hasHash = r.hasHash[:0]
r.hb.Reset()
r.wasIH = false
r.wasIHStorage = false
r.curr.Reset()
r.succ.Reset()
r.currStorage.Reset()
r.succStorage.Reset()
}
return nil
}
//nolint
// func (r *RootHashAggregator) traceIf(acc, st string) {
// // "succ" - because on this iteration this "succ" will become "curr"
// if r.succStorage.Len() == 0 {
// var accNibbles []byte
// hexutil.DecompressNibbles(common.FromHex(acc), &accNibbles)
// r.trace = bytes.HasPrefix(r.succ.Bytes(), accNibbles)
// } else {
// r.trace = bytes.HasPrefix(r.currAccK, common.FromHex(acc)) && bytes.HasPrefix(r.succStorage.Bytes(), common.FromHex(st))
// }
// }
func (r *RootHashAggregator) Root() libcommon.Hash {
return r.root
}
func (r *RootHashAggregator) advanceKeysStorage(k []byte, terminator bool) {
r.currStorage.Reset()
r.currStorage.Write(r.succStorage.Bytes())
r.succStorage.Reset()
// Transform k to nibbles, but skip the incarnation part in the middle
r.succStorage.Write(k)
if terminator {
r.succStorage.WriteByte(16)
}
}
func (r *RootHashAggregator) cutoffKeysStorage(cutoff int) {
r.currStorage.Reset()
r.currStorage.Write(r.succStorage.Bytes())
r.succStorage.Reset()
//if r.currStorage.Len() > 0 {
//r.succStorage.Write(r.currStorage.Bytes()[:cutoff-1])
//r.succStorage.WriteByte(r.currStorage.Bytes()[cutoff-1] + 1) // Modify last nibble in the incarnation part of the `currStorage`
//}
}
func (r *RootHashAggregator) genStructStorage() error {
var err error
var data GenStructStepData
if r.wasIHStorage {
r.hashData.Hash = r.hashStorage
r.hashData.HasTree = r.hadTreeStorage
data = &r.hashData
} else {
r.leafData.Value = rlphacks.RlpSerializableBytes(r.valueStorage)
data = &r.leafData
}
var wantProof func(_ []byte) *proofElement
if r.proofRetainer != nil {
var fullKey [2 * (length2.Hash + length2.Incarnation + length2.Hash)]byte
for i, b := range r.currAccK {
fullKey[i*2] = b / 16
fullKey[i*2+1] = b % 16
}
for i, b := range binary.BigEndian.AppendUint64(nil, r.a.Incarnation) {
fullKey[2*length2.Hash+i*2] = b / 16
fullKey[2*length2.Hash+i*2+1] = b % 16
}
baseKeyLen := 2 * (length2.Hash + length2.Incarnation)
wantProof = func(prefix []byte) *proofElement {
copy(fullKey[baseKeyLen:], prefix)
return r.proofRetainer.ProofElement(fullKey[:baseKeyLen+len(prefix)])
}
}
r.groupsStorage, r.hasTreeStorage, r.hasHashStorage, err = GenStructStepEx(r.RetainNothing, r.currStorage.Bytes(), r.succStorage.Bytes(), r.hb, func(keyHex []byte, hasState, hasTree, hasHash uint16, hashes, rootHash []byte) error {
if r.shc == nil {
return nil
}
return r.shc(r.currAccK, keyHex, hasState, hasTree, hasHash, hashes, rootHash)
}, data, r.groupsStorage, r.hasTreeStorage, r.hasHashStorage,
r.trace,
wantProof,
r.cutoff,
)
if err != nil {
return err
}
return nil
}
func (r *RootHashAggregator) saveValueStorage(isIH, hasTree bool, v, h []byte) {
// Remember the current value
r.wasIHStorage = isIH
r.valueStorage = nil
if isIH {
r.hashStorage.SetBytes(h)
r.hadTreeStorage = hasTree
} else {
r.valueStorage = v
}
}
func (r *RootHashAggregator) advanceKeysAccount(k []byte, terminator bool) {
r.curr.Reset()
r.curr.Write(r.succ.Bytes())
r.succ.Reset()
r.succ.Write(k)
if terminator {
r.succ.WriteByte(16)
}
}
func (r *RootHashAggregator) cutoffKeysAccount(cutoff int) {
r.curr.Reset()
r.curr.Write(r.succ.Bytes())
r.succ.Reset()
if r.curr.Len() > 0 && cutoff > 0 {
r.succ.Write(r.curr.Bytes()[:cutoff-1])
r.succ.WriteByte(r.curr.Bytes()[cutoff-1] + 1) // Modify last nibble before the cutoff point
}
}
func (r *RootHashAggregator) genStructAccount() error {
var data GenStructStepData
if r.wasIH {
r.hashData.Hash = r.hashAccount
r.hashData.HasTree = r.hadTreeAcc
data = &r.hashData
} else {
r.accData.Balance.Set(&r.a.Balance)
if !r.a.Balance.IsZero() {
r.accData.FieldSet |= AccountFieldBalanceOnly
}
r.accData.Nonce = r.a.Nonce
if r.a.Nonce != 0 {
r.accData.FieldSet |= AccountFieldNonceOnly
}
r.accData.Incarnation = r.a.Incarnation
data = &r.accData
}
r.wasIHStorage = false
r.currStorage.Reset()
r.succStorage.Reset()
var err error
var wantProof func(_ []byte) *proofElement
if r.proofRetainer != nil {
wantProof = r.proofRetainer.ProofElement
}
if r.groups, r.hasTree, r.hasHash, err = GenStructStepEx(r.RetainNothing, r.curr.Bytes(), r.succ.Bytes(), r.hb, func(keyHex []byte, hasState, hasTree, hasHash uint16, hashes, rootHash []byte) error {
if r.hc == nil {
return nil
}
return r.hc(keyHex, hasState, hasTree, hasHash, hashes, rootHash)
}, data, r.groups, r.hasTree, r.hasHash,
//false,
r.trace,
wantProof,
r.cutoff,
); err != nil {
return err
}
r.accData.FieldSet = 0
return nil
}
func (r *RootHashAggregator) saveValueAccount(isIH, hasTree bool, v *accounts.Account, h []byte) error {
r.wasIH = isIH
if isIH {
r.hashAccount.SetBytes(h)
r.hadTreeAcc = hasTree
return nil
}
r.a.Copy(v)
// Place code on the stack first, the storage will follow
if !r.a.IsEmptyCodeHash() {
// the first item ends up deepest on the stack, the second item - on the top
r.accData.FieldSet |= AccountFieldCodeOnly
if err := r.hb.hash(r.a.CodeHash[:]); err != nil {
return err
}
}
return nil
}
// AccTrieCursor - holds logic related to iteration over AccTrie bucket
// has 2 basic operations: _preOrderTraversalStep and _preOrderTraversalStepNoInDepth
type AccTrieCursor struct {
SkipState bool
lvl int
k, v [64][]byte // store up to 64 levels of key/value pairs in nibbles format
hasState [64]uint16 // says that records in dbutil.HashedAccounts exists by given prefix
hasTree [64]uint16 // says that records in dbutil.TrieOfAccounts exists by given prefix
hasHash [64]uint16 // store ownership of hashes stored in .v
childID, hashID [64]int8 // meta info: current child in .hasState[lvl] field, max child id, current hash in .v[lvl]
deleted [64]bool // helper to avoid multiple deletes of same key
c kv.Cursor
hc HashCollector2
prev, cur, next []byte
prefix []byte // global prefix - cursor will never return records without this prefix
firstNotCoveredPrefix []byte
canUse func([]byte) (bool, []byte) // if this function returns true - then this AccTrie can be used as is and don't need continue PostorderTraversal, but switch to sibling instead
nextCreated []byte
kBuf []byte
quit <-chan struct{}
}
func AccTrie(canUse func([]byte) (bool, []byte), hc HashCollector2, c kv.Cursor, quit <-chan struct{}) *AccTrieCursor {
return &AccTrieCursor{
c: c,
canUse: canUse,
firstNotCoveredPrefix: make([]byte, 0, 64),
next: make([]byte, 0, 64),
kBuf: make([]byte, 0, 64),
hc: hc,
quit: quit,
}
}
// _preOrderTraversalStep - goToChild || nextSiblingInMem || nextSiblingOfParentInMem || nextSiblingInDB
func (c *AccTrieCursor) _preOrderTraversalStep() error {
if c._hasTree() {
c.next = append(append(c.next[:0], c.k[c.lvl]...), byte(c.childID[c.lvl]))
ok, err := c._seek(c.next, c.next)
if err != nil {
return err
}
if ok {
return nil
}
}
return c._preOrderTraversalStepNoInDepth()
}
// _preOrderTraversalStepNoInDepth - nextSiblingInMem || nextSiblingOfParentInMem || nextSiblingInDB
func (c *AccTrieCursor) _preOrderTraversalStepNoInDepth() error {
ok := c._nextSiblingInMem() || c._nextSiblingOfParentInMem()
if ok {
return nil
}
err := c._nextSiblingInDB()
if err != nil {
return err
}
return nil
}
func (c *AccTrieCursor) FirstNotCoveredPrefix() ([]byte, bool) {
var ok bool
c.firstNotCoveredPrefix, ok = firstNotCoveredPrefix(c.prev, c.prefix, c.firstNotCoveredPrefix)
return c.firstNotCoveredPrefix, ok
}
func (c *AccTrieCursor) AtPrefix(prefix []byte) (k, v []byte, hasTree bool, err error) {
c.SkipState = false // There can be accounts with keys less than the first key in AccTrie
_, c.nextCreated = c.canUse([]byte{})
c.prev = append(c.prev[:0], c.cur...)
c.prefix = prefix
ok, err := c._seek(prefix, []byte{})
if err != nil {
return []byte{}, nil, false, err
}
if !ok {
c.cur = nil
c.SkipState = false
return nil, nil, false, nil
}
ok, err = c._consume()
if err != nil {
return []byte{}, nil, false, err
}
if ok {
return c.cur, c._hash(c.hashID[c.lvl]), c._hasTree(), nil
}
return c._next()
}
func (c *AccTrieCursor) Next() (k, v []byte, hasTree bool, err error) {
c.SkipState = true
c.prev = append(c.prev[:0], c.cur...)
err = c._preOrderTraversalStepNoInDepth()
if err != nil {
return []byte{}, nil, false, err
}
if c.k[c.lvl] == nil {
c.cur = nil
c.SkipState = c.SkipState && !dbutils2.NextNibblesSubtree(c.prev, &c.next)
return nil, nil, false, nil
}
ok, err := c._consume()
if err != nil {
return []byte{}, nil, false, err
}
if ok {
return c.cur, c._hash(c.hashID[c.lvl]), c._hasTree(), nil
}
return c._next()
}
func (c *AccTrieCursor) _seek(seek []byte, withinPrefix []byte) (bool, error) {
var k, v []byte
var err error
if len(seek) == 0 {
k, v, err = c.c.First()
} else {
//TODO: write more common optimization - maintain .canUseNext variable by hasTree info - similar to skipState
// optimistic .Next call, can use result in 2 cases:
// - k is not child of current key
// - looking for first child, means: c.childID[c.lvl] <= int16(bits.TrailingZeros16(c.hasTree[c.lvl]))
// otherwise do .Seek call
//k, v, err = c.c.Next()
//if err != nil {
// return false, err
//}
//if bytes.HasPrefix(k, c.k[c.lvl]) {
// c.is++
k, v, err = c.c.Seek(seek)
//}
}
if err != nil {
return false, err
}
if len(withinPrefix) > 0 { // seek within given prefix must not terminate overall process, even if k==nil
if k == nil {
return false, nil
}
if !bytes.HasPrefix(k, withinPrefix) {
return false, nil
}
} else { // seek over global prefix does terminate overall process
if k == nil {
c.k[c.lvl] = nil
return false, nil
}
if !bytes.HasPrefix(k, c.prefix) {
c.k[c.lvl] = nil
return false, nil
}
}
c._unmarshal(k, v)
c._nextSiblingInMem()
return true, nil
}
func (c *AccTrieCursor) _nextSiblingInMem() bool {
for c.childID[c.lvl] < int8(bits.Len16(c.hasState[c.lvl])) {
c.childID[c.lvl]++
if c._hasHash() {
c.hashID[c.lvl]++
return true
}
if c._hasTree() {
return true
}
if c._hasState() {
c.SkipState = false
}
}
return false
}
func (c *AccTrieCursor) _nextSiblingOfParentInMem() bool {
originalLvl := c.lvl
for c.lvl > 1 {
c.lvl--
if c.k[c.lvl] == nil {
continue
}
c.next = append(append(c.next[:0], c.k[originalLvl]...), uint8(c.childID[originalLvl]))
c.kBuf = append(append(c.kBuf[:0], c.k[c.lvl]...), uint8(c.childID[c.lvl]))
ok, err := c._seek(c.next, c.kBuf)
if err != nil {
panic(err)
}
if ok {
return true
}
if c._nextSiblingInMem() {
return true
}
originalLvl = c.lvl
}
return false
}
func (c *AccTrieCursor) _nextSiblingInDB() error {
ok := dbutils2.NextNibblesSubtree(c.k[c.lvl], &c.next)
if !ok {
c.k[c.lvl] = nil
return nil
}
if _, err := c._seek(c.next, []byte{}); err != nil {
return err
}
if c.k[c.lvl] == nil || !bytes.HasPrefix(c.next, c.k[c.lvl]) {
// If the cursor has moved beyond the next subtree, we need to check to make
// sure that any modified keys in between are processed.
c.SkipState = false
}
return nil
}
func (c *AccTrieCursor) _unmarshal(k, v []byte) {
from, to := c.lvl+1, len(k)
if c.lvl >= len(k) {
from, to = len(k)+1, c.lvl+2
}
// Consider a trie DB with keys like: [0xa, 0xbb], then unmarshaling 0xbb
// needs to nil the existing 0xa key entry, as it is no longer a parent.
for i := from - 1; i > 0; i-- {
if c.k[i] == nil {
continue
}
if bytes.HasPrefix(k, c.k[i]) {
break
}
from = i
}
for i := from; i < to; i++ { // if first meet key is not 0 length, then nullify all shorter metadata
c.k[i], c.hasState[i], c.hasTree[i], c.hasHash[i], c.hashID[i], c.childID[i], c.deleted[i] = nil, 0, 0, 0, 0, 0, false
}
c.lvl = len(k)
c.k[c.lvl] = k
c.deleted[c.lvl] = false
c.hasState[c.lvl], c.hasTree[c.lvl], c.hasHash[c.lvl], c.v[c.lvl], _ = UnmarshalTrieNode(v)
c.hashID[c.lvl] = -1
c.childID[c.lvl] = int8(bits.TrailingZeros16(c.hasState[c.lvl]) - 1)
}
func (c *AccTrieCursor) _deleteCurrent() error {
if c.hc == nil {
return nil
}
if c.hc == nil || c.deleted[c.lvl] {
return nil
}
if err := c.hc(c.k[c.lvl], 0, 0, 0, nil, nil); err != nil {
return err
}
c.deleted[c.lvl] = true
return nil
}
func (c *AccTrieCursor) _hasState() bool { return (1<<c.childID[c.lvl])&c.hasState[c.lvl] != 0 }
func (c *AccTrieCursor) _hasTree() bool { return (1<<c.childID[c.lvl])&c.hasTree[c.lvl] != 0 }
func (c *AccTrieCursor) _hasHash() bool { return (1<<c.childID[c.lvl])&c.hasHash[c.lvl] != 0 }
func (c *AccTrieCursor) _hash(i int8) []byte {
return c.v[c.lvl][length2.Hash*int(i) : length2.Hash*(int(i)+1)]
}
func (c *AccTrieCursor) _consume() (bool, error) {
if c._hasHash() {
c.kBuf = append(append(c.kBuf[:0], c.k[c.lvl]...), uint8(c.childID[c.lvl]))
if ok, nextCreated := c.canUse(c.kBuf); ok {
c.SkipState = c.SkipState && keyIsBefore(c.kBuf, c.nextCreated)
c.nextCreated = nextCreated
c.cur = append(c.cur[:0], c.kBuf...)
return true, nil
}
}
if err := c._deleteCurrent(); err != nil {
return false, err
}
return false, nil
}
func (c *AccTrieCursor) _next() (k, v []byte, hasTree bool, err error) {
var ok bool
if err = libcommon.Stopped(c.quit); err != nil {
return []byte{}, nil, false, err
}
c.SkipState = c.SkipState && c._hasTree()
err = c._preOrderTraversalStep()
if err != nil {
return []byte{}, nil, false, err
}
for {
if c.k[c.lvl] == nil {
c.cur = nil
c.SkipState = c.SkipState && !dbutils2.NextNibblesSubtree(c.prev, &c.next)
return nil, nil, false, nil
}
ok, err = c._consume()
if err != nil {
return []byte{}, nil, false, err
}
if ok {
return c.cur, c._hash(c.hashID[c.lvl]), c._hasTree(), nil
}
c.SkipState = c.SkipState && c._hasTree()
err = c._preOrderTraversalStep()
if err != nil {
return []byte{}, nil, false, err
}
}
}
// StorageTrieCursor - holds logic related to iteration over AccTrie bucket