@aztec-labs/aztec-node 6.0.0-nightly.20260829

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 (83) hide show
  1. package/README.md +15 -0
  2. package/dest/aztec-node/block_response_helpers.d.ts +25 -0
  3. package/dest/aztec-node/block_response_helpers.d.ts.map +1 -0
  4. package/dest/aztec-node/block_response_helpers.js +112 -0
  5. package/dest/aztec-node/config.d.ts +52 -0
  6. package/dest/aztec-node/config.d.ts.map +1 -0
  7. package/dest/aztec-node/config.js +129 -0
  8. package/dest/aztec-node/node_metrics.d.ts +12 -0
  9. package/dest/aztec-node/node_metrics.d.ts.map +1 -0
  10. package/dest/aztec-node/node_metrics.js +36 -0
  11. package/dest/aztec-node/node_public_calls_simulator.d.ts +108 -0
  12. package/dest/aztec-node/node_public_calls_simulator.d.ts.map +1 -0
  13. package/dest/aztec-node/node_public_calls_simulator.js +380 -0
  14. package/dest/aztec-node/public_data_overrides.d.ts +13 -0
  15. package/dest/aztec-node/public_data_overrides.d.ts.map +1 -0
  16. package/dest/aztec-node/public_data_overrides.js +21 -0
  17. package/dest/aztec-node/register_node_rpc_handlers.d.ts +11 -0
  18. package/dest/aztec-node/register_node_rpc_handlers.d.ts.map +1 -0
  19. package/dest/aztec-node/register_node_rpc_handlers.js +48 -0
  20. package/dest/aztec-node/server.d.ts +281 -0
  21. package/dest/aztec-node/server.d.ts.map +1 -0
  22. package/dest/aztec-node/server.js +1235 -0
  23. package/dest/bin/index.d.ts +3 -0
  24. package/dest/bin/index.d.ts.map +1 -0
  25. package/dest/bin/index.js +57 -0
  26. package/dest/factory.d.ts +33 -0
  27. package/dest/factory.d.ts.map +1 -0
  28. package/dest/factory.js +539 -0
  29. package/dest/index.d.ts +5 -0
  30. package/dest/index.d.ts.map +1 -0
  31. package/dest/index.js +4 -0
  32. package/dest/modules/block_parameter.d.ts +25 -0
  33. package/dest/modules/block_parameter.d.ts.map +1 -0
  34. package/dest/modules/block_parameter.js +100 -0
  35. package/dest/modules/node_block_provider.d.ts +19 -0
  36. package/dest/modules/node_block_provider.d.ts.map +1 -0
  37. package/dest/modules/node_block_provider.js +112 -0
  38. package/dest/modules/node_tx_receipt.d.ts +24 -0
  39. package/dest/modules/node_tx_receipt.d.ts.map +1 -0
  40. package/dest/modules/node_tx_receipt.js +70 -0
  41. package/dest/modules/node_world_state_queries.d.ts +65 -0
  42. package/dest/modules/node_world_state_queries.d.ts.map +1 -0
  43. package/dest/modules/node_world_state_queries.js +270 -0
  44. package/dest/sentinel/config.d.ts +9 -0
  45. package/dest/sentinel/config.d.ts.map +1 -0
  46. package/dest/sentinel/config.js +39 -0
  47. package/dest/sentinel/factory.d.ts +11 -0
  48. package/dest/sentinel/factory.d.ts.map +1 -0
  49. package/dest/sentinel/factory.js +24 -0
  50. package/dest/sentinel/index.d.ts +3 -0
  51. package/dest/sentinel/index.d.ts.map +1 -0
  52. package/dest/sentinel/index.js +1 -0
  53. package/dest/sentinel/sentinel.d.ts +217 -0
  54. package/dest/sentinel/sentinel.d.ts.map +1 -0
  55. package/dest/sentinel/sentinel.js +551 -0
  56. package/dest/sentinel/store.d.ts +35 -0
  57. package/dest/sentinel/store.d.ts.map +1 -0
  58. package/dest/sentinel/store.js +182 -0
  59. package/dest/test/index.d.ts +31 -0
  60. package/dest/test/index.d.ts.map +1 -0
  61. package/dest/test/index.js +1 -0
  62. package/package.json +118 -0
  63. package/src/aztec-node/block_response_helpers.ts +161 -0
  64. package/src/aztec-node/config.ts +216 -0
  65. package/src/aztec-node/node_metrics.ts +49 -0
  66. package/src/aztec-node/node_public_calls_simulator.ts +437 -0
  67. package/src/aztec-node/public_data_overrides.ts +35 -0
  68. package/src/aztec-node/register_node_rpc_handlers.ts +45 -0
  69. package/src/aztec-node/server.ts +1155 -0
  70. package/src/bin/index.ts +77 -0
  71. package/src/factory.ts +704 -0
  72. package/src/index.ts +4 -0
  73. package/src/modules/block_parameter.ts +93 -0
  74. package/src/modules/node_block_provider.ts +149 -0
  75. package/src/modules/node_tx_receipt.ts +115 -0
  76. package/src/modules/node_world_state_queries.ts +373 -0
  77. package/src/sentinel/README.md +103 -0
  78. package/src/sentinel/config.ts +49 -0
  79. package/src/sentinel/factory.ts +46 -0
  80. package/src/sentinel/index.ts +8 -0
  81. package/src/sentinel/sentinel.ts +694 -0
  82. package/src/sentinel/store.ts +193 -0
  83. package/src/test/index.ts +32 -0
@@ -0,0 +1,694 @@
1
+ import type { EpochCache } from '@aztec-labs/epoch-cache';
2
+ import {
3
+ CheckpointNumber,
4
+ CheckpointProposalHash,
5
+ EpochNumber,
6
+ SlotNumber,
7
+ } from '@aztec-labs/foundation/branded-types';
8
+ import { countWhile, filterAsync, fromEntries, getEntries, mapValues } from '@aztec-labs/foundation/collection';
9
+ import { EthAddress } from '@aztec-labs/foundation/eth-address';
10
+ import { createLogger } from '@aztec-labs/foundation/log';
11
+ import { RunningPromise } from '@aztec-labs/foundation/running-promise';
12
+ import type { P2PClient } from '@aztec-labs/p2p';
13
+ import {
14
+ OffenseType,
15
+ WANT_TO_SLASH_EVENT,
16
+ type WantToSlashArgs,
17
+ type Watcher,
18
+ type WatcherEmitter,
19
+ getOffenseTypeName,
20
+ } from '@aztec-labs/slasher';
21
+ import type { SlasherConfig } from '@aztec-labs/slasher/config';
22
+ import { type L2BlockSource, getAttestationInfoFromPublishedCheckpoint } from '@aztec-labs/stdlib/block';
23
+ import type { CheckpointReexecutionTracker } from '@aztec-labs/stdlib/checkpoint';
24
+ import type { ChainConfig } from '@aztec-labs/stdlib/config';
25
+ import { getEpochAtSlot, getSlotRangeForEpoch, getTimestampForSlot } from '@aztec-labs/stdlib/epoch-helpers';
26
+ import { ConsensusPayload, type CoordinationSignatureContext } from '@aztec-labs/stdlib/p2p';
27
+ import type {
28
+ SingleValidatorStats,
29
+ ValidatorStats,
30
+ ValidatorStatusHistory,
31
+ ValidatorStatusInSlot,
32
+ ValidatorStatusType,
33
+ ValidatorsEpochPerformance,
34
+ ValidatorsStats,
35
+ } from '@aztec-labs/stdlib/validators';
36
+ import EventEmitter from 'node:events';
37
+
38
+ import type { SentinelConfig } from './config.js';
39
+ import { SentinelStore } from './store.js';
40
+
41
+ export type SentinelRuntimeConfig = Pick<
42
+ SlasherConfig,
43
+ 'slashInactivityTargetPercentage' | 'slashInactivityPenalty' | 'slashInactivityConsecutiveEpochThreshold'
44
+ > &
45
+ Pick<SentinelConfig, 'sentinelEpochEndBufferSlots'> &
46
+ Pick<ChainConfig, 'l1ChainId' | 'rollupAddress'>;
47
+
48
+ /** Maps a validator status to its category: proposer or attestation. */
49
+ function statusToCategory(status: ValidatorStatusInSlot): ValidatorStatusType {
50
+ switch (status) {
51
+ case 'attestation-sent':
52
+ case 'attestation-missed':
53
+ return 'attestation';
54
+ default:
55
+ return 'proposer';
56
+ }
57
+ }
58
+
59
+ /**
60
+ * The Sentinel observes validator behaviour every L2 slot, classifies it into a per-slot status,
61
+ * aggregates those statuses into per-epoch performance once each epoch is fully observed, and
62
+ * emits inactivity slash payloads when a validator has been inactive for the configured number
63
+ * of consecutive epochs.
64
+ *
65
+ * ## Two cadences
66
+ *
67
+ * The sentinel runs `work()` every quarter L2 slot and drives two independent pipelines:
68
+ *
69
+ * 1. **Per-slot activity recording.** `processSlot(currentSlot - 2)` runs once per slot, with a
70
+ * two-slot lag to let P2P attestations settle and the archiver catch up. It classifies each
71
+ * committee member's behaviour for that slot via `getSlotActivity` and persists the result to
72
+ * `SentinelStore.historyMap` (sliding window of `sentinelHistoryLengthInEpochs * epochDuration`
73
+ * slots, default 24 epochs).
74
+ *
75
+ * 2. **Per-epoch evaluation.** `processEpochEnds(currentSlot)` runs every tick too. Once
76
+ * `sentinelEpochEndBufferSlots` (default 2) has elapsed past an epoch's last slot AND the
77
+ * per-slot recorder has covered that last slot, the sentinel calls `handleEpochEnd(epoch)`.
78
+ * That aggregates the slot-level statuses for the epoch into per-validator `{missed, total}`,
79
+ * persists it to `SentinelStore.epochMap` (default 2000-epoch window), and runs the slashing
80
+ * decision.
81
+ *
82
+ * Triggering per-epoch evaluation off local L2 state — rather than waiting for L1 proof
83
+ * publication — decouples slashing from prover availability.
84
+ *
85
+ * ## Six-case taxonomy in `getSlotActivity`
86
+ *
87
+ * For each slot, the sentinel assigns the proposer one of six statuses, ranked highest-confidence
88
+ * first:
89
+ *
90
+ * - `checkpoint-mined` — a checkpoint covering this slot has landed on L1
91
+ * (fetched on demand via `archiver.getCheckpoint({ slot })`).
92
+ * - `checkpoint-valid` — the local node re-executed a checkpoint proposal for this slot
93
+ * successfully (consulted via `CheckpointReexecutionTracker`).
94
+ * - `checkpoint-invalid` — the local node re-executed a checkpoint proposal for this slot
95
+ * and rejected it (e.g. header/archive/out-hash mismatch, limit
96
+ * breach). Proposer-fault.
97
+ * - `checkpoint-unvalidated` — the local node observed a checkpoint proposal but could not
98
+ * validate it (missing blocks/txs, timeouts). Treated as
99
+ * proposer-fault for slashing.
100
+ * - `checkpoint-missed` — block proposals seen on P2P but no checkpoint proposal at all.
101
+ * - `blocks-missed` — no block proposals seen for this slot.
102
+ *
103
+ * Missing-attestor faults are recorded only in `checkpoint-mined` and `checkpoint-valid`, where
104
+ * the local node has positive evidence the checkpoint was canonical or valid. In the other four
105
+ * cases the proposer is at fault and no attestor penalty applies.
106
+ *
107
+ * ## Re-execution tracker
108
+ *
109
+ * `CheckpointReexecutionTracker` is populated by the validator client's checkpoint proposal
110
+ * handler. Every early return in `validateCheckpointProposal` records an outcome
111
+ * (`valid` / `invalid` / `unvalidated`) keyed by slot.
112
+ *
113
+ * ## Inactivity slashing
114
+ *
115
+ * `handleEpochPerformance` filters the epoch's per-validator stats by
116
+ * `slashInactivityTargetPercentage` and then calls `checkPastInactivity` to require
117
+ * `slashInactivityConsecutiveEpochThreshold` consecutive past epochs over the same threshold
118
+ * (read from `SentinelStore.epochMap`). Only validators meeting both conditions are emitted as
119
+ * `WANT_TO_SLASH_EVENT` with `OffenseType.INACTIVITY`. The slot-level counters that feed this —
120
+ * `missedProposals` and `missedAttestations` — include the four proposer-fault statuses plus
121
+ * `attestation-missed`.
122
+ *
123
+ * ## Escape hatch
124
+ *
125
+ * If `epochCache.getCommittee(slot)` reports `isEscapeHatchOpen`, per-slot recording is skipped
126
+ * (no history entries for that slot) and per-epoch evaluation writes an empty performance map
127
+ * (no slashing).
128
+ */
129
+ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
130
+ protected runningPromise: RunningPromise;
131
+
132
+ protected initialSlot: SlotNumber | undefined;
133
+ protected lastProcessedSlot: SlotNumber | undefined;
134
+ /** Largest epoch number for which the end-of-epoch aggregator has run. */
135
+ protected lastEvaluatedEpoch: EpochNumber | undefined;
136
+
137
+ constructor(
138
+ protected epochCache: EpochCache,
139
+ protected archiver: L2BlockSource,
140
+ protected p2p: P2PClient,
141
+ protected store: SentinelStore,
142
+ protected reexecutionTracker: CheckpointReexecutionTracker,
143
+ protected config: SentinelRuntimeConfig,
144
+ protected logger = createLogger('node:sentinel'),
145
+ ) {
146
+ super();
147
+ const interval = (epochCache.getL1Constants().ethereumSlotDuration * 1000) / 4;
148
+ this.runningPromise = new RunningPromise(this.work.bind(this), logger, interval);
149
+ }
150
+
151
+ private getSignatureContext(): CoordinationSignatureContext {
152
+ return {
153
+ chainId: this.config.l1ChainId,
154
+ rollupAddress: this.config.rollupAddress,
155
+ };
156
+ }
157
+
158
+ public updateConfig(config: Partial<SlasherConfig>) {
159
+ this.config = { ...this.config, ...config };
160
+ }
161
+
162
+ public async start() {
163
+ await this.init();
164
+ this.runningPromise.start();
165
+ }
166
+
167
+ /**
168
+ * Loads the initial slot. We will not process anything at or before the initial slot. Floors at the
169
+ * archiver's synced L2 slot so the sentinel keeps making forward progress when L1 is advancing but L2 has no
170
+ * activity (the synced slot is driven by L1 sync, not by L2 blocks). Falls back to the wallclock if the
171
+ * archiver isn't ready yet (cold start).
172
+ */
173
+ protected async init() {
174
+ this.initialSlot = await this.getCurrentSlot();
175
+ this.logger.info(`Starting validator sentinel with initial slot ${this.initialSlot}`);
176
+ }
177
+
178
+ /**
179
+ * Returns the L2 slot the sentinel should treat as "current": the archiver's last fully
180
+ * synced L2 slot, falling back to the wallclock slot when the archiver isn't ready yet
181
+ * (cold start). Anchoring to the synced slot keeps timing arithmetic (initial floor,
182
+ * per-slot lag, end-of-epoch buffer, stats-range fallback) from speculating ahead of where
183
+ * L1 actually is.
184
+ */
185
+ protected async getCurrentSlot(): Promise<SlotNumber> {
186
+ return (await this.archiver.getSyncedL2SlotNumber()) ?? this.epochCache.getSlotNow();
187
+ }
188
+
189
+ public stop() {
190
+ return this.runningPromise.stop();
191
+ }
192
+
193
+ /**
194
+ * Fetches the L1-confirmed checkpoint covering a slot (if any) and derives the slot-level data the
195
+ * activity classifier needs: the checkpoint number, archive root, consensus payload hash (used to fetch
196
+ * matching p2p attestations regardless of feeAssetPriceModifier variants), and the recovered attestor set.
197
+ * Reads on demand so the result is always against the canonical chain — a reorged-out checkpoint simply
198
+ * stops being returned, with no stale mapping to clean up.
199
+ */
200
+ protected async getCheckpointForSlot(slot: SlotNumber): Promise<
201
+ | {
202
+ checkpointNumber: CheckpointNumber;
203
+ archive: string;
204
+ proposalPayloadHash: CheckpointProposalHash;
205
+ attestors: EthAddress[];
206
+ }
207
+ | undefined
208
+ > {
209
+ const checkpoint = await this.archiver.getCheckpoint({ slot });
210
+ if (!checkpoint) {
211
+ return undefined;
212
+ }
213
+ const signatureContext = this.getSignatureContext();
214
+ const proposalPayloadHash = CheckpointProposalHash.fromBuffer(
215
+ ConsensusPayload.fromCheckpoint(checkpoint.checkpoint, signatureContext).getPayloadHash(),
216
+ );
217
+ return {
218
+ checkpointNumber: checkpoint.checkpoint.number,
219
+ archive: checkpoint.checkpoint.archive.root.toString(),
220
+ proposalPayloadHash,
221
+ attestors: getAttestationInfoFromPublishedCheckpoint(checkpoint, signatureContext)
222
+ .filter(a => a.status === 'recovered-from-signature')
223
+ .map(a => a.address!),
224
+ };
225
+ }
226
+
227
+ /**
228
+ * Called once per epoch, after the configured end-of-epoch buffer has elapsed beyond the
229
+ * epoch's last slot. Computes per-epoch performance from the slot-level history collected
230
+ * by `processSlot` and emits any inactivity slash payloads.
231
+ */
232
+ protected async handleEpochEnd(epoch: EpochNumber) {
233
+ this.logger.debug(`Computing epoch performance for epoch ${epoch}`);
234
+ const performance = await this.computeEpochPerformance(epoch);
235
+ this.logger.info(`Computed epoch performance for epoch ${epoch}`, performance);
236
+
237
+ await this.store.updateEpochPerformance(epoch, performance);
238
+ await this.handleEpochPerformance(epoch, performance);
239
+ }
240
+
241
+ protected async computeEpochPerformance(epoch: EpochNumber): Promise<ValidatorsEpochPerformance> {
242
+ const [fromSlot, toSlot] = getSlotRangeForEpoch(epoch, this.epochCache.getL1Constants());
243
+ const { committee, isEscapeHatchOpen } = await this.epochCache.getCommittee(fromSlot);
244
+ if (isEscapeHatchOpen) {
245
+ this.logger.info(`Skipping epoch performance for epoch ${epoch} - escape hatch is open`);
246
+ return {};
247
+ }
248
+ if (!committee) {
249
+ this.logger.trace(`No committee found for slot ${fromSlot}`);
250
+ return {};
251
+ }
252
+
253
+ const stats = await this.computeStats({
254
+ fromSlot,
255
+ toSlot,
256
+ validators: committee,
257
+ });
258
+ this.logger.debug(`Stats for epoch ${epoch}`, { ...stats, fromSlot, toSlot, epoch });
259
+
260
+ // Note that we are NOT using the total slots in the epoch as `total` here, since we only
261
+ // compute missed attestations over the blocks that had a proposal in them. So, let's say
262
+ // we have an epoch with 10 slots, but only 5 had a block proposal. A validator that was
263
+ // offline, assuming they were not picked as proposer, will then be reported as having missed
264
+ // 5/5 attestations. If we used the total, they'd be reported as 5/10, which would probably
265
+ // allow them to avoid being slashed.
266
+ return mapValues(stats.stats, stat => ({
267
+ missed: stat.missedAttestations.count + stat.missedProposals.count,
268
+ total: stat.missedAttestations.total + stat.missedProposals.total,
269
+ }));
270
+ }
271
+
272
+ /**
273
+ * Checks if a validator has been inactive for the specified number of consecutive epochs for which we have data on it.
274
+ * @param validator The validator address to check
275
+ * @param currentEpoch Epochs strictly before the current one are evaluated only
276
+ * @param requiredConsecutiveEpochs Number of consecutive epochs required for slashing
277
+ */
278
+ protected async checkPastInactivity(
279
+ validator: EthAddress,
280
+ currentEpoch: EpochNumber,
281
+ requiredConsecutiveEpochs: number,
282
+ ): Promise<boolean> {
283
+ if (requiredConsecutiveEpochs === 0) {
284
+ return true;
285
+ }
286
+
287
+ // Get all historical per-epoch performance for this validator
288
+ const allPerformance = await this.store.getEpochPerformance(validator);
289
+
290
+ // Sort by epoch descending to get most recent first, keep only epochs strictly before the current one, and get the first N
291
+ const pastEpochs = allPerformance.sort((a, b) => Number(b.epoch - a.epoch)).filter(p => p.epoch < currentEpoch);
292
+
293
+ // If we don't have enough historical data, don't slash
294
+ if (pastEpochs.length < requiredConsecutiveEpochs) {
295
+ this.logger.debug(
296
+ `Not enough historical data for slashing ${validator} for inactivity (${allPerformance.length} epochs < ${requiredConsecutiveEpochs} required)`,
297
+ );
298
+ return false;
299
+ }
300
+
301
+ // Check that we have at least requiredConsecutiveEpochs and that all of them are above the inactivity threshold
302
+ return pastEpochs
303
+ .slice(0, requiredConsecutiveEpochs)
304
+ .every(p => (p.total === 0 ? false : p.missed / p.total >= this.config.slashInactivityTargetPercentage));
305
+ }
306
+
307
+ protected async handleEpochPerformance(epoch: EpochNumber, performance: ValidatorsEpochPerformance) {
308
+ const inactiveValidators = getEntries(performance)
309
+ .filter(([_, { missed, total }]) => total > 0 && missed / total >= this.config.slashInactivityTargetPercentage)
310
+ .map(([address]) => address);
311
+
312
+ this.logger.debug(`Found ${inactiveValidators.length} inactive validators in epoch ${epoch}`, {
313
+ inactiveValidators,
314
+ epoch,
315
+ inactivityTargetPercentage: this.config.slashInactivityTargetPercentage,
316
+ });
317
+
318
+ const epochThreshold = this.config.slashInactivityConsecutiveEpochThreshold;
319
+ const criminals: string[] = await filterAsync(inactiveValidators, address =>
320
+ this.checkPastInactivity(EthAddress.fromString(address), epoch, epochThreshold - 1),
321
+ );
322
+
323
+ const args: WantToSlashArgs[] = criminals.map(address => ({
324
+ validator: EthAddress.fromString(address),
325
+ amount: this.config.slashInactivityPenalty,
326
+ offenseType: OffenseType.INACTIVITY,
327
+ epochOrSlot: BigInt(epoch),
328
+ }));
329
+
330
+ if (criminals.length > 0) {
331
+ this.logger.info(
332
+ `Identified ${criminals.length} inactivity offenses in at least ${epochThreshold} consecutive epochs`,
333
+ {
334
+ offenses: args.map(arg => ({
335
+ validator: arg.validator.toString(),
336
+ amount: arg.amount,
337
+ offenseType: getOffenseTypeName(arg.offenseType),
338
+ epochOrSlot: arg.epochOrSlot,
339
+ })),
340
+ epochThreshold,
341
+ },
342
+ );
343
+ this.emit(WANT_TO_SLASH_EVENT, args);
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Process data for two L2 slots ago.
349
+ * Note that we do not process historical data, since we rely on p2p data for processing,
350
+ * and we don't have that data if we were offline during the period.
351
+ *
352
+ * `currentSlot` is anchored to the archiver's last synced L2 slot rather than the wallclock,
353
+ * so the per-slot lag (`isReadyToProcess`) and the end-of-epoch buffer (`processEpochEnds`)
354
+ * advance with archiver.
355
+ */
356
+ public async work() {
357
+ const currentSlot = await this.getCurrentSlot();
358
+ try {
359
+ // Per-slot activity recording (lag = 2 slots for P2P attestation settlement).
360
+ const targetSlot = await this.isReadyToProcess(currentSlot);
361
+ if (targetSlot !== false) {
362
+ await this.processSlot(targetSlot);
363
+ }
364
+
365
+ // End-of-epoch evaluation (lag = sentinelEpochEndBufferSlots beyond the epoch's last slot).
366
+ await this.processEpochEnds(currentSlot);
367
+ } catch (err) {
368
+ this.logger.error(`Failed to process slot ${currentSlot}`, err);
369
+ }
370
+ }
371
+
372
+ /**
373
+ * After the configured buffer has elapsed past an epoch's last slot, runs the end-of-epoch
374
+ * aggregator for that epoch. Catches up if multiple epochs become eligible at once.
375
+ */
376
+ protected async processEpochEnds(currentSlot: SlotNumber) {
377
+ const constants = this.epochCache.getL1Constants();
378
+ const buffer = this.config.sentinelEpochEndBufferSlots;
379
+ if (currentSlot < buffer) {
380
+ return;
381
+ }
382
+ if (this.initialSlot === undefined) {
383
+ return;
384
+ }
385
+
386
+ // We can close epoch E iff:
387
+ // - the per-slot recorder has covered the epoch's last slot (lastProcessedSlot ≥ toSlot(E))
388
+ // - the buffer has elapsed past the epoch's last slot (currentSlot − buffer ≥ toSlot(E))
389
+ // - the epoch is not in the past relative to when the sentinel started (toSlot(E) > initialSlot)
390
+ if (this.lastProcessedSlot === undefined) {
391
+ return;
392
+ }
393
+ const slotForBuffer = SlotNumber(currentSlot - buffer);
394
+
395
+ // First eligible epoch to close is the one after lastEvaluatedEpoch, or the epoch containing
396
+ // the initial slot if we haven't evaluated any yet (the initialSlot epoch may be partial — we
397
+ // don't try to evaluate it, we start from initialSlot's epoch + 1).
398
+ const startEpoch =
399
+ this.lastEvaluatedEpoch !== undefined
400
+ ? EpochNumber(this.lastEvaluatedEpoch + 1)
401
+ : EpochNumber(getEpochAtSlot(this.initialSlot, constants) + 1);
402
+
403
+ for (let epoch = startEpoch; ; epoch = EpochNumber(epoch + 1)) {
404
+ const [, toSlot] = getSlotRangeForEpoch(epoch, constants);
405
+ if (toSlot > this.lastProcessedSlot || toSlot > slotForBuffer) {
406
+ break;
407
+ }
408
+ await this.handleEpochEnd(epoch);
409
+ this.lastEvaluatedEpoch = epoch;
410
+ }
411
+ }
412
+
413
+ /**
414
+ * Check if we are ready to process data for two L2 slots ago, so we allow plenty of time for p2p to process all in-flight attestations.
415
+ * We also don't move past the archiver last synced L2 slot, as we don't want to process data that is not yet available.
416
+ * Last, we check the p2p is synced with the archiver, so it has pulled all attestations from it.
417
+ */
418
+ protected async isReadyToProcess(currentSlot: SlotNumber): Promise<SlotNumber | false> {
419
+ if (currentSlot < 2) {
420
+ this.logger.trace(`Current slot ${currentSlot} too early.`);
421
+ return false;
422
+ }
423
+
424
+ const targetSlot = SlotNumber(currentSlot - 2);
425
+ if (this.lastProcessedSlot && this.lastProcessedSlot >= targetSlot) {
426
+ this.logger.trace(`Already processed slot ${targetSlot}`, { lastProcessedSlot: this.lastProcessedSlot });
427
+ return false;
428
+ }
429
+
430
+ if (this.initialSlot === undefined) {
431
+ this.logger.error(`Initial slot not loaded.`);
432
+ return false;
433
+ }
434
+
435
+ if (targetSlot <= this.initialSlot) {
436
+ this.logger.trace(`Refusing to process slot ${targetSlot} given initial slot ${this.initialSlot}`);
437
+ return false;
438
+ }
439
+
440
+ const syncedSlot = await this.archiver.getSyncedL2SlotNumber();
441
+ if (syncedSlot === undefined || syncedSlot < targetSlot) {
442
+ this.logger.debug(`Waiting for archiver to sync with L2 slot ${targetSlot}`, { syncedSlot, targetSlot });
443
+ return false;
444
+ }
445
+
446
+ const archiverLastBlockHash = await this.archiver.getL2Tips().then(tip => tip.proposed.hash);
447
+ const p2pLastBlockHash = await this.p2p.getL2Tips().then(tips => tips.proposed.hash);
448
+ const isP2pSynced = archiverLastBlockHash === p2pLastBlockHash;
449
+ if (!isP2pSynced) {
450
+ this.logger.debug(`Waiting for P2P client to sync with archiver`, { archiverLastBlockHash, p2pLastBlockHash });
451
+ return false;
452
+ }
453
+
454
+ return targetSlot;
455
+ }
456
+
457
+ /**
458
+ * Gathers committee and proposer data for a given slot, computes slot stats,
459
+ * and updates overall stats.
460
+ */
461
+ protected async processSlot(slot: SlotNumber) {
462
+ const { epoch, seed, committee, isEscapeHatchOpen } = await this.epochCache.getCommittee(slot);
463
+ if (isEscapeHatchOpen) {
464
+ this.logger.info(`Skipping slot ${slot} at epoch ${epoch} - escape hatch is open`);
465
+ this.lastProcessedSlot = slot;
466
+ return;
467
+ }
468
+ if (!committee || committee.length === 0) {
469
+ this.logger.trace(`No committee found for slot ${slot} at epoch ${epoch}`);
470
+ this.lastProcessedSlot = slot;
471
+ return;
472
+ }
473
+ const proposerIndex = this.epochCache.computeProposerIndex(slot, epoch, seed, BigInt(committee.length));
474
+ const proposer = committee[Number(proposerIndex)];
475
+ const stats = await this.getSlotActivity(slot, epoch, proposer, committee);
476
+ this.logger.verbose(`Updating L2 slot ${slot} observed activity`, stats);
477
+ await this.updateValidators(slot, stats);
478
+ this.lastProcessedSlot = slot;
479
+ }
480
+
481
+ /**
482
+ * Computes activity for a given slot using the six-case taxonomy.
483
+ *
484
+ * Proposer status:
485
+ * - case 6 `checkpoint-mined` — a checkpoint covering this slot has landed on L1.
486
+ * - case 5 `checkpoint-valid` — the local node re-executed a checkpoint proposal for this
487
+ * slot successfully.
488
+ * - case 4 `checkpoint-invalid` — the local node re-executed a checkpoint proposal for this
489
+ * slot and rejected it.
490
+ * - case 3 `checkpoint-unvalidated` — the local node observed a checkpoint proposal for this
491
+ * slot but could not validate it (missing data, timeouts).
492
+ * - case 2 `checkpoint-missed` — block proposals seen on P2P but no checkpoint proposal.
493
+ * - case 1 `blocks-missed` — no block proposals seen for this slot.
494
+ *
495
+ * Missing-attestor penalties apply only in cases 5 and 6, where the local node has positive
496
+ * evidence the checkpoint was valid or has been canonicalised on L1.
497
+ */
498
+ protected async getSlotActivity(slot: SlotNumber, epoch: EpochNumber, proposer: EthAddress, committee: EthAddress[]) {
499
+ this.logger.debug(`Computing stats for slot ${slot} at epoch ${epoch}`, { slot, epoch, proposer, committee });
500
+
501
+ // Gather attestors from both p2p (live attestations) and the archiver (signers on the
502
+ // checkpoint if one has landed on L1). Fetched on demand so it always reflects the canonical chain.
503
+ // Used regardless of which case applies.
504
+ const checkpoint = await this.getCheckpointForSlot(slot);
505
+ const p2pAttested = await this.p2p.getCheckpointAttestationsForSlot(slot, checkpoint?.proposalPayloadHash);
506
+ const p2pAttestors = p2pAttested.map(a => a.getSender()).filter((s): s is EthAddress => s !== undefined);
507
+ const attestors = new Set(
508
+ [...p2pAttestors.map(a => a.toString()), ...(checkpoint?.attestors.map(a => a.toString()) ?? [])].filter(
509
+ addr => proposer.toString() !== addr,
510
+ ),
511
+ );
512
+
513
+ // Determine the proposer status from the six-case taxonomy.
514
+ const reexecutionOutcome = this.reexecutionTracker.getOutcomeForSlot(slot);
515
+ let status:
516
+ | 'checkpoint-mined'
517
+ | 'checkpoint-valid'
518
+ | 'checkpoint-invalid'
519
+ | 'checkpoint-unvalidated'
520
+ | 'checkpoint-missed'
521
+ | 'blocks-missed';
522
+ if (checkpoint) {
523
+ status = 'checkpoint-mined';
524
+ } else if (reexecutionOutcome === 'valid') {
525
+ status = 'checkpoint-valid';
526
+ } else if (reexecutionOutcome === 'invalid') {
527
+ status = 'checkpoint-invalid';
528
+ } else if (reexecutionOutcome === 'unvalidated') {
529
+ status = 'checkpoint-unvalidated';
530
+ } else {
531
+ // No L1 checkpoint, no local re-execution outcome for this slot. Distinguish "proposer
532
+ // sent block proposals but never made a checkpoint" from "proposer sent nothing".
533
+ const hasBlockProposals = await this.p2p.hasBlockProposalsForSlot(slot);
534
+ status = hasBlockProposals ? 'checkpoint-missed' : 'blocks-missed';
535
+ }
536
+ this.logger.debug(`Checkpoint status for slot ${slot}: ${status}`, { ...checkpoint, slot });
537
+
538
+ // Missing-attestor faults only apply when we have positive evidence the proposal was valid.
539
+ const attestorsExpected = status === 'checkpoint-mined' || status === 'checkpoint-valid';
540
+ const missedAttestors = new Set(
541
+ attestorsExpected
542
+ ? committee.filter(v => !attestors.has(v.toString()) && !proposer.equals(v)).map(v => v.toString())
543
+ : [],
544
+ );
545
+
546
+ this.logger.debug(`Retrieved ${attestors.size} attestors out of ${committee.length} for slot ${slot}`, {
547
+ status,
548
+ proposer: proposer.toString(),
549
+ ...checkpoint,
550
+ slot,
551
+ attestors: [...attestors],
552
+ missedAttestors: [...missedAttestors],
553
+ committee: committee.map(c => c.toString()),
554
+ });
555
+
556
+ // Compute the status for each validator in the committee
557
+ const statusFor = (who: `0x${string}`): ValidatorStatusInSlot | undefined => {
558
+ if (who === proposer.toString()) {
559
+ return status;
560
+ } else if (attestors.has(who)) {
561
+ return 'attestation-sent';
562
+ } else if (missedAttestors.has(who)) {
563
+ return 'attestation-missed';
564
+ } else {
565
+ return undefined;
566
+ }
567
+ };
568
+
569
+ return Object.fromEntries(committee.map(v => v.toString()).map(who => [who, statusFor(who)]));
570
+ }
571
+
572
+ /** Push the status for each slot for each validator. */
573
+ protected updateValidators(slot: SlotNumber, stats: Record<`0x${string}`, ValidatorStatusInSlot | undefined>) {
574
+ return this.store.updateValidators(slot, stats);
575
+ }
576
+
577
+ /** Computes stats to be returned based on stored data. */
578
+ public async computeStats({
579
+ fromSlot,
580
+ toSlot,
581
+ validators,
582
+ }: { fromSlot?: SlotNumber; toSlot?: SlotNumber; validators?: EthAddress[] } = {}): Promise<ValidatorsStats> {
583
+ const histories = validators
584
+ ? fromEntries(await Promise.all(validators.map(async v => [v.toString(), await this.store.getHistory(v)])))
585
+ : await this.store.getHistories();
586
+
587
+ const slotNow = await this.getCurrentSlot();
588
+ fromSlot ??= SlotNumber(Math.max((this.lastProcessedSlot ?? slotNow) - this.store.getHistoryLength(), 0));
589
+ toSlot ??= this.lastProcessedSlot ?? slotNow;
590
+
591
+ const stats = mapValues(histories, (history, address) =>
592
+ this.computeStatsForValidator(address, history ?? [], fromSlot, toSlot),
593
+ );
594
+
595
+ return {
596
+ stats,
597
+ lastProcessedSlot: this.lastProcessedSlot,
598
+ initialSlot: this.initialSlot,
599
+ slotWindow: this.store.getHistoryLength(),
600
+ };
601
+ }
602
+
603
+ /** Computes stats for a single validator. */
604
+ public async getValidatorStats(
605
+ validatorAddress: EthAddress,
606
+ fromSlot?: SlotNumber,
607
+ toSlot?: SlotNumber,
608
+ ): Promise<SingleValidatorStats | undefined> {
609
+ const history = await this.store.getHistory(validatorAddress);
610
+
611
+ if (!history || history.length === 0) {
612
+ return undefined;
613
+ }
614
+
615
+ const slotNow = await this.getCurrentSlot();
616
+ const effectiveFromSlot =
617
+ fromSlot ?? SlotNumber(Math.max((this.lastProcessedSlot ?? slotNow) - this.store.getHistoryLength(), 0));
618
+ const effectiveToSlot = toSlot ?? this.lastProcessedSlot ?? slotNow;
619
+
620
+ const historyLength = BigInt(this.store.getHistoryLength());
621
+ if (BigInt(effectiveToSlot) - BigInt(effectiveFromSlot) > historyLength) {
622
+ throw new Error(
623
+ `Slot range (${BigInt(effectiveToSlot) - BigInt(effectiveFromSlot)}) exceeds history length (${historyLength}). ` +
624
+ `Requested range: ${effectiveFromSlot} to ${effectiveToSlot}.`,
625
+ );
626
+ }
627
+
628
+ const validator = this.computeStatsForValidator(
629
+ validatorAddress.toString(),
630
+ history,
631
+ effectiveFromSlot,
632
+ effectiveToSlot,
633
+ );
634
+
635
+ return {
636
+ validator,
637
+ allTimeEpochPerformance: await this.store.getEpochPerformance(validatorAddress),
638
+ lastProcessedSlot: this.lastProcessedSlot,
639
+ initialSlot: this.initialSlot,
640
+ slotWindow: this.store.getHistoryLength(),
641
+ };
642
+ }
643
+
644
+ protected computeStatsForValidator(
645
+ address: `0x${string}`,
646
+ allHistory: ValidatorStatusHistory,
647
+ fromSlot?: SlotNumber,
648
+ toSlot?: SlotNumber,
649
+ ): ValidatorStats {
650
+ let history = fromSlot ? allHistory.filter(h => BigInt(h.slot) >= fromSlot) : allHistory;
651
+ history = toSlot ? history.filter(h => BigInt(h.slot) <= toSlot) : history;
652
+ const lastProposal = history.filter(h => h.status === 'checkpoint-valid' || h.status === 'checkpoint-mined').at(-1);
653
+ const lastAttestation = history.filter(h => h.status === 'attestation-sent').at(-1);
654
+ return {
655
+ address: EthAddress.fromString(address),
656
+ lastProposal: this.computeFromSlot(lastProposal?.slot),
657
+ lastAttestation: this.computeFromSlot(lastAttestation?.slot),
658
+ totalSlots: history.length,
659
+ missedProposals: this.computeMissed(history, 'proposer', [
660
+ 'checkpoint-missed',
661
+ 'blocks-missed',
662
+ 'checkpoint-invalid',
663
+ 'checkpoint-unvalidated',
664
+ ]),
665
+ missedAttestations: this.computeMissed(history, 'attestation', ['attestation-missed']),
666
+ history,
667
+ };
668
+ }
669
+
670
+ protected computeMissed(
671
+ history: ValidatorStatusHistory,
672
+ computeOverCategory: ValidatorStatusType | undefined,
673
+ filter: ValidatorStatusInSlot[],
674
+ ) {
675
+ const relevantHistory = history.filter(
676
+ h => !computeOverCategory || statusToCategory(h.status) === computeOverCategory,
677
+ );
678
+ const filteredHistory = relevantHistory.filter(h => filter.includes(h.status));
679
+ return {
680
+ currentStreak: countWhile([...relevantHistory].reverse(), h => filter.includes(h.status)),
681
+ rate: relevantHistory.length === 0 ? undefined : filteredHistory.length / relevantHistory.length,
682
+ count: filteredHistory.length,
683
+ total: relevantHistory.length,
684
+ };
685
+ }
686
+
687
+ protected computeFromSlot(slot: SlotNumber | undefined) {
688
+ if (slot === undefined) {
689
+ return undefined;
690
+ }
691
+ const timestamp = getTimestampForSlot(slot, this.epochCache.getL1Constants());
692
+ return { timestamp, slot, date: new Date(Number(timestamp) * 1000).toISOString() };
693
+ }
694
+ }