-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGateway.sol
387 lines (321 loc) · 15.2 KB
/
Gateway.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.26;
import {Auth} from "src/Auth.sol";
import {ArrayLib} from "src/libraries/ArrayLib.sol";
import {BytesLib} from "src/libraries/BytesLib.sol";
import {MathLib} from "src/libraries/MathLib.sol";
import {MessagesLib} from "src/libraries/MessagesLib.sol";
import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol";
import {TransientStorage} from "src/libraries/TransientStorage.sol";
import {IGateway, IMessageHandler} from "src/interfaces/gateway/IGateway.sol";
import {IRoot} from "src/interfaces/IRoot.sol";
import {IGasService} from "src/interfaces/gateway/IGasService.sol";
import {IAdapter} from "src/interfaces/gateway/IAdapter.sol";
import {IRecoverable} from "src/interfaces/IRoot.sol";
/// @title Gateway
/// @notice Routing contract that forwards outgoing messages to multiple adapters (1 full message, n-1 proofs)
/// and validates that multiple adapters have confirmed a message.
/// Handling incoming messages from the Centrifuge Chain through multiple adapters.
/// Supports processing multiple duplicate messages in parallel by
/// storing counts of messages and proofs that have been received.
contract Gateway is Auth, IGateway, IRecoverable {
using ArrayLib for uint16[8];
using BytesLib for bytes;
using MathLib for uint256;
using TransientStorage for bytes32;
bytes32 public constant QUOTA_SLOT = bytes32(uint256(keccak256("Centrifuge/quota")) - 1);
uint8 public constant MAX_ADAPTER_COUNT = 8;
uint8 public constant PRIMARY_ADAPTER_ID = 1;
uint256 public constant RECOVERY_CHALLENGE_PERIOD = 7 days;
IRoot public immutable root;
address public poolManager;
address public investmentManager;
IGasService public gasService;
mapping(bytes32 messageHash => Message) internal _messages;
mapping(address adapter => Adapter) internal _activeAdapters;
/// @inheritdoc IGateway
address[] public adapters;
/// @inheritdoc IGateway
mapping(address payer => bool) public payers;
/// @inheritdoc IGateway
mapping(uint8 messageId => address) public messageHandlers;
/// @inheritdoc IGateway
mapping(address adapter => mapping(bytes32 messageHash => uint256 timestamp)) public recoveries;
constructor(address root_, address poolManager_, address investmentManager_, address gasService_)
Auth(msg.sender)
{
root = IRoot(root_);
poolManager = poolManager_;
investmentManager = investmentManager_;
gasService = IGasService(gasService_);
}
modifier pauseable() {
require(!root.paused(), "Gateway/paused");
_;
}
receive() external payable {
emit ReceiveNativeTokens(msg.sender, msg.value);
}
// --- Administration ---
/// @inheritdoc IGateway
function file(bytes32 what, address[] calldata addresses) external auth {
if (what == "adapters") {
uint8 quorum_ = addresses.length.toUint8();
require(quorum_ != 0, "Gateway/empty-adapter-set");
require(quorum_ <= MAX_ADAPTER_COUNT, "Gateway/exceeds-max");
// Increment session id to reset pending votes
uint256 numAdapters = adapters.length;
uint64 sessionId = numAdapters > 0 ? _activeAdapters[adapters[0]].activeSessionId + 1 : 0;
// Disable old adapters
for (uint8 i; i < numAdapters; i++) {
delete _activeAdapters[adapters[i]];
}
// Enable new adapters, setting quorum to number of adapters
for (uint8 j; j < quorum_; j++) {
require(_activeAdapters[addresses[j]].id == 0, "Gateway/no-duplicates-allowed");
// Ids are assigned sequentially starting at 1
_activeAdapters[addresses[j]] = Adapter(j + 1, quorum_, sessionId);
}
adapters = addresses;
} else {
revert("Gateway/file-unrecognized-param");
}
emit File(what, addresses);
}
/// @inheritdoc IGateway
function file(bytes32 what, address instance) external auth {
if (what == "gasService") gasService = IGasService(instance);
else if (what == "investmentManager") investmentManager = instance;
else if (what == "poolManager") poolManager = instance;
else revert("Gateway/file-unrecognized-param");
emit File(what, instance);
}
/// @inheritdoc IGateway
function file(bytes32 what, uint8 data1, address data2) public auth {
if (what == "message") {
require(data1 > uint8(type(MessagesLib.Call).max), "Gateway/hardcoded-message-id");
messageHandlers[data1] = data2;
} else {
revert("Gateway/file-unrecognized-param");
}
emit File(what, data1, data2);
}
/// @inheritdoc IGateway
function file(bytes32 what, address payer, bool isAllowed) external auth {
if (what == "payers") payers[payer] = isAllowed;
else revert("Gateway/file-unrecognized-param");
emit File(what, payer, isAllowed);
}
/// @inheritdoc IRecoverable
function recoverTokens(address token, address receiver, uint256 amount) external auth {
if (token == address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)) {
SafeTransferLib.safeTransferETH(receiver, amount);
} else {
SafeTransferLib.safeTransfer(token, receiver, amount);
}
}
// --- Incoming ---
/// @inheritdoc IGateway
function handle(bytes calldata message) external pauseable {
_handle(message, msg.sender, false);
}
function _handle(bytes calldata payload, address adapter_, bool isRecovery) internal {
Adapter memory adapter = _activeAdapters[adapter_];
require(adapter.id != 0, "Gateway/invalid-adapter");
uint8 call = payload.toUint8(0);
if (
call == uint8(MessagesLib.Call.InitiateMessageRecovery)
|| call == uint8(MessagesLib.Call.DisputeMessageRecovery)
) {
require(!isRecovery, "Gateway/no-recursion");
require(adapters.length > 1, "Gateway/no-recovery-with-one-adapter-allowed");
return _handleRecovery(payload);
}
bool isMessageProof = call == uint8(MessagesLib.Call.MessageProof);
if (adapter.quorum == 1 && !isMessageProof) {
// Special case for gas efficiency
_dispatch(payload, false);
emit ExecuteMessage(payload, adapter_);
return;
}
// Verify adapter and parse message hash
bytes32 messageHash;
if (isMessageProof) {
require(isRecovery || adapter.id != PRIMARY_ADAPTER_ID, "Gateway/non-proof-adapter");
messageHash = payload.toBytes32(1);
emit ProcessProof(messageHash, adapter_);
} else {
require(isRecovery || adapter.id == PRIMARY_ADAPTER_ID, "Gateway/non-message-adapter");
messageHash = keccak256(payload);
emit ProcessMessage(payload, adapter_);
}
Message storage state = _messages[messageHash];
if (adapter.activeSessionId != state.sessionId) {
// Clear votes from previous session
delete state.votes;
state.sessionId = adapter.activeSessionId;
}
// Increase vote
state.votes[adapter.id - 1]++;
if (state.votes.countNonZeroValues() >= adapter.quorum) {
// Reduce votes by quorum
state.votes.decreaseFirstNValues(adapter.quorum);
// Handle message
if (isMessageProof) {
_dispatch(state.pendingMessage, false);
emit ExecuteMessage(state.pendingMessage, adapter_);
} else {
_dispatch(payload, false);
emit ExecuteMessage(payload, adapter_);
}
// Only if there are no more pending messages, remove the pending message
if (state.votes.isEmpty()) {
delete state.pendingMessage;
}
} else if (!isMessageProof) {
state.pendingMessage = payload;
}
}
function _dispatch(bytes memory message, bool isBatched) internal {
uint8 id = message.toUint8(0);
address manager;
if (id == 4) {
// Handle batch messages
require(!isBatched, "Gateway/no-recursive-batching-allowed");
uint256 offset = 1; // Offsets the message type which is 1 byte
uint256 messageLength = message.length;
// Check if the message actually contains 2 bytes dedicated for the subMessage length
while (offset + 2 <= messageLength) {
uint16 subMessageLength = message.toUint16(offset);
bytes memory subMessage = new bytes(subMessageLength);
offset = offset + 2; // Skip subMessage length
require(offset + subMessageLength <= messageLength, "Gateway/corrupted-message");
for (uint256 i; i < subMessageLength; i++) {
subMessage[i] = message[offset + i];
}
_dispatch(subMessage, true);
offset += subMessageLength;
}
return;
} else if (id >= 5 && id <= 7) {
manager = address(root);
} else if (id == 8) {
manager = address(gasService);
} else if (id >= 9 && id <= 19) {
manager = poolManager;
} else if (id >= 20 && id <= 28) {
manager = investmentManager;
} else {
// Dynamic path for other managers, to be able to easily
// extend functionality of Liquidity Pools
manager = messageHandlers[id];
require(manager != address(0), "Gateway/unregistered-message-id");
}
IMessageHandler(manager).handle(message);
}
function _handleRecovery(bytes memory payload) internal {
bytes32 messageHash = payload.toBytes32(1);
address adapter = payload.toAddress(33);
if (MessagesLib.messageType(payload) == MessagesLib.Call.InitiateMessageRecovery) {
require(_activeAdapters[adapter].id != 0, "Gateway/invalid-adapter");
recoveries[adapter][messageHash] = block.timestamp + RECOVERY_CHALLENGE_PERIOD;
emit InitiateMessageRecovery(messageHash, adapter);
} else if (MessagesLib.messageType(payload) == MessagesLib.Call.DisputeMessageRecovery) {
return _disputeMessageRecovery(adapter, messageHash);
}
}
/// @inheritdoc IGateway
function disputeMessageRecovery(address adapter, bytes32 messageHash) external auth {
_disputeMessageRecovery(adapter, messageHash);
}
function _disputeMessageRecovery(address adapter, bytes32 messageHash) internal {
delete recoveries[adapter][messageHash];
emit DisputeMessageRecovery(messageHash, adapter);
}
/// @inheritdoc IGateway
function executeMessageRecovery(address adapter, bytes calldata message) external {
bytes32 messageHash = keccak256(message);
uint256 recovery = recoveries[adapter][messageHash];
require(recovery != 0, "Gateway/message-recovery-not-initiated");
require(recovery <= block.timestamp, "Gateway/challenge-period-has-not-ended");
delete recoveries[adapter][messageHash];
_handle(message, adapter, true);
emit ExecuteMessageRecovery(message, adapter);
}
// --- Outgoing ---
/// @inheritdoc IGateway
function send(bytes calldata message, address source) public payable pauseable {
bool isManager = msg.sender == investmentManager || msg.sender == poolManager;
require(isManager || msg.sender == messageHandlers[message.toUint8(0)], "Gateway/invalid-manager");
bytes memory proof = abi.encodePacked(uint8(MessagesLib.Call.MessageProof), keccak256(message));
uint256 numAdapters = adapters.length;
require(numAdapters != 0, "Gateway/not-initialized");
uint256 fuel = QUOTA_SLOT.tloadUint256();
uint256 messageCost = gasService.estimate(message);
uint256 proofCost = gasService.estimate(proof);
if (fuel != 0) {
uint256 tank = fuel;
for (uint256 i; i < numAdapters; i++) {
IAdapter currentAdapter = IAdapter(adapters[i]);
bool isPrimaryAdapter = i == PRIMARY_ADAPTER_ID - 1;
bytes memory payload = isPrimaryAdapter ? message : proof;
uint256 consumed = currentAdapter.estimate(payload, isPrimaryAdapter ? messageCost : proofCost);
require(consumed <= tank, "Gateway/not-enough-gas-funds");
tank -= consumed;
currentAdapter.pay{value: consumed}(payload, address(this));
currentAdapter.send(payload);
}
QUOTA_SLOT.tstore(0);
} else if (gasService.shouldRefuel(source, message)) {
for (uint256 i; i < numAdapters; i++) {
IAdapter currentAdapter = IAdapter(adapters[i]);
bool isPrimaryAdapter = i == PRIMARY_ADAPTER_ID - 1;
bytes memory payload = isPrimaryAdapter ? message : proof;
uint256 consumed = currentAdapter.estimate(payload, isPrimaryAdapter ? messageCost : proofCost);
if (consumed <= address(this).balance) {
currentAdapter.pay{value: consumed}(payload, address(this));
}
currentAdapter.send(payload);
}
} else {
revert("Gateway/not-enough-gas-funds");
}
emit SendMessage(message);
}
/// @inheritdoc IGateway
function topUp() external payable {
require(payers[msg.sender], "Gateway/only-payers-can-top-up");
require(msg.value != 0, "Gateway/cannot-topup-with-nothing");
QUOTA_SLOT.tstore(msg.value);
}
// --- Helpers ---
/// @inheritdoc IGateway
function estimate(bytes calldata payload) external view returns (uint256[] memory perAdapter, uint256 total) {
bytes memory proof = abi.encodePacked(uint8(MessagesLib.Call.MessageProof), keccak256(payload));
uint256 messageCost = gasService.estimate(payload);
uint256 proofCost = gasService.estimate(proof);
perAdapter = new uint256[](adapters.length);
uint256 adaptersCount = adapters.length;
for (uint256 i; i < adaptersCount; i++) {
uint256 centrifugeCost = i == PRIMARY_ADAPTER_ID - 1 ? messageCost : proofCost;
bytes memory message = i == PRIMARY_ADAPTER_ID - 1 ? payload : proof;
uint256 estimated = IAdapter(adapters[i]).estimate(message, centrifugeCost);
perAdapter[i] = estimated;
total += estimated;
}
}
/// @inheritdoc IGateway
function quorum() external view returns (uint8) {
Adapter memory adapter = _activeAdapters[adapters[0]];
return adapter.quorum;
}
/// @inheritdoc IGateway
function activeSessionId() external view returns (uint64) {
Adapter memory adapter = _activeAdapters[adapters[0]];
return adapter.activeSessionId;
}
/// @inheritdoc IGateway
function votes(bytes32 messageHash) external view returns (uint16[MAX_ADAPTER_COUNT] memory) {
return _messages[messageHash].votes;
}
}