-
Notifications
You must be signed in to change notification settings - Fork 35
/
SFC.sol
808 lines (667 loc) · 37.5 KB
/
SFC.sol
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
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "./StakerConstants.sol";
import "../ownership/Ownable.sol";
import "../version/Version.sol";
import "./NodeDriver.sol";
import "./StakeTokenizer.sol";
/**
* @dev Stakers contract defines data structure and methods for validators / validators.
*/
contract SFC is Initializable, Ownable, StakersConstants, Version {
using SafeMath for uint256;
/**
* @dev The staking for validation
*/
struct Validator {
uint256 status;
uint256 deactivatedTime;
uint256 deactivatedEpoch;
uint256 receivedStake;
uint256 createdEpoch;
uint256 createdTime;
address auth;
}
NodeDriverAuth internal node;
uint256 public currentSealedEpoch;
mapping(uint256 => Validator) public getValidator;
mapping(address => uint256) public getValidatorID;
mapping(uint256 => bytes) public getValidatorPubkey;
uint256 public lastValidatorID;
uint256 public totalStake;
uint256 public totalActiveStake;
uint256 public totalSlashedStake;
struct Rewards {
uint256 lockupExtraReward;
uint256 lockupBaseReward;
uint256 unlockedReward;
}
mapping(address => mapping(uint256 => Rewards)) internal _rewardsStash; // addr, validatorID -> Rewards
mapping(address => mapping(uint256 => uint256)) public stashedRewardsUntilEpoch;
struct WithdrawalRequest {
uint256 epoch;
uint256 time;
uint256 amount;
}
mapping(address => mapping(uint256 => mapping(uint256 => WithdrawalRequest))) public getWithdrawalRequest;
struct LockedDelegation {
uint256 lockedStake;
uint256 fromEpoch;
uint256 endTime;
uint256 duration;
}
mapping(address => mapping(uint256 => uint256)) public getStake;
mapping(address => mapping(uint256 => LockedDelegation)) public getLockupInfo;
mapping(address => mapping(uint256 => Rewards)) public getStashedLockupRewards;
struct EpochSnapshot {
mapping(uint256 => uint256) receivedStake;
mapping(uint256 => uint256) accumulatedRewardPerToken;
mapping(uint256 => uint256) accumulatedUptime;
mapping(uint256 => uint256) accumulatedOriginatedTxsFee;
mapping(uint256 => uint256) offlineTime;
mapping(uint256 => uint256) offlineBlocks;
uint256[] validatorIDs;
uint256 endTime;
uint256 epochFee;
uint256 totalBaseRewardWeight;
uint256 totalTxRewardWeight;
uint256 baseRewardPerSecond;
uint256 totalStake;
uint256 totalSupply;
}
uint256 public baseRewardPerSecond;
uint256 public totalSupply;
mapping(uint256 => EpochSnapshot) public getEpochSnapshot;
uint256 offlinePenaltyThresholdBlocksNum;
uint256 offlinePenaltyThresholdTime;
mapping(uint256 => uint256) public slashingRefundRatio; // validator ID -> (slashing refund ratio)
address public stakeTokenizerAddress;
function isNode(address addr) internal view returns (bool) {
return addr == address(node);
}
modifier onlyDriver() {
require(isNode(msg.sender), "caller is not the NodeDriverAuth contract");
_;
}
event CreatedValidator(uint256 indexed validatorID, address indexed auth, uint256 createdEpoch, uint256 createdTime);
event DeactivatedValidator(uint256 indexed validatorID, uint256 deactivatedEpoch, uint256 deactivatedTime);
event ChangedValidatorStatus(uint256 indexed validatorID, uint256 status);
event Delegated(address indexed delegator, uint256 indexed toValidatorID, uint256 amount);
event Undelegated(address indexed delegator, uint256 indexed toValidatorID, uint256 indexed wrID, uint256 amount);
event Withdrawn(address indexed delegator, uint256 indexed toValidatorID, uint256 indexed wrID, uint256 amount);
event ClaimedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 lockupExtraReward, uint256 lockupBaseReward, uint256 unlockedReward);
event RestakedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 lockupExtraReward, uint256 lockupBaseReward, uint256 unlockedReward);
event LockedUpStake(address indexed delegator, uint256 indexed validatorID, uint256 duration, uint256 amount);
event UnlockedStake(address indexed delegator, uint256 indexed validatorID, uint256 amount, uint256 penalty);
event UpdatedBaseRewardPerSec(uint256 value);
event UpdatedOfflinePenaltyThreshold(uint256 blocksNum, uint256 period);
event UpdatedSlashingRefundRatio(uint256 indexed validatorID, uint256 refundRatio);
/*
Getters
*/
function currentEpoch() public view returns (uint256) {
return currentSealedEpoch + 1;
}
function getEpochValidatorIDs(uint256 epoch) public view returns (uint256[] memory) {
return getEpochSnapshot[epoch].validatorIDs;
}
function getEpochReceivedStake(uint256 epoch, uint256 validatorID) public view returns (uint256) {
return getEpochSnapshot[epoch].receivedStake[validatorID];
}
function getEpochAccumulatedRewardPerToken(uint256 epoch, uint256 validatorID) public view returns (uint256) {
return getEpochSnapshot[epoch].accumulatedRewardPerToken[validatorID];
}
function getEpochAccumulatedUptime(uint256 epoch, uint256 validatorID) public view returns (uint256) {
return getEpochSnapshot[epoch].accumulatedUptime[validatorID];
}
function getEpochAccumulatedOriginatedTxsFee(uint256 epoch, uint256 validatorID) public view returns (uint256) {
return getEpochSnapshot[epoch].accumulatedOriginatedTxsFee[validatorID];
}
function getEpochOfflineTime(uint256 epoch, uint256 validatorID) public view returns (uint256) {
return getEpochSnapshot[epoch].offlineTime[validatorID];
}
function getEpochOfflineBlocks(uint256 epoch, uint256 validatorID) public view returns (uint256) {
return getEpochSnapshot[epoch].offlineBlocks[validatorID];
}
function rewardsStash(address delegator, uint256 validatorID) public view returns (uint256) {
Rewards memory stash = _rewardsStash[delegator][validatorID];
return stash.lockupBaseReward.add(stash.lockupExtraReward).add(stash.unlockedReward);
}
function getLockedStake(address delegator, uint256 toValidatorID) public view returns (uint256) {
if (!isLockedUp(delegator, toValidatorID)) {
return 0;
}
return getLockupInfo[delegator][toValidatorID].lockedStake;
}
/*
Constructor
*/
function initialize(uint256 sealedEpoch, uint256 _totalSupply, address nodeDriver, address owner) external initializer {
Ownable.initialize(owner);
currentSealedEpoch = sealedEpoch;
node = NodeDriverAuth(nodeDriver);
totalSupply = _totalSupply;
baseRewardPerSecond = 6.183414351851851852 * 1e18;
offlinePenaltyThresholdBlocksNum = 1000;
offlinePenaltyThresholdTime = 3 days;
getEpochSnapshot[sealedEpoch].endTime = _now();
}
function setGenesisValidator(address auth, uint256 validatorID, bytes calldata pubkey, uint256 status, uint256 createdEpoch, uint256 createdTime, uint256 deactivatedEpoch, uint256 deactivatedTime) external onlyDriver {
_rawCreateValidator(auth, validatorID, pubkey, status, createdEpoch, createdTime, deactivatedEpoch, deactivatedTime);
if (validatorID > lastValidatorID) {
lastValidatorID = validatorID;
}
}
function setGenesisDelegation(address delegator, uint256 toValidatorID, uint256 stake, uint256 lockedStake, uint256 lockupFromEpoch, uint256 lockupEndTime, uint256 lockupDuration, uint256 earlyUnlockPenalty, uint256 rewards) external onlyDriver {
_rawDelegate(delegator, toValidatorID, stake);
_rewardsStash[delegator][toValidatorID].unlockedReward = rewards;
_mintNativeToken(stake);
if (lockedStake != 0) {
require(lockedStake <= stake, "locked stake is greater than the whole stake");
LockedDelegation storage ld = getLockupInfo[delegator][toValidatorID];
ld.lockedStake = lockedStake;
ld.fromEpoch = lockupFromEpoch;
ld.endTime = lockupEndTime;
ld.duration = lockupDuration;
getStashedLockupRewards[delegator][toValidatorID].lockupExtraReward = earlyUnlockPenalty;
emit LockedUpStake(delegator, toValidatorID, lockupDuration, lockedStake);
}
}
/*
Methods
*/
function createValidator(bytes calldata pubkey) external payable {
require(msg.value >= minSelfStake(), "insufficient self-stake");
require(pubkey.length > 0, "empty pubkey");
_createValidator(msg.sender, pubkey);
_delegate(msg.sender, lastValidatorID, msg.value);
}
function _createValidator(address auth, bytes memory pubkey) internal {
uint256 validatorID = ++lastValidatorID;
_rawCreateValidator(auth, validatorID, pubkey, OK_STATUS, currentEpoch(), _now(), 0, 0);
}
function _rawCreateValidator(address auth, uint256 validatorID, bytes memory pubkey, uint256 status, uint256 createdEpoch, uint256 createdTime, uint256 deactivatedEpoch, uint256 deactivatedTime) internal {
require(getValidatorID[auth] == 0, "validator already exists");
getValidatorID[auth] = validatorID;
getValidator[validatorID].status = status;
getValidator[validatorID].createdEpoch = createdEpoch;
getValidator[validatorID].createdTime = createdTime;
getValidator[validatorID].deactivatedTime = deactivatedTime;
getValidator[validatorID].deactivatedEpoch = deactivatedEpoch;
getValidator[validatorID].auth = auth;
getValidatorPubkey[validatorID] = pubkey;
emit CreatedValidator(validatorID, auth, createdEpoch, createdTime);
if (deactivatedEpoch != 0) {
emit DeactivatedValidator(validatorID, deactivatedEpoch, deactivatedTime);
}
if (status != 0) {
emit ChangedValidatorStatus(validatorID, status);
}
}
function getSelfStake(uint256 validatorID) public view returns (uint256) {
return getStake[getValidator[validatorID].auth][validatorID];
}
function _checkDelegatedStakeLimit(uint256 validatorID) internal view returns (bool) {
return getValidator[validatorID].receivedStake <= getSelfStake(validatorID).mul(maxDelegatedRatio()).div(Decimal.unit());
}
function delegate(uint256 toValidatorID) external payable {
_delegate(msg.sender, toValidatorID, msg.value);
}
function _delegate(address delegator, uint256 toValidatorID, uint256 amount) internal {
require(_validatorExists(toValidatorID), "validator doesn't exist");
require(getValidator[toValidatorID].status == OK_STATUS, "validator isn't active");
_rawDelegate(delegator, toValidatorID, amount);
require(_checkDelegatedStakeLimit(toValidatorID), "validator's delegations limit is exceeded");
}
function _rawDelegate(address delegator, uint256 toValidatorID, uint256 amount) internal {
require(amount > 0, "zero amount");
_stashRewards(delegator, toValidatorID);
getStake[delegator][toValidatorID] = getStake[delegator][toValidatorID].add(amount);
uint256 origStake = getValidator[toValidatorID].receivedStake;
getValidator[toValidatorID].receivedStake = origStake.add(amount);
totalStake = totalStake.add(amount);
if (getValidator[toValidatorID].status == OK_STATUS) {
totalActiveStake = totalActiveStake.add(amount);
}
_syncValidator(toValidatorID, origStake == 0);
emit Delegated(delegator, toValidatorID, amount);
}
function _setValidatorDeactivated(uint256 validatorID, uint256 status) internal {
if (getValidator[validatorID].status == OK_STATUS && status != OK_STATUS) {
totalActiveStake = totalActiveStake.sub(getValidator[validatorID].receivedStake);
}
// status as a number is proportional to severity
if (status > getValidator[validatorID].status) {
getValidator[validatorID].status = status;
if (getValidator[validatorID].deactivatedEpoch == 0) {
getValidator[validatorID].deactivatedEpoch = currentEpoch();
getValidator[validatorID].deactivatedTime = _now();
emit DeactivatedValidator(validatorID, getValidator[validatorID].deactivatedEpoch, getValidator[validatorID].deactivatedTime);
}
emit ChangedValidatorStatus(validatorID, status);
}
}
function undelegate(uint256 toValidatorID, uint256 wrID, uint256 amount) public {
address delegator = msg.sender;
_stashRewards(delegator, toValidatorID);
require(amount > 0, "zero amount");
require(amount <= getUnlockedStake(delegator, toValidatorID), "not enough unlocked stake");
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");
require(getWithdrawalRequest[delegator][toValidatorID][wrID].amount == 0, "wrID already exists");
getStake[delegator][toValidatorID] -= amount;
getValidator[toValidatorID].receivedStake = getValidator[toValidatorID].receivedStake.sub(amount);
totalStake = totalStake.sub(amount);
if (getValidator[toValidatorID].status == OK_STATUS) {
totalActiveStake = totalActiveStake.sub(amount);
}
uint256 selfStakeAfterwards = getSelfStake(toValidatorID);
if (selfStakeAfterwards != 0) {
require(selfStakeAfterwards >= minSelfStake(), "insufficient self-stake");
require(_checkDelegatedStakeLimit(toValidatorID), "validator's delegations limit is exceeded");
} else {
_setValidatorDeactivated(toValidatorID, WITHDRAWN_BIT);
}
getWithdrawalRequest[delegator][toValidatorID][wrID].amount = amount;
getWithdrawalRequest[delegator][toValidatorID][wrID].epoch = currentEpoch();
getWithdrawalRequest[delegator][toValidatorID][wrID].time = _now();
_syncValidator(toValidatorID, false);
emit Undelegated(delegator, toValidatorID, wrID, amount);
}
function isSlashed(uint256 validatorID) view public returns (bool) {
return getValidator[validatorID].status & CHEATER_MASK != 0;
}
function getSlashingPenalty(uint256 amount, bool isCheater, uint256 refundRatio) internal pure returns (uint256 penalty) {
if (!isCheater || refundRatio >= Decimal.unit()) {
return 0;
}
// round penalty upwards (ceiling) to prevent dust amount attacks
penalty = amount.mul(Decimal.unit() - refundRatio).div(Decimal.unit()).add(1);
if (penalty > amount) {
return amount;
}
return penalty;
}
function withdraw(uint256 toValidatorID, uint256 wrID) public {
address payable delegator = msg.sender;
WithdrawalRequest memory request = getWithdrawalRequest[delegator][toValidatorID][wrID];
require(request.epoch != 0, "request doesn't exist");
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");
uint256 requestTime = request.time;
uint256 requestEpoch = request.epoch;
if (getValidator[toValidatorID].deactivatedTime != 0 && getValidator[toValidatorID].deactivatedTime < requestTime) {
requestTime = getValidator[toValidatorID].deactivatedTime;
requestEpoch = getValidator[toValidatorID].deactivatedEpoch;
}
require(_now() >= requestTime + withdrawalPeriodTime(), "not enough time passed");
require(currentEpoch() >= requestEpoch + withdrawalPeriodEpochs(), "not enough epochs passed");
uint256 amount = getWithdrawalRequest[delegator][toValidatorID][wrID].amount;
bool isCheater = isSlashed(toValidatorID);
uint256 penalty = getSlashingPenalty(amount, isCheater, slashingRefundRatio[toValidatorID]);
delete getWithdrawalRequest[delegator][toValidatorID][wrID];
totalSlashedStake += penalty;
require(amount > penalty, "stake is fully slashed");
// It's important that we transfer after erasing (protection against Re-Entrancy)
delegator.transfer(amount.sub(penalty));
emit Withdrawn(delegator, toValidatorID, wrID, amount);
}
function deactivateValidator(uint256 validatorID, uint256 status) external onlyDriver {
require(status != OK_STATUS, "wrong status");
_setValidatorDeactivated(validatorID, status);
_syncValidator(validatorID, false);
}
function _calcRawValidatorEpochBaseReward(uint256 epochDuration, uint256 _baseRewardPerSecond, uint256 baseRewardWeight, uint256 totalBaseRewardWeight) internal pure returns (uint256) {
if (baseRewardWeight == 0) {
return 0;
}
uint256 totalReward = epochDuration.mul(_baseRewardPerSecond);
return totalReward.mul(baseRewardWeight).div(totalBaseRewardWeight);
}
function _calcRawValidatorEpochTxReward(uint256 epochFee, uint256 txRewardWeight, uint256 totalTxRewardWeight) internal pure returns (uint256) {
if (txRewardWeight == 0) {
return 0;
}
uint256 txReward = epochFee.mul(txRewardWeight).div(totalTxRewardWeight);
// fee reward except contractCommission
return txReward.mul(Decimal.unit() - contractCommission()).div(Decimal.unit());
}
function _calcValidatorCommission(uint256 rawReward, uint256 commission) internal pure returns (uint256) {
return rawReward.mul(commission).div(Decimal.unit());
}
function _highestPayableEpoch(uint256 validatorID) internal view returns (uint256) {
if (getValidator[validatorID].deactivatedEpoch != 0) {
if (currentSealedEpoch < getValidator[validatorID].deactivatedEpoch) {
return currentSealedEpoch;
}
return getValidator[validatorID].deactivatedEpoch;
}
return currentSealedEpoch;
}
// find highest epoch such that _isLockedUpAtEpoch returns true (using binary search)
function _highestLockupEpoch(address delegator, uint256 validatorID) internal view returns (uint256) {
uint256 l = getLockupInfo[delegator][validatorID].fromEpoch;
uint256 r = currentSealedEpoch;
if (_isLockedUpAtEpoch(delegator, validatorID, r)) {
return r;
}
if (!_isLockedUpAtEpoch(delegator, validatorID, l)) {
return 0;
}
if (l > r) {
return 0;
}
while (l < r) {
uint256 m = (l + r) / 2;
if (_isLockedUpAtEpoch(delegator, validatorID, m)) {
l = m + 1;
} else {
r = m;
}
}
if (r == 0) {
return 0;
}
return r - 1;
}
function _scaleLockupReward(uint256 fullReward, uint256 lockupDuration) internal pure returns (Rewards memory reward) {
reward = Rewards(0, 0, 0);
if (lockupDuration != 0) {
uint256 maxLockupExtraRatio = Decimal.unit() - unlockedRewardRatio();
uint256 lockupExtraRatio = maxLockupExtraRatio.mul(lockupDuration).div(maxLockupDuration());
uint256 totalScaledReward = fullReward.mul(unlockedRewardRatio() + lockupExtraRatio).div(Decimal.unit());
reward.lockupBaseReward = fullReward.mul(unlockedRewardRatio()).div(Decimal.unit());
reward.lockupExtraReward = totalScaledReward - reward.lockupBaseReward;
} else {
reward.unlockedReward = fullReward.mul(unlockedRewardRatio()).div(Decimal.unit());
}
return reward;
}
function sumRewards(Rewards memory a, Rewards memory b) internal pure returns (Rewards memory) {
return Rewards(a.lockupExtraReward.add(b.lockupExtraReward), a.lockupBaseReward.add(b.lockupBaseReward), a.unlockedReward.add(b.unlockedReward));
}
function sumRewards(Rewards memory a, Rewards memory b, Rewards memory c) internal pure returns (Rewards memory) {
return sumRewards(sumRewards(a, b), c);
}
function _newRewards(address delegator, uint256 toValidatorID) internal view returns (Rewards memory) {
uint256 stashedUntil = stashedRewardsUntilEpoch[delegator][toValidatorID];
uint256 payableUntil = _highestPayableEpoch(toValidatorID);
uint256 lockedUntil = _highestLockupEpoch(delegator, toValidatorID);
if (lockedUntil > payableUntil) {
lockedUntil = payableUntil;
}
if (lockedUntil < stashedUntil) {
lockedUntil = stashedUntil;
}
LockedDelegation storage ld = getLockupInfo[delegator][toValidatorID];
uint256 wholeStake = getStake[delegator][toValidatorID];
uint256 unlockedStake = wholeStake.sub(ld.lockedStake);
uint256 fullReward;
// count reward for locked stake during lockup epochs
fullReward = _newRewardsOf(ld.lockedStake, toValidatorID, stashedUntil, lockedUntil);
Rewards memory plReward = _scaleLockupReward(fullReward, ld.duration);
// count reward for unlocked stake during lockup epochs
fullReward = _newRewardsOf(unlockedStake, toValidatorID, stashedUntil, lockedUntil);
Rewards memory puReward = _scaleLockupReward(fullReward, 0);
// count lockup reward for unlocked stake during unlocked epochs
fullReward = _newRewardsOf(wholeStake, toValidatorID, lockedUntil, payableUntil);
Rewards memory wuReward = _scaleLockupReward(fullReward, 0);
return sumRewards(plReward, puReward, wuReward);
}
function _newRewardsOf(uint256 stakeAmount, uint256 toValidatorID, uint256 fromEpoch, uint256 toEpoch) internal view returns (uint256) {
if (fromEpoch >= toEpoch) {
return 0;
}
uint256 stashedRate = getEpochSnapshot[fromEpoch].accumulatedRewardPerToken[toValidatorID];
uint256 currentRate = getEpochSnapshot[toEpoch].accumulatedRewardPerToken[toValidatorID];
return currentRate.sub(stashedRate).mul(stakeAmount).div(Decimal.unit());
}
function _pendingRewards(address delegator, uint256 toValidatorID) internal view returns (Rewards memory) {
Rewards memory reward = _newRewards(delegator, toValidatorID);
return sumRewards(_rewardsStash[delegator][toValidatorID], reward);
}
function pendingRewards(address delegator, uint256 toValidatorID) public view returns (uint256) {
Rewards memory reward = _pendingRewards(delegator, toValidatorID);
return reward.unlockedReward.add(reward.lockupBaseReward).add(reward.lockupExtraReward);
}
function stashRewards(address delegator, uint256 toValidatorID) external {
require(_stashRewards(delegator, toValidatorID), "nothing to stash");
}
function _stashRewards(address delegator, uint256 toValidatorID) internal returns (bool updated) {
Rewards memory nonStashedReward = _newRewards(delegator, toValidatorID);
stashedRewardsUntilEpoch[delegator][toValidatorID] = _highestPayableEpoch(toValidatorID);
_rewardsStash[delegator][toValidatorID] = sumRewards(_rewardsStash[delegator][toValidatorID], nonStashedReward);
getStashedLockupRewards[delegator][toValidatorID] = sumRewards(getStashedLockupRewards[delegator][toValidatorID], nonStashedReward);
return true;
}
function _mintNativeToken(uint256 amount) internal {
// balance will be increased after the transaction is processed
node.incBalance(address(this), amount);
}
function _claimRewards(address delegator, uint256 toValidatorID) internal returns (Rewards memory rewards) {
_stashRewards(delegator, toValidatorID);
rewards = _rewardsStash[delegator][toValidatorID];
uint256 totalReward = rewards.unlockedReward.add(rewards.lockupBaseReward).add(rewards.lockupExtraReward);
require(totalReward != 0, "zero rewards");
delete _rewardsStash[delegator][toValidatorID];
// It's important that we mint after erasing (protection against Re-Entrancy)
_mintNativeToken(totalReward);
return rewards;
}
function claimRewards(uint256 toValidatorID) public {
address payable delegator = msg.sender;
Rewards memory rewards = _claimRewards(delegator, toValidatorID);
// It's important that we transfer after erasing (protection against Re-Entrancy)
delegator.transfer(rewards.lockupExtraReward.add(rewards.lockupBaseReward).add(rewards.unlockedReward));
emit ClaimedRewards(delegator, toValidatorID, rewards.lockupExtraReward, rewards.lockupBaseReward, rewards.unlockedReward);
}
function restakeRewards(uint256 toValidatorID) public {
address delegator = msg.sender;
Rewards memory rewards = _claimRewards(delegator, toValidatorID);
uint256 lockupReward = rewards.lockupExtraReward.add(rewards.lockupBaseReward);
_delegate(delegator, toValidatorID, lockupReward.add(rewards.unlockedReward));
getLockupInfo[delegator][toValidatorID].lockedStake += lockupReward;
emit RestakedRewards(delegator, toValidatorID, rewards.lockupExtraReward, rewards.lockupBaseReward, rewards.unlockedReward);
}
// _syncValidator updates the validator data on node
function _syncValidator(uint256 validatorID, bool syncPubkey) public {
require(_validatorExists(validatorID), "validator doesn't exist");
// emit special log for node
uint256 weight = getValidator[validatorID].receivedStake;
if (getValidator[validatorID].status != OK_STATUS) {
weight = 0;
}
node.updateValidatorWeight(validatorID, weight);
if (syncPubkey && weight != 0) {
node.updateValidatorPubkey(validatorID, getValidatorPubkey[validatorID]);
}
}
function _validatorExists(uint256 validatorID) view internal returns (bool) {
return getValidator[validatorID].createdTime != 0;
}
function offlinePenaltyThreshold() public view returns (uint256 blocksNum, uint256 time) {
return (offlinePenaltyThresholdBlocksNum, offlinePenaltyThresholdTime);
}
function updateBaseRewardPerSecond(uint256 value) onlyOwner external {
require(value <= 32.967977168935185184 * 1e18, "too large reward per second");
baseRewardPerSecond = value;
emit UpdatedBaseRewardPerSec(value);
}
function updateOfflinePenaltyThreshold(uint256 blocksNum, uint256 time) onlyOwner external {
offlinePenaltyThresholdTime = time;
offlinePenaltyThresholdBlocksNum = blocksNum;
emit UpdatedOfflinePenaltyThreshold(blocksNum, time);
}
function updateSlashingRefundRatio(uint256 validatorID, uint256 refundRatio) onlyOwner external {
require(isSlashed(validatorID), "validator isn't slashed");
require(refundRatio <= Decimal.unit(), "must be less than or equal to 1.0");
slashingRefundRatio[validatorID] = refundRatio;
emit UpdatedSlashingRefundRatio(validatorID, refundRatio);
}
function updateStakeTokenizerAddress(address addr) onlyOwner external {
stakeTokenizerAddress = addr;
}
function _sealEpoch_offline(EpochSnapshot storage snapshot, uint256[] memory validatorIDs, uint256[] memory offlineTime, uint256[] memory offlineBlocks) internal {
// mark offline nodes
for (uint256 i = 0; i < validatorIDs.length; i++) {
if (offlineBlocks[i] > offlinePenaltyThresholdBlocksNum && offlineTime[i] >= offlinePenaltyThresholdTime) {
_setValidatorDeactivated(validatorIDs[i], OFFLINE_BIT);
_syncValidator(validatorIDs[i], false);
}
// log data
snapshot.offlineTime[validatorIDs[i]] = offlineTime[i];
snapshot.offlineBlocks[validatorIDs[i]] = offlineBlocks[i];
}
}
struct _SealEpochRewardsCtx {
uint256[] baseRewardWeights;
uint256 totalBaseRewardWeight;
uint256[] txRewardWeights;
uint256 totalTxRewardWeight;
uint256 epochDuration;
uint256 epochFee;
}
function _sealEpoch_rewards(EpochSnapshot storage snapshot, uint256[] memory validatorIDs, uint256[] memory uptimes, uint256[] memory originatedTxsFee) internal {
_SealEpochRewardsCtx memory ctx = _SealEpochRewardsCtx(new uint[](validatorIDs.length), 0, new uint[](validatorIDs.length), 0, 0, 0);
EpochSnapshot storage prevSnapshot = getEpochSnapshot[currentEpoch().sub(1)];
ctx.epochDuration = 1;
if (_now() > prevSnapshot.endTime) {
ctx.epochDuration = _now() - prevSnapshot.endTime;
}
for (uint256 i = 0; i < validatorIDs.length; i++) {
// txRewardWeight = {originatedTxsFee} * {uptime}
// originatedTxsFee is roughly proportional to {uptime} * {stake}, so the whole formula is roughly
// {stake} * {uptime} ^ 2
ctx.txRewardWeights[i] = originatedTxsFee[i] * uptimes[i] / ctx.epochDuration;
ctx.totalTxRewardWeight = ctx.totalTxRewardWeight.add(ctx.txRewardWeights[i]);
ctx.epochFee = ctx.epochFee.add(originatedTxsFee[i]);
}
for (uint256 i = 0; i < validatorIDs.length; i++) {
// baseRewardWeight = {stake} * {uptime ^ 2}
ctx.baseRewardWeights[i] = (((snapshot.receivedStake[validatorIDs[i]] * uptimes[i]) / ctx.epochDuration) * uptimes[i]) / ctx.epochDuration;
ctx.totalBaseRewardWeight = ctx.totalBaseRewardWeight.add(ctx.baseRewardWeights[i]);
}
for (uint256 i = 0; i < validatorIDs.length; i++) {
uint256 rawReward = _calcRawValidatorEpochBaseReward(ctx.epochDuration, baseRewardPerSecond, ctx.baseRewardWeights[i], ctx.totalBaseRewardWeight);
rawReward = rawReward.add(_calcRawValidatorEpochTxReward(ctx.epochFee, ctx.txRewardWeights[i], ctx.totalTxRewardWeight));
uint256 validatorID = validatorIDs[i];
address validatorAddr = getValidator[validatorID].auth;
// accounting validator's commission
uint256 commissionRewardFull = _calcValidatorCommission(rawReward, validatorCommission());
uint256 selfStake = getStake[validatorAddr][validatorID];
if (selfStake != 0) {
uint256 lCommissionRewardFull = (commissionRewardFull * getLockedStake(validatorAddr, validatorID)) / selfStake;
uint256 uCommissionRewardFull = commissionRewardFull - lCommissionRewardFull;
Rewards memory lCommissionReward = _scaleLockupReward(lCommissionRewardFull, getLockupInfo[validatorAddr][validatorID].duration);
Rewards memory uCommissionReward = _scaleLockupReward(uCommissionRewardFull, 0);
_rewardsStash[validatorAddr][validatorID] = sumRewards(_rewardsStash[validatorAddr][validatorID], lCommissionReward, uCommissionReward);
getStashedLockupRewards[validatorAddr][validatorID] = sumRewards(getStashedLockupRewards[validatorAddr][validatorID], lCommissionReward, uCommissionReward);
}
// accounting reward per token for delegators
uint256 delegatorsReward = rawReward - commissionRewardFull;
// note: use latest stake for the sake of rewards distribution accuracy, not snapshot.receivedStake
uint256 receivedStake = getValidator[validatorID].receivedStake;
uint256 rewardPerToken = 0;
if (receivedStake != 0) {
rewardPerToken = (delegatorsReward * Decimal.unit()) / receivedStake;
}
snapshot.accumulatedRewardPerToken[validatorID] = prevSnapshot.accumulatedRewardPerToken[validatorID] + rewardPerToken;
//
snapshot.accumulatedOriginatedTxsFee[validatorID] = prevSnapshot.accumulatedOriginatedTxsFee[validatorID] + originatedTxsFee[i];
snapshot.accumulatedUptime[validatorID] = prevSnapshot.accumulatedUptime[validatorID] + uptimes[i];
}
snapshot.epochFee = ctx.epochFee;
snapshot.totalBaseRewardWeight = ctx.totalBaseRewardWeight;
snapshot.totalTxRewardWeight = ctx.totalTxRewardWeight;
}
function sealEpoch(uint256[] calldata offlineTime, uint256[] calldata offlineBlocks, uint256[] calldata uptimes, uint256[] calldata originatedTxsFee) external onlyDriver {
EpochSnapshot storage snapshot = getEpochSnapshot[currentEpoch()];
uint256[] memory validatorIDs = snapshot.validatorIDs;
_sealEpoch_offline(snapshot, validatorIDs, offlineTime, offlineBlocks);
_sealEpoch_rewards(snapshot, validatorIDs, uptimes, originatedTxsFee);
currentSealedEpoch = currentEpoch();
snapshot.endTime = _now();
snapshot.baseRewardPerSecond = baseRewardPerSecond;
snapshot.totalSupply = totalSupply;
}
function sealEpochValidators(uint256[] calldata nextValidatorIDs) external onlyDriver {
// fill data for the next snapshot
EpochSnapshot storage snapshot = getEpochSnapshot[currentEpoch()];
for (uint256 i = 0; i < nextValidatorIDs.length; i++) {
uint256 validatorID = nextValidatorIDs[i];
uint256 receivedStake = getValidator[validatorID].receivedStake;
snapshot.receivedStake[validatorID] = receivedStake;
snapshot.totalStake = snapshot.totalStake.add(receivedStake);
}
snapshot.validatorIDs = nextValidatorIDs;
}
function _now() internal view returns (uint256) {
return block.timestamp;
}
function epochEndTime(uint256 epoch) view internal returns (uint256) {
return getEpochSnapshot[epoch].endTime;
}
function isLockedUp(address delegator, uint256 toValidatorID) view public returns (bool) {
return getLockupInfo[delegator][toValidatorID].endTime != 0 && _now() <= getLockupInfo[delegator][toValidatorID].endTime;
}
function _isLockedUpAtEpoch(address delegator, uint256 toValidatorID, uint256 epoch) internal view returns (bool) {
return getLockupInfo[delegator][toValidatorID].fromEpoch <= epoch && epochEndTime(epoch) <= getLockupInfo[delegator][toValidatorID].endTime;
}
function _checkAllowedToWithdraw(address delegator, uint256 toValidatorID) internal view returns(bool) {
if (stakeTokenizerAddress == address(0)) {
return true;
}
return StakeTokenizer(stakeTokenizerAddress).allowedToWithdrawStake(delegator, toValidatorID);
}
function getUnlockedStake(address delegator, uint256 toValidatorID) public view returns (uint256) {
if (!isLockedUp(delegator, toValidatorID)) {
return getStake[delegator][toValidatorID];
}
return getStake[delegator][toValidatorID].sub(getLockupInfo[delegator][toValidatorID].lockedStake);
}
function lockStake(uint256 toValidatorID, uint256 lockupDuration, uint256 amount) public {
address delegator = msg.sender;
require(amount > 0, "zero amount");
require(!isLockedUp(delegator, toValidatorID), "already locked up");
require(amount <= getUnlockedStake(delegator, toValidatorID), "not enough stake");
require(getValidator[toValidatorID].status == OK_STATUS, "validator isn't active");
require(lockupDuration >= minLockupDuration() && lockupDuration <= maxLockupDuration(), "incorrect duration");
uint256 endTime = _now().add(lockupDuration);
address validatorAddr = getValidator[toValidatorID].auth;
if (delegator != validatorAddr) {
require(getLockupInfo[validatorAddr][toValidatorID].endTime >= endTime, "validator lockup period will end earlier");
}
_stashRewards(delegator, toValidatorID);
LockedDelegation storage ld = getLockupInfo[delegator][toValidatorID];
ld.lockedStake = amount;
ld.fromEpoch = currentEpoch();
ld.endTime = endTime;
ld.duration = lockupDuration;
delete getStashedLockupRewards[delegator][toValidatorID];
emit LockedUpStake(delegator, toValidatorID, lockupDuration, amount);
}
function _popDelegationUnlockPenalty(address delegator, uint256 toValidatorID, uint256 unlockAmount, uint256 totalAmount) internal returns (uint256) {
uint256 lockupExtraRewardShare = getStashedLockupRewards[delegator][toValidatorID].lockupExtraReward.mul(unlockAmount).div(totalAmount);
uint256 lockupBaseRewardShare = getStashedLockupRewards[delegator][toValidatorID].lockupBaseReward.mul(unlockAmount).div(totalAmount);
uint256 totalPenaltyAmount = lockupExtraRewardShare + lockupBaseRewardShare / 2;
uint256 penalty = totalPenaltyAmount.mul(unlockAmount).div(totalAmount);
getStashedLockupRewards[delegator][toValidatorID].lockupExtraReward = getStashedLockupRewards[delegator][toValidatorID].lockupExtraReward.sub(lockupExtraRewardShare);
getStashedLockupRewards[delegator][toValidatorID].lockupBaseReward = getStashedLockupRewards[delegator][toValidatorID].lockupBaseReward.sub(lockupBaseRewardShare);
if (penalty >= unlockAmount) {
penalty = unlockAmount;
}
return penalty;
}
function unlockStake(uint256 toValidatorID, uint256 amount) external returns (uint256) {
address delegator = msg.sender;
LockedDelegation storage ld = getLockupInfo[delegator][toValidatorID];
require(amount > 0, "zero amount");
require(isLockedUp(delegator, toValidatorID), "not locked up");
require(amount <= ld.lockedStake, "not enough locked stake");
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");
_stashRewards(delegator, toValidatorID);
uint256 penalty = _popDelegationUnlockPenalty(delegator, toValidatorID, amount, ld.lockedStake);
ld.lockedStake -= amount;
getStake[delegator][toValidatorID] -= penalty;
emit UnlockedStake(delegator, toValidatorID, amount, penalty);
return penalty;
}
}