-
Notifications
You must be signed in to change notification settings - Fork 52
/
ValidatorSetBase.sol
1198 lines (997 loc) · 50.2 KB
/
ValidatorSetBase.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
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
pragma solidity 0.5.2;
import "../interfaces/IBlockReward.sol";
import "../interfaces/IERC20Minting.sol";
import "../interfaces/IRandom.sol";
import "../interfaces/IValidatorSet.sol";
import "../eternal-storage/OwnedEternalStorage.sol";
import "../libs/SafeMath.sol";
contract ValidatorSetBase is OwnedEternalStorage, IValidatorSet {
using SafeMath for uint256;
// TODO: add a description for each function
// ============================================== Constants =======================================================
// These values must be set before deploy
uint256 public constant MAX_CANDIDATES = 2000;
uint256 public constant MAX_VALIDATORS = 20;
uint256 public constant STAKE_UNIT = 1 ether;
// ================================================ Events ========================================================
/// Issue this log event to signal a desired change in validator set.
/// This will not lead to a change in active validator set until
/// finalizeChange is called.
///
/// Only the last log event of any block can take effect.
/// If a signal is issued while another is being finalized it may never
/// take effect.
///
/// parentHash here should be the parent block hash, or the
/// signal will not be recognized.
event InitiateChange(bytes32 indexed parentHash, address[] newSet);
/// @dev Emitted by `stake` function to signal that the staker made a stake of the specified
/// amount for the specified pool during the specified staking epoch.
/// @param toPoolStakingAddress The pool for which the `staker` made the stake.
/// @param staker The address of staker who made the stake.
/// @param stakingEpoch The serial number of staking epoch during which the stake was made.
/// @param amount The amount of the stake.
event Staked(
address indexed toPoolStakingAddress,
address indexed staker,
uint256 indexed stakingEpoch,
uint256 amount
);
/// @dev Emitted by `moveStake` function to signal that the staker moved the specified
/// amount of a stake from one pool to another during the specified staking epoch.
/// @param fromPoolStakingAddress The pool from which the `staker` moved the stake.
/// @param toPoolStakingAddress The pool to which the `staker` moved the stake.
/// @param staker The address of staker who moved the `amount`.
/// @param stakingEpoch The serial number of staking epoch during which the `amount` was moved.
/// @param amount The amount of the stake.
event StakeMoved(
address fromPoolStakingAddress,
address indexed toPoolStakingAddress,
address indexed staker,
uint256 indexed stakingEpoch,
uint256 amount
);
/// @dev Emitted by `orderWithdraw` function to signal that the staker ordered the withdrawal of the
/// specified amount of their stake from the specified pool during the specified staking epoch.
/// @param fromPoolStakingAddress The pool from which the `staker` ordered withdrawal of `amount`.
/// @param staker The address of staker who ordered withdrawal of `amount`.
/// @param stakingEpoch The serial number of staking epoch during which the order was made.
/// @param amount The amount of the ordered withdrawal. Can be either positive or negative.
event WithdrawalOrdered(
address indexed fromPoolStakingAddress,
address indexed staker,
uint256 indexed stakingEpoch,
int256 amount
);
/// @dev Emitted by `withdraw` function to signal that the staker withdrew the specified
/// amount of a stake from the specified pool during the specified staking epoch.
/// @param fromPoolStakingAddress The pool from which the `staker` withdrew `amount`.
/// @param staker The address of staker who withdrew `amount`.
/// @param stakingEpoch The serial number of staking epoch during which the withdrawal was made.
/// @param amount The amount of the withdrawal.
event Withdrawn(
address indexed fromPoolStakingAddress,
address indexed staker,
uint256 indexed stakingEpoch,
uint256 amount
);
// ============================================== Modifiers =======================================================
modifier gasPriceIsValid() {
require(tx.gasprice != 0);
_;
}
modifier onlyBlockRewardContract() {
require(msg.sender == address(blockRewardContract()));
_;
}
modifier onlySystem() {
require(msg.sender == 0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE);
_;
}
// =============================================== Setters ========================================================
function emitInitiateChange() external {
require(emitInitiateChangeCallable());
(address[] memory newSet, bool newStakingEpoch) = _dequeuePendingValidators();
if (newSet.length > 0) {
emit InitiateChange(blockhash(block.number - 1), newSet);
_setInitiateChangeAllowed(false);
_setQueueValidators(newSet, newStakingEpoch);
}
}
function performOrderedWithdrawals() external onlyBlockRewardContract {
if (validatorSetApplyBlock() != _getCurrentBlockNumber()) return;
uint256 candidateMinStake = getCandidateMinStake();
uint256 delegatorMinStake = getDelegatorMinStake();
IERC20Minting tokenContract = IERC20Minting(erc20TokenContract());
address[] storage validators = addressArrayStorage[PREVIOUS_VALIDATORS];
for (uint256 i = 0; i < validators.length; i++) {
address poolStakingAddress = stakingByMiningAddress(validators[i]);
// Withdraw validator's stake
_performOrderedWithdrawals(poolStakingAddress, poolStakingAddress, candidateMinStake, tokenContract);
// Withdraw delegators' stakes
address[] storage delegators = addressArrayStorage[keccak256(abi.encode(
POOL_DELEGATORS, poolStakingAddress
))];
for (uint256 d = 0; d < delegators.length; d++) {
_performOrderedWithdrawals(poolStakingAddress, delegators[d], delegatorMinStake, tokenContract);
}
// Reset total ordered withdrawal amount for the pool
_setOrderedWithdrawAmountTotal(poolStakingAddress, 0);
}
}
function removePool() public gasPriceIsValid {
address stakingAddress = msg.sender;
// initial validator cannot remove their pool during the initial staking epoch
require(stakingEpoch() > 0 || !isValidator(miningByStakingAddress(stakingAddress)));
_removeFromPools(stakingAddress);
}
function finalizeChange() public onlySystem {
(address[] memory queueValidators, bool newStakingEpoch) = getQueueValidators();
if (validatorSetApplyBlock() == 0 && newStakingEpoch) {
// Apply new validator set after `newValidatorSet()` is called
address[] memory previousValidators = getPreviousValidators();
address[] memory currentValidators = getValidators();
uint256 i;
// Save the previous validator set
for (i = 0; i < previousValidators.length; i++) {
_setIsValidatorOnPreviousEpoch(previousValidators[i], false);
}
for (i = 0; i < currentValidators.length; i++) {
_setIsValidatorOnPreviousEpoch(currentValidators[i], true);
}
_setPreviousValidators(currentValidators);
_applyQueueValidators(queueValidators);
_setValidatorSetApplyBlock(_getCurrentBlockNumber());
// Set a new snapshot inside BlockReward contract
IBlockReward(blockRewardContract()).setSnapshot();
} else if (queueValidators.length > 0) {
// Apply new validator set after `reportMalicious` is called
_applyQueueValidators(queueValidators);
}
_setInitiateChangeAllowed(true);
}
/// @dev Moves the tokens from one pool to another.
/// @param _fromPoolStakingAddress The staking address of the source pool.
/// @param _toPoolStakingAddress The staking address of the target pool.
/// @param _amount The amount to be moved.
function moveStake(
address _fromPoolStakingAddress,
address _toPoolStakingAddress,
uint256 _amount
) public gasPriceIsValid {
require(_fromPoolStakingAddress != _toPoolStakingAddress);
address staker = msg.sender;
_withdraw(_fromPoolStakingAddress, staker, _amount);
_stake(_toPoolStakingAddress, staker, _amount);
emit StakeMoved(_fromPoolStakingAddress, _toPoolStakingAddress, staker, stakingEpoch(), _amount);
}
/// @dev Moves the tokens from staker address to ValidatorSet address
/// on the account of staking address of the pool.
/// @param _toPoolStakingAddress The staking address of the pool.
/// @param _amount The amount of the stake.
function stake(address _toPoolStakingAddress, uint256 _amount) public gasPriceIsValid {
IERC20Minting tokenContract = IERC20Minting(erc20TokenContract());
require(address(tokenContract) != address(0));
address staker = msg.sender;
_stake(_toPoolStakingAddress, staker, _amount);
tokenContract.stake(staker, _amount);
emit Staked(_toPoolStakingAddress, staker, stakingEpoch(), _amount);
}
/// @dev Moves the tokens from ValidatorSet address (from the account of
/// staking address of the pool) to staker address.
/// @param _fromPoolStakingAddress The staking address of the pool.
/// @param _amount The amount of the withdrawal.
function withdraw(address _fromPoolStakingAddress, uint256 _amount) public gasPriceIsValid {
IERC20Minting tokenContract = IERC20Minting(erc20TokenContract());
require(address(tokenContract) != address(0));
address staker = msg.sender;
_withdraw(_fromPoolStakingAddress, staker, _amount);
tokenContract.withdraw(staker, _amount);
emit Withdrawn(_fromPoolStakingAddress, staker, stakingEpoch(), _amount);
}
/// @dev Makes an order of tokens withdrawal from ValidatorSet address
/// (from the account of staking address of the pool) to staker address.
/// The tokens will be automatically withdrawn at the beginning of the
/// next staking epoch.
/// @param _fromPoolStakingAddress The staking address of the pool.
/// @param _amount The amount of the withdrawal. Positive value means
/// that the staker wants to set or increase their withdrawal amount.
/// Negative value means that the staker wants to decrease their
/// withdrawal amount which was set before.
function orderWithdraw(address _fromPoolStakingAddress, int256 _amount) public gasPriceIsValid {
IERC20Minting tokenContract = IERC20Minting(erc20TokenContract());
require(address(tokenContract) != address(0));
require(_fromPoolStakingAddress != address(0));
require(_amount != 0);
require(areStakeAndWithdrawAllowed());
address staker = msg.sender;
// How much can `staker` order for withdrawal from `_fromPoolStakingAddress` at the moment?
require(_amount < 0 || uint256(_amount) <= maxWithdrawOrderAllowed(_fromPoolStakingAddress, staker));
uint256 alreadyOrderedAmount = orderedWithdrawAmount(_fromPoolStakingAddress, staker);
require(_amount > 0 || uint256(-_amount) <= alreadyOrderedAmount);
uint256 newOrderedAmount;
if (_amount > 0) {
newOrderedAmount = alreadyOrderedAmount.add(uint256(_amount));
} else {
newOrderedAmount = alreadyOrderedAmount.sub(uint256(-_amount));
}
uint256 newStakeAmount = stakeAmount(_fromPoolStakingAddress, staker).sub(newOrderedAmount);
// The amount to be withdrawn must be the whole staked amount or
// must not exceed the diff between the entire amount and MIN_STAKE
if (staker == _fromPoolStakingAddress) {
require(newStakeAmount == 0 || newStakeAmount >= getCandidateMinStake());
} else {
require(newStakeAmount == 0 || newStakeAmount >= getDelegatorMinStake());
}
_setOrderedWithdrawAmount(_fromPoolStakingAddress, staker, newOrderedAmount);
// Set total ordered amount for this pool
alreadyOrderedAmount = orderedWithdrawAmountTotal(_fromPoolStakingAddress);
if (_amount > 0) {
newOrderedAmount = alreadyOrderedAmount.add(uint256(_amount));
} else {
newOrderedAmount = alreadyOrderedAmount.sub(uint256(-_amount));
}
_setOrderedWithdrawAmountTotal(_fromPoolStakingAddress, newOrderedAmount);
emit WithdrawalOrdered(_fromPoolStakingAddress, staker, stakingEpoch(), _amount);
}
function setErc20TokenContract(address _erc20TokenContract) public onlyOwner {
require(_erc20TokenContract != address(0));
addressStorage[ERC20_TOKEN_CONTRACT] = _erc20TokenContract;
}
function setCandidateMinStake(uint256 _minStake) public onlyOwner {
_setCandidateMinStake(_minStake);
}
function setDelegatorMinStake(uint256 _minStake) public onlyOwner {
_setDelegatorMinStake(_minStake);
}
// =============================================== Getters ========================================================
function areStakeAndWithdrawAllowed() public view returns(bool);
/// @dev Returns the block number or unix timestamp (depending on
/// consensus algorithm) from which the address will be unbanned.
/// @param _miningAddress The address of participant.
/// @return The block number (for AuRa) or unix timestamp (for HBBFT)
/// from which the address will be unbanned.
function bannedUntil(address _miningAddress) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(BANNED_UNTIL, _miningAddress))
];
}
function blockRewardContract() public view returns(address) {
return addressStorage[BLOCK_REWARD_CONTRACT];
}
// Returns the serial number of validator set changing request
function changeRequestCount() public view returns(uint256) {
return uintStorage[CHANGE_REQUEST_COUNT];
}
function doesPoolExist(address _stakingAddress) public view returns(bool) {
return isPoolActive(_stakingAddress);
}
function emitInitiateChangeCallable() public view returns(bool) {
return initiateChangeAllowed() && uintStorage[QUEUE_PV_LAST] >= uintStorage[QUEUE_PV_FIRST];
}
function erc20TokenContract() public view returns(address) {
return addressStorage[ERC20_TOKEN_CONTRACT];
}
// Returns the list of current pools (candidates and validators)
// (their staking addresses)
function getPools() public view returns(address[] memory) {
return addressArrayStorage[POOLS];
}
// Returns the list of pools which are inactive or banned
// (their staking addresses)
function getPoolsInactive() public view returns(address[] memory) {
return addressArrayStorage[POOLS_INACTIVE];
}
// Returns the set of validators which was actual at the end of previous staking epoch
// (their mining addresses)
function getPreviousValidators() public view returns(address[] memory) {
return addressArrayStorage[PREVIOUS_VALIDATORS];
}
// Returns the set of pending validators
// (their mining addresses)
function getPendingValidators() public view returns(address[] memory) {
return addressArrayStorage[PENDING_VALIDATORS];
}
// Returns the set of validators to be finalized in engine
// (their mining addresses)
function getQueueValidators() public view returns(address[] memory, bool) {
return (addressArrayStorage[QUEUE_VALIDATORS], boolStorage[QUEUE_VALIDATORS_NEW_STAKING_EPOCH]);
}
function getCandidateMinStake() public view returns(uint256) {
return uintStorage[CANDIDATE_MIN_STAKE];
}
function getDelegatorMinStake() public view returns(uint256) {
return uintStorage[DELEGATOR_MIN_STAKE];
}
// Returns the current set of validators (the same as in the engine)
// (their mining addresses)
function getValidators() public view returns(address[] memory) {
return addressArrayStorage[CURRENT_VALIDATORS];
}
function initiateChangeAllowed() public view returns(bool) {
return boolStorage[INITIATE_CHANGE_ALLOWED];
}
// Returns the flag whether the address is in the `pools` array
function isPoolActive(address _stakingAddress) public view returns(bool) {
return boolStorage[keccak256(abi.encode(IS_POOL_ACTIVE, _stakingAddress))];
}
function isReportValidatorValid(address _miningAddress) public view returns(bool) {
bool isValid = isValidator(_miningAddress) && !isValidatorBanned(_miningAddress);
if (stakingEpoch() == 0 || validatorSetApplyBlock() == 0) {
return isValid;
}
if (_getCurrentBlockNumber() - validatorSetApplyBlock() <= 20) {
// The current validator set was applied in engine,
// but we should let the previous validators finish
// reporting malicious validator within a few blocks
bool previousEpochValidator =
isValidatorOnPreviousEpoch(_miningAddress) && !isValidatorBanned(_miningAddress);
return isValid || previousEpochValidator;
}
return isValid;
}
// Returns the flag whether the mining address is in the `currentValidators` array
function isValidator(address _miningAddress) public view returns(bool) {
return boolStorage[keccak256(abi.encode(IS_VALIDATOR, _miningAddress))];
}
// Returns the flag whether the mining address was a validator at the end of previous staking epoch
function isValidatorOnPreviousEpoch(address _miningAddress) public view returns(bool) {
return boolStorage[keccak256(abi.encode(IS_VALIDATOR_ON_PREVIOUS_EPOCH, _miningAddress))];
}
function isValidatorBanned(address _miningAddress) public view returns(bool);
function maxWithdrawAllowed(address _poolStakingAddress, address _staker) public view returns(uint256) {
address miningAddress = miningByStakingAddress(_poolStakingAddress);
if (!_isWithdrawAllowed(miningAddress)) {
return 0;
}
if (!isValidator(miningAddress)) {
// The whole amount can be withdrawn if the pool is not a validator
return stakeAmount(_poolStakingAddress, _staker);
}
// The pool is an active validator, so the staker can
// withdraw staked amount minus already ordered amount
// but no more than amount staked during the current
// staking epoch
uint256 canWithdraw = stakeAmountMinusOrderedWithdraw(_poolStakingAddress, _staker);
uint256 stakedDuringEpoch = stakeAmountByCurrentEpoch(_poolStakingAddress, _staker);
if (canWithdraw > stakedDuringEpoch) {
canWithdraw = stakedDuringEpoch;
}
return canWithdraw;
}
function maxWithdrawOrderAllowed(address _poolStakingAddress, address _staker) public view returns(uint256) {
address miningAddress = miningByStakingAddress(_poolStakingAddress);
if (!_isWithdrawAllowed(miningAddress)) {
return 0;
}
if (!isValidator(miningAddress)) {
// If the pool is a candidate (not a validator yet),
// no one can order withdrawal from `_poolStakingAddress`,
// but anyone can withdraw immediately (see `maxWithdrawAllowed()` getter)
return 0;
}
// It the pool is an active validator, the staker can
// order withdrawal up to their total staking amount
// minus already ordered amount
return stakeAmountMinusOrderedWithdraw(_poolStakingAddress, _staker);
}
function miningByStakingAddress(address _stakingAddress) public view returns(address) {
return addressStorage[keccak256(abi.encode(MINING_BY_STAKING_ADDRESS, _stakingAddress))];
}
/// @dev Prevents sending tokens to `ValidatorSet` contract address
/// directly by `ERC677BridgeTokenRewardable.transferAndCall` function.
function onTokenTransfer(address, uint256, bytes memory) public pure returns(bool) {
return false;
}
function orderedWithdrawAmount(address _poolStakingAddress, address _staker) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(ORDERED_WITHDRAW_AMOUNT, _poolStakingAddress, _staker))
];
}
function orderedWithdrawAmountTotal(address _poolStakingAddress) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(ORDERED_WITHDRAW_AMOUNT_TOTAL, _poolStakingAddress))
];
}
function stakeAmountTotal(address _poolStakingAddress) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(STAKE_AMOUNT_TOTAL, _poolStakingAddress))
];
}
// Returns an index of the pool in the `pools` array
function poolIndex(address _stakingAddress) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(POOL_INDEX, _stakingAddress))
];
}
// Returns an index of the pool in the `poolsInactive` array
function poolInactiveIndex(address _stakingAddress) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(POOL_INACTIVE_INDEX, _stakingAddress))
];
}
// Returns the list of the current delegators in the specified pool
function poolDelegators(address _poolStakingAddress) public view returns(address[] memory) {
return addressArrayStorage[
keccak256(abi.encode(POOL_DELEGATORS, _poolStakingAddress))
];
}
// Returns delegator's index in `poolDelegators` array
function poolDelegatorIndex(address _poolStakingAddress, address _delegator) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(POOL_DELEGATOR_INDEX, _poolStakingAddress, _delegator))
];
}
function randomContract() public view returns(address) {
return addressStorage[RANDOM_CONTRACT];
}
function stakeAmount(address _poolStakingAddress, address _staker) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(STAKE_AMOUNT, _poolStakingAddress, _staker))
];
}
function stakeAmountByCurrentEpoch(address _poolStakingAddress, address _staker)
public
view
returns(uint256)
{
if (!_wasValidatorSetApplied()) {
return 0;
}
return uintStorage[
keccak256(abi.encode(STAKE_AMOUNT_BY_CURRENT_EPOCH, _poolStakingAddress, _staker))
];
}
function stakeAmountMinusOrderedWithdraw(
address _poolStakingAddress,
address _staker
) public view returns(uint256) {
return stakeAmount(_poolStakingAddress, _staker).sub(orderedWithdrawAmount(_poolStakingAddress, _staker));
}
function stakeAmountTotalMinusOrderedWithdraw(address _poolStakingAddress) public view returns(uint256) {
return stakeAmountTotal(_poolStakingAddress).sub(orderedWithdrawAmountTotal(_poolStakingAddress));
}
function stakingByMiningAddress(address _miningAddress) public view returns(address) {
return addressStorage[keccak256(abi.encode(STAKING_BY_MINING_ADDRESS, _miningAddress))];
}
// Returns the internal serial number of staking epoch
function stakingEpoch() public view returns(uint256) {
return uintStorage[STAKING_EPOCH];
}
// Returns the index of validator in the `currentValidators`
function validatorIndex(address _miningAddress) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(VALIDATOR_INDEX, _miningAddress))
];
}
// Returns the block number when `finalizeChange` was called to apply the current validator set
function validatorSetApplyBlock() public view returns(uint256) {
return uintStorage[VALIDATOR_SET_APPLY_BLOCK];
}
// =============================================== Private ========================================================
bytes32 internal constant BLOCK_REWARD_CONTRACT = keccak256("blockRewardContract");
bytes32 internal constant CANDIDATE_MIN_STAKE = keccak256("candidateMinStake");
bytes32 internal constant CHANGE_REQUEST_COUNT = keccak256("changeRequestCount");
bytes32 internal constant CURRENT_VALIDATORS = keccak256("currentValidators");
bytes32 internal constant DELEGATOR_MIN_STAKE = keccak256("delegatorMinStake");
bytes32 internal constant ERC20_TOKEN_CONTRACT = keccak256("erc20TokenContract");
bytes32 internal constant INITIATE_CHANGE_ALLOWED = keccak256("initiateChangeAllowed");
bytes32 internal constant PENDING_VALIDATORS = keccak256("pendingValidators");
bytes32 internal constant POOLS = keccak256("pools");
bytes32 internal constant POOLS_INACTIVE = keccak256("poolsInactive");
bytes32 internal constant POOLS_EMPTY = keccak256("poolsEmpty");
bytes32 internal constant POOLS_NON_EMPTY = keccak256("poolsNonEmpty");
bytes32 internal constant PREVIOUS_VALIDATORS = keccak256("previousValidators");
bytes32 internal constant QUEUE_PV_FIRST = keccak256("queuePVFirst");
bytes32 internal constant QUEUE_PV_LAST = keccak256("queuePVLast");
bytes32 internal constant QUEUE_VALIDATORS = keccak256("queueValidators");
bytes32 internal constant QUEUE_VALIDATORS_NEW_STAKING_EPOCH = keccak256("queueValidatorsNewStakingEpoch");
bytes32 internal constant RANDOM_CONTRACT = keccak256("randomContract");
bytes32 internal constant STAKING_EPOCH = keccak256("stakingEpoch");
bytes32 internal constant VALIDATOR_SET_APPLY_BLOCK = keccak256("validatorSetApplyBlock");
bytes32 internal constant BANNED_UNTIL = "bannedUntil";
bytes32 internal constant IS_POOL_ACTIVE = "isPoolActive";
bytes32 internal constant IS_VALIDATOR = "isValidator";
bytes32 internal constant IS_VALIDATOR_ON_PREVIOUS_EPOCH = "isValidatorOnPreviousEpoch";
bytes32 internal constant MINING_BY_STAKING_ADDRESS = "miningByStakingAddress";
bytes32 internal constant POOL_DELEGATORS = "poolDelegators";
bytes32 internal constant POOL_DELEGATOR_INDEX = "poolDelegatorIndex";
bytes32 internal constant POOL_INDEX = "poolIndex";
bytes32 internal constant POOL_INACTIVE_INDEX = "poolInactiveIndex";
bytes32 internal constant QUEUE_PV_BLOCK = "queuePVBlock";
bytes32 internal constant QUEUE_PV_LIST = "queuePVList";
bytes32 internal constant QUEUE_PV_NEW_EPOCH = "queuePVNewEpoch";
bytes32 internal constant STAKE_AMOUNT = "stakeAmount";
bytes32 internal constant STAKE_AMOUNT_BY_CURRENT_EPOCH = "stakeAmountByCurrentEpoch";
bytes32 internal constant STAKE_AMOUNT_TOTAL = "stakeAmountTotal";
bytes32 internal constant STAKING_BY_MINING_ADDRESS = "stakingByMiningAddress";
bytes32 internal constant VALIDATOR_INDEX = "validatorIndex";
bytes32 internal constant ORDERED_WITHDRAW_AMOUNT = "orderedWithdrawAmount";
bytes32 internal constant ORDERED_WITHDRAW_AMOUNT_TOTAL = "orderedWithdrawAmountTotal";
// Adds `_stakingAddress` to the array of pools
function _addToPools(address _stakingAddress) internal {
if (!doesPoolExist(_stakingAddress)) {
address[] storage pools = addressArrayStorage[POOLS];
_setPoolIndex(_stakingAddress, pools.length);
pools.push(_stakingAddress);
require(pools.length <= _getMaxCandidates());
_setIsPoolActive(_stakingAddress, true);
}
_removeFromPoolsInactive(_stakingAddress);
}
// Adds `_stakingAddress` to the array of inactive pools
function _addToPoolsInactive(address _stakingAddress) internal {
address[] storage poolsInactive = addressArrayStorage[POOLS_INACTIVE];
if (poolsInactive.length == 0 || poolsInactive[poolInactiveIndex(_stakingAddress)] != _stakingAddress) {
_setPoolInactiveIndex(_stakingAddress, poolsInactive.length);
poolsInactive.push(_stakingAddress);
}
}
// Removes `_stakingAddress` from the array of pools
function _removeFromPools(address _stakingAddress) internal {
uint256 indexToRemove = poolIndex(_stakingAddress);
address[] storage pools = addressArrayStorage[POOLS];
if (pools.length > 0 && pools[indexToRemove] == _stakingAddress) {
pools[indexToRemove] = pools[pools.length - 1];
_setPoolIndex(pools[indexToRemove], indexToRemove);
pools.length--;
_setPoolIndex(_stakingAddress, 0);
_setIsPoolActive(_stakingAddress, false);
if (stakeAmountTotal(_stakingAddress) != 0) {
_addToPoolsInactive(_stakingAddress);
}
}
}
// Removes `_stakingAddress` from the array of inactive pools
function _removeFromPoolsInactive(address _stakingAddress) internal {
address[] storage poolsInactive = addressArrayStorage[POOLS_INACTIVE];
uint256 indexToRemove = poolInactiveIndex(_stakingAddress);
if (poolsInactive.length > 0 && poolsInactive[indexToRemove] == _stakingAddress) {
poolsInactive[indexToRemove] = poolsInactive[poolsInactive.length - 1];
_setPoolInactiveIndex(poolsInactive[indexToRemove], indexToRemove);
poolsInactive.length--;
_setPoolInactiveIndex(_stakingAddress, 0);
}
}
function _applyQueueValidators(address[] memory _queueValidators) internal {
address[] memory prevValidators = getValidators();
uint256 i;
// Clear indexes for old validator set
for (i = 0; i < prevValidators.length; i++) {
_setValidatorIndex(prevValidators[i], 0);
_setIsValidator(prevValidators[i], false);
}
_setCurrentValidators(_queueValidators);
// Set indexes for new validator set
for (i = 0; i < _queueValidators.length; i++) {
_setValidatorIndex(_queueValidators[i], i);
_setIsValidator(_queueValidators[i], true);
}
}
function _banValidator(address _miningAddress) internal {
uintStorage[
keccak256(abi.encode(BANNED_UNTIL, _miningAddress))
] = _banUntil();
}
function _enqueuePendingValidators(bool _newStakingEpoch) internal {
uint256 queueFirst = uintStorage[QUEUE_PV_FIRST];
uint256 queueLast = uintStorage[QUEUE_PV_LAST];
for (uint256 i = queueLast; i >= queueFirst; i--) {
if (uintStorage[keccak256(abi.encode(QUEUE_PV_BLOCK, i))] == block.number) {
addressArrayStorage[keccak256(abi.encode(QUEUE_PV_LIST, i))] = getPendingValidators();
if (_newStakingEpoch) {
boolStorage[keccak256(abi.encode(QUEUE_PV_NEW_EPOCH, i))] = true;
}
return;
}
}
queueLast++;
addressArrayStorage[keccak256(abi.encode(QUEUE_PV_LIST, queueLast))] = getPendingValidators();
boolStorage[keccak256(abi.encode(QUEUE_PV_NEW_EPOCH, queueLast))] = _newStakingEpoch;
uintStorage[keccak256(abi.encode(QUEUE_PV_BLOCK, queueLast))] = block.number;
uintStorage[QUEUE_PV_LAST] = queueLast;
}
function _dequeuePendingValidators() internal returns(address[] memory newSet, bool newStakingEpoch) {
uint256 queueFirst = uintStorage[QUEUE_PV_FIRST];
uint256 queueLast = uintStorage[QUEUE_PV_LAST];
if (queueLast < queueFirst) {
newSet = new address[](0);
newStakingEpoch = false;
} else {
newSet = addressArrayStorage[keccak256(abi.encode(QUEUE_PV_LIST, queueFirst))];
newStakingEpoch = boolStorage[keccak256(abi.encode(QUEUE_PV_NEW_EPOCH, queueFirst))];
delete addressArrayStorage[keccak256(abi.encode(QUEUE_PV_LIST, queueFirst))];
delete boolStorage[keccak256(abi.encode(QUEUE_PV_NEW_EPOCH, queueFirst))];
delete uintStorage[keccak256(abi.encode(QUEUE_PV_BLOCK, queueFirst))];
uintStorage[QUEUE_PV_FIRST]++;
}
}
function _incrementChangeRequestCount() internal {
uintStorage[CHANGE_REQUEST_COUNT]++;
}
function _incrementStakingEpoch() internal {
uintStorage[STAKING_EPOCH]++;
}
function _initialize(
address _blockRewardContract,
address _randomContract,
address _erc20TokenContract,
address[] memory _initialMiningAddresses,
address[] memory _initialStakingAddresses,
uint256 _delegatorMinStake,
uint256 _candidateMinStake
) internal {
require(_getCurrentBlockNumber() == 0); // initialization must be done on genesis block
require(_blockRewardContract != address(0));
require(_randomContract != address(0));
require(_initialMiningAddresses.length > 0);
require(_initialMiningAddresses.length == _initialStakingAddresses.length);
require(_delegatorMinStake != 0);
require(_candidateMinStake != 0);
addressStorage[BLOCK_REWARD_CONTRACT] = _blockRewardContract;
addressStorage[RANDOM_CONTRACT] = _randomContract;
addressStorage[ERC20_TOKEN_CONTRACT] = _erc20TokenContract;
address[] storage currentValidators = addressArrayStorage[CURRENT_VALIDATORS];
address[] storage pendingValidators = addressArrayStorage[PENDING_VALIDATORS];
require(currentValidators.length == 0);
// Add initial validators to the `currentValidators` array
for (uint256 i = 0; i < _initialMiningAddresses.length; i++) {
currentValidators.push(_initialMiningAddresses[i]);
pendingValidators.push(_initialMiningAddresses[i]);
_setValidatorIndex(_initialMiningAddresses[i], i);
_setIsValidator(_initialMiningAddresses[i], true);
_addToPools(_initialStakingAddresses[i]);
_setMiningAddress(_initialStakingAddresses[i], _initialMiningAddresses[i]);
}
_setDelegatorMinStake(_delegatorMinStake);
_setCandidateMinStake(_candidateMinStake);
_setValidatorSetApplyBlock(1);
uintStorage[QUEUE_PV_FIRST] = 1;
uintStorage[QUEUE_PV_LAST] = 0;
}
function _newValidatorSet() internal {
address[] memory pools = getPools();
uint256 i;
// Filter pools and leave only non-empty
delete addressArrayStorage[POOLS_NON_EMPTY];
delete addressArrayStorage[POOLS_EMPTY];
for (i = 0; i < pools.length; i++) {
if (stakeAmountMinusOrderedWithdraw(pools[i], pools[i]) > 0) {
addressArrayStorage[POOLS_NON_EMPTY].push(pools[i]);
} else {
addressArrayStorage[POOLS_EMPTY].push(pools[i]);
}
}
pools = addressArrayStorage[POOLS_NON_EMPTY];
// Choose new validators
if (pools.length <= MAX_VALIDATORS) {
if (pools.length > 0) {
_setPendingValidators(pools);
}
} else {
uint256 randomNumber = uint256(keccak256(abi.encode(IRandom(randomContract()).getCurrentSeed())));
address[] memory poolsLocal = pools;
uint256 poolsLocalLength = poolsLocal.length;
uint256[] memory likelihood = new uint256[](poolsLocalLength);
address[] memory newValidators = new address[](MAX_VALIDATORS);
uint256 likelihoodSum = 0;
for (i = 0; i < poolsLocalLength; i++) {
likelihood[i] = stakeAmountTotalMinusOrderedWithdraw(poolsLocal[i]).mul(100).div(STAKE_UNIT);
likelihoodSum = likelihoodSum.add(likelihood[i]);
}
for (i = 0; i < MAX_VALIDATORS; i++) {
uint256 randomPoolIndex = _getRandomIndex(
likelihood,
likelihoodSum,
randomNumber
);
newValidators[i] = poolsLocal[randomPoolIndex];
likelihoodSum -= likelihood[randomPoolIndex];
poolsLocalLength--;
poolsLocal[randomPoolIndex] = poolsLocal[poolsLocalLength];
likelihood[randomPoolIndex] = likelihood[poolsLocalLength];
randomNumber = uint256(keccak256(abi.encode(randomNumber)));
}
_setPendingValidators(newValidators);
}
// From this moment `getPendingValidators()` will return the new validator set
// Increment counters
_incrementStakingEpoch();
_incrementChangeRequestCount();
_enqueuePendingValidators(true);
_setValidatorSetApplyBlock(0);
}
function _performOrderedWithdrawals(
address _poolStakingAddress,
address _staker,
uint256 _minAllowedStake,
IERC20Minting _tokenContract
) internal {
uint256 orderedAmount = orderedWithdrawAmount(_poolStakingAddress, _staker);
if (orderedAmount > 0) {
uint256 currentStakeAmount = stakeAmount(_poolStakingAddress, _staker);
if (
currentStakeAmount >= orderedAmount &&
address(_tokenContract) != address(0) &&
orderedAmount <= _tokenContract.balanceOf(address(this)) &&
_withdraw(
_poolStakingAddress,
_staker,
orderedAmount,
currentStakeAmount,
currentStakeAmount - orderedAmount,
_minAllowedStake
)
) {
_tokenContract.withdraw(_staker, orderedAmount);
}
_setOrderedWithdrawAmount(_poolStakingAddress, _staker, 0);
}
_setStakeAmountByCurrentEpoch(_poolStakingAddress, _staker, 0);
}
function _removeMaliciousValidator(address _miningAddress) internal returns(bool) {
uint256 i;
address stakingAddress = stakingByMiningAddress(_miningAddress);
// Remove malicious validator from `pools`
_removeFromPools(stakingAddress);
// Ban the malicious validator for the next 3 months
_banValidator(_miningAddress);
// Remove all ordered withdrawals from the pool of this validator
address[] memory delegators = poolDelegators(stakingAddress);
for (i = 0; i < delegators.length; i++) {
_setOrderedWithdrawAmount(stakingAddress, delegators[i], 0);
_setStakeAmountByCurrentEpoch(stakingAddress, delegators[i], 0);
}
_setOrderedWithdrawAmount(stakingAddress, stakingAddress, 0);
_setOrderedWithdrawAmountTotal(stakingAddress, 0);
_setStakeAmountByCurrentEpoch(stakingAddress, stakingAddress, 0);
address[] storage miningAddresses = addressArrayStorage[PENDING_VALIDATORS];
bool isPendingValidator = false;
for (i = 0; i < miningAddresses.length; i++) {
if (miningAddresses[i] == _miningAddress) {
isPendingValidator = true;
break;
}
}
if (isPendingValidator) {
// Remove the malicious validator from `pendingValidators`
miningAddresses[i] = miningAddresses[miningAddresses.length - 1];
miningAddresses.length--;
return true;
}
return false;
}
function _setCurrentValidators(address[] memory _miningAddresses) internal {
addressArrayStorage[CURRENT_VALIDATORS] = _miningAddresses;
}
function _setInitiateChangeAllowed(bool _allowed) internal {
boolStorage[INITIATE_CHANGE_ALLOWED] = _allowed;
}
function _setIsPoolActive(address _stakingAddress, bool _isPoolActive) internal {
boolStorage[keccak256(abi.encode(IS_POOL_ACTIVE, _stakingAddress))] = _isPoolActive;
}
function _setIsValidator(address _miningAddress, bool _isValidator) internal {
boolStorage[keccak256(abi.encode(IS_VALIDATOR, _miningAddress))] = _isValidator;
}
function _setIsValidatorOnPreviousEpoch(address _miningAddress, bool _isValidator) internal {
boolStorage[keccak256(abi.encode(IS_VALIDATOR_ON_PREVIOUS_EPOCH, _miningAddress))] = _isValidator;
}
function _setPendingValidators(address[] memory _stakingAddresses) internal {
uint256 i;
delete addressArrayStorage[PENDING_VALIDATORS];
for (i = 0; i < _stakingAddresses.length; i++) {
addressArrayStorage[PENDING_VALIDATORS].push(miningByStakingAddress(_stakingAddresses[i]));
}
for (i = 0; i < addressArrayStorage[POOLS_EMPTY].length; i++) {
_removeFromPools(addressArrayStorage[POOLS_EMPTY][i]);
}
}
function _setPoolIndex(address _stakingAddress, uint256 _index) internal {
uintStorage[
keccak256(abi.encode(POOL_INDEX, _stakingAddress))
] = _index;
}
function _setPoolInactiveIndex(address _stakingAddress, uint256 _index) internal {
uintStorage[
keccak256(abi.encode(POOL_INACTIVE_INDEX, _stakingAddress))
] = _index;
}
function _setPoolDelegatorIndex(address _poolStakingAddress, address _delegator, uint256 _index) internal {
uintStorage[keccak256(abi.encode(POOL_DELEGATOR_INDEX, _poolStakingAddress, _delegator))] = _index;
}
function _setQueueValidators(address[] memory _miningAddresses, bool _newStakingEpoch) internal {
addressArrayStorage[QUEUE_VALIDATORS] = _miningAddresses;
boolStorage[QUEUE_VALIDATORS_NEW_STAKING_EPOCH] = _newStakingEpoch;
}
// Add `_delegator` to the array of pool's delegators
function _addPoolDelegator(address _poolStakingAddress, address _delegator) internal {
address[] storage delegators = addressArrayStorage[
keccak256(abi.encode(POOL_DELEGATORS, _poolStakingAddress))
];
_setPoolDelegatorIndex(_poolStakingAddress, _delegator, delegators.length);
delegators.push(_delegator);
}
// Remove `_delegator` from the array of pool's delegators
function _removePoolDelegator(address _poolStakingAddress, address _delegator) internal {
address[] storage delegators = addressArrayStorage[
keccak256(abi.encode(POOL_DELEGATORS, _poolStakingAddress))
];
if (delegators.length == 0) return;
uint256 indexToRemove = poolDelegatorIndex(_poolStakingAddress, _delegator);
delegators[indexToRemove] = delegators[delegators.length - 1];
_setPoolDelegatorIndex(_poolStakingAddress, delegators[indexToRemove], indexToRemove);
delegators.length--;
_setPoolDelegatorIndex(_poolStakingAddress, _delegator, 0);
}
function _setMiningAddress(address _stakingAddress, address _miningAddress) internal {
require(_stakingAddress != address(0));
require(_miningAddress != address(0));
require(_stakingAddress != _miningAddress);
require(miningByStakingAddress(_stakingAddress) == address(0));
require(miningByStakingAddress(_miningAddress) == address(0));