Skip to content

Commit af27120

Browse files
committed
no need to check block has no children when indexing new block by its parent
1 parent 701b84d commit af27120

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

storage/operation/children.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,19 @@ func IndexNewClusterBlock(lctx lockctx.Proof, rw storage.ReaderBatchWriter, bloc
4343
return indexBlockByParent(rw, blockID, parentID)
4444
}
4545

46+
// indexBlockByParent is the internal function that implements the indexing logic for both regular blocks and cluster blocks.
47+
// the caller must ensure the required locks are held to prevent concurrent writes to the [codeBlockChildren] key space.
4648
func indexBlockByParent(rw storage.ReaderBatchWriter, blockID flow.Identifier, parentID flow.Identifier) error {
47-
// Step 1: make sure the new block has no children yet
48-
var nonExist flow.IdentifierList
49-
err := RetrieveBlockChildren(rw.GlobalReader(), blockID, &nonExist)
50-
if err != nil {
51-
if !errors.Is(err, storage.ErrNotFound) {
52-
return fmt.Errorf("could not check for existing children of new block: %w", err)
53-
}
54-
}
55-
56-
if len(nonExist) > 0 {
57-
return fmt.Errorf("a new block supposed to have no children, but found %v: %w", nonExist,
58-
storage.ErrAlreadyExists)
59-
}
60-
61-
// By convention, the parentID being [flow.ZeroID] means that the block has no parent.
62-
// This is the case for genesis blocks and cluster root blocks. In this case, there
63-
// is no parent, whose child we can index.
49+
// By convention, the parentID being [flow.ZeroID] means that the block is a root block that has no parent.
50+
// This is the case for genesis blocks and cluster root blocks. In this case, we don't need to index anything.
6451
if parentID == flow.ZeroID {
6552
return nil
6653
}
6754

6855
// If the parent block is not zero, depending on whether the parent block has
6956
// children or not, we will either update the index or insert the index.
7057
var childrenIDs flow.IdentifierList
71-
err = RetrieveBlockChildren(rw.GlobalReader(), parentID, &childrenIDs)
58+
err := RetrieveBlockChildren(rw.GlobalReader(), parentID, &childrenIDs)
7259
if err != nil {
7360
if !errors.Is(err, storage.ErrNotFound) {
7461
return fmt.Errorf("could not look up block children: %w", err)

0 commit comments

Comments
 (0)