Skip to content

Commit 18855c0

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

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

storage/store/headers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (h *Headers) ByParentID(parentID flow.Identifier) ([]*flow.Header, error) {
202202
return nil, fmt.Errorf("could not check existence of parent %x: %w", parentID, err)
203203
}
204204
if !exists {
205-
return nil, fmt.Errorf("could not find parent %x: %w", parentID, storage.ErrNotFound)
205+
return nil, fmt.Errorf("cannot retrieve children of unknown block %x: %w", parentID, storage.ErrNotFound)
206206
}
207207
// parent exists but has no children
208208
return []*flow.Header{}, nil

storage/store/headers_test.go

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,12 @@ func TestHeadersByParentID(t *testing.T) {
127127
require.NoError(t, err)
128128
require.Empty(t, children)
129129

130-
// Create child blocks
131-
child1 := unittest.BlockWithParentFixture(parentBlock.ToHeader())
132-
child2 := unittest.BlockWithParentFixture(parentBlock.ToHeader())
133-
child3 := unittest.BlockWithParentFixture(parentBlock.ToHeader())
134-
135-
// Store child blocks
136-
childProposals := []*flow.Proposal{
137-
unittest.ProposalFromBlock(child1),
138-
unittest.ProposalFromBlock(child2),
139-
unittest.ProposalFromBlock(child3),
140-
}
130+
// Test case 2: Parent with 3 children
131+
var childProposals []*flow.Proposal
132+
for i := 0; i < 3; i++ {
133+
childProposal := unittest.ProposalFromBlock(unittest.BlockWithParentFixture(parentBlock.ToHeader()))
134+
childProposals = append(childProposals, childProposal)
141135

142-
for _, childProposal := range childProposals {
143136
err := unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
144137
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
145138
// Store the block
@@ -154,19 +147,16 @@ func TestHeadersByParentID(t *testing.T) {
154147
require.NoError(t, err)
155148
}
156149

157-
// Test case 2: Parent with multiple children
150+
// confirm correct behaviour for test case 2: we should retrieve the headers of the 3 children
158151
children, err = headers.ByParentID(parentBlock.ID())
159152
require.NoError(t, err)
160-
require.Len(t, children, 3)
161-
162-
// Verify we got the correct child headers
163-
childHeaders := []*flow.Header{child1.ToHeader(), child2.ToHeader(), child3.ToHeader()}
164-
require.ElementsMatch(t, childHeaders, children)
153+
require.ElementsMatch(t,
154+
children,
155+
[]*flow.Header{childProposals[0].Block.ToHeader(), childProposals[0].Block.ToHeader(), childProposals[0].Block.ToHeader()})
165156

166157
// Test case 3: Non-existent parent should return ErrNotFound
167158
nonExistentParent := unittest.IdentifierFixture()
168159
_, err = headers.ByParentID(nonExistentParent)
169-
require.Error(t, err)
170160
require.ErrorIs(t, err, storage.ErrNotFound)
171161
})
172162
}
@@ -186,23 +176,16 @@ func TestHeadersByParentIDChainStructure(t *testing.T) {
186176
headers := all.Headers
187177
blocks := all.Blocks
188178

189-
// Create a chain: parent -> child1 -> grandchild1, grandchild2
190-
parentProposal := unittest.ProposalFixture()
191-
parentBlock := parentProposal.Block
192-
193-
child1 := unittest.BlockWithParentFixture(parentBlock.ToHeader())
194-
grandchild1 := unittest.BlockWithParentFixture(child1.ToHeader())
195-
grandchild2 := unittest.BlockWithParentFixture(child1.ToHeader())
179+
// Create child structure: parent -> child -> grandchild1, grandchild2
180+
parent := unittest.BlockFixture()
181+
child := unittest.BlockWithParentFixture(parent.ToHeader())
182+
grandchild1 := unittest.BlockWithParentFixture(child.ToHeader())
183+
grandchild2 := unittest.BlockWithParentFixture(child.ToHeader())
196184

197185
// Store all blocks
198-
proposals := []*flow.Proposal{
199-
parentProposal,
200-
unittest.ProposalFromBlock(child1),
201-
unittest.ProposalFromBlock(grandchild1),
202-
unittest.ProposalFromBlock(grandchild2),
203-
}
186+
for _, b := range []*flow.Block{parent, child, grandchild1, grandchild2} {
187+
proposal := unittest.ProposalFromBlock(b)
204188

205-
for _, proposal := range proposals {
206189
err := unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
207190
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
208191
// Store the block
@@ -227,12 +210,11 @@ func TestHeadersByParentIDChainStructure(t *testing.T) {
227210
require.Equal(t, child1.ToHeader(), children[0])
228211

229212
// Test that child1 returns its direct children (grandchild1, grandchild2)
230-
children, err = headers.ByParentID(child1.ID())
213+
// Test that child returns its direct children (grandchild1, grandchild2)
214+
grandchildren, err := headers.ByParentID(child.ID())
231215
require.NoError(t, err)
232-
require.Len(t, children, 2)
233-
234-
grandchildHeaders := []*flow.Header{grandchild1.ToHeader(), grandchild2.ToHeader()}
235-
require.ElementsMatch(t, grandchildHeaders, children)
216+
require.ElementsMatch(t, grandchildren,
217+
[]*flow.Header{grandchild1.ToHeader(), grandchild2.ToHeader()})
236218

237219
// Test that grandchildren have no children
238220
children, err = headers.ByParentID(grandchild1.ID())

0 commit comments

Comments
 (0)