-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBridgeMock.sol
More file actions
477 lines (384 loc) · 15.2 KB
/
Copy pathBridgeMock.sol
File metadata and controls
477 lines (384 loc) · 15.2 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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {IBridge} from "src/interfaces/IBridge.sol";
contract BridgeMock is IBridge {
mapping(bytes32 => uint256) private amounts;
mapping(uint256 => bytes) private headers;
mapping(bytes32 => bytes) private headersByHash;
int256 private bestBlock;
int256 private confirmations;
// RSKIP-502 state variables
uint256 private lockingCap = 400 ether;
uint256 private weisTransferredToUnionBridge = 0;
bool private transfersDisabled = false;
address payable private unionBridgeContractAddress = payable(address(0));
uint256 private maxIncreaseMultiplier = 2; // Maximum allowed increase (e.g., 2 = doubling)
// RSKIP-529: Base Event state variable
bytes private baseEvent;
bytes private superEvent;
bool public storeEvents = false;
// accept rbtc from blockchain and send it to the union bridge contract
receive() external payable {}
function registerFastBridgeBtcTransaction(
bytes memory,
uint256,
bytes memory,
bytes32 derivationArgumentsHash,
bytes memory,
address payable liquidityBridgeContractAddress,
bytes memory,
bool
) external override returns (int256) {
uint256 amount = amounts[derivationArgumentsHash];
amounts[derivationArgumentsHash] = 0;
(bool success,) = liquidityBridgeContractAddress.call{value: amount}("");
require(success, "Sending funds failed");
return int256(amount);
}
function getBtcBlockchainBlockHeaderByHeight(uint256 height) external view override returns (bytes memory) {
return headers[height];
}
function setPegin(bytes32 derivationArgumentsHash) public payable {
amounts[derivationArgumentsHash] = msg.value;
}
function setHeader(uint256 height, bytes memory header) public {
headers[height] = header;
}
function setHeaderByHash(bytes32 blockHash, bytes memory header) public {
headersByHash[blockHash] = header;
}
function getActivePowpegRedeemScript() external pure returns (bytes memory) {
bytes memory part1 = hex"522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab5";
bytes memory part2 = hex"7dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da86";
bytes memory part3 = hex"3c9ed534e0878657175b132b8ca630f245df04db53ae";
return abi.encodePacked(part1, part2, part3);
}
function setBtcBlockchainBestChainHeight(int256 _block) external {
bestBlock = _block;
}
function getBtcBlockchainBestChainHeight() external view override returns (int256) {
return bestBlock;
}
function getStateForBtcReleaseClient() external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getStateForDebugging() external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getBtcTxHashProcessedHeight(string calldata) external pure override returns (int64) {
return int64(0);
}
function isBtcTxHashAlreadyProcessed(string calldata) external pure override returns (bool) {
return false;
}
function getFederationAddress() external pure override returns (string memory) {
return "2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p";
} // regtest genesis fed addr
function registerBtcTransaction(bytes calldata atx, int256 height, bytes calldata pmt) external override {}
function addSignature(bytes calldata pubkey, bytes[] calldata signatures, bytes calldata txhash)
external
override
{}
function receiveHeaders(bytes[] calldata blocks) external override {}
function receiveHeader(bytes calldata) external pure override returns (int256) {
return int256(0);
}
function getFederationSize() external pure override returns (int256) {
return int256(0);
}
function getFederationThreshold() external pure override returns (int256) {
return int256(0);
}
function getFederatorPublicKey(int256) external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getFederatorPublicKeyOfType(int256, string calldata) external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getFederationCreationTime() external pure override returns (int256) {
return int256(0);
}
function getFederationCreationBlockNumber() external pure override returns (int256) {
return int256(0);
}
function getRetiringFederationAddress() external pure override returns (string memory) {
return "";
}
function getRetiringFederationSize() external pure override returns (int256) {
return int256(0);
}
function getRetiringFederationThreshold() external pure override returns (int256) {
return int256(0);
}
function getRetiringFederatorPublicKey(int256) external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getRetiringFederatorPublicKeyOfType(int256, string calldata)
external
pure
override
returns (bytes memory)
{
bytes memory b;
return b;
}
function getRetiringFederationCreationTime() external pure override returns (int256) {
return int256(0);
}
function getRetiringFederationCreationBlockNumber() external pure override returns (int256) {
return int256(0);
}
function createFederation() external pure override returns (int256) {
return int256(0);
}
function addFederatorPublicKey(bytes calldata) external pure override returns (int256) {
return int256(0);
}
function addFederatorPublicKeyMultikey(bytes calldata, bytes calldata, bytes calldata)
external
pure
override
returns (int256)
{
return int256(0);
}
function commitFederation(bytes calldata) external pure override returns (int256) {
return int256(0);
}
function rollbackFederation() external pure override returns (int256) {
return int256(0);
}
function getPendingFederationHash() external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getPendingFederationSize() external pure override returns (int256) {
return int256(0);
}
function getPendingFederatorPublicKey(int256) external pure override returns (bytes memory) {
bytes memory b;
return b;
}
function getPendingFederatorPublicKeyOfType(int256, string calldata)
external
pure
override
returns (bytes memory)
{
bytes memory b;
return b;
}
function getLockWhitelistSize() external pure override returns (int256) {
return int256(0);
}
function getLockWhitelistAddress(int256) external pure override returns (string memory) {
return "";
}
function getLockWhitelistEntryByAddress(string calldata) external pure override returns (int256) {
return int256(0);
}
function addLockWhitelistAddress(string calldata, int256) external pure override returns (int256) {
return int256(0);
}
function addOneOffLockWhitelistAddress(string calldata, int256) external pure override returns (int256) {
return int256(0);
}
function addUnlimitedLockWhitelistAddress(string calldata) external pure override returns (int256) {
return int256(0);
}
function removeLockWhitelistAddress(string calldata) external pure override returns (int256) {
return int256(0);
}
function setLockWhitelistDisableBlockDelay(int256) external pure override returns (int256) {
return int256(0);
}
function getFeePerKb() external pure override returns (int256) {
return int256(0);
}
function voteFeePerKbChange(int256) external pure override returns (int256) {
return int256(0);
}
function updateCollections() external override {}
function getMinimumLockTxValue() external pure override returns (int256) {
return int256(2);
}
function setBtcTransactionConfirmations(int256 _confirmations) external {
confirmations = _confirmations;
}
function getBtcTransactionConfirmations(bytes32, bytes32, uint256, bytes32[] calldata)
external
view
override
returns (int256)
{
return confirmations;
}
function getLockingCap() external pure override returns (int256) {
return int256(0);
}
function increaseLockingCap(int256) external pure override returns (bool) {
return false;
}
function registerBtcCoinbaseTransaction(
bytes calldata btcTxSerialized,
bytes32 blockHash,
bytes calldata pmtSerialized,
bytes32 witnessMerkleRoot,
bytes32 witnessReservedValue
) external override {}
function hasBtcBlockCoinbaseTransactionInformation(bytes32) external pure override returns (bool) {
return false;
}
function getActiveFederationCreationBlockHeight() external pure override returns (uint256) {
return uint256(0);
}
function getBtcBlockchainBestBlockHeader() external view override returns (bytes memory) {
// Check if there's a header set for the current RSK block number
// This allows tests to return different BTC headers for different RSK blocks
if (headers[block.number].length > 0) {
return headers[block.number];
}
// Return a mock Bitcoin block hash for testing purposes
// bytes corresponding to block 904463 hash 0000000000000000000049b460f18614380a01b8709d2c3a8ddf451d08d862b8
// This simulates a Bitcoin best block header
bytes memory b =
hex"0000003c9d087b22bc8c482ad398dcfed27a115490cfbc144b3801000000000000000000a923a1461ac1dd2b222f3525f071bec2026acee9038ee440d6915488894a1452e7d66b6816680217f35754aa";
return b;
}
function getBtcBlockchainBlockHeaderByHash(bytes32 blockHash) external view override returns (bytes memory) {
return headersByHash[blockHash];
}
function getBtcBlockchainParentBlockHeaderByHash(bytes32) external pure override returns (bytes memory) {
bytes memory b;
return b;
}
// ===================== RSKIP-502: Union Bridge RBTC Methods =====================
function requestUnionBridgeRbtcHarness() external payable returns (int256) {
return _requestUnionBridgeRbtc(msg.value);
}
function _requestUnionBridgeRbtc(uint256 amount) internal returns (int256) {
if (amount > lockingCap) {
return int256(-2);
}
if (transfersDisabled) {
return int256(-3);
}
weisTransferredToUnionBridge += amount;
lockingCap -= amount;
(bool success,) = unionBridgeContractAddress.call{value: amount}("");
require(success, "BridgeMock: Sending funds failed");
return int256(0);
}
function requestUnionBridgeRbtc(uint256 amount) external override returns (int256) {
if (msg.sender != unionBridgeContractAddress) {
return int256(-1);
}
return _requestUnionBridgeRbtc(amount);
}
function releaseUnionBridgeRbtcHarness() external payable returns (int256) {
return _releaseUnionBridgeRbtc();
}
function _releaseUnionBridgeRbtc() internal returns (int256) {
if (transfersDisabled) {
return int256(-3);
}
if (msg.value > weisTransferredToUnionBridge) {
return int256(-2);
}
weisTransferredToUnionBridge -= msg.value;
lockingCap += msg.value;
return int256(0);
}
function releaseUnionBridgeRbtc() external payable override returns (int256) {
if (msg.sender != unionBridgeContractAddress) {
return int256(-1);
}
return _releaseUnionBridgeRbtc();
}
function getUnionBridgeLockingCap() external view override returns (uint256) {
return lockingCap;
}
function increaseUnionBridgeLockingCap(uint256 newCap) external override returns (int256) {
uint256 currentTotalCap = lockingCap + weisTransferredToUnionBridge;
// Check that newCap is actually an increase (RSKIP-502: cap can only be increased)
if (newCap <= currentTotalCap) {
return int256(-2); // Invalid value - less than current cap
}
// Check that increase is not excessive (RSKIP-502: increase should be controlled)
uint256 maxAllowedCap = currentTotalCap * maxIncreaseMultiplier;
if (newCap > maxAllowedCap) {
return int256(-2); // Invalid value - excessive increase
}
// Update the available capacity
lockingCap = newCap - weisTransferredToUnionBridge;
return int256(0);
}
function setUnionBridgeContractAddressForTestnet(address newAddress) external override returns (int256) {
unionBridgeContractAddress = payable(newAddress);
return int256(0);
}
function getUnionBridgeContractAddress() external view override returns (address) {
return unionBridgeContractAddress;
}
// ===================== RSKIP-529: Base Event Methods =====================
function getBaseEvent() external view override returns (bytes memory) {
return baseEvent;
}
function setBaseEvent(bytes memory _baseEvent) external override onlyUnionBridgeContract {
if (_baseEvent.length > 128) {
revert("BaseEventTooLong");
}
if (_baseEvent.length == 0) {
revert("EmptyBaseEvent");
}
if (storeEvents) {
baseEvent = _baseEvent;
}
}
function clearBaseEvent() external override onlyUnionBridgeContract {
baseEvent = "";
}
function getSuperEvent() external view override returns (bytes memory) {
return superEvent;
}
function setSuperEvent(bytes memory _superEvent) external override onlyUnionBridgeContract {
if (_superEvent.length > 128) {
revert("SuperEventTooLong");
}
if (_superEvent.length == 0) {
revert("EmptySuperEvent");
}
if (storeEvents) {
superEvent = _superEvent;
}
}
function clearSuperEvent() external override onlyUnionBridgeContract {
superEvent = "";
}
//-------------------------------- Test Helpers ------------------------------------
function setTransfersDisabled(bool _disabled) external {
transfersDisabled = _disabled;
}
function setWeisTransferredToUnionBridge(uint256 _amount) external {
uint256 totalCap = lockingCap + weisTransferredToUnionBridge;
weisTransferredToUnionBridge = _amount;
lockingCap = totalCap - _amount;
}
function setStoreEvents(bool _storeEvents) external {
storeEvents = _storeEvents;
}
modifier onlyUnionBridgeContract() {
_onlyUnionBridgeContract(msg.sender);
_;
}
function _onlyUnionBridgeContract(address _sender) internal view {
if (_sender != unionBridgeContractAddress) {
revert("OnlyUnionBridgeContract");
}
}
}