github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/validator/selectors.go (about)

     1  // Copyright 2019 The Celo Authors
     2  // This file is part of the celo library.
     3  //
     4  // The celo library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The celo library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the celo library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package validator
    18  
    19  import (
    20  	"encoding/binary"
    21  	"fmt"
    22  	"math/rand"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    26  )
    27  
    28  func proposerIndex(valSet istanbul.ValidatorSet, proposer common.Address) uint64 {
    29  	if idx := valSet.GetIndex(proposer); idx >= 0 {
    30  		return uint64(idx)
    31  	}
    32  	return 0
    33  }
    34  
    35  // TODO: Pull ordering from smart contract and deprecate this function.
    36  func randFromHash(hash common.Hash) *rand.Rand {
    37  	// Reduce the hash to 64 bits to use as the seed.
    38  	var seed uint64
    39  	for i := 0; i < common.HashLength; i += 8 {
    40  		seed ^= binary.BigEndian.Uint64(hash[i : i+8])
    41  	}
    42  	return rand.New(rand.NewSource(int64(seed)))
    43  }
    44  
    45  // ShuffledRoundRobinProposer selects the next proposer with a round robin strategy according to a shuffled order.
    46  func ShuffledRoundRobinProposer(valSet istanbul.ValidatorSet, proposer common.Address, round uint64) istanbul.Validator {
    47  	if valSet.Size() == 0 {
    48  		return nil
    49  	}
    50  	seed := valSet.GetRandomness()
    51  
    52  	shuffle := randFromHash(seed).Perm(valSet.Size())
    53  	reverse := make([]int, len(shuffle))
    54  	for i, n := range shuffle {
    55  		reverse[n] = i
    56  	}
    57  	idx := round
    58  	if proposer != (common.Address{}) {
    59  		idx += uint64(reverse[proposerIndex(valSet, proposer)]) + 1
    60  	}
    61  	return valSet.List()[shuffle[idx%uint64(valSet.Size())]]
    62  }
    63  
    64  // RoundRobinProposer selects the next proposer with a round robin strategy according to storage order.
    65  func RoundRobinProposer(valSet istanbul.ValidatorSet, proposer common.Address, round uint64) istanbul.Validator {
    66  	if valSet.Size() == 0 {
    67  		return nil
    68  	}
    69  	idx := round
    70  	if proposer != (common.Address{}) {
    71  		idx += proposerIndex(valSet, proposer) + 1
    72  	}
    73  	return valSet.List()[idx%uint64(valSet.Size())]
    74  }
    75  
    76  // StickyProposer selects the next proposer with a sticky strategy, advancing on round change.
    77  func StickyProposer(valSet istanbul.ValidatorSet, proposer common.Address, round uint64) istanbul.Validator {
    78  	if valSet.Size() == 0 {
    79  		return nil
    80  	}
    81  	idx := round
    82  	if proposer != (common.Address{}) {
    83  		idx += proposerIndex(valSet, proposer)
    84  	}
    85  	return valSet.List()[idx%uint64(valSet.Size())]
    86  }
    87  
    88  // GetProposerSelector returns the ProposerSelector for the given Policy
    89  func GetProposerSelector(pp istanbul.ProposerPolicy) istanbul.ProposerSelector {
    90  	switch pp {
    91  	case istanbul.Sticky:
    92  		return StickyProposer
    93  	case istanbul.RoundRobin:
    94  		return RoundRobinProposer
    95  	case istanbul.ShuffledRoundRobin:
    96  		return ShuffledRoundRobinProposer
    97  	default:
    98  		// Programming error.
    99  		panic(fmt.Sprintf("unknown proposer selection policy: %v", pp))
   100  		return nil
   101  	}
   102  }