Skip to content

Commit

Permalink
chore(trees): gofmt and fixed lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
meling committed Dec 22, 2024
1 parent 9840f5f commit 0131823
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
11 changes: 5 additions & 6 deletions trees/treeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ type Tree struct {

// CreateTree Creates the tree configuration, currently only fault free tree configuration is supported.
func CreateTree(configurationSize int, myID hotstuff.ID, bf int) TreeConfiguration {

if configurationSize <= 0 {
return nil
}
temp := configurationSize
temp = temp - 1 //root
temp = temp - 1 // root
height := 1
for i := 1; temp > 0; i++ {
temp = temp - int(math.Pow(float64(bf), float64(i)))
Expand All @@ -50,15 +49,15 @@ func CreateTree(configurationSize int, myID hotstuff.ID, bf int) TreeConfigurati
// InitializeWithPIDs uses the map to initialize the position of replicas.
func (t *Tree) InitializeWithPIDs(ids []hotstuff.ID) error {
if len(ids) != t.configurationSize {
return fmt.Errorf("Invalid number of replicas")
return fmt.Errorf("invalid number of replicas")
}
t.posToIDMapping = ids
t.idToPosMapping = make(map[hotstuff.ID]int)
for index, ID := range ids {
if _, ok := t.idToPosMapping[ID]; !ok {
t.idToPosMapping[ID] = index
} else {
return fmt.Errorf("Duplicate replica ID")
return fmt.Errorf("duplicate replica ID")
}
}
return nil
Expand All @@ -71,15 +70,15 @@ func (t *Tree) GetTreeHeight() int {
func (t *Tree) getPosition() (int, error) {
pos, ok := t.idToPosMapping[t.id]
if !ok {
return 0, fmt.Errorf("Replica not found")
return 0, fmt.Errorf("replica not found")
}
return pos, nil
}

func (t *Tree) getReplicaPosition(replicaId hotstuff.ID) (int, error) {
pos, ok := t.idToPosMapping[replicaId]
if !ok {
return 0, fmt.Errorf("Replica not found")
return 0, fmt.Errorf("replica not found")
}
return pos, nil
}
Expand Down
14 changes: 9 additions & 5 deletions trees/treeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ func TestTreeAPIWithInitializeWithPIDs(t *testing.T) {
{10, 2, 2, 4, []hotstuff.ID{4, 5}, []hotstuff.ID{4, 5, 8, 9, 10}, 1, false, 3, []hotstuff.ID{2, 3}},
{10, 3, 2, 4, []hotstuff.ID{6, 7}, []hotstuff.ID{6, 7}, 1, false, 3, []hotstuff.ID{2, 3}},
{10, 4, 2, 4, []hotstuff.ID{8, 9}, []hotstuff.ID{8, 9}, 2, false, 2, []hotstuff.ID{4, 5}},
{21, 1, 4, 3, []hotstuff.ID{2, 3, 4, 5}, []hotstuff.ID{2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, 1, true, 3, []hotstuff.ID{}},
{21, 1, 3, 4, []hotstuff.ID{2, 3, 4}, []hotstuff.ID{2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, 1, true, 4, []hotstuff.ID{}},
{21, 1, 4, 3, []hotstuff.ID{2, 3, 4, 5}, []hotstuff.ID{
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
}, 1, true, 3, []hotstuff.ID{}},
{21, 1, 3, 4, []hotstuff.ID{2, 3, 4}, []hotstuff.ID{
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
}, 1, true, 4, []hotstuff.ID{}},
{21, 2, 4, 3, []hotstuff.ID{6, 7, 8, 9}, []hotstuff.ID{6, 7, 8, 9}, 1, false, 2, []hotstuff.ID{2, 3, 4, 5}},
{21, 2, 3, 4, []hotstuff.ID{5, 6, 7}, []hotstuff.ID{5, 6, 7, 14, 15, 16, 17, 18, 19, 20, 21}, 1, false, 3, []hotstuff.ID{2, 3, 4}},
{21, 9, 3, 4, []hotstuff.ID{}, []hotstuff.ID{}, 3, false, 2, []hotstuff.ID{8, 9, 10}},
Expand All @@ -102,7 +104,9 @@ func TestTreeAPIWithInitializeWithPIDs(t *testing.T) {
for i := 0; i < test.configurationSize; i++ {
ids[i] = hotstuff.ID(i + 1)
}
tree.InitializeWithPIDs(ids)
if err := tree.InitializeWithPIDs(ids); err != nil {
t.Errorf("Expected nil, got %v", err)
}
if tree.GetTreeHeight() != test.height {
t.Errorf("Expected height %d, got %d", test.height, tree.GetTreeHeight())
}
Expand Down

0 comments on commit 0131823

Please sign in to comment.