Skip to content

Commit

Permalink
refactor(tree): simplified SubTree implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
meling committed Dec 24, 2024
1 parent ad4f140 commit 8001f10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
15 changes: 6 additions & 9 deletions internal/tree/treeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,18 @@ func (t *Tree) PeersOf(nodeID hotstuff.ID) []hotstuff.ID {
return t.ChildrenOf(parent)
}

// SubTree returns all the nodes of its subtree.
// SubTree returns all subtree nodes of this tree's replica.
func (t *Tree) SubTree() []hotstuff.ID {
nodeID := t.id
subTreeNodes := make([]hotstuff.ID, 0)
children := t.ChildrenOfNode(nodeID)
queue := make([]hotstuff.ID, 0)
queue = append(queue, children...)
subTreeNodes = append(subTreeNodes, children...)
children := t.ChildrenOf(t.id)
if len(children) == 0 {
return subTreeNodes
return nil
}
subTreeNodes := slices.Clone(children)
queue := slices.Clone(children)
for len(queue) > 0 {
child := queue[0]
queue = queue[1:]
children := t.ChildrenOfNode(child)
children := t.ChildrenOf(child)
subTreeNodes = append(subTreeNodes, children...)
queue = append(queue, children...)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/tree/treeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func TestTreeAPIWithInitializeWithPIDs(t *testing.T) {
if tree.TreeHeight() != test.height {
t.Errorf("Expected height %d, got %d", test.height, tree.TreeHeight())
}
gotChildren := tree.ChildrenOf()
gotChildren := tree.NodeChildren()
sort.Slice(gotChildren, func(i, j int) bool { return gotChildren[i] < gotChildren[j] })
if len(gotChildren) != len(test.children) || !slices.Equal(gotChildren, test.children) {
t.Errorf("Expected %v, got %v", test.children, tree.ChildrenOf())
t.Errorf("Expected %v, got %v", test.children, tree.NodeChildren())
}
subTree := tree.SubTree()
sort.Slice(subTree, func(i, j int) bool { return subTree[i] < subTree[j] })
Expand All @@ -112,8 +112,8 @@ func TestTreeAPIWithInitializeWithPIDs(t *testing.T) {
if tree.IsRoot(test.id) != test.isRoot {
t.Errorf("Expected %t, got %t", test.isRoot, tree.IsRoot(test.id))
}
if tree.GetHeight() != test.replicaHeight {
t.Errorf("Expected %d, got %d", test.replicaHeight, tree.GetHeight())
if tree.NodeHeight() != test.replicaHeight {
t.Errorf("Expected %d, got %d", test.replicaHeight, tree.NodeHeight())
}
gotPeers := tree.PeersOf(test.id)
sort.Slice(gotPeers, func(i, j int) bool { return gotPeers[i] < gotPeers[j] })
Expand All @@ -130,7 +130,7 @@ func benchmarkGetChildren(size int, bf int, b *testing.B) {
}
tree := CreateTree(1, bf, ids)
for i := 0; i < b.N; i++ {
tree.ChildrenOf()
tree.NodeChildren()
}
}

Expand Down

0 comments on commit 8001f10

Please sign in to comment.