-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathTimelock.sol
300 lines (263 loc) · 10.2 KB
/
Timelock.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
pragma solidity ^0.5.17;
import "../openzeppelin/SafeMath.sol";
import "./ErrorDecoder.sol";
interface ITimelock {
function delay() external view returns (uint256);
function GRACE_PERIOD() external view returns (uint256);
function acceptAdmin() external;
function queuedTransactions(bytes32 hash) external view returns (bool);
function queueTransaction(
address target,
uint256 value,
string calldata signature,
bytes calldata data,
uint256 eta
) external returns (bytes32);
function cancelTransaction(
address target,
uint256 value,
string calldata signature,
bytes calldata data,
uint256 eta
) external;
function executeTransaction(
address target,
uint256 value,
string calldata signature,
bytes calldata data,
uint256 eta
) external payable returns (bytes memory);
}
/**
* @title Sovryn Protocol Timelock contract, based on Compound system.
*
* @notice This contract lets Sovryn governance system set up its
* own Time Lock instance to execute transactions proposed through the
* GovernorAlpha contract instance.
*
* The Timelock contract allows its admin (Sovryn governance on
* GovernorAlpha contract) to add arbitrary function calls to a
* queue. This contract can only execute a function call if the
* function call has been in the queue for at least 3 hours.
*
* Anytime the Timelock contract makes a function call, it must be the
* case that the function call was first made public by having been publicly
* added to the queue at least 3 hours prior.
*
* The intention is to provide GovernorAlpha contract the functionality to
* queue proposal actions. This would mean that any changes made by Sovryn
* governance of any contract would necessarily come with at least an
* advanced warning. This makes the Sovryn system follow a “time-delayed,
* opt-out” upgrade pattern (rather than an “instant, forced” upgrade pattern).
*
* Time-delaying admin actions gives users a chance to exit system if its
* admins become malicious or compromised (or make a change that the users
* do not like). Downside is that honest admins would be unable
* to lock down functionality to protect users if a critical bug was found.
*
* Delayed transactions reduce the amount of trust required by users of Sovryn
* and the overall risk for contracts building on top of it, as GovernorAlpha.
* */
contract Timelock is ErrorDecoder, ITimelock {
using SafeMath for uint256;
uint256 public constant GRACE_PERIOD = 14 days;
uint256 public constant MINIMUM_DELAY = 3 hours;
uint256 public constant MAXIMUM_DELAY = 30 days;
address public admin;
address public pendingAdmin;
uint256 public delay;
mapping(bytes32 => bool) public queuedTransactions;
event NewAdmin(address indexed newAdmin);
event NewPendingAdmin(address indexed newPendingAdmin);
event NewDelay(uint256 indexed newDelay);
event CancelTransaction(
bytes32 indexed txHash,
address indexed target,
uint256 value,
string signature,
bytes data,
uint256 eta
);
event ExecuteTransaction(
bytes32 indexed txHash,
address indexed target,
uint256 value,
string signature,
bytes data,
uint256 eta
);
event QueueTransaction(
bytes32 indexed txHash,
address indexed target,
uint256 value,
string signature,
bytes data,
uint256 eta
);
/**
* @notice Function called on instance deployment of the contract.
* @param admin_ Governance contract address.
* @param delay_ Time to wait for queued transactions to be executed.
* */
constructor(address admin_, uint256 delay_) public {
require(
delay_ >= MINIMUM_DELAY,
"Timelock::constructor: Delay must exceed minimum delay."
);
require(
delay_ <= MAXIMUM_DELAY,
"Timelock::setDelay: Delay must not exceed maximum delay."
);
admin = admin_;
delay = delay_;
}
/**
* @notice Fallback function is to react to receiving value (rBTC).
* */
function() external payable {}
/**
* @notice Set a new delay when executing the contract calls.
* @param delay_ The amount of time to wait until execution.
* */
function setDelay(uint256 delay_) public {
require(msg.sender == address(this), "Timelock::setDelay: Call must come from Timelock.");
require(delay_ >= MINIMUM_DELAY, "Timelock::setDelay: Delay must exceed minimum delay.");
require(
delay_ <= MAXIMUM_DELAY,
"Timelock::setDelay: Delay must not exceed maximum delay."
);
delay = delay_;
emit NewDelay(delay);
}
/**
* @notice Accept a new admin for the timelock.
* */
function acceptAdmin() public {
require(
msg.sender == pendingAdmin,
"Timelock::acceptAdmin: Call must come from pendingAdmin."
);
admin = msg.sender;
pendingAdmin = address(0);
emit NewAdmin(admin);
}
/**
* @notice Set a new pending admin for the timelock.
* @param pendingAdmin_ The new pending admin address.
* */
function setPendingAdmin(address pendingAdmin_) public {
require(
msg.sender == address(this),
"Timelock::setPendingAdmin: Call must come from Timelock."
);
pendingAdmin = pendingAdmin_;
emit NewPendingAdmin(pendingAdmin);
}
/**
* @notice Queue a new transaction from the governance contract.
* @param target The contract to call.
* @param value The amount to send in the transaction.
* @param signature The stanndard representation of the function called.
* @param data The ethereum transaction input data payload.
* @param eta Estimated Time of Accomplishment. The timestamp that the
* proposal will be available for execution, set once the vote succeeds.
* */
function queueTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) public returns (bytes32) {
require(msg.sender == admin, "Timelock::queueTransaction: Call must come from admin.");
require(
eta >= getBlockTimestamp().add(delay),
"Timelock::queueTransaction: Estimated execution block must satisfy delay."
);
bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
queuedTransactions[txHash] = true;
emit QueueTransaction(txHash, target, value, signature, data, eta);
return txHash;
}
/**
* @notice Cancel a transaction.
* @param target The contract to call.
* @param value The amount to send in the transaction.
* @param signature The stanndard representation of the function called.
* @param data The ethereum transaction input data payload.
* @param eta Estimated Time of Accomplishment. The timestamp that the
* proposal will be available for execution, set once the vote succeeds.
* */
function cancelTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) public {
require(msg.sender == admin, "Timelock::cancelTransaction: Call must come from admin.");
bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
queuedTransactions[txHash] = false;
emit CancelTransaction(txHash, target, value, signature, data, eta);
}
/**
* @notice Executes a previously queued transaction from the governance.
* @param target The contract to call.
* @param value The amount to send in the transaction.
* @param signature The stanndard representation of the function called.
* @param data The ethereum transaction input data payload.
* @param eta Estimated Time of Accomplishment. The timestamp that the
* proposal will be available for execution, set once the vote succeeds.
* */
function executeTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) public payable returns (bytes memory) {
require(msg.sender == admin, "Timelock::executeTransaction: Call must come from admin.");
bytes32 txHash = keccak256(abi.encode(target, value, signature, data, eta));
require(
queuedTransactions[txHash],
"Timelock::executeTransaction: Transaction hasn't been queued."
);
require(
getBlockTimestamp() >= eta,
"Timelock::executeTransaction: Transaction hasn't surpassed time lock."
);
require(
getBlockTimestamp() <= eta.add(GRACE_PERIOD),
"Timelock::executeTransaction: Transaction is stale."
);
queuedTransactions[txHash] = false;
bytes memory callData;
if (bytes(signature).length == 0) {
callData = data;
} else {
callData = abi.encodePacked(bytes4(keccak256(bytes(signature))), data);
}
// solium-disable-next-line security/no-call-value
(bool success, bytes memory returnData) = target.call.value(value)(callData);
if (!success) {
if (returnData.length <= ERROR_MESSAGE_SHIFT) {
revert("Timelock::executeTransaction: Transaction execution reverted.");
} else {
revert(_addErrorMessage("Timelock::executeTransaction: ", string(returnData)));
}
}
emit ExecuteTransaction(txHash, target, value, signature, data, eta);
return returnData;
}
/**
* @notice A function used to get the current Block Timestamp.
* @dev Timestamp of the current block in seconds since the epoch.
* It is a Unix time stamp. So, it has the complete information about
* the date, hours, minutes, and seconds (in UTC) when the block was
* created.
* */
function getBlockTimestamp() internal view returns (uint256) {
// solium-disable-next-line security/no-block-members
return block.timestamp;
}
}