From 0131823e38b87d0cde82cac92b556bb930bc377b Mon Sep 17 00:00:00 2001 From: Hein Meling Date: Sun, 22 Dec 2024 11:42:41 +0100 Subject: [PATCH] chore(trees): gofmt and fixed lint issues --- trees/treeconfig.go | 11 +++++------ trees/treeconfig_test.go | 14 +++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/trees/treeconfig.go b/trees/treeconfig.go index 9ec94ab7..e1f613cf 100644 --- a/trees/treeconfig.go +++ b/trees/treeconfig.go @@ -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))) @@ -50,7 +49,7 @@ 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) @@ -58,7 +57,7 @@ func (t *Tree) InitializeWithPIDs(ids []hotstuff.ID) error { 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 @@ -71,7 +70,7 @@ 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 } @@ -79,7 +78,7 @@ func (t *Tree) getPosition() (int, error) { 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 } diff --git a/trees/treeconfig_test.go b/trees/treeconfig_test.go index b4fca5db..1463985b 100644 --- a/trees/treeconfig_test.go +++ b/trees/treeconfig_test.go @@ -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}}, @@ -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()) }