-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathsequencer.test.ts
799 lines (647 loc) · 28.1 KB
/
sequencer.test.ts
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
import {
BlockAttestation,
type BlockBuilder,
BlockProposal,
ConsensusPayload,
type EpochProofQuote,
type L1ToL2MessageSource,
L2Block,
type L2BlockSource,
MerkleTreeId,
type MerkleTreeReadOperations,
type MerkleTreeWriteOperations,
type Tx,
TxHash,
type UnencryptedL2Log,
UnencryptedTxL2Logs,
WorldStateRunningState,
type WorldStateSynchronizer,
makeProcessedTx,
mockEpochProofQuote,
mockTxForRollup,
} from '@aztec/circuit-types';
import {
AZTEC_EPOCH_DURATION,
AztecAddress,
type ContractDataSource,
EthAddress,
Fr,
GasFees,
GlobalVariables,
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
} from '@aztec/circuits.js';
import { Buffer32 } from '@aztec/foundation/buffer';
import { times } from '@aztec/foundation/collection';
import { randomBytes } from '@aztec/foundation/crypto';
import { Signature } from '@aztec/foundation/eth-signature';
import { type Writeable } from '@aztec/foundation/types';
import { type P2P, P2PClientState } from '@aztec/p2p';
import { type PublicProcessor, type PublicProcessorFactory } from '@aztec/simulator';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
import { type ValidatorClient } from '@aztec/validator-client';
import { type MockProxy, mock, mockFn } from 'jest-mock-extended';
import { type BlockBuilderFactory } from '../block_builder/index.js';
import { type GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
import { type L1Publisher } from '../publisher/l1-publisher.js';
import { TxValidatorFactory } from '../tx_validator/tx_validator_factory.js';
import { Sequencer } from './sequencer.js';
describe('sequencer', () => {
let publisher: MockProxy<L1Publisher>;
let validatorClient: MockProxy<ValidatorClient>;
let globalVariableBuilder: MockProxy<GlobalVariableBuilder>;
let p2p: MockProxy<P2P>;
let worldState: MockProxy<WorldStateSynchronizer>;
let fork: MockProxy<MerkleTreeWriteOperations>;
let blockBuilder: MockProxy<BlockBuilder>;
let merkleTreeOps: MockProxy<MerkleTreeReadOperations>;
let publicProcessor: MockProxy<PublicProcessor>;
let l2BlockSource: MockProxy<L2BlockSource>;
let l1ToL2MessageSource: MockProxy<L1ToL2MessageSource>;
let publicProcessorFactory: MockProxy<PublicProcessorFactory>;
let lastBlockNumber: number;
let hash: string;
let sequencer: TestSubject;
const chainId = new Fr(12345);
const version = Fr.ZERO;
const coinbase = EthAddress.random();
const feeRecipient = AztecAddress.random();
const gasFees = GasFees.empty();
const archive = Fr.random();
const mockedSig = new Signature(Buffer32.fromField(Fr.random()), Buffer32.fromField(Fr.random()), 27);
const committee = [EthAddress.random()];
const getSignatures = () => [mockedSig];
const getAttestations = () => {
const attestation = new BlockAttestation(new ConsensusPayload(block.header, archive, []), mockedSig);
(attestation as any).sender = committee[0];
return [attestation];
};
const createBlockProposal = () => {
return new BlockProposal(new ConsensusPayload(block.header, archive, [TxHash.random()]), mockedSig);
};
let block: L2Block;
let mockedGlobalVariables: GlobalVariables;
beforeEach(() => {
lastBlockNumber = 0;
hash = Fr.ZERO.toString();
block = L2Block.random(lastBlockNumber + 1);
mockedGlobalVariables = new GlobalVariables(
chainId,
version,
block.header.globalVariables.blockNumber,
block.header.globalVariables.slotNumber,
Fr.ZERO,
coinbase,
feeRecipient,
gasFees,
);
publisher = mock<L1Publisher>();
publisher.getSenderAddress.mockImplementation(() => EthAddress.random());
publisher.getCurrentEpochCommittee.mockResolvedValue(committee);
publisher.canProposeAtNextEthBlock.mockResolvedValue([
block.header.globalVariables.slotNumber.toBigInt(),
block.header.globalVariables.blockNumber.toBigInt(),
]);
publisher.validateBlockForSubmission.mockResolvedValue();
globalVariableBuilder = mock<GlobalVariableBuilder>();
merkleTreeOps = mock<MerkleTreeReadOperations>();
blockBuilder = mock<BlockBuilder>();
p2p = mock<P2P>({
getStatus: mockFn().mockResolvedValue({
state: P2PClientState.IDLE,
syncedToL2Block: { number: lastBlockNumber, hash },
}),
});
fork = mock<MerkleTreeWriteOperations>();
worldState = mock<WorldStateSynchronizer>({
fork: () => Promise.resolve(fork),
getCommitted: () => merkleTreeOps,
status: mockFn().mockResolvedValue({
state: WorldStateRunningState.IDLE,
syncedToL2Block: { number: lastBlockNumber, hash },
}),
});
publicProcessor = mock<PublicProcessor>({
process: async txs => [
await Promise.all(txs.map(tx => makeProcessedTx(tx, tx.data.toKernelCircuitPublicInputs(), []))),
[],
[],
],
});
publicProcessorFactory = mock<PublicProcessorFactory>({
create: (_a, _b) => publicProcessor,
});
l2BlockSource = mock<L2BlockSource>({
getBlockNumber: mockFn().mockResolvedValue(lastBlockNumber),
getL2Tips: mockFn().mockResolvedValue({ latest: { number: lastBlockNumber, hash } }),
});
l1ToL2MessageSource = mock<L1ToL2MessageSource>({
getL1ToL2Messages: () => Promise.resolve(Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(Fr.ZERO)),
getBlockNumber: mockFn().mockResolvedValue(lastBlockNumber),
});
// all txs use the same allowed FPC class
const fpcClassId = Fr.random();
const contractSource = mock<ContractDataSource>({
getContractClass: mockFn().mockResolvedValue(fpcClassId),
});
const blockBuilderFactory = mock<BlockBuilderFactory>({
create: () => blockBuilder,
});
validatorClient = mock<ValidatorClient>({
collectAttestations: mockFn().mockResolvedValue(getAttestations()),
createBlockProposal: mockFn().mockResolvedValue(createBlockProposal()),
});
sequencer = new TestSubject(
publisher,
// TDOO(md): add the relevant methods to the validator client that will prevent it stalling when waiting for attestations
validatorClient,
globalVariableBuilder,
p2p,
worldState,
blockBuilderFactory,
l2BlockSource,
l1ToL2MessageSource,
publicProcessorFactory,
new TxValidatorFactory(merkleTreeOps, contractSource, false),
new NoopTelemetryClient(),
);
});
it('builds a block out of a single tx', async () => {
const tx = mockTxForRollup();
tx.data.constants.txContext.chainId = chainId;
const txHash = tx.getTxHash();
p2p.getTxs.mockReturnValueOnce([tx]);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
2,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
// Ok, we have an issue that we never actually call the process L2 block
expect(publisher.proposeL2Block).toHaveBeenCalledTimes(1);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], undefined);
});
it('builds a block when it is their turn', async () => {
const tx = mockTxForRollup();
tx.data.constants.txContext.chainId = chainId;
const txHash = tx.getTxHash();
p2p.getTxs.mockReturnValue([tx]);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValue(mockedGlobalVariables);
// Not your turn!
publisher.canProposeAtNextEthBlock.mockRejectedValue(new Error());
publisher.validateBlockForSubmission.mockRejectedValue(new Error());
await sequencer.work();
expect(blockBuilder.startNewBlock).not.toHaveBeenCalled();
// Now we can propose, but lets assume that the content is still "bad" (missing sigs etc)
publisher.canProposeAtNextEthBlock.mockResolvedValue([
block.header.globalVariables.slotNumber.toBigInt(),
block.header.globalVariables.blockNumber.toBigInt(),
]);
await sequencer.work();
expect(blockBuilder.startNewBlock).not.toHaveBeenCalled();
// Now it is!
publisher.validateBlockForSubmission.mockClear();
publisher.validateBlockForSubmission.mockResolvedValue();
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
2,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], undefined);
});
it('builds a block out of several txs rejecting double spends', async () => {
const doubleSpendTxIndex = 1;
const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000), mockTxForRollup(0x30000)];
txs.forEach(tx => {
tx.data.constants.txContext.chainId = chainId;
});
const validTxHashes = txs.filter((_, i) => i !== doubleSpendTxIndex).map(tx => tx.getTxHash());
const doubleSpendTx = txs[doubleSpendTxIndex];
p2p.getTxs.mockReturnValueOnce(txs);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
// We make a nullifier from tx1 a part of the nullifier tree, so it gets rejected as double spend
const doubleSpendNullifier = doubleSpendTx.data.forRollup!.end.nullifiers[0].toBuffer();
merkleTreeOps.findLeafIndex.mockImplementation((treeId: MerkleTreeId, value: any) => {
return Promise.resolve(
treeId === MerkleTreeId.NULLIFIER_TREE && value.equals(doubleSpendNullifier) ? 1n : undefined,
);
});
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
2,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), validTxHashes, undefined);
expect(p2p.deleteTxs).toHaveBeenCalledWith([doubleSpendTx.getTxHash()]);
});
it('builds a block out of several txs rejecting incorrect chain ids', async () => {
const invalidChainTxIndex = 1;
const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000), mockTxForRollup(0x30000)];
txs.forEach(tx => {
tx.data.constants.txContext.chainId = chainId;
});
const invalidChainTx = txs[invalidChainTxIndex];
const validTxHashes = txs.filter((_, i) => i !== invalidChainTxIndex).map(tx => tx.getTxHash());
p2p.getTxs.mockReturnValueOnce(txs);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
// We make the chain id on the invalid tx not equal to the configured chain id
invalidChainTx.data.constants.txContext.chainId = new Fr(1n + chainId.value);
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
2,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), validTxHashes, undefined);
expect(p2p.deleteTxs).toHaveBeenCalledWith([invalidChainTx.getTxHash()]);
});
it('builds a block out of several txs dropping the ones that go over max size', async () => {
const invalidTransactionIndex = 1;
const txs = [mockTxForRollup(0x10000), mockTxForRollup(0x20000), mockTxForRollup(0x30000)];
txs.forEach(tx => {
tx.data.constants.txContext.chainId = chainId;
});
const validTxHashes = txs.filter((_, i) => i !== invalidTransactionIndex).map(tx => tx.getTxHash());
p2p.getTxs.mockReturnValueOnce(txs);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
// We make txs[1] too big to fit
(txs[invalidTransactionIndex] as Writeable<Tx>).unencryptedLogs = UnencryptedTxL2Logs.random(2, 4);
(txs[invalidTransactionIndex].unencryptedLogs.functionLogs[0].logs[0] as Writeable<UnencryptedL2Log>).data =
randomBytes(1024 * 1022);
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
2,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), validTxHashes, undefined);
});
it('builds a block once it reaches the minimum number of transactions', async () => {
const txs = times(8, i => {
const tx = mockTxForRollup(i * 0x10000);
tx.data.constants.txContext.chainId = chainId;
return tx;
});
const block = L2Block.random(lastBlockNumber + 1);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
sequencer.updateConfig({ minTxsPerBlock: 4 });
// block is not built with 0 txs
p2p.getTxs.mockReturnValueOnce([]);
//p2p.getTxs.mockReturnValueOnce(txs.slice(0, 4));
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(0);
// block is not built with 3 txs
p2p.getTxs.mockReturnValueOnce(txs.slice(0, 3));
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(0);
// block is built with 4 txs
p2p.getTxs.mockReturnValueOnce(txs.slice(0, 4));
const txHashes = txs.slice(0, 4).map(tx => tx.getTxHash());
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
4,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledTimes(1);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), txHashes, undefined);
});
it('builds a block that contains zero real transactions once flushed', async () => {
const txs = times(8, i => {
const tx = mockTxForRollup(i * 0x10000);
tx.data.constants.txContext.chainId = chainId;
return tx;
});
const block = L2Block.random(lastBlockNumber + 1);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
sequencer.updateConfig({ minTxsPerBlock: 4 });
// block is not built with 0 txs
p2p.getTxs.mockReturnValueOnce([]);
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(0);
// block is not built with 3 txs
p2p.getTxs.mockReturnValueOnce(txs.slice(0, 3));
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(0);
// flush the sequencer and it should build a block
sequencer.flush();
// block is built with 0 txs
p2p.getTxs.mockReturnValueOnce([]);
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(1);
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
2,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledTimes(1);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [], undefined);
});
it('builds a block that contains less than the minimum number of transactions once flushed', async () => {
const txs = times(8, i => {
const tx = mockTxForRollup(i * 0x10000);
tx.data.constants.txContext.chainId = chainId;
return tx;
});
const block = L2Block.random(lastBlockNumber + 1);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
sequencer.updateConfig({ minTxsPerBlock: 4 });
// block is not built with 0 txs
p2p.getTxs.mockReturnValueOnce([]);
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(0);
// block is not built with 3 txs
p2p.getTxs.mockReturnValueOnce(txs.slice(0, 3));
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(0);
// flush the sequencer and it should build a block
sequencer.flush();
// block is built with 3 txs
const postFlushTxs = txs.slice(0, 3);
p2p.getTxs.mockReturnValueOnce(postFlushTxs);
const postFlushTxHashes = postFlushTxs.map(tx => tx.getTxHash());
await sequencer.work();
expect(blockBuilder.startNewBlock).toHaveBeenCalledTimes(1);
expect(blockBuilder.startNewBlock).toHaveBeenCalledWith(
3,
mockedGlobalVariables,
Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(new Fr(0n)),
);
expect(publisher.proposeL2Block).toHaveBeenCalledTimes(1);
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), postFlushTxHashes, undefined);
});
it('aborts building a block if the chain moves underneath it', async () => {
const tx = mockTxForRollup();
tx.data.constants.txContext.chainId = chainId;
p2p.getTxs.mockReturnValueOnce([tx]);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
publisher.proposeL2Block.mockResolvedValueOnce(true);
const mockedGlobalVariables = new GlobalVariables(
chainId,
version,
block.header.globalVariables.blockNumber,
block.header.globalVariables.slotNumber,
Fr.ZERO,
coinbase,
feeRecipient,
gasFees,
);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(mockedGlobalVariables);
// This could practically be for any reason, e.g., could also be that we have entered a new slot.
publisher.validateBlockForSubmission
.mockResolvedValueOnce()
.mockResolvedValueOnce()
.mockRejectedValueOnce(new Error());
await sequencer.work();
expect(publisher.proposeL2Block).not.toHaveBeenCalled();
});
describe('Handling proof quotes', () => {
let txHash: TxHash;
let currentEpoch = 0n;
const setupForBlockNumber = (blockNumber: number) => {
currentEpoch = BigInt(blockNumber) / BigInt(AZTEC_EPOCH_DURATION);
// Create a new block and header
block = L2Block.random(blockNumber);
mockedGlobalVariables = new GlobalVariables(
chainId,
version,
block.header.globalVariables.blockNumber,
block.header.globalVariables.slotNumber,
Fr.ZERO,
coinbase,
feeRecipient,
gasFees,
);
worldState.status.mockResolvedValue({
state: WorldStateRunningState.IDLE,
syncedToL2Block: { number: block.header.globalVariables.blockNumber.toNumber() - 1, hash },
});
p2p.getStatus.mockResolvedValue({
syncedToL2Block: { number: block.header.globalVariables.blockNumber.toNumber() - 1, hash },
state: P2PClientState.IDLE,
});
l2BlockSource.getBlockNumber.mockResolvedValue(block.header.globalVariables.blockNumber.toNumber() - 1);
l1ToL2MessageSource.getBlockNumber.mockResolvedValue(block.header.globalVariables.blockNumber.toNumber() - 1);
globalVariableBuilder.buildGlobalVariables.mockResolvedValue(mockedGlobalVariables);
publisher.canProposeAtNextEthBlock.mockResolvedValue([
block.header.globalVariables.slotNumber.toBigInt(),
block.header.globalVariables.blockNumber.toBigInt(),
]);
publisher.getEpochForSlotNumber.mockImplementation((slotNumber: bigint) =>
Promise.resolve(slotNumber / BigInt(AZTEC_EPOCH_DURATION)),
);
const tx = mockTxForRollup();
tx.data.constants.txContext.chainId = chainId;
txHash = tx.getTxHash();
p2p.getTxs.mockReturnValue([tx]);
blockBuilder.setBlockCompleted.mockResolvedValue(block);
};
it('submits a valid proof quote with a block', async () => {
const blockNumber = AZTEC_EPOCH_DURATION + 1;
setupForBlockNumber(blockNumber);
const proofQuote = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
1,
);
p2p.getEpochProofQuotes.mockResolvedValue([proofQuote]);
publisher.proposeL2Block.mockResolvedValueOnce(true);
publisher.validateProofQuote.mockImplementation((x: EpochProofQuote) => Promise.resolve(x));
// The previous epoch can be claimed
publisher.getClaimableEpoch.mockImplementation(() => Promise.resolve(currentEpoch - 1n));
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], proofQuote);
});
it('does not claim the epoch previous to the first', async () => {
const blockNumber = 1;
setupForBlockNumber(blockNumber);
const proofQuote = mockEpochProofQuote(
0n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
1,
);
p2p.getEpochProofQuotes.mockResolvedValue([proofQuote]);
publisher.proposeL2Block.mockResolvedValueOnce(true);
publisher.validateProofQuote.mockImplementation((x: EpochProofQuote) => Promise.resolve(x));
publisher.getClaimableEpoch.mockImplementation(() => Promise.resolve(undefined));
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], undefined);
});
it('does not submit a quote with an expired slot number', async () => {
const blockNumber = AZTEC_EPOCH_DURATION + 1;
setupForBlockNumber(blockNumber);
const proofQuote = mockEpochProofQuote(
currentEpoch - 1n,
// Slot number expired
block.header.globalVariables.slotNumber.toBigInt() - 1n,
10000n,
EthAddress.random(),
1,
);
p2p.getEpochProofQuotes.mockResolvedValue([proofQuote]);
publisher.proposeL2Block.mockResolvedValueOnce(true);
publisher.validateProofQuote.mockImplementation((x: EpochProofQuote) => Promise.resolve(x));
// The previous epoch can be claimed
publisher.getClaimableEpoch.mockImplementation(() => Promise.resolve(currentEpoch - 1n));
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], undefined);
});
it('does not submit a valid quote if unable to claim epoch', async () => {
const blockNumber = AZTEC_EPOCH_DURATION + 1;
setupForBlockNumber(blockNumber);
const proofQuote = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
1,
);
p2p.getEpochProofQuotes.mockResolvedValue([proofQuote]);
publisher.proposeL2Block.mockResolvedValueOnce(true);
publisher.validateProofQuote.mockImplementation((x: EpochProofQuote) => Promise.resolve(x));
publisher.getClaimableEpoch.mockResolvedValue(undefined);
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], undefined);
});
it('does not submit an invalid quote', async () => {
const blockNumber = AZTEC_EPOCH_DURATION + 1;
setupForBlockNumber(blockNumber);
const proofQuote = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
1,
);
p2p.getEpochProofQuotes.mockResolvedValue([proofQuote]);
publisher.proposeL2Block.mockResolvedValueOnce(true);
// Quote is reported as invalid
publisher.validateProofQuote.mockImplementation(_ => Promise.resolve(undefined));
// The previous epoch can be claimed
publisher.getClaimableEpoch.mockImplementation(() => Promise.resolve(currentEpoch - 1n));
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], undefined);
});
it('only selects valid quotes', async () => {
const blockNumber = AZTEC_EPOCH_DURATION + 1;
setupForBlockNumber(blockNumber);
// Create 1 valid quote and 3 that have a higher fee but are invalid
const validProofQuote = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
1,
);
const proofQuoteInvalidSlot = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() - 1n,
10000n,
EthAddress.random(),
2,
);
const proofQuoteInvalidEpoch = mockEpochProofQuote(
currentEpoch,
block.header.globalVariables.slotNumber.toBigInt() - 1n,
10000n,
EthAddress.random(),
2,
);
// This is deemed invalid by the contract, we identify it by a fee of 2
const proofQuoteInvalid = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
2,
);
const allQuotes = [validProofQuote, proofQuoteInvalidSlot, proofQuoteInvalidEpoch, proofQuoteInvalid];
p2p.getEpochProofQuotes.mockResolvedValue(allQuotes);
publisher.proposeL2Block.mockResolvedValueOnce(true);
// Quote is reported as invalid
publisher.validateProofQuote.mockImplementation(p =>
Promise.resolve(p.payload.basisPointFee === 2 ? undefined : p),
);
// The previous epoch can be claimed
publisher.getClaimableEpoch.mockImplementation(() => Promise.resolve(currentEpoch - 1n));
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], validProofQuote);
});
it('selects the lowest cost valid quote', async () => {
const blockNumber = AZTEC_EPOCH_DURATION + 1;
setupForBlockNumber(blockNumber);
// Create 3 valid quotes with different fees.
// And 3 invalid quotes with lower fees
// We should select the lowest cost valid quote
const validQuotes = times(3, (i: number) =>
mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
10 + i,
),
);
const proofQuoteInvalidSlot = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() - 1n,
10000n,
EthAddress.random(),
1,
);
const proofQuoteInvalidEpoch = mockEpochProofQuote(
currentEpoch,
block.header.globalVariables.slotNumber.toBigInt() - 1n,
10000n,
EthAddress.random(),
2,
);
// This is deemed invalid by the contract, we identify it by it's fee
const proofQuoteInvalid = mockEpochProofQuote(
currentEpoch - 1n,
block.header.globalVariables.slotNumber.toBigInt() + 1n,
10000n,
EthAddress.random(),
3,
);
const allQuotes = [proofQuoteInvalidSlot, proofQuoteInvalidEpoch, ...validQuotes, proofQuoteInvalid];
p2p.getEpochProofQuotes.mockResolvedValue(allQuotes);
publisher.proposeL2Block.mockResolvedValueOnce(true);
// Quote is reported as invalid
publisher.validateProofQuote.mockImplementation(p =>
Promise.resolve(p.payload.basisPointFee === 3 ? undefined : p),
);
// The previous epoch can be claimed
publisher.getClaimableEpoch.mockImplementation(() => Promise.resolve(currentEpoch - 1n));
await sequencer.work();
expect(publisher.proposeL2Block).toHaveBeenCalledWith(block, getSignatures(), [txHash], validQuotes[0]);
});
});
});
class TestSubject extends Sequencer {
public override work() {
return super.work();
}
}