-
Notifications
You must be signed in to change notification settings - Fork 198
/
validator.go
194 lines (151 loc) · 6.01 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package istanbul
import (
"bytes"
"errors"
"fmt"
"math/big"
blscrypto "github.com/celo-org/celo-blockchain/crypto/bls"
"github.com/celo-org/celo-blockchain/common"
)
var (
errInvalidValidatorSetDiffSize = errors.New("istanbul extra validator set data has different size")
)
type ValidatorData struct {
Address common.Address
BLSPublicKey blscrypto.SerializedPublicKey
}
type ValidatorDataWithBLSKeyCache struct {
Address common.Address
BLSPublicKey blscrypto.SerializedPublicKey
UncompressedBLSPublicKey []byte
}
type Validator interface {
fmt.Stringer
// Address returns address
Address() common.Address
// BLSPublicKey returns the BLS public key (compressed format)
BLSPublicKey() blscrypto.SerializedPublicKey
// BLSPublicKeyUncompressed returns the BLS public key (uncompressed format)
BLSPublicKeyUncompressed() []byte
// Serialize returns binary reprenstation of the Validator
// can be use used to instantiate a validator with DeserializeValidator()
Serialize() ([]byte, error)
// AsData returns Validator representation as ValidatorData
AsData() *ValidatorData
// AsData returns Validator representation as ValidatorData
AsDataWithBLSKeyCache() *ValidatorDataWithBLSKeyCache
// CacheUncompressedBLSKey stores the uncompressed BLS public key to cache
CacheUncompressedBLSKey()
// Copy validator
Copy() Validator
}
// MapValidatorsToAddresses maps a slice of validator to a slice of addresses
func MapValidatorsToAddresses(validators []Validator) []common.Address {
returnList := make([]common.Address, len(validators))
for i, val := range validators {
returnList[i] = val.Address()
}
return returnList
}
// MapValidatorsToPublicKeys maps a slice of validator to a slice of public keys
func MapValidatorsToPublicKeys(validators []Validator) []blscrypto.SerializedPublicKey {
returnList := make([]blscrypto.SerializedPublicKey, len(validators))
for i, val := range validators {
returnList[i] = val.BLSPublicKey()
}
return returnList
}
// ----------------------------------------------------------------------------
type ValidatorsDataByAddress []ValidatorData
func (a ValidatorsDataByAddress) Len() int { return len(a) }
func (a ValidatorsDataByAddress) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ValidatorsDataByAddress) Less(i, j int) bool {
return bytes.Compare(a[i].Address[:], a[j].Address[:]) < 0
}
// ----------------------------------------------------------------------------
type ValidatorSet interface {
fmt.Stringer
// Sets the randomness for use in the proposer policy.
// This is injected into the ValidatorSet when we call `getOrderedValidators`
SetRandomness(seed common.Hash)
// Sets the randomness for use in the proposer policy
GetRandomness() common.Hash
// Return the validator size
Size() int
// Get the maximum number of faulty nodes
F() int
// Get the minimum quorum size
MinQuorumSize() int
// List returns all the validators
List() []Validator
// Return the validator index
GetIndex(addr common.Address) int
// Get validator by index
GetByIndex(i uint64) Validator
// Get validator by given address
GetByAddress(addr common.Address) (int, Validator)
// CointainByAddress indicates if a validator with the given address is present
ContainsByAddress(add common.Address) bool
// Add validators
AddValidators(validators []ValidatorData) bool
// Remove validators
RemoveValidators(removedValidators *big.Int) bool
// Copy validator set
Copy() ValidatorSet
// CacheUncompressedBLSKey stores the uncompressed BLS public key to cache for each validator in the valset
CacheUncompressedBLSKey()
// HasBLSKeyCache tests that all uncompressed BLS public keys are in the cache, otherwise returns false
HasBLSKeyCache() bool
// Serialize returns binary reprentation of the ValidatorSet
// can be use used to instantiate a validator with DeserializeValidatorSet()
Serialize() ([]byte, error)
}
type ValidatorSetData struct {
Validators []ValidatorData
Randomness common.Hash
}
type ValidatorSetDataWithBLSKeyCache struct {
Validators []ValidatorDataWithBLSKeyCache
Randomness common.Hash
}
// ----------------------------------------------------------------------------
// ProposerSelector returns the block proposer for a round given the last proposer, round number, and randomness.
type ProposerSelector func(validatorSet ValidatorSet, lastBlockProposer common.Address, currentRound uint64) Validator
// ----------------------------------------------------------------------------
func CombineIstanbulExtraToValidatorData(addrs []common.Address, blsPublicKeys []blscrypto.SerializedPublicKey) ([]ValidatorData, error) {
if len(addrs) != len(blsPublicKeys) {
return nil, errInvalidValidatorSetDiffSize
}
validators := []ValidatorData{}
for i := range addrs {
validators = append(validators, ValidatorData{
Address: addrs[i],
BLSPublicKey: blsPublicKeys[i],
})
}
return validators, nil
}
func SeparateValidatorDataIntoIstanbulExtra(validators []ValidatorData) ([]common.Address, []blscrypto.SerializedPublicKey) {
addrs := []common.Address{}
pubKeys := []blscrypto.SerializedPublicKey{}
for i := range validators {
addrs = append(addrs, validators[i].Address)
pubKeys = append(pubKeys, validators[i].BLSPublicKey)
}
return addrs, pubKeys
}