-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBridgeExecutorBase.sol
355 lines (311 loc) · 11.6 KB
/
BridgeExecutorBase.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;
import { IExecutorBase } from 'src/interfaces/IExecutorBase.sol';
/**
* @title BridgeExecutorBase
* @author Aave
* @notice Abstract contract that implements basic executor functionality
* @dev It does not implement an external `queue` function. This should instead be done in the inheriting
* contract with proper access control
*/
abstract contract BridgeExecutorBase is IExecutorBase {
// Minimum allowed grace period, which reduces the risk of having an actions set expire due to network congestion
uint256 constant MINIMUM_GRACE_PERIOD = 10 minutes;
// Time between queuing and execution
uint256 private _delay;
// Time after the execution time during which the actions set can be executed
uint256 private _gracePeriod;
// Address with the ability of canceling actions sets
address private _guardian;
// Number of actions sets
uint256 private _actionsSetCounter;
// Map of registered actions sets (id => ActionsSet)
mapping(uint256 => ActionsSet) private _actionsSets;
// Map of queued actions (actionHash => isQueued)
mapping(bytes32 => bool) private _queuedActions;
/**
* @dev Only guardian can call functions marked by this modifier.
**/
modifier onlyGuardian() {
if (msg.sender != _guardian) revert NotGuardian();
_;
}
/**
* @dev Only this contract can call functions marked by this modifier.
**/
modifier onlyThis() {
if (msg.sender != address(this)) revert OnlyCallableByThis();
_;
}
/**
* @dev Constructor
*
* @param delay The delay before which an actions set can be executed
* @param gracePeriod The time period after a delay during which an actions set can be executed
* @param guardian The address of the guardian, which can cancel queued proposals (can be zero)
*/
constructor(
uint256 delay,
uint256 gracePeriod,
address guardian
) {
if (
gracePeriod < MINIMUM_GRACE_PERIOD
) revert InvalidInitParams();
_updateDelay(delay);
_updateGracePeriod(gracePeriod);
_updateGuardian(guardian);
}
/// @inheritdoc IExecutorBase
function execute(uint256 actionsSetId) external payable override {
if (getCurrentState(actionsSetId) != ActionsSetState.Queued) revert OnlyQueuedActions();
ActionsSet storage actionsSet = _actionsSets[actionsSetId];
if (block.timestamp < actionsSet.executionTime) revert TimelockNotFinished();
actionsSet.executed = true;
uint256 actionCount = actionsSet.targets.length;
bytes[] memory returnedData = new bytes[](actionCount);
for (uint256 i = 0; i < actionCount; ) {
returnedData[i] = _executeTransaction(
actionsSet.targets[i],
actionsSet.values[i],
actionsSet.signatures[i],
actionsSet.calldatas[i],
actionsSet.executionTime,
actionsSet.withDelegatecalls[i]
);
unchecked {
++i;
}
}
emit ActionsSetExecuted(actionsSetId, msg.sender, returnedData);
}
/// @inheritdoc IExecutorBase
function cancel(uint256 actionsSetId) external override onlyGuardian {
if (getCurrentState(actionsSetId) != ActionsSetState.Queued) revert OnlyQueuedActions();
ActionsSet storage actionsSet = _actionsSets[actionsSetId];
actionsSet.canceled = true;
uint256 targetsLength = actionsSet.targets.length;
for (uint256 i = 0; i < targetsLength; ) {
_cancelTransaction(
actionsSet.targets[i],
actionsSet.values[i],
actionsSet.signatures[i],
actionsSet.calldatas[i],
actionsSet.executionTime,
actionsSet.withDelegatecalls[i]
);
unchecked {
++i;
}
}
emit ActionsSetCanceled(actionsSetId);
}
/// @inheritdoc IExecutorBase
function updateGuardian(address guardian) external override onlyThis {
_updateGuardian(guardian);
}
/// @inheritdoc IExecutorBase
function updateDelay(uint256 delay) external override onlyThis {
_updateDelay(delay);
}
/// @inheritdoc IExecutorBase
function updateGracePeriod(uint256 gracePeriod) external override onlyThis {
if (gracePeriod < MINIMUM_GRACE_PERIOD) revert GracePeriodTooShort();
_updateGracePeriod(gracePeriod);
}
/// @inheritdoc IExecutorBase
function executeDelegateCall(address target, bytes calldata data)
external
payable
override
onlyThis
returns (bool, bytes memory)
{
bool success;
bytes memory resultData;
// solium-disable-next-line security/no-call-value
(success, resultData) = target.delegatecall(data);
return (success, resultData);
}
/// @inheritdoc IExecutorBase
function receiveFunds() external payable override {}
/// @inheritdoc IExecutorBase
function getDelay() external view override returns (uint256) {
return _delay;
}
/// @inheritdoc IExecutorBase
function getGracePeriod() external view override returns (uint256) {
return _gracePeriod;
}
/// @inheritdoc IExecutorBase
function getGuardian() external view override returns (address) {
return _guardian;
}
/// @inheritdoc IExecutorBase
function getActionsSetCount() external view override returns (uint256) {
return _actionsSetCounter;
}
/// @inheritdoc IExecutorBase
function getActionsSetById(uint256 actionsSetId)
external
view
override
returns (ActionsSet memory)
{
return _actionsSets[actionsSetId];
}
/// @inheritdoc IExecutorBase
function getCurrentState(uint256 actionsSetId) public view override returns (ActionsSetState) {
if (_actionsSetCounter <= actionsSetId) revert InvalidActionsSetId();
ActionsSet storage actionsSet = _actionsSets[actionsSetId];
if (actionsSet.canceled) {
return ActionsSetState.Canceled;
} else if (actionsSet.executed) {
return ActionsSetState.Executed;
} else if (block.timestamp > actionsSet.executionTime + _gracePeriod) {
return ActionsSetState.Expired;
} else {
return ActionsSetState.Queued;
}
}
/// @inheritdoc IExecutorBase
function isActionQueued(bytes32 actionHash) public view override returns (bool) {
return _queuedActions[actionHash];
}
function _updateGuardian(address guardian) internal {
emit GuardianUpdate(_guardian, guardian);
_guardian = guardian;
}
function _updateDelay(uint256 delay) internal {
emit DelayUpdate(_delay, delay);
_delay = delay;
}
function _updateGracePeriod(uint256 gracePeriod) internal {
emit GracePeriodUpdate(_gracePeriod, gracePeriod);
_gracePeriod = gracePeriod;
}
/**
* @notice Queue an ActionsSet
* @dev If a signature is empty, calldata is used for the execution, calldata is appended to signature otherwise
* @param targets Array of targets to be called by the actions set
* @param values Array of values to pass in each call by the actions set
* @param signatures Array of function signatures to encode in each call (can be empty)
* @param calldatas Array of calldata to pass in each call (can be empty)
* @param withDelegatecalls Array of whether to delegatecall for each call
**/
function _queue(
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
bool[] memory withDelegatecalls
) internal {
if (targets.length == 0) revert EmptyTargets();
uint256 targetsLength = targets.length;
if (
targetsLength != values.length ||
targetsLength != signatures.length ||
targetsLength != calldatas.length ||
targetsLength != withDelegatecalls.length
) revert InconsistentParamsLength();
uint256 actionsSetId = _actionsSetCounter;
uint256 executionTime = block.timestamp + _delay;
unchecked {
++_actionsSetCounter;
}
for (uint256 i = 0; i < targetsLength; ) {
bytes32 actionHash = keccak256(
abi.encode(
targets[i],
values[i],
signatures[i],
calldatas[i],
executionTime,
withDelegatecalls[i]
)
);
if (isActionQueued(actionHash)) revert DuplicateAction();
_queuedActions[actionHash] = true;
unchecked {
++i;
}
}
ActionsSet storage actionsSet = _actionsSets[actionsSetId];
actionsSet.targets = targets;
actionsSet.values = values;
actionsSet.signatures = signatures;
actionsSet.calldatas = calldatas;
actionsSet.withDelegatecalls = withDelegatecalls;
actionsSet.executionTime = executionTime;
emit ActionsSetQueued(
actionsSetId,
targets,
values,
signatures,
calldatas,
withDelegatecalls,
executionTime
);
}
function _executeTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 executionTime,
bool withDelegatecall
) internal returns (bytes memory) {
if (address(this).balance < value) revert InsufficientBalance();
bytes32 actionHash = keccak256(
abi.encode(target, value, signature, data, executionTime, withDelegatecall)
);
_queuedActions[actionHash] = false;
bytes memory callData;
if (bytes(signature).length == 0) {
callData = data;
} else {
callData = abi.encodePacked(bytes4(keccak256(bytes(signature))), data);
}
bool success;
bytes memory resultData;
if (withDelegatecall) {
(success, resultData) = this.executeDelegateCall{value: value}(target, callData);
} else {
// solium-disable-next-line security/no-call-value
(success, resultData) = target.call{value: value}(callData);
}
return _verifyCallResult(success, resultData);
}
function _cancelTransaction(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 executionTime,
bool withDelegatecall
) internal {
bytes32 actionHash = keccak256(
abi.encode(target, value, signature, data, executionTime, withDelegatecall)
);
_queuedActions[actionHash] = false;
}
function _verifyCallResult(bool success, bytes memory returnData)
private pure returns (bytes memory)
{
if (success) {
return returnData;
} else {
// Look for revert reason and bubble it up if present
if (returnData.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returnData)
revert(add(32, returnData), returndata_size)
}
} else {
revert FailedActionExecution();
}
}
}
}