-
Notifications
You must be signed in to change notification settings - Fork 8
/
validator.go
209 lines (172 loc) · 4.9 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
package types
import (
"sort"
"strings"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/types/bech32"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var _ stakingtypes.ValidatorI = Validator{}
// DoNotModifyDesc used in flags to indicate that description field should not be updated
const DoNotModifyDesc = "[do-not-modify]"
// NewValidator creates a new MsgCreateValidator instance.
func NewValidator(
id tmbytes.HexBytes,
name string,
description string,
pubKey cryptotypes.PubKey,
cert string,
power int64,
operator sdk.AccAddress,
) Validator {
var pkStr string
var err error
if pubKey != nil {
pkStr, err = bech32.ConvertAndEncode(sdk.GetConfig().GetBech32ConsensusPubPrefix(), legacy.Cdc.MustMarshal(pubKey))
}
if err != nil {
panic(err)
}
return Validator{
Id: id.String(),
Name: name,
Pubkey: pkStr,
Certificate: cert,
Power: power,
Description: description,
Operator: operator.String(),
}
}
// IsJailed implement ValidatorI
func (v Validator) IsJailed() bool {
return v.Jailed
}
// GetMoniker implement ValidatorI
func (v Validator) GetMoniker() string {
return v.Name
}
// GetStatus implement ValidatorI
func (v Validator) GetStatus() stakingtypes.BondStatus {
if v.Jailed {
return stakingtypes.Unbonded
} else {
return stakingtypes.Bonded
}
}
// IsBonded implement ValidatorI
func (v Validator) IsBonded() bool {
return !v.Jailed
}
// IsUnbonded implement ValidatorI
func (v Validator) IsUnbonded() bool {
return v.Jailed
}
// IsUnbonding implement ValidatorI
func (v Validator) IsUnbonding() bool {
return false
}
// GetOperator implement ValidatorI
func (v Validator) GetOperator() sdk.ValAddress {
pubkey, err := v.ConsPubKey()
if err != nil {
panic(err)
}
return sdk.ValAddress(pubkey.Address())
}
// ConsPubKey returns the validator PubKey as a cryptotypes.PubKey.
func (v Validator) ConsPubKey() (pk cryptotypes.PubKey, err error) {
bz, err := sdk.GetFromBech32(v.Pubkey, sdk.GetConfig().GetBech32ConsensusPubPrefix())
return legacy.PubKeyFromBytes(bz)
}
// TmConsPublicKey casts Validator.ConsensusPubkey to tmprotocrypto.PubKey.
func (v Validator) TmConsPublicKey() (tmprotocrypto.PublicKey, error) {
pk, err := v.ConsPubKey()
if err != nil {
return tmprotocrypto.PublicKey{}, err
}
tmPk, err := cryptocodec.ToTmProtoPublicKey(pk)
if err != nil {
return tmprotocrypto.PublicKey{}, err
}
return tmPk, nil
}
// GetConsAddr implement ValidatorI
func (v Validator) GetConsAddr() (sdk.ConsAddress, error) {
pk, err := v.ConsPubKey()
if err != nil {
return sdk.ConsAddress{}, err
}
return sdk.ConsAddress(pk.Address()), nil
}
// GetTokens implement ValidatorI
func (v Validator) GetTokens() sdk.Int {
return sdk.TokensFromConsensusPower(v.Power, sdk.DefaultPowerReduction)
}
// GetBondedTokens implement ValidatorI
func (v Validator) GetBondedTokens() sdk.Int {
if v.Jailed {
return sdk.NewInt(0)
}
return sdk.TokensFromConsensusPower(v.Power, sdk.DefaultPowerReduction)
}
// GetConsensusPower implement ValidatorI
func (v Validator) GetConsensusPower(_ sdk.Int) int64 {
return v.Power
}
// GetCommission implement ValidatorI
func (v Validator) GetCommission() sdk.Dec {
return sdk.NewDec(0)
}
// GetMinSelfDelegation implement ValidatorI
func (v Validator) GetMinSelfDelegation() sdk.Int {
return sdk.NewInt(0)
}
// GetDelegatorShares implement ValidatorI
func (v Validator) GetDelegatorShares() sdk.Dec {
return sdk.NewDec(0)
}
// TokensFromShares implement ValidatorI
func (v Validator) TokensFromShares(dec sdk.Dec) sdk.Dec {
return sdk.NewDec(0)
}
// TokensFromSharesTruncated implement ValidatorI
func (v Validator) TokensFromSharesTruncated(dec sdk.Dec) sdk.Dec {
return sdk.NewDec(0)
}
// TokensFromSharesRoundUp implement ValidatorI
func (v Validator) TokensFromSharesRoundUp(dec sdk.Dec) sdk.Dec {
return sdk.NewDec(0)
}
// SharesFromTokens implement ValidatorI
func (v Validator) SharesFromTokens(amt sdk.Int) (sdk.Dec, error) {
return sdk.NewDec(0), nil
}
// SharesFromTokensTruncated implement ValidatorI
func (v Validator) SharesFromTokensTruncated(amt sdk.Int) (sdk.Dec, error) {
return sdk.NewDec(0), nil
}
// Validators is a collection of Validator
type Validators []Validator
// Sort Validators sorts validator array in ascending operator address order
func (v Validators) Sort() {
sort.Sort(v)
}
// Implements sort interface
func (v Validators) Len() int {
return len(v)
}
// Implements sort interface
func (v Validators) Less(i, j int) bool {
return strings.Compare(v[i].Name, v[j].Name) == -1
}
// Implements sort interface
func (v Validators) Swap(i, j int) {
it := v[i]
v[i] = v[j]
v[j] = it
}