-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathExecutor.sol
239 lines (188 loc) · 9.49 KB
/
Executor.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.22;
import { AccessControl } from "openzeppelin-contracts/contracts/access/AccessControl.sol";
import { Address } from "openzeppelin-contracts/contracts/utils/Address.sol";
import { IExecutor } from './interfaces/IExecutor.sol';
/**
* @title Executor
* @author Aave
* @notice Executor which queues up message calls and executes them after an optional delay
*/
contract Executor is IExecutor, AccessControl {
using Address for address;
/******************************************************************************************************************/
/*** State variables and constructor ***/
/******************************************************************************************************************/
bytes32 public constant SUBMISSION_ROLE = keccak256('SUBMISSION_ROLE');
bytes32 public constant GUARDIAN_ROLE = keccak256('GUARDIAN_ROLE');
uint256 public constant MINIMUM_GRACE_PERIOD = 10 minutes;
// Map of registered actions sets (id => ActionsSet)
mapping(uint256 => ActionsSet) private _actionsSets;
uint256 public override actionsSetCount; // Number of actions sets
uint256 public override delay; // Time between queuing and execution
uint256 public override gracePeriod; // Time after delay during which an actions set can be executed
/**
* @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.
*/
constructor(
uint256 delay_,
uint256 gracePeriod_
) {
if (
gracePeriod_ < MINIMUM_GRACE_PERIOD
) revert InvalidInitParams();
_updateDelay(delay_);
_updateGracePeriod(gracePeriod_);
_setRoleAdmin(SUBMISSION_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(GUARDIAN_ROLE, DEFAULT_ADMIN_ROLE);
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(DEFAULT_ADMIN_ROLE, address(this)); // Necessary for self-referential calls to change configuration
}
/******************************************************************************************************************/
/*** ActionSet functions ***/
/******************************************************************************************************************/
/// @inheritdoc IExecutor
function queue(
address[] memory targets,
uint256[] memory values,
string[] memory signatures,
bytes[] memory calldatas,
bool[] memory withDelegatecalls
) external override onlyRole(SUBMISSION_ROLE) {
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 = actionsSetCount;
uint256 executionTime = block.timestamp + delay;
unchecked { ++actionsSetCount; }
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
);
}
/// @inheritdoc IExecutor
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; ++i) {
returnedData[i] = _executeTransaction(
actionsSet.targets[i],
actionsSet.values[i],
actionsSet.signatures[i],
actionsSet.calldatas[i],
actionsSet.executionTime,
actionsSet.withDelegatecalls[i]
);
}
emit ActionsSetExecuted(actionsSetId, msg.sender, returnedData);
}
/// @inheritdoc IExecutor
function cancel(uint256 actionsSetId) external override onlyRole(GUARDIAN_ROLE) {
if (getCurrentState(actionsSetId) != ActionsSetState.Queued) revert OnlyQueuedActions();
ActionsSet storage actionsSet = _actionsSets[actionsSetId];
actionsSet.canceled = true;
emit ActionsSetCanceled(actionsSetId);
}
/******************************************************************************************************************/
/*** Admin functions ***/
/******************************************************************************************************************/
/// @inheritdoc IExecutor
function updateDelay(uint256 newDelay) external override onlyRole(DEFAULT_ADMIN_ROLE) {
_updateDelay(newDelay);
}
/// @inheritdoc IExecutor
function updateGracePeriod(uint256 newGracePeriod)
external override onlyRole(DEFAULT_ADMIN_ROLE)
{
if (newGracePeriod < MINIMUM_GRACE_PERIOD) revert GracePeriodTooShort();
_updateGracePeriod(newGracePeriod);
}
/******************************************************************************************************************/
/*** External misc functions ***/
/******************************************************************************************************************/
/// @inheritdoc IExecutor
function executeDelegateCall(address target, bytes calldata data)
external
payable
override
onlyRole(DEFAULT_ADMIN_ROLE)
returns (bytes memory)
{
return target.functionDelegateCall(data);
}
/// @inheritdoc IExecutor
function receiveFunds() external payable override {}
/******************************************************************************************************************/
/*** External view functions ***/
/******************************************************************************************************************/
/// @inheritdoc IExecutor
function getActionsSetById(uint256 actionsSetId)
external
view
override
returns (ActionsSet memory)
{
return _actionsSets[actionsSetId];
}
/// @inheritdoc IExecutor
function getCurrentState(uint256 actionsSetId) public view override returns (ActionsSetState) {
if (actionsSetCount <= 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;
}
/******************************************************************************************************************/
/*** Internal ActionSet helper functions ***/
/******************************************************************************************************************/
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();
bytes memory callData = bytes(signature).length == 0
? data
: abi.encodePacked(bytes4(keccak256(bytes(signature))), data);
if (withDelegatecall) return this.executeDelegateCall{value: value}(target, callData);
return target.functionCallWithValue(callData, value);
}
/******************************************************************************************************************/
/*** Internal admin helper functions ***/
/******************************************************************************************************************/
function _updateDelay(uint256 newDelay) internal {
emit DelayUpdate(delay, newDelay);
delay = newDelay;
}
function _updateGracePeriod(uint256 newGracePeriod) internal {
emit GracePeriodUpdate(gracePeriod, newGracePeriod);
gracePeriod = newGracePeriod;
}
}