@@ -459,31 +459,36 @@ func newRootkeyCache() *rootkeyCache {
459
459
// deletes orphans
460
460
func (ndb * nodeDB ) deleteVersion (version int64 , cache * rootkeyCache ) error {
461
461
rootKey , err := cache .getRootKey (ndb , version )
462
- if err != nil {
462
+ if err != nil && err != ErrVersionDoesNotExist {
463
463
return err
464
464
}
465
465
466
- if err := ndb .traverseOrphansWithRootkeyCache (cache , version , version + 1 , func (orphan * Node ) error {
467
- if orphan .nodeKey .nonce == 0 && ! orphan .isLegacy {
468
- // if the orphan is a reformatted root, it can be a legacy root
469
- // so it should be removed from the pruning process.
470
- if err := ndb .deleteFromPruning (ndb .legacyNodeKey (orphan .hash )); err != nil {
471
- return err
466
+ // If rootKey is nil, it indicates that the root is either a dangling reference or does not exist at all.
467
+ // In this case, we can skip the orphans pruning process since there are no nodes in the current version to be considered orphans.
468
+ // Otherwise, proceed with pruning the orphans.
469
+ if rootKey != nil {
470
+ if err := ndb .traverseOrphansWithRootkeyCache (cache , version , version + 1 , func (orphan * Node ) error {
471
+ if orphan .nodeKey .nonce == 0 && ! orphan .isLegacy {
472
+ // if the orphan is a reformatted root, it can be a legacy root
473
+ // so it should be removed from the pruning process.
474
+ if err := ndb .deleteFromPruning (ndb .legacyNodeKey (orphan .hash )); err != nil {
475
+ return err
476
+ }
472
477
}
478
+ if orphan .nodeKey .nonce == 1 && orphan .nodeKey .version < version {
479
+ // if the orphan is referred to the previous root, it should be reformatted
480
+ // to (version, 0), because the root (version, 1) should be removed but not
481
+ // applied now due to the batch writing.
482
+ orphan .nodeKey .nonce = 0
483
+ }
484
+ nk := orphan .GetKey ()
485
+ if orphan .isLegacy {
486
+ return ndb .deleteFromPruning (ndb .legacyNodeKey (nk ))
487
+ }
488
+ return ndb .deleteFromPruning (ndb .nodeKey (nk ))
489
+ }); err != nil {
490
+ return err
473
491
}
474
- if orphan .nodeKey .nonce == 1 && orphan .nodeKey .version < version {
475
- // if the orphan is referred to the previous root, it should be reformatted
476
- // to (version, 0), because the root (version, 1) should be removed but not
477
- // applied now due to the batch writing.
478
- orphan .nodeKey .nonce = 0
479
- }
480
- nk := orphan .GetKey ()
481
- if orphan .isLegacy {
482
- return ndb .deleteFromPruning (ndb .legacyNodeKey (nk ))
483
- }
484
- return ndb .deleteFromPruning (ndb .nodeKey (nk ))
485
- }); err != nil {
486
- return err
487
492
}
488
493
489
494
literalRootKey := GetRootKey (version )
0 commit comments