-
Notifications
You must be signed in to change notification settings - Fork 175
/
cluster.go
46 lines (39 loc) · 1.42 KB
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package leader
import (
"fmt"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/state/protocol/prg"
)
// SelectionForCluster pre-computes and returns leaders for the given cluster
// committee in the given epoch.
func SelectionForCluster(cluster protocol.Cluster, epoch protocol.Epoch) (*LeaderSelection, error) {
// sanity check to ensure the cluster and epoch match
counter, err := epoch.Counter()
if err != nil {
return nil, fmt.Errorf("could not get epoch counter: %w", err)
}
if counter != cluster.EpochCounter() {
return nil, fmt.Errorf("inconsistent counter between epoch (%d) and cluster (%d)", counter, cluster.EpochCounter())
}
identities := cluster.Members()
// get the random source of the current epoch
randomSeed, err := epoch.RandomSource()
if err != nil {
return nil, fmt.Errorf("could not get leader selection seed for cluster (index: %v) at epoch: %v: %w", cluster.Index(), counter, err)
}
// create random number generator from the seed and customizer
rng, err := prg.New(randomSeed, prg.CollectorClusterLeaderSelection(cluster.Index()), nil)
if err != nil {
return nil, fmt.Errorf("could not create rng: %w", err)
}
firstView := cluster.RootBlock().Header.View
// TODO what is a good value here?
finalView := firstView + EstimatedSixMonthOfViews
leaders, err := ComputeLeaderSelection(
firstView,
rng,
int(finalView-firstView+1),
identities,
)
return leaders, err
}