forked from wormhole-foundation/native-token-transfers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpointRegistry.sol
261 lines (207 loc) · 9.46 KB
/
EndpointRegistry.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
// SPDX-License-Identifier: Apache 2
pragma solidity >=0.8.8 <0.9.0;
/// @dev This contract is responsible for handling the registration of Endpoints.
abstract contract EndpointRegistry {
constructor() {
_checkEndpointsInvariants();
}
/// @dev Information about registered endpoints.
struct EndpointInfo {
// whether this endpoint is registered
bool registered;
// whether this endpoint is enabled
bool enabled;
uint8 index;
}
/// @dev Bitmap encoding the enabled endpoints.
/// invariant: forall (i: uint8), enabledEndpointBitmap & i == 1 <=> endpointInfos[i].enabled
struct _EnabledEndpointBitmap {
uint64 bitmap;
}
/// @dev Total number of registered endpoints. This number can only increase.
/// invariant: numRegisteredEndpoints <= MAX_ENDPOINTS
/// invariant: forall (i: uint8),
/// i < numRegisteredEndpoints <=> exists (a: address), endpointInfos[a].index == i
struct _NumRegisteredEndpoints {
uint8 num;
}
// TODO: should we just increase this to 255?
uint8 constant MAX_ENDPOINTS = 64;
error CallerNotEndpoint(address caller);
error InvalidEndpointZeroAddress();
error DisabledEndpoint(address endpoint);
error TooManyEndpoints();
error NonRegisteredEndpoint(address endpoint);
error EndpointAlreadyEnabled(address endpoint);
event EndpointAdded(address endpoint);
event EndpointRemoved(address endpoint);
modifier onlyEndpoint() {
if (!_getEndpointInfosStorage()[msg.sender].enabled) {
revert CallerNotEndpoint(msg.sender);
}
_;
}
/// =============== STORAGE ===============================================
bytes32 public constant ENDPOINT_INFOS_SLOT =
bytes32(uint256(keccak256("ntt.endpointInfos")) - 1);
bytes32 public constant ENDPOINT_BITMAP_SLOT =
bytes32(uint256(keccak256("ntt.endpointBitmap")) - 1);
bytes32 public constant ENABLED_ENDPOINTS_SLOT =
bytes32(uint256(keccak256("ntt.enabledEndpoints")) - 1);
bytes32 public constant NUM_REGISTERED_ENDPOINTS_SLOT =
bytes32(uint256(keccak256("ntt.numRegisteredEndpoints")) - 1);
function _getEndpointInfosStorage()
internal
pure
returns (mapping(address => EndpointInfo) storage $)
{
uint256 slot = uint256(ENDPOINT_INFOS_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
function _getEnabledEndpointsStorage() internal pure returns (address[] storage $) {
uint256 slot = uint256(ENABLED_ENDPOINTS_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
function _getEndpointBitmapStorage() private pure returns (_EnabledEndpointBitmap storage $) {
uint256 slot = uint256(ENDPOINT_BITMAP_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
function _getNumRegisteredEndpointsStorage()
private
pure
returns (_NumRegisteredEndpoints storage $)
{
uint256 slot = uint256(NUM_REGISTERED_ENDPOINTS_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
/// =============== GETTERS/SETTERS ========================================
function _setEndpoint(address endpoint) internal returns (uint8 index) {
mapping(address => EndpointInfo) storage endpointInfos = _getEndpointInfosStorage();
_EnabledEndpointBitmap storage _enabledEndpointBitmap = _getEndpointBitmapStorage();
address[] storage _enabledEndpoints = _getEnabledEndpointsStorage();
_NumRegisteredEndpoints storage _numRegisteredEndpoints =
_getNumRegisteredEndpointsStorage();
if (endpoint == address(0)) {
revert InvalidEndpointZeroAddress();
}
if (_numRegisteredEndpoints.num >= MAX_ENDPOINTS) {
revert TooManyEndpoints();
}
if (endpointInfos[endpoint].registered) {
endpointInfos[endpoint].enabled = true;
} else {
endpointInfos[endpoint] =
EndpointInfo({registered: true, enabled: true, index: _numRegisteredEndpoints.num});
_numRegisteredEndpoints.num++;
}
_enabledEndpoints.push(endpoint);
uint64 updatedEnabledEndpointBitmap =
_enabledEndpointBitmap.bitmap | uint64(1 << endpointInfos[endpoint].index);
// ensure that this actually changed the bitmap
if (updatedEnabledEndpointBitmap == _enabledEndpointBitmap.bitmap) {
revert EndpointAlreadyEnabled(endpoint);
}
_enabledEndpointBitmap.bitmap = updatedEnabledEndpointBitmap;
emit EndpointAdded(endpoint);
_checkEndpointsInvariants();
return endpointInfos[endpoint].index;
}
function _removeEndpoint(address endpoint) internal {
mapping(address => EndpointInfo) storage endpointInfos = _getEndpointInfosStorage();
_EnabledEndpointBitmap storage _enabledEndpointBitmap = _getEndpointBitmapStorage();
address[] storage _enabledEndpoints = _getEnabledEndpointsStorage();
if (endpoint == address(0)) {
revert InvalidEndpointZeroAddress();
}
if (!endpointInfos[endpoint].registered) {
revert NonRegisteredEndpoint(endpoint);
}
if (!endpointInfos[endpoint].enabled) {
revert DisabledEndpoint(endpoint);
}
endpointInfos[endpoint].enabled = false;
uint64 updatedEnabledEndpointBitmap =
_enabledEndpointBitmap.bitmap & uint64(~(1 << endpointInfos[endpoint].index));
// ensure that this actually changed the bitmap
assert(updatedEnabledEndpointBitmap < _enabledEndpointBitmap.bitmap);
_enabledEndpointBitmap.bitmap = updatedEnabledEndpointBitmap;
bool removed = false;
for (uint256 i = 0; i < _enabledEndpoints.length; i++) {
if (_enabledEndpoints[i] == endpoint) {
_enabledEndpoints[i] = _enabledEndpoints[_enabledEndpoints.length - 1];
_enabledEndpoints.pop();
removed = true;
break;
}
}
assert(removed);
emit EndpointRemoved(endpoint);
_checkEndpointsInvariants();
// we call the invariant check on the endpoint here as well, since
// the above check only iterates through the enabled endpoints.
_checkEndpointInvariants(endpoint);
}
function _getEnabledEndpointsBitmap() internal view virtual returns (uint64 bitmap) {
return _getEndpointBitmapStorage().bitmap;
}
/// @notice Returns the Endpoint contracts that have been registered via governance.
function getEndpoints() external pure returns (address[] memory result) {
result = _getEnabledEndpointsStorage();
}
/// ============== INVARIANTS =============================================
/// @dev Check that the endpoint manager is in a valid state.
/// Checking these invariants is somewhat costly, but we only need to do it
/// when modifying the endpoints, which happens infrequently.
function _checkEndpointsInvariants() internal view {
// TODO: add custom errors for each invariant
_NumRegisteredEndpoints storage _numRegisteredEndpoints =
_getNumRegisteredEndpointsStorage();
address[] storage _enabledEndpoints = _getEnabledEndpointsStorage();
for (uint256 i = 0; i < _enabledEndpoints.length; i++) {
_checkEndpointInvariants(_enabledEndpoints[i]);
}
// invariant: each endpoint is only enabled once
for (uint256 i = 0; i < _enabledEndpoints.length; i++) {
for (uint256 j = i + 1; j < _enabledEndpoints.length; j++) {
assert(_enabledEndpoints[i] != _enabledEndpoints[j]);
}
}
// invariant: numRegisteredEndpoints <= MAX_ENDPOINTS
assert(_numRegisteredEndpoints.num <= MAX_ENDPOINTS);
}
// @dev Check that the endpoint is in a valid state.
function _checkEndpointInvariants(address endpoint) private view {
mapping(address => EndpointInfo) storage endpointInfos = _getEndpointInfosStorage();
_EnabledEndpointBitmap storage _enabledEndpointBitmap = _getEndpointBitmapStorage();
_NumRegisteredEndpoints storage _numRegisteredEndpoints =
_getNumRegisteredEndpointsStorage();
address[] storage _enabledEndpoints = _getEnabledEndpointsStorage();
EndpointInfo memory endpointInfo = endpointInfos[endpoint];
// if an endpoint is not registered, it should not be enabled
assert(endpointInfo.registered || (!endpointInfo.enabled && endpointInfo.index == 0));
bool endpointInEnabledBitmap =
(_enabledEndpointBitmap.bitmap & uint64(1 << endpointInfo.index)) != 0;
bool endpointEnabled = endpointInfo.enabled;
bool endpointInEnabledEndpoints = false;
for (uint256 i = 0; i < _enabledEndpoints.length; i++) {
if (_enabledEndpoints[i] == endpoint) {
endpointInEnabledEndpoints = true;
break;
}
}
// invariant: endpointInfos[endpoint].enabled
// <=> enabledEndpointBitmap & (1 << endpointInfos[endpoint].index) != 0
assert(endpointInEnabledBitmap == endpointEnabled);
// invariant: endpointInfos[endpoint].enabled <=> endpoint in _enabledEndpoints
assert(endpointInEnabledEndpoints == endpointEnabled);
assert(endpointInfo.index < _numRegisteredEndpoints.num);
}
}