-
Notifications
You must be signed in to change notification settings - Fork 364
/
validator_vesting_account.go
347 lines (310 loc) · 14.5 KB
/
validator_vesting_account.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package types
import (
"errors"
"fmt"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/tendermint/tendermint/crypto"
)
// Assert ValidatorVestingAccount implements the vestexported.VestingAccount interface
// Assert ValidatorVestingAccount implements the authexported.GenesisAccount interface
var _ vestexported.VestingAccount = (*ValidatorVestingAccount)(nil)
var _ authexported.GenesisAccount = (*ValidatorVestingAccount)(nil)
// Register the ValidatorVestingAccount type on the auth module codec
func init() {
authtypes.RegisterAccountTypeCodec(&ValidatorVestingAccount{}, "cosmos-sdk/ValidatorVestingAccount")
}
// VestingProgress tracks the status of each vesting period
type VestingProgress struct {
PeriodComplete bool `json:"period_complete" yaml:"period_complete"`
VestingSuccessful bool `json:"vesting_successful" yaml:"vesting_successful"`
}
// CurrentPeriodProgress tracks the progress of the current vesting period
type CurrentPeriodProgress struct {
MissedBlocks int64 `json:"missed_blocks" yaml:"missed_blocks"`
TotalBlocks int64 `json:"total_blocks" yaml:"total_blocks"`
}
// GetSignedPercentage returns the percentage of blocks signed for the current vesting period
func (cpp CurrentPeriodProgress) GetSignedPercentage() sdk.Dec {
blocksSigned := cpp.TotalBlocks - cpp.MissedBlocks
// signed_percentage = blocksSigned/TotalBlocks * 100
signedPercentage := sdk.NewDec(blocksSigned).Quo(
sdk.NewDec(cpp.TotalBlocks)).Mul(
sdk.NewDec(100))
return signedPercentage
}
// SignedPercetageIsOverThreshold checks if the signed percentage exceeded the threshold
func (cpp CurrentPeriodProgress) SignedPercetageIsOverThreshold(threshold int64) bool {
signedPercentage := cpp.GetSignedPercentage()
return signedPercentage.GTE(sdk.NewDec(threshold))
}
// ValidatorVestingAccount implements the VestingAccount interface. It
// conditionally vests by unlocking coins during each specified period, provided
// that the validator address has validated at least **SigningThreshold** blocks during
// the previous vesting period. The signing threshold takes values 0 to 100 are represents the
// percentage of blocks that must be signed each period for the vesting to complete successfully.
// If the validator has not signed at least the threshold percentage of blocks during a period,
// the coins are returned to the return address, or burned if the return address is null.
type ValidatorVestingAccount struct {
*vestingtypes.PeriodicVestingAccount
ValidatorAddress sdk.ConsAddress `json:"validator_address" yaml:"validator_address"`
ReturnAddress sdk.AccAddress `json:"return_address" yaml:"return_address"`
SigningThreshold int64 `json:"signing_threshold" yaml:"signing_threshold"`
CurrentPeriodProgress CurrentPeriodProgress `json:"current_period_progress" yaml:"current_period_progress"`
VestingPeriodProgress []VestingProgress `json:"vesting_period_progress" yaml:"vesting_period_progress"`
DebtAfterFailedVesting sdk.Coins `json:"debt_after_failed_vesting" yaml:"debt_after_failed_vesting"`
}
// NewValidatorVestingAccountRaw creates a new ValidatorVestingAccount object from BaseVestingAccount
func NewValidatorVestingAccountRaw(bva *vestingtypes.BaseVestingAccount,
startTime int64, periods vestingtypes.Periods, validatorAddress sdk.ConsAddress, returnAddress sdk.AccAddress, signingThreshold int64) *ValidatorVestingAccount {
pva := &vestingtypes.PeriodicVestingAccount{
BaseVestingAccount: bva,
StartTime: startTime,
VestingPeriods: periods,
}
var vestingPeriodProgress []VestingProgress
for i := 0; i < len(periods); i++ {
vestingPeriodProgress = append(vestingPeriodProgress, VestingProgress{false, false})
}
return &ValidatorVestingAccount{
PeriodicVestingAccount: pva,
ValidatorAddress: validatorAddress,
ReturnAddress: returnAddress,
SigningThreshold: signingThreshold,
CurrentPeriodProgress: CurrentPeriodProgress{
MissedBlocks: 0,
TotalBlocks: 0,
},
VestingPeriodProgress: vestingPeriodProgress,
DebtAfterFailedVesting: sdk.NewCoins(),
}
}
// NewValidatorVestingAccount creates a ValidatorVestingAccount object from a BaseAccount
func NewValidatorVestingAccount(baseAcc *authtypes.BaseAccount, startTime int64, periods vestingtypes.Periods, validatorAddress sdk.ConsAddress, returnAddress sdk.AccAddress, signingThreshold int64) *ValidatorVestingAccount {
endTime := startTime
for _, p := range periods {
endTime += p.Length
}
baseVestingAcc := &vestingtypes.BaseVestingAccount{
BaseAccount: baseAcc,
OriginalVesting: baseAcc.Coins,
EndTime: endTime,
}
pva := &vestingtypes.PeriodicVestingAccount{
BaseVestingAccount: baseVestingAcc,
StartTime: startTime,
VestingPeriods: periods,
}
var vestingPeriodProgress []VestingProgress
for i := 0; i < len(periods); i++ {
vestingPeriodProgress = append(vestingPeriodProgress, VestingProgress{false, false})
}
return &ValidatorVestingAccount{
PeriodicVestingAccount: pva,
ValidatorAddress: validatorAddress,
ReturnAddress: returnAddress,
SigningThreshold: signingThreshold,
CurrentPeriodProgress: CurrentPeriodProgress{0, 0},
VestingPeriodProgress: vestingPeriodProgress,
DebtAfterFailedVesting: sdk.NewCoins(),
}
}
// GetVestedCoins returns the total number of vested coins.
func (vva ValidatorVestingAccount) GetVestedCoins(blockTime time.Time) sdk.Coins {
var vestedCoins sdk.Coins
if blockTime.Unix() <= vva.StartTime {
return vestedCoins
}
currentPeriodStartTime := vva.StartTime
numberPeriods := len(vva.VestingPeriods)
for i := 0; i < numberPeriods; i++ {
x := blockTime.Unix() - currentPeriodStartTime
if x >= vva.VestingPeriods[i].Length {
if vva.VestingPeriodProgress[i].PeriodComplete {
vestedCoins = vestedCoins.Add(vva.VestingPeriods[i].Amount...)
}
currentPeriodStartTime += vva.VestingPeriods[i].Length
} else {
break
}
}
return vestedCoins
}
// GetFailedVestedCoins returns the total number of coins for which the vesting period has passed but the vesting threshold was not met.
func (vva ValidatorVestingAccount) GetFailedVestedCoins() sdk.Coins {
var failedVestedCoins sdk.Coins
numberPeriods := len(vva.VestingPeriods)
for i := 0; i < numberPeriods; i++ {
if vva.VestingPeriodProgress[i].PeriodComplete {
if !vva.VestingPeriodProgress[i].VestingSuccessful {
failedVestedCoins = failedVestedCoins.Add(vva.VestingPeriods[i].Amount...)
}
} else {
break
}
}
return failedVestedCoins
}
// GetVestingCoins returns the total number of vesting coins. For validator vesting accounts, this excludes coins for which the vesting period has passed, but the vesting threshold was not met.
func (vva ValidatorVestingAccount) GetVestingCoins(blockTime time.Time) sdk.Coins {
return vva.OriginalVesting.Sub(vva.GetVestedCoins(blockTime))
}
// SpendableCoins returns the total number of spendable coins per denom for a
// periodic vesting account.
func (vva ValidatorVestingAccount) SpendableCoins(blockTime time.Time) sdk.Coins {
return vva.BaseVestingAccount.SpendableCoinsVestingAccount(vva.GetVestingCoins(blockTime))
}
// TrackDelegation tracks a desired delegation amount by setting the appropriate
// values for the amount of delegated vesting, delegated free, and reducing the
// overall amount of base coins.
func (vva *ValidatorVestingAccount) TrackDelegation(blockTime time.Time, amount sdk.Coins) {
vva.BaseVestingAccount.TrackDelegation(vva.GetVestingCoins(blockTime), amount)
}
// Validate checks for errors on the account fields
func (vva ValidatorVestingAccount) Validate() error {
if vva.ValidatorAddress.Empty() {
return errors.New("validator address cannot be empty")
}
if vva.SigningThreshold > 100 || vva.SigningThreshold < 0 {
return errors.New("signing threshold must be between 0 and 100")
}
if vva.ReturnAddress.Equals(vva.Address) {
return errors.New("return address cannot be the same as the account address")
}
if !vva.DebtAfterFailedVesting.IsValid() {
return fmt.Errorf("invalid debt after failed vesting coins: %s", vva.DebtAfterFailedVesting)
}
return vva.PeriodicVestingAccount.Validate()
}
type validatorVestingAccountYAML struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"`
DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"`
DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"`
EndTime int64 `json:"end_time" yaml:"end_time"`
// non-base vesting account fields
StartTime int64 `json:"start_time" yaml:"start_time"`
VestingPeriods vestingtypes.Periods `json:"vesting_periods" yaml:"vesting_periods"`
ValidatorAddress sdk.ConsAddress `json:"validator_address" yaml:"validator_address"`
ReturnAddress sdk.AccAddress `json:"return_address" yaml:"return_address"`
SigningThreshold int64 `json:"signing_threshold" yaml:"signing_threshold"`
CurrentPeriodProgress CurrentPeriodProgress `json:"current_period_progress" yaml:"current_period_progress"`
VestingPeriodProgress []VestingProgress `json:"vesting_period_progress" yaml:"vesting_period_progress"`
DebtAfterFailedVesting sdk.Coins `json:"debt_after_failed_vesting" yaml:"debt_after_failed_vesting"`
}
type validatorVestingAccountJSON struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
PubKey crypto.PubKey `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"`
DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"`
DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"`
EndTime int64 `json:"end_time" yaml:"end_time"`
// non-base vesting account fields
StartTime int64 `json:"start_time" yaml:"start_time"`
VestingPeriods vestingtypes.Periods `json:"vesting_periods" yaml:"vesting_periods"`
ValidatorAddress sdk.ConsAddress `json:"validator_address" yaml:"validator_address"`
ReturnAddress sdk.AccAddress `json:"return_address" yaml:"return_address"`
SigningThreshold int64 `json:"signing_threshold" yaml:"signing_threshold"`
CurrentPeriodProgress CurrentPeriodProgress `json:"current_period_progress" yaml:"current_period_progress"`
VestingPeriodProgress []VestingProgress `json:"vesting_period_progress" yaml:"vesting_period_progress"`
DebtAfterFailedVesting sdk.Coins `json:"debt_after_failed_vesting" yaml:"debt_after_failed_vesting"`
}
// MarshalJSON returns the JSON representation of a PeriodicVestingAccount.
func (vva ValidatorVestingAccount) MarshalJSON() ([]byte, error) {
alias := validatorVestingAccountJSON{
Address: vva.Address,
Coins: vva.Coins,
PubKey: vva.GetPubKey(),
AccountNumber: vva.AccountNumber,
Sequence: vva.Sequence,
OriginalVesting: vva.OriginalVesting,
DelegatedFree: vva.DelegatedFree,
DelegatedVesting: vva.DelegatedVesting,
EndTime: vva.EndTime,
StartTime: vva.StartTime,
VestingPeriods: vva.VestingPeriods,
ValidatorAddress: vva.ValidatorAddress,
ReturnAddress: vva.ReturnAddress,
SigningThreshold: vva.SigningThreshold,
CurrentPeriodProgress: vva.CurrentPeriodProgress,
VestingPeriodProgress: vva.VestingPeriodProgress,
DebtAfterFailedVesting: vva.DebtAfterFailedVesting,
}
return codec.Cdc.MarshalJSON(alias)
}
// UnmarshalJSON unmarshals raw JSON bytes into a ValidatorVestingAccount.
func (vva *ValidatorVestingAccount) UnmarshalJSON(bz []byte) error {
var alias validatorVestingAccountJSON
if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil {
return err
}
ba := authtypes.NewBaseAccount(alias.Address, alias.Coins, alias.PubKey, alias.AccountNumber, alias.Sequence)
bva := &vestingtypes.BaseVestingAccount{
BaseAccount: ba,
OriginalVesting: alias.OriginalVesting,
DelegatedFree: alias.DelegatedFree,
DelegatedVesting: alias.DelegatedVesting,
EndTime: alias.EndTime,
}
pva := vestingtypes.NewPeriodicVestingAccountRaw(bva, alias.StartTime, alias.VestingPeriods)
vva.PeriodicVestingAccount = pva
vva.ValidatorAddress = alias.ValidatorAddress
vva.ReturnAddress = alias.ReturnAddress
vva.SigningThreshold = alias.SigningThreshold
vva.CurrentPeriodProgress = alias.CurrentPeriodProgress
vva.VestingPeriodProgress = alias.VestingPeriodProgress
vva.DebtAfterFailedVesting = alias.DebtAfterFailedVesting
return nil
}
// MarshalYAML returns the YAML representation of an account.
func (vva ValidatorVestingAccount) MarshalYAML() (interface{}, error) {
alias := validatorVestingAccountYAML{
Address: vva.Address,
Coins: vva.Coins,
// no Pubkey
AccountNumber: vva.AccountNumber,
Sequence: vva.Sequence,
OriginalVesting: vva.OriginalVesting,
DelegatedFree: vva.DelegatedFree,
DelegatedVesting: vva.DelegatedVesting,
EndTime: vva.EndTime,
StartTime: vva.StartTime,
VestingPeriods: vva.VestingPeriods,
ValidatorAddress: vva.ValidatorAddress,
ReturnAddress: vva.ReturnAddress,
SigningThreshold: vva.SigningThreshold,
CurrentPeriodProgress: vva.CurrentPeriodProgress,
VestingPeriodProgress: vva.VestingPeriodProgress,
DebtAfterFailedVesting: vva.DebtAfterFailedVesting,
}
pk := vva.GetPubKey()
if pk != nil {
pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
return nil, err
}
alias.PubKey = pubkey
}
bz, err := yaml.Marshal(alias)
if err != nil {
return nil, err
}
return string(bz), err
}
func (vva ValidatorVestingAccount) String() string {
out, _ := vva.MarshalYAML()
return out.(string)
}