-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExecutor.sol
More file actions
278 lines (244 loc) · 11.3 KB
/
Executor.sol
File metadata and controls
278 lines (244 loc) · 11.3 KB
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
// SPDX‑License‑Identifier: MIT
pragma solidity ^0.8.20;
/* OpenZeppelin v5.3 Upgradeables */
import "@openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin-contracts-upgradeable/contracts/access/OwnableUpgradeable.sol";
import "@openzeppelin-contracts-upgradeable/contracts/utils/PausableUpgradeable.sol";
import "@openzeppelin-contracts-upgradeable/contracts/utils/ReentrancyGuardUpgradeable.sol";
import {IHats} from "@hats-protocol/src/Interfaces/IHats.sol";
import {SwitchableBeacon} from "./SwitchableBeacon.sol";
interface IExecutor {
struct Call {
address target;
uint256 value;
bytes data;
}
function execute(uint256 proposalId, Call[] calldata batch) external;
}
/**
* @title Executor
* @notice Batch‑executor behind an UpgradeableBeacon.
* Exactly **one** governor address is authorised to trigger `execute`.
*/
contract Executor is Initializable, OwnableUpgradeable, PausableUpgradeable, ReentrancyGuardUpgradeable, IExecutor {
/* ─────────── Errors ─────────── */
error UnauthorizedCaller();
error CallFailed(uint256 index, bytes lowLevelData);
error EmptyBatch();
error TooManyCalls();
error TargetSelf();
error ZeroAddress();
error TimelockNotExpired();
/* ─────────── Constants ─────────── */
uint8 public constant MAX_CALLS_PER_BATCH = 20;
uint256 public constant CALLER_CHANGE_DELAY = 2 days;
/* ─────────── ERC-7201 Storage ─────────── */
/// @custom:storage-location erc7201:poa.executor.storage
struct Layout {
address allowedCaller; // sole authorised governor
IHats hats; // Hats Protocol interface
mapping(address => bool) authorizedHatMinters; // contracts authorized to request hat minting
address pendingCaller;
uint256 callerChangeTimestamp;
}
bytes32 private constant _STORAGE_SLOT = keccak256("poa.executor.storage");
function _layout() private pure returns (Layout storage s) {
bytes32 slot = _STORAGE_SLOT;
assembly {
s.slot := slot
}
}
/* ─────────── Events ─────────── */
event CallerSet(address indexed caller);
event CallerChangeProposed(address indexed newCaller, uint256 effectiveAt);
event CallerChangeCancelled();
event BatchExecuted(uint256 indexed proposalId, uint256 calls);
event CallExecuted(uint256 indexed proposalId, uint256 indexed index, address target, uint256 value);
event Swept(address indexed to, uint256 amount);
event HatsSet(address indexed hats);
event HatMinterAuthorized(address indexed minter, bool authorized);
event HatsMinted(address indexed user, uint256[] hatIds);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/* ─────────── Initialiser ─────────── */
function initialize(address owner_, address hats_) external initializer {
if (owner_ == address(0) || hats_ == address(0)) revert ZeroAddress();
__Ownable_init(owner_);
__Pausable_init();
__ReentrancyGuard_init();
Layout storage l = _layout();
l.hats = IHats(hats_);
emit HatsSet(hats_);
}
/* ─────────── Governor management ─────────── */
/// @notice Instant set only allowed for first-time setup (allowedCaller == address(0)), restricted to owner
function setCaller(address newCaller) external {
if (newCaller == address(0)) revert ZeroAddress();
Layout storage l = _layout();
if (l.allowedCaller != address(0)) revert UnauthorizedCaller();
if (msg.sender != owner()) revert UnauthorizedCaller();
l.allowedCaller = newCaller;
emit CallerSet(newCaller);
}
/// @notice Propose a new caller (subject to CALLER_CHANGE_DELAY)
function proposeCaller(address newCaller) external {
if (newCaller == address(0)) revert ZeroAddress();
Layout storage l = _layout();
if (msg.sender != l.allowedCaller && msg.sender != owner()) revert UnauthorizedCaller();
l.pendingCaller = newCaller;
l.callerChangeTimestamp = block.timestamp;
emit CallerChangeProposed(newCaller, block.timestamp + CALLER_CHANGE_DELAY);
}
/// @notice Accept the pending caller after the timelock delay
function acceptCaller() external {
Layout storage l = _layout();
if (l.pendingCaller == address(0)) revert ZeroAddress();
if (block.timestamp < l.callerChangeTimestamp + CALLER_CHANGE_DELAY) revert TimelockNotExpired();
if (msg.sender != l.allowedCaller && msg.sender != owner()) revert UnauthorizedCaller();
l.allowedCaller = l.pendingCaller;
l.pendingCaller = address(0);
l.callerChangeTimestamp = 0;
emit CallerSet(l.allowedCaller);
}
/// @notice Cancel a pending caller change
function cancelCallerChange() external {
Layout storage l = _layout();
if (msg.sender != l.allowedCaller && msg.sender != owner()) revert UnauthorizedCaller();
l.pendingCaller = address(0);
l.callerChangeTimestamp = 0;
emit CallerChangeCancelled();
}
/* ─────────── Hat minting management ─────────── */
function setHatMinterAuthorization(address minter, bool authorized) external {
if (minter == address(0)) revert ZeroAddress();
Layout storage l = _layout();
// Only owner or allowed caller can set authorizations
if (msg.sender != owner() && msg.sender != l.allowedCaller) revert UnauthorizedCaller();
l.authorizedHatMinters[minter] = authorized;
emit HatMinterAuthorized(minter, authorized);
}
function mintHatsForUser(address user, uint256[] calldata hatIds) external {
Layout storage l = _layout();
if (!l.authorizedHatMinters[msg.sender]) revert UnauthorizedCaller();
if (user == address(0)) revert ZeroAddress();
// Mint each hat to the user
for (uint256 i = 0; i < hatIds.length; i++) {
l.hats.mintHat(hatIds[i], user);
}
emit HatsMinted(user, hatIds);
}
/* ─────────── Batch execution ─────────── */
function execute(uint256 proposalId, Call[] calldata batch) external override whenNotPaused nonReentrant {
if (msg.sender != _layout().allowedCaller) revert UnauthorizedCaller();
uint256 len = batch.length;
if (len == 0) revert EmptyBatch();
if (len > MAX_CALLS_PER_BATCH) revert TooManyCalls();
for (uint256 i; i < len;) {
if (batch[i].target == address(this)) revert TargetSelf();
(bool ok, bytes memory ret) = batch[i].target.call{value: batch[i].value}(batch[i].data);
if (!ok) revert CallFailed(i, ret);
emit CallExecuted(proposalId, i, batch[i].target, batch[i].value);
unchecked {
++i;
}
}
emit BatchExecuted(proposalId, len);
}
/* ─────────── Beacon ownership ─────────── */
function acceptBeaconOwnership(address beacon) external onlyOwner {
SwitchableBeacon(beacon).acceptOwnership();
}
/* ─────────── Guardian helpers ─────────── */
function pause() external onlyOwner {
_pause();
}
function unpause() external onlyOwner {
_unpause();
}
/* ─────────── ETH recovery ─────────── */
function sweep(address payable to) external onlyOwner {
if (to == address(0)) revert ZeroAddress();
uint256 bal = address(this).balance;
to.transfer(bal);
emit Swept(to, bal);
}
/* ─────────── Module Configuration ─────────── */
/**
* @notice Configure vouching on EligibilityModule during initial setup
* @dev Only callable by owner before renouncing ownership
* @param eligibilityModule Address of the EligibilityModule
* @param hatId Hat ID to configure vouching for
* @param quorum Number of vouches required
* @param membershipHatId Hat ID whose wearers can vouch
* @param combineWithHierarchy Whether to combine with parent hat eligibility
*/
function configureVouching(
address eligibilityModule,
uint256 hatId,
uint32 quorum,
uint256 membershipHatId,
bool combineWithHierarchy
) external onlyOwner {
if (eligibilityModule == address(0)) revert ZeroAddress();
(bool success,) = eligibilityModule.call(
abi.encodeWithSignature(
"configureVouching(uint256,uint32,uint256,bool)", hatId, quorum, membershipHatId, combineWithHierarchy
)
);
require(success, "configureVouching failed");
}
/**
* @notice Batch configure vouching for multiple hats during initial setup
* @dev Only callable by owner before renouncing ownership - gas optimized for org deployment
* @param eligibilityModule Address of the EligibilityModule
* @param hatIds Array of hat IDs to configure
* @param quorums Array of quorum values
* @param membershipHatIds Array of membership hat IDs
* @param combineWithHierarchyFlags Array of combine flags
*/
function batchConfigureVouching(
address eligibilityModule,
uint256[] calldata hatIds,
uint32[] calldata quorums,
uint256[] calldata membershipHatIds,
bool[] calldata combineWithHierarchyFlags
) external onlyOwner {
if (eligibilityModule == address(0)) revert ZeroAddress();
(bool success,) = eligibilityModule.call(
abi.encodeWithSignature(
"batchConfigureVouching(uint256[],uint32[],uint256[],bool[])",
hatIds,
quorums,
membershipHatIds,
combineWithHierarchyFlags
)
);
require(success, "batchConfigureVouching failed");
}
/**
* @notice Set default eligibility for a hat during initial setup
* @dev Only callable by owner before renouncing ownership
* @param eligibilityModule Address of the EligibilityModule
* @param hatId Hat ID to set default eligibility for
* @param eligible Whether wearers are eligible by default
* @param standing Whether wearers have good standing by default
*/
function setDefaultEligibility(address eligibilityModule, uint256 hatId, bool eligible, bool standing)
external
onlyOwner
{
if (eligibilityModule == address(0)) revert ZeroAddress();
(bool success,) = eligibilityModule.call(
abi.encodeWithSignature("setDefaultEligibility(uint256,bool,bool)", hatId, eligible, standing)
);
require(success, "setDefaultEligibility failed");
}
/* ─────────── View Helpers ─────────── */
function allowedCaller() external view returns (address) {
return _layout().allowedCaller;
}
/* accept ETH for payable calls within a batch */
receive() external payable {}
}