@@ -11,8 +11,8 @@ import (
1111 "github.com/onflow/flow-go/storage/operation"
1212)
1313
14- // IndexNewBlock and IndexNewClusterBlock will add parent-child index for the new block.
15- // - Each block has a parent, we use this parent-child relationship to build a reverse index
14+ // IndexNewBlock will add parent-child index for the new block.
15+ // - Each block that we ingest has a parent, we use this parent-child relationship to build a reverse index
1616// for looking up children blocks for a given block. This is useful for forks recovery
1717// where we want to find all the pending children blocks for the lastest finalized block.
1818//
@@ -33,6 +33,20 @@ func IndexNewBlock(lctx lockctx.Proof, rw storage.ReaderBatchWriter, blockID flo
3333 return insertNewBlock (lctx , rw , blockID , parentID )
3434}
3535
36+ // IndexNewClusterBlock will add parent-child index for the new block.
37+ // - Each block that we ingest has a parent, we use this parent-child relationship to build a reverse index
38+ // for looking up children blocks for a given block. This is useful for forks recovery
39+ // where we want to find all the pending children blocks for the lastest finalized block.
40+ //
41+ // When adding parent-child index for a new block, we will update two indexes:
42+ // 1. Per protocol convention, blocks must be ingested in "ancestors-first" order. Hence,
43+ // if a block is new (needs to be verified, to avoid state corruption in case of repeated
44+ // calls), its set of persisted children is empty at the time of insertion.
45+ // 2. Since the parent block has this new block as a child, we add to the parent block's children.
46+ // There are two special cases for (2):
47+ // - If the parent block is zero (i.e. genesis block), then we don't need to add this index.
48+ // - If the parent block doesn't exist, then we will index the new block as the only child
49+ // of the parent anyway. This is useful for bootstrapping nodes with truncated history.
3650func IndexNewClusterBlock (lctx lockctx.Proof , rw storage.ReaderBatchWriter , blockID flow.Identifier , parentID flow.Identifier ) error {
3751 if ! lctx .HoldsLock (storage .LockInsertOrFinalizeClusterBlock ) {
3852 return fmt .Errorf ("missing required lock: %s" , storage .LockInsertOrFinalizeClusterBlock )
0 commit comments