github.com/koko1123/flow-go-1@v0.29.6/state/protocol/seed/customizers.go (about)

     1  package seed
     2  
     3  import "encoding/binary"
     4  
     5  // list of customizers used for different sub-protocol PRNGs.
     6  // These customizers help instanciate different PRNGs from the
     7  // same source of randomness.
     8  
     9  var (
    10  	// ProtocolConsensusLeaderSelection is the customizer for consensus leader selection
    11  	ProtocolConsensusLeaderSelection = customizerFromIndices([]uint16{0, 1, 1})
    12  	// ProtocolVerificationChunkAssignment is the customizer for verification nodes determines chunk assignment
    13  	ProtocolVerificationChunkAssignment = customizerFromIndices([]uint16{0, 2, 0})
    14  	// collectorClusterLeaderSelectionPrefix is the prefix of the customizer for the leader selection of collector clusters
    15  	collectorClusterLeaderSelectionPrefix = []uint16{0, 0}
    16  	// executionChunkPrefix is the prefix of the customizer for executing chunks
    17  	executionChunkPrefix = []uint16{1}
    18  )
    19  
    20  // ProtocolCollectorClusterLeaderSelection returns the indices for the leader selection for the i-th collector cluster
    21  func ProtocolCollectorClusterLeaderSelection(clusterIndex uint) []byte {
    22  	indices := append(collectorClusterLeaderSelectionPrefix, uint16(clusterIndex))
    23  	return customizerFromIndices(indices)
    24  }
    25  
    26  // ExecutionChunk returns the indices for i-th chunk
    27  func ExecutionChunk(chunkIndex uint16) []byte {
    28  	indices := append(executionChunkPrefix, chunkIndex)
    29  	return customizerFromIndices(indices)
    30  }
    31  
    32  // customizerFromIndices maps the input indices into a slice of bytes.
    33  // The implementation insures there are no collisions of mapping of different indices.
    34  //
    35  // The output is built as a concatenation of indices, each index encoded over 2 bytes.
    36  // (the implementation could be updated to map the indices differently depending on the
    37  // constraints over the output length)
    38  func customizerFromIndices(indices []uint16) []byte {
    39  	customizerLen := 2 * len(indices)
    40  	customizer := make([]byte, customizerLen)
    41  	// concatenate the indices
    42  	for i, index := range indices {
    43  		binary.LittleEndian.PutUint16(customizer[2*i:2*i+2], index)
    44  	}
    45  	return customizer
    46  }