@aztec/sequencer-client 3.0.0-nightly.20251224 → 3.0.0-nightly.20251225
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/sequencer/checkpoint_proposal_job.d.ts +7 -6
- package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
- package/dest/sequencer/checkpoint_proposal_job.js +14 -12
- package/dest/sequencer/metrics.d.ts +3 -1
- package/dest/sequencer/metrics.d.ts.map +1 -1
- package/dest/sequencer/metrics.js +9 -0
- package/dest/sequencer/sequencer.d.ts +5 -5
- package/dest/sequencer/sequencer.d.ts.map +1 -1
- package/dest/sequencer/sequencer.js +2 -2
- package/dest/sequencer/timetable.d.ts +36 -16
- package/dest/sequencer/timetable.d.ts.map +1 -1
- package/dest/sequencer/timetable.js +111 -56
- package/dest/sequencer/utils.d.ts +3 -3
- package/dest/sequencer/utils.js +1 -1
- package/dest/test/mock_checkpoint_builder.d.ts +83 -0
- package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
- package/dest/test/mock_checkpoint_builder.js +179 -0
- package/dest/test/utils.d.ts +49 -0
- package/dest/test/utils.d.ts.map +1 -0
- package/dest/test/utils.js +94 -0
- package/package.json +27 -27
- package/src/sequencer/README.md +531 -0
- package/src/sequencer/checkpoint_proposal_job.ts +21 -19
- package/src/sequencer/metrics.ts +11 -0
- package/src/sequencer/sequencer.ts +5 -5
- package/src/sequencer/timetable.ts +135 -76
- package/src/sequencer/utils.ts +3 -3
- package/src/test/mock_checkpoint_builder.ts +247 -0
- package/src/test/utils.ts +137 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { L2Block } from '@aztec/aztec.js/block';
|
|
2
1
|
import { getKzg } from '@aztec/blob-lib';
|
|
3
2
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
4
3
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
@@ -13,7 +12,7 @@ import type { DateProvider } from '@aztec/foundation/timer';
|
|
|
13
12
|
import type { TypedEventEmitter } from '@aztec/foundation/types';
|
|
14
13
|
import type { P2P } from '@aztec/p2p';
|
|
15
14
|
import type { SlasherClientInterface } from '@aztec/slasher';
|
|
16
|
-
import type { L2BlockSource, ValidateBlockResult } from '@aztec/stdlib/block';
|
|
15
|
+
import type { L2BlockNew, L2BlockSource, ValidateBlockResult } from '@aztec/stdlib/block';
|
|
17
16
|
import type { Checkpoint } from '@aztec/stdlib/checkpoint';
|
|
18
17
|
import { getSlotAtTimestamp, getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
|
|
19
18
|
import {
|
|
@@ -225,6 +224,8 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
225
224
|
this.logStrategyComparison(epoch, checkpointProposalJob.getPublisher());
|
|
226
225
|
this.lastEpochForStrategyComparison = epoch;
|
|
227
226
|
}
|
|
227
|
+
|
|
228
|
+
return checkpoint;
|
|
228
229
|
}
|
|
229
230
|
|
|
230
231
|
/**
|
|
@@ -496,8 +497,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
496
497
|
return { blockNumber: BlockNumber(INITIAL_L2_BLOCK_NUM - 1), archive, l1Timestamp, pendingChainValidationStatus };
|
|
497
498
|
}
|
|
498
499
|
|
|
499
|
-
|
|
500
|
-
const block = await this.l2BlockSource.getBlock(blockNumber);
|
|
500
|
+
const block = await this.l2BlockSource.getL2BlockNew(blockNumber);
|
|
501
501
|
if (!block) {
|
|
502
502
|
// this shouldn't really happen because a moment ago we checked that all components were in sync
|
|
503
503
|
this.log.error(`Failed to get L2 block ${blockNumber} from the archiver with all components in sync`);
|
|
@@ -788,7 +788,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
788
788
|
}
|
|
789
789
|
|
|
790
790
|
type SequencerSyncCheckResult = {
|
|
791
|
-
block?:
|
|
791
|
+
block?: L2BlockNew;
|
|
792
792
|
blockNumber: BlockNumber;
|
|
793
793
|
archive: Fr;
|
|
794
794
|
l1Timestamp: bigint;
|
|
@@ -5,9 +5,9 @@ import { SequencerTooSlowError } from './errors.js';
|
|
|
5
5
|
import type { SequencerMetrics } from './metrics.js';
|
|
6
6
|
import { SequencerState } from './utils.js';
|
|
7
7
|
|
|
8
|
-
export const MIN_EXECUTION_TIME =
|
|
8
|
+
export const MIN_EXECUTION_TIME = 2;
|
|
9
9
|
export const CHECKPOINT_INITIALIZATION_TIME = 1;
|
|
10
|
-
export const
|
|
10
|
+
export const CHECKPOINT_ASSEMBLE_TIME = 1;
|
|
11
11
|
|
|
12
12
|
export class SequencerTimetable {
|
|
13
13
|
/**
|
|
@@ -17,6 +17,21 @@ export class SequencerTimetable {
|
|
|
17
17
|
*/
|
|
18
18
|
public readonly initializeDeadline: number;
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Fixed time offset (in seconds) when the first sub-slot starts, used as baseline for all block deadlines.
|
|
22
|
+
* This is an estimate of how long initialization (sync + proposer check) typically takes.
|
|
23
|
+
* If actual initialization takes longer/shorter, blocks just get less/more time accordingly.
|
|
24
|
+
*/
|
|
25
|
+
public readonly initializationOffset: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Total time needed to finalize and publish a checkpoint, including:
|
|
29
|
+
* - Assembling the checkpoint
|
|
30
|
+
* - Round-trip p2p propagation for the checkpoint proposal and its corresponding attestations
|
|
31
|
+
* - Publishing to L1
|
|
32
|
+
*/
|
|
33
|
+
public readonly checkpointFinalizationTime: number;
|
|
34
|
+
|
|
20
35
|
/**
|
|
21
36
|
* How long it takes to get a published block into L1. L1 builders typically accept txs up to 4 seconds into their slot,
|
|
22
37
|
* but we'll timeout sooner to give it more time to propagate (remember we also have blobs!). Still, when working in anvil,
|
|
@@ -36,8 +51,8 @@ export class SequencerTimetable {
|
|
|
36
51
|
/** How long it takes to for proposals and attestations to travel across the p2p layer (one-way) */
|
|
37
52
|
public readonly p2pPropagationTime: number;
|
|
38
53
|
|
|
39
|
-
/** How much time we spend
|
|
40
|
-
public readonly
|
|
54
|
+
/** How much time we spend assembling a checkpoint after building the last block */
|
|
55
|
+
public readonly checkpointAssembleTime: number = CHECKPOINT_ASSEMBLE_TIME;
|
|
41
56
|
|
|
42
57
|
/** Ethereum slot duration in seconds */
|
|
43
58
|
public readonly ethereumSlotDuration: number;
|
|
@@ -51,6 +66,9 @@ export class SequencerTimetable {
|
|
|
51
66
|
/** Duration per block when building multiple blocks per slot (undefined = single block per slot) */
|
|
52
67
|
public readonly blockDuration: number | undefined;
|
|
53
68
|
|
|
69
|
+
/** Maximum number of blocks that can be built in this slot configuration */
|
|
70
|
+
public readonly maxNumberOfBlocks: number;
|
|
71
|
+
|
|
54
72
|
constructor(
|
|
55
73
|
opts: {
|
|
56
74
|
ethereumSlotDuration: number;
|
|
@@ -68,39 +86,68 @@ export class SequencerTimetable {
|
|
|
68
86
|
this.l1PublishingTime = opts.l1PublishingTime;
|
|
69
87
|
this.p2pPropagationTime = opts.p2pPropagationTime ?? DEFAULT_P2P_PROPAGATION_TIME;
|
|
70
88
|
this.blockDuration = opts.blockDurationMs ? opts.blockDurationMs / 1000 : undefined;
|
|
71
|
-
this.minExecutionTime = MIN_EXECUTION_TIME;
|
|
72
89
|
this.enforce = opts.enforce;
|
|
73
90
|
|
|
74
91
|
// Assume zero-cost propagation time and faster runs in test environments where L1 slot duration is shortened
|
|
75
92
|
if (this.ethereumSlotDuration < 8) {
|
|
76
93
|
this.p2pPropagationTime = 0;
|
|
77
|
-
this.
|
|
94
|
+
this.checkpointAssembleTime = 0.5;
|
|
78
95
|
this.checkpointInitializationTime = 0.5;
|
|
96
|
+
this.minExecutionTime = 1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Min execution time cannot be less than the block duration if set
|
|
100
|
+
if (this.blockDuration !== undefined && this.minExecutionTime > this.blockDuration) {
|
|
101
|
+
this.minExecutionTime = this.blockDuration;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Calculate initialization offset - estimate of time needed for sync + proposer check
|
|
105
|
+
// This is the baseline for all sub-slot deadlines
|
|
106
|
+
this.initializationOffset = this.checkpointInitializationTime;
|
|
107
|
+
|
|
108
|
+
// Calculate total checkpoint finalization time (assembly + attestations + L1 publishing)
|
|
109
|
+
this.checkpointFinalizationTime =
|
|
110
|
+
this.checkpointAssembleTime +
|
|
111
|
+
this.p2pPropagationTime * 2 + // Round-trip propagation
|
|
112
|
+
this.l1PublishingTime; // L1 publishing
|
|
113
|
+
|
|
114
|
+
// Calculate maximum number of blocks that fit in this slot
|
|
115
|
+
if (!this.blockDuration) {
|
|
116
|
+
this.maxNumberOfBlocks = 1; // Single block per slot
|
|
117
|
+
} else {
|
|
118
|
+
const timeReservedAtEnd =
|
|
119
|
+
this.blockDuration + // Last sub-slot for validator re-execution
|
|
120
|
+
this.checkpointFinalizationTime; // Checkpoint finalization
|
|
121
|
+
const timeAvailableForBlocks = this.aztecSlotDuration - this.initializationOffset - timeReservedAtEnd;
|
|
122
|
+
this.maxNumberOfBlocks = Math.floor(timeAvailableForBlocks / this.blockDuration);
|
|
79
123
|
}
|
|
80
124
|
|
|
81
125
|
// Minimum work to do within a slot for building a block with the minimum time for execution and publishing its checkpoint
|
|
82
126
|
const minWorkToDo =
|
|
83
|
-
this.
|
|
127
|
+
this.initializationOffset +
|
|
84
128
|
this.minExecutionTime * 2 + // Execution and reexecution
|
|
85
|
-
this.checkpointFinalizationTime
|
|
86
|
-
this.p2pPropagationTime * 2 + // Send proposal and receive attestations
|
|
87
|
-
this.l1PublishingTime; // Submit to L1
|
|
129
|
+
this.checkpointFinalizationTime;
|
|
88
130
|
|
|
89
131
|
const initializeDeadline = this.aztecSlotDuration - minWorkToDo;
|
|
90
132
|
this.initializeDeadline = initializeDeadline;
|
|
91
133
|
|
|
92
|
-
this.log.verbose(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
134
|
+
this.log.verbose(
|
|
135
|
+
`Sequencer timetable initialized with ${this.maxNumberOfBlocks} blocks per slot (${this.enforce ? 'enforced' : 'not enforced'})`,
|
|
136
|
+
{
|
|
137
|
+
ethereumSlotDuration: this.ethereumSlotDuration,
|
|
138
|
+
aztecSlotDuration: this.aztecSlotDuration,
|
|
139
|
+
l1PublishingTime: this.l1PublishingTime,
|
|
140
|
+
minExecutionTime: this.minExecutionTime,
|
|
141
|
+
blockPrepareTime: this.checkpointInitializationTime,
|
|
142
|
+
p2pPropagationTime: this.p2pPropagationTime,
|
|
143
|
+
blockAssembleTime: this.checkpointAssembleTime,
|
|
144
|
+
initializeDeadline: this.initializeDeadline,
|
|
145
|
+
enforce: this.enforce,
|
|
146
|
+
minWorkToDo,
|
|
147
|
+
blockDuration: this.blockDuration,
|
|
148
|
+
maxNumberOfBlocks: this.maxNumberOfBlocks,
|
|
149
|
+
},
|
|
150
|
+
);
|
|
104
151
|
|
|
105
152
|
if (initializeDeadline <= 0) {
|
|
106
153
|
throw new Error(
|
|
@@ -109,36 +156,6 @@ export class SequencerTimetable {
|
|
|
109
156
|
}
|
|
110
157
|
}
|
|
111
158
|
|
|
112
|
-
/** Deadline for a block proposal execution. Ensures we have enough time left for reexecution and publishing. */
|
|
113
|
-
public getProposerExecTimeEnd(secondsIntoSlot: number): number {
|
|
114
|
-
// We are N seconds into the slot. We need to account for `afterBlockBuildingTimeNeededWithoutReexec` seconds,
|
|
115
|
-
// send then split the remaining time between the re-execution and the block building.
|
|
116
|
-
const afterBlockBuildingTimeNeededWithoutReexec =
|
|
117
|
-
this.checkpointFinalizationTime + this.p2pPropagationTime * 2 + this.l1PublishingTime;
|
|
118
|
-
const maxAllowed = this.aztecSlotDuration - afterBlockBuildingTimeNeededWithoutReexec;
|
|
119
|
-
const available = maxAllowed - secondsIntoSlot;
|
|
120
|
-
const executionTimeEnd = secondsIntoSlot + available / 2;
|
|
121
|
-
this.log.debug(`Block proposal execution time deadline is ${executionTimeEnd}`, {
|
|
122
|
-
secondsIntoSlot,
|
|
123
|
-
maxAllowed,
|
|
124
|
-
available,
|
|
125
|
-
executionTimeEnd,
|
|
126
|
-
});
|
|
127
|
-
return executionTimeEnd;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Deadline for block proposal reexecution. Ensures the proposer has enough time for publishing. */
|
|
131
|
-
public getValidatorReexecTimeEnd(secondsIntoSlot?: number): number {
|
|
132
|
-
// We need to leave for `afterBlockReexecTimeNeeded` seconds available.
|
|
133
|
-
const afterBlockReexecTimeNeeded = this.p2pPropagationTime + this.l1PublishingTime;
|
|
134
|
-
const validationTimeEnd = this.aztecSlotDuration - afterBlockReexecTimeNeeded;
|
|
135
|
-
this.log.debug(`Validator re-execution time deadline is ${validationTimeEnd}`, {
|
|
136
|
-
secondsIntoSlot,
|
|
137
|
-
validationTimeEnd,
|
|
138
|
-
});
|
|
139
|
-
return validationTimeEnd;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
159
|
public getMaxAllowedTime(
|
|
143
160
|
state: Extract<SequencerState, SequencerState.STOPPED | SequencerState.IDLE | SequencerState.SYNCHRONIZING>,
|
|
144
161
|
): undefined;
|
|
@@ -160,7 +177,7 @@ export class SequencerTimetable {
|
|
|
160
177
|
case SequencerState.CREATING_BLOCK:
|
|
161
178
|
case SequencerState.WAITING_UNTIL_NEXT_BLOCK:
|
|
162
179
|
return this.initializeDeadline + this.checkpointInitializationTime;
|
|
163
|
-
case SequencerState.
|
|
180
|
+
case SequencerState.ASSEMBLING_CHECKPOINT:
|
|
164
181
|
case SequencerState.COLLECTING_ATTESTATIONS:
|
|
165
182
|
return this.aztecSlotDuration - this.l1PublishingTime - 2 * this.p2pPropagationTime;
|
|
166
183
|
case SequencerState.PUBLISHING_CHECKPOINT:
|
|
@@ -192,32 +209,74 @@ export class SequencerTimetable {
|
|
|
192
209
|
}
|
|
193
210
|
|
|
194
211
|
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
212
|
+
* Determines if we can start building the next block and returns its deadline.
|
|
213
|
+
*
|
|
214
|
+
* The timetable divides the slot into fixed sub-slots. This method finds the next
|
|
215
|
+
* available sub-slot that has enough time remaining to build a block.
|
|
216
|
+
*
|
|
217
|
+
* @param secondsIntoSlot - Current time (seconds into the slot)
|
|
218
|
+
* @returns Object with canStart flag, deadline, and whether this is the last block
|
|
201
219
|
*/
|
|
202
|
-
public canStartNextBlock(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
isLastBlock:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
220
|
+
public canStartNextBlock(
|
|
221
|
+
secondsIntoSlot: number,
|
|
222
|
+
):
|
|
223
|
+
| { canStart: true; deadline: undefined; isLastBlock: true }
|
|
224
|
+
| { canStart: false; deadline: undefined; isLastBlock: false }
|
|
225
|
+
| { canStart: boolean; deadline: number; isLastBlock: boolean } {
|
|
226
|
+
// When timetable enforcement is disabled, always allow starting,
|
|
227
|
+
// and build a single block with no deadline. This is here just to
|
|
228
|
+
// satisfy a subset of e2e tests and the sandbox that assume that the
|
|
229
|
+
// sequencer is permanently mining every tx sent.
|
|
230
|
+
if (!this.enforce) {
|
|
231
|
+
return { canStart: true, deadline: undefined, isLastBlock: true };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// If no block duration is set, then we're in single-block mode, which
|
|
235
|
+
// is handled for backwards compatibility towards another subset of e2e tests.
|
|
214
236
|
if (this.blockDuration === undefined) {
|
|
215
|
-
|
|
216
|
-
|
|
237
|
+
// In single block mode, execution and re-execution happen sequentially, so we need to
|
|
238
|
+
// split the available time between them. After building, we need time for attestations and L1.
|
|
239
|
+
const maxAllowed = this.aztecSlotDuration - this.checkpointFinalizationTime;
|
|
240
|
+
const available = (maxAllowed - secondsIntoSlot) / 2; // Split remaining time: half for execution, half for re-execution
|
|
241
|
+
const canStart = available >= this.minExecutionTime;
|
|
242
|
+
const deadline = secondsIntoSlot + available;
|
|
243
|
+
|
|
244
|
+
this.log.verbose(
|
|
245
|
+
`${canStart ? 'Can' : 'Cannot'} start single-block checkpoint at ${secondsIntoSlot}s into slot`,
|
|
246
|
+
{ secondsIntoSlot, maxAllowed, available, deadline },
|
|
247
|
+
);
|
|
248
|
+
return { canStart, deadline, isLastBlock: true };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Otherwise, we're in multi-block-per-slot mode, the default when running in production
|
|
252
|
+
// Find the next available sub-slot that has enough time remaining
|
|
253
|
+
for (let subSlot = 1; subSlot <= this.maxNumberOfBlocks; subSlot++) {
|
|
254
|
+
// Calculate end for this sub-slot
|
|
255
|
+
const deadline = this.initializationOffset + subSlot * this.blockDuration;
|
|
256
|
+
|
|
257
|
+
// Check if we have enough time to build a block with this deadline
|
|
258
|
+
const timeUntilDeadline = deadline - secondsIntoSlot;
|
|
259
|
+
|
|
260
|
+
if (timeUntilDeadline >= this.minExecutionTime) {
|
|
261
|
+
// Found an available sub-slot! Is this the last one?
|
|
262
|
+
const isLastBlock = subSlot === this.maxNumberOfBlocks;
|
|
263
|
+
|
|
264
|
+
this.log.verbose(
|
|
265
|
+
`Can start ${isLastBlock ? 'last block' : 'block'} in sub-slot ${subSlot} with deadline ${deadline}s`,
|
|
266
|
+
{ secondsIntoSlot, deadline, timeUntilDeadline, subSlot, maxBlocks: this.maxNumberOfBlocks },
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return { canStart: true, deadline, isLastBlock };
|
|
270
|
+
}
|
|
217
271
|
}
|
|
218
272
|
|
|
219
|
-
//
|
|
220
|
-
|
|
221
|
-
|
|
273
|
+
// No sub-slots available with enough time
|
|
274
|
+
this.log.verbose(`No time left to start any more blocks`, {
|
|
275
|
+
secondsIntoSlot,
|
|
276
|
+
maxBlocks: this.maxNumberOfBlocks,
|
|
277
|
+
initializationOffset: this.initializationOffset,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
return { canStart: false, deadline: undefined, isLastBlock: false };
|
|
222
281
|
}
|
|
223
282
|
}
|
package/src/sequencer/utils.ts
CHANGED
|
@@ -17,8 +17,8 @@ export enum SequencerState {
|
|
|
17
17
|
CREATING_BLOCK = 'CREATING_BLOCK',
|
|
18
18
|
/** Waiting until the next block can be created. */
|
|
19
19
|
WAITING_UNTIL_NEXT_BLOCK = 'WAITING_UNTIL_NEXT_BLOCK',
|
|
20
|
-
/**
|
|
21
|
-
|
|
20
|
+
/** Assembling and broadcasting the checkpoint. */
|
|
21
|
+
ASSEMBLING_CHECKPOINT = 'ASSEMBLING_CHECKPOINT',
|
|
22
22
|
/** Collecting attestations from its peers. */
|
|
23
23
|
COLLECTING_ATTESTATIONS = 'COLLECTING_ATTESTATIONS',
|
|
24
24
|
/** Sending the tx to L1 with the L2 checkpoint data and awaiting it to be mined.. */
|
|
@@ -33,7 +33,7 @@ export type SequencerStateWithSlot =
|
|
|
33
33
|
| SequencerState.COLLECTING_ATTESTATIONS
|
|
34
34
|
| SequencerState.PUBLISHING_CHECKPOINT
|
|
35
35
|
| SequencerState.PROPOSER_CHECK
|
|
36
|
-
| SequencerState.
|
|
36
|
+
| SequencerState.ASSEMBLING_CHECKPOINT;
|
|
37
37
|
|
|
38
38
|
export type SequencerStateCallback = () => SequencerState;
|
|
39
39
|
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { type BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
4
|
+
import type { FunctionsOf } from '@aztec/foundation/types';
|
|
5
|
+
import { L2BlockNew } from '@aztec/stdlib/block';
|
|
6
|
+
import { Checkpoint } from '@aztec/stdlib/checkpoint';
|
|
7
|
+
import { Gas } from '@aztec/stdlib/gas';
|
|
8
|
+
import type { FullNodeBlockBuilderConfig, PublicProcessorLimits } from '@aztec/stdlib/interfaces/server';
|
|
9
|
+
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
10
|
+
import { makeAppendOnlyTreeSnapshot } from '@aztec/stdlib/testing';
|
|
11
|
+
import type { CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
BuildBlockInCheckpointResult,
|
|
15
|
+
CheckpointBuilder,
|
|
16
|
+
FullNodeCheckpointsBuilder,
|
|
17
|
+
} from '../sequencer/checkpoint_builder.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A fake CheckpointBuilder for testing that implements the same interface as the real one.
|
|
21
|
+
* Can be seeded with blocks to return sequentially on each `buildBlock` call.
|
|
22
|
+
*/
|
|
23
|
+
export class MockCheckpointBuilder implements FunctionsOf<CheckpointBuilder> {
|
|
24
|
+
private blocks: L2BlockNew[] = [];
|
|
25
|
+
private builtBlocks: L2BlockNew[] = [];
|
|
26
|
+
private usedTxsPerBlock: Tx[][] = [];
|
|
27
|
+
private blockIndex = 0;
|
|
28
|
+
|
|
29
|
+
/** Optional function to dynamically provide the block (alternative to seedBlocks) */
|
|
30
|
+
private blockProvider: (() => L2BlockNew) | undefined = undefined;
|
|
31
|
+
|
|
32
|
+
/** Track calls for assertions */
|
|
33
|
+
public buildBlockCalls: Array<{
|
|
34
|
+
blockNumber: BlockNumber;
|
|
35
|
+
timestamp: bigint;
|
|
36
|
+
opts: PublicProcessorLimits;
|
|
37
|
+
}> = [];
|
|
38
|
+
public completeCheckpointCalled = false;
|
|
39
|
+
public getCheckpointCalled = false;
|
|
40
|
+
|
|
41
|
+
/** Set to an error to make buildBlock throw on next call */
|
|
42
|
+
public errorOnBuild: Error | undefined = undefined;
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
private readonly constants: CheckpointGlobalVariables,
|
|
46
|
+
private readonly checkpointNumber: CheckpointNumber,
|
|
47
|
+
) {}
|
|
48
|
+
|
|
49
|
+
/** Seed the builder with blocks to return on successive buildBlock calls */
|
|
50
|
+
seedBlocks(blocks: L2BlockNew[], usedTxsPerBlock?: Tx[][]): this {
|
|
51
|
+
this.blocks = blocks;
|
|
52
|
+
this.usedTxsPerBlock = usedTxsPerBlock ?? blocks.map(() => []);
|
|
53
|
+
this.blockIndex = 0;
|
|
54
|
+
this.blockProvider = undefined;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Set a function that provides blocks dynamically.
|
|
60
|
+
* Useful for tests where the block is determined at call time (e.g., sequencer tests).
|
|
61
|
+
*/
|
|
62
|
+
setBlockProvider(provider: () => L2BlockNew): this {
|
|
63
|
+
this.blockProvider = provider;
|
|
64
|
+
this.blocks = [];
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getConstantData(): CheckpointGlobalVariables {
|
|
69
|
+
return this.constants;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
buildBlock(
|
|
73
|
+
_pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
|
|
74
|
+
blockNumber: BlockNumber,
|
|
75
|
+
timestamp: bigint,
|
|
76
|
+
opts: PublicProcessorLimits,
|
|
77
|
+
): Promise<BuildBlockInCheckpointResult> {
|
|
78
|
+
this.buildBlockCalls.push({ blockNumber, timestamp, opts });
|
|
79
|
+
|
|
80
|
+
if (this.errorOnBuild) {
|
|
81
|
+
return Promise.reject(this.errorOnBuild);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let block: L2BlockNew;
|
|
85
|
+
let usedTxs: Tx[];
|
|
86
|
+
|
|
87
|
+
if (this.blockProvider) {
|
|
88
|
+
// Dynamic mode: get block from provider
|
|
89
|
+
block = this.blockProvider();
|
|
90
|
+
usedTxs = [];
|
|
91
|
+
this.builtBlocks.push(block);
|
|
92
|
+
} else {
|
|
93
|
+
// Seeded mode: get block from pre-seeded list
|
|
94
|
+
block = this.blocks[this.blockIndex];
|
|
95
|
+
usedTxs = this.usedTxsPerBlock[this.blockIndex] ?? [];
|
|
96
|
+
this.blockIndex++;
|
|
97
|
+
this.builtBlocks.push(block);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return Promise.resolve({
|
|
101
|
+
block,
|
|
102
|
+
publicGas: Gas.empty(),
|
|
103
|
+
publicProcessorDuration: 0,
|
|
104
|
+
numTxs: block?.body?.txEffects?.length ?? usedTxs.length,
|
|
105
|
+
blockBuildingTimer: new Timer(),
|
|
106
|
+
usedTxs,
|
|
107
|
+
failedTxs: [],
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
completeCheckpoint(): Promise<Checkpoint> {
|
|
112
|
+
this.completeCheckpointCalled = true;
|
|
113
|
+
const allBlocks = this.blockProvider ? this.builtBlocks : this.blocks;
|
|
114
|
+
const lastBlock = allBlocks[allBlocks.length - 1];
|
|
115
|
+
// Create a CheckpointHeader from the last block's header for testing
|
|
116
|
+
const checkpointHeader = this.createCheckpointHeader(lastBlock);
|
|
117
|
+
return Promise.resolve(
|
|
118
|
+
new Checkpoint(
|
|
119
|
+
makeAppendOnlyTreeSnapshot(lastBlock.header.globalVariables.blockNumber + 1),
|
|
120
|
+
checkpointHeader,
|
|
121
|
+
allBlocks,
|
|
122
|
+
this.checkpointNumber,
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getCheckpoint(): Promise<Checkpoint> {
|
|
128
|
+
this.getCheckpointCalled = true;
|
|
129
|
+
const builtBlocks = this.blockProvider ? this.builtBlocks : this.blocks.slice(0, this.blockIndex);
|
|
130
|
+
const lastBlock = builtBlocks[builtBlocks.length - 1];
|
|
131
|
+
if (!lastBlock) {
|
|
132
|
+
throw new Error('No blocks built yet');
|
|
133
|
+
}
|
|
134
|
+
// Create a CheckpointHeader from the last block's header for testing
|
|
135
|
+
const checkpointHeader = this.createCheckpointHeader(lastBlock);
|
|
136
|
+
return Promise.resolve(
|
|
137
|
+
new Checkpoint(
|
|
138
|
+
makeAppendOnlyTreeSnapshot(lastBlock.header.globalVariables.blockNumber + 1),
|
|
139
|
+
checkpointHeader,
|
|
140
|
+
builtBlocks,
|
|
141
|
+
this.checkpointNumber,
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Creates a CheckpointHeader from a block's header for testing.
|
|
148
|
+
* This is a simplified version that creates a minimal CheckpointHeader.
|
|
149
|
+
*/
|
|
150
|
+
private createCheckpointHeader(block: L2BlockNew): CheckpointHeader {
|
|
151
|
+
const header = block.header;
|
|
152
|
+
const gv = header.globalVariables;
|
|
153
|
+
return CheckpointHeader.empty({
|
|
154
|
+
lastArchiveRoot: header.lastArchive.root,
|
|
155
|
+
blockHeadersHash: Fr.random(), // Use random for testing
|
|
156
|
+
slotNumber: gv.slotNumber,
|
|
157
|
+
timestamp: gv.timestamp,
|
|
158
|
+
coinbase: gv.coinbase,
|
|
159
|
+
feeRecipient: gv.feeRecipient,
|
|
160
|
+
gasFees: gv.gasFees,
|
|
161
|
+
totalManaUsed: header.totalManaUsed,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Reset for reuse in another test */
|
|
166
|
+
reset(): void {
|
|
167
|
+
this.blocks = [];
|
|
168
|
+
this.builtBlocks = [];
|
|
169
|
+
this.usedTxsPerBlock = [];
|
|
170
|
+
this.blockIndex = 0;
|
|
171
|
+
this.buildBlockCalls = [];
|
|
172
|
+
this.completeCheckpointCalled = false;
|
|
173
|
+
this.getCheckpointCalled = false;
|
|
174
|
+
this.errorOnBuild = undefined;
|
|
175
|
+
this.blockProvider = undefined;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A fake CheckpointsBuilder (factory) for testing that implements the same interface
|
|
181
|
+
* as FullNodeCheckpointsBuilder. Returns MockCheckpointBuilder instances.
|
|
182
|
+
* Does NOT use jest mocks - this is a proper test double.
|
|
183
|
+
*/
|
|
184
|
+
export class MockCheckpointsBuilder implements FunctionsOf<FullNodeCheckpointsBuilder> {
|
|
185
|
+
private checkpointBuilder: MockCheckpointBuilder | undefined;
|
|
186
|
+
|
|
187
|
+
/** Track calls for assertions */
|
|
188
|
+
public startCheckpointCalls: Array<{
|
|
189
|
+
checkpointNumber: CheckpointNumber;
|
|
190
|
+
constants: CheckpointGlobalVariables;
|
|
191
|
+
l1ToL2Messages: Fr[];
|
|
192
|
+
}> = [];
|
|
193
|
+
public updateConfigCalls: Array<Partial<FullNodeBlockBuilderConfig>> = [];
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Set the MockCheckpointBuilder to return from startCheckpoint.
|
|
197
|
+
* Must be called before startCheckpoint is invoked.
|
|
198
|
+
*/
|
|
199
|
+
setCheckpointBuilder(builder: MockCheckpointBuilder): this {
|
|
200
|
+
this.checkpointBuilder = builder;
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Creates a new MockCheckpointBuilder with the given constants.
|
|
206
|
+
* Convenience method that creates and sets the builder in one call.
|
|
207
|
+
*/
|
|
208
|
+
createCheckpointBuilder(
|
|
209
|
+
constants: CheckpointGlobalVariables,
|
|
210
|
+
checkpointNumber: CheckpointNumber,
|
|
211
|
+
): MockCheckpointBuilder {
|
|
212
|
+
this.checkpointBuilder = new MockCheckpointBuilder(constants, checkpointNumber);
|
|
213
|
+
return this.checkpointBuilder;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Get the current checkpoint builder (for assertions) */
|
|
217
|
+
getCheckpointBuilder(): MockCheckpointBuilder | undefined {
|
|
218
|
+
return this.checkpointBuilder;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
updateConfig(config: Partial<FullNodeBlockBuilderConfig>): void {
|
|
222
|
+
this.updateConfigCalls.push(config);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
startCheckpoint(
|
|
226
|
+
checkpointNumber: CheckpointNumber,
|
|
227
|
+
constants: CheckpointGlobalVariables,
|
|
228
|
+
l1ToL2Messages: Fr[],
|
|
229
|
+
_fork: unknown,
|
|
230
|
+
): Promise<CheckpointBuilder> {
|
|
231
|
+
this.startCheckpointCalls.push({ checkpointNumber, constants, l1ToL2Messages });
|
|
232
|
+
|
|
233
|
+
if (!this.checkpointBuilder) {
|
|
234
|
+
// Auto-create a builder if none was set
|
|
235
|
+
this.checkpointBuilder = new MockCheckpointBuilder(constants, checkpointNumber);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return Promise.resolve(this.checkpointBuilder as unknown as CheckpointBuilder);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Reset for reuse in another test */
|
|
242
|
+
reset(): void {
|
|
243
|
+
this.checkpointBuilder = undefined;
|
|
244
|
+
this.startCheckpointCalls = [];
|
|
245
|
+
this.updateConfigCalls = [];
|
|
246
|
+
}
|
|
247
|
+
}
|