Skip to content

Commit ab1ad81

Browse files
Apply suggestions from code review
Co-authored-by: Alexander Hentschel <[email protected]>
1 parent ab7d791 commit ab1ad81

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

state/cluster/badger/state_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func TestUnknownSnapshotReference(t *testing.T) {
4343

4444
// Verify that Collection() returns state.ErrUnknownSnapshotReference
4545
_, err = snapshot.Collection()
46-
assert.Error(t, err)
4746
assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference)
4847

4948
// Verify that Head() returns state.ErrUnknownSnapshotReference
@@ -58,7 +57,7 @@ func TestUnknownSnapshotReference(t *testing.T) {
5857
}
5958

6059
// TestValidSnapshotReference verifies that AtBlockID returns a working snapshot
61-
// when given a valid block ID (the genesis block).
60+
// when given a valid block ID (which in this test is the genesis block ID).
6261
func TestValidSnapshotReference(t *testing.T) {
6362
// Setup
6463
genesis, err := unittest.ClusterBlock.Genesis()

state/cluster/invalid/snapshot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type Snapshot struct {
1515
}
1616

1717
// NewSnapshot returns a new invalid snapshot, containing an error describing why the
18-
// snapshot could not be retrieved. The following are expected
19-
// errors when constructing an invalid Snapshot:
18+
// snapshot could not be retrieved. The following are typical
19+
// errors resulting in the construction of an invalid Snapshot:
2020
// - state.ErrUnknownSnapshotReference if the reference point for the snapshot
2121
// (height or block ID) does not resolve to a queriable block in the state.
22-
// - generic error in case of unexpected critical internal corruption or bugs
22+
// - generic error in case of unexpected state inconsistencies or bugs
2323
func NewSnapshot(err error) *Snapshot {
2424
if errors.Is(err, state.ErrUnknownSnapshotReference) {
2525
return &Snapshot{err: err}

storage/operation/children.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"github.com/onflow/flow-go/storage"
1111
)
1212

13-
// IndexNewBlock indexes a new block and updates the parent-child relationship in the block children index.
14-
// This function creates an empty children index for the new block and adds the new block to the parent's children list.
13+
// IndexNewBlock populates the parent-child index for block, by adding the given blockID to the set of children of its parent.
1514
//
1615
// CAUTION:
16+
// - This function should only be used for KNOWN BLOCKs (neither existence of the block nor its parent is verified here)
1717
// - The caller must acquire the [storage.LockInsertBlock] and hold it until the database write has been committed.
1818
//
1919
// Expected error returns during normal operations:
@@ -26,10 +26,11 @@ func IndexNewBlock(lctx lockctx.Proof, rw storage.ReaderBatchWriter, blockID flo
2626
return indexBlockByParent(rw, blockID, parentID)
2727
}
2828

29-
// IndexNewClusterBlock indexes a new cluster block and updates the parent-child relationship in the block children index.
30-
// This function creates an empty children index for the new cluster block and adds the new block to the parent's children list.
29+
// IndexNewClusterBlock populates the parent-child index for cluster blocks, aka collections, by adding the given
30+
// blockID to the set of children of its parent.
3131
//
3232
// CAUTION:
33+
// - This function should only be used for KNOWN BLOCKs (neither existence of the block nor its parent is verified here)
3334
// - The caller must acquire the [storage.LockInsertOrFinalizeClusterBlock] and hold it until the database write has been committed.
3435
//
3536
// Expected error returns during normal operations:
@@ -57,18 +58,15 @@ func indexBlockByParent(rw storage.ReaderBatchWriter, blockID flow.Identifier, p
5758
storage.ErrAlreadyExists)
5859
}
5960

60-
// Step 2: adding the second index for the parent block
61-
// if the parent block is zero, for instance root block has no parent,
62-
// then no need to add index for it
63-
// useful to skip for cluster root block which has no parent
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.
6464
if parentID == flow.ZeroID {
6565
return nil
6666
}
6767

68-
// if the parent block is not zero, depending on whether the parent block has
69-
// children or not, we will either update the index or insert the index:
70-
// when parent block doesn't exist, we will insert the block children.
71-
// when parent block exists already, we will update the block children,
68+
// If the parent block is not zero, depending on whether the parent block has
69+
// children or not, we will either update the index or insert the index.
7270
var childrenIDs flow.IdentifierList
7371
err = RetrieveBlockChildren(rw.GlobalReader(), parentID, &childrenIDs)
7472
if err != nil {
@@ -98,7 +96,7 @@ func indexBlockByParent(rw storage.ReaderBatchWriter, blockID flow.Identifier, p
9896

9997
// RetrieveBlockChildren retrieves the list of child block IDs for the specified parent block.
10098
//
101-
// Expected errors during normal operations:
99+
// No error returns expected during normal operations.
102100
// It returns [storage.ErrNotFound] if the block has no children.
103101
// Note, this would mean either the block does not exist or the block exists but has no children.
104102
// The caller has to check if the block exists by other means if needed.

storage/operation/children_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ import (
1313
"github.com/onflow/flow-go/utils/unittest"
1414
)
1515

16-
// TestOperationPair represents a pair of operations to test
16+
// TestOperationPair is a pair of indexing operation and corresponding lock to be held during the operation.
17+
// The name is an auxiliary identifier, for debugging only.
1718
type TestOperationPair struct {
1819
name string
1920
indexFunc func(lockctx.Proof, storage.ReaderBatchWriter, flow.Identifier, flow.Identifier) error
2021
lockType string
2122
}
2223

23-
// getTestOperationPairs returns the operation pairs to test
24+
// getTestOperationPairs returns a `TestOperationPair` for indexing main consensus blocks
25+
// and one `TestOperationPair` for indexing collector blocks (aka collections).
2426
func getTestOperationPairs() []TestOperationPair {
2527
return []TestOperationPair{
2628
{
@@ -47,7 +49,6 @@ func TestIndexAndLookupChild(t *testing.T) {
4749
// retrieving children of a non-existent block should return empty list
4850
var retrievedIDs flow.IdentifierList
4951
err := operation.RetrieveBlockChildren(db.Reader(), nonExist, &retrievedIDs)
50-
require.Error(t, err)
5152
require.ErrorIs(t, err, storage.ErrNotFound)
5253

5354
parentID := unittest.IdentifierFixture()
@@ -68,7 +69,6 @@ func TestIndexAndLookupChild(t *testing.T) {
6869

6970
err = operation.RetrieveBlockChildren(db.Reader(), childID, &retrievedIDs)
7071
// verify new block has no children index (returning storage.ErrNotFound)
71-
require.Error(t, err)
7272
require.ErrorIs(t, err, storage.ErrNotFound)
7373

7474
// verify indexing again would hit storage.ErrAlreadyExists error
@@ -77,7 +77,6 @@ func TestIndexAndLookupChild(t *testing.T) {
7777
return opPair.indexFunc(lctx, rw, childID, parentID)
7878
})
7979
})
80-
require.Error(t, err)
8180
require.ErrorIs(t, err, storage.ErrAlreadyExists)
8281
})
8382
})

0 commit comments

Comments
 (0)