@aztec/sequencer-client 0.0.1-commit.a072138 → 0.0.1-commit.a4600f49

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 (129) hide show
  1. package/README.md +281 -21
  2. package/dest/client/sequencer-client.d.ts +34 -11
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +77 -39
  5. package/dest/config.d.ts +32 -6
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +94 -46
  8. package/dest/global_variable_builder/fee_predictor.d.ts +37 -0
  9. package/dest/global_variable_builder/fee_predictor.d.ts.map +1 -0
  10. package/dest/global_variable_builder/fee_predictor.js +138 -0
  11. package/dest/global_variable_builder/fee_provider.d.ts +21 -0
  12. package/dest/global_variable_builder/fee_provider.d.ts.map +1 -0
  13. package/dest/global_variable_builder/fee_provider.js +80 -0
  14. package/dest/global_variable_builder/global_builder.d.ts +13 -28
  15. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  16. package/dest/global_variable_builder/global_builder.js +9 -66
  17. package/dest/global_variable_builder/index.d.ts +4 -2
  18. package/dest/global_variable_builder/index.d.ts.map +1 -1
  19. package/dest/global_variable_builder/index.js +2 -0
  20. package/dest/publisher/config.d.ts +51 -17
  21. package/dest/publisher/config.d.ts.map +1 -1
  22. package/dest/publisher/config.js +131 -42
  23. package/dest/publisher/index.d.ts +2 -1
  24. package/dest/publisher/index.d.ts.map +1 -1
  25. package/dest/publisher/l1_to_l2_messaging.d.ts +21 -0
  26. package/dest/publisher/l1_to_l2_messaging.d.ts.map +1 -0
  27. package/dest/publisher/l1_to_l2_messaging.js +70 -0
  28. package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
  29. package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
  30. package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
  31. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +98 -0
  32. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
  33. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +68 -0
  34. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
  35. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
  36. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +36 -0
  37. package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
  38. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
  39. package/dest/publisher/l1_tx_failed_store/index.js +3 -0
  40. package/dest/publisher/sequencer-bundle-simulator.d.ts +96 -0
  41. package/dest/publisher/sequencer-bundle-simulator.d.ts.map +1 -0
  42. package/dest/publisher/sequencer-bundle-simulator.js +198 -0
  43. package/dest/publisher/sequencer-publisher-factory.d.ts +11 -5
  44. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  45. package/dest/publisher/sequencer-publisher-factory.js +27 -3
  46. package/dest/publisher/sequencer-publisher.d.ts +122 -67
  47. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  48. package/dest/publisher/sequencer-publisher.js +745 -380
  49. package/dest/publisher/write_json.d.ts +11 -0
  50. package/dest/publisher/write_json.d.ts.map +1 -0
  51. package/dest/publisher/write_json.js +57 -0
  52. package/dest/sequencer/automine/automine_factory.d.ts +56 -0
  53. package/dest/sequencer/automine/automine_factory.d.ts.map +1 -0
  54. package/dest/sequencer/automine/automine_factory.js +85 -0
  55. package/dest/sequencer/automine/automine_sequencer.d.ts +189 -0
  56. package/dest/sequencer/automine/automine_sequencer.d.ts.map +1 -0
  57. package/dest/sequencer/automine/automine_sequencer.js +690 -0
  58. package/dest/sequencer/automine/index.d.ts +3 -0
  59. package/dest/sequencer/automine/index.d.ts.map +1 -0
  60. package/dest/sequencer/automine/index.js +2 -0
  61. package/dest/sequencer/checkpoint_proposal_job.d.ts +81 -19
  62. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  63. package/dest/sequencer/checkpoint_proposal_job.js +891 -282
  64. package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts +34 -0
  65. package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts.map +1 -0
  66. package/dest/sequencer/checkpoint_proposal_job_metrics.js +72 -0
  67. package/dest/sequencer/checkpoint_voter.d.ts +1 -2
  68. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -1
  69. package/dest/sequencer/checkpoint_voter.js +2 -5
  70. package/dest/sequencer/events.d.ts +62 -5
  71. package/dest/sequencer/events.d.ts.map +1 -1
  72. package/dest/sequencer/metrics.d.ts +23 -8
  73. package/dest/sequencer/metrics.d.ts.map +1 -1
  74. package/dest/sequencer/metrics.js +117 -21
  75. package/dest/sequencer/requests_tracker.d.ts +22 -0
  76. package/dest/sequencer/requests_tracker.d.ts.map +1 -0
  77. package/dest/sequencer/requests_tracker.js +33 -0
  78. package/dest/sequencer/sequencer.d.ts +181 -44
  79. package/dest/sequencer/sequencer.d.ts.map +1 -1
  80. package/dest/sequencer/sequencer.js +614 -216
  81. package/dest/sequencer/types.d.ts +2 -2
  82. package/dest/sequencer/types.d.ts.map +1 -1
  83. package/dest/test/index.d.ts +5 -7
  84. package/dest/test/index.d.ts.map +1 -1
  85. package/dest/test/mock_checkpoint_builder.d.ts +11 -11
  86. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
  87. package/dest/test/mock_checkpoint_builder.js +45 -34
  88. package/dest/test/utils.d.ts +17 -3
  89. package/dest/test/utils.d.ts.map +1 -1
  90. package/dest/test/utils.js +28 -9
  91. package/package.json +30 -29
  92. package/src/client/sequencer-client.ts +110 -47
  93. package/src/config.ts +120 -55
  94. package/src/global_variable_builder/README.md +44 -0
  95. package/src/global_variable_builder/fee_predictor.ts +182 -0
  96. package/src/global_variable_builder/fee_provider.ts +97 -0
  97. package/src/global_variable_builder/global_builder.ts +20 -89
  98. package/src/global_variable_builder/index.ts +3 -1
  99. package/src/publisher/config.ts +173 -40
  100. package/src/publisher/index.ts +3 -0
  101. package/src/publisher/l1_to_l2_messaging.ts +85 -0
  102. package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
  103. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +164 -0
  104. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +47 -0
  105. package/src/publisher/l1_tx_failed_store/index.ts +3 -0
  106. package/src/publisher/sequencer-bundle-simulator.ts +254 -0
  107. package/src/publisher/sequencer-publisher-factory.ts +38 -9
  108. package/src/publisher/sequencer-publisher.ts +847 -470
  109. package/src/publisher/write_json.ts +78 -0
  110. package/src/sequencer/README.md +162 -450
  111. package/src/sequencer/automine/README.md +60 -0
  112. package/src/sequencer/automine/automine_factory.ts +152 -0
  113. package/src/sequencer/automine/automine_sequencer.ts +796 -0
  114. package/src/sequencer/automine/index.ts +6 -0
  115. package/src/sequencer/checkpoint_proposal_job.ts +1083 -305
  116. package/src/sequencer/checkpoint_proposal_job_metrics.ts +128 -0
  117. package/src/sequencer/checkpoint_voter.ts +1 -12
  118. package/src/sequencer/events.ts +66 -5
  119. package/src/sequencer/metrics.ts +132 -25
  120. package/src/sequencer/requests_tracker.ts +43 -0
  121. package/src/sequencer/sequencer.ts +728 -241
  122. package/src/sequencer/types.ts +1 -1
  123. package/src/test/index.ts +4 -6
  124. package/src/test/mock_checkpoint_builder.ts +63 -49
  125. package/src/test/utils.ts +64 -10
  126. package/dest/sequencer/timetable.d.ts +0 -90
  127. package/dest/sequencer/timetable.d.ts.map +0 -1
  128. package/dest/sequencer/timetable.js +0 -226
  129. package/src/sequencer/timetable.ts +0 -282
@@ -1,226 +0,0 @@
1
- import { createLogger } from '@aztec/aztec.js/log';
2
- import { DEFAULT_ATTESTATION_PROPAGATION_TIME as DEFAULT_P2P_PROPAGATION_TIME } from '../config.js';
3
- import { SequencerTooSlowError } from './errors.js';
4
- import { SequencerState } from './utils.js';
5
- export const MIN_EXECUTION_TIME = 2;
6
- export const CHECKPOINT_INITIALIZATION_TIME = 1;
7
- export const CHECKPOINT_ASSEMBLE_TIME = 1;
8
- export class SequencerTimetable {
9
- metrics;
10
- log;
11
- /**
12
- * How late into the slot can we be to start working. Computed as the total time needed for assembling and publishing a block,
13
- * assuming an execution time equal to `minExecutionTime`, subtracted from the slot duration. This means that, if the proposer
14
- * starts building at this time, and all times hold, it will have at least `minExecutionTime` to execute txs for the block.
15
- */ initializeDeadline;
16
- /**
17
- * Fixed time offset (in seconds) when the first sub-slot starts, used as baseline for all block deadlines.
18
- * This is an estimate of how long initialization (sync + proposer check) typically takes.
19
- * If actual initialization takes longer/shorter, blocks just get less/more time accordingly.
20
- */ initializationOffset;
21
- /**
22
- * Total time needed to finalize and publish a checkpoint, including:
23
- * - Assembling the checkpoint
24
- * - Round-trip p2p propagation for the checkpoint proposal and its corresponding attestations
25
- * - Publishing to L1
26
- */ checkpointFinalizationTime;
27
- /**
28
- * How long it takes to get a published block into L1. L1 builders typically accept txs up to 4 seconds into their slot,
29
- * but we'll timeout sooner to give it more time to propagate (remember we also have blobs!). Still, when working in anvil,
30
- * we can just post in the very last second of the L1 slot and still expect the tx to be accepted.
31
- */ l1PublishingTime;
32
- /**
33
- * What's the minimum time we want to leave available for execution and reexecution (used to derive init deadline)
34
- * Defaults to half of the block duration if set, otherwise a constant.
35
- */ minExecutionTime;
36
- /** How long it takes to get ready to start building */ checkpointInitializationTime;
37
- /** How long it takes to for proposals and attestations to travel across the p2p layer (one-way) */ p2pPropagationTime;
38
- /** How much time we spend assembling a checkpoint after building the last block */ checkpointAssembleTime;
39
- /** Ethereum slot duration in seconds */ ethereumSlotDuration;
40
- /** Aztec slot duration in seconds (must be multiple of ethereum slot duration) */ aztecSlotDuration;
41
- /** Whether assertTimeLeft will throw if not enough time. */ enforce;
42
- /** Duration per block when building multiple blocks per slot (undefined = single block per slot) */ blockDuration;
43
- /** Maximum number of blocks that can be built in this slot configuration */ maxNumberOfBlocks;
44
- constructor(opts, metrics, log = createLogger('sequencer:timetable')){
45
- this.metrics = metrics;
46
- this.log = log;
47
- this.minExecutionTime = MIN_EXECUTION_TIME;
48
- this.checkpointInitializationTime = CHECKPOINT_INITIALIZATION_TIME;
49
- this.checkpointAssembleTime = CHECKPOINT_ASSEMBLE_TIME;
50
- this.ethereumSlotDuration = opts.ethereumSlotDuration;
51
- this.aztecSlotDuration = opts.aztecSlotDuration;
52
- this.l1PublishingTime = opts.l1PublishingTime;
53
- this.p2pPropagationTime = opts.p2pPropagationTime ?? DEFAULT_P2P_PROPAGATION_TIME;
54
- this.blockDuration = opts.blockDurationMs ? opts.blockDurationMs / 1000 : undefined;
55
- this.enforce = opts.enforce;
56
- // Assume zero-cost propagation time and faster runs in test environments where L1 slot duration is shortened
57
- if (this.ethereumSlotDuration < 8) {
58
- this.p2pPropagationTime = 0;
59
- this.checkpointAssembleTime = 0.5;
60
- this.checkpointInitializationTime = 0.5;
61
- this.minExecutionTime = 1;
62
- }
63
- // Min execution time cannot be less than the block duration if set
64
- if (this.blockDuration !== undefined && this.minExecutionTime > this.blockDuration) {
65
- this.minExecutionTime = this.blockDuration;
66
- }
67
- // Calculate initialization offset - estimate of time needed for sync + proposer check
68
- // This is the baseline for all sub-slot deadlines
69
- this.initializationOffset = this.checkpointInitializationTime;
70
- // Calculate total checkpoint finalization time (assembly + attestations + L1 publishing)
71
- this.checkpointFinalizationTime = this.checkpointAssembleTime + this.p2pPropagationTime * 2 + // Round-trip propagation
72
- this.l1PublishingTime; // L1 publishing
73
- // Calculate maximum number of blocks that fit in this slot
74
- if (!this.blockDuration) {
75
- this.maxNumberOfBlocks = 1; // Single block per slot
76
- } else {
77
- const timeReservedAtEnd = this.blockDuration + // Last sub-slot for validator re-execution
78
- this.checkpointFinalizationTime; // Checkpoint finalization
79
- const timeAvailableForBlocks = this.aztecSlotDuration - this.initializationOffset - timeReservedAtEnd;
80
- this.maxNumberOfBlocks = Math.floor(timeAvailableForBlocks / this.blockDuration);
81
- }
82
- // Minimum work to do within a slot for building a block with the minimum time for execution and publishing its checkpoint
83
- const minWorkToDo = this.initializationOffset + this.minExecutionTime * 2 + // Execution and reexecution
84
- this.checkpointFinalizationTime;
85
- const initializeDeadline = this.aztecSlotDuration - minWorkToDo;
86
- this.initializeDeadline = initializeDeadline;
87
- this.log.verbose(`Sequencer timetable initialized with ${this.maxNumberOfBlocks} blocks per slot (${this.enforce ? 'enforced' : 'not enforced'})`, {
88
- ethereumSlotDuration: this.ethereumSlotDuration,
89
- aztecSlotDuration: this.aztecSlotDuration,
90
- l1PublishingTime: this.l1PublishingTime,
91
- minExecutionTime: this.minExecutionTime,
92
- blockPrepareTime: this.checkpointInitializationTime,
93
- p2pPropagationTime: this.p2pPropagationTime,
94
- blockAssembleTime: this.checkpointAssembleTime,
95
- initializeDeadline: this.initializeDeadline,
96
- enforce: this.enforce,
97
- minWorkToDo,
98
- blockDuration: this.blockDuration,
99
- maxNumberOfBlocks: this.maxNumberOfBlocks
100
- });
101
- if (initializeDeadline <= 0) {
102
- throw new Error(`Block proposal initialize deadline cannot be negative (got ${initializeDeadline} from total time needed ${minWorkToDo} and a slot duration of ${this.aztecSlotDuration}).`);
103
- }
104
- }
105
- getMaxAllowedTime(state) {
106
- switch(state){
107
- case SequencerState.STOPPED:
108
- case SequencerState.STOPPING:
109
- case SequencerState.IDLE:
110
- case SequencerState.SYNCHRONIZING:
111
- return; // We don't really care about times for this states
112
- case SequencerState.PROPOSER_CHECK:
113
- case SequencerState.INITIALIZING_CHECKPOINT:
114
- return this.initializeDeadline;
115
- case SequencerState.WAITING_FOR_TXS:
116
- case SequencerState.CREATING_BLOCK:
117
- case SequencerState.WAITING_UNTIL_NEXT_BLOCK:
118
- return this.initializeDeadline + this.checkpointInitializationTime;
119
- case SequencerState.ASSEMBLING_CHECKPOINT:
120
- case SequencerState.COLLECTING_ATTESTATIONS:
121
- return this.aztecSlotDuration - this.l1PublishingTime - 2 * this.p2pPropagationTime;
122
- case SequencerState.PUBLISHING_CHECKPOINT:
123
- return this.aztecSlotDuration - this.l1PublishingTime;
124
- default:
125
- {
126
- const _exhaustiveCheck = state;
127
- throw new Error(`Unexpected state: ${state}`);
128
- }
129
- }
130
- }
131
- assertTimeLeft(newState, secondsIntoSlot) {
132
- if (!this.enforce) {
133
- return;
134
- }
135
- const maxAllowedTime = this.getMaxAllowedTime(newState);
136
- if (maxAllowedTime === undefined) {
137
- return;
138
- }
139
- const bufferSeconds = maxAllowedTime - secondsIntoSlot;
140
- if (bufferSeconds < 0) {
141
- throw new SequencerTooSlowError(newState, maxAllowedTime, secondsIntoSlot);
142
- }
143
- this.metrics?.recordStateTransitionBufferMs(Math.floor(bufferSeconds * 1000), newState);
144
- this.log.trace(`Enough time to transition to ${newState}`, {
145
- maxAllowedTime,
146
- secondsIntoSlot
147
- });
148
- }
149
- /**
150
- * Determines if we can start building the next block and returns its deadline.
151
- *
152
- * The timetable divides the slot into fixed sub-slots. This method finds the next
153
- * available sub-slot that has enough time remaining to build a block.
154
- *
155
- * @param secondsIntoSlot - Current time (seconds into the slot)
156
- * @returns Object with canStart flag, deadline, and whether this is the last block
157
- */ canStartNextBlock(secondsIntoSlot) {
158
- // When timetable enforcement is disabled, always allow starting,
159
- // and build a single block with no deadline. This is here just to
160
- // satisfy a subset of e2e tests and the sandbox that assume that the
161
- // sequencer is permanently mining every tx sent.
162
- if (!this.enforce) {
163
- return {
164
- canStart: true,
165
- deadline: undefined,
166
- isLastBlock: true
167
- };
168
- }
169
- // If no block duration is set, then we're in single-block mode, which
170
- // is handled for backwards compatibility towards another subset of e2e tests.
171
- if (this.blockDuration === undefined) {
172
- // In single block mode, execution and re-execution happen sequentially, so we need to
173
- // split the available time between them. After building, we need time for attestations and L1.
174
- const maxAllowed = this.aztecSlotDuration - this.checkpointFinalizationTime;
175
- const available = (maxAllowed - secondsIntoSlot) / 2; // Split remaining time: half for execution, half for re-execution
176
- const canStart = available >= this.minExecutionTime;
177
- const deadline = secondsIntoSlot + available;
178
- this.log.verbose(`${canStart ? 'Can' : 'Cannot'} start single-block checkpoint at ${secondsIntoSlot}s into slot`, {
179
- secondsIntoSlot,
180
- maxAllowed,
181
- available,
182
- deadline
183
- });
184
- return {
185
- canStart,
186
- deadline,
187
- isLastBlock: true
188
- };
189
- }
190
- // Otherwise, we're in multi-block-per-slot mode, the default when running in production
191
- // Find the next available sub-slot that has enough time remaining
192
- for(let subSlot = 1; subSlot <= this.maxNumberOfBlocks; subSlot++){
193
- // Calculate end for this sub-slot
194
- const deadline = this.initializationOffset + subSlot * this.blockDuration;
195
- // Check if we have enough time to build a block with this deadline
196
- const timeUntilDeadline = deadline - secondsIntoSlot;
197
- if (timeUntilDeadline >= this.minExecutionTime) {
198
- // Found an available sub-slot! Is this the last one?
199
- const isLastBlock = subSlot === this.maxNumberOfBlocks;
200
- this.log.verbose(`Can start ${isLastBlock ? 'last block' : 'block'} in sub-slot ${subSlot} with deadline ${deadline}s`, {
201
- secondsIntoSlot,
202
- deadline,
203
- timeUntilDeadline,
204
- subSlot,
205
- maxBlocks: this.maxNumberOfBlocks
206
- });
207
- return {
208
- canStart: true,
209
- deadline,
210
- isLastBlock
211
- };
212
- }
213
- }
214
- // No sub-slots available with enough time
215
- this.log.verbose(`No time left to start any more blocks`, {
216
- secondsIntoSlot,
217
- maxBlocks: this.maxNumberOfBlocks,
218
- initializationOffset: this.initializationOffset
219
- });
220
- return {
221
- canStart: false,
222
- deadline: undefined,
223
- isLastBlock: false
224
- };
225
- }
226
- }
@@ -1,282 +0,0 @@
1
- import { createLogger } from '@aztec/aztec.js/log';
2
-
3
- import { DEFAULT_ATTESTATION_PROPAGATION_TIME as DEFAULT_P2P_PROPAGATION_TIME } from '../config.js';
4
- import { SequencerTooSlowError } from './errors.js';
5
- import type { SequencerMetrics } from './metrics.js';
6
- import { SequencerState } from './utils.js';
7
-
8
- export const MIN_EXECUTION_TIME = 2;
9
- export const CHECKPOINT_INITIALIZATION_TIME = 1;
10
- export const CHECKPOINT_ASSEMBLE_TIME = 1;
11
-
12
- export class SequencerTimetable {
13
- /**
14
- * How late into the slot can we be to start working. Computed as the total time needed for assembling and publishing a block,
15
- * assuming an execution time equal to `minExecutionTime`, subtracted from the slot duration. This means that, if the proposer
16
- * starts building at this time, and all times hold, it will have at least `minExecutionTime` to execute txs for the block.
17
- */
18
- public readonly initializeDeadline: number;
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
-
35
- /**
36
- * How long it takes to get a published block into L1. L1 builders typically accept txs up to 4 seconds into their slot,
37
- * but we'll timeout sooner to give it more time to propagate (remember we also have blobs!). Still, when working in anvil,
38
- * we can just post in the very last second of the L1 slot and still expect the tx to be accepted.
39
- */
40
- public readonly l1PublishingTime: number;
41
-
42
- /**
43
- * What's the minimum time we want to leave available for execution and reexecution (used to derive init deadline)
44
- * Defaults to half of the block duration if set, otherwise a constant.
45
- */
46
- public readonly minExecutionTime: number = MIN_EXECUTION_TIME;
47
-
48
- /** How long it takes to get ready to start building */
49
- public readonly checkpointInitializationTime: number = CHECKPOINT_INITIALIZATION_TIME;
50
-
51
- /** How long it takes to for proposals and attestations to travel across the p2p layer (one-way) */
52
- public readonly p2pPropagationTime: number;
53
-
54
- /** How much time we spend assembling a checkpoint after building the last block */
55
- public readonly checkpointAssembleTime: number = CHECKPOINT_ASSEMBLE_TIME;
56
-
57
- /** Ethereum slot duration in seconds */
58
- public readonly ethereumSlotDuration: number;
59
-
60
- /** Aztec slot duration in seconds (must be multiple of ethereum slot duration) */
61
- public readonly aztecSlotDuration: number;
62
-
63
- /** Whether assertTimeLeft will throw if not enough time. */
64
- public readonly enforce: boolean;
65
-
66
- /** Duration per block when building multiple blocks per slot (undefined = single block per slot) */
67
- public readonly blockDuration: number | undefined;
68
-
69
- /** Maximum number of blocks that can be built in this slot configuration */
70
- public readonly maxNumberOfBlocks: number;
71
-
72
- constructor(
73
- opts: {
74
- ethereumSlotDuration: number;
75
- aztecSlotDuration: number;
76
- l1PublishingTime: number;
77
- p2pPropagationTime?: number;
78
- blockDurationMs?: number;
79
- enforce: boolean;
80
- },
81
- private readonly metrics?: SequencerMetrics,
82
- private readonly log = createLogger('sequencer:timetable'),
83
- ) {
84
- this.ethereumSlotDuration = opts.ethereumSlotDuration;
85
- this.aztecSlotDuration = opts.aztecSlotDuration;
86
- this.l1PublishingTime = opts.l1PublishingTime;
87
- this.p2pPropagationTime = opts.p2pPropagationTime ?? DEFAULT_P2P_PROPAGATION_TIME;
88
- this.blockDuration = opts.blockDurationMs ? opts.blockDurationMs / 1000 : undefined;
89
- this.enforce = opts.enforce;
90
-
91
- // Assume zero-cost propagation time and faster runs in test environments where L1 slot duration is shortened
92
- if (this.ethereumSlotDuration < 8) {
93
- this.p2pPropagationTime = 0;
94
- this.checkpointAssembleTime = 0.5;
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);
123
- }
124
-
125
- // Minimum work to do within a slot for building a block with the minimum time for execution and publishing its checkpoint
126
- const minWorkToDo =
127
- this.initializationOffset +
128
- this.minExecutionTime * 2 + // Execution and reexecution
129
- this.checkpointFinalizationTime;
130
-
131
- const initializeDeadline = this.aztecSlotDuration - minWorkToDo;
132
- this.initializeDeadline = initializeDeadline;
133
-
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
- );
151
-
152
- if (initializeDeadline <= 0) {
153
- throw new Error(
154
- `Block proposal initialize deadline cannot be negative (got ${initializeDeadline} from total time needed ${minWorkToDo} and a slot duration of ${this.aztecSlotDuration}).`,
155
- );
156
- }
157
- }
158
-
159
- public getMaxAllowedTime(
160
- state: Extract<SequencerState, SequencerState.STOPPED | SequencerState.IDLE | SequencerState.SYNCHRONIZING>,
161
- ): undefined;
162
- public getMaxAllowedTime(
163
- state: Exclude<SequencerState, SequencerState.STOPPED | SequencerState.IDLE | SequencerState.SYNCHRONIZING>,
164
- ): number;
165
- public getMaxAllowedTime(state: SequencerState): number | undefined;
166
- public getMaxAllowedTime(state: SequencerState): number | undefined {
167
- switch (state) {
168
- case SequencerState.STOPPED:
169
- case SequencerState.STOPPING:
170
- case SequencerState.IDLE:
171
- case SequencerState.SYNCHRONIZING:
172
- return; // We don't really care about times for this states
173
- case SequencerState.PROPOSER_CHECK:
174
- case SequencerState.INITIALIZING_CHECKPOINT:
175
- return this.initializeDeadline;
176
- case SequencerState.WAITING_FOR_TXS:
177
- case SequencerState.CREATING_BLOCK:
178
- case SequencerState.WAITING_UNTIL_NEXT_BLOCK:
179
- return this.initializeDeadline + this.checkpointInitializationTime;
180
- case SequencerState.ASSEMBLING_CHECKPOINT:
181
- case SequencerState.COLLECTING_ATTESTATIONS:
182
- return this.aztecSlotDuration - this.l1PublishingTime - 2 * this.p2pPropagationTime;
183
- case SequencerState.PUBLISHING_CHECKPOINT:
184
- return this.aztecSlotDuration - this.l1PublishingTime;
185
- default: {
186
- const _exhaustiveCheck: never = state;
187
- throw new Error(`Unexpected state: ${state}`);
188
- }
189
- }
190
- }
191
-
192
- public assertTimeLeft(newState: SequencerState, secondsIntoSlot: number) {
193
- if (!this.enforce) {
194
- return;
195
- }
196
-
197
- const maxAllowedTime = this.getMaxAllowedTime(newState);
198
- if (maxAllowedTime === undefined) {
199
- return;
200
- }
201
-
202
- const bufferSeconds = maxAllowedTime - secondsIntoSlot;
203
- if (bufferSeconds < 0) {
204
- throw new SequencerTooSlowError(newState, maxAllowedTime, secondsIntoSlot);
205
- }
206
-
207
- this.metrics?.recordStateTransitionBufferMs(Math.floor(bufferSeconds * 1000), newState);
208
- this.log.trace(`Enough time to transition to ${newState}`, { maxAllowedTime, secondsIntoSlot });
209
- }
210
-
211
- /**
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
219
- */
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.
236
- if (this.blockDuration === undefined) {
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
- }
271
- }
272
-
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 };
281
- }
282
- }