This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinbox.mini
621 lines (541 loc) · 22.3 KB
/
inbox.mini
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//
// Copyright 2020, Offchain Labs, Inc. All rights reserved.
//
use std::queue::Queue;
use std::queue::queue_new;
use std::queue::queue_isEmpty;
use std::queue::queue_get;
use std::queue::queue_getOrDie;
use std::queue::queue_put;
use std::queue::QueueStack;
use std::queue::queuestack_new;
use std::queue::queuestack_push;
use std::queue::queuestack_get;
use accounts::getGlobalAccountStore;
use accounts::setGlobalAccountStore;
use accounts::accountStore_get;
use accounts::accountStore_set;
use accounts::account_addToEthBalance;
use blockhash::blockhash_submitHash;
use chainParameters::chainParams_gotParamsMessage;
use chainParameters::chainParams_chainId;
use chainParameters::haveReceivedChainInitMessage;
use gasAccounting::updateL1GasPriceEstimate;
use gasAccounting::txFeeStats_zero;
use messages::TxRequestData;
use messageBatch::MessageBatch;
use messageBatch::messageBatch_tryNew;
use messageBatch::messageBatch_get;
use output::outputStats_endOfBlock;
use pluggables::callPluggable;
use retrybuffer::RetryableTx;
use retrybuffer::processRetryableTx;
use std::bytearray::ByteArray;
use std::bytearray::bytearray_new;
use std::bytearray::bytearray_size;
use std::bytearray::bytearray_fromSizeAndBuffer;
use std::bytearray::bytearray_getByte;
use std::bytearray::bytearray_get256;
use std::bytearray::bytearray_getCalldataUnits;
use std::bytestream::ByteStream;
use std::bytestream::bytestream_new;
use std::bytestream::bytestream_getByte;
use std::bytestream::bytestream_get256;
use std::bytestream::bytestream_getRemainingBytes;
use std::bytestream::bytestream_skipBytes;
use std::keccak::keccak256;
use decompression::decompressAndVerifyEcdsaSignedTx;
use decompression::decompressAndVerifyBLSSignedTxBatch;
type Inbox = struct {
queue: IncomingRequestQueue, // queue of requests that haven't yet been retrieved from the inbox
arbBlockNum: uint, // highest Arbitrum blocknum of messages that we have retrieved from the inbox
ethBlockNum: uint, // highest L1 blocknum of messages that we have retrieved from the inbox
timestamp: uint, // highest timestamp of messages that we have retrieved from the inbox
atStartOfBlock: bool, // true iff we should start a new block when the next message arrives
inboxHashAccumulator: bytes32, // hash accumulator covering all inbox messages so far
pendingMessage: option<IncomingRequest>,
}
var globalInbox: Inbox;
var globalInputQueue: struct {
txQ: TxRequestQueue,
batchQ: IncomingRequestQueueStack,
};
public impure func inbox_init() {
globalInbox = struct {
queue: incomingRequestQueue_new(),
arbBlockNum: 0,
ethBlockNum: 0,
timestamp: 0,
atStartOfBlock: true,
inboxHashAccumulator: bytes32(0),
pendingMessage: None<IncomingRequest>,
};
globalInputQueue = struct {
txQ: txRequestQueue_new(),
batchQ: incomingRequestQueueStack_new()
};
}
public impure func emitGenesisBlockReceipt() {
outputStats_endOfBlock(globalInbox.arbBlockNum, globalInbox.ethBlockNum, globalInbox.timestamp);
blockhash_submitHash(globalInbox.ethBlockNum, globalInbox.inboxHashAccumulator);
globalInbox = globalInbox with { arbBlockNum: globalInbox.arbBlockNum + 1 };
}
public impure func inbox_get() -> IncomingRequest {
// Get the next message, in order of arrival.
// If no messages have arrived, this will block until one arrives.
// This is a loop because some events don't generate a message that we can return,
// so we keep trying until we find something we can return.
return xloop {
while (incomingRequestQueue_isEmpty(globalInbox.queue)) {
globalInbox = getFromL1Inbox(globalInbox);
}
let (updatedQ, rawMsg,) = incomingRequestQueue_getOrDie(globalInbox.queue);
globalInbox = globalInbox with { queue: updatedQ };
let ebMsg = unsafecast<IncomingRequest>(rawMsg);
let ebMsgHash = hashIncomingRequest(ebMsg);
globalInbox = globalInbox with {
inboxHashAccumulator: hash(globalInbox.inboxHashAccumulator, ebMsgHash)
};
if (ebMsg.kind == const::L1MessageType_chainInit) {
chainParams_gotParamsMessage(ebMsg.sender, ebMsg.msgData);
} else {
return ebMsg with {
requestId: uint(hash(bytes32(chainParams_chainId()), bytes32(ebMsg.requestId)))
};
}
};
}
impure func incomingRequestFromRaw(newMsgRaw: IncomingRequestFromInbox, fromSideload: bool) -> IncomingRequest {
if (! fromSideload) {
updateL1GasPriceEstimate(newMsgRaw.sender, newMsgRaw.gasPriceL1);
}
if (newMsgRaw.ethBlockNumber < globalInbox.ethBlockNum) {
newMsgRaw = newMsgRaw with { ethBlockNumber: globalInbox.ethBlockNum };
}
if (newMsgRaw.timestamp < globalInbox.timestamp) {
newMsgRaw = newMsgRaw with { timestamp: globalInbox.timestamp };
}
return struct {
kind: newMsgRaw.kind,
arbBlockNumber: globalInbox.arbBlockNum,
ethBlockNumber: newMsgRaw.ethBlockNumber,
timestamp: newMsgRaw.timestamp,
sender: newMsgRaw.sender,
requestId: newMsgRaw.requestId,
msgData: bytearray_fromSizeAndBuffer(newMsgRaw.msgSize, newMsgRaw.msgData),
provenance: struct {
l1SeqNum: newMsgRaw.requestId,
parentRequestId: 0,
indexInParent: ~0,
},
aggregator: None<AggregatorInfo>,
adminMode: fromSideload && (newMsgRaw.kind != const::L1MessageType_L2ForGasEstimation),
gasEstimationParams: None<GasEstimationParams>,
};
}
#[noinline, ] impure func getFromL1Inbox(inbox: Inbox) -> Inbox {
// Get a message from the L1 inbox, and append it to the inbox.
// Automatically generate an end-of-block message if that is needed.
// If there are no messages available in the L1 inbox, this blocks until one is available
if let Some(pendingMsg) = inbox.pendingMessage {
let rawSideloadMsg = asm(inbox.arbBlockNum,) any { sideload };
if (rawSideloadMsg != ()) {
let newMsg = incomingRequestFromRaw(unsafecast<IncomingRequestFromInbox>(rawSideloadMsg), true);
return inbox with {
queue: incomingRequestQueue_put(inbox.queue, newMsg)
};
}
// do end of block processing
outputStats_endOfBlock(inbox.arbBlockNum, inbox.ethBlockNum, inbox.timestamp);
blockhash_submitHash(inbox.ethBlockNum, inbox.inboxHashAccumulator);
inbox = inbox with {
pendingMessage: None<IncomingRequest>
} with {
arbBlockNum: inbox.arbBlockNum + 1
} with {
ethBlockNum: pendingMsg.ethBlockNumber
} with {
timestamp: pendingMsg.timestamp
};
return xif (pendingMsg.kind != const::L1MessageType_endOfBlock) {
inbox with {
queue: incomingRequestQueue_put(
inbox.queue,
pendingMsg with { arbBlockNumber: pendingMsg.arbBlockNumber + 1 },
)
}
} else {
inbox with {
atStartOfBlock: true
}
};
}
let newMsg = incomingRequestFromRaw(asm() IncomingRequestFromInbox { inbox }, false);
if ( (!inbox.atStartOfBlock) && ((newMsg.ethBlockNumber > inbox.ethBlockNum) || (newMsg.timestamp > inbox.timestamp)) ){
return inbox with {
pendingMessage: Some(newMsg)
};
} elseif (newMsg.kind == const::L1MessageType_endOfBlock) {
return xif (inbox.atStartOfBlock) {
inbox
} else {
inbox with {
pendingMessage: Some(newMsg)
}
};
}
return inbox with {
queue: incomingRequestQueue_put(inbox.queue, newMsg)
} with {
ethBlockNum: newMsg.ethBlockNumber
} with {
arbBlockNum: newMsg.arbBlockNumber
} with {
timestamp: newMsg.timestamp
} with {
atStartOfBlock: false
};
}
func endOfArbBlockMessage(arbBlockNum: uint, ethBlockNum: uint, timestamp: uint) -> IncomingRequest {
return struct {
kind: const::L1MessageType_endOfBlock,
arbBlockNumber: arbBlockNum,
ethBlockNumber: ethBlockNum,
timestamp: timestamp,
sender: address(0),
requestId: 0,
msgData: bytearray_new(0),
provenance: struct {
l1SeqNum: 0,
parentRequestId: 0,
indexInParent: ~0,
},
aggregator: None<AggregatorInfo>,
adminMode: false,
gasEstimationParams: None<GasEstimationParams>,
};
}
public impure func inbox_currentTimestamp() -> uint {
return globalInbox.timestamp;
}
public impure func inbox_currentArbBlockNumber() -> uint {
return globalInbox.arbBlockNum;
}
public impure func inbox_currentEthBlockNumber() -> uint {
return globalInbox.ethBlockNum;
}
type IncomingRequestFromInbox = struct {
kind: uint, // type of message
ethBlockNumber: uint, // block number of the L1 block
timestamp: uint, // timestamp of the L1 block
sender: address, // address of the sender
requestId: uint,
gasPriceL1: uint, // L1 gas price paid by this tx
msgSize: uint,
msgData: buffer,
}
type IncomingRequest = struct {
kind: uint, // type of message
arbBlockNumber: uint, // Arbitrum block number
ethBlockNumber: uint, // block number of the L1 block
timestamp: uint, // timestamp of the L1 block
sender: address, // address of the sender
requestId: uint,
msgData: ByteArray, // kind-specific data
provenance: RequestProvenance,
aggregator: option<AggregatorInfo>,
adminMode: bool,
gasEstimationParams: option<GasEstimationParams>,
}
type GasEstimationParams = struct {
computeGasLimit: uint,
ignoreGasPrice: bool,
ignoreMaxGas: bool,
}
type RequestProvenance = struct {
l1SeqNum: uint,
parentRequestId: uint,
indexInParent: uint,
}
type AggregatorInfo = struct {
aggregator: option<address>,
calldataUnits: uint,
}
func hashIncomingRequest(req: IncomingRequest) -> bytes32 {
return hash(
bytes32(req.kind),
hash(
bytes32(req.arbBlockNumber),
hash(
bytes32(req.ethBlockNumber),
hash(
bytes32(req.timestamp),
hash(
bytes32(req.sender),
hash(
bytes32(req.requestId),
keccak256(req.msgData, 0, bytearray_size(req.msgData)),
)
)
)
)
)
);
}
public impure func inbox_getNextUnpackedRequest() -> TxRequestData {
let msg = unsafecast<IncomingRequest>(()); // value will be re-initialized before use
return xloop {
if let Some(res) = txRequestQueue_get(globalInputQueue.txQ) {
let (newQ, tx) = res;
globalInputQueue = globalInputQueue with {
txQ: newQ
};
return unsafecast<TxRequestData>(tx);
}
if let Some(res) = incomingRequestQueueStack_get(globalInputQueue.batchQ) {
let (newQ, umsg) = res;
globalInputQueue = globalInputQueue with {
batchQ: newQ
};
msg = umsg;
} else {
msg = inbox_get();
}
if let Some(batch) = messageBatch_tryNew(msg) {
// it's a batch message, so unpack its components and queue them
let moreToDo = true;
let queue = incomingRequestQueue_new();
while (moreToDo) {
if let Some(res) = messageBatch_get(batch) {
let (newMsg, restOfBatch) = res;
queue = incomingRequestQueue_put(queue, newMsg);
batch = restOfBatch;
} else {
moreToDo = false;
}
}
globalInputQueue = globalInputQueue with {
batchQ: incomingRequestQueueStack_push(globalInputQueue.batchQ, queue)
};
} else {
// it's not a batch, so process it immediately
if (msg.kind == const::L1MessageType_L2FundedByL1) {
// record the calldata usage, so it can be reported in tx receipt
msg = msg with {
aggregator: Some(struct {
aggregator: None<address>,
calldataUnits: bytearray_getCalldataUnits(msg.msgData),
})
};
// the message carries an ETH deposit
// verify the validity of the message, process the deposit, then process the tx
let firstByte = bytearray_getByte(msg.msgData, 0);
if (firstByte == const::L2MessageType_unsignedEOATx) {
let callvalue = bytearray_get256(msg.msgData, 1+4*32);
let gAcctStore = getGlobalAccountStore();
setGlobalAccountStore(
accountStore_set(
gAcctStore,
msg.sender,
account_addToEthBalance(accountStore_get(gAcctStore, msg.sender), callvalue)
)
);
if let Some(txRequest) = translateUnsignedTx(msg) {
return txRequest;
}
// else request was malformatted, ignore it and execute loop again to get another
// but don't undo the deposit
} elseif (firstByte == const::L2MessageType_unsignedContractTx) {
let callvalue = bytearray_get256(msg.msgData, 1+3*32);
let gAcctStore = getGlobalAccountStore();
setGlobalAccountStore(
accountStore_set(
gAcctStore,
msg.sender,
account_addToEthBalance(accountStore_get(gAcctStore, msg.sender), callvalue)
)
);
if let Some(txRequest) = translateUnsignedTx(msg) {
return txRequest;
}
// else request was malformatted, ignore it and execute loop again to get another
// but don't undo the deposit
}
// else L2 msg type is invalid in ETH deposit message, ignore msg and execute loop again to get another
// note that deposit doesn't happen in this case
} elseif (msg.kind == const::L1MessageType_endOfBlock) {
// it's an end-of-block message, so just trigger end-of-block processing
outputStats_endOfBlock(msg.arbBlockNumber, msg.ethBlockNumber, msg.timestamp);
blockhash_submitHash(msg.ethBlockNumber, globalInbox.inboxHashAccumulator);
globalInbox = globalInbox with {
arbBlockNum: globalInbox.arbBlockNum + 1
};
} elseif (msg.kind == const::L1MessageType_L2 || msg.kind == const::L1MessageType_L2ForGasEstimation) {
// record the calldata usage, so it can be reported in tx receipt
msg = msg with {
aggregator: Some(struct {
aggregator: xif let Some(aggInfo) = msg.aggregator { aggInfo.aggregator } else { None<address> },
calldataUnits: bytearray_getCalldataUnits(msg.msgData),
})
};
// it's an L2 message, so switch based on the L2 type
let firstByte = bytearray_getByte(msg.msgData, 0);
if (firstByte == const::L2MessageType_heartbeat) {
// it's a heartbeat message, don't do anything
} elseif (firstByte == const::L2MessageType_signedCompressedTx) {
// it's a single message with compressed headers
if let Some(txRequest) = decompressAndVerifyEcdsaSignedTx(msg.msgData, msg) {
return txRequest;
}
// else signature was invalid, ignore msg and execute loop again to get another
} elseif (firstByte == const::L2MessageType_blsBatch) {
if let Some(updatedQ) = decompressAndVerifyBLSSignedTxBatch(
msg.msgData,
msg,
unsafecast<Queue>(globalInputQueue.txQ)
) {
globalInputQueue = globalInputQueue with { txQ: unsafecast<TxRequestQueue>(updatedQ) };
}
// else BLS batch was bad, so ignore it
} else {
// subtype must be unsigned EOA, unsigned contract, or nonmutating
if let Some(txRequest) = translateUnsignedTx(msg) {
return txRequest;
}
// else request was malformatted, ignore it and execute loop again to get another
}
} elseif (msg.kind == const::L1MessageType_rollupProtocolEvent) {
if (msg.sender == address(0)) {
let _ = callPluggable(const::PluggableModuleID_rollupTracker, msg.msgData);
}
// else request was forged or malformatted, ignore it and execute loop again to get another
} elseif (msg.kind == const::L1MessageType_submitRetryableTx) {
if let Some(txRequest) = processRetryableTx(msg) {
return txRequest;
}
// submission failed, or no immediate tx included; either way, loop back to get next message
} else {
// invalid message type -- ignore it
}
}
};
}
impure func translateUnsignedTx(request: IncomingRequest) -> option<TxRequestData> {
let inStream = bytestream_new(request.msgData);
let (us, subtype) = bytestream_getByte(inStream)?;
inStream = us;
let (us, maxGas) = bytestream_get256(inStream)?;
inStream = us;
let (us, gasPrice) = bytestream_get256(inStream)?;
inStream = us;
let seqNum = None<uint>;
if (subtype == const::L2MessageType_unsignedEOATx) {
let (us, sn) = bytestream_get256(inStream)?;
inStream = us;
seqNum = Some(sn);
}
let (us, destAddrAsUint) = bytestream_get256(inStream)?;
inStream = us;
let (us, value) = bytestream_get256(inStream)?;
inStream = us;
let calldata = bytestream_getRemainingBytes(inStream);
if (subtype == const::L2MessageType_unsignedEOATx) {
request = request with {
requestId: uint(hash(
bytes32(request.sender),
hash(bytes32(chainParams_chainId()), keccak256(request.msgData, 0, bytearray_size(request.msgData))),
))
};
}
return Some(
struct {
maxGas: maxGas,
gasPrice: gasPrice,
seqNum: seqNum,
caller: request.sender,
calleeAddr: address(destAddrAsUint),
gasPayer: request.sender,
value: value,
calldata: calldata,
nonMutating: (subtype == const::L2MessageType_nonmutatingCall),
isConstructor: (destAddrAsUint == 0),
incomingRequest: request,
feeStats: txFeeStats_zero(),
fromRetryable: None<RetryableTx>,
}
);
}
// below are specialized queue and queuestack types, to facilitate typechecking in the code above
// we can eliminate these if/when the Mini language supports generics
type IncomingRequestQueue = struct {
q: Queue,
}
func incomingRequestQueue_new() -> IncomingRequestQueue {
return struct { q: queue_new() };
}
func incomingRequestQueue_isEmpty(q: IncomingRequestQueue) -> bool {
return queue_isEmpty(q.q);
}
func incomingRequestQueue_get(q: IncomingRequestQueue) -> option<(IncomingRequestQueue, IncomingRequest)> {
let (uq, item) = queue_get(q.q)?;
return Some((struct{ q: uq }, unsafecast<IncomingRequest>(item)));
}
func incomingRequestQueue_getOrDie(q: IncomingRequestQueue) -> (IncomingRequestQueue, IncomingRequest) {
return xif let Some(res) = incomingRequestQueue_get(q) {
res
} else {
panic
};
}
func incomingRequestQueue_put(q: IncomingRequestQueue, req: IncomingRequest) -> IncomingRequestQueue {
return struct { q: queue_put(q.q, req) };
}
type IncomingRequestQueueStack = struct {
qs: QueueStack,
}
func incomingRequestQueueStack_new() -> IncomingRequestQueueStack {
return struct { qs: queuestack_new() };
}
func incomingRequestQueueStack_get(qs: IncomingRequestQueueStack) -> option<(IncomingRequestQueueStack, IncomingRequest)> {
let (uqs, rawMsg) = queuestack_get(qs.qs)?;
return Some((struct { qs: uqs }, unsafecast<IncomingRequest>(rawMsg)));
}
func incomingRequestQueueStack_push(qs: IncomingRequestQueueStack, q: IncomingRequestQueue) -> IncomingRequestQueueStack {
return struct { qs: queuestack_push(qs.qs, q.q) };
}
type TxRequestQueue = struct {
q: Queue,
}
func txRequestQueue_new() -> TxRequestQueue {
return struct { q: queue_new() };
}
func txRequestQueue_isEmpty(q: TxRequestQueue) -> bool {
return queue_isEmpty(q.q);
}
func txRequestQueue_get(q: TxRequestQueue) -> option<(TxRequestQueue, TxRequestData)> {
let (uq, item) = queue_get(q.q)?;
return Some((struct{ q: uq }, unsafecast<TxRequestData>(item)));
}
func txRequestQueue_getOrDie(q: TxRequestQueue) -> (TxRequestQueue, TxRequestData) {
return xif let Some(res) = txRequestQueue_get(q) {
res
} else {
panic
};
}
func txRequestQueue_put(q: TxRequestQueue, req: TxRequestData) -> TxRequestQueue {
return struct { q: queue_put(q.q, req) };
}
type TxRequestQueueStack = struct {
qs: QueueStack,
}
func txRequestQueueStack_new() -> TxRequestQueueStack {
return struct { qs: queuestack_new() };
}
func txRequestQueueStack_get(qs: TxRequestQueueStack) -> option<(TxRequestQueueStack, TxRequestData)> {
let (uqs, rawMsg) = queuestack_get(qs.qs)?;
return Some((struct { qs: uqs }, unsafecast<TxRequestData>(rawMsg)));
}
func txRequestQueueStack_push(qs: TxRequestQueueStack, q: TxRequestQueue) -> TxRequestQueueStack {
return struct { qs: queuestack_push(qs.qs, q.q) };
}