Skip to content

Commit 7b6a377

Browse files
committed
handle deleting dangling ref root key
1 parent 0bce70d commit 7b6a377

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

nodedb.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -459,31 +459,36 @@ func newRootkeyCache() *rootkeyCache {
459459
// deletes orphans
460460
func (ndb *nodeDB) deleteVersion(version int64, cache *rootkeyCache) error {
461461
rootKey, err := cache.getRootKey(ndb, version)
462-
if err != nil {
462+
if err != nil && err != ErrVersionDoesNotExist {
463463
return err
464464
}
465465

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+
}
472477
}
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
473491
}
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
487492
}
488493

489494
literalRootKey := GetRootKey(version)

0 commit comments

Comments
 (0)