/
salary.go
1173 lines (1084 loc) · 42.4 KB
/
salary.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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package blockchain
import (
"fmt"
"log"
"math/big"
"sort"
"strconv"
"github.com/incognitochain/incognito-chain/transaction"
"github.com/incognitochain/incognito-chain/blockchain/committeestate"
"github.com/incognitochain/incognito-chain/blockchain/types"
"github.com/incognitochain/incognito-chain/common"
"github.com/incognitochain/incognito-chain/common/base58"
"github.com/incognitochain/incognito-chain/config"
"github.com/incognitochain/incognito-chain/dataaccessobject/statedb"
"github.com/incognitochain/incognito-chain/instruction"
"github.com/incognitochain/incognito-chain/metadata"
"github.com/incognitochain/incognito-chain/privacy"
"github.com/incognitochain/incognito-chain/wallet"
"github.com/pkg/errors"
)
type mintDelegationRewardInfo struct {
TxRequest metadata.Transaction
ReceiverPayment privacy.PaymentAddress
RewardAmount uint64
}
func (blockchain *BlockChain) addShardRewardRequestToBeacon(beaconBlock *types.BeaconBlock, rewardStateDB *statedb.StateDB, bestState *BeaconBestState) error {
//get shard block version that confirmed by this beacon block
shardsBlockVersions := map[int]map[uint64]int{}
for _, sID := range blockchain.GetShardIDs() {
shardsBlockVersions[sID] = map[uint64]int{}
sStates := beaconBlock.Body.ShardState[byte(sID)]
for _, sState := range sStates {
shardsBlockVersions[sID][sState.Height] = sState.Version
}
}
for _, inst := range beaconBlock.Body.Instructions {
if len(inst) <= 2 {
continue
}
version := beaconBlock.Header.Version
if inst[0] == instruction.ACCEPT_BLOCK_REWARD_V3_ACTION {
acceptBlockRewardIns, err := instruction.ValidateAndImportAcceptBlockRewardV3InstructionFromString(inst)
if err != nil {
return err
}
if shardBlockVersions, ok := shardsBlockVersions[int(acceptBlockRewardIns.ShardID())]; ok {
if blkVersion, ok := shardBlockVersions[acceptBlockRewardIns.ShardBlockHeight()]; ok {
version = blkVersion
}
}
rewardAmount, err := blockchain.GetRewardAmount(acceptBlockRewardIns.ShardID(), version, acceptBlockRewardIns.ShardBlockHeight())
if err != nil {
return err
} else {
if beaconBlock.Header.Version >= types.INSTANT_FINALITY_VERSION_V2 {
if bestState.RewardMinted+rewardAmount > config.Param().MaxReward {
if config.Param().MaxReward > bestState.RewardMinted {
rewardAmount = config.Param().MaxReward - bestState.RewardMinted
} else {
rewardAmount = 0
}
}
bestState.RewardMinted += rewardAmount
}
}
acceptBlockRewardIns.TxsFee()[common.PRVCoinID] += rewardAmount
for key, value := range acceptBlockRewardIns.TxsFee() {
if value != 0 {
err = statedb.AddShardRewardRequestMultiset(
rewardStateDB,
beaconBlock.Header.Epoch,
acceptBlockRewardIns.ShardID(),
acceptBlockRewardIns.SubsetID(),
key, value)
if err != nil {
return err
}
}
}
continue
}
if instruction.IsConsensusInstruction(inst[0]) {
continue
}
metaType, err := strconv.Atoi(inst[0])
if err != nil {
continue
}
if metaType == instruction.ACCEPT_BLOCK_REWARD_V1_ACTION {
acceptedBlkRewardInfo, err := instruction.NewAcceptedBlockRewardV1FromString(inst[2])
if err != nil {
return err
}
if acceptedBlkRewardInfo.TxsFee == nil {
acceptedBlkRewardInfo.TxsFee = map[common.Hash]uint64{}
}
if shardBlockVersions, ok := shardsBlockVersions[int(acceptedBlkRewardInfo.ShardID)]; ok {
if blkVersion, ok := shardBlockVersions[acceptedBlkRewardInfo.ShardBlockHeight]; ok {
version = blkVersion
}
}
rewardAmount, err := blockchain.GetRewardAmount(acceptedBlkRewardInfo.ShardID, version, acceptedBlkRewardInfo.ShardBlockHeight)
if err != nil {
return err
} else {
if beaconBlock.Header.Version >= types.INSTANT_FINALITY_VERSION_V2 {
if bestState.RewardMinted+rewardAmount > config.Param().MaxReward {
if config.Param().MaxReward > bestState.RewardMinted {
rewardAmount = config.Param().MaxReward - bestState.RewardMinted
} else {
rewardAmount = 0
}
}
bestState.RewardMinted += rewardAmount
}
}
acceptedBlkRewardInfo.TxsFee[common.PRVCoinID] += rewardAmount
for key, value := range acceptedBlkRewardInfo.TxsFee {
if value != 0 {
err = statedb.AddShardRewardRequest(
rewardStateDB,
beaconBlock.Header.Epoch,
acceptedBlkRewardInfo.ShardID,
key, value)
if err != nil {
return err
}
}
}
}
}
return nil
}
func (blockchain *BlockChain) processSalaryInstructions(rewardStateDB *statedb.StateDB, beaconBlocks []*types.BeaconBlock, confirmBeaconHeight uint64, shardID byte) error {
for _, beaconBlock := range beaconBlocks {
for _, l := range beaconBlock.Body.Instructions {
if len(l) <= 2 {
continue
}
if l[0] == instruction.SHARD_RECEIVE_REWARD_V3_ACTION {
shardReceiveRewardV3, err := instruction.ValidateAndImportShardReceiveRewardV3InstructionFromString(l)
if err != nil {
Logger.log.Debug(err)
continue
}
if shardReceiveRewardV3.Epoch() != 0 {
if confirmBeaconHeight < config.Param().ConsensusParam.EnableSlashingHeightV2 {
cInfos, err := blockchain.GetAllCommitteeStakeInfo(shardReceiveRewardV3.Epoch())
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
shardSubsetStakerInfo := getCommitteeToPayRewardMultiset(cInfos[int(shardReceiveRewardV3.ShardID())], shardReceiveRewardV3)
err = blockchain.addShardCommitteeReward(rewardStateDB, shardID, shardReceiveRewardV3.Reward(), shardSubsetStakerInfo)
if err != nil {
return err
}
} else {
cInfosV2, err := blockchain.GetAllCommitteeStakeInfoSlashingVersion(shardReceiveRewardV3.Epoch())
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
shardSubsetStakerInfo := getCommitteeToPayRewardMultisetSlashingVersion(cInfosV2[int(shardReceiveRewardV3.ShardID())], shardReceiveRewardV3)
beaconBestState := blockchain.BeaconChain.GetBestView().(*BeaconBestState)
nonSlashingCInfosV2, err := beaconBestState.GetNonSlashingCommittee(shardSubsetStakerInfo, shardReceiveRewardV3.Epoch(), shardReceiveRewardV3.ShardID())
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
err = blockchain.addShardCommitteeRewardSlashingVersion(rewardStateDB, shardID, shardReceiveRewardV3.Reward(), nonSlashingCInfosV2)
if err != nil {
return err
}
}
}
continue
}
if instruction.IsConsensusInstruction(l[0]) {
continue
}
shardToProcess, err := strconv.Atoi(l[1])
if err != nil {
continue
}
instType, err := strconv.Atoi(l[0])
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
if shardToProcess == int(shardID) {
switch instType {
case metadata.BeaconRewardRequestMeta:
beaconBlkRewardInfo, err := metadata.NewBeaconBlockRewardInfoFromStr(l[3])
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
for key := range beaconBlkRewardInfo.BeaconReward {
Logger.log.Criticalf("Add Committee Reward BeaconReward, Public Key %+v, reward %+v, token %+v", beaconBlkRewardInfo.PayToPublicKey, beaconBlkRewardInfo.BeaconReward[key], key)
err = statedb.AddCommitteeReward(rewardStateDB, beaconBlkRewardInfo.PayToPublicKey, beaconBlkRewardInfo.BeaconReward[key], key)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
}
continue
case metadata.IncDAORewardRequestMeta:
incDAORewardInfo, err := metadata.NewIncDAORewardInfoFromStr(l[3])
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
keyWalletDevAccount, err := wallet.Base58CheckDeserialize(config.Param().IncognitoDAOAddress)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
for key := range incDAORewardInfo.IncDAOReward {
tempPublicKey := base58.Base58Check{}.Encode(keyWalletDevAccount.KeySet.PaymentAddress.Pk, common.Base58Version)
Logger.log.Criticalf("Add Committee Reward IncDAOReward, Public Key %+v, reward %+v, token %+v", tempPublicKey, incDAORewardInfo.IncDAOReward[key], key)
err = statedb.AddCommitteeReward(rewardStateDB, tempPublicKey, incDAORewardInfo.IncDAOReward[key], key)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
}
continue
}
}
switch instType {
case instruction.SHARD_RECEIVE_REWARD_V1_ACTION:
shardRewardInfo, err := instruction.NewShardReceiveRewardV1FromString(l[3])
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
if confirmBeaconHeight < config.Param().ConsensusParam.EnableSlashingHeightV2 {
cInfos, err := blockchain.GetAllCommitteeStakeInfo(shardRewardInfo.Epoch)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
err = blockchain.addShardCommitteeReward(rewardStateDB, shardID, shardRewardInfo.ShardReward, cInfos[int(shardToProcess)])
if err != nil {
return err
}
} else {
cInfosV2, err := blockchain.GetAllCommitteeStakeInfoSlashingVersion(shardRewardInfo.Epoch)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
beaconBestState := blockchain.BeaconChain.GetBestView().(*BeaconBestState)
nonSlashingCInfosV2, err := beaconBestState.GetNonSlashingCommittee(cInfosV2[int(shardToProcess)], shardRewardInfo.Epoch, byte(shardToProcess))
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
err = blockchain.addShardCommitteeRewardSlashingVersion(rewardStateDB, shardID, shardRewardInfo.ShardReward, nonSlashingCInfosV2)
if err != nil {
return err
}
}
continue
}
}
}
return nil
}
func getCommitteeToPayRewardMultiset(
committees []*statedb.StakerInfo,
shardReceiveRewardV3 *instruction.ShardReceiveRewardV3,
) []*statedb.StakerInfo {
res := []*statedb.StakerInfo{}
for i, v := range committees {
if i%MaxSubsetCommittees == int(shardReceiveRewardV3.SubsetID()) {
res = append(res, v)
}
}
return res
}
func getCommitteeToPayRewardMultisetSlashingVersion(
committees []*statedb.StakerInfoSlashingVersion,
shardReceiveRewardV3 *instruction.ShardReceiveRewardV3,
) []*statedb.StakerInfoSlashingVersion {
res := []*statedb.StakerInfoSlashingVersion{}
for i, v := range committees {
if i%MaxSubsetCommittees == int(shardReceiveRewardV3.SubsetID()) {
res = append(res, v)
}
}
return res
}
func (blockchain *BlockChain) addShardCommitteeReward(
rewardStateDB *statedb.StateDB,
shardID byte,
reward map[common.Hash]uint64,
cStakeInfos []*statedb.StakerInfo,
) (
err error,
) {
committeeSize := len(cStakeInfos)
for _, candidate := range cStakeInfos {
if common.GetShardIDFromLastByte(candidate.RewardReceiver().Pk[common.PublicKeySize-1]) == shardID {
for key, value := range reward {
tempPK := base58.Base58Check{}.Encode(candidate.RewardReceiver().Pk, common.Base58Version)
Logger.log.Criticalf("Add Committee Reward ShardCommitteeReward, Public Key %+v, reward %+v, token %+v", tempPK, value/uint64(committeeSize), key)
err = statedb.AddCommitteeReward(rewardStateDB, tempPK, value/uint64(committeeSize), key)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
}
}
}
return nil
}
func (blockchain *BlockChain) addShardCommitteeRewardSlashingVersion(
rewardStateDB *statedb.StateDB,
shardID byte,
reward map[common.Hash]uint64,
cStakeInfos []*statedb.StakerInfoSlashingVersion,
) (
err error,
) {
committeeSize := len(cStakeInfos)
for _, candidate := range cStakeInfos {
if common.GetShardIDFromLastByte(candidate.RewardReceiver().Pk[common.PublicKeySize-1]) == shardID {
for key, value := range reward {
tempPK := base58.Base58Check{}.Encode(candidate.RewardReceiver().Pk, common.Base58Version)
Logger.log.Criticalf("Add Committee Reward ShardCommitteeReward, Public Key %+v, reward %+v, token %+v", tempPK, value/uint64(committeeSize), key)
err = statedb.AddCommitteeReward(rewardStateDB, tempPK, value/uint64(committeeSize), key)
if err != nil {
return NewBlockChainError(ProcessSalaryInstructionsError, err)
}
}
}
}
return nil
}
func (blockchain *BlockChain) calculateRewardMultiset(
splitRewardRuleProcessor committeestate.SplitRewardRuleProcessor,
curView *BeaconBestState,
maxBeaconBlockCreation uint64,
maxSubsetsCommittee int,
beaconHeight uint64,
epoch uint64,
isSplitRewardForCustodian bool,
percentCustodianRewards uint64,
) (map[common.Hash]uint64,
[][]map[common.Hash]uint64,
map[common.Hash]uint64,
map[common.Hash]uint64, error,
) {
allCoinID := statedb.GetAllTokenIDForRewardMultiset(curView.rewardStateDB, epoch)
currentBeaconYear, err := blockchain.GetYearOfBeacon(beaconHeight)
if err != nil {
return nil, nil, nil, nil, err
}
percentForIncognitoDAO := getPercentForIncognitoDAOV2(currentBeaconYear)
totalRewardForShardSubset := make([][]map[common.Hash]uint64, curView.ActiveShards)
totalRewards := make([][]map[common.Hash]uint64, curView.ActiveShards)
totalRewardForBeacon := map[common.Hash]uint64{}
totalRewardForIncDAO := map[common.Hash]uint64{}
totalRewardForCustodian := map[common.Hash]uint64{}
for shardID := 0; shardID < curView.ActiveShards; shardID++ {
totalRewardForShardSubset[shardID] = make([]map[common.Hash]uint64, maxSubsetsCommittee)
totalRewards[shardID] = make([]map[common.Hash]uint64, maxSubsetsCommittee)
for subsetID := 0; subsetID < maxSubsetsCommittee; subsetID++ {
if totalRewards[shardID][subsetID] == nil {
totalRewards[shardID][subsetID] = map[common.Hash]uint64{}
}
if totalRewardForShardSubset[shardID][subsetID] == nil {
totalRewardForShardSubset[shardID][subsetID] = map[common.Hash]uint64{}
}
for _, coinID := range allCoinID {
totalRewards[shardID][subsetID][coinID], err = statedb.GetRewardOfShardByEpochMultiset(
curView.rewardStateDB, epoch,
byte(shardID), byte(subsetID), coinID)
if err != nil {
return nil, nil, nil, nil, err
}
if totalRewards[shardID][subsetID][coinID] == 0 {
delete(totalRewards[shardID][subsetID], coinID)
}
}
env := committeestate.NewSplitRewardEnvironmentMultiset(
byte(shardID),
byte(subsetID),
byte(maxSubsetsCommittee),
beaconHeight,
totalRewards[shardID][subsetID],
isSplitRewardForCustodian,
percentCustodianRewards,
percentForIncognitoDAO,
curView.GetBeaconCommittee(),
curView.GetShardCommittee(),
)
Logger.log.Info("[dcs] env.MaxSubsetCommittees:", env.MaxSubsetCommittees)
rewardForBeacon, rewardForShardSubset, rewardForDAO, rewardForCustodian, err := splitRewardRuleProcessor.SplitReward(env)
if err != nil {
return nil, nil, nil, nil, err
}
plusMap(rewardForBeacon, totalRewardForBeacon)
plusMap(rewardForShardSubset, totalRewardForShardSubset[shardID][subsetID])
plusMap(rewardForDAO, totalRewardForIncDAO)
plusMap(rewardForCustodian, totalRewardForCustodian)
}
}
return totalRewardForBeacon, totalRewardForShardSubset, totalRewardForIncDAO, totalRewardForCustodian, nil
}
func (blockchain *BlockChain) calculateReward(
splitRewardRuleProcessor committeestate.SplitRewardRuleProcessor,
curView *BeaconBestState,
numberOfActiveShards int,
beaconHeight uint64,
epoch uint64,
rewardStateDB *statedb.StateDB,
isSplitRewardForCustodian bool,
percentCustodianRewards uint64,
) (map[common.Hash]uint64,
[]map[common.Hash]uint64,
map[common.Hash]uint64,
map[common.Hash]uint64, uint64, error,
) {
allCoinID := statedb.GetAllTokenIDForReward(rewardStateDB, epoch)
currentBeaconYear, err := blockchain.GetYearOfBeacon(beaconHeight)
if err != nil {
return nil, nil, nil, nil, 0, err
}
percentForIncognitoDAO := getPercentForIncognitoDAOV2(currentBeaconYear)
totalRewardForShard := make([]map[common.Hash]uint64, numberOfActiveShards)
totalRewards := make([]map[common.Hash]uint64, numberOfActiveShards)
totalRewardForBeacon := map[common.Hash]uint64{}
totalRewardForIncDAO := map[common.Hash]uint64{}
totalRewardForCustodian := map[common.Hash]uint64{}
delegationReward := uint64(0)
for id := 0; id < numberOfActiveShards; id++ {
if totalRewards[id] == nil {
totalRewards[id] = map[common.Hash]uint64{}
}
if totalRewardForShard[id] == nil {
totalRewardForShard[id] = map[common.Hash]uint64{}
}
for _, coinID := range allCoinID {
totalRewards[id][coinID], err = statedb.GetRewardOfShardByEpoch(rewardStateDB, epoch, byte(id), coinID)
if err != nil {
return nil, nil, nil, nil, 0, err
}
if totalRewards[id][coinID] == 0 {
delete(totalRewards[id], coinID)
}
}
env := committeestate.NewSplitRewardEnvironmentV1(
byte(id),
beaconHeight,
totalRewards[id],
isSplitRewardForCustodian,
percentCustodianRewards,
percentForIncognitoDAO,
numberOfActiveShards,
curView.GetBeaconCommittee(),
curView.GetShardCommittee(),
)
rewardForBeacon, rewardForShard, rewardForDAO, rewardForCustodian, err := splitRewardRuleProcessor.SplitReward(env)
if err != nil {
return nil, nil, nil, nil, 0, err
}
plusMap(rewardForBeacon, totalRewardForBeacon)
plusMap(rewardForShard, totalRewardForShard[id])
plusMap(rewardForDAO, totalRewardForIncDAO)
plusMap(rewardForCustodian, totalRewardForCustodian)
}
return totalRewardForBeacon, totalRewardForShard, totalRewardForIncDAO, totalRewardForCustodian, delegationReward, nil
}
func (blockchain *BlockChain) buildRewardInstructionByEpoch(
curView *BeaconBestState,
blkHeight, epoch uint64,
isSplitRewardForCustodian bool,
percentCustodianRewards uint64,
isSplitRewardForPdex bool,
pdexRewardPercent uint,
blockVersion int,
) ([][]string, map[common.Hash]uint64, uint64, error) {
//Declare variables
var resInst [][]string
var err error
var instRewardForBeacons [][]string
var instRewardForIncDAO [][]string
var instRewardForShards [][]string
var delegationRewardInst [][]string
beaconBestView := blockchain.BeaconChain.GetBestView().(*BeaconBestState)
totalRewardForBeacon := make(map[common.Hash]uint64)
totalRewardForShard := make([]map[common.Hash]uint64, beaconBestView.ActiveShards)
totalRewardForShardSubset := make([][]map[common.Hash]uint64, beaconBestView.ActiveShards)
totalRewardForCustodian := make(map[common.Hash]uint64)
totalRewardForIncDAO := make(map[common.Hash]uint64)
rewardForPdex := uint64(0)
delegationReward := uint64(0)
if blockVersion >= types.BLOCK_PRODUCINGV3_VERSION && blockVersion < types.INSTANT_FINALITY_VERSION_V2 {
splitRewardRuleProcessor := committeestate.GetRewardSplitRule(blockVersion)
totalRewardForBeacon,
totalRewardForShardSubset,
totalRewardForIncDAO,
totalRewardForCustodian,
err = blockchain.calculateRewardMultiset(
splitRewardRuleProcessor,
curView,
uint64(config.Param().BlockTime.MaxBeaconBlockCreation.Seconds()),
MaxSubsetCommittees,
blkHeight,
epoch,
isSplitRewardForCustodian,
percentCustodianRewards,
)
instRewardForShards, err = blockchain.buildInstructionRewardForShardsV3(epoch, totalRewardForShardSubset)
if err != nil {
return nil, nil, rewardForPdex, err
}
} else {
splitRewardRuleProcessor := committeestate.GetRewardSplitRule(blockVersion)
totalRewardForBeacon,
totalRewardForShard,
totalRewardForIncDAO,
totalRewardForCustodian,
delegationReward,
err = blockchain.calculateReward(
splitRewardRuleProcessor,
curView,
curView.ActiveShards, blkHeight, epoch,
curView.GetBeaconRewardStateDB(),
isSplitRewardForCustodian, percentCustodianRewards,
)
instRewardForShards, err = blockchain.buildInstructionRewardForShards(epoch, totalRewardForShard)
if err != nil {
return nil, nil, rewardForPdex, err
}
}
if len(totalRewardForBeacon) > 0 {
committeeOfEpoch, err := blockchain.GetBeaconCommitteeOfEpoch(epoch)
if err != nil {
return nil, nil, rewardForPdex, err
}
committeeGotReward, err := curView.beaconCommitteeState.GetNonSlashingRewardReceiver(committeeOfEpoch)
if err != nil {
return nil, nil, rewardForPdex, err
}
instRewardForBeacons, err = curView.buildInstRewardForBeacons(epoch, totalRewardForBeacon, committeeGotReward)
if err != nil {
return nil, nil, rewardForPdex, err
}
}
if len(totalRewardForIncDAO) > 0 {
prvReward := totalRewardForIncDAO[common.PRVCoinID]
if prvReward > 0 && isSplitRewardForPdex {
temp := new(big.Int).Mul(new(big.Int).SetUint64(prvReward), new(big.Int).SetUint64(uint64(pdexRewardPercent)))
temp = temp.Div(temp, big.NewInt(100))
if !temp.IsUint64() {
return nil, nil, rewardForPdex, errors.New("Reward for Pdex is not uint64")
}
rewardForPdex = temp.Uint64()
totalRewardForIncDAO[common.PRVCoinID] = prvReward - rewardForPdex
}
instRewardForIncDAO, err = blockchain.buildInstRewardForIncDAO(epoch, totalRewardForIncDAO)
if err != nil {
return nil, nil, rewardForPdex, err
}
}
if delegationReward > 0 {
Logger.log.Info("DelegationReward", delegationReward)
delegationRewardInst, err = curView.CalculateDelegationSharePrice(blockchain, delegationReward)
if err != nil {
panic(err)
}
}
resInst = common.AppendSliceString(instRewardForBeacons, instRewardForIncDAO, instRewardForShards, delegationRewardInst)
return resInst, totalRewardForCustodian, rewardForPdex, nil
}
// buildInstRewardForBeacons create reward instruction for beacons
func (beaconBestState *BeaconBestState) buildInstRewardForBeacons(epoch uint64, totalReward map[common.Hash]uint64, rewardReceiver []privacy.PaymentAddress) ([][]string, error) {
resInst := [][]string{}
baseRewards := map[common.Hash]uint64{}
for key, value := range totalReward {
baseRewards[key] = value / uint64(len(rewardReceiver))
}
for _, receiver := range rewardReceiver {
singleInst, err := metadata.BuildInstForBeaconReward(baseRewards, receiver.Pk)
if err != nil {
Logger.log.Errorf("BuildInstForBeaconReward error %+v\n Totalreward: %+v, epoch: %+v, reward: %+v\n", err, totalReward, epoch, baseRewards)
return nil, err
}
resInst = append(resInst, singleInst)
}
return resInst, nil
}
func (blockchain *BlockChain) buildInstRewardForIncDAO(epoch uint64, totalReward map[common.Hash]uint64) ([][]string, error) {
resInst := [][]string{}
devRewardInst, err := metadata.BuildInstForIncDAOReward(totalReward, config.Param().IncognitoDAOAddress)
if err != nil {
Logger.log.Errorf("buildInstRewardForIncDAO error %+v\n Totalreward: %+v, epoch: %+v\n", err, totalReward, epoch)
return nil, err
}
resInst = append(resInst, devRewardInst)
return resInst, nil
}
func (blockchain *BlockChain) buildInstructionRewardForShardsV3(epoch uint64, totalRewards [][]map[common.Hash]uint64) ([][]string, error) {
resInst := [][]string{}
for shardID, v := range totalRewards {
for subsetID, reward := range v {
if len(reward) > 0 {
shardSubsetReward := instruction.NewShardReceiveRewardV3WithValue(reward, epoch, byte(shardID), byte(subsetID))
shardSubsetRewardInst := shardSubsetReward.String()
resInst = append(resInst, shardSubsetRewardInst)
}
}
}
return resInst, nil
}
func (blockchain *BlockChain) buildInstructionRewardForShards(epoch uint64, totalRewards []map[common.Hash]uint64) ([][]string, error) {
resInst := [][]string{}
for i, reward := range totalRewards {
if len(reward) > 0 {
shardRewardInst, err := instruction.NewShardReceiveRewardV1WithValue(reward, epoch, byte(i))
if err != nil {
Logger.log.Errorf("BuildInstForShardReward error %+v\n Totalreward: %+v, epoch: %+v\n; shard:%+v", err, reward, epoch, byte(i))
return nil, err
}
resInst = append(resInst, shardRewardInst...)
}
}
return resInst, nil
}
func (blockchain *BlockChain) buildWithDrawTransactionResponse(view *ShardBestState, txRequest *metadata.Transaction, blkProducerPrivateKey *privacy.PrivateKey, shardID byte) (metadata.Transaction, error) {
if (*txRequest).GetMetadataType() != metadata.WithDrawRewardRequestMeta {
return nil, errors.New("Can not understand this request!")
}
requestDetail := (*txRequest).GetMetadata().(*metadata.WithDrawRewardRequest)
tempPublicKey := base58.Base58Check{}.Encode(requestDetail.PaymentAddress.Pk, common.Base58Version)
amount, err := statedb.GetCommitteeReward(view.GetShardRewardStateDB(), tempPublicKey, requestDetail.TokenID)
if (amount == 0) || (err != nil) {
return nil, errors.New("Not enough reward")
}
responseMeta, err := metadata.NewWithDrawRewardResponse(requestDetail, (*txRequest).Hash())
if err != nil {
return nil, err
}
txParam := transaction.TxSalaryOutputParams{Amount: amount, ReceiverAddress: &requestDetail.PaymentAddress, TokenID: &requestDetail.TokenID}
makeMD := func(c privacy.Coin) metadata.Metadata {
if c != nil && c.GetSharedRandom() != nil {
responseMeta.SetSharedRandom(c.GetSharedRandom().ToBytesS())
}
return responseMeta
}
salaryTx, err := txParam.BuildTxSalary(blkProducerPrivateKey, view.GetCopiedTransactionStateDB(), makeMD)
if err != nil {
return nil, errors.Errorf("cannot init salary tx. Error %v", err)
}
salaryTx.SetType(common.TxRewardType)
return salaryTx, nil
}
func (blockchain *BlockChain) GetBlockTimeByBlockVersion(blkVersion int) (int64, error) {
blockTimeMap := config.Param().BlockTimeParam
defaultBlockTime := blockTimeMap[BLOCKTIME_DEFAULT]
blockTime := defaultBlockTime
for _, anchor := range blockchain.GetBeaconBestState().TSManager.Anchors {
if config.Param().FeatureVersion[anchor.Feature] > int64(blkVersion) {
break
}
blockTime = int64(anchor.Timeslot)
}
return blockTime, nil
}
func (blockchain *BlockChain) GetBasicRewardByVersion(version int) (uint64, error) {
blockTimeMap := config.Param().BlockTimeParam
defaultBlockTime := blockTimeMap[BLOCKTIME_DEFAULT]
curBlockTime, err := blockchain.GetBlockTimeByBlockVersion(version)
if err != nil {
return 0, err
}
return config.Param().BasicReward * uint64(curBlockTime) / uint64(defaultBlockTime), nil
}
func (blockchain *BlockChain) getRewardAmount(blkHeight uint64) uint64 {
blockBeaconInterval := config.Param().BlockTime.MinBeaconBlockInterval.Seconds()
blockInYear := getNoBlkPerYear(uint64(blockBeaconInterval))
n := (blkHeight - 1) / blockInYear
reward := uint64(config.Param().BasicReward)
for ; n > 0; n-- {
reward *= 91
reward /= 100
}
return reward
}
func (blockchain *BlockChain) GetRewardAmount(shardID byte, shardVersion int, shardHeight uint64) (uint64, error) {
if shardVersion < types.ADJUST_BLOCKTIME_VERSION {
return blockchain.getRewardAmount(shardHeight), nil
}
basicReward, err := blockchain.GetBasicRewardByVersion(shardVersion)
if err != nil {
return 0, err
}
yearOfShardHeight, err := blockchain.GetYearOfShard(shardID, shardHeight)
if err != nil {
return 0, err
}
return blockchain.getRewardAmountV2(basicReward, yearOfShardHeight), nil
}
func (blockchain *BlockChain) getRewardAmountV2(basicReward, year uint64) uint64 {
n := year
reward := basicReward
for ; n > 0; n-- {
reward *= 91
reward /= 100
}
return reward
}
func (blockchain *BlockChain) GetYearOfBeacon(blockHeight uint64) (uint64, error) {
bView := blockchain.GetBeaconBestState()
if bView == nil {
return 0, errors.Errorf("Can not get beacon view for get reward amount at block beacon %v", blockHeight)
}
featureManager := bView.TSManager
return getYearOfBlockChain(&featureManager, blockHeight), nil
}
func (blockchain *BlockChain) GetYearOfShard(sID byte, blockHeight uint64) (uint64, error) {
bView := blockchain.GetBeaconBestState()
if bView == nil {
return 0, errors.Errorf("Can not get beacon view for get reward amount at block beacon %v", blockHeight)
}
featureManager := bView.ShardTSManager[sID]
return getYearOfBlockChain(featureManager, blockHeight), nil
}
func getYearOfBlockChain(featuresManager *TSManager, blkHeight uint64) uint64 {
anchors := featuresManager.Anchors
startBlkHeight := uint64(0)
endBlkHeight := uint64(0)
prevBlockTime := config.Param().BlockTimeParam[BLOCKTIME_DEFAULT]
defBlockTime := config.Param().BlockTimeParam[BLOCKTIME_DEFAULT]
totalBlock := uint64(0)
if len(anchors) > 0 {
for _, anchor := range anchors {
endBlkHeight = anchor.BlockHeight
if endBlkHeight > blkHeight {
endBlkHeight = blkHeight
}
// fmt.Printf("%v - %v - %v - %v\n", prevBlockTime, startBlkHeight, endBlkHeight, (endBlkHeight-startBlkHeight)*uint64(prevBlockTime)/uint64(defBlockTime))
totalBlock += (endBlkHeight - startBlkHeight) * uint64(prevBlockTime) / uint64(defBlockTime)
startBlkHeight = endBlkHeight
prevBlockTime = config.Param().BlockTimeParam[anchor.Feature]
}
}
if endBlkHeight < blkHeight {
endBlkHeight = blkHeight
// fmt.Printf("%v - %v - %v - %v\n", prevBlockTime, startBlkHeight, endBlkHeight, (endBlkHeight-startBlkHeight)*uint64(prevBlockTime)/uint64(defBlockTime))
totalBlock += (endBlkHeight - startBlkHeight) * uint64(prevBlockTime) / uint64(defBlockTime)
}
// fmt.Printf("%v - %v\n", totalBlock, getNoBlkPerYear(uint64(defBlockTime)))
return totalBlock / getNoBlkPerYear(uint64(defBlockTime))
}
func getNoBlkPerYear(blockCreationTimeSeconds uint64) uint64 {
return (365.25 * 24 * 60 * 60) / blockCreationTimeSeconds
}
func GetBlockTimeInterval(blkTimeFeature string) int64 {
blockTimeMap := config.Param().BlockTimeParam
if blkTime, ok := blockTimeMap[blkTimeFeature]; ok {
return int64(blkTime)
}
return int64(config.Param().BlockTime.MinBeaconBlockInterval.Seconds())
}
func GetNumberBlkPerYear(blkTimeFeature string) uint64 {
return getNoBlkPerYear(uint64(GetBlockTimeInterval(blkTimeFeature)))
}
func getPercentForIncognitoDAO(blockHeight, blkPerYear uint64) int {
year := (blockHeight - 1) / blkPerYear
if year > (UpperBoundPercentForIncDAO - LowerBoundPercentForIncDAO) {
return LowerBoundPercentForIncDAO
} else {
return UpperBoundPercentForIncDAO - int(year)
}
}
func getPercentForIncognitoDAOV2(year uint64) int {
if year > (UpperBoundPercentForIncDAO - LowerBoundPercentForIncDAO) {
return LowerBoundPercentForIncDAO
} else {
return UpperBoundPercentForIncDAO - int(year)
}
}
// plusMap(src, dst): dst = dst + src
func plusMap(src, dst map[common.Hash]uint64) {
if src != nil {
for key, value := range src {
dst[key] += value
}
}
}
func (blockchain *BlockChain) buildMintDRewardTxsFromBeaconInstructions(
curView *ShardBestState,
beaconBlocks []*types.BeaconBlock,
producerPrivateKey *privacy.PrivateKey,
shardID byte,
) (
[]metadata.Transaction,
error,
) {
mintTxs := []metadata.Transaction{}
mintInfoByRequestPK, err := blockchain.getMintDRewardInfoFromBeaconInstructions(curView, beaconBlocks, shardID)
if err != nil {
return nil, err
}
pks := []string{}
for pk := range mintInfoByRequestPK {
pks = append(pks, pk)
}
sort.Slice(pks, func(i, j int) bool {
return pks[i] < pks[j]
})
for _, pk := range pks {
mintInfo := mintInfoByRequestPK[pk]
mintTx, amount, err := blockchain.buildMintDelegationRewardTransaction(
curView,
&mintInfo,
producerPrivateKey,
shardID,
)
if err != nil {
return nil, err
}
Logger.log.Infof("Build tx mint delegation reward successful, txID %v, amount %v, for pk %v", mintTx.Hash().String(), amount, pk)
mintTxs = append(mintTxs, mintTx)
}
return mintTxs, nil
}
func (blockchain *BlockChain) getMintDRewardInfoFromBeaconInstructions(
curView *ShardBestState,
bBlocks []*types.BeaconBlock,
shardID byte,
) (
map[string]mintDelegationRewardInfo, //pk request -> mint info
error,
) {
mintInsts := getMintDRewardInstructionsFromBeaconBlocks(bBlocks)
if len(mintInsts) == 0 {
return nil, nil
}
mintInfoByRequestID := map[common.Hash]interface{}{}
mintInfoByRequestPK := map[string]mintDelegationRewardInfo{}
for _, inst := range mintInsts {
for idx, requestPayment := range inst.GetPaymentAddressesStruct() {
pkRequest := base58.Base58Check{}.Encode(requestPayment.Pk, common.Base58Version)
txRequestHash := inst.TxRequestIdHashes[idx]
if _, ok := mintInfoByRequestID[txRequestHash]; ok {
Logger.log.Infof("Duplicate request for the same tx request hash %v. Detail info: Pk %v, payment %v, reward amount %v", txRequestHash.String(), pkRequest, inst.PaymentAddresses[idx], inst.RewardAmount[idx])
continue
}
if _, ok := mintInfoByRequestPK[pkRequest]; ok {
Logger.log.Infof("Duplicate request for the same PK %v. Detail info: Tx request hash %v, payment %v, reward amount %v", pkRequest, txRequestHash.String(), inst.PaymentAddresses[idx], inst.RewardAmount[idx])
continue
}
if sID := privacy.GetShardIDFromPublicKey(requestPayment.Pk); sID != shardID {
Logger.log.Infof("SKIP This payment %v (shard %v) is not belong to current shard %v, tx request %v, amount %v", inst.PaymentAddresses[idx], sID, shardID, inst.TxRequestIDs[idx], inst.RewardAmount[idx])
continue
}
_, _, rawTxRequest, err := blockchain.GetTransactionByHashWithShardID(txRequestHash, shardID)
if err != nil {
Logger.log.Infof("SKIP This request (payment %v, dreward amount %v) belong to current shard %v, but can not find tx request %v in database,", inst.PaymentAddresses[idx], inst.RewardAmount[idx], shardID, inst.TxRequestIDs[idx])
continue
}
mInfo := mintDelegationRewardInfo{
TxRequest: rawTxRequest,
ReceiverPayment: requestPayment,
RewardAmount: inst.RewardAmount[idx],
}
mintInfoByRequestID[txRequestHash] = nil
mintInfoByRequestPK[pkRequest] = mInfo
}
}
if len(mintInfoByRequestID) != len(mintInfoByRequestPK) {
return nil, errors.Errorf("Get mint delegation reward info error, total txs request %v is different from total pk request %v", len(mintInfoByRequestID), len(mintInfoByRequestPK))
}
return mintInfoByRequestPK, nil
}
func (blockchain *BlockChain) ValidateMintDRewardTxsFromBeaconInstructions(
curView *ShardBestState,
beaconBlocks []*types.BeaconBlock,
shardBlock *types.ShardBlock,
shardID byte,
) error {
mintInfoByRequestPKGot := map[string]mintDelegationRewardInfo{}
mintInfoByRequestPKExpected, err := blockchain.getMintDRewardInfoFromBeaconInstructions(curView, beaconBlocks, shardID)
if err != nil {
return err
}
mintDRewardTxs := map[common.Hash]struct{}{}
for _, tx := range shardBlock.Body.Transactions {
switch tx.GetMetadata().(type) {
case *metadata.MintDelegationRewardMetadata:
if tx.GetType() != common.TxRewardType {
return errors.Errorf("Wrong transaction type for metadata %v, expected type %v, got %v", tx.GetMetadata().GetType(), common.TxRewardType, tx.GetType())
}
default:
continue
}
md := tx.GetMetadata().(*metadata.MintDelegationRewardMetadata)
h, _ := common.Hash{}.NewHashFromStr(md.TxID)
_, _, txRequest, err := blockchain.GetTransactionByHashWithShardID(*h, shardID)
if err != nil {
err = NewBlockChainError(WrongShardIDError, fmt.Errorf("This request delegation reward tx %v not found in this shard %+v", md.TxID, shardID))
Logger.log.Error(err)
return err
}
receiverPK := base58.Base58Check{}.Encode(md.ReceiverAddress.Pk, common.Base58Version)
mintInfo := mintDelegationRewardInfo{
ReceiverPayment: md.ReceiverAddress,
TxRequest: txRequest,
RewardAmount: md.RewardAmount,
}
mintInfoByRequestPKGot[receiverPK] = mintInfo
txHash := txRequest.Hash()
if _, ok := mintDRewardTxs[*txHash]; ok {
return errors.Errorf("Double tx mint delegation reward from instruction for tx request %+v", tx)
}
mintDRewardTxs[*txHash] = struct{}{}
isMinted, mintCoin, coinID, err := tx.GetTxMintData()
if err != nil || !isMinted {
return errors.Errorf("this is not tx mint for mint delegation reward. Error %v", err)
}
if coinID.String() != common.PRVIDStr {
return errors.Errorf("Delegation reward tx only mints prv. Error token %v", coinID.String())
}
if ok := mintCoin.CheckCoinValid(mintInfo.ReceiverPayment, md.SharedRandom, mintInfo.RewardAmount); !ok {
return errors.Errorf("mint data is invalid: Address %v; Amount %v, StakeAmount: %v", mintInfo.ReceiverPayment, mintCoin.GetValue(), mintInfo.RewardAmount)
}
}
if len(mintInfoByRequestPKExpected) != len(mintInfoByRequestPKGot) {
return errors.Errorf("List mint delegation tx of producer (len %v) and validator (len %v) not match", len(mintInfoByRequestPKGot), len(mintInfoByRequestPKExpected))