Skip to content

Commit 6e87c4b

Browse files
authored
Don't Expect Sorted Nodes From Communication Interface (#182)
* sort nodes in initialize * comment
1 parent e4fe035 commit 6e87c4b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Storage interface {
5454

5555
type Communication interface {
5656

57-
// Nodes returns all nodes known to the application.
57+
// Nodes returns all nodes that participate in the epoch.
5858
Nodes() []NodeID
5959

6060
// Send sends a message to the given destination node

epoch.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ func (e *Epoch) init() error {
174174
e.cancelWaitForBlockNotarization = func() {}
175175
e.finishCtx, e.finishFn = context.WithCancel(context.Background())
176176
e.nodes = e.Comm.Nodes()
177+
sortNodes(e.nodes)
178+
177179
e.quorumSize = Quorum(len(e.nodes))
178180
e.rounds = make(map[uint64]*Round)
179181
e.maxRoundWindow = DefaultMaxRoundWindow
@@ -2685,6 +2687,13 @@ func (e *Epoch) isWithinMaxRoundWindow(round uint64) bool {
26852687
return e.round < round && round-e.round < e.maxRoundWindow
26862688
}
26872689

2690+
// sortNodes sorts the nodes in place by their byte representations.
2691+
func sortNodes(nodes []NodeID) {
2692+
slices.SortFunc(nodes, func(a, b NodeID) int {
2693+
return bytes.Compare(a[:], b[:])
2694+
})
2695+
}
2696+
26882697
func LeaderForRound(nodes []NodeID, r uint64) NodeID {
26892698
n := len(nodes)
26902699
return nodes[r%uint64(n)]

0 commit comments

Comments
 (0)