github.com/klaytn/klaytn@v1.12.1/consensus/istanbul/validator.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from quorum/consensus/istanbul/validator.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package istanbul
    22  
    23  import (
    24  	"strings"
    25  
    26  	"github.com/klaytn/klaytn/params"
    27  
    28  	"github.com/klaytn/klaytn/common"
    29  )
    30  
    31  type Validator interface {
    32  	// Address returns address
    33  	Address() common.Address
    34  
    35  	// String representation of Validator
    36  	String() string
    37  
    38  	RewardAddress() common.Address
    39  	VotingPower() uint64
    40  	Weight() uint64
    41  }
    42  
    43  // ----------------------------------------------------------------------------
    44  
    45  type Validators []Validator
    46  
    47  func (slice Validators) Len() int {
    48  	return len(slice)
    49  }
    50  
    51  func (slice Validators) Less(i, j int) bool {
    52  	return strings.Compare(slice[i].String(), slice[j].String()) < 0
    53  }
    54  
    55  func (slice Validators) Swap(i, j int) {
    56  	slice[i], slice[j] = slice[j], slice[i]
    57  }
    58  
    59  func (slice Validators) AddressStringList() []string {
    60  	var stringAddrs []string
    61  	for _, val := range slice {
    62  		stringAddrs = append(stringAddrs, val.Address().String())
    63  	}
    64  	return stringAddrs
    65  }
    66  
    67  // ----------------------------------------------------------------------------
    68  
    69  type ValidatorSet interface {
    70  	// Calculate the proposer
    71  	CalcProposer(lastProposer common.Address, round uint64)
    72  	// Return the validator size
    73  	Size() uint64
    74  	// Return the sub validator group size
    75  	SubGroupSize() uint64
    76  	// Set the sub validator group size
    77  	SetSubGroupSize(size uint64)
    78  	// Return the validator array
    79  	List() []Validator
    80  	// Return the demoted validator array
    81  	DemotedList() []Validator
    82  	// SubList composes a committee after setting a proposer with a default value.
    83  	SubList(prevHash common.Hash, view *View) []Validator
    84  	// Return whether the given address is one of sub-list
    85  	CheckInSubList(prevHash common.Hash, view *View, addr common.Address) bool
    86  	// SubListWithProposer composes a committee with given parameters.
    87  	SubListWithProposer(prevHash common.Hash, proposer common.Address, view *View) []Validator
    88  	// Get validator by index
    89  	GetByIndex(i uint64) Validator
    90  	// Get validator by given address
    91  	GetByAddress(addr common.Address) (int, Validator)
    92  	// Get demoted validator by given address
    93  	GetDemotedByAddress(addr common.Address) (int, Validator)
    94  	// Get current proposer
    95  	GetProposer() Validator
    96  	// Check whether the validator with given address is a proposer
    97  	IsProposer(address common.Address) bool
    98  	// Add validator
    99  	AddValidator(address common.Address) bool
   100  	// Remove validator
   101  	RemoveValidator(address common.Address) bool
   102  	// Copy validator set
   103  	Copy() ValidatorSet
   104  	// Get the maximum number of faulty nodes
   105  	F() int
   106  	// Get proposer policy
   107  	Policy() ProposerPolicy
   108  
   109  	IsSubSet() bool
   110  
   111  	// Refreshes a list of candidate proposers with given hash and blockNum
   112  	Refresh(hash common.Hash, blockNum uint64, config *params.ChainConfig, isSingle bool, governingNode common.Address, minStaking uint64) error
   113  
   114  	SetBlockNum(blockNum uint64)
   115  	SetMixHash(mixHash []byte)
   116  
   117  	Proposers() []Validator // TODO-Klaytn-Issue1166 For debugging
   118  
   119  	TotalVotingPower() uint64
   120  
   121  	Selector(valSet ValidatorSet, lastProposer common.Address, round uint64) Validator
   122  }
   123  
   124  // ----------------------------------------------------------------------------
   125  
   126  type ProposalSelector func(ValidatorSet, common.Address, uint64) Validator