github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/committees/leader/consensus.go (about)

     1  package leader
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/flow-go/model/flow/filter"
     7  	"github.com/onflow/flow-go/state/protocol"
     8  	"github.com/onflow/flow-go/state/protocol/prg"
     9  )
    10  
    11  // SelectionForConsensus pre-computes and returns leaders for the consensus committee
    12  // in the given epoch. The consensus committee spans multiple epochs and the leader
    13  // selection returned here is only valid for the input epoch, so it is necessary to
    14  // call this for each upcoming epoch.
    15  func SelectionForConsensus(epoch protocol.Epoch) (*LeaderSelection, error) {
    16  
    17  	// pre-compute leader selection for the epoch
    18  	identities, err := epoch.InitialIdentities()
    19  	if err != nil {
    20  		return nil, fmt.Errorf("could not get epoch initial identities: %w", err)
    21  	}
    22  
    23  	// get the epoch source of randomness
    24  	randomSeed, err := epoch.RandomSource()
    25  	if err != nil {
    26  		return nil, fmt.Errorf("could not get epoch seed: %w", err)
    27  	}
    28  	// create random number generator from the seed and customizer
    29  	rng, err := prg.New(randomSeed, prg.ConsensusLeaderSelection, nil)
    30  	if err != nil {
    31  		return nil, fmt.Errorf("could not create rng: %w", err)
    32  	}
    33  	firstView, err := epoch.FirstView()
    34  	if err != nil {
    35  		return nil, fmt.Errorf("could not get epoch first view: %w", err)
    36  	}
    37  	finalView, err := epoch.FinalView()
    38  	if err != nil {
    39  		return nil, fmt.Errorf("could not get epoch final view: %w", err)
    40  	}
    41  
    42  	leaders, err := ComputeLeaderSelection(
    43  		firstView,
    44  		rng,
    45  		int(finalView-firstView+1), // add 1 because both first/final view are inclusive
    46  		identities.Filter(filter.IsConsensusCommitteeMember),
    47  	)
    48  	return leaders, err
    49  }