Skip to content

Commit 482ca11

Browse files
committed
bug: forest can get child count for nonextnt node
1 parent 542eea6 commit 482ca11

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

module/forest/leveled_forest.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,12 @@ func (f *LevelledForest) GetChildren(id flow.Identifier) VertexIterator {
141141
return newVertexIterator(nil) // VertexIterator gracefully handles nil slices
142142
}
143143

144-
// GetNumberOfChildren returns number of children of given vertex
144+
// GetNumberOfChildren returns the number of children of the given vertex that exist in the forest.
145145
func (f *LevelledForest) GetNumberOfChildren(id flow.Identifier) int {
146-
container := f.vertices[id] // if vertex does not exist, container is the default zero value for vertexContainer, which contains a nil-slice for its children
146+
container, ok := f.vertices[id]
147+
if !ok {
148+
return 0
149+
}
147150
num := 0
148151
for _, child := range container.children {
149152
if child.vertex != nil {

module/forest/leveled_forest_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ func TestLevelledForest_GetChildren(t *testing.T) {
206206
// testing children for Block that is contained in Tree but no children are known
207207
it = F.GetChildren(string2Identifier("D"))
208208
assert.False(t, it.HasNext())
209+
210+
// testing children for Block that does not exist (and is not a parent of any existent vertices) (should return 0)
211+
it = F.GetChildren(string2Identifier("NotAddedOrReferenced"))
212+
assert.False(t, it.HasNext())
209213
}
210214

211215
// TestLevelledForest_GetNumberOfChildren tests that children are returned properly
@@ -215,11 +219,14 @@ func TestLevelledForest_GetNumberOfChildren(t *testing.T) {
215219
// testing children for Block that is contained in Tree
216220
assert.Equal(t, 2, F.GetNumberOfChildren(string2Identifier("X")))
217221

218-
// testing children for referenced Block that is NOT contained in Tree
222+
// testing children for referenced Block that is NOT contained in Tree (but is referenced as parent of existent vertices)
219223
assert.Equal(t, 2, F.GetNumberOfChildren(string2Identifier("Genesis")))
220224

221225
// testing children for Block that is contained in Tree but no children are known
222226
assert.Equal(t, 0, F.GetNumberOfChildren(string2Identifier("D")))
227+
228+
// testing children for Block that does not exist (and is not a parent of any existent vertices) (should return 0)
229+
assert.Equal(t, 0, F.GetNumberOfChildren(string2Identifier("NotAddedOrReferenced")))
223230
}
224231

225232
// TestLevelledForest_GetVerticesAtLevel tests that Vertex blob is returned properly

0 commit comments

Comments
 (0)