github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/consensus/istanbul/config.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 "sync" 21 22 "github.com/naoina/toml" 23 ) 24 25 type ProposerPolicyId uint64 26 27 const ( 28 RoundRobin ProposerPolicyId = iota 29 Sticky 30 ) 31 32 // ProposerPolicy represents the Validator Proposer Policy 33 type ProposerPolicy struct { 34 Id ProposerPolicyId // Could be RoundRobin or Sticky 35 By ValidatorSortByFunc // func that defines how the ValidatorSet should be sorted 36 registry []ValidatorSet // Holds the ValidatorSet for a given block height 37 registryMU *sync.Mutex // Mutex to lock access to changes to Registry 38 } 39 40 // NewRoundRobinProposerPolicy returns a RoundRobin ProposerPolicy with ValidatorSortByString as default sort function 41 func NewRoundRobinProposerPolicy() *ProposerPolicy { 42 return NewProposerPolicy(RoundRobin) 43 } 44 45 // NewStickyProposerPolicy return a Sticky ProposerPolicy with ValidatorSortByString as default sort function 46 func NewStickyProposerPolicy() *ProposerPolicy { 47 return NewProposerPolicy(Sticky) 48 } 49 50 func NewProposerPolicy(id ProposerPolicyId) *ProposerPolicy { 51 return NewProposerPolicyByIdAndSortFunc(id, ValidatorSortByString()) 52 } 53 54 func NewProposerPolicyByIdAndSortFunc(id ProposerPolicyId, by ValidatorSortByFunc) *ProposerPolicy { 55 return &ProposerPolicy{Id: id, By: by, registryMU: new(sync.Mutex)} 56 } 57 58 type proposerPolicyToml struct { 59 Id ProposerPolicyId 60 } 61 62 func (p *ProposerPolicy) MarshalTOML() (interface{}, error) { 63 if p == nil { 64 return nil, nil 65 } 66 pp := &proposerPolicyToml{Id: p.Id} 67 data, err := toml.Marshal(pp) 68 if err != nil { 69 return nil, err 70 } 71 return string(data), nil 72 } 73 74 func (p *ProposerPolicy) UnmarshalTOML(decode func(interface{}) error) error { 75 var innerToml string 76 err := decode(&innerToml) 77 if err != nil { 78 return err 79 } 80 var pp proposerPolicyToml 81 err = toml.Unmarshal([]byte(innerToml), &pp) 82 if err != nil { 83 return err 84 } 85 p.Id = pp.Id 86 p.By = ValidatorSortByString() 87 return nil 88 } 89 90 // Use sets the ValidatorSortByFunc for the given ProposerPolicy and sorts the validatorSets according to it 91 func (p *ProposerPolicy) Use(v ValidatorSortByFunc) { 92 p.By = v 93 94 for _, validatorSet := range p.registry { 95 validatorSet.SortValidators() 96 } 97 } 98 99 // RegisterValidatorSet stores the given ValidatorSet in the policy registry 100 func (p *ProposerPolicy) RegisterValidatorSet(valSet ValidatorSet) { 101 p.registryMU.Lock() 102 defer p.registryMU.Unlock() 103 104 if len(p.registry) == 0 { 105 p.registry = []ValidatorSet{valSet} 106 } else { 107 p.registry = append(p.registry, valSet) 108 } 109 } 110 111 // ClearRegistry removes any ValidatorSet from the ProposerPolicy registry 112 func (p *ProposerPolicy) ClearRegistry() { 113 p.registryMU.Lock() 114 defer p.registryMU.Unlock() 115 116 p.registry = nil 117 } 118 119 type Config struct { 120 RequestTimeout uint64 `toml:",omitempty"` // The timeout for each Istanbul round in milliseconds. 121 BlockPeriod uint64 `toml:",omitempty"` // Default minimum difference between two consecutive block's timestamps in second 122 ProposerPolicy *ProposerPolicy `toml:",omitempty"` // The policy for proposer selection 123 Epoch uint64 `toml:",omitempty"` // The number of blocks after which to checkpoint and reset the pending votes 124 AllowedFutureBlockTime uint64 `toml:",omitempty"` // Max time (in seconds) from current time allowed for blocks, before they're considered future blocks 125 } 126 127 var DefaultConfig = &Config{ 128 RequestTimeout: 10000, 129 BlockPeriod: 5, 130 ProposerPolicy: NewRoundRobinProposerPolicy(), 131 Epoch: 30000, 132 AllowedFutureBlockTime: 0, 133 }