validator.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. package istanbul
  17. import (
  18. "bytes"
  19. "sort"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. type Validator interface {
  24. // Address returns address
  25. Address() common.Address
  26. // String representation of Validator
  27. String() string
  28. }
  29. // ----------------------------------------------------------------------------
  30. type Validators []Validator
  31. func (vs validatorSorter) Len() int {
  32. return len(vs.validators)
  33. }
  34. func (vs validatorSorter) Swap(i, j int) {
  35. vs.validators[i], vs.validators[j] = vs.validators[j], vs.validators[i]
  36. }
  37. func (vs validatorSorter) Less(i, j int) bool {
  38. return vs.by(vs.validators[i], vs.validators[j])
  39. }
  40. type validatorSorter struct {
  41. validators Validators
  42. by ValidatorSortByFunc
  43. }
  44. type ValidatorSortByFunc func(v1 Validator, v2 Validator) bool
  45. func ValidatorSortByString() ValidatorSortByFunc {
  46. return func(v1 Validator, v2 Validator) bool {
  47. return strings.Compare(v1.String(), v2.String()) < 0
  48. }
  49. }
  50. func ValidatorSortByByte() ValidatorSortByFunc {
  51. return func(v1 Validator, v2 Validator) bool {
  52. return bytes.Compare(v1.Address().Bytes(), v2.Address().Bytes()) < 0
  53. }
  54. }
  55. func (by ValidatorSortByFunc) Sort(validators []Validator) {
  56. v := &validatorSorter{
  57. validators: validators,
  58. by: by,
  59. }
  60. sort.Sort(v)
  61. }
  62. // ----------------------------------------------------------------------------
  63. type ValidatorSet interface {
  64. // Calculate the proposer
  65. CalcProposer(lastProposer common.Address, round uint64)
  66. // Return the validator size
  67. Size() int
  68. // Return the validator array
  69. List() []Validator
  70. // Get validator by index
  71. GetByIndex(i uint64) Validator
  72. // Get validator by given address
  73. GetByAddress(addr common.Address) (int, Validator)
  74. // Get current proposer
  75. GetProposer() Validator
  76. // Check whether the validator with given address is a proposer
  77. IsProposer(address common.Address) bool
  78. // Add validator
  79. AddValidator(address common.Address) bool
  80. // Remove validator
  81. RemoveValidator(address common.Address) bool
  82. // Copy validator set
  83. Copy() ValidatorSet
  84. // Get the maximum number of faulty nodes
  85. F() int
  86. // Get proposer policy
  87. Policy() ProposerPolicy
  88. // SortValidators sorts the validators based on the configured By function
  89. SortValidators()
  90. }
  91. // ----------------------------------------------------------------------------
  92. type ProposalSelector func(ValidatorSet, common.Address, uint64) Validator