/
handler.go
910 lines (764 loc) · 26 KB
/
handler.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
package stake
import (
"fmt"
"github.com/CyberMiles/travis/commons"
"github.com/CyberMiles/travis/sdk"
"github.com/CyberMiles/travis/sdk/errors"
"github.com/CyberMiles/travis/sdk/state"
"github.com/CyberMiles/travis/types"
"github.com/CyberMiles/travis/utils"
"github.com/ethereum/go-ethereum/common"
ethstat "github.com/ethereum/go-ethereum/core/state"
"math/big"
"strconv"
)
// DelegatedProofOfStake - interface to enforce delegation stake
type delegatedProofOfStake interface {
declareCandidacy(TxDeclareCandidacy, sdk.Int) error
updateCandidacy(TxUpdateCandidacy, sdk.Int) error
withdrawCandidacy(TxWithdrawCandidacy) error
verifyCandidacy(TxVerifyCandidacy) error
activateCandidacy(TxActivateCandidacy) error
delegate(TxDelegate) error
withdraw(TxWithdraw) error
setCompRate(TxSetCompRate, sdk.Int) error
updateCandidateAccount(TxUpdateCandidacyAccount, sdk.Int) (int64, error)
acceptCandidateAccountUpdateRequest(TxAcceptCandidacyAccountUpdate, sdk.Int) error
}
func SetGenesisValidator(val types.GenesisValidator, store state.SimpleDB) error {
if val.Address == "0000000000000000000000000000000000000000" {
return ErrBadValidatorAddr()
}
addr := common.HexToAddress(val.Address)
// create and save the empty candidate
bond := GetCandidateByAddress(addr)
if bond != nil {
return ErrCandidateExistsAddr()
}
params := utils.GetParams()
deliverer := deliver{
store: store,
sender: addr,
params: params,
ctx: types.NewContext("", 0, 0, nil),
}
desc := Description{
Name: val.Name,
Website: val.Website,
Location: val.Location,
Email: val.Email,
Profile: val.Profile,
}
tx := TxDeclareCandidacy{types.PubKeyString(val.PubKey), utils.ToWei(val.MaxAmount).String(), val.CompRate, desc}
return deliverer.declareGenesisCandidacy(tx, val)
}
// CheckTx checks if the tx is properly structured
func CheckTx(ctx types.Context, store state.SimpleDB, tx sdk.Tx) (res sdk.CheckResult, err error) {
err = tx.ValidateBasic()
if err != nil {
return res, err
}
// get the sender
sender, err := getTxSender(ctx)
if err != nil {
return res, err
}
params := utils.GetParams()
checker := check{
store: store,
sender: sender,
params: params,
ctx: ctx,
}
switch txInner := tx.Unwrap().(type) {
case TxDeclareCandidacy:
gasFee := utils.CalGasFee(params.DeclareCandidacyGas, params.GasPrice)
return res, checker.declareCandidacy(txInner, gasFee)
case TxUpdateCandidacy:
gasFee := utils.CalGasFee(params.UpdateCandidacyGas, params.GasPrice)
return res, checker.updateCandidacy(txInner, gasFee)
case TxWithdrawCandidacy:
return res, checker.withdrawCandidacy(txInner)
case TxVerifyCandidacy:
return res, checker.verifyCandidacy(txInner)
case TxActivateCandidacy:
return res, checker.activateCandidacy(txInner)
case TxDelegate:
return res, checker.delegate(txInner)
case TxWithdraw:
return res, checker.withdraw(txInner)
case TxSetCompRate:
gasFee := utils.CalGasFee(params.SetCompRateGas, params.GasPrice)
return res, checker.setCompRate(txInner, gasFee)
case TxUpdateCandidacyAccount:
gasFee := utils.CalGasFee(params.UpdateCandidateAccountGas, params.GasPrice)
_, err := checker.updateCandidateAccount(txInner, gasFee)
return res, err
case TxAcceptCandidacyAccountUpdate:
gasFee := utils.CalGasFee(params.AcceptCandidateAccountUpdateRequestGas, params.GasPrice)
return res, checker.acceptCandidateAccountUpdateRequest(txInner, gasFee)
}
return res, errors.ErrUnknownTxType(tx)
}
// DeliverTx executes the tx if valid
func DeliverTx(ctx types.Context, store state.SimpleDB, tx sdk.Tx, hash []byte) (res sdk.DeliverResult, err error) {
_, err = CheckTx(ctx, store, tx)
if err != nil {
return
}
sender, err := getTxSender(ctx)
if err != nil {
return
}
params := utils.GetParams()
deliverer := deliver{
store: store,
sender: sender,
params: params,
ctx: ctx,
}
res.GasFee = big.NewInt(0)
// Run the transaction
switch txInner := tx.Unwrap().(type) {
case TxDeclareCandidacy:
gasFee := utils.CalGasFee(params.DeclareCandidacyGas, params.GasPrice)
err := deliverer.declareCandidacy(txInner, gasFee)
if err == nil {
res.GasUsed = int64(params.DeclareCandidacyGas)
res.GasFee = gasFee.Int
}
return res, err
case TxUpdateCandidacy:
gasFee := utils.CalGasFee(params.UpdateCandidacyGas, params.GasPrice)
err := deliverer.updateCandidacy(txInner, gasFee)
if err == nil {
res.GasUsed = int64(params.UpdateCandidacyGas)
res.GasFee = gasFee.Int
}
return res, err
case TxWithdrawCandidacy:
return res, deliverer.withdrawCandidacy(txInner)
case TxVerifyCandidacy:
return res, deliverer.verifyCandidacy(txInner)
case TxActivateCandidacy:
return res, deliverer.activateCandidacy(txInner)
case TxDelegate:
return res, deliverer.delegate(txInner)
case TxWithdraw:
return res, deliverer.withdraw(txInner)
case TxSetCompRate:
gasFee := utils.CalGasFee(params.SetCompRateGas, params.GasPrice)
err := deliverer.setCompRate(txInner, gasFee)
if err == nil {
res.GasUsed = int64(params.SetCompRateGas)
res.GasFee = gasFee.Int
}
return res, err
case TxUpdateCandidacyAccount:
gasFee := utils.CalGasFee(params.UpdateCandidateAccountGas, params.GasPrice)
id, err := deliverer.updateCandidateAccount(txInner, gasFee)
if err == nil {
res.GasUsed = int64(params.UpdateCandidateAccountGas)
res.GasFee = gasFee.Int
}
res.Data = []byte(strconv.Itoa(int(id)))
return res, err
case TxAcceptCandidacyAccountUpdate:
gasFee := utils.CalGasFee(params.AcceptCandidateAccountUpdateRequestGas, params.GasPrice)
err := deliverer.acceptCandidateAccountUpdateRequest(txInner, gasFee)
if err == nil {
res.GasUsed = int64(params.AcceptCandidateAccountUpdateRequestGas)
res.GasFee = gasFee.Int
}
return res, err
}
return
}
// get the sender from the ctx and ensure it matches the tx pubkey
func getTxSender(ctx types.Context) (sender common.Address, err error) {
senders := ctx.GetSigners()
if len(senders) != 1 {
return sender, ErrMissingSignature()
}
return senders[0], nil
}
//_______________________________________________________________________
type check struct {
store state.SimpleDB
sender common.Address
params *utils.Params
ctx types.Context
}
var _ delegatedProofOfStake = check{} // enforce interface at compile time
func (c check) declareCandidacy(tx TxDeclareCandidacy, gasFee sdk.Int) error {
pk, err := types.GetPubKey(tx.PubKey)
if err != nil {
return err
}
// check to see if the pubkey or address has been registered before
candidate := GetCandidateByAddress(c.sender)
if candidate != nil {
return fmt.Errorf("address has been declared")
}
candidate = GetCandidateByPubKey(pk)
if candidate != nil {
return fmt.Errorf("pubkey has been declared")
}
// check to see if the associated account has 10%(ssr, short for self-staking ratio, configurable) of the max staked CMT amount
maxAmount, ok := sdk.NewIntFromString(tx.MaxAmount)
if !ok || maxAmount.LTE(sdk.ZeroInt) {
return ErrBadAmount()
}
ss := tx.SelfStakingAmount(c.params.SelfStakingRatio)
totalCost := ss.Add(gasFee)
// check if the delegator has sufficient funds
if err := checkBalance(c.ctx.EthappState(), c.sender, totalCost); err != nil {
return err
}
// Check to see if the compensation rate is between 0 and 1
if tx.CompRate.IsNil() || tx.CompRate.LTE(sdk.ZeroRat) || tx.CompRate.GTE(sdk.OneRat) {
return ErrBadCompRate()
}
return nil
}
func (c check) updateCandidacy(tx TxUpdateCandidacy, gasFee sdk.Int) error {
candidate := GetCandidateByAddress(c.sender)
if candidate == nil {
return fmt.Errorf("cannot edit non-exsits candidacy")
}
totalCost := gasFee
// If the max amount of CMTs is updated, the 10% of self-staking will be re-computed,
// and the different will be charged
if tx.MaxAmount != "" {
maxAmount, ok := sdk.NewIntFromString(tx.MaxAmount)
if !ok || maxAmount.LTE(sdk.ZeroInt) {
return ErrBadAmount()
}
if maxAmount.GT(candidate.ParseMaxShares()) {
rechargeAmount := getRechargeAmount(maxAmount, candidate, c.params.SelfStakingRatio)
totalCost = totalCost.Add(rechargeAmount)
}
}
// check if the delegator has sufficient funds
if err := checkBalance(c.ctx.EthappState(), c.sender, totalCost); err != nil {
return err
}
return nil
}
func (c check) withdrawCandidacy(tx TxWithdrawCandidacy) error {
// check to see if the address has been registered before
candidate := GetCandidateByAddress(c.sender)
if candidate == nil {
return fmt.Errorf("cannot withdraw non-exsits candidacy")
}
return nil
}
func (c check) verifyCandidacy(tx TxVerifyCandidacy) error {
// check to see if the candidate address to be verified has been registered before
candidate := GetCandidateByAddress(tx.CandidateAddress)
if candidate == nil {
return fmt.Errorf("cannot verify non-exsits candidacy")
}
// check to see if the request was initiated by a special account
if c.sender != common.HexToAddress(utils.GetParams().FoundationAddress) {
return ErrVerificationDisallowed()
}
return nil
}
func (c check) activateCandidacy(tx TxActivateCandidacy) error {
// check to see if the address has been registered before
candidate := GetCandidateByAddress(c.sender)
if candidate == nil {
return fmt.Errorf("cannot activate non-exsits candidacy")
}
if candidate.Active == "Y" {
return fmt.Errorf("already activated")
}
if candidate.ParseShares().Equal(sdk.ZeroInt) {
return fmt.Errorf("cannot activate withdrawed candidacy")
}
return nil
}
func (c check) delegate(tx TxDelegate) error {
err := VerifyCubeSignature(c.sender, c.ctx.GetNonce(), tx.CubeBatch, tx.Sig)
if err != nil {
return err
}
candidate := GetCandidateByAddress(tx.ValidatorAddress)
if candidate == nil {
return ErrBadValidatorAddr()
}
// check if the delegator has sufficient funds
amount, ok := sdk.NewIntFromString(tx.Amount)
if !ok || amount.LTE(sdk.ZeroInt) {
return ErrBadAmount()
}
err = checkBalance(c.ctx.EthappState(), c.sender, amount)
if err != nil {
return err
}
// check to see if the simpleValidator has reached its declared max amount CMTs to be staked.
if candidate.ParseShares().Add(amount).GT(candidate.ParseMaxShares()) {
return ErrReachMaxAmount()
}
return nil
}
func (c check) withdraw(tx TxWithdraw) error {
// check if has delegated
candidate := GetCandidateByAddress(tx.ValidatorAddress)
if candidate == nil {
return ErrBadValidatorAddr()
}
amount, ok := sdk.NewIntFromString(tx.Amount)
if !ok || amount.LTE(sdk.ZeroInt) {
return ErrBadAmount()
}
d := GetDelegation(c.sender, candidate.Id)
if d == nil {
return ErrDelegationNotExists()
}
if amount.GT(d.Shares()) {
return ErrInvalidWithdrawalAmount()
}
return nil
}
func (c check) setCompRate(tx TxSetCompRate, gasFee sdk.Int) error {
// Check to see if the compensation rate is between 0 and 1
if tx.CompRate.IsNil() || tx.CompRate.LTE(sdk.ZeroRat) || tx.CompRate.GTE(sdk.OneRat) {
return ErrBadCompRate()
}
candidate := GetCandidateByAddress(c.sender)
if candidate == nil {
return ErrBadValidatorAddr()
}
d := GetDelegation(tx.DelegatorAddress, candidate.Id)
if d == nil {
return ErrDelegationNotExists()
}
if tx.CompRate.GT(candidate.CompRate) {
return ErrBadCompRate()
}
// check if the delegator has sufficient funds
if err := checkBalance(c.ctx.EthappState(), c.sender, gasFee); err != nil {
return err
}
return nil
}
func (c check) updateCandidateAccount(tx TxUpdateCandidacyAccount, gasFee sdk.Int) (int64, error) {
candidate := GetCandidateByAddress(c.sender)
if candidate == nil {
return 0, ErrBadRequest()
}
// check if the address has been changed
ownerAddress := common.HexToAddress(candidate.OwnerAddress)
if utils.IsEmptyAddress(tx.NewCandidateAddress) || tx.NewCandidateAddress == ownerAddress {
return 0, ErrBadRequest()
}
// check if the candidate has sufficient funds
if err := checkBalance(c.ctx.EthappState(), c.sender, gasFee); err != nil {
return 0, err
}
return 0, nil
}
func (c check) acceptCandidateAccountUpdateRequest(tx TxAcceptCandidacyAccountUpdate, gasFee sdk.Int) error {
req := getCandidateAccountUpdateRequest(tx.AccountUpdateRequestId)
if req == nil {
return ErrBadRequest()
}
if req.ToAddress != c.sender || req.State != "PENDING" {
return ErrBadRequest()
}
candidate := GetCandidateById(req.CandidateId)
totalCost := candidate.ParseShares().Add(gasFee)
// check if the new account has sufficient funds
if err := checkBalance(c.ctx.EthappState(), req.ToAddress, totalCost); err != nil {
return err
}
// check if the candidate has some pending withdrawal requests
reqs := GetUnstakeRequestsByDelegator(req.FromAddress)
if len(reqs) > 0 {
return ErrCandidateHasPendingUnstakeRequests()
}
return nil
}
//_____________________________________________________________________
type deliver struct {
store state.SimpleDB
sender common.Address
params *utils.Params
ctx types.Context
}
var _ delegatedProofOfStake = deliver{} // enforce interface at compile time
// These functions assume everything has been authenticated,
// now we just perform action and save
func (d deliver) declareCandidacy(tx TxDeclareCandidacy, gasFee sdk.Int) error {
// create and save the empty candidate
pubKey, err := types.GetPubKey(tx.PubKey)
if err != nil {
return err
}
now := d.ctx.BlockTime()
candidate := &Candidate{
PubKey: pubKey,
OwnerAddress: d.sender.String(),
Shares: "0",
VotingPower: 0,
MaxShares: tx.MaxAmount,
CompRate: tx.CompRate,
CreatedAt: now,
Description: tx.Description,
Verified: "N",
Active: "Y",
BlockHeight: d.ctx.BlockHeight(),
State: "Candidate",
}
// delegate a part of the max staked CMT amount
amount := tx.SelfStakingAmount(d.params.SelfStakingRatio)
totalCost := amount.Add(gasFee)
// check if the delegator has sufficient funds
if err := checkBalance(d.ctx.EthappState(), d.sender, totalCost); err != nil {
return err
}
// only charge gas fee here
commons.Transfer(d.sender, utils.HoldAccount, gasFee)
SaveCandidate(candidate)
txDelegate := TxDelegate{ValidatorAddress: d.sender, Amount: amount.String()}
d.delegate(txDelegate)
candidate = GetCandidateByPubKey(pubKey) // candidate object was modified by the delegation operation.
cds := &CandidateDailyStake{CandidateId: candidate.Id, Amount: candidate.Shares, BlockHeight: d.ctx.BlockHeight()}
SaveCandidateDailyStake(cds)
candidate.PendingVotingPower = candidate.CalcVotingPower(d.ctx.BlockHeight())
updateCandidate(candidate)
return nil
}
func (d deliver) declareGenesisCandidacy(tx TxDeclareCandidacy, val types.GenesisValidator) error {
// create and save the empty candidate
pubKey, err := types.GetPubKey(tx.PubKey)
if err != nil {
return err
}
power, _ := strconv.ParseInt(val.Power, 10, 64)
candidate := &Candidate{
PubKey: pubKey,
OwnerAddress: d.sender.String(),
Shares: "0",
VotingPower: power,
MaxShares: tx.MaxAmount,
CompRate: tx.CompRate,
CreatedAt: 0,
Description: tx.Description,
Verified: "N",
Active: "Y",
BlockHeight: d.ctx.BlockHeight(),
State: "Validator",
}
SaveCandidate(candidate)
// delegate a part of the max staked CMT amount
amount := sdk.NewInt(val.Shares).Mul(sdk.E18Int).String()
txDelegate := TxDelegate{ValidatorAddress: d.sender, Amount: amount}
d.delegate(txDelegate)
candidate = GetCandidateByPubKey(pubKey) // candidate object was modified by the delegation operation.
cds := &CandidateDailyStake{CandidateId: candidate.Id, Amount: candidate.Shares, BlockHeight: d.ctx.BlockHeight()}
SaveCandidateDailyStake(cds)
candidate.PendingVotingPower = candidate.CalcVotingPower(d.ctx.BlockHeight())
updateCandidate(candidate)
return nil
}
func (d deliver) updateCandidacy(tx TxUpdateCandidacy, gasFee sdk.Int) error {
// create and save the empty candidate
candidate := GetCandidateByAddress(d.sender)
if candidate == nil {
return ErrBadValidatorAddr()
}
totalCost := gasFee
// If the max amount of CMTs is updated, the 10% of self-staking will be re-computed,
// and the different will be charged
if tx.MaxAmount != "" {
maxAmount, _ := sdk.NewIntFromString(tx.MaxAmount)
if !candidate.ParseMaxShares().Equal(maxAmount) {
rechargeAmount := getRechargeAmount(maxAmount, candidate, d.params.SelfStakingRatio)
if rechargeAmount.Cmp(big.NewInt(0)) > 0 {
// charge
totalCost = totalCost.Add(rechargeAmount)
candidate.AddShares(rechargeAmount)
// update delegation
delegation := GetDelegation(d.sender, candidate.Id)
delegation.AddDelegateAmount(rechargeAmount)
UpdateDelegation(delegation)
delegateHistory := &DelegateHistory{DelegatorAddress: d.sender, CandidateId: candidate.Id, Amount: rechargeAmount, OpCode: "recharge", BlockHeight: d.ctx.BlockHeight()}
saveDelegateHistory(delegateHistory)
}
candidate.MaxShares = maxAmount.String()
}
}
// If other information was updated, set the verified status to false
if candidate.Description != tx.Description {
candidate.Verified = "N"
candidate.Description = tx.Description
}
// check if the delegator has sufficient funds
if err := checkBalance(d.ctx.EthappState(), d.sender, totalCost); err != nil {
return err
}
commons.Transfer(d.sender, utils.HoldAccount, totalCost)
updateCandidate(candidate)
return nil
}
func (d deliver) withdrawCandidacy(tx TxWithdrawCandidacy) error {
// create and save the empty candidate
validatorAddress := d.sender
candidate := GetCandidateByAddress(validatorAddress)
if candidate == nil {
return ErrBadValidatorAddr()
}
// All staked tokens will be distributed back to delegator addresses.
// Self-staked CMTs will be refunded back to the validator address.
delegations := GetDelegationsByCandidate(candidate.Id, "Y")
for _, delegation := range delegations {
txWithdraw := TxWithdraw{ValidatorAddress: validatorAddress, Amount: delegation.Shares().String()}
d.doWithdraw(delegation, delegation.Shares(), candidate, txWithdraw)
}
candidate.Shares = "0"
candidate.NumOfDelegators = 0
updateCandidate(candidate)
return nil
}
func (d deliver) verifyCandidacy(tx TxVerifyCandidacy) error {
// verify candidacy
candidate := GetCandidateByAddress(tx.CandidateAddress)
if tx.Verified {
candidate.Verified = "Y"
} else {
candidate.Verified = "N"
}
updateCandidate(candidate)
return nil
}
func (d deliver) activateCandidacy(tx TxActivateCandidacy) error {
// check to see if the address has been registered before
candidate := GetCandidateByAddress(d.sender)
if candidate == nil {
return fmt.Errorf("cannot activate non-exsits candidacy")
}
candidate.Active = "Y"
updateCandidate(candidate)
return nil
}
func (d deliver) delegate(tx TxDelegate) error {
// Get the pubKey bond account
candidate := GetCandidateByAddress(tx.ValidatorAddress)
delegateAmount, ok := sdk.NewIntFromString(tx.Amount)
if !ok || delegateAmount.LT(sdk.ZeroInt) {
return ErrBadAmount()
}
// Move coins from the delegator account to the pubKey lock account
err := commons.Transfer(d.sender, utils.HoldAccount, delegateAmount)
if err != nil {
return err
}
// create or update delegation
now := d.ctx.BlockTime()
delegation := GetDelegation(d.sender, candidate.Id)
if delegation == nil {
delegation = &Delegation{
DelegatorAddress: d.sender,
PubKey: candidate.PubKey,
CandidateId: candidate.Id,
DelegateAmount: tx.Amount,
AwardAmount: "0",
WithdrawAmount: "0",
PendingWithdrawAmount: "0",
SlashAmount: "0",
State: "Y",
CompRate: candidate.CompRate,
BlockHeight: d.ctx.BlockHeight(),
CreatedAt: now,
}
SaveDelegation(delegation)
} else {
delegation.AddDelegateAmount(delegateAmount)
delegation.State = "Y"
UpdateDelegation(delegation)
}
// Add delegateAmount to candidate
candidate.AddShares(delegateAmount)
candidate.NumOfDelegators = GetNumOfDelegatorsByCandidate(candidate.Id)
updateCandidate(candidate)
delegateHistory := &DelegateHistory{DelegatorAddress: d.sender, CandidateId: candidate.Id, Amount: delegateAmount, OpCode: "delegate", BlockHeight: d.ctx.BlockHeight()}
saveDelegateHistory(delegateHistory)
return nil
}
func (d deliver) withdraw(tx TxWithdraw) error {
// get pubKey candidate
candidate := GetCandidateByAddress(tx.ValidatorAddress)
if candidate == nil {
return ErrBadValidatorAddr()
}
amount, ok := sdk.NewIntFromString(tx.Amount)
if !ok {
return ErrInvalidWithdrawalAmount()
}
delegation := GetDelegation(d.sender, candidate.Id)
// candidates can't withdraw the reserved reservation fund
if d.sender.String() == candidate.OwnerAddress {
remained := delegation.Shares().Sub(amount)
if remained.LT(candidate.SelfStakingAmount(d.params.SelfStakingRatio)) {
return ErrCandidateWithdrawalDisallowed()
}
}
d.doWithdraw(delegation, amount, candidate, tx)
// deduct shares from the candidate
candidate.AddShares(amount.Neg())
updateCandidate(candidate)
return nil
}
func (d deliver) doWithdraw(delegation *Delegation, amount sdk.Int, candidate *Candidate, tx TxWithdraw) {
delegation.ReduceAverageStakingDate(amount)
delegation.AddPendingWithdrawAmount(amount)
UpdateDelegation(delegation)
// record the unstaking requests which will be processed in 7 days
performedBlockHeight := d.ctx.BlockHeight() + int64(utils.GetParams().UnstakeWaitingPeriod)
unstakeRequest := &UnstakeRequest{
DelegatorAddress: delegation.DelegatorAddress,
InitiatedBlockHeight: d.ctx.BlockHeight(),
CandidateId: candidate.Id,
PerformedBlockHeight: performedBlockHeight,
Amount: amount.String(),
State: "PENDING",
}
saveUnstakeRequest(unstakeRequest)
delegateHistory := &DelegateHistory{DelegatorAddress: d.sender, CandidateId: candidate.Id, Amount: amount, OpCode: "withdraw", BlockHeight: d.ctx.BlockHeight()}
saveDelegateHistory(delegateHistory)
return
}
func (d deliver) setCompRate(tx TxSetCompRate, gasFee sdk.Int) error {
candidate := GetCandidateByAddress(d.sender)
delegation := GetDelegation(tx.DelegatorAddress, candidate.Id)
if delegation == nil {
return ErrDelegationNotExists()
}
// check if the candidate has sufficient funds
if err := checkBalance(d.ctx.EthappState(), d.sender, gasFee); err != nil {
return err
}
// only charge gas fee here
commons.Transfer(d.sender, utils.HoldAccount, gasFee)
delegation.CompRate = tx.CompRate
UpdateDelegation(delegation)
return nil
}
func (d deliver) updateCandidateAccount(tx TxUpdateCandidacyAccount, gasFee sdk.Int) (int64, error) {
// check if the delegator has sufficient funds
if err := checkBalance(d.ctx.EthappState(), d.sender, gasFee); err != nil {
return 0, err
}
// only charge gas fee here
commons.Transfer(d.sender, utils.HoldAccount, gasFee)
candidate := GetCandidateByAddress(d.sender)
req := &CandidateAccountUpdateRequest{
CandidateId: candidate.Id,
FromAddress: d.sender, ToAddress: tx.NewCandidateAddress,
CreatedBlockHeight: d.ctx.BlockHeight(),
State: "PENDING",
}
id := saveCandidateAccountUpdateRequest(req)
return id, nil
}
func (d deliver) acceptCandidateAccountUpdateRequest(tx TxAcceptCandidacyAccountUpdate, gasFee sdk.Int) error {
req := getCandidateAccountUpdateRequest(tx.AccountUpdateRequestId)
if req == nil {
return ErrBadRequest()
}
if req.ToAddress != d.sender || req.State != "PENDING" {
return ErrBadRequest()
}
candidate := GetCandidateById(req.CandidateId)
if candidate == nil {
return ErrBadRequest()
}
// check if the candidate has sufficient funds
if err := checkBalance(d.ctx.EthappState(), d.sender, gasFee); err != nil {
return err
}
candidate.OwnerAddress = req.ToAddress.String()
updateCandidate(candidate)
// update the candidate's self-delegation
delegation := GetDelegation(req.FromAddress, candidate.Id)
if delegation == nil {
return ErrBadRequest()
}
delegation.DelegatorAddress = req.ToAddress
UpdateDelegation(delegation)
// return coins to the original account
commons.Transfer(utils.HoldAccount, req.FromAddress, candidate.ParseShares())
// lock coins from the new account
commons.Transfer(req.ToAddress, utils.HoldAccount, candidate.ParseShares().Add(gasFee))
// mark the request as completed
req.State = "COMPLETED"
req.AcceptedBlockHeight = d.ctx.BlockHeight()
updateCandidateAccountUpdateRequest(req)
return nil
}
func HandlePendingUnstakeRequests(height int64) error {
reqs := GetUnstakeRequests(height)
for _, req := range reqs {
amount, _ := sdk.NewIntFromString(req.Amount)
candidate := GetCandidateById(req.CandidateId)
if candidate == nil {
continue
}
delegation := GetDelegation(req.DelegatorAddress, candidate.Id)
if delegation == nil {
continue
}
delegation.AddWithdrawAmount(amount)
delegation.AddPendingWithdrawAmount(amount.Neg())
UpdateDelegation(delegation)
if delegation.Shares().LT(sdk.NewInt(10)) {
RemoveDelegation(delegation.Id)
candidate.NumOfDelegators = GetNumOfDelegatorsByCandidate(candidate.Id)
updateCandidate(candidate)
}
req.State = "COMPLETED"
updateUnstakeRequest(req)
// transfer coins back to account
commons.Transfer(utils.HoldAccount, req.DelegatorAddress, amount)
}
return nil
}
func checkBalance(state *ethstat.StateDB, addr common.Address, amount sdk.Int) error {
balance, err := commons.GetBalance(state, addr)
if err != nil {
return err
}
if balance.LT(amount) {
return ErrInsufficientFunds()
}
return nil
}
func getRechargeAmount(maxAmount sdk.Int, candidate *Candidate, ssr sdk.Rat) (res sdk.Int) {
diff := maxAmount.Sub(candidate.ParseMaxShares())
tmp := diff.MulRat(ssr)
d := GetDelegation(common.HexToAddress(candidate.OwnerAddress), candidate.Id)
res = tmp.Sub(d.Shares())
return
}
func RecordCandidateDailyStakes(blockHeight int64) error {
candidates := GetActiveCandidates()
for _, candidate := range candidates {
cds := &CandidateDailyStake{CandidateId: candidate.Id, Amount: candidate.Shares, BlockHeight: blockHeight}
SaveCandidateDailyStake(cds)
}
// remove expired records
startBlockHeight := blockHeight - utils.ConvertDaysToHeight(90)
RemoveExpiredCandidateDailyStakes(startBlockHeight)
return nil
}
func AccumulateDelegationsAverageStakingDate() error {
delegations := GetDelegations("Y")
for _, d := range delegations {
d.AccumulateAverageStakingDate()
UpdateDelegation(d)
}
return nil
}