@@ -18,9 +18,13 @@ var EmptyHash, _ = NewEmptyTrie().Hash()
18
18
19
19
// Trie is a base 16 modified Merkle Patricia trie.
20
20
type Trie struct {
21
- generation uint64
22
- root * Node
23
- childTries map [common.Hash ]* Trie
21
+ generation uint64
22
+ root * Node
23
+ childTries map [common.Hash ]* Trie
24
+ // deletedMerkleValues are the node Merkle values that were deleted
25
+ // from this trie since the last snapshot. These are used by the online
26
+ // pruner to detect with database keys (trie node Merkle values) can
27
+ // be deleted.
24
28
deletedMerkleValues map [string ]struct {}
25
29
}
26
30
@@ -319,20 +323,22 @@ func findNextKeyChild(children []*Node, startIndex byte,
319
323
// key specified in little Endian format.
320
324
func (t * Trie ) Put (keyLE , value []byte ) {
321
325
nibblesKey := codec .KeyLEToNibbles (keyLE )
322
- t .root , _ = t .insert (t .root , nibblesKey , value )
326
+ t .root , _ , _ = t .insert (t .root , nibblesKey , value )
323
327
}
324
328
325
329
// insert inserts a value in the trie at the key specified.
326
330
// It may create one or more new nodes or update an existing node.
327
- func (t * Trie ) insert (parent * Node , key , value []byte ) (newParent * Node , nodesCreated uint32 ) {
331
+ func (t * Trie ) insert (parent * Node , key , value []byte ) (
332
+ newParent * Node , mutated bool , nodesCreated uint32 ) {
328
333
if parent == nil {
329
- const nodesCreated = 1
334
+ mutated = true
335
+ nodesCreated = 1
330
336
return & Node {
331
337
Key : key ,
332
338
SubValue : value ,
333
339
Generation : t .generation ,
334
340
Dirty : true ,
335
- }, nodesCreated
341
+ }, mutated , nodesCreated
336
342
}
337
343
338
344
// TODO ensure all values have dirty set to true
@@ -344,23 +350,26 @@ func (t *Trie) insert(parent *Node, key, value []byte) (newParent *Node, nodesCr
344
350
}
345
351
346
352
func (t * Trie ) insertInLeaf (parentLeaf * Node , key , value []byte ) (
347
- newParent * Node , nodesCreated uint32 ) {
353
+ newParent * Node , mutated bool , nodesCreated uint32 ) {
348
354
if bytes .Equal (parentLeaf .Key , key ) {
349
355
nodesCreated = 0
350
- if bytes .Equal (value , parentLeaf .SubValue ) {
351
- return parentLeaf , nodesCreated
356
+ if parentLeaf .SubValueEqual (value ) {
357
+ mutated = false
358
+ return parentLeaf , mutated , nodesCreated
352
359
}
353
360
354
361
copySettings := node .DefaultCopySettings
355
362
copySettings .CopyValue = false
356
363
parentLeaf = t .prepLeafForMutation (parentLeaf , copySettings )
357
364
parentLeaf .SubValue = value
358
- return parentLeaf , nodesCreated
365
+ mutated = true
366
+ return parentLeaf , mutated , nodesCreated
359
367
}
360
368
361
369
commonPrefixLength := lenCommonPrefix (key , parentLeaf .Key )
362
370
363
371
// Convert the current leaf parent into a branch parent
372
+ mutated = true
364
373
newBranchParent := & Node {
365
374
Key : key [:commonPrefixLength ],
366
375
Generation : t .generation ,
@@ -376,15 +385,18 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, value []byte) (
376
385
if len (key ) < len (parentLeafKey ) {
377
386
// Move the current leaf parent as a child to the new branch.
378
387
copySettings := node .DefaultCopySettings
379
- parentLeaf = t .prepLeafForMutation (parentLeaf , copySettings )
380
388
childIndex := parentLeafKey [commonPrefixLength ]
381
- parentLeaf .Key = parentLeaf .Key [commonPrefixLength + 1 :]
389
+ newParentLeafKey := parentLeaf .Key [commonPrefixLength + 1 :]
390
+ if ! bytes .Equal (parentLeaf .Key , newParentLeafKey ) {
391
+ parentLeaf = t .prepLeafForMutation (parentLeaf , copySettings )
392
+ parentLeaf .Key = newParentLeafKey
393
+ }
382
394
newBranchParent .Children [childIndex ] = parentLeaf
383
395
newBranchParent .Descendants ++
384
396
nodesCreated ++
385
397
}
386
398
387
- return newBranchParent , nodesCreated
399
+ return newBranchParent , mutated , nodesCreated
388
400
}
389
401
390
402
if len (parentLeaf .Key ) == commonPrefixLength {
@@ -393,9 +405,12 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, value []byte) (
393
405
} else {
394
406
// make the leaf a child of the new branch
395
407
copySettings := node .DefaultCopySettings
396
- parentLeaf = t .prepLeafForMutation (parentLeaf , copySettings )
397
408
childIndex := parentLeafKey [commonPrefixLength ]
398
- parentLeaf .Key = parentLeaf .Key [commonPrefixLength + 1 :]
409
+ newParentLeafKey := parentLeaf .Key [commonPrefixLength + 1 :]
410
+ if ! bytes .Equal (parentLeaf .Key , newParentLeafKey ) {
411
+ parentLeaf = t .prepLeafForMutation (parentLeaf , copySettings )
412
+ parentLeaf .Key = newParentLeafKey
413
+ }
399
414
newBranchParent .Children [childIndex ] = parentLeaf
400
415
newBranchParent .Descendants ++
401
416
nodesCreated ++
@@ -410,17 +425,22 @@ func (t *Trie) insertInLeaf(parentLeaf *Node, key, value []byte) (
410
425
newBranchParent .Descendants ++
411
426
nodesCreated ++
412
427
413
- return newBranchParent , nodesCreated
428
+ return newBranchParent , mutated , nodesCreated
414
429
}
415
430
416
431
func (t * Trie ) insertInBranch (parentBranch * Node , key , value []byte ) (
417
- newParent * Node , nodesCreated uint32 ) {
432
+ newParent * Node , mutated bool , nodesCreated uint32 ) {
418
433
copySettings := node .DefaultCopySettings
419
- parentBranch = t .prepBranchForMutation (parentBranch , copySettings )
420
434
421
435
if bytes .Equal (key , parentBranch .Key ) {
436
+ if parentBranch .SubValueEqual (value ) {
437
+ mutated = false
438
+ return parentBranch , mutated , 0
439
+ }
440
+ parentBranch = t .prepBranchForMutation (parentBranch , copySettings )
422
441
parentBranch .SubValue = value
423
- return parentBranch , 0
442
+ mutated = true
443
+ return parentBranch , mutated , 0
424
444
}
425
445
426
446
if bytes .HasPrefix (key , parentBranch .Key ) {
@@ -438,17 +458,27 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
438
458
Dirty : true ,
439
459
}
440
460
nodesCreated = 1
441
- } else {
442
- child , nodesCreated = t .insert (child , remainingKey , value )
461
+ parentBranch = t .prepBranchForMutation (parentBranch , copySettings )
462
+ parentBranch .Children [childIndex ] = child
463
+ parentBranch .Descendants += nodesCreated
464
+ mutated = true
465
+ return parentBranch , mutated , nodesCreated
466
+ }
467
+
468
+ child , mutated , nodesCreated = t .insert (child , remainingKey , value )
469
+ if ! mutated {
470
+ return parentBranch , mutated , 0
443
471
}
444
472
473
+ parentBranch = t .prepBranchForMutation (parentBranch , copySettings )
445
474
parentBranch .Children [childIndex ] = child
446
475
parentBranch .Descendants += nodesCreated
447
- return parentBranch , nodesCreated
476
+ return parentBranch , mutated , nodesCreated
448
477
}
449
478
450
479
// we need to branch out at the point where the keys diverge
451
480
// update partial keys, new branch has key up to matching length
481
+ mutated = true
452
482
nodesCreated = 1
453
483
commonPrefixLength := lenCommonPrefix (key , parentBranch .Key )
454
484
newParentBranch := & Node {
@@ -461,6 +491,8 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
461
491
oldParentIndex := parentBranch .Key [commonPrefixLength ]
462
492
remainingOldParentKey := parentBranch .Key [commonPrefixLength + 1 :]
463
493
494
+ // Note: parentBranch.Key != remainingOldParentKey
495
+ parentBranch = t .prepBranchForMutation (parentBranch , copySettings )
464
496
parentBranch .Key = remainingOldParentKey
465
497
newParentBranch .Children [oldParentIndex ] = parentBranch
466
498
newParentBranch .Descendants += 1 + parentBranch .Descendants
@@ -471,12 +503,12 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
471
503
childIndex := key [commonPrefixLength ]
472
504
remainingKey := key [commonPrefixLength + 1 :]
473
505
var additionalNodesCreated uint32
474
- newParentBranch .Children [childIndex ], additionalNodesCreated = t .insert (nil , remainingKey , value )
506
+ newParentBranch .Children [childIndex ], _ , additionalNodesCreated = t .insert (nil , remainingKey , value )
475
507
nodesCreated += additionalNodesCreated
476
508
newParentBranch .Descendants += additionalNodesCreated
477
509
}
478
510
479
- return newParentBranch , nodesCreated
511
+ return newParentBranch , mutated , nodesCreated
480
512
}
481
513
482
514
// LoadFromMap loads the given data mapping of key to value into the trie.
@@ -792,7 +824,11 @@ func (t *Trie) deleteNodesLimit(parent *Node, limit uint32) (
792
824
793
825
copySettings := node .DefaultCopySettings
794
826
branch = t .prepBranchForMutation (branch , copySettings )
827
+
795
828
branch .Children [i ], newDeleted , newNodesRemoved = t .deleteNodesLimit (child , limit )
829
+ // Note: newDeleted can never be zero here since the limit isn't zero
830
+ // and the child is not nil. Therefore it is safe to prepare the branch
831
+ // for mutation right before this call.
796
832
if branch .Children [i ] == nil {
797
833
nilChildren ++
798
834
}
0 commit comments