/
validator.go
264 lines (212 loc) · 6.38 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package types
import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
"sigs.k8s.io/yaml"
)
// TODO implement ValidatorI as export interface with:
// - GetConsAddress
// - IsJailed
// - GetPower
// - ...
//
// additionally:
// - MinEqual
// - Equal
// TODO consider to implement collection of validators:
// - Validators []Validator
// ============= Validator ================
func NewValidator(owner sdk.ValAddress, pubKey cryptotypes.PubKey, description Description) (Validator, error) {
pkAny, err := codectypes.NewAnyWithValue(pubKey)
if err != nil {
return Validator{}, err
}
return Validator{
Owner: owner.String(),
Description: &description,
PubKey: pkAny,
Power: Power,
Jailed: false,
}, nil
}
func (v Validator) GetConsAddress() (sdk.ConsAddress, error) {
pk, ok := v.PubKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expecting cryptotypes.PubKey, got %T", pk)
}
return sdk.ConsAddress(pk.Address()), nil
}
func (v Validator) GetConsPubKey() (cryptotypes.PubKey, error) {
pk, ok := v.PubKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expecting cryptotypes.PubKey, got %T", pk)
}
return pk, nil
}
func (v Validator) GetPower() int32 { return v.Power }
func (v Validator) GetOwner() sdk.ValAddress {
if v.Owner == "" {
return nil
}
addr, err := sdk.ValAddressFromBech32(v.Owner)
if err != nil {
panic(err)
}
return addr
}
func (v Validator) GetMoniker() string { return v.Description.Moniker }
func (v Validator) IsJailed() bool { return v.Jailed }
// ABCI ValidatorUpdate message to add new validator to validator set.
func (v Validator) ABCIValidatorUpdate() abci.ValidatorUpdate {
tmProtoPk, err := v.TmConsPublicKey()
if err != nil {
panic(err)
}
return abci.ValidatorUpdate{
PubKey: tmProtoPk,
Power: int64(v.GetPower()),
}
}
// ABCI ValidatorUpdate message to remove validator from validator set.
func (v Validator) ABCIValidatorUpdateZero() abci.ValidatorUpdate {
tmProtoPk, err := v.TmConsPublicKey()
if err != nil {
panic(err)
}
return abci.ValidatorUpdate{
PubKey: tmProtoPk,
Power: int64(ZeroPower),
}
}
func (v Validator) String() string {
bz, err := codec.ProtoMarshalJSON(&v, nil)
if err != nil {
panic(err)
}
out, err := yaml.JSONToYAML(bz)
if err != nil {
panic(err)
}
return string(out)
}
// return the redelegation.
func MustMarshalValidator(cdc codec.BinaryCodec, validator *Validator) []byte {
return cdc.MustMarshal(validator)
}
// unmarshal a redelegation from a store value.
func MustUnmarshalValidator(cdc codec.BinaryCodec, value []byte) Validator {
validator, err := UnmarshalValidator(cdc, value)
if err != nil {
panic(err)
}
return validator
}
// unmarshal a redelegation from a store value.
func UnmarshalValidator(cdc codec.BinaryCodec, value []byte) (v Validator, err error) {
err = cdc.Unmarshal(value, &v)
return v, err
}
// TmConsPublicKey casts Validator.Pubkey to tmprotocrypto.PubKey.
func (v Validator) TmConsPublicKey() (tmprotocrypto.PublicKey, error) {
pk, err := v.GetConsPubKey()
if err != nil {
return tmprotocrypto.PublicKey{}, err
}
tmPk, err := cryptocodec.ToTmProtoPublicKey(pk)
if err != nil {
return tmprotocrypto.PublicKey{}, err
}
return tmPk, nil
}
// GetConsAddr extracts Consensus key address.
func (v Validator) GetConsAddr() (sdk.ConsAddress, error) {
pk, ok := v.PubKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expecting cryptotypes.PubKey, got %T", pk)
}
return sdk.ConsAddress(pk.Address()), nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces.
func (v Validator) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pk cryptotypes.PubKey
return unpacker.UnpackAny(v.PubKey, &pk)
}
// ============= Description of Validator ================
// NewDescription returns a new Description with the provided values.
func NewDescription(moniker string, identity, website, details string) Description {
return Description{
Moniker: moniker,
Identity: identity,
Website: website,
Details: details,
}
}
const (
MaxNameLength = 70
MaxIdentityLength = 3000
MaxWebsiteLength = 140
MaxDetailsLength = 280
)
// Ensure the length of a validator's description.
func (d Description) Validate() error {
if len(d.Moniker) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Invalid Description Moniker: it cannot be empty")
}
if len(d.Moniker) > MaxNameLength {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest,
"Invalid Description Moniker: received string of length %v, max is %v", len(d.Moniker), MaxNameLength,
)
}
if len(d.Identity) > MaxIdentityLength {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest,
"Invalid Description Identity: "+
"received string of length %v, max is %v", len(d.Identity), MaxIdentityLength,
)
}
if len(d.Website) > MaxWebsiteLength {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest,
"Invalid Description Website: "+
"received string of length %v, max is %v", len(d.Website), MaxWebsiteLength,
)
}
if len(d.Details) > MaxDetailsLength {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest,
"Invalid Description Details: received string of length %v, max is %v",
len(d.Details), MaxDetailsLength,
)
}
return nil
}
func (d Description) String() string {
out, _ := yaml.Marshal(d)
return string(out)
}
// ============= LastValidatorPower ================
// needed for taking validator set updates
func NewLastValidatorPower(owner sdk.ValAddress) LastValidatorPower {
return LastValidatorPower{
Owner: owner.String(),
Power: Power,
}
}
func (vp LastValidatorPower) GetOwner() sdk.ValAddress {
if vp.Owner == "" {
return nil
}
addr, err := sdk.ValAddressFromBech32(vp.Owner)
if err != nil {
panic(err)
}
return addr
}
func (vp LastValidatorPower) GetPower() int32 { return vp.Power }
func (vp LastValidatorPower) String() string {
out, _ := yaml.Marshal(vp)
return string(out)
}