@aztec/epoch-cache 0.0.1-commit.c31f2472 → 0.0.1-commit.c52d6e7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/config.ts CHANGED
@@ -3,7 +3,7 @@ import { type L1ReaderConfig, getL1ReaderConfigFromEnv } from '@aztec/ethereum/l
3
3
 
4
4
  export type EpochCacheConfig = Pick<
5
5
  L1ReaderConfig & L1ContractsConfig,
6
- 'l1RpcUrls' | 'l1ChainId' | 'viemPollingIntervalMS' | 'ethereumSlotDuration'
6
+ 'l1RpcUrls' | 'l1ChainId' | 'viemPollingIntervalMS' | 'ethereumSlotDuration' | 'l1HttpTimeoutMS'
7
7
  >;
8
8
 
9
9
  export function getEpochCacheConfigEnvVars(): EpochCacheConfig {
@@ -1,5 +1,7 @@
1
1
  import { createEthereumChain } from '@aztec/ethereum/chain';
2
+ import { makeL1HttpTransport } from '@aztec/ethereum/client';
2
3
  import { NoCommitteeError, RollupContract } from '@aztec/ethereum/contracts';
4
+ import { getFinalizedL1Block } from '@aztec/ethereum/queries';
3
5
  import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
6
  import { EthAddress } from '@aztec/foundation/eth-address';
5
7
  import { type Logger, createLogger } from '@aztec/foundation/log';
@@ -8,19 +10,25 @@ import {
8
10
  type L1RollupConstants,
9
11
  getEpochAtSlot,
10
12
  getEpochNumberAtTimestamp,
13
+ getNextL1SlotTimestamp,
14
+ getSlotAtNextL1Block,
11
15
  getSlotAtTimestamp,
12
16
  getSlotRangeForEpoch,
17
+ getStartTimestampForEpoch,
13
18
  getTimestampForSlot,
14
- getTimestampRangeForEpoch,
15
19
  } from '@aztec/stdlib/epoch-helpers';
16
20
 
17
- import { createPublicClient, encodeAbiParameters, fallback, http, keccak256 } from 'viem';
21
+ import { createPublicClient, encodeAbiParameters, keccak256 } from 'viem';
18
22
 
19
23
  import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
20
24
 
25
+ /** The proposer pipelines by building one slot ahead. */
26
+ export const PROPOSER_PIPELINING_SLOT_OFFSET = 1;
27
+
28
+ /** Flat return type for compound epoch/slot getters. */
21
29
  export type EpochAndSlot = {
22
- epoch: EpochNumber;
23
30
  slot: SlotNumber;
31
+ epoch: EpochNumber;
24
32
  ts: bigint;
25
33
  };
26
34
 
@@ -34,17 +42,44 @@ export type EpochCommitteeInfo = {
34
42
 
35
43
  export type SlotTag = 'now' | 'next' | SlotNumber;
36
44
 
45
+ /** Minimal L1 block info used for cache provenance. */
46
+ type L1BlockInfo = { number: bigint; hash: `0x${string}`; timestamp: bigint };
47
+
48
+ /** Resolved cache entry with L1 provenance metadata. */
49
+ type CachedEpochEntry = {
50
+ data: EpochCommitteeInfo;
51
+ /** L1 block number at which the committee data was originally queried. */
52
+ lastQueryL1BlockNumber: bigint;
53
+ /** L1 block hash at which the committee data was originally queried. Used to detect reorgs. */
54
+ lastQueryL1BlockHash: `0x${string}`;
55
+ /** Latest L1 block timestamp at the time of the most recent refresh (full fetch or lightweight check). */
56
+ lastRefreshL1Timestamp: bigint;
57
+ /** Whether the epoch's sampling data falls within finalized L1 history. */
58
+ finalized: boolean;
59
+ };
60
+
37
61
  export interface EpochCacheInterface {
38
62
  getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
63
+ getSlotNow(): SlotNumber;
64
+ getTargetSlot(): SlotNumber;
65
+ getEpochNow(): EpochNumber;
66
+ getTargetEpoch(): EpochNumber;
39
67
  getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint };
40
- getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint };
68
+ getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
69
+ /** Returns epoch/slot info for the next L1 slot with pipeline offset applied. */
70
+ getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
71
+ isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean>;
72
+ isEscapeHatchOpenAtSlot(slot: SlotTag): Promise<boolean>;
41
73
  getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
42
74
  computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
43
75
  getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
76
+ getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber };
44
77
  getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
45
78
  getRegisteredValidators(): Promise<EthAddress[]>;
46
79
  isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
47
80
  filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
81
+ getL1Constants(): L1RollupConstants;
82
+ getLagInEpochsForValidatorSet(): number;
48
83
  }
49
84
 
50
85
  /**
@@ -57,8 +92,11 @@ export interface EpochCacheInterface {
57
92
  * Note: This class is very dependent on the system clock being in sync.
58
93
  */
59
94
  export class EpochCache implements EpochCacheInterface {
60
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
61
- protected cache: Map<EpochNumber, EpochCommitteeInfo> = new Map();
95
+ /**
96
+ * Single map holding both resolved entries and in-flight promises.
97
+ * A `Promise` value means a fetch is in progress; concurrent callers await it.
98
+ */
99
+ protected cache: Map<EpochNumber, CachedEpochEntry | Promise<CachedEpochEntry>> = new Map();
62
100
  private allValidators: Set<string> = new Set();
63
101
  private lastValidatorRefresh = 0;
64
102
  private readonly log: Logger = createLogger('epoch-cache');
@@ -92,7 +130,7 @@ export class EpochCache implements EpochCacheInterface {
92
130
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
93
131
  const publicClient = createPublicClient({
94
132
  chain: chain.chainInfo,
95
- transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
133
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
96
134
  pollingInterval: config.viemPollingIntervalMS,
97
135
  });
98
136
  rollup = new RollupContract(publicClient, rollupOrAddress.toString());
@@ -106,6 +144,8 @@ export class EpochCache implements EpochCacheInterface {
106
144
  epochDuration,
107
145
  lagInEpochsForValidatorSet,
108
146
  lagInEpochsForRandao,
147
+ targetCommitteeSize,
148
+ rollupManaLimit,
109
149
  ] = await Promise.all([
110
150
  rollup.getL1StartBlock(),
111
151
  rollup.getL1GenesisTime(),
@@ -114,6 +154,8 @@ export class EpochCache implements EpochCacheInterface {
114
154
  rollup.getEpochDuration(),
115
155
  rollup.getLagInEpochsForValidatorSet(),
116
156
  rollup.getLagInEpochsForRandao(),
157
+ rollup.getTargetCommitteeSize(),
158
+ rollup.getManaLimit(),
117
159
  ] as const);
118
160
 
119
161
  const l1RollupConstants = {
@@ -125,43 +167,78 @@ export class EpochCache implements EpochCacheInterface {
125
167
  ethereumSlotDuration: config.ethereumSlotDuration,
126
168
  lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
127
169
  lagInEpochsForRandao: Number(lagInEpochsForRandao),
170
+ targetCommitteeSize: Number(targetCommitteeSize),
171
+ rollupManaLimit: Number(rollupManaLimit),
128
172
  };
129
173
 
130
- return new EpochCache(rollup, l1RollupConstants, deps.dateProvider);
174
+ return new EpochCache(rollup, l1RollupConstants, deps.dateProvider, {
175
+ cacheSize: 12,
176
+ validatorRefreshIntervalSeconds: 60,
177
+ });
131
178
  }
132
179
 
133
180
  public getL1Constants(): L1RollupConstants {
134
181
  return this.l1constants;
135
182
  }
136
183
 
184
+ /**
185
+ * The number of epochs by which validator-set sampling lags. The committee for epoch `E` samples the
186
+ * validator set as of `lagInEpochsForValidatorSet` epochs before `E` (see
187
+ * `ValidatorSelectionLib.stableEpochToValidatorSetSampleTime` on L1), so a newly staked validator only
188
+ * becomes eligible for a committee this many epochs after staking.
189
+ */
190
+ public getLagInEpochsForValidatorSet(): number {
191
+ return this.l1constants.lagInEpochsForValidatorSet;
192
+ }
193
+
194
+ public getSlotNow(): SlotNumber {
195
+ return this.getEpochAndSlotNow().slot;
196
+ }
197
+
198
+ public getTargetSlot(): SlotNumber {
199
+ const slotNow = this.getSlotNow();
200
+ const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
201
+ return SlotNumber(slotNow + offset);
202
+ }
203
+
204
+ public getEpochNow(): EpochNumber {
205
+ return this.getEpochAndSlotNow().epoch;
206
+ }
207
+
208
+ public getTargetEpoch(): EpochNumber {
209
+ return getEpochAtSlot(this.getTargetSlot(), this.l1constants);
210
+ }
211
+
137
212
  public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
138
213
  const nowMs = BigInt(this.dateProvider.now());
139
214
  const nowSeconds = nowMs / 1000n;
140
215
  return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
141
216
  }
142
217
 
143
- public nowInSeconds(): bigint {
144
- return BigInt(Math.floor(this.dateProvider.now() / 1000));
218
+ private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
219
+ return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
145
220
  }
146
221
 
147
- private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
148
- const epoch = getEpochAtSlot(slot, this.l1constants);
149
- const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
150
- return { epoch, ts, slot };
222
+ public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
223
+ const nowSeconds = this.dateProvider.nowInSeconds();
224
+ const nextSlotTs = getNextL1SlotTimestamp(nowSeconds, this.l1constants);
225
+ return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), nowSeconds: BigInt(nowSeconds) };
151
226
  }
152
227
 
153
- public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
154
- const now = this.nowInSeconds();
155
- const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
156
- return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), now };
228
+ public getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
229
+ const result = this.getEpochAndSlotInNextL1Slot();
230
+ const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
231
+ const targetSlot = SlotNumber(result.slot + offset);
232
+ return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1constants) };
157
233
  }
158
234
 
159
235
  private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
160
236
  const slot = getSlotAtTimestamp(ts, this.l1constants);
237
+ const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
161
238
  return {
162
- epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
163
- ts: getTimestampForSlot(slot, this.l1constants),
164
239
  slot,
240
+ epoch,
241
+ ts: getTimestampForSlot(slot, this.l1constants),
165
242
  };
166
243
  }
167
244
 
@@ -170,17 +247,8 @@ export class EpochCache implements EpochCacheInterface {
170
247
  return this.getCommittee(startSlot);
171
248
  }
172
249
 
173
- /**
174
- * Returns whether the escape hatch is open for the given epoch.
175
- *
176
- * Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
177
- * the epoch committee info (which includes the escape hatch flag) and return it.
178
- */
250
+ /** Returns whether the escape hatch is open for the given epoch. */
179
251
  public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
180
- const cached = this.cache.get(epoch);
181
- if (cached) {
182
- return cached.isEscapeHatchOpen;
183
- }
184
252
  const info = await this.getCommitteeForEpoch(epoch);
185
253
  return info.isEscapeHatchOpen;
186
254
  }
@@ -194,7 +262,7 @@ export class EpochCache implements EpochCacheInterface {
194
262
  public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
195
263
  const epoch =
196
264
  slot === 'now'
197
- ? this.getEpochAndSlotNow().epoch
265
+ ? this.getEpochNow()
198
266
  : slot === 'next'
199
267
  ? this.getEpochAndSlotInNextL1Slot().epoch
200
268
  : getEpochAtSlot(slot, this.l1constants);
@@ -203,33 +271,52 @@ export class EpochCache implements EpochCacheInterface {
203
271
  }
204
272
 
205
273
  /**
206
- * Get the current validator set
207
- * @param nextSlot - If true, get the validator set for the next slot.
208
- * @returns The current validator set.
274
+ * Get the current validator set.
275
+ *
276
+ * Returns cached data if the entry is finalized or still fresh (queried less than one
277
+ * Ethereum slot ago). Stale non-finalized entries are re-queried, and concurrent callers
278
+ * coalesce on the same in-flight promise so the L1 query happens only once.
209
279
  */
210
280
  public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
211
281
  const { epoch, ts } = this.getEpochAndTimestamp(slot);
212
282
 
213
- if (this.cache.has(epoch)) {
214
- return this.cache.get(epoch)!;
283
+ const cached = this.cache.get(epoch);
284
+
285
+ // In-flight promise: another caller is already fetching this epoch — just await it.
286
+ if (cached instanceof Promise) {
287
+ return (await cached).data;
215
288
  }
216
289
 
217
- const epochData = await this.computeCommittee({ epoch, ts });
218
- // If the committee size is 0 or undefined, then do not cache
219
- if (!epochData.committee || epochData.committee.length === 0) {
220
- return epochData;
290
+ // Resolved entry: return it if finalized or still fresh.
291
+ if (cached && (cached.finalized || !this.isStale(cached))) {
292
+ return cached.data;
221
293
  }
222
- this.cache.set(epoch, epochData);
223
294
 
224
- const toPurge = Array.from(this.cache.keys())
225
- .sort((a, b) => Number(b - a))
226
- .slice(this.config.cacheSize);
227
- toPurge.forEach(key => this.cache.delete(key));
295
+ // Stale non-finalized entry: do a lightweight refresh first (check block hash + finalized ts).
296
+ // Only fall back to a full re-fetch if the L1 block was reorged.
297
+ if (cached) {
298
+ const promise = this.refreshStaleEntry(cached, epoch, ts);
299
+ this.cache.set(epoch, promise);
300
+ try {
301
+ return (await promise).data;
302
+ } catch (err) {
303
+ this.cache.set(epoch, cached);
304
+ throw err;
305
+ }
306
+ }
228
307
 
229
- return epochData;
308
+ // No entry at all: full fetch.
309
+ const promise = this.fetchAndCache(epoch, ts);
310
+ this.cache.set(epoch, promise);
311
+ try {
312
+ return (await promise).data;
313
+ } catch (err) {
314
+ this.cache.delete(epoch);
315
+ throw err;
316
+ }
230
317
  }
231
318
 
232
- private getEpochAndTimestamp(slot: SlotTag = 'now') {
319
+ private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
233
320
  if (slot === 'now') {
234
321
  return this.getEpochAndSlotNow();
235
322
  } else if (slot === 'next') {
@@ -239,22 +326,140 @@ export class EpochCache implements EpochCacheInterface {
239
326
  }
240
327
  }
241
328
 
242
- private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
243
- const { ts, epoch } = when;
244
- const [committee, seedBuffer, l1Timestamp, isEscapeHatchOpen] = await Promise.all([
329
+ /** Evicts oldest cache entries (resolved or in-flight) beyond cacheSize. */
330
+ private purgeCache(): void {
331
+ if (this.cache.size <= this.config.cacheSize) {
332
+ return;
333
+ }
334
+ const toPurge = Array.from(this.cache.keys())
335
+ .sort((a, b) => Number(b - a))
336
+ .slice(this.config.cacheSize);
337
+ toPurge.forEach(key => this.cache.delete(key));
338
+ }
339
+
340
+ /** Returns true if a non-finalized cache entry is older than one Ethereum slot. */
341
+ private isStale(entry: CachedEpochEntry): boolean {
342
+ const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
343
+ return nowSeconds - entry.lastRefreshL1Timestamp >= BigInt(this.l1constants.ethereumSlotDuration);
344
+ }
345
+
346
+ /** Whether a cached epoch entry has been marked as finalized. Returns undefined if not cached or still in-flight. */
347
+ public isFinalized(epoch: EpochNumber): boolean | undefined {
348
+ const entry = this.cache.get(epoch);
349
+ if (!entry || entry instanceof Promise) {
350
+ return undefined;
351
+ }
352
+ return entry.finalized;
353
+ }
354
+
355
+ /** Returns the latest L1 timestamp stored in the cached entry. Undefined if not cached or in-flight. */
356
+ public getCachedLastRefreshL1Timestamp(epoch: EpochNumber): bigint | undefined {
357
+ const entry = this.cache.get(epoch);
358
+ if (!entry || entry instanceof Promise) {
359
+ return undefined;
360
+ }
361
+ return entry.lastRefreshL1Timestamp;
362
+ }
363
+
364
+ /** Computes the sampling timestamp for an epoch's committee data. */
365
+ private getSamplingTimestamp(epoch: EpochNumber): bigint {
366
+ const { lagInEpochsForRandao, epochDuration, slotDuration } = this.l1constants;
367
+ const epochStartTs = getStartTimestampForEpoch(epoch, this.l1constants);
368
+ return epochStartTs - BigInt(lagInEpochsForRandao) * BigInt(epochDuration) * BigInt(slotDuration);
369
+ }
370
+
371
+ /**
372
+ * Lightweight refresh for a stale non-finalized entry. Queries only the block hash at
373
+ * the original block number and the finalized block timestamp — avoids the expensive
374
+ * getCommitteeAt and getSampleSeedAt calls on the rollup contract.
375
+ *
376
+ * If the block hash still matches (no L1 reorg), we keep the existing data and just
377
+ * update the provenance timestamp. If the finalized block has caught up, we promote the
378
+ * entry to finalized. If there was a reorg (hash mismatch), we fall back to a full fetch.
379
+ */
380
+ private async refreshStaleEntry(stale: CachedEpochEntry, epoch: EpochNumber, ts: bigint): Promise<CachedEpochEntry> {
381
+ const [blockAtOriginal, l1FinalizedBlock, latestBlock] = await Promise.all([
382
+ this.rollup.client.getBlock({ blockNumber: stale.lastQueryL1BlockNumber, includeTransactions: false }),
383
+ getFinalizedL1Block(this.rollup.client),
384
+ this.rollup.client.getBlock({ includeTransactions: false }),
385
+ ]);
386
+
387
+ if (blockAtOriginal.hash === stale.lastQueryL1BlockHash) {
388
+ // No reorg: the data is still valid. Check if we can now mark it as finalized.
389
+ const samplingTs = this.getSamplingTimestamp(epoch);
390
+ const finalized =
391
+ !!(stale.data.committee && stale.data.committee.length > 0) &&
392
+ l1FinalizedBlock !== undefined &&
393
+ samplingTs <= l1FinalizedBlock.timestamp;
394
+
395
+ const refreshed: CachedEpochEntry = {
396
+ ...stale,
397
+ lastRefreshL1Timestamp: latestBlock.timestamp,
398
+ finalized,
399
+ };
400
+ this.cache.set(epoch, refreshed);
401
+ return refreshed;
402
+ }
403
+
404
+ // Reorg detected: block hash mismatch. Do a full re-fetch.
405
+ // Pass the already-fetched block timestamps to avoid redundant queries.
406
+ this.log.warn(`L1 reorg detected for epoch ${epoch}: block ${stale.lastQueryL1BlockNumber} hash changed`, {
407
+ epoch,
408
+ expectedHash: stale.lastQueryL1BlockHash,
409
+ actualHash: blockAtOriginal.hash,
410
+ });
411
+ return this.fetchAndCache(epoch, ts, { latestBlock, finalizedBlock: l1FinalizedBlock });
412
+ }
413
+
414
+ /**
415
+ * Fetches committee data from L1, determines finalization status, and stores in the cache.
416
+ *
417
+ * Uses `lagInEpochsForRandao` (the binding constraint, always <= lagInEpochsForValidatorSet)
418
+ * and computes the sampling timestamp from the epoch start to match the L1 contract's logic.
419
+ *
420
+ * When called from refreshStaleEntry after a reorg, the latest and finalized blocks are
421
+ * passed in to avoid redundant L1 queries.
422
+ */
423
+ private async fetchAndCache(
424
+ epoch: EpochNumber,
425
+ ts: bigint,
426
+ prefetched?: { latestBlock: L1BlockInfo; finalizedBlock: { timestamp: bigint } | undefined },
427
+ ): Promise<CachedEpochEntry> {
428
+ const [committee, seedBuffer, latestBlock, finalizedBlock, isEscapeHatchOpen] = await Promise.all([
245
429
  this.rollup.getCommitteeAt(ts),
246
430
  this.rollup.getSampleSeedAt(ts),
247
- this.rollup.client.getBlock({ includeTransactions: false }).then(b => b.timestamp),
431
+ prefetched?.latestBlock ?? this.rollup.client.getBlock({ includeTransactions: false }),
432
+ prefetched !== undefined ? prefetched.finalizedBlock : getFinalizedL1Block(this.rollup.client),
248
433
  this.rollup.isEscapeHatchOpen(epoch),
249
434
  ]);
250
- const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
251
- const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
252
- if (ts - sub > l1Timestamp) {
435
+
436
+ const samplingTs = this.getSamplingTimestamp(epoch);
437
+
438
+ if (samplingTs > latestBlock.timestamp) {
253
439
  throw new Error(
254
- `Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`,
440
+ `Cannot query committee for future epoch ${epoch}: ` +
441
+ `sampling timestamp ${samplingTs} is beyond latest L1 block at ${latestBlock.timestamp}. ` +
442
+ `Check your Ethereum node is synced.`,
255
443
  );
256
444
  }
257
- return { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
445
+
446
+ // Empty committees are never marked finalized so they always get re-queried after TTL.
447
+ // If L1 has no finalized block yet (devnet startup), entries stay unfinalized.
448
+ const hasCommittee = !!(committee && committee.length > 0);
449
+ const finalized = hasCommittee && finalizedBlock !== undefined && samplingTs <= finalizedBlock.timestamp;
450
+ const data: EpochCommitteeInfo = { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
451
+ const entry: CachedEpochEntry = {
452
+ data,
453
+ lastQueryL1BlockNumber: latestBlock.number!,
454
+ lastQueryL1BlockHash: latestBlock.hash!,
455
+ lastRefreshL1Timestamp: latestBlock.timestamp,
456
+ finalized,
457
+ };
458
+
459
+ this.cache.set(epoch, entry);
460
+ this.purgeCache();
461
+
462
+ return entry;
258
463
  }
259
464
 
260
465
  /**
@@ -279,17 +484,31 @@ export class EpochCache implements EpochCacheInterface {
279
484
  return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
280
485
  }
281
486
 
282
- /** Returns the current and next L2 slot numbers. */
487
+ /** Returns the current and next L2 slot in next eth L1 Slot. */
283
488
  public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
284
- const current = this.getEpochAndSlotNow();
489
+ const currentSlot = this.getSlotNow();
285
490
  const next = this.getEpochAndSlotInNextL1Slot();
286
491
 
287
492
  return {
288
- currentSlot: current.slot,
493
+ currentSlot,
289
494
  nextSlot: next.slot,
290
495
  };
291
496
  }
292
497
 
498
+ /** Returns the target and next L2 slot in the next L1 slot. */
499
+ public getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
500
+ const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
501
+ const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
502
+
503
+ const currentSlot = getSlotAtTimestamp(nowSeconds, this.l1constants);
504
+ const targetSlot = SlotNumber(currentSlot + offset);
505
+
506
+ const nextL2SlotOnL1 = getSlotAtNextL1Block(nowSeconds, this.l1constants);
507
+ const nextSlot = SlotNumber(nextL2SlotOnL1 + offset);
508
+
509
+ return { targetSlot, nextSlot };
510
+ }
511
+
293
512
  /**
294
513
  * Get the proposer attester address in the given L2 slot
295
514
  * @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
@@ -368,10 +587,11 @@ export class EpochCache implements EpochCacheInterface {
368
587
  async getRegisteredValidators(): Promise<EthAddress[]> {
369
588
  const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
370
589
  const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
371
- if (validatorRefreshTime < this.dateProvider.now()) {
372
- const currentSet = await this.rollup.getAttesters();
590
+ const now = this.dateProvider.now();
591
+ if (validatorRefreshTime < now) {
592
+ const currentSet = await this.rollup.getAttesters(BigInt(Math.floor(now / 1000)));
373
593
  this.allValidators = new Set(currentSet.map(v => v.toString()));
374
- this.lastValidatorRefresh = this.dateProvider.now();
594
+ this.lastValidatorRefresh = now;
375
595
  }
376
596
  return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
377
597
  }
@@ -1,9 +1,20 @@
1
1
  import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
2
2
  import { EthAddress } from '@aztec/foundation/eth-address';
3
3
  import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
4
- import { getEpochAtSlot, getSlotAtTimestamp, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
5
-
6
- import type { EpochAndSlot, EpochCacheInterface, EpochCommitteeInfo, SlotTag } from '../epoch_cache.js';
4
+ import {
5
+ getEpochAtSlot,
6
+ getSlotAtTimestamp,
7
+ getTimestampForSlot,
8
+ getTimestampRangeForEpoch,
9
+ } from '@aztec/stdlib/epoch-helpers';
10
+
11
+ import {
12
+ type EpochAndSlot,
13
+ type EpochCacheInterface,
14
+ type EpochCommitteeInfo,
15
+ PROPOSER_PIPELINING_SLOT_OFFSET,
16
+ type SlotTag,
17
+ } from '../epoch_cache.js';
7
18
 
8
19
  /** Default L1 constants for testing. */
9
20
  const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
@@ -13,6 +24,8 @@ const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
13
24
  epochDuration: 16,
14
25
  ethereumSlotDuration: 12,
15
26
  proofSubmissionEpochs: 2,
27
+ targetCommitteeSize: 48,
28
+ rollupManaLimit: Number.MAX_SAFE_INTEGER,
16
29
  };
17
30
 
18
31
  /**
@@ -30,11 +43,17 @@ export class TestEpochCache implements EpochCacheInterface {
30
43
  private seed: bigint = 0n;
31
44
  private registeredValidators: EthAddress[] = [];
32
45
  private l1Constants: L1RollupConstants;
46
+ private lagInEpochsForValidatorSet = 2;
33
47
 
34
48
  constructor(l1Constants: Partial<L1RollupConstants> = {}) {
35
49
  this.l1Constants = { ...DEFAULT_L1_CONSTANTS, ...l1Constants };
36
50
  }
37
51
 
52
+ setLagInEpochsForValidatorSet(lag: number): this {
53
+ this.lagInEpochsForValidatorSet = lag;
54
+ return this;
55
+ }
56
+
38
57
  /**
39
58
  * Sets the committee members. Used in validation and attestation flows.
40
59
  * @param committee - Array of committee member addresses.
@@ -102,6 +121,10 @@ export class TestEpochCache implements EpochCacheInterface {
102
121
  return this.l1Constants;
103
122
  }
104
123
 
124
+ getLagInEpochsForValidatorSet(): number {
125
+ return this.lagInEpochsForValidatorSet;
126
+ }
127
+
105
128
  getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo> {
106
129
  const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
107
130
  return Promise.resolve({
@@ -112,19 +135,55 @@ export class TestEpochCache implements EpochCacheInterface {
112
135
  });
113
136
  }
114
137
 
138
+ getSlotNow(): SlotNumber {
139
+ return this.currentSlot;
140
+ }
141
+
142
+ getTargetSlot(): SlotNumber {
143
+ return SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET);
144
+ }
145
+
146
+ getEpochNow(): EpochNumber {
147
+ return getEpochAtSlot(this.currentSlot, this.l1Constants);
148
+ }
149
+
150
+ getTargetEpoch(): EpochNumber {
151
+ return getEpochAtSlot(this.getTargetSlot(), this.l1Constants);
152
+ }
153
+
115
154
  getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
116
- const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
117
- const ts = getTimestampRangeForEpoch(epoch, this.l1Constants)[0];
118
- return { epoch, slot: this.currentSlot, ts, nowMs: ts * 1000n };
155
+ // Model "now" as the start of the current slot (mirroring the real EpochCache, which derives nowMs
156
+ // from the wall clock). Using the slot start rather than the epoch start keeps nowMs consistent with
157
+ // currentSlot, which the pipelining receive-window check (clock_tolerance) relies on.
158
+ const epochNow = getEpochAtSlot(this.currentSlot, this.l1Constants);
159
+ const ts = getTimestampForSlot(this.currentSlot, this.l1Constants);
160
+ return {
161
+ epoch: epochNow,
162
+ slot: this.currentSlot,
163
+ ts,
164
+ nowMs: ts * 1000n,
165
+ };
119
166
  }
120
167
 
121
- getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
122
- const now = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
123
- const nextSlotTs = now + BigInt(this.l1Constants.ethereumSlotDuration);
168
+ getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
169
+ const nowTs = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
170
+ const nextSlotTs = nowTs + BigInt(this.l1Constants.ethereumSlotDuration);
124
171
  const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
125
- const epoch = getEpochAtSlot(nextSlot, this.l1Constants);
126
- const ts = getTimestampRangeForEpoch(epoch, this.l1Constants)[0];
127
- return { epoch, slot: nextSlot, ts, now };
172
+ const epochNow = getEpochAtSlot(nextSlot, this.l1Constants);
173
+ const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
174
+ return {
175
+ epoch: epochNow,
176
+ slot: nextSlot,
177
+ ts,
178
+ nowSeconds: nowTs,
179
+ };
180
+ }
181
+
182
+ getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
183
+ const result = this.getEpochAndSlotInNextL1Slot();
184
+ const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
185
+ const targetSlot = SlotNumber(result.slot + offset);
186
+ return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1Constants) };
128
187
  }
129
188
 
130
189
  getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
@@ -140,9 +199,22 @@ export class TestEpochCache implements EpochCacheInterface {
140
199
  }
141
200
 
142
201
  getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
202
+ const currentSlot = this.getSlotNow();
203
+ const next = this.getEpochAndSlotInNextL1Slot();
204
+
143
205
  return {
144
- currentSlot: this.currentSlot,
145
- nextSlot: SlotNumber(this.currentSlot + 1),
206
+ currentSlot,
207
+ nextSlot: next.slot,
208
+ };
209
+ }
210
+
211
+ getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
212
+ const targetSlot = this.getTargetSlot();
213
+ const next = this.getTargetEpochAndSlotInNextL1Slot();
214
+
215
+ return {
216
+ targetSlot,
217
+ nextSlot: next.slot,
146
218
  };
147
219
  }
148
220
 
@@ -163,6 +235,10 @@ export class TestEpochCache implements EpochCacheInterface {
163
235
  return Promise.resolve(validators.filter(v => committeeSet.has(v.toString())));
164
236
  }
165
237
 
238
+ isEscapeHatchOpen(_epoch: EpochNumber): Promise<boolean> {
239
+ return Promise.resolve(this.escapeHatchOpen);
240
+ }
241
+
166
242
  isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean> {
167
243
  return Promise.resolve(this.escapeHatchOpen);
168
244
  }