@aztec/sequencer-client 0.0.1-commit.b9865e97 → 0.0.1-commit.be03c316

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.
Files changed (101) hide show
  1. package/README.md +27 -27
  2. package/dest/client/sequencer-client.d.ts +16 -3
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +17 -12
  5. package/dest/config.d.ts +6 -2
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +23 -14
  8. package/dest/global_variable_builder/fee_predictor.d.ts +1 -1
  9. package/dest/global_variable_builder/fee_predictor.d.ts.map +1 -1
  10. package/dest/global_variable_builder/fee_predictor.js +11 -1
  11. package/dest/global_variable_builder/fee_provider.d.ts +1 -1
  12. package/dest/global_variable_builder/fee_provider.d.ts.map +1 -1
  13. package/dest/global_variable_builder/fee_provider.js +27 -5
  14. package/dest/global_variable_builder/global_builder.d.ts +3 -16
  15. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  16. package/dest/global_variable_builder/global_builder.js +2 -25
  17. package/dest/index.d.ts +2 -2
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +1 -1
  20. package/dest/publisher/config.d.ts +5 -1
  21. package/dest/publisher/config.d.ts.map +1 -1
  22. package/dest/publisher/config.js +11 -1
  23. package/dest/publisher/l1_to_l2_messaging.d.ts +21 -0
  24. package/dest/publisher/l1_to_l2_messaging.d.ts.map +1 -0
  25. package/dest/publisher/l1_to_l2_messaging.js +70 -0
  26. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +48 -8
  27. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -1
  28. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +68 -1
  29. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +2 -2
  30. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -1
  31. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +4 -2
  32. package/dest/publisher/l1_tx_failed_store/index.d.ts +2 -2
  33. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -1
  34. package/dest/publisher/l1_tx_failed_store/index.js +1 -0
  35. package/dest/publisher/sequencer-publisher.d.ts +34 -6
  36. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  37. package/dest/publisher/sequencer-publisher.js +362 -61
  38. package/dest/publisher/write_json.d.ts +11 -0
  39. package/dest/publisher/write_json.d.ts.map +1 -0
  40. package/dest/publisher/write_json.js +57 -0
  41. package/dest/sequencer/automine/automine_factory.d.ts +5 -3
  42. package/dest/sequencer/automine/automine_factory.d.ts.map +1 -1
  43. package/dest/sequencer/automine/automine_factory.js +2 -1
  44. package/dest/sequencer/automine/automine_sequencer.d.ts +43 -5
  45. package/dest/sequencer/automine/automine_sequencer.d.ts.map +1 -1
  46. package/dest/sequencer/automine/automine_sequencer.js +199 -18
  47. package/dest/sequencer/automine/index.d.ts +3 -0
  48. package/dest/sequencer/automine/index.d.ts.map +1 -0
  49. package/dest/sequencer/automine/index.js +2 -0
  50. package/dest/sequencer/checkpoint_proposal_job.d.ts +24 -16
  51. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  52. package/dest/sequencer/checkpoint_proposal_job.js +98 -90
  53. package/dest/sequencer/events.d.ts +16 -5
  54. package/dest/sequencer/events.d.ts.map +1 -1
  55. package/dest/sequencer/index.d.ts +1 -3
  56. package/dest/sequencer/index.d.ts.map +1 -1
  57. package/dest/sequencer/index.js +0 -2
  58. package/dest/sequencer/requests_tracker.d.ts +22 -0
  59. package/dest/sequencer/requests_tracker.d.ts.map +1 -0
  60. package/dest/sequencer/requests_tracker.js +33 -0
  61. package/dest/sequencer/sequencer.d.ts +110 -36
  62. package/dest/sequencer/sequencer.d.ts.map +1 -1
  63. package/dest/sequencer/sequencer.js +329 -174
  64. package/dest/test/index.d.ts +3 -3
  65. package/dest/test/index.d.ts.map +1 -1
  66. package/dest/test/utils.d.ts +15 -1
  67. package/dest/test/utils.d.ts.map +1 -1
  68. package/dest/test/utils.js +18 -1
  69. package/package.json +30 -28
  70. package/src/client/sequencer-client.ts +20 -13
  71. package/src/config.ts +25 -12
  72. package/src/global_variable_builder/fee_predictor.ts +11 -1
  73. package/src/global_variable_builder/fee_provider.ts +27 -5
  74. package/src/global_variable_builder/global_builder.ts +2 -34
  75. package/src/index.ts +1 -10
  76. package/src/publisher/config.ts +22 -1
  77. package/src/publisher/l1_to_l2_messaging.ts +85 -0
  78. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +114 -7
  79. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +4 -3
  80. package/src/publisher/l1_tx_failed_store/index.ts +1 -1
  81. package/src/publisher/sequencer-publisher.ts +383 -72
  82. package/src/publisher/write_json.ts +78 -0
  83. package/src/sequencer/automine/README.md +18 -4
  84. package/src/sequencer/automine/automine_factory.ts +8 -1
  85. package/src/sequencer/automine/automine_sequencer.ts +221 -20
  86. package/src/sequencer/automine/index.ts +6 -0
  87. package/src/sequencer/checkpoint_proposal_job.ts +119 -100
  88. package/src/sequencer/events.ts +16 -4
  89. package/src/sequencer/index.ts +0 -2
  90. package/src/sequencer/requests_tracker.ts +43 -0
  91. package/src/sequencer/sequencer.ts +360 -187
  92. package/src/test/index.ts +2 -2
  93. package/src/test/utils.ts +33 -0
  94. package/dest/sequencer/chain_state_overrides.d.ts +0 -61
  95. package/dest/sequencer/chain_state_overrides.d.ts.map +0 -1
  96. package/dest/sequencer/chain_state_overrides.js +0 -98
  97. package/dest/sequencer/timetable.d.ts +0 -98
  98. package/dest/sequencer/timetable.d.ts.map +0 -1
  99. package/dest/sequencer/timetable.js +0 -227
  100. package/src/sequencer/chain_state_overrides.ts +0 -162
  101. package/src/sequencer/timetable.ts +0 -288
@@ -1,162 +0,0 @@
1
- import { RollupContract, SimulationOverridesBuilder, type SimulationOverridesPlan } from '@aztec/ethereum/contracts';
2
- import { CheckpointNumber } from '@aztec/foundation/branded-types';
3
- import type { Logger } from '@aztec/foundation/log';
4
- import { type ProposedCheckpointData, computeCheckpointPayloadDigest } from '@aztec/stdlib/checkpoint';
5
- import type { CoordinationSignatureContext } from '@aztec/stdlib/p2p';
6
-
7
- type CheckpointSimulationOverridesPlanInput = {
8
- /** Target rollup contract. */
9
- rollup: RollupContract;
10
- /** Checkpoint number to be proposed. */
11
- checkpointNumber: CheckpointNumber;
12
- /** Logger instance. */
13
- log: Logger;
14
- /**
15
- * The proposed parent checkpoint when pipelining. Its `checkpointNumber` must equal
16
- * `checkpointNumber - 1`; the helper enforces this. Mutually exclusive with
17
- * `invalidateToPendingCheckpointNumber`.
18
- */
19
- proposedCheckpointData?: ProposedCheckpointData;
20
- /**
21
- * The pending checkpoint number we'll end up at after invalidation lands. Mutually exclusive
22
- * with `proposedCheckpointData`.
23
- */
24
- invalidateToPendingCheckpointNumber?: CheckpointNumber;
25
- /**
26
- * The real on-chain pending checkpoint number (typically `syncedTo.checkpointedCheckpointNumber`).
27
- * Used as the snapshot we pin both `pending` and `proven` to avoid prunes in simulation.
28
- */
29
- checkpointedCheckpointNumber: CheckpointNumber;
30
- /**
31
- * Chain-level consensus signature context. Used to recompute the parent's `payloadDigest` for the
32
- * pipelined simulation override so it matches what `propose` will write into `tempCheckpointLogs[parent]`
33
- * once the parent lands.
34
- */
35
- signatureContext: CoordinationSignatureContext;
36
- };
37
-
38
- /**
39
- * Builds the SimulationOverridesPlan describing the simulated L1 rollup state for a checkpoint's
40
- * enqueue-time simulations: `canProposeAt` (in Sequencer.doWork) and the propose-related sims
41
- * (validateBlockHeader, simulateProposeTx). The plan reflects "as if our pipelined parent
42
- * checkpoint has landed and any required invalidation has executed" — the gap that needs to be
43
- * bridged at enqueue time.
44
- *
45
- * Pipelining (`proposedCheckpointData`) and invalidation (`invalidateToPendingCheckpointNumber`)
46
- * are mutually exclusive; passing both throws.
47
- */
48
- export async function buildCheckpointSimulationOverridesPlan(
49
- input: CheckpointSimulationOverridesPlanInput,
50
- ): Promise<SimulationOverridesPlan | undefined> {
51
- if (input.proposedCheckpointData && input.invalidateToPendingCheckpointNumber !== undefined) {
52
- throw new Error(
53
- 'Error in buildCheckpointSimulationOverridesPlan: proposedCheckpointData and invalidateToPendingCheckpointNumber are mutually exclusive',
54
- );
55
- }
56
-
57
- const builder = new SimulationOverridesBuilder();
58
- const pendingCheckpointNumber = derivePendingCheckpointNumber(input);
59
-
60
- // Override the latest checkpoint number when invalidating or pipelining, so our checkpoint
61
- // follows from it. We also override the proven chain tip so we dont need to worry about
62
- // prunes kicking in that would break out simulation if there's a prune pending. We always
63
- // assume that a proof will land in time. If we don't have a pending checkpoint number to force,
64
- // we still set both tips to the current checkpoint number to avoid the prune trigger.
65
- const overridenChainTip = pendingCheckpointNumber ?? input.checkpointedCheckpointNumber;
66
- builder.withChainTips({ pending: overridenChainTip, proven: overridenChainTip });
67
-
68
- if (input.proposedCheckpointData) {
69
- const { header, archive, checkpointOutHash, feeAssetPriceModifier } = input.proposedCheckpointData;
70
- builder.withPendingArchive(archive.root);
71
- // Override every locally-derivable `tempCheckpointLogs[parent]` field that L1 will eventually
72
- // write. `slotNumber` is load-bearing for `STFLib.canPruneAtTime`: without it the cell reads
73
- // slotNumber 0, the contract treats the pending tip as belonging to an expired epoch, and
74
- // `getEffectivePendingCheckpointNumber` silently collapses pending back to proven — producing
75
- // a spurious `Rollup__InvalidArchive` against the on-chain genesis archive. The other fields
76
- // (headerHash, outHash, payloadDigest) are not strictly load-bearing for `canProposeAt` /
77
- // `validateBlockHeader`, but mirroring the full cell keeps the simulation byte-faithful with
78
- // what the actual `propose()` send will observe, which is a defense against future reads
79
- // taking dependencies on them.
80
- builder.withPendingTempCheckpointLogFields({
81
- headerHash: header.hash(),
82
- outHash: checkpointOutHash,
83
- slotNumber: header.slotNumber,
84
- payloadDigest: computeCheckpointPayloadDigest({
85
- header,
86
- archiveRoot: archive.root,
87
- feeAssetPriceModifier,
88
- signatureContext: input.signatureContext,
89
- }),
90
- });
91
-
92
- const feeHeader = await computePipelinedParentFeeHeader({
93
- checkpointNumber: input.checkpointNumber,
94
- proposedCheckpointData: input.proposedCheckpointData,
95
- rollup: input.rollup,
96
- log: input.log,
97
- });
98
- if (feeHeader) {
99
- builder.withPendingFeeHeader(feeHeader);
100
- }
101
- }
102
-
103
- return builder.build();
104
- }
105
-
106
- function derivePendingCheckpointNumber(input: CheckpointSimulationOverridesPlanInput): CheckpointNumber | undefined {
107
- if (input.invalidateToPendingCheckpointNumber !== undefined) {
108
- return input.invalidateToPendingCheckpointNumber;
109
- }
110
- if (!input.proposedCheckpointData) {
111
- return undefined;
112
- }
113
- if (input.checkpointNumber < 1) {
114
- throw new Error(`Cannot build simulation override for checkpoint ${input.checkpointNumber}: no parent exists`);
115
- }
116
- const expectedParent = CheckpointNumber(input.checkpointNumber - 1);
117
- if (input.proposedCheckpointData.checkpointNumber !== expectedParent) {
118
- throw new Error(
119
- `Cannot build simulation override for checkpoint ${input.checkpointNumber}: proposedCheckpointData.checkpointNumber (${input.proposedCheckpointData.checkpointNumber}) does not match expected parent ${expectedParent}`,
120
- );
121
- }
122
- return expectedParent;
123
- }
124
-
125
- type PipelinedParentFeeHeaderInput = {
126
- checkpointNumber: CheckpointNumber;
127
- proposedCheckpointData: ProposedCheckpointData;
128
- rollup: RollupContract;
129
- log: Logger;
130
- };
131
-
132
- /**
133
- * Derives the pending parent fee header used during pipelined proposal simulation. Returns
134
- * `undefined` only when no grandparent exists (i.e. the proposed parent is the genesis
135
- * checkpoint); all other failure modes (missing grandparent state, missing fee header, RPC
136
- * errors) throw so callers don't silently desync the fee-header override.
137
- */
138
- export async function computePipelinedParentFeeHeader(input: PipelinedParentFeeHeaderInput) {
139
- if (input.checkpointNumber < 2) {
140
- return undefined;
141
- }
142
-
143
- const grandparentCheckpointNumber = CheckpointNumber(input.checkpointNumber - 2);
144
-
145
- const [grandparentCheckpoint, manaTarget] = await Promise.all([
146
- input.rollup.getCheckpoint(grandparentCheckpointNumber),
147
- input.rollup.getManaTarget(),
148
- ]);
149
-
150
- if (!grandparentCheckpoint?.feeHeader) {
151
- throw new Error(
152
- `Grandparent checkpoint or feeHeader missing for checkpoint ${grandparentCheckpointNumber.toString()}`,
153
- );
154
- }
155
-
156
- return RollupContract.computeChildFeeHeader(
157
- grandparentCheckpoint.feeHeader,
158
- input.proposedCheckpointData.totalManaUsed,
159
- input.proposedCheckpointData.feeAssetPriceModifier,
160
- manaTarget,
161
- );
162
- }
@@ -1,288 +0,0 @@
1
- import type { Logger } from '@aztec/foundation/log';
2
- import {
3
- CHECKPOINT_ASSEMBLE_TIME,
4
- CHECKPOINT_INITIALIZATION_TIME,
5
- type CheckpointTiming,
6
- DEFAULT_P2P_PROPAGATION_TIME,
7
- MIN_EXECUTION_TIME,
8
- createCheckpointTimingModel,
9
- } from '@aztec/stdlib/timetable';
10
-
11
- import { SequencerTooSlowError } from './errors.js';
12
- import type { SequencerMetrics } from './metrics.js';
13
- import { SequencerState } from './utils.js';
14
-
15
- export class SequencerTimetable {
16
- private readonly checkpointTiming: CheckpointTiming;
17
-
18
- /**
19
- * How late into the slot can we be to start working. Computed as the total time needed for assembling and publishing a block,
20
- * assuming an execution time equal to `minExecutionTime`, subtracted from the slot duration. This means that, if the proposer
21
- * starts building at this time, and all times hold, it will have at least `minExecutionTime` to execute txs for the block.
22
- */
23
- public readonly initializeDeadline: number;
24
-
25
- /**
26
- * Fixed time offset (in seconds) when the first sub-slot starts, used as baseline for all block deadlines.
27
- * This is an estimate of how long initialization (sync + proposer check) typically takes.
28
- * If actual initialization takes longer/shorter, blocks just get less/more time accordingly.
29
- */
30
- public readonly initializationOffset: number;
31
-
32
- /**
33
- * Total time needed to finalize and publish a checkpoint, including:
34
- * - Assembling the checkpoint
35
- * - Round-trip p2p propagation for the checkpoint proposal and its corresponding attestations
36
- * - Publishing to L1
37
- */
38
- public readonly checkpointFinalizationTime: number;
39
-
40
- /**
41
- * How long it takes to get a published block into L1. L1 builders typically accept txs up to 4 seconds into their slot,
42
- * but we'll timeout sooner to give it more time to propagate (remember we also have blobs!). Still, when working in anvil,
43
- * we can just post in the very last second of the L1 slot and still expect the tx to be accepted.
44
- */
45
- public readonly l1PublishingTime: number;
46
-
47
- /**
48
- * What's the minimum time we want to leave available for execution and reexecution (used to derive init deadline)
49
- * Defaults to half of the block duration if set, otherwise a constant.
50
- */
51
- public readonly minExecutionTime: number = MIN_EXECUTION_TIME;
52
-
53
- /** How long it takes to get ready to start building */
54
- public readonly checkpointInitializationTime: number = CHECKPOINT_INITIALIZATION_TIME;
55
-
56
- /** How long it takes to for proposals and attestations to travel across the p2p layer (one-way) */
57
- public readonly p2pPropagationTime: number;
58
-
59
- /** How much time we spend assembling a checkpoint after building the last block */
60
- public readonly checkpointAssembleTime: number = CHECKPOINT_ASSEMBLE_TIME;
61
-
62
- /** Ethereum slot duration in seconds */
63
- public readonly ethereumSlotDuration: number;
64
-
65
- /** Aztec slot duration in seconds (must be multiple of ethereum slot duration) */
66
- public readonly aztecSlotDuration: number;
67
-
68
- /** Whether assertTimeLeft will throw if not enough time. */
69
- public readonly enforce: boolean;
70
-
71
- /** Duration per block when building multiple blocks per slot (undefined = single block per slot) */
72
- public readonly blockDuration: number | undefined;
73
-
74
- /** Maximum number of blocks that can be built in this slot configuration */
75
- public readonly maxNumberOfBlocks: number;
76
-
77
- /**
78
- * How far into the target slot assembly and attestation collection can start when pipelining.
79
- * Production-like timing keeps this at zero; fast local publishing profiles can use the attestation window.
80
- */
81
- public readonly pipeliningAttestationGracePeriod: number;
82
-
83
- constructor(
84
- opts: {
85
- ethereumSlotDuration: number;
86
- aztecSlotDuration: number;
87
- l1PublishingTime: number;
88
- p2pPropagationTime?: number;
89
- blockDurationMs?: number;
90
- enforce: boolean;
91
- },
92
- private readonly metrics?: SequencerMetrics,
93
- private readonly log?: Logger,
94
- ) {
95
- this.ethereumSlotDuration = opts.ethereumSlotDuration;
96
- this.aztecSlotDuration = opts.aztecSlotDuration;
97
- this.l1PublishingTime = opts.l1PublishingTime;
98
- this.blockDuration = opts.blockDurationMs ? opts.blockDurationMs / 1000 : undefined;
99
- this.enforce = opts.enforce;
100
-
101
- this.checkpointTiming = createCheckpointTimingModel({
102
- aztecSlotDuration: this.aztecSlotDuration,
103
- ethereumSlotDuration: this.ethereumSlotDuration,
104
- blockDuration: this.blockDuration,
105
- l1PublishingTime: this.l1PublishingTime,
106
- p2pPropagationTime: opts.p2pPropagationTime ?? DEFAULT_P2P_PROPAGATION_TIME,
107
- });
108
-
109
- this.p2pPropagationTime = this.checkpointTiming.p2pPropagationTime;
110
- this.checkpointAssembleTime = this.checkpointTiming.checkpointAssembleTime;
111
- this.checkpointInitializationTime = this.checkpointTiming.checkpointInitializationTime;
112
- this.minExecutionTime = this.checkpointTiming.minExecutionTime;
113
-
114
- // Calculate initialization offset - estimate of time needed for sync + proposer check
115
- // This is the baseline for all sub-slot deadlines
116
- this.initializationOffset = this.checkpointTiming.checkpointInitializationTime;
117
- this.checkpointFinalizationTime = this.checkpointTiming.checkpointFinalizationTime;
118
- this.pipeliningAttestationGracePeriod = this.checkpointTiming.pipeliningAttestationGracePeriod;
119
- this.maxNumberOfBlocks = this.checkpointTiming.calculateMaxBlocksPerSlot();
120
- this.initializeDeadline = this.checkpointTiming.initializeDeadline;
121
-
122
- this.log?.info(
123
- `Sequencer timetable initialized with ${this.maxNumberOfBlocks} blocks per slot (${this.enforce ? 'enforced' : 'not enforced'})`,
124
- {
125
- ethereumSlotDuration: this.ethereumSlotDuration,
126
- aztecSlotDuration: this.aztecSlotDuration,
127
- l1PublishingTime: this.l1PublishingTime,
128
- minExecutionTime: this.minExecutionTime,
129
- blockPrepareTime: this.checkpointInitializationTime,
130
- p2pPropagationTime: this.p2pPropagationTime,
131
- blockAssembleTime: this.checkpointAssembleTime,
132
- initializeDeadline: this.initializeDeadline,
133
- enforce: this.enforce,
134
- pipeliningAttestationGracePeriod: this.pipeliningAttestationGracePeriod,
135
- minWorkToDo: this.checkpointTiming.minimumBuildSlotWork,
136
- blockDuration: this.blockDuration,
137
- maxNumberOfBlocks: this.maxNumberOfBlocks,
138
- },
139
- );
140
-
141
- if (this.initializeDeadline <= 0) {
142
- throw new Error(
143
- `Block proposal initialize deadline cannot be negative (got ${this.initializeDeadline} from total time needed ${this.checkpointTiming.minimumBuildSlotWork} and a slot duration of ${this.aztecSlotDuration}).`,
144
- );
145
- }
146
- }
147
-
148
- public getCheckpointAssemblyDeadline(): number {
149
- return this.checkpointTiming.checkpointAssemblyDeadline;
150
- }
151
-
152
- public getCheckpointAttestationDeadline(): number {
153
- return this.checkpointTiming.checkpointAttestationDeadline;
154
- }
155
-
156
- public getCheckpointAttestationStartDeadline(): number {
157
- return this.checkpointTiming.checkpointAttestationStartDeadline;
158
- }
159
-
160
- public getCheckpointPublishingDeadline(): number {
161
- return this.checkpointTiming.checkpointPublishingDeadline;
162
- }
163
-
164
- public getMaxAllowedTime(
165
- state: Extract<SequencerState, SequencerState.STOPPED | SequencerState.IDLE | SequencerState.SYNCHRONIZING>,
166
- ): undefined;
167
- public getMaxAllowedTime(
168
- state: Exclude<SequencerState, SequencerState.STOPPED | SequencerState.IDLE | SequencerState.SYNCHRONIZING>,
169
- ): number;
170
- public getMaxAllowedTime(state: SequencerState): number | undefined;
171
- public getMaxAllowedTime(state: SequencerState): number | undefined {
172
- switch (state) {
173
- case SequencerState.STOPPED:
174
- case SequencerState.STOPPING:
175
- case SequencerState.IDLE:
176
- case SequencerState.SYNCHRONIZING:
177
- return; // We don't really care about times for this states
178
- case SequencerState.PROPOSER_CHECK:
179
- case SequencerState.INITIALIZING_CHECKPOINT:
180
- return this.initializeDeadline;
181
- case SequencerState.WAITING_FOR_TXS:
182
- case SequencerState.CREATING_BLOCK:
183
- case SequencerState.WAITING_UNTIL_NEXT_BLOCK:
184
- return this.initializeDeadline + this.checkpointInitializationTime;
185
- case SequencerState.ASSEMBLING_CHECKPOINT:
186
- return this.getCheckpointAssemblyDeadline();
187
- case SequencerState.COLLECTING_ATTESTATIONS:
188
- return this.getCheckpointAttestationStartDeadline();
189
- case SequencerState.PUBLISHING_CHECKPOINT:
190
- return this.getCheckpointPublishingDeadline();
191
- default: {
192
- const _exhaustiveCheck: never = state;
193
- throw new Error(`Unexpected state: ${state}`);
194
- }
195
- }
196
- }
197
-
198
- public assertTimeLeft(newState: SequencerState, secondsIntoSlot: number) {
199
- if (!this.enforce) {
200
- return;
201
- }
202
-
203
- const maxAllowedTime = this.getMaxAllowedTime(newState);
204
- if (maxAllowedTime === undefined) {
205
- return;
206
- }
207
-
208
- const bufferSeconds = maxAllowedTime - secondsIntoSlot;
209
- if (bufferSeconds < 0) {
210
- throw new SequencerTooSlowError(newState, maxAllowedTime, secondsIntoSlot);
211
- }
212
-
213
- this.metrics?.recordStateTransitionBufferMs(Math.floor(bufferSeconds * 1000), newState);
214
- this.log?.trace(`Enough time to transition to ${newState}`, { maxAllowedTime, secondsIntoSlot });
215
- }
216
-
217
- /**
218
- * Determines if we can start building the next block and returns its deadline.
219
- *
220
- * The timetable divides the slot into fixed sub-slots. This method finds the next
221
- * available sub-slot that has enough time remaining to build a block.
222
- *
223
- * @param secondsIntoSlot - Current time (seconds into the slot)
224
- * @returns Object with canStart flag, deadline, and whether this is the last block
225
- */
226
- public canStartNextBlock(
227
- secondsIntoSlot: number,
228
- ):
229
- | { canStart: true; deadline: undefined; isLastBlock: true }
230
- | { canStart: false; deadline: undefined; isLastBlock: false }
231
- | { canStart: boolean; deadline: number; isLastBlock: boolean } {
232
- // When timetable enforcement is disabled, always allow starting,
233
- // and build a single block with no deadline. This is here just to
234
- // satisfy a subset of e2e tests and the sandbox that assume that the
235
- // sequencer is permanently mining every tx sent.
236
- if (!this.enforce) {
237
- return { canStart: true, deadline: undefined, isLastBlock: true };
238
- }
239
-
240
- // If no block duration is set, then we're in single-block mode, which
241
- // is handled for backwards compatibility towards another subset of e2e tests.
242
- if (this.blockDuration === undefined) {
243
- // In single block mode, execution and re-execution happen sequentially, so we need to
244
- // split the available time between them. After building, we need time for attestations and L1.
245
- const maxAllowed = this.aztecSlotDuration - this.checkpointFinalizationTime;
246
- const available = (maxAllowed - secondsIntoSlot) / 2; // Split remaining time: half for execution, half for re-execution
247
- const canStart = available >= this.minExecutionTime;
248
- const deadline = secondsIntoSlot + available;
249
-
250
- this.log?.verbose(
251
- `${canStart ? 'Can' : 'Cannot'} start single-block checkpoint at ${secondsIntoSlot}s into slot`,
252
- { secondsIntoSlot, maxAllowed, available, deadline },
253
- );
254
- return { canStart, deadline, isLastBlock: true };
255
- }
256
-
257
- // Otherwise, we're in multi-block-per-slot mode, the default when running in production
258
- // Find the next available sub-slot that has enough time remaining
259
- for (let subSlot = 1; subSlot <= this.maxNumberOfBlocks; subSlot++) {
260
- // Calculate end for this sub-slot
261
- const deadline = this.initializationOffset + subSlot * this.blockDuration;
262
-
263
- // Check if we have enough time to build a block with this deadline
264
- const timeUntilDeadline = deadline - secondsIntoSlot;
265
-
266
- if (timeUntilDeadline >= this.minExecutionTime) {
267
- // Found an available sub-slot! Is this the last one?
268
- const isLastBlock = subSlot === this.maxNumberOfBlocks;
269
-
270
- this.log?.verbose(
271
- `Can start ${isLastBlock ? 'last block' : 'block'} in sub-slot ${subSlot} with deadline ${deadline}s`,
272
- { secondsIntoSlot, deadline, timeUntilDeadline, subSlot, maxBlocks: this.maxNumberOfBlocks },
273
- );
274
-
275
- return { canStart: true, deadline, isLastBlock };
276
- }
277
- }
278
-
279
- // No sub-slots available with enough time
280
- this.log?.verbose(`No time left to start any more blocks`, {
281
- secondsIntoSlot,
282
- maxBlocks: this.maxNumberOfBlocks,
283
- initializationOffset: this.initializationOffset,
284
- });
285
-
286
- return { canStart: false, deadline: undefined, isLastBlock: false };
287
- }
288
- }