@arbitrum/nitro-contracts 1.0.0-beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. package/.prettierrc +5 -0
  2. package/.solhint.json +18 -0
  3. package/deploy/BridgeStubCreator.js +10 -0
  4. package/deploy/HashProofHelper.js +13 -0
  5. package/deploy/InboxStubCreator.js +17 -0
  6. package/deploy/OneStepProofEntryCreator.js +19 -0
  7. package/deploy/OneStepProver0Creator.js +14 -0
  8. package/deploy/OneStepProverHostIoCreator.js +14 -0
  9. package/deploy/OneStepProverMathCreator.js +14 -0
  10. package/deploy/OneStepProverMemoryCreator.js +14 -0
  11. package/deploy/SequencerInboxStubCreator.js +13 -0
  12. package/deploy/ValueArrayTesterCreator.js +13 -0
  13. package/hardhat.config.ts +47 -0
  14. package/hardhat.prod-config.js +18 -0
  15. package/package.json +49 -0
  16. package/scripts/build.bash +5 -0
  17. package/src/bridge/Bridge.sol +168 -0
  18. package/src/bridge/IBridge.sol +68 -0
  19. package/src/bridge/IInbox.sol +80 -0
  20. package/src/bridge/IMessageProvider.sol +11 -0
  21. package/src/bridge/IOutbox.sol +52 -0
  22. package/src/bridge/ISequencerInbox.sol +85 -0
  23. package/src/bridge/Inbox.sol +414 -0
  24. package/src/bridge/Messages.sol +38 -0
  25. package/src/bridge/Outbox.sol +188 -0
  26. package/src/bridge/SequencerInbox.sol +274 -0
  27. package/src/challenge/ChallengeLib.sol +135 -0
  28. package/src/challenge/ChallengeManager.sol +367 -0
  29. package/src/challenge/IChallengeManager.sol +75 -0
  30. package/src/challenge/IChallengeResultReceiver.sol +13 -0
  31. package/src/libraries/AddressAliasHelper.sol +29 -0
  32. package/src/libraries/AdminFallbackProxy.sol +153 -0
  33. package/src/libraries/ArbitrumProxy.sol +20 -0
  34. package/src/libraries/Constants.sol +10 -0
  35. package/src/libraries/CryptographyPrimitives.sol +323 -0
  36. package/src/libraries/DelegateCallAware.sol +44 -0
  37. package/src/libraries/Error.sol +38 -0
  38. package/src/libraries/IGasRefunder.sol +35 -0
  39. package/src/libraries/MerkleLib.sol +46 -0
  40. package/src/libraries/MessageTypes.sol +14 -0
  41. package/src/libraries/SecondaryLogicUUPSUpgradeable.sol +58 -0
  42. package/src/libraries/UUPSNotUpgradeable.sol +56 -0
  43. package/src/mocks/BridgeStub.sol +115 -0
  44. package/src/mocks/Counter.sol +13 -0
  45. package/src/mocks/ExecutionManager.sol +41 -0
  46. package/src/mocks/InboxStub.sol +131 -0
  47. package/src/mocks/MockResultReceiver.sol +59 -0
  48. package/src/mocks/SequencerInboxStub.sol +42 -0
  49. package/src/mocks/SimpleProxy.sol +19 -0
  50. package/src/node-interface/NodeInterface.sol +50 -0
  51. package/src/osp/HashProofHelper.sol +154 -0
  52. package/src/osp/IOneStepProofEntry.sol +20 -0
  53. package/src/osp/IOneStepProver.sol +27 -0
  54. package/src/osp/OneStepProofEntry.sol +129 -0
  55. package/src/osp/OneStepProver0.sol +566 -0
  56. package/src/osp/OneStepProverHostIo.sol +357 -0
  57. package/src/osp/OneStepProverMath.sol +514 -0
  58. package/src/osp/OneStepProverMemory.sol +313 -0
  59. package/src/precompiles/ArbAddressTable.sol +60 -0
  60. package/src/precompiles/ArbAggregator.sol +62 -0
  61. package/src/precompiles/ArbBLS.sol +53 -0
  62. package/src/precompiles/ArbDebug.sol +39 -0
  63. package/src/precompiles/ArbFunctionTable.sol +29 -0
  64. package/src/precompiles/ArbGasInfo.sol +121 -0
  65. package/src/precompiles/ArbInfo.sol +15 -0
  66. package/src/precompiles/ArbOwner.sol +65 -0
  67. package/src/precompiles/ArbOwnerPublic.sol +18 -0
  68. package/src/precompiles/ArbRetryableTx.sol +89 -0
  69. package/src/precompiles/ArbStatistics.sol +29 -0
  70. package/src/precompiles/ArbSys.sol +134 -0
  71. package/src/precompiles/ArbosActs.sol +41 -0
  72. package/src/precompiles/ArbosTest.sol +14 -0
  73. package/src/rollup/BridgeCreator.sol +120 -0
  74. package/src/rollup/IRollupCore.sol +152 -0
  75. package/src/rollup/IRollupLogic.sol +183 -0
  76. package/src/rollup/Node.sol +99 -0
  77. package/src/rollup/RollupAdminLogic.sol +322 -0
  78. package/src/rollup/RollupCore.sol +627 -0
  79. package/src/rollup/RollupCreator.sol +133 -0
  80. package/src/rollup/RollupEventBridge.sol +46 -0
  81. package/src/rollup/RollupLib.sol +135 -0
  82. package/src/rollup/RollupUserLogic.sol +712 -0
  83. package/src/rollup/ValidatorUtils.sol +243 -0
  84. package/src/rollup/ValidatorWallet.sol +76 -0
  85. package/src/rollup/ValidatorWalletCreator.sol +43 -0
  86. package/src/state/Deserialize.sol +321 -0
  87. package/src/state/GlobalState.sol +44 -0
  88. package/src/state/Instructions.sol +159 -0
  89. package/src/state/Machine.sol +65 -0
  90. package/src/state/MerkleProof.sol +99 -0
  91. package/src/state/Module.sol +33 -0
  92. package/src/state/ModuleMemory.sol +42 -0
  93. package/src/state/PcArray.sol +45 -0
  94. package/src/state/PcStack.sol +32 -0
  95. package/src/state/StackFrame.sol +63 -0
  96. package/src/state/Value.sol +65 -0
  97. package/src/state/ValueArray.sol +47 -0
  98. package/src/state/ValueStack.sol +39 -0
  99. package/src/test-helpers/CryptographyPrimitivesTester.sol +27 -0
  100. package/src/test-helpers/MessageTester.sol +34 -0
  101. package/src/test-helpers/ValueArrayTester.sol +34 -0
  102. package/test/contract/arbRollup.spec.ts +869 -0
  103. package/test/contract/common/challengeLib.ts +43 -0
  104. package/test/contract/common/globalStateLib.ts +17 -0
  105. package/test/contract/common/rolluplib.ts +259 -0
  106. package/test/contract/cryptographyPrimitives.spec.ts +82 -0
  107. package/test/contract/sequencerInboxForceInclude.spec.ts +516 -0
  108. package/test/contract/utils.ts +40 -0
  109. package/test/prover/hash-proofs.ts +75 -0
  110. package/test/prover/one-step-proof.ts +93 -0
  111. package/test/prover/proofs/.gitkeep +0 -0
  112. package/test/prover/value-arrays.ts +11 -0
  113. package/tsconfig.json +13 -0
@@ -0,0 +1,323 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity ^0.8.0;
6
+
7
+ /// This algorithm has been extracted from the implementation of smart pool (https://github.com/smartpool)
8
+ library CryptographyPrimitives {
9
+ // WARNING: This function has the keccak state in a weird order.
10
+ // If the normal Keccak state is [0, 1, 2, 3, 4, 5, 6, ..., 24]
11
+ // this function has its state as [0, 5, 10, 15, 20, 1, 6, 11, ..., 24]
12
+ function keccakF(uint256[25] memory a) internal pure returns (uint256[25] memory) {
13
+ uint256[5] memory c;
14
+ uint256[5] memory d;
15
+ //uint D_0; uint D_1; uint D_2; uint D_3; uint D_4;
16
+ uint256[25] memory b;
17
+
18
+ uint256[24] memory rc = [
19
+ uint256(0x0000000000000001),
20
+ 0x0000000000008082,
21
+ 0x800000000000808A,
22
+ 0x8000000080008000,
23
+ 0x000000000000808B,
24
+ 0x0000000080000001,
25
+ 0x8000000080008081,
26
+ 0x8000000000008009,
27
+ 0x000000000000008A,
28
+ 0x0000000000000088,
29
+ 0x0000000080008009,
30
+ 0x000000008000000A,
31
+ 0x000000008000808B,
32
+ 0x800000000000008B,
33
+ 0x8000000000008089,
34
+ 0x8000000000008003,
35
+ 0x8000000000008002,
36
+ 0x8000000000000080,
37
+ 0x000000000000800A,
38
+ 0x800000008000000A,
39
+ 0x8000000080008081,
40
+ 0x8000000000008080,
41
+ 0x0000000080000001,
42
+ 0x8000000080008008
43
+ ];
44
+
45
+ unchecked {
46
+ for (uint256 i = 0; i < 24; i++) {
47
+ /*
48
+ for( x = 0 ; x < 5 ; x++ ) {
49
+ C[x] = A[5*x]^A[5*x+1]^A[5*x+2]^A[5*x+3]^A[5*x+4];
50
+ }*/
51
+
52
+ c[0] = a[0] ^ a[1] ^ a[2] ^ a[3] ^ a[4];
53
+ c[1] = a[5] ^ a[6] ^ a[7] ^ a[8] ^ a[9];
54
+ c[2] = a[10] ^ a[11] ^ a[12] ^ a[13] ^ a[14];
55
+ c[3] = a[15] ^ a[16] ^ a[17] ^ a[18] ^ a[19];
56
+ c[4] = a[20] ^ a[21] ^ a[22] ^ a[23] ^ a[24];
57
+
58
+ /*
59
+ for( x = 0 ; x < 5 ; x++ ) {
60
+ D[x] = C[(x+4)%5]^((C[(x+1)%5] * 2)&0xffffffffffffffff | (C[(x+1)%5]/(2**63)));
61
+ }*/
62
+
63
+ d[0] = c[4] ^ (((c[1] * 2) & 0xffffffffffffffff) | (c[1] / (2**63)));
64
+ d[1] = c[0] ^ (((c[2] * 2) & 0xffffffffffffffff) | (c[2] / (2**63)));
65
+ d[2] = c[1] ^ (((c[3] * 2) & 0xffffffffffffffff) | (c[3] / (2**63)));
66
+ d[3] = c[2] ^ (((c[4] * 2) & 0xffffffffffffffff) | (c[4] / (2**63)));
67
+ d[4] = c[3] ^ (((c[0] * 2) & 0xffffffffffffffff) | (c[0] / (2**63)));
68
+
69
+ /*
70
+ for( x = 0 ; x < 5 ; x++ ) {
71
+ for( y = 0 ; y < 5 ; y++ ) {
72
+ A[5*x+y] = A[5*x+y] ^ D[x];
73
+ }
74
+ }*/
75
+
76
+ a[0] = a[0] ^ d[0];
77
+ a[1] = a[1] ^ d[0];
78
+ a[2] = a[2] ^ d[0];
79
+ a[3] = a[3] ^ d[0];
80
+ a[4] = a[4] ^ d[0];
81
+ a[5] = a[5] ^ d[1];
82
+ a[6] = a[6] ^ d[1];
83
+ a[7] = a[7] ^ d[1];
84
+ a[8] = a[8] ^ d[1];
85
+ a[9] = a[9] ^ d[1];
86
+ a[10] = a[10] ^ d[2];
87
+ a[11] = a[11] ^ d[2];
88
+ a[12] = a[12] ^ d[2];
89
+ a[13] = a[13] ^ d[2];
90
+ a[14] = a[14] ^ d[2];
91
+ a[15] = a[15] ^ d[3];
92
+ a[16] = a[16] ^ d[3];
93
+ a[17] = a[17] ^ d[3];
94
+ a[18] = a[18] ^ d[3];
95
+ a[19] = a[19] ^ d[3];
96
+ a[20] = a[20] ^ d[4];
97
+ a[21] = a[21] ^ d[4];
98
+ a[22] = a[22] ^ d[4];
99
+ a[23] = a[23] ^ d[4];
100
+ a[24] = a[24] ^ d[4];
101
+
102
+ /*Rho and pi steps*/
103
+ b[0] = a[0];
104
+ b[8] = (((a[1] * (2**36)) & 0xffffffffffffffff) | (a[1] / (2**28)));
105
+ b[11] = (((a[2] * (2**3)) & 0xffffffffffffffff) | (a[2] / (2**61)));
106
+ b[19] = (((a[3] * (2**41)) & 0xffffffffffffffff) | (a[3] / (2**23)));
107
+ b[22] = (((a[4] * (2**18)) & 0xffffffffffffffff) | (a[4] / (2**46)));
108
+ b[2] = (((a[5] * (2**1)) & 0xffffffffffffffff) | (a[5] / (2**63)));
109
+ b[5] = (((a[6] * (2**44)) & 0xffffffffffffffff) | (a[6] / (2**20)));
110
+ b[13] = (((a[7] * (2**10)) & 0xffffffffffffffff) | (a[7] / (2**54)));
111
+ b[16] = (((a[8] * (2**45)) & 0xffffffffffffffff) | (a[8] / (2**19)));
112
+ b[24] = (((a[9] * (2**2)) & 0xffffffffffffffff) | (a[9] / (2**62)));
113
+ b[4] = (((a[10] * (2**62)) & 0xffffffffffffffff) | (a[10] / (2**2)));
114
+ b[7] = (((a[11] * (2**6)) & 0xffffffffffffffff) | (a[11] / (2**58)));
115
+ b[10] = (((a[12] * (2**43)) & 0xffffffffffffffff) | (a[12] / (2**21)));
116
+ b[18] = (((a[13] * (2**15)) & 0xffffffffffffffff) | (a[13] / (2**49)));
117
+ b[21] = (((a[14] * (2**61)) & 0xffffffffffffffff) | (a[14] / (2**3)));
118
+ b[1] = (((a[15] * (2**28)) & 0xffffffffffffffff) | (a[15] / (2**36)));
119
+ b[9] = (((a[16] * (2**55)) & 0xffffffffffffffff) | (a[16] / (2**9)));
120
+ b[12] = (((a[17] * (2**25)) & 0xffffffffffffffff) | (a[17] / (2**39)));
121
+ b[15] = (((a[18] * (2**21)) & 0xffffffffffffffff) | (a[18] / (2**43)));
122
+ b[23] = (((a[19] * (2**56)) & 0xffffffffffffffff) | (a[19] / (2**8)));
123
+ b[3] = (((a[20] * (2**27)) & 0xffffffffffffffff) | (a[20] / (2**37)));
124
+ b[6] = (((a[21] * (2**20)) & 0xffffffffffffffff) | (a[21] / (2**44)));
125
+ b[14] = (((a[22] * (2**39)) & 0xffffffffffffffff) | (a[22] / (2**25)));
126
+ b[17] = (((a[23] * (2**8)) & 0xffffffffffffffff) | (a[23] / (2**56)));
127
+ b[20] = (((a[24] * (2**14)) & 0xffffffffffffffff) | (a[24] / (2**50)));
128
+
129
+ /*Xi state*/
130
+ /*
131
+ for( x = 0 ; x < 5 ; x++ ) {
132
+ for( y = 0 ; y < 5 ; y++ ) {
133
+ A[5*x+y] = B[5*x+y]^((~B[5*((x+1)%5)+y]) & B[5*((x+2)%5)+y]);
134
+ }
135
+ }*/
136
+
137
+ a[0] = b[0] ^ ((~b[5]) & b[10]);
138
+ a[1] = b[1] ^ ((~b[6]) & b[11]);
139
+ a[2] = b[2] ^ ((~b[7]) & b[12]);
140
+ a[3] = b[3] ^ ((~b[8]) & b[13]);
141
+ a[4] = b[4] ^ ((~b[9]) & b[14]);
142
+ a[5] = b[5] ^ ((~b[10]) & b[15]);
143
+ a[6] = b[6] ^ ((~b[11]) & b[16]);
144
+ a[7] = b[7] ^ ((~b[12]) & b[17]);
145
+ a[8] = b[8] ^ ((~b[13]) & b[18]);
146
+ a[9] = b[9] ^ ((~b[14]) & b[19]);
147
+ a[10] = b[10] ^ ((~b[15]) & b[20]);
148
+ a[11] = b[11] ^ ((~b[16]) & b[21]);
149
+ a[12] = b[12] ^ ((~b[17]) & b[22]);
150
+ a[13] = b[13] ^ ((~b[18]) & b[23]);
151
+ a[14] = b[14] ^ ((~b[19]) & b[24]);
152
+ a[15] = b[15] ^ ((~b[20]) & b[0]);
153
+ a[16] = b[16] ^ ((~b[21]) & b[1]);
154
+ a[17] = b[17] ^ ((~b[22]) & b[2]);
155
+ a[18] = b[18] ^ ((~b[23]) & b[3]);
156
+ a[19] = b[19] ^ ((~b[24]) & b[4]);
157
+ a[20] = b[20] ^ ((~b[0]) & b[5]);
158
+ a[21] = b[21] ^ ((~b[1]) & b[6]);
159
+ a[22] = b[22] ^ ((~b[2]) & b[7]);
160
+ a[23] = b[23] ^ ((~b[3]) & b[8]);
161
+ a[24] = b[24] ^ ((~b[4]) & b[9]);
162
+
163
+ /*Last step*/
164
+ a[0] = a[0] ^ rc[i];
165
+ }
166
+ }
167
+
168
+ return a;
169
+ }
170
+
171
+ function rightRotate(uint32 x, uint32 n) internal pure returns (uint32) {
172
+ return ((x) >> (n)) | ((x) << (32 - (n)));
173
+ }
174
+
175
+ function ch(
176
+ uint32 e,
177
+ uint32 f,
178
+ uint32 g
179
+ ) internal pure returns (uint32) {
180
+ return ((e & f) ^ ((~e) & g));
181
+ }
182
+
183
+ // SHA256 compression function that operates on a 512 bit chunk
184
+ // Note that the input must be padded by the caller
185
+ // For the initial chunk, the initial values from the SHA256 spec should be passed in as hashState
186
+ // For subsequent rounds, hashState is the output from the previous round
187
+ function sha256Block(uint256[2] memory inputChunk, uint256 hashState)
188
+ internal
189
+ pure
190
+ returns (uint256)
191
+ {
192
+ unchecked {
193
+ uint32[64] memory k = [
194
+ 0x428a2f98,
195
+ 0x71374491,
196
+ 0xb5c0fbcf,
197
+ 0xe9b5dba5,
198
+ 0x3956c25b,
199
+ 0x59f111f1,
200
+ 0x923f82a4,
201
+ 0xab1c5ed5,
202
+ 0xd807aa98,
203
+ 0x12835b01,
204
+ 0x243185be,
205
+ 0x550c7dc3,
206
+ 0x72be5d74,
207
+ 0x80deb1fe,
208
+ 0x9bdc06a7,
209
+ 0xc19bf174,
210
+ 0xe49b69c1,
211
+ 0xefbe4786,
212
+ 0x0fc19dc6,
213
+ 0x240ca1cc,
214
+ 0x2de92c6f,
215
+ 0x4a7484aa,
216
+ 0x5cb0a9dc,
217
+ 0x76f988da,
218
+ 0x983e5152,
219
+ 0xa831c66d,
220
+ 0xb00327c8,
221
+ 0xbf597fc7,
222
+ 0xc6e00bf3,
223
+ 0xd5a79147,
224
+ 0x06ca6351,
225
+ 0x14292967,
226
+ 0x27b70a85,
227
+ 0x2e1b2138,
228
+ 0x4d2c6dfc,
229
+ 0x53380d13,
230
+ 0x650a7354,
231
+ 0x766a0abb,
232
+ 0x81c2c92e,
233
+ 0x92722c85,
234
+ 0xa2bfe8a1,
235
+ 0xa81a664b,
236
+ 0xc24b8b70,
237
+ 0xc76c51a3,
238
+ 0xd192e819,
239
+ 0xd6990624,
240
+ 0xf40e3585,
241
+ 0x106aa070,
242
+ 0x19a4c116,
243
+ 0x1e376c08,
244
+ 0x2748774c,
245
+ 0x34b0bcb5,
246
+ 0x391c0cb3,
247
+ 0x4ed8aa4a,
248
+ 0x5b9cca4f,
249
+ 0x682e6ff3,
250
+ 0x748f82ee,
251
+ 0x78a5636f,
252
+ 0x84c87814,
253
+ 0x8cc70208,
254
+ 0x90befffa,
255
+ 0xa4506ceb,
256
+ 0xbef9a3f7,
257
+ 0xc67178f2
258
+ ];
259
+
260
+ uint32[64] memory w;
261
+ uint32 i;
262
+ for (i = 0; i < 8; i++) {
263
+ w[i] = uint32(inputChunk[0] >> (224 - (32 * i)));
264
+ w[i + 8] = uint32(inputChunk[1] >> (224 - (32 * i)));
265
+ }
266
+
267
+ uint32 s0;
268
+ uint32 s1;
269
+ for (i = 16; i < 64; i++) {
270
+ s0 = rightRotate(w[i - 15], 7) ^ rightRotate(w[i - 15], 18) ^ (w[i - 15] >> 3);
271
+
272
+ s1 = rightRotate(w[i - 2], 17) ^ rightRotate(w[i - 2], 19) ^ (w[i - 2] >> 10);
273
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
274
+ }
275
+
276
+ uint32[8] memory state;
277
+
278
+ for (i = 0; i < 8; i++) {
279
+ state[i] = uint32(hashState >> (224 - (32 * i)));
280
+ }
281
+
282
+ uint32 temp1;
283
+ uint32 temp2;
284
+ uint32 maj;
285
+
286
+ for (i = 0; i < 64; i++) {
287
+ s1 =
288
+ rightRotate(state[4], 6) ^
289
+ rightRotate(state[4], 11) ^
290
+ rightRotate(state[4], 25);
291
+ temp1 = state[7] + s1 + ch(state[4], state[5], state[6]) + k[i] + w[i];
292
+ s0 =
293
+ rightRotate(state[0], 2) ^
294
+ rightRotate(state[0], 13) ^
295
+ rightRotate(state[0], 22);
296
+
297
+ maj = (state[0] & (state[1] ^ state[2])) ^ (state[1] & state[2]);
298
+ temp2 = s0 + maj;
299
+
300
+ state[7] = state[6];
301
+ state[6] = state[5];
302
+ state[5] = state[4];
303
+ state[4] = state[3] + temp1;
304
+ state[3] = state[2];
305
+ state[2] = state[1];
306
+ state[1] = state[0];
307
+ state[0] = temp1 + temp2;
308
+ }
309
+
310
+ for (i = 0; i < 8; i++) {
311
+ state[i] += uint32(hashState >> (224 - (32 * i)));
312
+ }
313
+
314
+ uint256 result;
315
+
316
+ for (i = 0; i < 8; i++) {
317
+ result |= (uint256(state[i]) << (224 - (32 * i)));
318
+ }
319
+
320
+ return result;
321
+ }
322
+ }
323
+ }
@@ -0,0 +1,44 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity ^0.8.0;
6
+
7
+ import {NotOwner} from "./Error.sol";
8
+
9
+ /// @dev A stateless contract that allows you to infer if the current call has been delegated or not
10
+ /// Pattern used here is from UUPS implementation by the OpenZeppelin team
11
+ abstract contract DelegateCallAware {
12
+ address private immutable __self = address(this);
13
+
14
+ /**
15
+ * @dev Check that the execution is being performed through a delegate call. This allows a function to be
16
+ * callable on the proxy contract but not on the logic contract.
17
+ */
18
+ modifier onlyDelegated() {
19
+ require(address(this) != __self, "Function must be called through delegatecall");
20
+ _;
21
+ }
22
+
23
+ /**
24
+ * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
25
+ * callable on the implementing contract but not through proxies.
26
+ */
27
+ modifier notDelegated() {
28
+ require(address(this) == __self, "Function must not be called through delegatecall");
29
+ _;
30
+ }
31
+
32
+ /// @dev Check that msg.sender is the current EIP 1967 proxy admin
33
+ modifier onlyProxyOwner() {
34
+ // Storage slot with the admin of the proxy contract
35
+ // This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1
36
+ bytes32 slot = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
37
+ address admin;
38
+ assembly {
39
+ admin := sload(slot)
40
+ }
41
+ if (msg.sender != admin) revert NotOwner(msg.sender, admin);
42
+ _;
43
+ }
44
+ }
@@ -0,0 +1,38 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity ^0.8.4;
6
+
7
+ /// @dev Init was already called
8
+ error AlreadyInit();
9
+
10
+ /// Init was called with param set to zero that must be nonzero
11
+ error HadZeroInit();
12
+
13
+ /// @dev Thrown when non owner tries to access an only-owner function
14
+ /// @param sender The msg.sender who is not the owner
15
+ /// @param owner The owner address
16
+ error NotOwner(address sender, address owner);
17
+
18
+ /// @dev Thrown when an address that is not the rollup tries to call an only-rollup function
19
+ /// @param sender The sender who is not the rollup
20
+ /// @param rollup The rollup address authorized to call this function
21
+ error NotRollup(address sender, address rollup);
22
+
23
+ /// @dev Thrown when the contract was not called directly from the origin ie msg.sender != tx.origin
24
+ error NotOrigin();
25
+
26
+ /// @dev Provided data was too large
27
+ /// @param dataLength The length of the data that is too large
28
+ /// @param maxDataLength The max length the data can be
29
+ error DataTooLarge(uint256 dataLength, uint256 maxDataLength);
30
+
31
+ /// @dev The provided is not a contract and was expected to be
32
+ /// @param addr The adddress in question
33
+ error NotContract(address addr);
34
+
35
+ /// @dev The merkle proof provided was too long
36
+ /// @param actualLength The length of the merkle proof provided
37
+ /// @param maxProofLength The max length a merkle proof can have
38
+ error MerkleProofTooLong(uint256 actualLength, uint256 maxProofLength);
@@ -0,0 +1,35 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity >=0.6.11 <0.9.0;
6
+
7
+ interface IGasRefunder {
8
+ function onGasSpent(
9
+ address payable spender,
10
+ uint256 gasUsed,
11
+ uint256 calldataSize
12
+ ) external returns (bool success);
13
+ }
14
+
15
+ abstract contract GasRefundEnabled {
16
+ modifier refundsGasWithCalldata(IGasRefunder gasRefunder, address payable spender) {
17
+ uint256 startGasLeft = gasleft();
18
+ _;
19
+ if (address(gasRefunder) != address(0)) {
20
+ uint256 calldataSize;
21
+ assembly {
22
+ calldataSize := calldatasize()
23
+ }
24
+ gasRefunder.onGasSpent(spender, startGasLeft - gasleft(), calldataSize);
25
+ }
26
+ }
27
+
28
+ modifier refundsGasNoCalldata(IGasRefunder gasRefunder, address payable spender) {
29
+ uint256 startGasLeft = gasleft();
30
+ _;
31
+ if (address(gasRefunder) != address(0)) {
32
+ gasRefunder.onGasSpent(spender, startGasLeft - gasleft(), 0);
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,46 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity ^0.8.4;
6
+
7
+ import {MerkleProofTooLong} from "./Error.sol";
8
+
9
+ library MerkleLib {
10
+ function generateRoot(bytes32[] memory _hashes) internal pure returns (bytes32) {
11
+ bytes32[] memory prevLayer = _hashes;
12
+ while (prevLayer.length > 1) {
13
+ bytes32[] memory nextLayer = new bytes32[]((prevLayer.length + 1) / 2);
14
+ for (uint256 i = 0; i < nextLayer.length; i++) {
15
+ if (2 * i + 1 < prevLayer.length) {
16
+ nextLayer[i] = keccak256(
17
+ abi.encodePacked(prevLayer[2 * i], prevLayer[2 * i + 1])
18
+ );
19
+ } else {
20
+ nextLayer[i] = prevLayer[2 * i];
21
+ }
22
+ }
23
+ prevLayer = nextLayer;
24
+ }
25
+ return prevLayer[0];
26
+ }
27
+
28
+ function calculateRoot(
29
+ bytes32[] memory nodes,
30
+ uint256 route,
31
+ bytes32 item
32
+ ) internal pure returns (bytes32) {
33
+ uint256 proofItems = nodes.length;
34
+ if (proofItems > 256) revert MerkleProofTooLong(proofItems, 256);
35
+ bytes32 h = item;
36
+ for (uint256 i = 0; i < proofItems; i++) {
37
+ if (route % 2 == 0) {
38
+ h = keccak256(abi.encodePacked(h, nodes[i]));
39
+ } else {
40
+ h = keccak256(abi.encodePacked(nodes[i], h));
41
+ }
42
+ route /= 2;
43
+ }
44
+ return h;
45
+ }
46
+ }
@@ -0,0 +1,14 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity ^0.8.4;
6
+
7
+ uint8 constant L2_MSG = 3;
8
+ uint8 constant L1MessageType_L2FundedByL1 = 7;
9
+ uint8 constant L1MessageType_submitRetryableTx = 9;
10
+ uint8 constant L2MessageType_unsignedEOATx = 0;
11
+ uint8 constant L2MessageType_unsignedContractTx = 1;
12
+
13
+ uint8 constant ROLLUP_PROTOCOL_EVENT_TYPE = 8;
14
+ uint8 constant INITIALIZATION_MSG_TYPE = 11;
@@ -0,0 +1,58 @@
1
+ // Copyright 2021-2022, Offchain Labs, Inc.
2
+ // For license information, see https://github.com/nitro/blob/master/LICENSE
3
+ // SPDX-License-Identifier: BUSL-1.1
4
+
5
+ pragma solidity ^0.8.0;
6
+
7
+ import {DoubleLogicERC1967Upgrade} from "./AdminFallbackProxy.sol";
8
+ import "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
9
+
10
+ /// @notice An extension to OZ's UUPSUpgradeable contract to be used for handling UUPS upgrades with a DoubleLogicERC1967Upgrade proxy
11
+ /// @dev upgrades should be handles by the primary logic contract in order to pass the `onlyProxy` check
12
+ abstract contract SecondaryLogicUUPSUpgradeable is UUPSUpgradeable, DoubleLogicERC1967Upgrade {
13
+ /// @inheritdoc UUPSUpgradeable
14
+ function proxiableUUID() external view override notDelegated returns (bytes32) {
15
+ return _IMPLEMENTATION_SLOT;
16
+ }
17
+
18
+ /**
19
+ * @dev Function that should revert when `msg.sender` is not authorized to upgrade the secondary contract. Called by
20
+ * {upgradeSecondaryTo} and {upgradeSecondaryToAndCall}.
21
+ *
22
+ * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
23
+ *
24
+ * ```solidity
25
+ * function _authorizeSecondaryUpgrade(address) internal override onlyOwner {}
26
+ * ```
27
+ */
28
+ function _authorizeSecondaryUpgrade(address newImplementation) internal virtual;
29
+
30
+ /**
31
+ * @dev Upgrade the secondary implementation of the proxy to `newImplementation`.
32
+ *
33
+ * Calls {_authorizeSecondaryUpgrade}.
34
+ *
35
+ * Emits an {UpgradedSecondary} event.
36
+ */
37
+ function upgradeSecondaryTo(address newImplementation) external onlyProxy {
38
+ _authorizeSecondaryUpgrade(newImplementation);
39
+ _upgradeSecondaryToAndCallUUPS(newImplementation, new bytes(0), false);
40
+ }
41
+
42
+ /**
43
+ * @dev Upgrade the secondary implementation of the proxy to `newImplementation`, and subsequently execute the function call
44
+ * encoded in `data`.
45
+ *
46
+ * Calls {_authorizeSecondaryUpgrade}.
47
+ *
48
+ * Emits an {UpgradedSecondary} event.
49
+ */
50
+ function upgradeSecondaryToAndCall(address newImplementation, bytes memory data)
51
+ external
52
+ payable
53
+ onlyProxy
54
+ {
55
+ _authorizeSecondaryUpgrade(newImplementation);
56
+ _upgradeSecondaryToAndCallUUPS(newImplementation, data, true);
57
+ }
58
+ }
@@ -0,0 +1,56 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // OpenZeppelin Contracts (last updated v4.5.0) (proxy/utils/UUPSUpgradeable.sol)
3
+
4
+ pragma solidity ^0.8.0;
5
+
6
+ import "@openzeppelin/contracts/interfaces/draft-IERC1822.sol";
7
+ import {DoubleLogicERC1967Upgrade} from "./AdminFallbackProxy.sol";
8
+
9
+ /**
10
+ * @dev UUPSUpgradeable by OpenZeppelin but not upgradeable. This is expected to be used on the secondary
11
+ * logic slot behind a DoubleLogicERC1967Upgrade proxy
12
+ */
13
+ abstract contract UUPSNotUpgradeable is IERC1822Proxiable, DoubleLogicERC1967Upgrade {
14
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
15
+ address private immutable __self = address(this);
16
+
17
+ /**
18
+ * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
19
+ * a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
20
+ * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
21
+ * function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
22
+ * fail.
23
+ */
24
+ modifier onlyProxy() {
25
+ require(address(this) != __self, "Function must be called through delegatecall");
26
+ require(
27
+ _getSecondaryImplementation() == __self,
28
+ "Function must be called through active proxy"
29
+ );
30
+ _;
31
+ }
32
+
33
+ /**
34
+ * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
35
+ * callable on the implementing contract but not through proxies.
36
+ */
37
+ modifier notDelegated() {
38
+ require(
39
+ address(this) == __self,
40
+ "UUPSNotUpgradeable: must not be called through delegatecall"
41
+ );
42
+ _;
43
+ }
44
+
45
+ /**
46
+ * @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
47
+ * implementation. It is used to validate that the this implementation remains valid after an upgrade.
48
+ *
49
+ * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
50
+ * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
51
+ * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
52
+ */
53
+ function proxiableUUID() external view virtual override notDelegated returns (bytes32) {
54
+ return _IMPLEMENTATION_SECONDARY_SLOT;
55
+ }
56
+ }