-
Notifications
You must be signed in to change notification settings - Fork 20
/
validator.go
594 lines (503 loc) · 17.5 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package txs
import (
"fmt"
"github.com/QOSGroup/qbase/baseabci"
"github.com/QOSGroup/qbase/context"
"github.com/QOSGroup/qbase/txs"
btypes "github.com/QOSGroup/qbase/types"
"github.com/QOSGroup/qos/module/bank"
"github.com/QOSGroup/qos/module/stake/mapper"
"github.com/QOSGroup/qos/module/stake/types"
qtypes "github.com/QOSGroup/qos/types"
"github.com/tendermint/tendermint/crypto"
)
var (
MaxNameLen = 300
MaxLinkLen = 255
MaxDescriptionLen = 1000
GasForCreateValidator = int64(1.8*qtypes.UnitQOS) * qtypes.UnitQOSGas // 1.8 QOS
GasForModifyValidator = int64(0.18*qtypes.UnitQOS) * qtypes.UnitQOSGas // 0.18 QOS
GasForRevokeValidator = int64(18*qtypes.UnitQOS) * qtypes.UnitQOSGas // 18 QOS
)
// 创建验证节点Tx
type TxCreateValidator struct {
Owner btypes.AccAddress `json:"owner"` // 操作者, self delegator
ConsPubKey crypto.PubKey `json:"cons_pub_key"` // validator公钥
BondTokens btypes.BigInt `json:"bond_tokens"` // 绑定Token数量
IsCompound bool `json:"is_compound"` // 周期收益是否复投
Description types.Description `json:"description"` // 描述信息
Commission types.CommissionRates `json:"commission"` // 佣金比例
Delegations []types.DelegationInfo `json:"delegations"` // 初始委托,仅在iniChainer中执行有效
}
var _ txs.ITx = (*TxCreateValidator)(nil)
func NewCreateValidatorTx(operator btypes.AccAddress, bech32ConPubKey crypto.PubKey, bondTokens btypes.BigInt, isCompound bool, description types.Description, commission types.CommissionRates, delegations []types.DelegationInfo) *TxCreateValidator {
return &TxCreateValidator{
Owner: operator,
ConsPubKey: bech32ConPubKey,
BondTokens: bondTokens,
IsCompound: isCompound,
Description: description,
Commission: commission,
Delegations: delegations,
}
}
// 基础数据校验
func (tx *TxCreateValidator) ValidateInputs() (err error) {
if len(tx.Description.Moniker) == 0 {
return types.ErrInvalidInput("moniker is empty")
}
if len(tx.Description.Moniker) > MaxNameLen {
return types.ErrInvalidInput("moniker is too long")
}
if tx.ConsPubKey == nil {
return types.ErrInvalidInput("cons_pub_key is empty")
}
if len(tx.Description.Logo) > MaxLinkLen {
return types.ErrInvalidInput("logo is too long")
}
if len(tx.Description.Website) > MaxLinkLen {
return types.ErrInvalidInput("website is too long")
}
if len(tx.Description.Details) > MaxDescriptionLen {
return types.ErrInvalidInput("details is too long")
}
if len(tx.Owner) == 0 {
return types.ErrInvalidInput("operator is empty")
}
if !tx.BondTokens.GT(btypes.ZeroInt()) {
return types.ErrInvalidInput("bond_tokens must be positive")
}
// 验证创世文件中包含委托信息的验证节点创建交易
if len(tx.Delegations) != 0 {
totalDelegation := btypes.ZeroInt()
for _, delegation := range tx.Delegations {
totalDelegation = totalDelegation.Add(delegation.Amount)
}
if !totalDelegation.Equal(tx.BondTokens) {
return types.ErrInvalidInput("validator bondTokens must equal sum(amount) of delegations")
}
}
// 佣金参数校验
err = tx.Commission.Validate()
if err != nil {
return
}
return nil
}
// 数据校验
func (tx *TxCreateValidator) ValidateData(ctx context.Context) (err error) {
// 基础数据校验
err = tx.ValidateInputs()
if err != nil {
return err
}
// 验证创世文件中包含委托信息的验证节点创建交易
if ctx.BlockHeader().Height == 0 && len(tx.Delegations) != 0 {
for _, delegation := range tx.Delegations {
err = validateQOSAccount(ctx, delegation.DelegatorAddr, delegation.Amount)
if nil != err {
return err
}
}
} else {
err = validateQOSAccount(ctx, tx.Owner, tx.BondTokens)
if nil != err {
return err
}
}
mapper := mapper.GetMapper(ctx)
valAddr := btypes.ValAddress(tx.Owner)
// 验证节点已存在校验
if mapper.Exists(valAddr) {
return types.ErrValidatorExists()
}
consAddr := btypes.ConsAddress(tx.ConsPubKey.Address())
// 共识地址已存在校验
if mapper.ExistsWithConsensusAddr(consAddr) {
return types.ErrConsensusHasValidator()
}
return nil
}
// 交易执行
func (tx *TxCreateValidator) Exec(ctx context.Context) (result btypes.Result, crossTxQcp *txs.TxQcp) {
result = btypes.Result{Code: btypes.CodeOK}
valAddr := btypes.ValAddress(tx.Owner)
validator := types.Validator{
OperatorAddress: valAddr,
Owner: tx.Owner,
ConsPubKey: tx.ConsPubKey,
BondTokens: tx.BondTokens,
Description: tx.Description,
Status: types.Active,
MinPeriod: int64(0),
BondHeight: ctx.BlockHeight(),
Commission: types.NewCommissionWithTime(tx.Commission.Rate, tx.Commission.MaxRate, tx.Commission.MaxChangeRate, ctx.BlockHeader().Time.UTC()),
}
delegatorAddr := validator.Owner
// 创建validator
sm := mapper.GetMapper(ctx)
sm.CreateValidator(validator)
sm.AfterValidatorCreated(ctx, valAddr)
// 初始化delegations
if ctx.BlockHeader().Height == 0 && len(tx.Delegations) != 0 {
for _, delegation := range tx.Delegations {
sm.Delegate(ctx, delegation, false)
}
} else {
delegationInfo := types.NewDelegationInfo(delegatorAddr, valAddr, tx.BondTokens, tx.IsCompound)
sm.Delegate(ctx, delegationInfo, false)
}
// 发送事件
result.Events = btypes.Events{
btypes.NewEvent(
types.EventTypeCreateValidator,
btypes.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
btypes.NewAttribute(types.AttributeKeyOwner, tx.Owner.String()),
btypes.NewAttribute(types.AttributeKeyDelegator, tx.Owner.String()),
),
btypes.NewEvent(
btypes.EventTypeMessage,
btypes.NewAttribute(btypes.AttributeKeyModule, types.AttributeKeyModule),
btypes.NewAttribute(btypes.AttributeKeyAction, types.EventTypeCreateValidator),
btypes.NewAttribute(btypes.AttributeKeyGasPayer, tx.GetGasPayer().String()),
),
}
return
}
// 签名账户,operator
func (tx *TxCreateValidator) GetSigner() []btypes.AccAddress {
return []btypes.AccAddress{tx.Owner}
}
// Tx Gas, 1.8QOS
func (tx *TxCreateValidator) CalcGas() btypes.BigInt {
return btypes.NewInt(GasForCreateValidator)
}
// Gas payer, operator
func (tx *TxCreateValidator) GetGasPayer() btypes.AccAddress {
return tx.Owner
}
// 签名字节
func (tx *TxCreateValidator) GetSignData() (ret []byte) {
return Cdc.MustMarshalJSON(*tx)
}
// 修改验证节点基础信息Tx
type TxModifyValidator struct {
Owner btypes.AccAddress `json:"owner"` // 验证人Owner地址
ValidatorAddr btypes.ValAddress `json:"validator_addr"` // 验证人地址
Description types.Description `json:"description"` // 描述信息
CommissionRate *qtypes.Dec `json:"commission_rate"` // 佣金比例
}
var _ txs.ITx = (*TxModifyValidator)(nil)
func NewModifyValidatorTx(owner btypes.AccAddress, validatorAddr btypes.ValAddress, description types.Description, commissionRate *qtypes.Dec) *TxModifyValidator {
return &TxModifyValidator{
Owner: owner,
ValidatorAddr: validatorAddr,
Description: description,
CommissionRate: commissionRate,
}
}
// 基础数据校验
func (tx *TxModifyValidator) ValidateInputs() (err error) {
if len(tx.Owner) == 0 {
return types.ErrInvalidInput("owner is empty")
}
if len(tx.ValidatorAddr) == 0 {
return types.ErrInvalidInput("validator address is empty")
}
if len(tx.Description.Moniker) > MaxNameLen {
return types.ErrInvalidInput("moniker is too long")
}
if len(tx.Description.Logo) > MaxLinkLen {
return types.ErrInvalidInput("logo url is too long")
}
if len(tx.Description.Website) > MaxLinkLen {
return types.ErrInvalidInput("website is too long")
}
if len(tx.Description.Details) > MaxDescriptionLen {
return types.ErrInvalidInput("details is too long")
}
return
}
// 数据校验
func (tx *TxModifyValidator) ValidateData(ctx context.Context) (err error) {
// 校验基础数据
err = tx.ValidateInputs()
if err != nil {
return err
}
// 校验验证节点信息
validator, err := validateValidator(ctx, tx.ValidatorAddr, false, types.Active, tx.Owner, true)
if err != nil {
return err
}
// valid commission rate
if tx.CommissionRate != nil {
err = validator.Commission.ValidateNewRate(*tx.CommissionRate, ctx.BlockHeader().Time.UTC())
}
return
}
// 交易执行
func (tx *TxModifyValidator) Exec(ctx context.Context) (result btypes.Result, crossTxQcp *txs.TxQcp) {
result = btypes.Result{Code: btypes.CodeOK}
// 获取验证节点信息
validatorMapper := mapper.GetMapper(ctx)
validator, exists := validatorMapper.GetValidator(tx.ValidatorAddr)
if !exists {
return btypes.Result{Code: btypes.CodeInternal}, nil
}
// 更新描述信息
description := validator.Description
if len(tx.Description.Moniker) != 0 {
description.Moniker = tx.Description.Moniker
}
if len(tx.Description.Logo) != 0 {
description.Logo = tx.Description.Logo
}
if len(tx.Description.Website) != 0 {
description.Website = tx.Description.Website
}
if len(tx.Description.Details) != 0 {
description.Details = tx.Description.Details
}
validator.Description = description
// 更新佣金比例信息
if tx.CommissionRate != nil {
validator.Commission = types.NewCommissionWithTime(*tx.CommissionRate, validator.Commission.MaxRate, validator.Commission.MaxChangeRate, ctx.BlockHeader().Time.UTC())
}
// 更新验证节点信息
validatorMapper.Set(types.BuildValidatorKey(validator.GetValidatorAddress()), validator)
// 发送事件
result.Events = btypes.Events{
btypes.NewEvent(
types.EventTypeModifyValidator,
btypes.NewAttribute(types.AttributeKeyValidator, tx.ValidatorAddr.String()),
btypes.NewAttribute(types.AttributeKeyOwner, tx.Owner.String()),
),
btypes.NewEvent(
btypes.EventTypeMessage,
btypes.NewAttribute(btypes.AttributeKeyModule, types.AttributeKeyModule),
btypes.NewAttribute(btypes.AttributeKeyAction, types.EventTypeModifyValidator),
btypes.NewAttribute(btypes.AttributeKeyGasPayer, tx.GetGasPayer().String()),
),
}
return
}
// 签名账户
func (tx *TxModifyValidator) GetSigner() []btypes.AccAddress {
return []btypes.AccAddress{tx.Owner}
}
// Tx Gas, 0.18QOS
func (tx *TxModifyValidator) CalcGas() btypes.BigInt {
return btypes.NewInt(GasForModifyValidator)
}
// Gas payer,
func (tx *TxModifyValidator) GetGasPayer() btypes.AccAddress {
return tx.Owner
}
// 签名字节
func (tx *TxModifyValidator) GetSignData() (ret []byte) {
return Cdc.MustMarshalJSON(*tx)
}
// 撤销验证节点Tx
type TxRevokeValidator struct {
Owner btypes.AccAddress `json:"owner"` // 验证人Owner地址
ValidatorAddr btypes.ValAddress `json:"validator_addr"` // 验证人地址
}
var _ txs.ITx = (*TxRevokeValidator)(nil)
func NewRevokeValidatorTx(owner btypes.AccAddress, validatorAddr btypes.ValAddress) *TxRevokeValidator {
return &TxRevokeValidator{
Owner: owner,
ValidatorAddr: validatorAddr,
}
}
// 数据校验
func (tx *TxRevokeValidator) ValidateData(ctx context.Context) (err error) {
if len(tx.Owner) == 0 {
return types.ErrInvalidInput("owner is empty")
}
if len(tx.ValidatorAddr) == 0 {
return types.ErrInvalidInput("validator address is empty")
}
_, err = validateValidator(ctx, tx.ValidatorAddr, true, types.Active, tx.Owner, true)
if nil != err {
return err
}
return nil
}
// 交易执行
func (tx *TxRevokeValidator) Exec(ctx context.Context) (result btypes.Result, crossTxQcp *txs.TxQcp) {
result = btypes.Result{Code: btypes.CodeOK}
// 获取验证节点信息
mapper := ctx.Mapper(types.MapperName).(*mapper.Mapper)
validator, exists := mapper.GetValidator(tx.ValidatorAddr)
if !exists {
return btypes.Result{Code: btypes.CodeInternal}, nil
}
valAddr := validator.GetValidatorAddress()
// 更新验证节点状态, active -> inactive
mapper.MakeValidatorInactive(valAddr, ctx.BlockHeight(), ctx.BlockHeader().Time.UTC(), types.Revoke)
// 发送事件
result.Events = btypes.Events{
btypes.NewEvent(
types.EventTypeRevokeValidator,
btypes.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
btypes.NewAttribute(types.AttributeKeyOwner, validator.Owner.String()),
),
btypes.NewEvent(
btypes.EventTypeMessage,
btypes.NewAttribute(btypes.AttributeKeyModule, types.AttributeKeyModule),
btypes.NewAttribute(btypes.AttributeKeyAction, types.EventTypeRevokeValidator),
btypes.NewAttribute(btypes.AttributeKeyGasPayer, tx.GetGasPayer().String()),
),
}
return
}
// 签名账户,owenr
func (tx *TxRevokeValidator) GetSigner() []btypes.AccAddress {
return []btypes.AccAddress{tx.Owner}
}
// Tx Gas, 18QOS
func (tx *TxRevokeValidator) CalcGas() btypes.BigInt {
return btypes.NewInt(GasForRevokeValidator)
}
// Gas payer, owenr
func (tx *TxRevokeValidator) GetGasPayer() btypes.AccAddress {
return tx.Owner
}
// 签名字节
func (tx *TxRevokeValidator) GetSignData() (ret []byte) {
ret = Cdc.MustMarshalBinaryBare(tx)
return
}
// 激活验证节点Tx
type TxActiveValidator struct {
Owner btypes.AccAddress `json:"owner"` // 验证人Owner地址
ValidatorAddr btypes.ValAddress `json:"validator_addr"` // 验证人地址
BondTokens btypes.BigInt `json:"bond_tokens"` // 增加绑定Token数量
}
var _ txs.ITx = (*TxActiveValidator)(nil)
func NewActiveValidatorTx(owner btypes.AccAddress, validatorAddr btypes.ValAddress, bondTokens btypes.BigInt) *TxActiveValidator {
return &TxActiveValidator{
Owner: owner,
ValidatorAddr: validatorAddr,
BondTokens: bondTokens,
}
}
// 数据验证
func (tx *TxActiveValidator) ValidateData(ctx context.Context) (err error) {
if len(tx.Owner) == 0 {
return types.ErrInvalidInput("owner is empty")
}
if len(tx.ValidatorAddr) == 0 {
return types.ErrInvalidInput("validator address is empty")
}
err = validateQOSAccount(ctx, btypes.AccAddress(tx.ValidatorAddr), tx.BondTokens)
if nil != err {
return err
}
_, err = validateValidator(ctx, tx.ValidatorAddr, true, types.Inactive, tx.Owner, true)
if nil != err {
return err
}
return nil
}
// 交易执行
func (tx *TxActiveValidator) Exec(ctx context.Context) (result btypes.Result, crossTxQcp *txs.TxQcp) {
result = btypes.Result{Code: btypes.CodeOK}
// 准备 Mapper
stakeMapper := mapper.GetMapper(ctx)
accountMapper := baseabci.GetAccountMapper(ctx)
// 获取 Owner 对应的 Validator
validator, exists := stakeMapper.GetValidator(tx.ValidatorAddr)
if !exists {
return btypes.Result{Code: btypes.CodeInternal}, nil
}
// 激活 Validator
validatorAddr := validator.GetValidatorAddress()
stakeMapper.MakeValidatorActive(validatorAddr, tx.BondTokens)
// 重置 ValidatorVoteInfo
voteInfo := types.NewValidatorVoteInfo(validator.BondHeight+1, 0, 0)
stakeMapper.ResetValidatorVoteInfo(validator.GetValidatorAddress(), voteInfo)
// 获取 Owner 对应的 Delegator 账户
delegatorAddr := validator.Owner
delegator := accountMapper.GetAccount(delegatorAddr).(*qtypes.QOSAccount)
// 当增加委托的tokens大于0时
if tx.BondTokens.GT(btypes.ZeroInt()) {
// 从 delegator 账户, 扣去增加的自委托token数量
delegator.MustMinusQOS(tx.BondTokens)
accountMapper.SetAccount(delegator)
// 获取 delegationInfo
delegationInfo, exists := stakeMapper.GetDelegationInfo(delegatorAddr, validatorAddr)
if !exists {
return btypes.Result{Code: btypes.CodeInternal}, nil
}
// 修改 delegationInfo 中的token amount, 并保存.
delegationInfo.Amount = delegationInfo.Amount.Add(tx.BondTokens)
stakeMapper.BeforeDelegationModified(ctx, validatorAddr, delegatorAddr, delegationInfo.Amount)
stakeMapper.SetDelegationInfo(delegationInfo)
}
// 设置Events
result.Events = btypes.Events{
btypes.NewEvent(
types.EventTypeActiveValidator,
btypes.NewAttribute(types.AttributeKeyValidator, validatorAddr.String()),
btypes.NewAttribute(types.AttributeKeyOwner, validator.Owner.String()),
),
btypes.NewEvent(
btypes.EventTypeMessage,
btypes.NewAttribute(btypes.AttributeKeyModule, types.AttributeKeyModule),
btypes.NewAttribute(btypes.AttributeKeyAction, types.EventTypeActiveValidator),
btypes.NewAttribute(btypes.AttributeKeyGasPayer, tx.GetGasPayer().String()),
),
}
return
}
// 签名账户,owner
func (tx *TxActiveValidator) GetSigner() []btypes.AccAddress {
return []btypes.AccAddress{tx.Owner}
}
// Tx Gas, 0
func (tx *TxActiveValidator) CalcGas() btypes.BigInt {
return btypes.ZeroInt()
}
// Gas payer, owner
func (tx *TxActiveValidator) GetGasPayer() btypes.AccAddress {
return tx.Owner
}
// 签名字节
func (tx *TxActiveValidator) GetSignData() (ret []byte) {
return Cdc.MustMarshalJSON(*tx)
}
// 验证账户余额
func validateQOSAccount(ctx context.Context, addr btypes.AccAddress, toPay btypes.BigInt) error {
accountMapper := bank.GetMapper(ctx)
acc := accountMapper.GetAccount(addr)
if toPay.GT(btypes.ZeroInt()) {
if acc != nil {
qosAccount := acc.(*qtypes.QOSAccount)
if !qosAccount.EnoughOfQOS(toPay) {
return types.ErrOwnerNoEnoughToken()
}
} else {
return types.ErrOwnerNoEnoughToken()
}
}
return nil
}
// 验证验证节点状态, 返回验证节点信息
func validateValidator(ctx context.Context, validatorAddr btypes.ValAddress, checkStatus bool, expectedStatus int8, owner btypes.AccAddress, checkOwner bool) (validator types.Validator, err error) {
valMapper := mapper.GetMapper(ctx)
validator, exists := valMapper.GetValidator(validatorAddr)
if !exists {
return validator, types.ErrValidatorNotExists()
}
if checkOwner && !validator.Owner.Equals(owner) {
return validator, types.ErrOwnerNotMatch()
}
if checkStatus {
if validator.Status != expectedStatus {
return validator, fmt.Errorf("validator status not match. except: %d, actual:%d", expectedStatus, validator.Status)
}
}
return validator, nil
}