-
Notifications
You must be signed in to change notification settings - Fork 175
/
consensus.go
41 lines (37 loc) · 1.34 KB
/
consensus.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
package leader
import (
"fmt"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/model/indices"
"github.com/onflow/flow-go/state/protocol"
)
// SelectionForConsensus pre-computes and returns leaders for the consensus committee
// in the given epoch. The consensus committee spans multiple epochs and the leader
// selection returned here is only valid for the input epoch, so it is necessary to
// call this for each upcoming epoch.
func SelectionForConsensus(epoch protocol.Epoch) (*LeaderSelection, error) {
// pre-compute leader selection for the epoch
identities, err := epoch.InitialIdentities()
if err != nil {
return nil, fmt.Errorf("could not get epoch initial identities: %w", err)
}
seed, err := epoch.Seed(indices.ProtocolConsensusLeaderSelection...)
if err != nil {
return nil, fmt.Errorf("could not get epoch seed: %w", err)
}
firstView, err := epoch.FirstView()
if err != nil {
return nil, fmt.Errorf("could not get epoch first view: %w", err)
}
finalView, err := epoch.FinalView()
if err != nil {
return nil, fmt.Errorf("could not get epoch final view: %w", err)
}
leaders, err := ComputeLeaderSelectionFromSeed(
firstView,
seed,
int(finalView-firstView+1), // add 1 because both first/final view are inclusive
identities.Filter(filter.IsVotingConsensusCommitteeMember),
)
return leaders, err
}