@aztec/sequencer-client 0.0.1-commit.993d240 → 0.0.1-commit.9a89641

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 (106) hide show
  1. package/README.md +40 -41
  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 +18 -14
  5. package/dest/config.d.ts +9 -4
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +29 -15
  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 +39 -10
  36. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  37. package/dest/publisher/sequencer-publisher.js +403 -75
  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 +209 -19
  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 +28 -15
  51. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  52. package/dest/sequencer/checkpoint_proposal_job.js +158 -117
  53. package/dest/sequencer/events.d.ts +16 -4
  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/missing_committee.d.ts +72 -0
  59. package/dest/sequencer/missing_committee.d.ts.map +1 -0
  60. package/dest/sequencer/missing_committee.js +139 -0
  61. package/dest/sequencer/requests_tracker.d.ts +22 -0
  62. package/dest/sequencer/requests_tracker.d.ts.map +1 -0
  63. package/dest/sequencer/requests_tracker.js +33 -0
  64. package/dest/sequencer/sequencer.d.ts +113 -28
  65. package/dest/sequencer/sequencer.d.ts.map +1 -1
  66. package/dest/sequencer/sequencer.js +352 -162
  67. package/dest/test/index.d.ts +3 -3
  68. package/dest/test/index.d.ts.map +1 -1
  69. package/dest/test/utils.d.ts +15 -1
  70. package/dest/test/utils.d.ts.map +1 -1
  71. package/dest/test/utils.js +18 -1
  72. package/package.json +29 -28
  73. package/src/client/sequencer-client.ts +20 -14
  74. package/src/config.ts +32 -16
  75. package/src/global_variable_builder/fee_predictor.ts +11 -1
  76. package/src/global_variable_builder/fee_provider.ts +27 -5
  77. package/src/global_variable_builder/global_builder.ts +2 -34
  78. package/src/index.ts +1 -10
  79. package/src/publisher/config.ts +22 -1
  80. package/src/publisher/l1_to_l2_messaging.ts +85 -0
  81. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +114 -7
  82. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +4 -3
  83. package/src/publisher/l1_tx_failed_store/index.ts +1 -1
  84. package/src/publisher/sequencer-publisher.ts +430 -86
  85. package/src/publisher/write_json.ts +78 -0
  86. package/src/sequencer/README.md +16 -6
  87. package/src/sequencer/automine/README.md +18 -4
  88. package/src/sequencer/automine/automine_factory.ts +8 -1
  89. package/src/sequencer/automine/automine_sequencer.ts +229 -21
  90. package/src/sequencer/automine/index.ts +6 -0
  91. package/src/sequencer/checkpoint_proposal_job.ts +177 -132
  92. package/src/sequencer/events.ts +16 -3
  93. package/src/sequencer/index.ts +0 -2
  94. package/src/sequencer/missing_committee.ts +192 -0
  95. package/src/sequencer/requests_tracker.ts +43 -0
  96. package/src/sequencer/sequencer.ts +382 -176
  97. package/src/test/index.ts +2 -2
  98. package/src/test/utils.ts +33 -0
  99. package/dest/sequencer/chain_state_overrides.d.ts +0 -61
  100. package/dest/sequencer/chain_state_overrides.d.ts.map +0 -1
  101. package/dest/sequencer/chain_state_overrides.js +0 -98
  102. package/dest/sequencer/timetable.d.ts +0 -101
  103. package/dest/sequencer/timetable.d.ts.map +0 -1
  104. package/dest/sequencer/timetable.js +0 -231
  105. package/src/sequencer/chain_state_overrides.ts +0 -162
  106. package/src/sequencer/timetable.ts +0 -295
@@ -5,11 +5,24 @@ import type { Action } from '../publisher/sequencer-publisher.js';
5
5
  import type { SequencerState } from './utils.js';
6
6
 
7
7
  export type SequencerEvents = {
8
+ /**
9
+ * Emitted on every sequencer state transition (including no-op transitions to the same state). The
10
+ * timing fields are anchored to the build frame of the slot being proposed for, not to wall-clock
11
+ * slot boundaries, because the proposer builds for `targetSlot` during the previous (build) slot.
12
+ *
13
+ * - `oldState` / `newState` are the previous and new {@link SequencerState}.
14
+ * - `secondsIntoBuildFrame` is the wall-clock seconds elapsed since the build-frame start of
15
+ * `targetSlot` (`now − getBuildFrameStart(targetSlot)`). Undefined for lifecycle states with no
16
+ * associated slot (e.g. IDLE/STOPPING). It can be negative if the transition happens before the
17
+ * build frame opens.
18
+ * - `targetSlot` is the slot the checkpoint is being proposed for (the submission slot, one ahead of
19
+ * the wall-clock build slot under pipelining). Undefined for lifecycle states with no slot.
20
+ */
8
21
  ['state-changed']: (args: {
9
22
  oldState: SequencerState;
10
23
  newState: SequencerState;
11
- secondsIntoSlot?: number;
12
- slot?: SlotNumber;
24
+ secondsIntoBuildFrame?: number;
25
+ targetSlot?: SlotNumber;
13
26
  }) => void;
14
27
  /**
15
28
  * Emitted by the sequencer once it has decided it is going to attempt to build a
@@ -36,7 +49,7 @@ export type SequencerEvents = {
36
49
  simulatedPending: CheckpointNumber | undefined;
37
50
  }) => void;
38
51
  ['proposer-rollup-check-failed']: (args: { reason: string; slot: SlotNumber }) => void;
39
- ['block-tx-count-check-failed']: (args: { minTxs: number; availableTxs: number; slot: SlotNumber }) => void;
52
+ ['block-tx-count-check-failed']: (args: { minTxs: number; slot: SlotNumber }) => void;
40
53
  ['block-build-failed']: (args: { reason: string; slot: SlotNumber }) => void;
41
54
  ['block-proposed']: (args: {
42
55
  blockNumber: BlockNumber;
@@ -1,5 +1,3 @@
1
- export * from './automine/automine_factory.js';
2
- export * from './automine/automine_sequencer.js';
3
1
  export * from './checkpoint_proposal_job.js';
4
2
  export * from './checkpoint_voter.js';
5
3
  export * from './config.js';
@@ -0,0 +1,192 @@
1
+ import type { EpochCache } from '@aztec/epoch-cache';
2
+ import type { RollupContract } from '@aztec/ethereum/contracts';
3
+ import { EpochNumber, type SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { timesParallel } from '@aztec/foundation/collection';
5
+ import type { Logger } from '@aztec/foundation/log';
6
+ import { formatSeconds } from '@aztec/foundation/string';
7
+ import type { DateProvider } from '@aztec/foundation/timer';
8
+ import type { L2BlockSource } from '@aztec/stdlib/block';
9
+ import { type L1RollupConstants, getStartTimestampForEpoch } from '@aztec/stdlib/epoch-helpers';
10
+
11
+ /** Collaborators {@link logMissingCommittee} needs to diagnose and report why no committee exists. */
12
+ export interface MissingCommitteeContext {
13
+ epochCache: Pick<EpochCache, 'getL1Constants' | 'getLagInEpochsForValidatorSet'>;
14
+ rollupContract: Pick<RollupContract, 'getActiveAttesterCount' | 'getAttesterCountAtTime'>;
15
+ l2BlockSource: Pick<L2BlockSource, 'getBlockNumber'>;
16
+ l1Constants: Pick<L1RollupConstants, 'l1GenesisTime' | 'slotDuration' | 'epochDuration'>;
17
+ dateProvider: Pick<DateProvider, 'nowInSeconds'>;
18
+ logger: Pick<Logger, 'info' | 'warn' | 'debug'>;
19
+ }
20
+
21
+ /**
22
+ * Why the sequencer cannot find a validator committee for an upcoming slot. A committee for an epoch is
23
+ * sampled from the validator set as of `lagInEpochsForValidatorSet` epochs earlier, and L1 refuses to
24
+ * produce one until at least `targetCommitteeSize` validators were staked at that sampling time.
25
+ */
26
+ export type MissingCommitteeCause =
27
+ /** Enough validators are staked now; the committee is just waiting for the sampling window to advance. */
28
+ | 'awaiting-sampling-lag'
29
+ /** The chain has not produced any block yet and too few validators are staked: normal bootstrap state. */
30
+ | 'awaiting-first-validators'
31
+ /** The chain has produced blocks before but the validator set has since dropped below the required size. */
32
+ | 'validator-set-shrank';
33
+
34
+ export interface MissingCommitteeDiagnosis {
35
+ cause: MissingCommitteeCause;
36
+ /** Log severity: bootstrap and lag are expected (`info`); a shrunken set on a live chain is not (`warn`). */
37
+ severity: 'info' | 'warn';
38
+ }
39
+
40
+ /**
41
+ * Logs why no validator committee exists for the given slot. Rather than the cryptic "committee does not
42
+ * exist on L1", it diagnoses the cause from the live attester count and whether the chain has ever produced
43
+ * a block: a bootstrapping chain waiting for validators to stake, a full set still waiting for the sampling
44
+ * lag to elapse, or a validator set that has shrunk below the required size on a live chain. Only the last is
45
+ * a genuine problem, so only it is logged at `warn`.
46
+ *
47
+ * Best-effort: if the diagnostic L1 reads fail we fall back to a neutral message so this never throws on the
48
+ * propose path.
49
+ */
50
+ export async function logMissingCommittee(
51
+ targetSlot: SlotNumber,
52
+ targetEpoch: EpochNumber,
53
+ ctx: MissingCommitteeContext,
54
+ ): Promise<void> {
55
+ const targetCommitteeSize = ctx.epochCache.getL1Constants().targetCommitteeSize;
56
+ const lag = ctx.epochCache.getLagInEpochsForValidatorSet();
57
+
58
+ let attesterCount: number;
59
+ let hasProducedBlocks: boolean;
60
+ try {
61
+ [attesterCount, hasProducedBlocks] = await Promise.all([
62
+ ctx.rollupContract.getActiveAttesterCount(),
63
+ ctx.l2BlockSource.getBlockNumber().then(n => n > 0),
64
+ ]);
65
+ } catch (err) {
66
+ ctx.logger.warn(`No committee found for slot ${targetSlot}; could not determine validator set size`, {
67
+ targetSlot,
68
+ targetEpoch,
69
+ targetCommitteeSize,
70
+ err,
71
+ });
72
+ return;
73
+ }
74
+
75
+ const diagnosis = classifyMissingCommittee({ attesterCount, targetCommitteeSize, hasProducedBlocks });
76
+ const logCtx = { targetSlot, targetEpoch, attesterCount, targetCommitteeSize, cause: diagnosis.cause };
77
+
78
+ switch (diagnosis.cause) {
79
+ case 'awaiting-sampling-lag': {
80
+ const firstCommitteeEpoch = await estimateFirstCommitteeEpoch(targetEpoch, lag, targetCommitteeSize, ctx);
81
+ const staked = `${attesterCount} validators are staked (>= ${targetCommitteeSize} required)`;
82
+ const forming = `the committee is still forming as the ${lag}-epoch sampling window advances`;
83
+ // When we could not pin the exact epoch, report the safe upper bound rather than a misleading ETA.
84
+ const epoch = firstCommitteeEpoch ?? EpochNumber(targetEpoch + lag + 1);
85
+ const expectation =
86
+ firstCommitteeEpoch === undefined
87
+ ? `A committee should exist no later than epoch ${epoch}.`
88
+ : `The first committee is expected at epoch ${epoch}${formatEpochEta(epoch, ctx)}.`;
89
+ ctx.logger.info(`No committee for slot ${targetSlot} yet: ${staked}, ${forming}. ${expectation}`, {
90
+ ...logCtx,
91
+ firstCommitteeEpoch: epoch,
92
+ });
93
+ break;
94
+ }
95
+ case 'awaiting-first-validators':
96
+ ctx.logger.info(
97
+ `No committee for slot ${targetSlot}: the chain has not started producing blocks and only ${attesterCount} ` +
98
+ `of the ${targetCommitteeSize} required validators are staked. Block production begins once at least ` +
99
+ `${targetCommitteeSize} validators stake and the ${lag}-epoch sampling lag elapses.`,
100
+ logCtx,
101
+ );
102
+ break;
103
+ case 'validator-set-shrank':
104
+ ctx.logger.warn(
105
+ `No committee for slot ${targetSlot}: the validator set has dropped to ${attesterCount}, below the ` +
106
+ `${targetCommitteeSize} required to form a committee. The chain cannot progress until enough validators ` +
107
+ `are staked again — check for validators exiting or being slashed.`,
108
+ logCtx,
109
+ );
110
+ break;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Classifies why no committee exists for a slot, using only cheap live signals: the current attester count
116
+ * and whether the chain has ever produced a block. See {@link MissingCommitteeCause} for the reasoning.
117
+ */
118
+ export function classifyMissingCommittee(args: {
119
+ attesterCount: number;
120
+ targetCommitteeSize: number;
121
+ hasProducedBlocks: boolean;
122
+ }): MissingCommitteeDiagnosis {
123
+ const { attesterCount, targetCommitteeSize, hasProducedBlocks } = args;
124
+
125
+ if (attesterCount >= targetCommitteeSize) {
126
+ return { cause: 'awaiting-sampling-lag', severity: 'info' };
127
+ }
128
+
129
+ if (hasProducedBlocks) {
130
+ return { cause: 'validator-set-shrank', severity: 'warn' };
131
+ }
132
+
133
+ return { cause: 'awaiting-first-validators', severity: 'info' };
134
+ }
135
+
136
+ /**
137
+ * Given each candidate epoch after the one we failed to propose at, paired with the attester count staked at
138
+ * that epoch's validator-set sample time (`undefined` when the sample time is still in the future), returns
139
+ * the earliest epoch that will have a committee, or `undefined` if none qualifies.
140
+ *
141
+ * Candidates must be ordered ascending by epoch. A committee for epoch `E` exists iff at least
142
+ * `targetCommitteeSize` validators were staked at `E`'s sample time, so the first candidate whose sampled
143
+ * count meets the target is the answer; a candidate whose sample time is still in the future is assumed to
144
+ * have a committee if the set holds and wins as soon as it is reached. The predicate is not monotone in `E`
145
+ * (the set can dip below target between sample times), so this must scan ascending rather than binary-search.
146
+ */
147
+ export function findFirstEpochWithCommittee(args: {
148
+ candidates: { epoch: EpochNumber; sampledAttesterCount: number | undefined }[];
149
+ targetCommitteeSize: number;
150
+ }): EpochNumber | undefined {
151
+ return args.candidates.find(
152
+ c => c.sampledAttesterCount === undefined || c.sampledAttesterCount >= args.targetCommitteeSize,
153
+ )?.epoch;
154
+ }
155
+
156
+ /**
157
+ * Earliest epoch expected to have a committee, replaying L1's validator-set sampling rule: a committee for
158
+ * epoch `E` exists iff at least `targetCommitteeSize` attesters were staked at `E`'s sample time
159
+ * (`epochStart(E) - lag * epochDuration`). Only epochs after the one we failed at can qualify, and the set
160
+ * must have crossed the target within the last `lag` epochs' sample window, so the answer lies in
161
+ * `(targetEpoch, targetEpoch + lag + 1]`. Reads the historical attester count at each candidate's sample time
162
+ * (at most `lag` on-chain reads; future sample times are assumed to hold). Returns `undefined` if no candidate
163
+ * qualifies or the reads fail, leaving the caller to report a bound instead of an ETA.
164
+ */
165
+ async function estimateFirstCommitteeEpoch(
166
+ targetEpoch: EpochNumber,
167
+ lag: number,
168
+ targetCommitteeSize: number,
169
+ ctx: MissingCommitteeContext,
170
+ ): Promise<EpochNumber | undefined> {
171
+ try {
172
+ const epochDurationSeconds = BigInt(ctx.l1Constants.epochDuration * ctx.l1Constants.slotDuration);
173
+ const nowSeconds = BigInt(ctx.dateProvider.nowInSeconds());
174
+ const candidates = await timesParallel(lag + 1, async i => {
175
+ const epoch = EpochNumber(targetEpoch + 1 + i);
176
+ const sampleTime = getStartTimestampForEpoch(epoch, ctx.l1Constants) - epochDurationSeconds * BigInt(lag);
177
+ const sampledAttesterCount =
178
+ sampleTime > nowSeconds ? undefined : await ctx.rollupContract.getAttesterCountAtTime(sampleTime);
179
+ return { epoch, sampledAttesterCount };
180
+ });
181
+ return findFirstEpochWithCommittee({ candidates, targetCommitteeSize });
182
+ } catch (err) {
183
+ ctx.logger.debug(`Could not estimate first committee epoch after epoch ${targetEpoch}`, { targetEpoch, err });
184
+ return undefined;
185
+ }
186
+ }
187
+
188
+ /** Formats the ETA to the start of the given epoch as ` (~12m from now)`, or empty if it is in the past. */
189
+ function formatEpochEta(epoch: EpochNumber, ctx: MissingCommitteeContext): string {
190
+ const secondsUntil = Number(getStartTimestampForEpoch(epoch, ctx.l1Constants)) - ctx.dateProvider.nowInSeconds();
191
+ return secondsUntil > 0 ? ` (~${formatSeconds(secondsUntil)} from now)` : '';
192
+ }
@@ -0,0 +1,43 @@
1
+ /** A tracked request: the awaitable to wait on, plus an optional interrupt to cancel it early. */
2
+ type TrackedRequest = { promise: Promise<unknown>; interrupt?: () => void };
3
+
4
+ /**
5
+ * Tracks fire-and-forget requests so a shutdown path can interrupt any in-flight work and await it to
6
+ * settle. Each tracked request is a promise plus an optional interrupt callback; the entry removes itself
7
+ * from the bag once its promise settles, so the tracker only ever holds requests that are still in flight.
8
+ */
9
+ export class RequestsTracker {
10
+ private readonly requests = new Map<number, TrackedRequest>();
11
+ private nextId = 0;
12
+
13
+ /** Number of in-flight requests currently tracked. */
14
+ public get size(): number {
15
+ return this.requests.size;
16
+ }
17
+
18
+ /**
19
+ * Tracks a fire-and-forget request. `interrupt`, when provided, is invoked by {@link interruptRequests}
20
+ * to cancel the in-flight work so its promise settles promptly. The entry auto-removes once `promise`
21
+ * settles, whether it resolves or rejects.
22
+ */
23
+ public trackRequest(promise: Promise<unknown>, interrupt?: () => void): void {
24
+ const id = this.nextId++;
25
+ const settled = promise.then(
26
+ () => this.requests.delete(id),
27
+ () => this.requests.delete(id),
28
+ );
29
+ this.requests.set(id, { promise: settled, interrupt });
30
+ }
31
+
32
+ /** Interrupts every in-flight request that was tracked with an interrupt callback. */
33
+ public interruptRequests(): void {
34
+ for (const { interrupt } of this.requests.values()) {
35
+ interrupt?.();
36
+ }
37
+ }
38
+
39
+ /** Awaits every currently in-flight request to settle. */
40
+ public async awaitRequests(): Promise<void> {
41
+ await Promise.all([...this.requests.values()].map(request => request.promise));
42
+ }
43
+ }