This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
1376 lines (1256 loc) · 36.3 KB
/
trie.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 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty off
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package trie implements Merkle Patricia Tries.
package trie
import (
"bytes"
"encoding/binary"
"fmt"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/ethdb"
)
var (
// EmptyRoot is the known root hash of an empty trie.
// DESCRIBED: docs/programmers_guide/guide.md#root
EmptyRoot = libcommon.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
// emptyState is the known hash of an empty state trie entry.
emptyState = crypto.Keccak256Hash(nil)
)
// Trie is a Merkle Patricia Trie.
// The zero value is an empty trie with no database.
// Use New to create a trie that sits on top of a database.
//
// Trie is not safe for concurrent use.
// Deprecated
// use package turbo/trie
type Trie struct {
root node
valueNodesRLPEncoded bool
newHasherFunc func() *hasher
hashMap map[libcommon.Hash]node
observers *ObserverMux
}
// New creates a trie with an existing root node from db.
//
// If root is the zero hash or the sha3 hash of an empty string, the
// trie is initially empty and does not require a database. Otherwise,
// New will panic if db is nil and returns a MissingNodeError if root does
// not exist in the database. Accessing the trie loads nodes from db on demand.
// Deprecated
// use package turbo/trie
func New(root libcommon.Hash) *Trie {
trie := &Trie{
newHasherFunc: func() *hasher { return newHasher( /*valueNodesRlpEncoded = */ false) },
hashMap: make(map[libcommon.Hash]node),
observers: NewTrieObserverMux(),
}
if (root != libcommon.Hash{}) && root != EmptyRoot {
trie.root = hashNode{hash: root[:]}
}
return trie
}
// NewTestRLPTrie treats all the data provided to `Update` function as rlp-encoded.
// it is usually used for testing purposes.
func NewTestRLPTrie(root libcommon.Hash) *Trie {
trie := &Trie{
valueNodesRLPEncoded: true,
newHasherFunc: func() *hasher { return newHasher( /*valueNodesRlpEncoded = */ true) },
hashMap: make(map[libcommon.Hash]node),
observers: NewTrieObserverMux(),
}
if (root != libcommon.Hash{}) && root != EmptyRoot {
trie.root = hashNode{hash: root[:]}
}
return trie
}
func (t *Trie) AddObserver(observer Observer) {
if t.observers == nil {
t.observers = NewTrieObserverMux()
}
t.observers.AddChild(observer)
}
// Get returns the value for key stored in the trie.
func (t *Trie) Get(key []byte) (value []byte, gotValue bool) {
if t.root == nil {
return nil, true
}
hex := keybytesToHex(key)
return t.get(t.root, hex, 0)
}
func (t *Trie) FindPath(key []byte) (value []byte, parents [][]byte, gotValue bool) {
if t.root == nil {
return nil, nil, true
}
hex := keybytesToHex(key)
return t.getPath(t.root, nil, hex, 0)
}
func (t *Trie) GetAccount(key []byte) (value *accounts.Account, gotValue bool) {
if t.root == nil {
return nil, true
}
hex := keybytesToHex(key)
accNode, gotValue := t.getAccount(t.root, hex, 0)
if accNode != nil {
var value accounts.Account
value.Copy(&accNode.Account)
return &value, gotValue
}
return nil, gotValue
}
func (t *Trie) GetAccountCode(key []byte) (value []byte, gotValue bool) {
if t.root == nil {
return nil, false
}
hex := keybytesToHex(key)
accNode, gotValue := t.getAccount(t.root, hex, 0)
if accNode != nil {
if bytes.Equal(accNode.Account.CodeHash[:], EmptyCodeHash[:]) {
return nil, gotValue
}
t.observers.CodeNodeTouched(hex)
if accNode.code == nil {
return nil, false
}
return accNode.code, gotValue
}
return nil, gotValue
}
func (t *Trie) GetAccountCodeSize(key []byte) (value int, gotValue bool) {
if t.root == nil {
return 0, false
}
hex := keybytesToHex(key)
accNode, gotValue := t.getAccount(t.root, hex, 0)
if accNode != nil {
if bytes.Equal(accNode.Account.CodeHash[:], EmptyCodeHash[:]) {
return 0, gotValue
}
if accNode.codeSize == codeSizeUncached {
return 0, false
}
return accNode.codeSize, gotValue
}
return 0, gotValue
}
func (t *Trie) getAccount(origNode node, key []byte, pos int) (value *accountNode, gotValue bool) {
switch n := (origNode).(type) {
case nil:
return nil, true
case *shortNode:
matchlen := prefixLen(key[pos:], n.Key)
if matchlen == len(n.Key) {
if v, ok := n.Val.(*accountNode); ok {
return v, true
} else {
return t.getAccount(n.Val, key, pos+matchlen)
}
} else {
return nil, true
}
case *duoNode:
t.observers.BranchNodeTouched(key[:pos])
i1, i2 := n.childrenIdx()
switch key[pos] {
case i1:
return t.getAccount(n.child1, key, pos+1)
case i2:
return t.getAccount(n.child2, key, pos+1)
default:
return nil, true
}
case *fullNode:
t.observers.BranchNodeTouched(key[:pos])
child := n.Children[key[pos]]
return t.getAccount(child, key, pos+1)
case hashNode:
return nil, false
case *accountNode:
return n, true
default:
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
}
}
func (t *Trie) get(origNode node, key []byte, pos int) (value []byte, gotValue bool) {
switch n := (origNode).(type) {
case nil:
return nil, true
case valueNode:
return n, true
case *accountNode:
return t.get(n.storage, key, pos)
case *shortNode:
matchlen := prefixLen(key[pos:], n.Key)
if matchlen == len(n.Key) || n.Key[matchlen] == 16 {
value, gotValue = t.get(n.Val, key, pos+matchlen)
} else {
value, gotValue = nil, true
}
return
case *duoNode:
t.observers.BranchNodeTouched(key[:pos])
i1, i2 := n.childrenIdx()
switch key[pos] {
case i1:
value, gotValue = t.get(n.child1, key, pos+1)
case i2:
value, gotValue = t.get(n.child2, key, pos+1)
default:
value, gotValue = nil, true
}
return
case *fullNode:
t.observers.BranchNodeTouched(key[:pos])
child := n.Children[key[pos]]
if child == nil {
return nil, true
}
return t.get(child, key, pos+1)
case hashNode:
return n.hash, false
default:
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
}
}
func (t *Trie) getPath(origNode node, parents [][]byte, key []byte, pos int) ([]byte, [][]byte, bool) {
switch n := (origNode).(type) {
case nil:
return nil, parents, true
case valueNode:
return n, parents, true
case *accountNode:
return t.getPath(n.storage, append(parents, n.reference()), key, pos)
case *shortNode:
matchlen := prefixLen(key[pos:], n.Key)
if matchlen == len(n.Key) || n.Key[matchlen] == 16 {
return t.getPath(n.Val, append(parents, n.reference()), key, pos+matchlen)
} else {
return nil, parents, true
}
case *duoNode:
i1, i2 := n.childrenIdx()
switch key[pos] {
case i1:
return t.getPath(n.child1, append(parents, n.reference()), key, pos+1)
case i2:
return t.getPath(n.child2, append(parents, n.reference()), key, pos+1)
default:
return nil, parents, true
}
case *fullNode:
child := n.Children[key[pos]]
if child == nil {
return nil, parents, true
}
return t.getPath(child, append(parents, n.reference()), key, pos+1)
case hashNode:
return n.hash, parents, false
default:
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
}
}
// Update associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
// DESCRIBED: docs/programmers_guide/guide.md#root
func (t *Trie) Update(key, value []byte) {
hex := keybytesToHex(key)
newnode := valueNode(value)
if t.root == nil {
t.root = NewShortNode(hex, newnode)
} else {
_, t.root = t.insert(t.root, hex, valueNode(value))
}
}
func (t *Trie) UpdateAccount(key []byte, acc *accounts.Account) {
//make account copy. There are some pointer into big.Int
value := new(accounts.Account)
value.Copy(acc)
hex := keybytesToHex(key)
var newnode *accountNode
if value.Root == EmptyRoot || value.Root == (libcommon.Hash{}) {
newnode = &accountNode{*value, nil, true, nil, codeSizeUncached}
} else {
newnode = &accountNode{*value, hashNode{hash: value.Root[:]}, true, nil, codeSizeUncached}
}
if t.root == nil {
t.root = NewShortNode(hex, newnode)
} else {
_, t.root = t.insert(t.root, hex, newnode)
}
}
// UpdateAccountCode attaches the code node to an account at specified key
func (t *Trie) UpdateAccountCode(key []byte, code codeNode) error {
if t.root == nil {
return nil
}
hex := keybytesToHex(key)
accNode, gotValue := t.getAccount(t.root, hex, 0)
if accNode == nil || !gotValue {
return fmt.Errorf("account not found with key: %x, %w", key, ethdb.ErrKeyNotFound)
}
actualCodeHash := crypto.Keccak256(code)
if !bytes.Equal(accNode.CodeHash[:], actualCodeHash) {
return fmt.Errorf("inserted code mismatch account hash (acc.CodeHash=%x codeHash=%x)", accNode.CodeHash[:], actualCodeHash)
}
accNode.code = codeNode(bytes.Clone(code))
accNode.codeSize = len(code)
// t.insert will call the observer methods itself
_, t.root = t.insert(t.root, hex, accNode)
return nil
}
// UpdateAccountCodeSize attaches the code size to the account
func (t *Trie) UpdateAccountCodeSize(key []byte, codeSize int) error {
if t.root == nil {
return nil
}
hex := keybytesToHex(key)
accNode, gotValue := t.getAccount(t.root, hex, 0)
if accNode == nil || !gotValue {
return fmt.Errorf("account not found with key: %x, %w", key, ethdb.ErrKeyNotFound)
}
accNode.codeSize = codeSize
// t.insert will call the observer methods itself
_, t.root = t.insert(t.root, hex, accNode)
return nil
}
// LoadRequestForCode Code expresses the need to fetch code from the DB (by its hash) and attach
// to a specific account leaf in the trie.
type LoadRequestForCode struct {
t *Trie
addrHash libcommon.Hash // contract address hash
codeHash libcommon.Hash
bytecode bool // include the bytecode too
}
func (lrc *LoadRequestForCode) String() string {
return fmt.Sprintf("rr_code{addrHash:%x,codeHash:%x,bytecode:%v}", lrc.addrHash, lrc.codeHash, lrc.bytecode)
}
func (t *Trie) NewLoadRequestForCode(addrHash libcommon.Hash, codeHash libcommon.Hash, bytecode bool) *LoadRequestForCode {
return &LoadRequestForCode{t, addrHash, codeHash, bytecode}
}
func (t *Trie) NeedLoadCode(addrHash libcommon.Hash, codeHash libcommon.Hash, bytecode bool) (bool, *LoadRequestForCode) {
if bytes.Equal(codeHash[:], EmptyCodeHash[:]) {
return false, nil
}
var ok bool
if bytecode {
_, ok = t.GetAccountCode(addrHash[:])
} else {
_, ok = t.GetAccountCodeSize(addrHash[:])
}
if !ok {
return true, t.NewLoadRequestForCode(addrHash, codeHash, bytecode)
}
return false, nil
}
// FindSubTriesToLoad walks over the trie and creates the list of DB prefixes and
// corresponding list of valid bits in the prefix (for the cases when prefix contains an
// odd number of nibbles) that would allow loading the missing information from the database
// It also create list of `hooks`, the paths in the trie (in nibbles) where the loaded
// sub-tries need to be inserted.
func (t *Trie) FindSubTriesToLoad(rl RetainDecider) (prefixes [][]byte, fixedbits []int, hooks [][]byte) {
return findSubTriesToLoad(t.root, nil, nil, rl, nil, 0, nil, nil, nil)
}
var bytes8 [8]byte
var bytes16 [16]byte
func findSubTriesToLoad(nd node, nibblePath []byte, hook []byte, rl RetainDecider, dbPrefix []byte, bits int, prefixes [][]byte, fixedbits []int, hooks [][]byte) (newPrefixes [][]byte, newFixedBits []int, newHooks [][]byte) {
switch n := nd.(type) {
case *shortNode:
nKey := n.Key
if nKey[len(nKey)-1] == 16 {
nKey = nKey[:len(nKey)-1]
}
nibblePath = append(nibblePath, nKey...)
hook = append(hook, nKey...)
if !rl.Retain(nibblePath) {
return prefixes, fixedbits, hooks
}
for _, b := range nKey {
if bits%8 == 0 {
dbPrefix = append(dbPrefix, b<<4)
} else {
dbPrefix[len(dbPrefix)-1] &= 0xf0
dbPrefix[len(dbPrefix)-1] |= b & 0xf
}
bits += 4
}
return findSubTriesToLoad(n.Val, nibblePath, hook, rl, dbPrefix, bits, prefixes, fixedbits, hooks)
case *duoNode:
i1, i2 := n.childrenIdx()
newPrefixes = prefixes
newFixedBits = fixedbits
newHooks = hooks
newNibblePath := append(nibblePath, i1)
newHook := append(hook, i1)
if rl.Retain(newNibblePath) {
var newDbPrefix []byte
if bits%8 == 0 {
newDbPrefix = append(dbPrefix, i1<<4)
} else {
newDbPrefix = dbPrefix
newDbPrefix[len(newDbPrefix)-1] &= 0xf0
newDbPrefix[len(newDbPrefix)-1] |= i1 & 0xf
}
newPrefixes, newFixedBits, newHooks = findSubTriesToLoad(n.child1, newNibblePath, newHook, rl, newDbPrefix, bits+4, newPrefixes, newFixedBits, newHooks)
}
newNibblePath = append(nibblePath, i2)
newHook = append(hook, i2)
if rl.Retain(newNibblePath) {
var newDbPrefix []byte
if bits%8 == 0 {
newDbPrefix = append(dbPrefix, i2<<4)
} else {
newDbPrefix = dbPrefix
newDbPrefix[len(newDbPrefix)-1] &= 0xf0
newDbPrefix[len(newDbPrefix)-1] |= i2 & 0xf
}
newPrefixes, newFixedBits, newHooks = findSubTriesToLoad(n.child2, newNibblePath, newHook, rl, newDbPrefix, bits+4, newPrefixes, newFixedBits, newHooks)
}
return newPrefixes, newFixedBits, newHooks
case *fullNode:
newPrefixes = prefixes
newFixedBits = fixedbits
newHooks = hooks
var newNibblePath []byte
var newHook []byte
for i, child := range n.Children {
if child != nil {
newNibblePath = append(nibblePath, byte(i))
newHook = append(hook, byte(i))
if rl.Retain(newNibblePath) {
var newDbPrefix []byte
if bits%8 == 0 {
newDbPrefix = append(dbPrefix, byte(i)<<4)
} else {
newDbPrefix = dbPrefix
newDbPrefix[len(newDbPrefix)-1] &= 0xf0
newDbPrefix[len(newDbPrefix)-1] |= byte(i) & 0xf
}
newPrefixes, newFixedBits, newHooks = findSubTriesToLoad(child, newNibblePath, newHook, rl, newDbPrefix, bits+4, newPrefixes, newFixedBits, newHooks)
}
}
}
return newPrefixes, newFixedBits, newHooks
case *accountNode:
if n.storage == nil {
return prefixes, fixedbits, hooks
}
binary.BigEndian.PutUint64(bytes8[:], n.Incarnation)
dbPrefix = append(dbPrefix, bytes8[:]...)
// Add decompressed incarnation to the nibblePath
for i, b := range bytes8[:] {
bytes16[i*2] = b / 16
bytes16[i*2+1] = b % 16
}
nibblePath = append(nibblePath, bytes16[:]...)
newPrefixes = prefixes
newFixedBits = fixedbits
newHooks = hooks
if rl.Retain(nibblePath) {
newPrefixes, newFixedBits, newHooks = findSubTriesToLoad(n.storage, nibblePath, hook, rl, dbPrefix, bits+64, prefixes, fixedbits, hooks)
}
return newPrefixes, newFixedBits, newHooks
case hashNode:
newPrefixes = append(prefixes, libcommon.Copy(dbPrefix))
newFixedBits = append(fixedbits, bits)
newHooks = append(hooks, libcommon.Copy(hook))
return newPrefixes, newFixedBits, newHooks
}
return prefixes, fixedbits, hooks
}
// can pass incarnation=0 if start from root, method internally will
// put incarnation from accountNode when pass it by traverse
func (t *Trie) insert(origNode node, key []byte, value node) (updated bool, newNode node) {
return t.insertRecursive(origNode, key, 0, value)
}
func (t *Trie) insertRecursive(origNode node, key []byte, pos int, value node) (updated bool, newNode node) {
if len(key) == pos {
origN, origNok := origNode.(valueNode)
vn, vnok := value.(valueNode)
if origNok && vnok {
updated = !bytes.Equal(origN, vn)
if updated {
newNode = value
} else {
newNode = origN
}
return
}
origAccN, origNok := origNode.(*accountNode)
vAccN, vnok := value.(*accountNode)
if origNok && vnok {
updated = !origAccN.Equals(&vAccN.Account)
if updated {
if !bytes.Equal(origAccN.CodeHash[:], vAccN.CodeHash[:]) {
origAccN.code = nil
} else if vAccN.code != nil {
origAccN.code = vAccN.code
}
origAccN.Account.Copy(&vAccN.Account)
origAccN.codeSize = vAccN.codeSize
origAccN.rootCorrect = false
}
newNode = origAccN
if len(origAccN.code) > 0 {
t.observers.CodeNodeSizeChanged(key[:pos-1], uint(len(origAccN.code)))
} else {
t.observers.CodeNodeDeleted(key[:pos-1])
}
return
}
// replacing nodes except accounts
if !origNok {
t.evictSubtreeFromHashMap(origNode)
return true, value
}
}
var nn node
switch n := origNode.(type) {
case nil:
return true, NewShortNode(libcommon.Copy(key[pos:]), value)
case *accountNode:
updated, nn = t.insertRecursive(n.storage, key, pos, value)
if updated {
n.storage = nn
n.rootCorrect = false
}
return updated, n
case *shortNode:
matchlen := prefixLen(key[pos:], n.Key)
// If the whole key matches, keep this short node as is
// and only update the value.
if matchlen == len(n.Key) || n.Key[matchlen] == 16 {
updated, nn = t.insertRecursive(n.Val, key, pos+matchlen, value)
if updated {
t.evictNodeFromHashMap(n)
n.Val = nn
n.ref.len = 0
}
newNode = n
} else {
// Otherwise branch out at the index where they differ.
t.evictNodeFromHashMap(n)
var c1 node
if len(n.Key) == matchlen+1 {
c1 = n.Val
} else {
c1 = NewShortNode(libcommon.Copy(n.Key[matchlen+1:]), n.Val)
}
var c2 node
if len(key) == pos+matchlen+1 {
c2 = value
} else {
c2 = NewShortNode(libcommon.Copy(key[pos+matchlen+1:]), value)
}
branch := &duoNode{}
if n.Key[matchlen] < key[pos+matchlen] {
branch.child1 = c1
branch.child2 = c2
} else {
branch.child1 = c2
branch.child2 = c1
}
branch.mask = (1 << (n.Key[matchlen])) | (1 << (key[pos+matchlen]))
// Replace this shortNode with the branch if it occurs at index 0.
if matchlen == 0 {
newNode = branch // current node leaves the generation, but new node branch joins it
} else {
// Otherwise, replace it with a short node leading up to the branch.
n.Key = libcommon.Copy(key[pos : pos+matchlen])
n.Val = branch
n.ref.len = 0
newNode = n
}
t.observers.BranchNodeCreated(key[:pos+matchlen])
updated = true
}
return
case *duoNode:
t.observers.BranchNodeTouched(key[:pos])
i1, i2 := n.childrenIdx()
switch key[pos] {
case i1:
updated, nn = t.insertRecursive(n.child1, key, pos+1, value)
if updated {
t.evictNodeFromHashMap(n)
n.child1 = nn
n.ref.len = 0
}
newNode = n
case i2:
updated, nn = t.insertRecursive(n.child2, key, pos+1, value)
if updated {
t.evictNodeFromHashMap(n)
n.child2 = nn
n.ref.len = 0
}
newNode = n
default:
t.evictNodeFromHashMap(n)
var child node
if len(key) == pos+1 {
child = value
} else {
child = NewShortNode(libcommon.Copy(key[pos+1:]), value)
}
newnode := &fullNode{}
newnode.Children[i1] = n.child1
newnode.Children[i2] = n.child2
newnode.Children[key[pos]] = child
updated = true
// current node leaves the generation but newnode joins it
newNode = newnode
}
return
case *fullNode:
t.observers.BranchNodeTouched(key[:pos])
child := n.Children[key[pos]]
if child == nil {
t.evictNodeFromHashMap(n)
if len(key) == pos+1 {
n.Children[key[pos]] = value
} else {
n.Children[key[pos]] = NewShortNode(libcommon.Copy(key[pos+1:]), value)
}
updated = true
n.ref.len = 0
} else {
updated, nn = t.insertRecursive(child, key, pos+1, value)
if updated {
t.evictNodeFromHashMap(n)
n.Children[key[pos]] = nn
n.ref.len = 0
}
}
newNode = n
return
default:
panic(fmt.Sprintf("%T: invalid node: %v. Searched by: key=%x, pos=%d", n, n, key, pos))
}
}
// non-recursive version of get and returns: node and parent node
func (t *Trie) getNode(hex []byte, doTouch bool) (node, node, bool, uint64) {
var nd = t.root
var parent node
pos := 0
var account bool
var incarnation uint64
for pos < len(hex) || account {
switch n := nd.(type) {
case nil:
return nil, nil, false, incarnation
case *shortNode:
matchlen := prefixLen(hex[pos:], n.Key)
if matchlen == len(n.Key) || n.Key[matchlen] == 16 {
parent = n
nd = n.Val
pos += matchlen
if _, ok := nd.(*accountNode); ok {
account = true
}
} else {
return nil, nil, false, incarnation
}
case *duoNode:
if doTouch {
t.observers.BranchNodeTouched(hex[:pos])
}
i1, i2 := n.childrenIdx()
switch hex[pos] {
case i1:
parent = n
nd = n.child1
pos++
case i2:
parent = n
nd = n.child2
pos++
default:
return nil, nil, false, incarnation
}
case *fullNode:
if doTouch {
t.observers.BranchNodeTouched(hex[:pos])
}
child := n.Children[hex[pos]]
if child == nil {
return nil, nil, false, incarnation
}
parent = n
nd = child
pos++
case *accountNode:
parent = n
nd = n.storage
incarnation = n.Incarnation
account = false
case valueNode:
return nd, parent, true, incarnation
case hashNode:
return nd, parent, true, incarnation
default:
panic(fmt.Sprintf("Unknown node: %T", n))
}
}
return nd, parent, true, incarnation
}
func (t *Trie) HookSubTries(subTries SubTries, hooks [][]byte) error {
for i, hookNibbles := range hooks {
root := subTries.roots[i]
hash := subTries.Hashes[i]
if root == nil {
return fmt.Errorf("root==nil for hook %x", hookNibbles)
}
if err := t.hook(hookNibbles, root, hash[:]); err != nil {
return fmt.Errorf("hook %x: %w", hookNibbles, err)
}
}
return nil
}
func (t *Trie) hook(hex []byte, n node, hash []byte) error {
nd, parent, ok, incarnation := t.getNode(hex, true)
if !ok {
return nil
}
if _, ok := nd.(valueNode); ok {
return nil
}
if hn, ok := nd.(hashNode); ok {
if !bytes.Equal(hn.hash, hash) {
return fmt.Errorf("wrong hash when hooking, expected %s, sub-tree hash %x", hn, hash)
}
} else if nd != nil {
return fmt.Errorf("expected hash node at %x, got %T", hex, nd)
}
t.touchAll(n, hex, false, incarnation)
switch p := parent.(type) {
case nil:
t.root = n
case *shortNode:
p.Val = n
case *duoNode:
i1, i2 := p.childrenIdx()
switch hex[len(hex)-1] {
case i1:
p.child1 = n
case i2:
p.child2 = n
}
case *fullNode:
idx := hex[len(hex)-1]
p.Children[idx] = n
case *accountNode:
p.storage = n
}
return nil
}
func (t *Trie) touchAll(n node, hex []byte, del bool, incarnation uint64) {
if del {
t.evictNodeFromHashMap(n)
} else if len(n.reference()) == length.Hash {
var key libcommon.Hash
copy(key[:], n.reference())
t.hashMap[key] = n
}
switch n := n.(type) {
case *shortNode:
if _, ok := n.Val.(valueNode); !ok {
// Don't need to compute prefix for a leaf
h := n.Key
// Remove terminator
if h[len(h)-1] == 16 {
h = h[:len(h)-1]
}
hexVal := concat(hex, h...)
t.touchAll(n.Val, hexVal, del, incarnation)
}
case *duoNode:
if del {
t.observers.BranchNodeDeleted(hex)
} else {
t.observers.BranchNodeCreated(hex)
t.observers.BranchNodeLoaded(hex, incarnation)
}
i1, i2 := n.childrenIdx()
hex1 := make([]byte, len(hex)+1)
copy(hex1, hex)
hex1[len(hex)] = i1
hex2 := make([]byte, len(hex)+1)
copy(hex2, hex)
hex2[len(hex)] = i2
t.touchAll(n.child1, hex1, del, incarnation)
t.touchAll(n.child2, hex2, del, incarnation)
case *fullNode:
if del {
t.observers.BranchNodeDeleted(hex)
} else {
t.observers.BranchNodeCreated(hex)
t.observers.BranchNodeLoaded(hex, incarnation)
}
for i, child := range n.Children {
if child != nil {
t.touchAll(child, concat(hex, byte(i)), del, incarnation)
}
}
case *accountNode:
if del {
t.observers.CodeNodeDeleted(hex)
} else {
t.observers.CodeNodeTouched(hex)
}
if n.storage != nil {
t.touchAll(n.storage, hex, del, n.Incarnation)
}
}
}
// Delete removes any existing value for key from the trie.
// DESCRIBED: docs/programmers_guide/guide.md#root
func (t *Trie) Delete(key []byte) {
hex := keybytesToHex(key)
_, t.root = t.delete(t.root, hex, false)
}
func (t *Trie) convertToShortNode(child node, pos uint) node {
if pos != 16 {
// If the remaining entry is a short node, it replaces
// n and its key gets the missing nibble tacked to the
// front. This avoids creating an invalid
// shortNode{..., shortNode{...}}. Since the entry
// might not be loaded yet, resolve it just for this
// check.
if short, ok := child.(*shortNode); ok {
t.evictNodeFromHashMap(child)
k := make([]byte, len(short.Key)+1)
k[0] = byte(pos)
copy(k[1:], short.Key)
return NewShortNode(k, short.Val)
}
}
// Otherwise, n is replaced by a one-nibble short node
// containing the child.
return NewShortNode([]byte{byte(pos)}, child)
}
func (t *Trie) delete(origNode node, key []byte, preserveAccountNode bool) (updated bool, newNode node) {
return t.deleteRecursive(origNode, key, 0, preserveAccountNode, 0)
}
// delete returns the new root of the trie with key deleted.
// It reduces the trie to minimal form by simplifying
// nodes on the way up after deleting recursively.
//
// can pass incarnation=0 if start from root, method internally will
// put incarnation from accountNode when pass it by traverse
func (t *Trie) deleteRecursive(origNode node, key []byte, keyStart int, preserveAccountNode bool, incarnation uint64) (updated bool, newNode node) {
var nn node
switch n := origNode.(type) {
case *shortNode:
matchlen := prefixLen(key[keyStart:], n.Key)
if matchlen == cmp.Min(len(n.Key), len(key[keyStart:])) || n.Key[matchlen] == 16 || key[keyStart+matchlen] == 16 {
fullMatch := matchlen == len(key)-keyStart
removeNodeEntirely := fullMatch
if preserveAccountNode {
removeNodeEntirely = len(key) == keyStart || matchlen == len(key[keyStart:])-1
}
if removeNodeEntirely {
t.evictNodeFromHashMap(n)
updated = true
touchKey := key[:keyStart+matchlen]
if touchKey[len(touchKey)-1] == 16 {
touchKey = touchKey[:len(touchKey)-1]
}
t.touchAll(n.Val, touchKey, true, incarnation)
newNode = nil
} else {
// The key is longer than n.Key. Remove the remaining suffix
// from the subtrie. Child can never be nil here since the
// subtrie must contain at least two other values with keys
// longer than n.Key.
updated, nn = t.deleteRecursive(n.Val, key, keyStart+matchlen, preserveAccountNode, incarnation)
if !updated {
newNode = n
} else {
t.evictNodeFromHashMap(n)
if nn == nil {
newNode = nil
} else {
if shortChild, ok := nn.(*shortNode); ok {
// Deleting from the subtrie reduced it to another
// short node. Merge the nodes to avoid creating a
// shortNode{..., shortNode{...}}. Use concat (which
// always creates a new slice) instead of append to
// avoid modifying n.Key since it might be shared with
// other nodes.
newNode = NewShortNode(concat(n.Key, shortChild.Key...), shortChild.Val)
} else {
n.Val = nn
newNode = n
n.ref.len = 0
}
}
}
}
} else {
updated = false
newNode = n // don't replace n on mismatch
}
return
case *duoNode:
i1, i2 := n.childrenIdx()