github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/consensus/istanbul/validator.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package istanbul 18 19 import ( 20 "bytes" 21 "sort" 22 "strings" 23 24 "github.com/electroneum/electroneum-sc/common" 25 ) 26 27 type Validator interface { 28 // Address returns address 29 Address() common.Address 30 31 // String representation of Validator 32 String() string 33 } 34 35 // ---------------------------------------------------------------------------- 36 37 type Validators []Validator 38 39 func (vs validatorSorter) Len() int { 40 return len(vs.validators) 41 } 42 43 func (vs validatorSorter) Swap(i, j int) { 44 vs.validators[i], vs.validators[j] = vs.validators[j], vs.validators[i] 45 } 46 47 func (vs validatorSorter) Less(i, j int) bool { 48 return vs.by(vs.validators[i], vs.validators[j]) 49 } 50 51 type validatorSorter struct { 52 validators Validators 53 by ValidatorSortByFunc 54 } 55 56 type ValidatorSortByFunc func(v1 Validator, v2 Validator) bool 57 58 func ValidatorSortByString() ValidatorSortByFunc { 59 return func(v1 Validator, v2 Validator) bool { 60 return strings.Compare(v1.String(), v2.String()) < 0 61 } 62 } 63 64 func ValidatorSortByByte() ValidatorSortByFunc { 65 return func(v1 Validator, v2 Validator) bool { 66 return bytes.Compare(v1.Address().Bytes(), v2.Address().Bytes()) < 0 67 } 68 } 69 70 func (by ValidatorSortByFunc) Sort(validators []Validator) { 71 v := &validatorSorter{ 72 validators: validators, 73 by: by, 74 } 75 sort.Sort(v) 76 } 77 78 // ---------------------------------------------------------------------------- 79 80 type ValidatorSet interface { 81 // Calculate the proposer 82 CalcProposer(lastProposer common.Address, round uint64) 83 // Return the validator size 84 Size() int 85 // Return the validator array 86 List() []Validator 87 // Get validator by index 88 GetByIndex(i uint64) Validator 89 // Get validator by given address 90 GetByAddress(addr common.Address) (int, Validator) 91 // Get current proposer 92 GetProposer() Validator 93 // Check whether the validator with given address is a proposer 94 IsProposer(address common.Address) bool 95 // Add validator 96 AddValidator(address common.Address) bool 97 // Remove validator 98 RemoveValidator(address common.Address) bool 99 // Copy validator set 100 Copy() ValidatorSet 101 // Get the maximum number of faulty nodes 102 F() int 103 // Get proposer policy 104 Policy() ProposerPolicy 105 106 // SortValidators sorts the validators based on the configured By function 107 SortValidators() 108 } 109 110 // ---------------------------------------------------------------------------- 111 112 type ProposalSelector func(ValidatorSet, common.Address, uint64) Validator