-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathExitHandler.sol
462 lines (401 loc) · 15.3 KB
/
ExitHandler.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
* Copyright (c) 2018-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the Mozilla Public License, version 2,
* found in the LICENSE file in the root directory of this source tree.
*/
/* solium-disable security/no-block-members */
pragma solidity 0.5.2;
import "../node_modules/openzeppelin-solidity/contracts/math/Math.sol";
import "./DepositHandler.sol";
import "./IExitHandler.sol";
import "./Bridge.sol";
import "./TxLib.sol";
import "./PriorityQueue.sol";
contract ExitHandler is IExitHandler, DepositHandler {
using PriorityQueue for PriorityQueue.Token;
event ExitStarted(
bytes32 indexed txHash,
uint8 indexed outIndex,
uint256 indexed color,
address exitor,
uint256 amount,
bytes32 periodRoot
);
/**
- tokenData — (optional) NST data
*/
struct Exit {
uint256 amount;
uint16 color;
address owner;
bool finalized;
uint32 priorityTimestamp;
uint256 stake;
bytes32 tokenData;
}
uint256 public exitDuration;
uint256 public exitStake;
uint256 public nftExitCounter;
uint256 public nstExitCounter;
/**
* UTXO → Exit mapping
*/
mapping(bytes32 => Exit) exitMapping;
mapping(bytes32 => bytes32) exitRoots;
function exits(bytes32 _utxoId) public view returns (
uint256 amount,
uint16 color,
address owner,
bool finalized,
uint32 priorityTimestamp,
uint256 stake,
bytes32 tokenData,
bytes32 periodRoot
) {
Exit memory exit = exitMapping[_utxoId];
bytes32 periodRoot = exitRoots[_utxoId];
return (
exit.amount,
exit.color,
exit.owner,
exit.finalized,
exit.priorityTimestamp,
exit.stake,
exit.tokenData,
periodRoot
);
}
function initializeWithExit(
Bridge _bridge,
uint256 _exitDuration,
uint256 _exitStake) public initializer {
initialize(_bridge);
exitDuration = _exitDuration;
exitStake = _exitStake;
emit MinGasPrice(0);
}
function setExitStake(uint256 _exitStake) public ifAdmin {
exitStake = _exitStake;
}
function setExitDuration(uint256 _exitDuration) public ifAdmin {
exitDuration = _exitDuration;
}
struct ProofResults {
bytes32 txHash;
bytes txData;
uint64 txPos;
}
function startExit(
bytes32[] memory _youngestInputProof, bytes32[] memory _proof,
uint8 _outputIndex, uint8 _inputIndex
) public payable {
require(msg.value >= exitStake, "Not enough ether sent to pay for exit stake");
uint32 timestamp;
(, timestamp,,) = bridge.periods(_proof[0]);
require(timestamp > 0, "The referenced period was not submitted to bridge");
if (_youngestInputProof.length > 0) {
(, timestamp,,) = bridge.periods(_youngestInputProof[0]);
require(timestamp > 0, "The referenced period was not submitted to bridge");
}
// check exiting tx inclusion in the root chain block
ProofResults memory pr;
(pr.txPos, pr.txHash, pr.txData) = TxLib.validateProof(32 * (_youngestInputProof.length + 2) + 64, _proof);
// parse exiting tx and check if it is exitable
TxLib.Tx memory exitingTx = TxLib.parseTx(pr.txData);
TxLib.Output memory out = exitingTx.outs[_outputIndex];
bytes32 utxoId = bytes32(uint256(_outputIndex) << 120 | uint120(uint256(pr.txHash)));
uint256 priority;
if (msg.sender != out.owner) {
// or caller code hashes to owner
address a = msg.sender;
assembly {
priority := extcodehash(a) // abusing priority for hashBytes here, to save stack
}
require(priority != 0, "caller not contract");
require(bytes20(out.owner) == ripemd160(abi.encode(priority)), "Only UTXO owner or contract can start exit");
out.owner = msg.sender;
}
require(out.value > 0, "UTXO has no value");
require(exitMapping[utxoId].amount == 0, "The exit for UTXO has already been started");
require(!exitMapping[utxoId].finalized, "The exit for UTXO has already been finalized");
if (_youngestInputProof.length > 0) {
// check youngest input tx inclusion in the root chain block
bytes32 inputTxHash;
(pr.txPos, inputTxHash,) = TxLib.validateProof(96, _youngestInputProof);
require(
inputTxHash == exitingTx.ins[_inputIndex].outpoint.hash,
"Input from the proof is not referenced in exiting tx"
);
if (isNft(out.color)) {
priority = (nftExitCounter << 128) | uint128(uint256(utxoId));
nftExitCounter++;
} else if (isNST(out.color)) {
priority = (nstExitCounter << 128) | uint128(uint256(utxoId));
nstExitCounter++;
} else {
priority = getERC20ExitPriority(timestamp, utxoId, pr.txPos);
}
} else {
require(exitingTx.txType == TxLib.TxType.Deposit, "Expected deposit tx");
if (isNft(out.color)) {
priority = (nftExitCounter << 128) | uint128(uint256(utxoId));
nftExitCounter++;
} else if (isNST(out.color)) {
priority = (nstExitCounter << 128) | uint128(uint256(utxoId));
nstExitCounter++;
} else {
priority = getERC20ExitPriority(timestamp, utxoId, pr.txPos);
}
}
tokens[out.color].insert(priority);
exitMapping[utxoId] = Exit({
owner: out.owner,
color: out.color,
amount: out.value,
finalized: false,
stake: exitStake,
priorityTimestamp: timestamp,
tokenData: out.stateRoot
});
exitRoots[utxoId] = _proof[0];
emit ExitStarted(
pr.txHash,
_outputIndex,
out.color,
out.owner,
out.value,
_proof[0]
);
}
function startDepositExit(uint256 _depositId) public payable {
require(msg.value >= exitStake, "Not enough ether sent to pay for exit stake");
// check that deposit exits
Deposit memory deposit = deposits[uint32(_depositId)];
require(deposit.owner == msg.sender, "Only deposit owner can start exit");
require(deposit.amount > 0, "deposit has no value");
require(exitMapping[bytes32(_depositId)].amount == 0, "The exit of deposit has already been started");
require(!exitMapping[bytes32(_depositId)].finalized, "The exit for deposit has already been finalized");
uint256 priority;
if (isNft(deposit.color)) {
priority = (nftExitCounter << 128) | uint128(_depositId);
nftExitCounter++;
} else if (isNST(deposit.color)) {
priority = (nstExitCounter << 128) | uint128(_depositId);
nstExitCounter++;
} else {
priority = getERC20ExitPriority(uint32(deposit.time), bytes32(_depositId), 0);
}
tokens[deposit.color].insert(priority);
exitMapping[bytes32(_depositId)] = Exit({
owner: deposit.owner,
color: deposit.color,
amount: deposit.amount,
finalized: false,
stake: exitStake,
priorityTimestamp: uint32(now),
tokenData: "0x"
});
// no need to emit ExitStartedV2
// no need to update emit data root for NSTs, as it only got deposit now.
emit ExitStarted(
bytes32(_depositId),
0,
deposit.color,
deposit.owner,
deposit.amount,
0
);
}
// @dev Finalizes exit for the chosen color with the highest priority
function finalizeExits(uint16 _color) public {
bytes32 utxoId;
uint256 exitableAt;
Exit memory currentExit;
(utxoId, exitableAt) = getNextExit(_color);
require(tokens[_color].currentSize > 0, "Queue empty for color.");
for (uint i = 0; i < 20; i++) {
// if queue is empty or top exit cannot be exited yet, stop
if (exitableAt > block.timestamp) {
return;
}
currentExit = exitMapping[utxoId];
// exits of deleted periods are not payed out but simply deleted
if (exitRoots[utxoId] > 0) {
uint32 timestamp;
(, timestamp,,) = bridge.periods(exitRoots[utxoId]);
if (timestamp == 0) {
// stake goes to governance contract
address(uint160(bridge.admin())).send(currentExit.stake);
tokens[currentExit.color].delMin();
exitMapping[utxoId].finalized = true;
return;
}
}
if (currentExit.owner != address(0) || currentExit.amount != 0) { // exit was not removed
// Note: for NFTs, the amount is actually the NFT id (both uint256)
if (isNft(currentExit.color)) {
tokens[currentExit.color].addr.transferFrom(address(this), currentExit.owner, currentExit.amount);
} else if (isNST(currentExit.color)) {
bytes32 tokenData = currentExit.tokenData;
address tokenAddr = address(tokens[currentExit.color].addr);
bool success;
(success, ) = tokenAddr.call(abi.encodeWithSignature("writeData(uint256,bytes32)", currentExit.amount, tokenData));
// if set data did not work, we assume the token hasn't been minted yet
if (!success) {
tokenAddr.call(
abi.encodeWithSignature(
"breed(uint256,address,bytes32)",
currentExit.amount, currentExit.owner, tokenData
)
);
} else {
// only if we were able to setData we try to transfer
tokens[currentExit.color].addr.transferFrom(address(this), currentExit.owner, currentExit.amount);
}
} else {
// why so complicated? why not transfer()?
tokens[currentExit.color].addr.approve(address(this), currentExit.amount);
tokens[currentExit.color].addr.transferFrom(address(this), currentExit.owner, currentExit.amount);
}
// Pay exit stake
address(uint160(currentExit.owner)).send(currentExit.stake);
}
tokens[currentExit.color].delMin();
exitMapping[utxoId].finalized = true;
if (tokens[currentExit.color].currentSize > 0) {
(utxoId, exitableAt) = getNextExit(_color);
} else {
return;
}
}
}
// @dev For backwards compatibility reasons...
function finalizeTopExit(uint16 _color) public {
finalizeExits(_color);
}
function challengeExit(
bytes32[] memory _proof,
bytes32[] memory _prevProof,
uint8 _outputIndex,
uint8 _inputIndex,
address challenger
) public {
require(msg.sender == challenger, "Wrong challenger");
uint32 timestamp;
if (_prevProof.length > 0) {
// check period for _prevProof
(, timestamp,,) = bridge.periods(_prevProof[0]);
require(timestamp > 0, "The referenced period was not submitted to bridge");
}
// validate exiting tx
uint256 offset = 32 * (_proof.length + 2);
bytes32 txHash1;
bytes memory txData;
(, txHash1, txData) = TxLib.validateProof(offset + 96, _prevProof);
bytes32 utxoId = bytes32(uint256(_outputIndex) << 120 | uint120(uint256(txHash1)));
TxLib.Tx memory txn;
if (_proof.length > 0) {
// check period for _proof
(, timestamp,,) = bridge.periods(_proof[0]);
require(timestamp > 0, "The referenced period was not submitted to bridge");
// validate spending tx
bytes32 txHash;
(, txHash, txData) = TxLib.validateProof(128, _proof);
txn = TxLib.parseTx(txData);
// make sure one is spending the other one
require(txHash1 == txn.ins[_inputIndex].outpoint.hash, "prevout check failed on hash");
require(_outputIndex == txn.ins[_inputIndex].outpoint.pos, "prevout check failed on pos");
// if transfer, make sure signature correct
if (txn.txType == TxLib.TxType.Transfer) {
bytes32 sigHash = TxLib.getSigHash(txData);
address signer = ecrecover(
sigHash,
txn.ins[_inputIndex].v,
txn.ins[_inputIndex].r,
txn.ins[_inputIndex].s
);
require(exitMapping[utxoId].owner == signer, "signer does not match");
} else if (txn.txType == TxLib.TxType.SpendCond) { // solium-disable-line whitespace
// check that hash of contract matches output address
// just have the pass through
// later we will check solEVM Enforcer here.
} else {
revert("unknown tx type");
}
} else {
// challenging deposit exit
txn = TxLib.parseTx(txData);
utxoId = txn.ins[_inputIndex].outpoint.hash;
if (txn.txType == TxLib.TxType.Deposit) {
// check that deposit was included correctly
// only then it should be usable for challenge
Deposit memory deposit = deposits[uint32(uint256(utxoId))];
require(deposit.amount == txn.outs[0].value, "value mismatch");
require(deposit.owner == txn.outs[0].owner, "owner mismatch");
require(deposit.color == txn.outs[0].color, "color mismatch");
if (isNST(deposit.color)) {
require(tokenData[uint32(uint256(utxoId))] == txn.outs[0].stateRoot, "data mismatch");
}
// todo: check timely inclusion of deposit tx
// this will prevent grieving attacks by the operator
} else {
revert("unexpected tx type");
}
}
require(exitMapping[utxoId].amount > 0, "exit not found");
require(!exitMapping[utxoId].finalized, "The exit has already been finalized");
// award stake to challanger
msg.sender.transfer(exitMapping[utxoId].stake);
// delete invalid exit
delete exitMapping[utxoId];
}
function challengeYoungestInput(
bytes32[] memory _youngerInputProof,
bytes32[] memory _exitingTxProof,
uint8 _outputIndex,
uint8 _inputIndex,
address challenger
) public {
require(msg.sender == challenger, "Wrong challenger");
// validate exiting input tx
bytes32 txHash;
bytes memory txData;
(, txHash, txData) = TxLib.validateProof(32 * (_youngerInputProof.length + 2) + 96, _exitingTxProof);
bytes32 utxoId = bytes32(uint256(_outputIndex) << 120 | uint120(uint256(txHash)));
// check the exit exists
require(exitMapping[utxoId].amount > 0, "There is no exit for this UTXO");
TxLib.Tx memory exitingTx = TxLib.parseTx(txData);
// validate younger input tx
(,txHash,) = TxLib.validateProof(128, _youngerInputProof);
// check younger input is actually an input of exiting tx
require(txHash == exitingTx.ins[_inputIndex].outpoint.hash, "Given output is not referenced in exiting tx");
uint32 youngerInputTimestamp;
(,youngerInputTimestamp,,) = bridge.periods(_youngerInputProof[0]);
require(youngerInputTimestamp > 0, "The referenced period was not submitted to bridge");
require(exitMapping[utxoId].priorityTimestamp < youngerInputTimestamp, "Challenged input should be older");
// award stake to challanger
msg.sender.transfer(exitMapping[utxoId].stake);
// delete invalid exit
delete exitMapping[utxoId];
}
function getNextExit(uint16 _color) internal view returns (bytes32 utxoId, uint256 exitableAt) {
uint256 priority = tokens[_color].getMin();
utxoId = bytes32(uint256(uint128(priority)));
exitableAt = priority >> 192;
}
function isNft(uint16 _color) internal pure returns (bool) {
return (_color >= NFT_FIRST_COLOR) && (_color < NST_FIRST_COLOR);
}
function isNST(uint16 _color) internal pure returns (bool) {
return _color >= NST_FIRST_COLOR;
}
function getERC20ExitPriority(
uint32 timestamp, bytes32 utxoId, uint64 txPos
) internal view returns (uint256 priority) {
uint256 exitableAt = Math.max(timestamp + (2 * exitDuration), block.timestamp + exitDuration);
return (exitableAt << 192) | uint256(txPos) << 128 | uint128(uint256(utxoId));
}
// solium-disable-next-line mixedcase
uint256[48] private ______gap;
}