-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.json
297 lines (297 loc) · 45.7 KB
/
input.json
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
{
"language": "Solidity",
"sources": {
"lib/fx-portal/contracts/lib/ExitPayloadReader.sol": {
"content": "pragma solidity ^0.8.0;\n\nimport {RLPReader} from \"./RLPReader.sol\";\n\nlibrary ExitPayloadReader {\n using RLPReader for bytes;\n using RLPReader for RLPReader.RLPItem;\n\n uint8 constant WORD_SIZE = 32;\n\n struct ExitPayload {\n RLPReader.RLPItem[] data;\n }\n\n struct Receipt {\n RLPReader.RLPItem[] data;\n bytes raw;\n uint256 logIndex;\n }\n\n struct Log {\n RLPReader.RLPItem data;\n RLPReader.RLPItem[] list;\n }\n\n struct LogTopics {\n RLPReader.RLPItem[] data;\n }\n\n // copy paste of private copy() from RLPReader to avoid changing of existing contracts\n function copy(\n uint256 src,\n uint256 dest,\n uint256 len\n ) private pure {\n if (len == 0) return;\n\n // copy as many word sizes as possible\n for (; len >= WORD_SIZE; len -= WORD_SIZE) {\n assembly {\n mstore(dest, mload(src))\n }\n\n src += WORD_SIZE;\n dest += WORD_SIZE;\n }\n\n if (len == 0) return;\n\n // left over bytes. Mask is used to remove unwanted bytes from the word\n uint256 mask = 256**(WORD_SIZE - len) - 1;\n assembly {\n let srcpart := and(mload(src), not(mask)) // zero out src\n let destpart := and(mload(dest), mask) // retrieve the bytes\n mstore(dest, or(destpart, srcpart))\n }\n }\n\n function toExitPayload(bytes memory data) internal pure returns (ExitPayload memory) {\n RLPReader.RLPItem[] memory payloadData = data.toRlpItem().toList();\n\n return ExitPayload(payloadData);\n }\n\n function getHeaderNumber(ExitPayload memory payload) internal pure returns (uint256) {\n return payload.data[0].toUint();\n }\n\n function getBlockProof(ExitPayload memory payload) internal pure returns (bytes memory) {\n return payload.data[1].toBytes();\n }\n\n function getBlockNumber(ExitPayload memory payload) internal pure returns (uint256) {\n return payload.data[2].toUint();\n }\n\n function getBlockTime(ExitPayload memory payload) internal pure returns (uint256) {\n return payload.data[3].toUint();\n }\n\n function getTxRoot(ExitPayload memory payload) internal pure returns (bytes32) {\n return bytes32(payload.data[4].toUint());\n }\n\n function getReceiptRoot(ExitPayload memory payload) internal pure returns (bytes32) {\n return bytes32(payload.data[5].toUint());\n }\n\n function getReceipt(ExitPayload memory payload) internal pure returns (Receipt memory receipt) {\n receipt.raw = payload.data[6].toBytes();\n RLPReader.RLPItem memory receiptItem = receipt.raw.toRlpItem();\n\n if (receiptItem.isList()) {\n // legacy tx\n receipt.data = receiptItem.toList();\n } else {\n // pop first byte before parsing receipt\n bytes memory typedBytes = receipt.raw;\n bytes memory result = new bytes(typedBytes.length - 1);\n uint256 srcPtr;\n uint256 destPtr;\n assembly {\n srcPtr := add(33, typedBytes)\n destPtr := add(0x20, result)\n }\n\n copy(srcPtr, destPtr, result.length);\n receipt.data = result.toRlpItem().toList();\n }\n\n receipt.logIndex = getReceiptLogIndex(payload);\n return receipt;\n }\n\n function getReceiptProof(ExitPayload memory payload) internal pure returns (bytes memory) {\n return payload.data[7].toBytes();\n }\n\n function getBranchMaskAsBytes(ExitPayload memory payload) internal pure returns (bytes memory) {\n return payload.data[8].toBytes();\n }\n\n function getBranchMaskAsUint(ExitPayload memory payload) internal pure returns (uint256) {\n return payload.data[8].toUint();\n }\n\n function getReceiptLogIndex(ExitPayload memory payload) internal pure returns (uint256) {\n return payload.data[9].toUint();\n }\n\n // Receipt methods\n function toBytes(Receipt memory receipt) internal pure returns (bytes memory) {\n return receipt.raw;\n }\n\n function getLog(Receipt memory receipt) internal pure returns (Log memory) {\n RLPReader.RLPItem memory logData = receipt.data[3].toList()[receipt.logIndex];\n return Log(logData, logData.toList());\n }\n\n // Log methods\n function getEmitter(Log memory log) internal pure returns (address) {\n return RLPReader.toAddress(log.list[0]);\n }\n\n function getTopics(Log memory log) internal pure returns (LogTopics memory) {\n return LogTopics(log.list[1].toList());\n }\n\n function getData(Log memory log) internal pure returns (bytes memory) {\n return log.list[2].toBytes();\n }\n\n function toRlpBytes(Log memory log) internal pure returns (bytes memory) {\n return log.data.toRlpBytes();\n }\n\n // LogTopics methods\n function getField(LogTopics memory topics, uint256 index) internal pure returns (RLPReader.RLPItem memory) {\n return topics.data[index];\n }\n}\n"
},
"lib/fx-portal/contracts/lib/Merkle.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nlibrary Merkle {\n function checkMembership(\n bytes32 leaf,\n uint256 index,\n bytes32 rootHash,\n bytes memory proof\n ) internal pure returns (bool) {\n require(proof.length % 32 == 0, \"Invalid proof length\");\n uint256 proofHeight = proof.length / 32;\n // Proof of size n means, height of the tree is n+1.\n // In a tree of height n+1, max #leafs possible is 2 ^ n\n require(index < 2**proofHeight, \"Leaf index is too big\");\n\n bytes32 proofElement;\n bytes32 computedHash = leaf;\n for (uint256 i = 32; i <= proof.length; i += 32) {\n assembly {\n proofElement := mload(add(proof, i))\n }\n\n if (index % 2 == 0) {\n computedHash = keccak256(abi.encodePacked(computedHash, proofElement));\n } else {\n computedHash = keccak256(abi.encodePacked(proofElement, computedHash));\n }\n\n index = index / 2;\n }\n return computedHash == rootHash;\n }\n}\n"
},
"lib/fx-portal/contracts/lib/MerklePatriciaProof.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport {RLPReader} from \"./RLPReader.sol\";\n\nlibrary MerklePatriciaProof {\n /*\n * @dev Verifies a merkle patricia proof.\n * @param value The terminating value in the trie.\n * @param encodedPath The path in the trie leading to value.\n * @param rlpParentNodes The rlp encoded stack of nodes.\n * @param root The root hash of the trie.\n * @return The boolean validity of the proof.\n */\n function verify(\n bytes memory value,\n bytes memory encodedPath,\n bytes memory rlpParentNodes,\n bytes32 root\n ) internal pure returns (bool) {\n RLPReader.RLPItem memory item = RLPReader.toRlpItem(rlpParentNodes);\n RLPReader.RLPItem[] memory parentNodes = RLPReader.toList(item);\n\n bytes memory currentNode;\n RLPReader.RLPItem[] memory currentNodeList;\n\n bytes32 nodeKey = root;\n uint256 pathPtr = 0;\n\n bytes memory path = _getNibbleArray(encodedPath);\n if (path.length == 0) {\n return false;\n }\n\n for (uint256 i = 0; i < parentNodes.length; i++) {\n if (pathPtr > path.length) {\n return false;\n }\n\n currentNode = RLPReader.toRlpBytes(parentNodes[i]);\n if (nodeKey != keccak256(currentNode)) {\n return false;\n }\n currentNodeList = RLPReader.toList(parentNodes[i]);\n\n if (currentNodeList.length == 17) {\n if (pathPtr == path.length) {\n if (keccak256(RLPReader.toBytes(currentNodeList[16])) == keccak256(value)) {\n return true;\n } else {\n return false;\n }\n }\n\n uint8 nextPathNibble = uint8(path[pathPtr]);\n if (nextPathNibble > 16) {\n return false;\n }\n nodeKey = bytes32(RLPReader.toUintStrict(currentNodeList[nextPathNibble]));\n pathPtr += 1;\n } else if (currentNodeList.length == 2) {\n uint256 traversed = _nibblesToTraverse(RLPReader.toBytes(currentNodeList[0]), path, pathPtr);\n if (pathPtr + traversed == path.length) {\n //leaf node\n if (keccak256(RLPReader.toBytes(currentNodeList[1])) == keccak256(value)) {\n return true;\n } else {\n return false;\n }\n }\n\n //extension node\n if (traversed == 0) {\n return false;\n }\n\n pathPtr += traversed;\n nodeKey = bytes32(RLPReader.toUintStrict(currentNodeList[1]));\n } else {\n return false;\n }\n }\n }\n\n function _nibblesToTraverse(\n bytes memory encodedPartialPath,\n bytes memory path,\n uint256 pathPtr\n ) private pure returns (uint256) {\n uint256 len = 0;\n // encodedPartialPath has elements that are each two hex characters (1 byte), but partialPath\n // and slicedPath have elements that are each one hex character (1 nibble)\n bytes memory partialPath = _getNibbleArray(encodedPartialPath);\n bytes memory slicedPath = new bytes(partialPath.length);\n\n // pathPtr counts nibbles in path\n // partialPath.length is a number of nibbles\n for (uint256 i = pathPtr; i < pathPtr + partialPath.length; i++) {\n bytes1 pathNibble = path[i];\n slicedPath[i - pathPtr] = pathNibble;\n }\n\n if (keccak256(partialPath) == keccak256(slicedPath)) {\n len = partialPath.length;\n } else {\n len = 0;\n }\n return len;\n }\n\n // bytes b must be hp encoded\n function _getNibbleArray(bytes memory b) internal pure returns (bytes memory) {\n bytes memory nibbles = \"\";\n if (b.length > 0) {\n uint8 offset;\n uint8 hpNibble = uint8(_getNthNibbleOfBytes(0, b));\n if (hpNibble == 1 || hpNibble == 3) {\n nibbles = new bytes(b.length * 2 - 1);\n bytes1 oddNibble = _getNthNibbleOfBytes(1, b);\n nibbles[0] = oddNibble;\n offset = 1;\n } else {\n nibbles = new bytes(b.length * 2 - 2);\n offset = 0;\n }\n\n for (uint256 i = offset; i < nibbles.length; i++) {\n nibbles[i] = _getNthNibbleOfBytes(i - offset + 2, b);\n }\n }\n return nibbles;\n }\n\n function _getNthNibbleOfBytes(uint256 n, bytes memory str) private pure returns (bytes1) {\n return bytes1(n % 2 == 0 ? uint8(str[n / 2]) / 0x10 : uint8(str[n / 2]) % 0x10);\n }\n}\n"
},
"lib/fx-portal/contracts/lib/RLPReader.sol": {
"content": "/*\n * @author Hamdi Allam [email protected]\n * Please reach out with any questions or concerns\n */\npragma solidity ^0.8.0;\n\nlibrary RLPReader {\n uint8 constant STRING_SHORT_START = 0x80;\n uint8 constant STRING_LONG_START = 0xb8;\n uint8 constant LIST_SHORT_START = 0xc0;\n uint8 constant LIST_LONG_START = 0xf8;\n uint8 constant WORD_SIZE = 32;\n\n struct RLPItem {\n uint256 len;\n uint256 memPtr;\n }\n\n struct Iterator {\n RLPItem item; // Item that's being iterated over.\n uint256 nextPtr; // Position of the next item in the list.\n }\n\n /*\n * @dev Returns the next element in the iteration. Reverts if it has not next element.\n * @param self The iterator.\n * @return The next element in the iteration.\n */\n function next(Iterator memory self) internal pure returns (RLPItem memory) {\n require(hasNext(self));\n\n uint256 ptr = self.nextPtr;\n uint256 itemLength = _itemLength(ptr);\n self.nextPtr = ptr + itemLength;\n\n return RLPItem(itemLength, ptr);\n }\n\n /*\n * @dev Returns true if the iteration has more elements.\n * @param self The iterator.\n * @return true if the iteration has more elements.\n */\n function hasNext(Iterator memory self) internal pure returns (bool) {\n RLPItem memory item = self.item;\n return self.nextPtr < item.memPtr + item.len;\n }\n\n /*\n * @param item RLP encoded bytes\n */\n function toRlpItem(bytes memory item) internal pure returns (RLPItem memory) {\n uint256 memPtr;\n assembly {\n memPtr := add(item, 0x20)\n }\n\n return RLPItem(item.length, memPtr);\n }\n\n /*\n * @dev Create an iterator. Reverts if item is not a list.\n * @param self The RLP item.\n * @return An 'Iterator' over the item.\n */\n function iterator(RLPItem memory self) internal pure returns (Iterator memory) {\n require(isList(self));\n\n uint256 ptr = self.memPtr + _payloadOffset(self.memPtr);\n return Iterator(self, ptr);\n }\n\n /*\n * @param item RLP encoded bytes\n */\n function rlpLen(RLPItem memory item) internal pure returns (uint256) {\n return item.len;\n }\n\n /*\n * @param item RLP encoded bytes\n */\n function payloadLen(RLPItem memory item) internal pure returns (uint256) {\n return item.len - _payloadOffset(item.memPtr);\n }\n\n /*\n * @param item RLP encoded list in bytes\n */\n function toList(RLPItem memory item) internal pure returns (RLPItem[] memory) {\n require(isList(item));\n\n uint256 items = numItems(item);\n RLPItem[] memory result = new RLPItem[](items);\n\n uint256 memPtr = item.memPtr + _payloadOffset(item.memPtr);\n uint256 dataLen;\n for (uint256 i = 0; i < items; i++) {\n dataLen = _itemLength(memPtr);\n result[i] = RLPItem(dataLen, memPtr);\n memPtr = memPtr + dataLen;\n }\n\n return result;\n }\n\n // @return indicator whether encoded payload is a list. negate this function call for isData.\n function isList(RLPItem memory item) internal pure returns (bool) {\n if (item.len == 0) return false;\n\n uint8 byte0;\n uint256 memPtr = item.memPtr;\n assembly {\n byte0 := byte(0, mload(memPtr))\n }\n\n if (byte0 < LIST_SHORT_START) return false;\n return true;\n }\n\n /*\n * @dev A cheaper version of keccak256(toRlpBytes(item)) that avoids copying memory.\n * @return keccak256 hash of RLP encoded bytes.\n */\n function rlpBytesKeccak256(RLPItem memory item) internal pure returns (bytes32) {\n uint256 ptr = item.memPtr;\n uint256 len = item.len;\n bytes32 result;\n assembly {\n result := keccak256(ptr, len)\n }\n return result;\n }\n\n function payloadLocation(RLPItem memory item) internal pure returns (uint256, uint256) {\n uint256 offset = _payloadOffset(item.memPtr);\n uint256 memPtr = item.memPtr + offset;\n uint256 len = item.len - offset; // data length\n return (memPtr, len);\n }\n\n /*\n * @dev A cheaper version of keccak256(toBytes(item)) that avoids copying memory.\n * @return keccak256 hash of the item payload.\n */\n function payloadKeccak256(RLPItem memory item) internal pure returns (bytes32) {\n (uint256 memPtr, uint256 len) = payloadLocation(item);\n bytes32 result;\n assembly {\n result := keccak256(memPtr, len)\n }\n return result;\n }\n\n /** RLPItem conversions into data types **/\n\n // @returns raw rlp encoding in bytes\n function toRlpBytes(RLPItem memory item) internal pure returns (bytes memory) {\n bytes memory result = new bytes(item.len);\n if (result.length == 0) return result;\n\n uint256 ptr;\n assembly {\n ptr := add(0x20, result)\n }\n\n copy(item.memPtr, ptr, item.len);\n return result;\n }\n\n // any non-zero byte < 128 is considered true\n function toBoolean(RLPItem memory item) internal pure returns (bool) {\n require(item.len == 1);\n uint256 result;\n uint256 memPtr = item.memPtr;\n assembly {\n result := byte(0, mload(memPtr))\n }\n\n return result == 0 ? false : true;\n }\n\n function toAddress(RLPItem memory item) internal pure returns (address) {\n // 1 byte for the length prefix\n require(item.len == 21);\n\n return address(uint160(toUint(item)));\n }\n\n function toUint(RLPItem memory item) internal pure returns (uint256) {\n require(item.len > 0 && item.len <= 33);\n\n uint256 offset = _payloadOffset(item.memPtr);\n uint256 len = item.len - offset;\n\n uint256 result;\n uint256 memPtr = item.memPtr + offset;\n assembly {\n result := mload(memPtr)\n\n // shift to the correct location if neccesary\n if lt(len, 32) {\n result := div(result, exp(256, sub(32, len)))\n }\n }\n\n return result;\n }\n\n // enforces 32 byte length\n function toUintStrict(RLPItem memory item) internal pure returns (uint256) {\n // one byte prefix\n require(item.len == 33);\n\n uint256 result;\n uint256 memPtr = item.memPtr + 1;\n assembly {\n result := mload(memPtr)\n }\n\n return result;\n }\n\n function toBytes(RLPItem memory item) internal pure returns (bytes memory) {\n require(item.len > 0);\n\n uint256 offset = _payloadOffset(item.memPtr);\n uint256 len = item.len - offset; // data length\n bytes memory result = new bytes(len);\n\n uint256 destPtr;\n assembly {\n destPtr := add(0x20, result)\n }\n\n copy(item.memPtr + offset, destPtr, len);\n return result;\n }\n\n /*\n * Private Helpers\n */\n\n // @return number of payload items inside an encoded list.\n function numItems(RLPItem memory item) private pure returns (uint256) {\n if (item.len == 0) return 0;\n\n uint256 count = 0;\n uint256 currPtr = item.memPtr + _payloadOffset(item.memPtr);\n uint256 endPtr = item.memPtr + item.len;\n while (currPtr < endPtr) {\n currPtr = currPtr + _itemLength(currPtr); // skip over an item\n count++;\n }\n\n return count;\n }\n\n // @return entire rlp item byte length\n function _itemLength(uint256 memPtr) private pure returns (uint256) {\n uint256 itemLen;\n uint256 byte0;\n assembly {\n byte0 := byte(0, mload(memPtr))\n }\n\n if (byte0 < STRING_SHORT_START) itemLen = 1;\n else if (byte0 < STRING_LONG_START) itemLen = byte0 - STRING_SHORT_START + 1;\n else if (byte0 < LIST_SHORT_START) {\n assembly {\n let byteLen := sub(byte0, 0xb7) // # of bytes the actual length is\n memPtr := add(memPtr, 1) // skip over the first byte\n /* 32 byte word size */\n let dataLen := div(mload(memPtr), exp(256, sub(32, byteLen))) // right shifting to get the len\n itemLen := add(dataLen, add(byteLen, 1))\n }\n } else if (byte0 < LIST_LONG_START) {\n itemLen = byte0 - LIST_SHORT_START + 1;\n } else {\n assembly {\n let byteLen := sub(byte0, 0xf7)\n memPtr := add(memPtr, 1)\n\n let dataLen := div(mload(memPtr), exp(256, sub(32, byteLen))) // right shifting to the correct length\n itemLen := add(dataLen, add(byteLen, 1))\n }\n }\n\n return itemLen;\n }\n\n // @return number of bytes until the data\n function _payloadOffset(uint256 memPtr) private pure returns (uint256) {\n uint256 byte0;\n assembly {\n byte0 := byte(0, mload(memPtr))\n }\n\n if (byte0 < STRING_SHORT_START) return 0;\n else if (byte0 < STRING_LONG_START || (byte0 >= LIST_SHORT_START && byte0 < LIST_LONG_START)) return 1;\n else if (byte0 < LIST_SHORT_START)\n // being explicit\n return byte0 - (STRING_LONG_START - 1) + 1;\n else return byte0 - (LIST_LONG_START - 1) + 1;\n }\n\n /*\n * @param src Pointer to source\n * @param dest Pointer to destination\n * @param len Amount of memory to copy from the source\n */\n function copy(\n uint256 src,\n uint256 dest,\n uint256 len\n ) private pure {\n if (len == 0) return;\n\n // copy as many word sizes as possible\n for (; len >= WORD_SIZE; len -= WORD_SIZE) {\n assembly {\n mstore(dest, mload(src))\n }\n\n src += WORD_SIZE;\n dest += WORD_SIZE;\n }\n\n if (len == 0) return;\n\n // left over bytes. Mask is used to remove unwanted bytes from the word\n uint256 mask = 256**(WORD_SIZE - len) - 1;\n\n assembly {\n let srcpart := and(mload(src), not(mask)) // zero out src\n let destpart := and(mload(dest), mask) // retrieve the bytes\n mstore(dest, or(destpart, srcpart))\n }\n }\n}\n"
},
"lib/fx-portal/contracts/tunnel/FxBaseChildTunnel.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\n// IFxMessageProcessor represents interface to process message\ninterface IFxMessageProcessor {\n function processMessageFromRoot(\n uint256 stateId,\n address rootMessageSender,\n bytes calldata data\n ) external;\n}\n\n/**\n * @notice Mock child tunnel contract to receive and send message from L2\n */\nabstract contract FxBaseChildTunnel is IFxMessageProcessor {\n // MessageTunnel on L1 will get data from this event\n event MessageSent(bytes message);\n\n // fx child\n address public fxChild;\n\n // fx root tunnel\n address public fxRootTunnel;\n\n constructor(address _fxChild) {\n fxChild = _fxChild;\n }\n\n // Sender must be fxRootTunnel in case of ERC20 tunnel\n modifier validateSender(address sender) {\n require(sender == fxRootTunnel, \"FxBaseChildTunnel: INVALID_SENDER_FROM_ROOT\");\n _;\n }\n\n // set fxRootTunnel if not set already\n function setFxRootTunnel(address _fxRootTunnel) external virtual {\n require(fxRootTunnel == address(0x0), \"FxBaseChildTunnel: ROOT_TUNNEL_ALREADY_SET\");\n fxRootTunnel = _fxRootTunnel;\n }\n\n function processMessageFromRoot(\n uint256 stateId,\n address rootMessageSender,\n bytes calldata data\n ) external override {\n require(msg.sender == fxChild, \"FxBaseChildTunnel: INVALID_SENDER\");\n _processMessageFromRoot(stateId, rootMessageSender, data);\n }\n\n /**\n * @notice Emit message that can be received on Root Tunnel\n * @dev Call the internal function when need to emit message\n * @param message bytes message that will be sent to Root Tunnel\n * some message examples -\n * abi.encode(tokenId);\n * abi.encode(tokenId, tokenMetadata);\n * abi.encode(messageType, messageData);\n */\n function _sendMessageToRoot(bytes memory message) internal {\n emit MessageSent(message);\n }\n\n /**\n * @notice Process message received from Root Tunnel\n * @dev function needs to be implemented to handle message as per requirement\n * This is called by onStateReceive function.\n * Since it is called via a system call, any event will not be emitted during its execution.\n * @param stateId unique state id\n * @param sender root message sender\n * @param message bytes message that was sent from Root Tunnel\n */\n function _processMessageFromRoot(\n uint256 stateId,\n address sender,\n bytes memory message\n ) internal virtual;\n}\n"
},
"lib/fx-portal/contracts/tunnel/FxBaseRootTunnel.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport {RLPReader} from \"../lib/RLPReader.sol\";\nimport {MerklePatriciaProof} from \"../lib/MerklePatriciaProof.sol\";\nimport {Merkle} from \"../lib/Merkle.sol\";\nimport \"../lib/ExitPayloadReader.sol\";\n\ninterface IFxStateSender {\n function sendMessageToChild(address _receiver, bytes calldata _data) external;\n}\n\ncontract ICheckpointManager {\n struct HeaderBlock {\n bytes32 root;\n uint256 start;\n uint256 end;\n uint256 createdAt;\n address proposer;\n }\n\n /**\n * @notice mapping of checkpoint header numbers to block details\n * @dev These checkpoints are submited by plasma contracts\n */\n mapping(uint256 => HeaderBlock) public headerBlocks;\n}\n\nabstract contract FxBaseRootTunnel {\n using RLPReader for RLPReader.RLPItem;\n using Merkle for bytes32;\n using ExitPayloadReader for bytes;\n using ExitPayloadReader for ExitPayloadReader.ExitPayload;\n using ExitPayloadReader for ExitPayloadReader.Log;\n using ExitPayloadReader for ExitPayloadReader.LogTopics;\n using ExitPayloadReader for ExitPayloadReader.Receipt;\n\n // keccak256(MessageSent(bytes))\n bytes32 public constant SEND_MESSAGE_EVENT_SIG = 0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036;\n\n // state sender contract\n IFxStateSender public fxRoot;\n // root chain manager\n ICheckpointManager public checkpointManager;\n // child tunnel contract which receives and sends messages\n address public fxChildTunnel;\n\n // storage to avoid duplicate exits\n mapping(bytes32 => bool) public processedExits;\n\n constructor(address _checkpointManager, address _fxRoot) {\n checkpointManager = ICheckpointManager(_checkpointManager);\n fxRoot = IFxStateSender(_fxRoot);\n }\n\n // set fxChildTunnel if not set already\n function setFxChildTunnel(address _fxChildTunnel) public virtual {\n require(fxChildTunnel == address(0x0), \"FxBaseRootTunnel: CHILD_TUNNEL_ALREADY_SET\");\n fxChildTunnel = _fxChildTunnel;\n }\n\n /**\n * @notice Send bytes message to Child Tunnel\n * @param message bytes message that will be sent to Child Tunnel\n * some message examples -\n * abi.encode(tokenId);\n * abi.encode(tokenId, tokenMetadata);\n * abi.encode(messageType, messageData);\n */\n function _sendMessageToChild(bytes memory message) internal {\n fxRoot.sendMessageToChild(fxChildTunnel, message);\n }\n\n function _validateAndExtractMessage(bytes memory inputData) internal returns (bytes memory) {\n ExitPayloadReader.ExitPayload memory payload = inputData.toExitPayload();\n\n bytes memory branchMaskBytes = payload.getBranchMaskAsBytes();\n uint256 blockNumber = payload.getBlockNumber();\n // checking if exit has already been processed\n // unique exit is identified using hash of (blockNumber, branchMask, receiptLogIndex)\n bytes32 exitHash = keccak256(\n abi.encodePacked(\n blockNumber,\n // first 2 nibbles are dropped while generating nibble array\n // this allows branch masks that are valid but bypass exitHash check (changing first 2 nibbles only)\n // so converting to nibble array and then hashing it\n MerklePatriciaProof._getNibbleArray(branchMaskBytes),\n payload.getReceiptLogIndex()\n )\n );\n require(processedExits[exitHash] == false, \"FxRootTunnel: EXIT_ALREADY_PROCESSED\");\n processedExits[exitHash] = true;\n\n ExitPayloadReader.Receipt memory receipt = payload.getReceipt();\n ExitPayloadReader.Log memory log = receipt.getLog();\n\n // check child tunnel\n require(fxChildTunnel == log.getEmitter(), \"FxRootTunnel: INVALID_FX_CHILD_TUNNEL\");\n\n bytes32 receiptRoot = payload.getReceiptRoot();\n // verify receipt inclusion\n require(\n MerklePatriciaProof.verify(receipt.toBytes(), branchMaskBytes, payload.getReceiptProof(), receiptRoot),\n \"FxRootTunnel: INVALID_RECEIPT_PROOF\"\n );\n\n // verify checkpoint inclusion\n _checkBlockMembershipInCheckpoint(\n blockNumber,\n payload.getBlockTime(),\n payload.getTxRoot(),\n receiptRoot,\n payload.getHeaderNumber(),\n payload.getBlockProof()\n );\n\n ExitPayloadReader.LogTopics memory topics = log.getTopics();\n\n require(\n bytes32(topics.getField(0).toUint()) == SEND_MESSAGE_EVENT_SIG, // topic0 is event sig\n \"FxRootTunnel: INVALID_SIGNATURE\"\n );\n\n // received message data\n bytes memory message = abi.decode(log.getData(), (bytes)); // event decodes params again, so decoding bytes to get message\n return message;\n }\n\n function _checkBlockMembershipInCheckpoint(\n uint256 blockNumber,\n uint256 blockTime,\n bytes32 txRoot,\n bytes32 receiptRoot,\n uint256 headerNumber,\n bytes memory blockProof\n ) private view {\n (bytes32 headerRoot, uint256 startBlock, , uint256 createdAt, ) = checkpointManager.headerBlocks(headerNumber);\n\n require(\n keccak256(abi.encodePacked(blockNumber, blockTime, txRoot, receiptRoot)).checkMembership(\n blockNumber - startBlock,\n headerRoot,\n blockProof\n ),\n \"FxRootTunnel: INVALID_HEADER\"\n );\n }\n\n /**\n * @notice receive message from L2 to L1, validated by proof\n * @dev This function verifies if the transaction actually happened on child chain\n *\n * @param inputData RLP encoded data of the reference tx containing following list of fields\n * 0 - headerNumber - Checkpoint header block number containing the reference tx\n * 1 - blockProof - Proof that the block header (in the child chain) is a leaf in the submitted merkle root\n * 2 - blockNumber - Block number containing the reference tx on child chain\n * 3 - blockTime - Reference tx block time\n * 4 - txRoot - Transactions root of block\n * 5 - receiptRoot - Receipts root of block\n * 6 - receipt - Receipt of the reference transaction\n * 7 - receiptProof - Merkle proof of the reference receipt\n * 8 - branchMask - 32 bits denoting the path of receipt in merkle tree\n * 9 - receiptLogIndex - Log Index to read from the receipt\n */\n function receiveMessage(bytes memory inputData) public virtual {\n bytes memory message = _validateAndExtractMessage(inputData);\n _processMessageFromChild(message);\n }\n\n /**\n * @notice Process message received from Child Tunnel\n * @dev function needs to be implemented to handle message as per requirement\n * This is called by receiveMessage function.\n * Since it is called via a system call, any event will not be emitted during its execution.\n * @param message bytes message that was sent from Child Tunnel\n */\n function _processMessageFromChild(bytes memory message) internal virtual;\n}\n"
},
"lib/rocketpool/contracts/interface/RocketStorageInterface.sol": {
"content": "pragma solidity >0.5.0 <0.9.0;\n\n// SPDX-License-Identifier: GPL-3.0-only\n\ninterface RocketStorageInterface {\n\n // Deploy status\n function getDeployedStatus() external view returns (bool);\n\n // Guardian\n function getGuardian() external view returns(address);\n function setGuardian(address _newAddress) external;\n function confirmGuardian() external;\n\n // Getters\n function getAddress(bytes32 _key) external view returns (address);\n function getUint(bytes32 _key) external view returns (uint);\n function getString(bytes32 _key) external view returns (string memory);\n function getBytes(bytes32 _key) external view returns (bytes memory);\n function getBool(bytes32 _key) external view returns (bool);\n function getInt(bytes32 _key) external view returns (int);\n function getBytes32(bytes32 _key) external view returns (bytes32);\n\n // Setters\n function setAddress(bytes32 _key, address _value) external;\n function setUint(bytes32 _key, uint _value) external;\n function setString(bytes32 _key, string calldata _value) external;\n function setBytes(bytes32 _key, bytes calldata _value) external;\n function setBool(bytes32 _key, bool _value) external;\n function setInt(bytes32 _key, int _value) external;\n function setBytes32(bytes32 _key, bytes32 _value) external;\n\n // Deleters\n function deleteAddress(bytes32 _key) external;\n function deleteUint(bytes32 _key) external;\n function deleteString(bytes32 _key) external;\n function deleteBytes(bytes32 _key) external;\n function deleteBool(bytes32 _key) external;\n function deleteInt(bytes32 _key) external;\n function deleteBytes32(bytes32 _key) external;\n\n // Arithmetic\n function addUint(bytes32 _key, uint256 _amount) external;\n function subUint(bytes32 _key, uint256 _amount) external;\n\n // Protected storage\n function getNodeWithdrawalAddress(address _nodeAddress) external view returns (address);\n function getNodePendingWithdrawalAddress(address _nodeAddress) external view returns (address);\n function setWithdrawalAddress(address _nodeAddress, address _newWithdrawalAddress, bool _confirm) external;\n function confirmWithdrawalAddress(address _nodeAddress) external;\n}\n"
},
"lib/rocketpool/contracts/interface/network/RocketNetworkBalancesInterface.sol": {
"content": "pragma solidity >0.5.0 <0.9.0;\n\n// SPDX-License-Identifier: GPL-3.0-only\n\ninterface RocketNetworkBalancesInterface {\n function getBalancesBlock() external view returns (uint256);\n function getLatestReportableBlock() external view returns (uint256);\n function getTotalETHBalance() external view returns (uint256);\n function getStakingETHBalance() external view returns (uint256);\n function getTotalRETHSupply() external view returns (uint256);\n function getETHUtilizationRate() external view returns (uint256);\n function submitBalances(uint256 _block, uint256 _total, uint256 _staking, uint256 _rethSupply) external;\n function executeUpdateBalances(uint256 _block, uint256 _totalEth, uint256 _stakingEth, uint256 _rethSupply) external;\n}\n"
},
"src/RocketPolygonPriceMessenger.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity ^0.8.13;\n\nimport \"@fx-portal/tunnel/FxBaseRootTunnel.sol\";\n\nimport \"rocketpool/contracts/interface/network/RocketNetworkBalancesInterface.sol\";\nimport \"rocketpool/contracts/interface/RocketStorageInterface.sol\";\n\n/// @author Kane Wallmann (Rocket Pool)\n/// @notice Retrieves the rETH exchange rate from Rocket Pool and submits it to the oracle contract on Polygon\ncontract RocketPolygonPriceMessenger is FxBaseRootTunnel {\n // Immutables\n RocketStorageInterface immutable rocketStorage;\n bytes32 immutable rocketNetworkBalancesKey;\n\n /// @notice The most recently submitted rate\n uint256 lastRate;\n\n constructor(RocketStorageInterface _rocketStorage, address _checkpointManager, address _fxRoot) FxBaseRootTunnel(_checkpointManager, _fxRoot) {\n rocketStorage = _rocketStorage;\n // Precompute storage key for RocketNetworkBalances address\n rocketNetworkBalancesKey = keccak256(abi.encodePacked(\"contract.address\", \"rocketNetworkBalances\"));\n }\n\n /// @notice Not used\n function _processMessageFromChild(bytes memory data) internal override {\n revert();\n }\n\n /// @notice Returns whether the rate has changed since it was last submitted\n function rateStale() external view returns (bool) {\n return rate() != lastRate;\n }\n\n /// @notice Returns the calculated rETH exchange rate\n function rate() public view returns (uint256) {\n // Retrieve the inputs from RocketNetworkBalances and calculate the rate\n RocketNetworkBalancesInterface rocketNetworkBalances = RocketNetworkBalancesInterface(rocketStorage.getAddress(rocketNetworkBalancesKey));\n uint256 supply = rocketNetworkBalances.getTotalRETHSupply();\n if (supply == 0) {\n return 0;\n }\n return 1 ether * rocketNetworkBalances.getTotalETHBalance() / supply;\n }\n\n /// @notice Submits the current rETH exchange rate to the L2 contract\n function submitRate() external {\n lastRate = rate();\n // Send the cross chain message\n bytes memory data = abi.encodeWithSignature('updateRate(uint256)', lastRate);\n _sendMessageToChild(data);\n }\n}\n"
},
"src/RocketBalancerRateProvider.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity ^0.8.13;\n\nimport \"./RocketPolygonPriceOracle.sol\";\nimport \"./interfaces/balancer/IRateProvider.sol\";\n\n/// @author Kane Wallmann (Rocket Pool)\n/// @notice Implements Balancer's IRateProvider interface\ncontract RocketBalancerRateProvider is IRateProvider {\n RocketPolygonPriceOracle immutable oracle;\n\n constructor(RocketPolygonPriceOracle _oracle) {\n oracle = _oracle;\n }\n\n function getRate() external override view returns (uint256) {\n return oracle.rate();\n }\n}"
},
"src/interfaces/balancer/IRateProvider.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-or-later\n// This program is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n\n// This program is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n\n// You should have received a copy of the GNU General Public License\n// along with this program. If not, see <http://www.gnu.org/licenses/>.\n\npragma solidity ^0.8.0;\n\ninterface IRateProvider {\n function getRate() external view returns (uint256);\n}"
},
"src/RocketPolygonPriceOracle.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity ^0.8.13;\n\nimport \"@fx-portal/tunnel/FxBaseChildTunnel.sol\";\n\n/// @author Kane Wallmann (Rocket Pool)\n/// @notice Receives updates from L1 on the canonical rETH exchange rate\ncontract RocketPolygonPriceOracle is FxBaseChildTunnel {\n // Events\n event RateUpdated(uint256 rate);\n\n /// @notice The rETH exchange rate in the form of how much ETH 1 rETH is worth\n uint256 public rate;\n\n /// @notice The timestamp of the block in which the rate was last updated\n uint256 public lastUpdated;\n\n constructor(address _fxChild) FxBaseChildTunnel(_fxChild) {\n }\n\n /// @notice Processes an incoming message from L1\n function _processMessageFromRoot(\n uint256 stateId,\n address sender,\n bytes memory data\n ) internal override validateSender(sender) {\n // Execute the transaction on self\n (bool success, ) = address(this).call(data);\n require(success, \"Failed to execute transaction on child\");\n }\n\n /// @notice Called by the messenger contract on L1 to update the exchange rate\n function updateRate(uint256 _newRate) external {\n // Only allow calls from self\n require(msg.sender == address(this));\n // Update state\n rate = _newRate;\n lastUpdated = block.timestamp;\n // Emit event\n emit RateUpdated(_newRate);\n }\n}\n"
}
},
"settings": {
"remappings": [
"@fx-portal/=lib/fx-portal/contracts/",
"@openzeppelin/=lib/openzeppelin-contracts/",
"@rocketpool/=lib/rocketpool/contracts/",
"ds-test/=lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-std/=lib/forge-std/src/",
"fx-portal/=lib/fx-portal/contracts/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/",
"rocketpool/=lib/rocketpool/",
"src/=src/",
"test/=test/",
"script/=script/"
],
"optimizer": {
"enabled": true,
"runs": 10000
},
"metadata": {
"bytecodeHash": "ipfs"
},
"outputSelection": {
"lib/forge-std/lib/ds-test/src/test.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/Base.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdAssertions.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdChains.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdCheats.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdError.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdInvariant.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdJson.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdMath.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdStorage.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdStyle.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/StdUtils.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/Test.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/Vm.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/console.sol": {
"*": []
},
"lib/forge-std/src/console2.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/forge-std/src/interfaces/IMulticall3.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
},
"lib/fx-portal/contracts/lib/ExitPayloadReader.sol": {
"*": []
},
"lib/fx-portal/contracts/lib/Merkle.sol": {
"*": []
},
"lib/fx-portal/contracts/lib/MerklePatriciaProof.sol": {
"*": []
},
"lib/fx-portal/contracts/lib/RLPReader.sol": {
"*": []
},
"lib/fx-portal/contracts/tunnel/FxBaseChildTunnel.sol": {
"*": []
},
"lib/fx-portal/contracts/tunnel/FxBaseRootTunnel.sol": {
"*": []
},
"lib/rocketpool/contracts/interface/RocketStorageInterface.sol": {
"*": []
},
"lib/rocketpool/contracts/interface/network/RocketNetworkBalancesInterface.sol": {
"*": []
},
"src/RocketPolygonPriceMessenger.sol": {
"*": []
},
"src/RocketPolygonPriceOracle.sol": {
"*": []
},
"src/mock/PolygonStateSenderMock.sol": {
"*": []
},
"src/mock/RocketNetworkBalancesMock.sol": {
"*": []
},
"src/mock/RocketStorageMock.sol": {
"*": []
},
"test/RocketPolygonPriceOracle.t.sol": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
]
}
},
"evmVersion": "london",
"libraries": {}
}
}