-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathValidatorSetBase.sol
800 lines (666 loc) · 30.9 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
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 ========================================================
/// 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 toPool 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 toPool,
address indexed staker,
uint256 indexed stakingEpoch,
uint256 amount
);
/// 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 fromPool The pool from which the `staker` moved the stake.
/// @param toPool 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 fromPool,
address indexed toPool,
address indexed staker,
uint256 indexed stakingEpoch,
uint256 amount
);
/// 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 fromPool 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 fromPool,
address indexed staker,
uint256 indexed stakingEpoch,
uint256 amount
);
// ============================================== Modifiers =======================================================
modifier gasPriceIsValid() {
require(tx.gasprice != 0);
_;
}
modifier onlySystem() {
require(msg.sender == 0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE);
_;
}
// =============================================== Setters ========================================================
function removePool() public gasPriceIsValid {
if (stakingEpoch() == 0 && isValidator(msg.sender)) {
revert(); // initial validator cannot remove his pool during the initial staking epoch
}
_removeFromPools(msg.sender);
}
function finalizeChange() public onlySystem {
if (validatorSetApplyBlock() == 0) {
// 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);
_applyPendingValidators();
_setValidatorSetApplyBlock(_getCurrentBlockNumber());
// Set a new snapshot inside BlockReward contract
IBlockReward(blockRewardContract()).setSnapshot();
} else {
// Apply new validator set after `reportMalicious` is called
_applyPendingValidators();
}
}
function moveStake(address _fromPool, address _toPool, uint256 _amount) public gasPriceIsValid {
require(_fromPool != _toPool);
address staker = msg.sender;
_withdraw(_fromPool, staker, _amount);
_stake(_toPool, staker, _amount);
emit StakeMoved(_fromPool, _toPool, staker, stakingEpoch(), _amount);
}
function stake(address _toPool, uint256 _amount) public gasPriceIsValid {
IERC20Minting tokenContract = IERC20Minting(erc20TokenContract());
require(address(tokenContract) != address(0));
address staker = msg.sender;
_stake(_toPool, staker, _amount);
tokenContract.stake(staker, _amount);
emit Staked(_toPool, staker, stakingEpoch(), _amount);
}
function withdraw(address _fromPool, uint256 _amount) public gasPriceIsValid {
IERC20Minting tokenContract = IERC20Minting(erc20TokenContract());
require(address(tokenContract) != address(0));
address staker = msg.sender;
_withdraw(_fromPool, staker, _amount);
tokenContract.withdraw(staker, _amount);
emit Withdrawn(_fromPool, staker, stakingEpoch(), _amount);
}
function clearStakeHistory(address _pool, address[] memory _staker, uint256 _stakingEpoch) public onlyOwner {
require(_stakingEpoch <= stakingEpoch().sub(2));
for (uint256 i = 0; i < _staker.length; i++) {
_setStakeAmountByEpoch(_pool, _staker[i], _stakingEpoch, 0);
}
}
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 ========================================================
// Returns the unix timestamp from which the address will be unbanned
function bannedUntil(address _who) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(BANNED_UNTIL, _who))
];
}
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 _who) public view returns(bool) {
return isPoolActive(_who);
}
function erc20TokenContract() public view returns(address) {
return addressStorage[ERC20_TOKEN_CONTRACT];
}
// Returns the list of current pools (candidates and validators)
function getPools() public view returns(address[] memory) {
return addressArrayStorage[POOLS];
}
// Returns the list of pools which are inactive or banned
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
function getPreviousValidators() public view returns(address[] memory) {
return addressArrayStorage[PREVIOUS_VALIDATORS];
}
// Returns the set of validators to be finalized in engine
function getPendingValidators() public view returns(address[] memory) {
return addressArrayStorage[PENDING_VALIDATORS];
}
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)
function getValidators() public view returns(address[] memory) {
return addressArrayStorage[CURRENT_VALIDATORS];
}
// Returns the flag whether the address in the `pools` array
function isPoolActive(address _who) public view returns(bool) {
return boolStorage[keccak256(abi.encode(IS_POOL_ACTIVE, _who))];
}
function isReportValidatorValid(address _validator) public view returns(bool) {
bool isValid = isValidator(_validator) && !isValidatorBanned(_validator);
if (stakingEpoch() == 0 || validatorSetApplyBlock() == 0) {
return isValid;
}
if (_getCurrentBlockNumber() - validatorSetApplyBlock() <= 3) {
// 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(_validator) && !isValidatorBanned(_validator);
return isValid || previousEpochValidator;
}
return isValid;
}
// Returns the flag whether the address in the `currentValidators` array
function isValidator(address _who) public view returns(bool) {
return boolStorage[keccak256(abi.encode(IS_VALIDATOR, _who))];
}
// Returns the flag whether the address was a validator at the end of previous staking epoch
function isValidatorOnPreviousEpoch(address _who) public view returns(bool) {
return boolStorage[keccak256(abi.encode(IS_VALIDATOR_ON_PREVIOUS_EPOCH, _who))];
}
function isValidatorBanned(address _validator) public view returns(bool) {
return now < bannedUntil(_validator);
}
function maxWithdrawAllowed(address _pool, address _staker) public view returns(uint256) {
bool poolIsValidator = isValidator(_pool);
if (_staker == _pool && poolIsValidator) {
// A pool can't withdraw while it is a validator
return 0;
}
if (isValidatorBanned(_pool)) {
// No one can withdraw from `_pool` until the ban is expired
return 0;
}
if (!_areStakeAndWithdrawAllowed()) {
return 0;
}
if (!poolIsValidator) {
// The whole amount can be withdrawn if the pool is not a validator
return stakeAmount(_pool, _staker);
}
if (isValidatorOnPreviousEpoch(_pool)) {
// The pool was also a validator on the previous staking epoch, so
// the staker can't withdraw amount staked on the previous staking epoch
return stakeAmount(_pool, _staker).sub(
stakeAmountByEpoch(_pool, _staker, stakingEpoch().sub(1)) // stakingEpoch is always > 0 here
);
} else {
// The pool wasn't a validator on the previous staking epoch, so
// the staker can only withdraw amount staked on the current staking epoch
return stakeAmountByEpoch(_pool, _staker, stakingEpoch());
}
}
// Returns an index of the pool in the `pools` array
function poolIndex(address _who) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(POOL_INDEX, _who))
];
}
// Returns an index of the pool in the `poolsInactive` array
function poolInactiveIndex(address _who) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(POOL_INACTIVE_INDEX, _who))
];
}
// Returns the list of current delegators in the specified pool
function poolDelegators(address _pool) public view returns(address[] memory) {
return addressArrayStorage[
keccak256(abi.encode(POOL_DELEGATORS, _pool))
];
}
// Returns delegator's index in `poolDelegators` array
function poolDelegatorIndex(address _pool, address _delegator) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(POOL_DELEGATOR_INDEX, _pool, _delegator))
];
}
function randomContract() public view returns(address) {
return addressStorage[RANDOM_CONTRACT];
}
function stakeAmount(address _pool, address _staker) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(STAKE_AMOUNT, _pool, _staker))
];
}
function stakeAmountByEpoch(address _pool, address _staker, uint256 _stakingEpoch)
public
view
returns(uint256)
{
return uintStorage[
keccak256(abi.encode(STAKE_AMOUNT_BY_EPOCH, _pool, _staker, _stakingEpoch))
];
}
function stakeAmountTotal(address _pool) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(STAKE_AMOUNT_TOTAL, _pool))
];
}
// 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 _validator) public view returns(uint256) {
return uintStorage[
keccak256(abi.encode(VALIDATOR_INDEX, _validator))
];
}
// Returns the block number when `finalizeChange` was called to apply the 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 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 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 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 STAKE_AMOUNT = "stakeAmount";
bytes32 internal constant STAKE_AMOUNT_BY_EPOCH = "stakeAmountByEpoch";
bytes32 internal constant STAKE_AMOUNT_TOTAL = "stakeAmountTotal";
bytes32 internal constant VALIDATOR_INDEX = "validatorIndex";
// Adds `_who` to the array of pools
function _addToPools(address _who) internal {
if (!doesPoolExist(_who)) {
address[] storage pools = addressArrayStorage[POOLS];
_setPoolIndex(_who, pools.length);
pools.push(_who);
require(pools.length <= MAX_CANDIDATES);
_setIsPoolActive(_who, true);
}
_removeFromPoolsInactive(_who);
}
// Adds `_who` to the array of inactive pools
function _addToPoolsInactive(address _who) internal {
address[] storage poolsInactive = addressArrayStorage[POOLS_INACTIVE];
if (poolsInactive.length == 0 || poolsInactive[poolInactiveIndex(_who)] != _who) {
_setPoolInactiveIndex(_who, poolsInactive.length);
poolsInactive.push(_who);
}
}
// Removes `_who` from the array of pools
function _removeFromPools(address _who) internal {
uint256 indexToRemove = poolIndex(_who);
address[] storage pools = addressArrayStorage[POOLS];
if (pools.length > 0 && pools[indexToRemove] == _who) {
pools[indexToRemove] = pools[pools.length - 1];
_setPoolIndex(pools[indexToRemove], indexToRemove);
pools.length--;
_setPoolIndex(_who, 0);
_setIsPoolActive(_who, false);
if (stakeAmountTotal(_who) != 0) {
_addToPoolsInactive(_who);
}
}
}
// Removes `_who` from the array of inactive pools
function _removeFromPoolsInactive(address _who) internal {
address[] storage poolsInactive = addressArrayStorage[POOLS_INACTIVE];
uint256 indexToRemove = poolInactiveIndex(_who);
if (poolsInactive.length > 0 && poolsInactive[indexToRemove] == _who) {
poolsInactive[indexToRemove] = poolsInactive[poolsInactive.length - 1];
_setPoolInactiveIndex(poolsInactive[indexToRemove], indexToRemove);
poolsInactive.length--;
_setPoolInactiveIndex(_who, 0);
}
}
function _applyPendingValidators() internal {
address[] memory validators = getValidators();
uint256 i;
// Clear indexes for old validator set
for (i = 0; i < validators.length; i++) {
_setValidatorIndex(validators[i], 0);
_setIsValidator(validators[i], false);
}
validators = getPendingValidators();
_setCurrentValidators(validators);
// Set indexes for new validator set
for (i = 0; i < validators.length; i++) {
_setValidatorIndex(validators[i], i);
_setIsValidator(validators[i], true);
}
}
function _banValidator(address _validator, uint256 _bannedUntil) internal {
uintStorage[
keccak256(abi.encode(BANNED_UNTIL, _validator))
] = _bannedUntil;
}
function _incrementChangeRequestCount() internal {
uintStorage[CHANGE_REQUEST_COUNT]++;
}
function _incrementStakingEpoch() internal {
uintStorage[STAKING_EPOCH]++;
}
function _initialize(
address _blockRewardContract,
address _randomContract,
address _erc20TokenContract,
address[] memory _initialValidators,
uint256 _delegatorMinStake,
uint256 _candidateMinStake
) internal {
require(_getCurrentBlockNumber() == 0); // initialization must be done on genesis block
require(_blockRewardContract != address(0));
require(_randomContract != address(0));
require(_initialValidators.length > 0);
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 < _initialValidators.length; i++) {
currentValidators.push(_initialValidators[i]);
pendingValidators.push(_initialValidators[i]);
_setValidatorIndex(_initialValidators[i], i);
_setIsValidator(_initialValidators[i], true);
_addToPools(_initialValidators[i]);
}
_setDelegatorMinStake(_delegatorMinStake);
_setCandidateMinStake(_candidateMinStake);
_setValidatorSetApplyBlock(1);
}
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 (stakeAmount(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[] memory randomNumbers = IRandom(randomContract()).currentRandom();
require(randomNumbers.length == MAX_VALIDATORS);
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] = stakeAmountTotal(poolsLocal[i]).mul(100).div(STAKE_UNIT);
likelihoodSum = likelihoodSum.add(likelihood[i]);
}
for (i = 0; i < MAX_VALIDATORS; i++) {
uint256 randomPoolIndex = _getRandomIndex(
likelihood,
likelihoodSum,
uint256(keccak256(abi.encodePacked(randomNumbers[i])))
);
newValidators[i] = poolsLocal[randomPoolIndex];
likelihoodSum -= likelihood[randomPoolIndex];
poolsLocalLength--;
poolsLocal[randomPoolIndex] = poolsLocal[poolsLocalLength];
likelihood[randomPoolIndex] = likelihood[poolsLocalLength];
}
_setPendingValidators(newValidators);
}
// From this moment `getPendingValidators()` will return the new validator set
// Increment counters
_incrementChangeRequestCount();
_incrementStakingEpoch();
_setValidatorSetApplyBlock(0);
}
function _removeMaliciousValidator(address _validator) internal returns(bool) {
// Remove malicious validator from `pools`
_removeFromPools(_validator);
// Ban the malicious validator for the next 3 months
_banValidator(_validator, now + 90 days);
address[] storage validators = addressArrayStorage[PENDING_VALIDATORS];
bool isPendingValidator = false;
uint256 i;
for (i = 0; i < validators.length; i++) {
if (validators[i] == _validator) {
isPendingValidator = true;
break;
}
}
if (isPendingValidator) {
// Remove the malicious validator from `pendingValidators`
validators[i] = validators[--validators.length];
return true;
}
return false;
}
function _setCurrentValidators(address[] memory _validators) internal {
addressArrayStorage[CURRENT_VALIDATORS] = _validators;
}
function _setIsPoolActive(address _who, bool _isPoolActive) internal {
boolStorage[keccak256(abi.encode(IS_POOL_ACTIVE, _who))] = _isPoolActive;
}
function _setIsValidator(address _who, bool _isValidator) internal {
boolStorage[keccak256(abi.encode(IS_VALIDATOR, _who))] = _isValidator;
}
function _setIsValidatorOnPreviousEpoch(address _who, bool _isValidator) internal {
boolStorage[keccak256(abi.encode(IS_VALIDATOR_ON_PREVIOUS_EPOCH, _who))] = _isValidator;
}
function _setPendingValidators(address[] memory _validators) internal {
addressArrayStorage[PENDING_VALIDATORS] = _validators;
for (uint256 i = 0; i < addressArrayStorage[POOLS_EMPTY].length; i++) {
_removeFromPools(addressArrayStorage[POOLS_EMPTY][i]);
}
}
function _setPoolIndex(address _who, uint256 _index) internal {
uintStorage[
keccak256(abi.encode(POOL_INDEX, _who))
] = _index;
}
function _setPoolInactiveIndex(address _who, uint256 _index) internal {
uintStorage[
keccak256(abi.encode(POOL_INACTIVE_INDEX, _who))
] = _index;
}
function _setPoolDelegatorIndex(address _pool, address _delegator, uint256 _index) internal {
uintStorage[keccak256(abi.encode(POOL_DELEGATOR_INDEX, _pool, _delegator))] = _index;
}
// Add `_delegator` to the array of pool's delegators
function _addPoolDelegator(address _pool, address _delegator) internal {
address[] storage delegators = addressArrayStorage[
keccak256(abi.encode(POOL_DELEGATORS, _pool))
];
_setPoolDelegatorIndex(_pool, _delegator, delegators.length);
delegators.push(_delegator);
}
// Remove `_delegator` from the array of pool's delegators
function _removePoolDelegator(address _pool, address _delegator) internal {
address[] storage delegators = addressArrayStorage[
keccak256(abi.encode(POOL_DELEGATORS, _pool))
];
if (delegators.length == 0) return;
uint256 indexToRemove = poolDelegatorIndex(_pool, _delegator);
delegators[indexToRemove] = delegators[delegators.length - 1];
_setPoolDelegatorIndex(_pool, delegators[indexToRemove], indexToRemove);
delegators.length--;
_setPoolDelegatorIndex(_pool, _delegator, 0);
}
function _setPreviousValidators(address[] memory _validators) internal {
addressArrayStorage[PREVIOUS_VALIDATORS] = _validators;
}
function _setStakeAmount(address _pool, address _staker, uint256 _amount) internal {
uintStorage[
keccak256(abi.encode(STAKE_AMOUNT, _pool, _staker))
] = _amount;
}
function _setStakeAmountByEpoch(
address _pool,
address _staker,
uint256 _stakingEpoch,
uint256 _amount
) internal {
uintStorage[
keccak256(abi.encode(STAKE_AMOUNT_BY_EPOCH, _pool, _staker, _stakingEpoch))
] = _amount;
}
function _setStakeAmountTotal(address _pool, uint256 _amount) internal {
uintStorage[
keccak256(abi.encode(STAKE_AMOUNT_TOTAL, _pool))
] = _amount;
}
function _setDelegatorMinStake(uint256 _minStake) internal {
uintStorage[DELEGATOR_MIN_STAKE] = _minStake * STAKE_UNIT;
}
function _setCandidateMinStake(uint256 _minStake) internal {
uintStorage[CANDIDATE_MIN_STAKE] = _minStake * STAKE_UNIT;
}
function _setValidatorIndex(address _validator, uint256 _index) internal {
uintStorage[
keccak256(abi.encode(VALIDATOR_INDEX, _validator))
] = _index;
}
function _setValidatorSetApplyBlock(uint256 _blockNumber) internal {
uintStorage[VALIDATOR_SET_APPLY_BLOCK] = _blockNumber;
}
function _stake(address _pool, address _staker, uint256 _amount) internal {
require(_pool != address(0));
require(_amount != 0);
require(!isValidatorBanned(_pool));
require(_areStakeAndWithdrawAllowed());
uint256 epoch = stakingEpoch();
uint256 newStakeAmount = stakeAmount(_pool, _staker).add(_amount);
if (_staker == _pool) {
require(newStakeAmount >= getCandidateMinStake()); // the staked amount must be at least CANDIDATE_MIN_STAKE
} else {
require(newStakeAmount >= getDelegatorMinStake()); // the staked amount must be at least DELEGATOR_MIN_STAKE
}
_setStakeAmount(_pool, _staker, newStakeAmount);
_setStakeAmountByEpoch(_pool, _staker, epoch, stakeAmountByEpoch(_pool, _staker, epoch).add(_amount));
_setStakeAmountTotal(_pool, stakeAmountTotal(_pool).add(_amount));
if (_staker == _pool) { // `staker` makes a stake for himself and becomes a candidate
// Add `_pool` to the array of pools
_addToPools(_pool);
} else if (newStakeAmount == _amount) { // if the stake is first
// Add `_staker` to the array of pool's delegators
_addPoolDelegator(_pool, _staker);
}
}
function _withdraw(address _pool, address _staker, uint256 _amount) internal {
require(_pool != address(0));
require(_amount != 0);
// How much can `staker` withdraw from `_pool` at the moment?
require(_amount <= maxWithdrawAllowed(_pool, _staker));
uint256 epoch = stakingEpoch();
// The amount to be withdrawn must be the whole staked amount or
// must not exceed the diff between the entire amount and MIN_STAKE
uint256 newStakeAmount = stakeAmount(_pool, _staker).sub(_amount);
if (newStakeAmount > 0) {
if (_staker == _pool) {
require(newStakeAmount >= getCandidateMinStake());
} else {
require(newStakeAmount >= getDelegatorMinStake());
}
}
_setStakeAmount(_pool, _staker, newStakeAmount);
uint256 amountByEpoch = stakeAmountByEpoch(_pool, _staker, epoch);
if (_amount <= amountByEpoch) {
_setStakeAmountByEpoch(_pool, _staker, epoch, amountByEpoch - _amount);
} else {
_setStakeAmountByEpoch(_pool, _staker, epoch, 0);
}
_setStakeAmountTotal(_pool, stakeAmountTotal(_pool).sub(_amount));
if (newStakeAmount == 0) { // the whole amount has been withdrawn
if (_staker == _pool) {
// Remove `_pool` from the array of pools
_removeFromPools(_pool);
} else {
// Remove `_staker` from the array of pool's delegators
_removePoolDelegator(_pool, _staker);
}
if (stakeAmountTotal(_pool) == 0) {
_removeFromPoolsInactive(_pool);
}
}
}
function _areStakeAndWithdrawAllowed() internal view returns(bool);
function _getCurrentBlockNumber() internal view returns(uint256) {
return block.number;
}
function _getRandomIndex(uint256[] memory _likelihood, uint256 _likelihoodSum, uint256 _randomNumber)
internal
pure
returns(uint256)
{
int256 r = int256(_randomNumber % _likelihoodSum) + 1;
int256 index = -1;
do {
r -= int256(_likelihood[uint256(++index)]);
} while (r > 0);
return uint256(index);
}
}