-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEmergencyExit.sol
198 lines (167 loc) · 5.43 KB
/
EmergencyExit.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "../../extensions/RONTransferHelper.sol";
import "../../interfaces/IRoninGovernanceAdmin.sol";
import "../../interfaces/validator/IEmergencyExit.sol";
import "./CandidateManagerCallback.sol";
import "./storage-fragments/CommonStorage.sol";
abstract contract EmergencyExit is IEmergencyExit, RONTransferHelper, CandidateManagerCallback, CommonStorage {
/**
* @inheritdoc IEmergencyExit
*/
function emergencyExitLockedAmount() external view returns (uint256) {
return _emergencyExitLockedAmount;
}
/**
* @inheritdoc IEmergencyExit
*/
function emergencyExpiryDuration() external view returns (uint256) {
return _emergencyExpiryDuration;
}
/**
* @inheritdoc IEmergencyExit
*/
function execRequestEmergencyExit(address cid, uint256 secLeftToRevoke) external onlyContract(ContractType.STAKING) {
EmergencyExitInfo storage _info = _exitInfo[cid];
if (_info.recyclingAt != 0) revert ErrAlreadyRequestedEmergencyExit();
uint256 revokingTimestamp = block.timestamp + secLeftToRevoke;
_setRevokingTimestamp(_candidateInfo[cid], revokingTimestamp);
_emergencyExitJailedTimestamp[cid] = revokingTimestamp;
uint256 deductedAmount = IStaking(msg.sender).execDeductStakingAmount(cid, _emergencyExitLockedAmount);
if (deductedAmount > 0) {
uint256 recyclingAt = block.timestamp + _emergencyExpiryDuration;
_lockedConsensusList.push(cid);
_info.lockedAmount = deductedAmount;
_info.recyclingAt = recyclingAt;
IRoninGovernanceAdmin(_getAdmin()).createEmergencyExitPoll(
cid, _candidateInfo[cid].__shadowedTreasury, block.timestamp, recyclingAt
);
}
emit EmergencyExitRequested(cid, deductedAmount);
}
/**
* @inheritdoc IEmergencyExit
*/
function setEmergencyExitLockedAmount(
uint256 amount
) external onlyAdmin {
_setEmergencyExitLockedAmount(amount);
}
/**
* @inheritdoc IEmergencyExit
*/
function setEmergencyExpiryDuration(
uint256 duration
) external onlyAdmin {
_setEmergencyExpiryDuration(duration);
}
/**
* @inheritdoc IEmergencyExit
*/
function execReleaseLockedFundForEmergencyExitRequest(address cid, address payable recipient) external onlyAdmin {
if (_exitInfo[cid].recyclingAt == 0) {
revert ErrLockedFundReleaseInfoNotFound(cid);
}
uint256 length = _lockedConsensusList.length;
uint256 index = length;
for (uint256 i; i < length;) {
if (_lockedConsensusList[i] == cid) {
index = i;
break;
}
unchecked {
++i;
}
}
// The locked amount might be recycled
if (index == length) {
revert ErrLockedFundMightBeRecycled(cid);
}
uint256 amount = _exitInfo[cid].lockedAmount;
if (amount > 0) {
delete _exitInfo[cid];
if (length > 1) {
_lockedConsensusList[index] = _lockedConsensusList[length - 1];
}
_lockedConsensusList.pop();
_lockedFundReleased[cid] = true;
if (_unsafeSendRONLimitGas(recipient, amount, DEFAULT_ADDITION_GAS)) {
emit EmergencyExitLockedFundReleased(cid, recipient, amount);
return;
}
emit EmergencyExitLockedFundReleasingFailed(cid, recipient, amount, address(this).balance);
}
}
/**
* @dev Tries to recycle the locked funds from emergency exit requests.
*/
function _tryRecycleLockedFundsFromEmergencyExits() internal {
uint256 length = _lockedConsensusList.length;
uint256 i;
address addr;
EmergencyExitInfo storage _info;
while (i < length) {
addr = _lockedConsensusList[i];
_info = _exitInfo[addr];
if (_info.recyclingAt <= block.timestamp) {
_totalDeprecatedReward += _info.lockedAmount;
delete _exitInfo[addr];
if (--length > 0) {
_lockedConsensusList[i] = _lockedConsensusList[length];
}
_lockedConsensusList.pop();
continue;
}
unchecked {
i++;
}
}
}
/**
* @dev Override `CandidateManager-_emergencyExitLockedFundReleased`.
*/
function _emergencyExitLockedFundReleased(
address cid
) internal virtual override returns (bool) {
return _lockedFundReleased[cid];
}
/**
* @dev Override `CandidateManager-_removeCandidate`.
*/
function _removeCandidate(
address cid
) internal override {
delete _lockedFundReleased[cid];
super._removeCandidate(cid);
}
/// @dev See {RoninValidatorSet-__css2cid}
function __css2cid(
TConsensus consensusAddr
) internal view virtual override(CandidateManager, CommonStorage) returns (address);
/// @dev See {RoninValidatorSet-__css2cidBatch}
function __css2cidBatch(
TConsensus[] memory consensusAddrs
) internal view virtual override(CandidateManager, CommonStorage) returns (address[] memory);
/// @dev See {RoninValidatorSet-__cid2cssBatch}
function __cid2cssBatch(
address[] memory cids
) internal view virtual override(CandidateManager, ValidatorInfoStorageV2) returns (TConsensus[] memory);
/**
* @dev See `setEmergencyExitLockedAmount.
*/
function _setEmergencyExitLockedAmount(
uint256 amount
) internal {
_emergencyExitLockedAmount = amount;
emit EmergencyExitLockedAmountUpdated(amount);
}
/**
* @dev See `setEmergencyExpiryDuration`.
*/
function _setEmergencyExpiryDuration(
uint256 duration
) internal {
_emergencyExpiryDuration = duration;
emit EmergencyExpiryDurationUpdated(duration);
}
}