@aztec/epoch-cache 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f

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.
@@ -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
+ /** When proposer pipelining is enabled, the proposer builds 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
 
@@ -28,25 +36,51 @@ export type EpochCommitteeInfo = {
28
36
  committee: EthAddress[] | undefined;
29
37
  seed: bigint;
30
38
  epoch: EpochNumber;
39
+ /** True if the epoch is within an open escape hatch window. */
40
+ isEscapeHatchOpen: boolean;
31
41
  };
32
42
 
33
43
  export type SlotTag = 'now' | 'next' | SlotNumber;
34
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
+
35
61
  export interface EpochCacheInterface {
36
62
  getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
37
- getEpochAndSlotNow(): EpochAndSlot;
38
- getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint };
63
+ getSlotNow(): SlotNumber;
64
+ getTargetSlot(): SlotNumber;
65
+ getEpochNow(): EpochNumber;
66
+ getTargetEpoch(): EpochNumber;
67
+ getEpochAndSlotNow(): EpochAndSlot & { nowMs: 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
+ isProposerPipeliningEnabled(): boolean;
72
+ pipeliningOffset(): number;
73
+ isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean>;
74
+ isEscapeHatchOpenAtSlot(slot: SlotTag): Promise<boolean>;
39
75
  getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
40
76
  computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
41
- getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
42
- currentProposer: EthAddress | undefined;
43
- nextProposer: EthAddress | undefined;
44
- currentSlot: SlotNumber;
45
- nextSlot: SlotNumber;
46
- }>;
77
+ getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
78
+ getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber };
79
+ getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
47
80
  getRegisteredValidators(): Promise<EthAddress[]>;
48
81
  isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
49
82
  filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
83
+ getL1Constants(): L1RollupConstants;
50
84
  }
51
85
 
52
86
  /**
@@ -59,12 +93,17 @@ export interface EpochCacheInterface {
59
93
  * Note: This class is very dependent on the system clock being in sync.
60
94
  */
61
95
  export class EpochCache implements EpochCacheInterface {
62
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
63
- protected cache: Map<EpochNumber, EpochCommitteeInfo> = new Map();
96
+ /**
97
+ * Single map holding both resolved entries and in-flight promises.
98
+ * A `Promise` value means a fetch is in progress; concurrent callers await it.
99
+ */
100
+ protected cache: Map<EpochNumber, CachedEpochEntry | Promise<CachedEpochEntry>> = new Map();
64
101
  private allValidators: Set<string> = new Set();
65
102
  private lastValidatorRefresh = 0;
66
103
  private readonly log: Logger = createLogger('epoch-cache');
67
104
 
105
+ protected enableProposerPipelining: boolean;
106
+
68
107
  constructor(
69
108
  private rollup: RollupContract,
70
109
  private readonly l1constants: L1RollupConstants & {
@@ -72,10 +111,12 @@ export class EpochCache implements EpochCacheInterface {
72
111
  lagInEpochsForRandao: number;
73
112
  },
74
113
  private readonly dateProvider: DateProvider = new DateProvider(),
75
- protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
114
+ protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60, enableProposerPipelining: false },
76
115
  ) {
116
+ this.enableProposerPipelining = this.config.enableProposerPipelining;
77
117
  this.log.debug(`Initialized EpochCache`, {
78
118
  l1constants,
119
+ enableProposerPipelining: this.enableProposerPipelining,
79
120
  });
80
121
  }
81
122
 
@@ -94,7 +135,7 @@ export class EpochCache implements EpochCacheInterface {
94
135
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
95
136
  const publicClient = createPublicClient({
96
137
  chain: chain.chainInfo,
97
- transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
138
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
98
139
  pollingInterval: config.viemPollingIntervalMS,
99
140
  });
100
141
  rollup = new RollupContract(publicClient, rollupOrAddress.toString());
@@ -108,6 +149,8 @@ export class EpochCache implements EpochCacheInterface {
108
149
  epochDuration,
109
150
  lagInEpochsForValidatorSet,
110
151
  lagInEpochsForRandao,
152
+ targetCommitteeSize,
153
+ rollupManaLimit,
111
154
  ] = await Promise.all([
112
155
  rollup.getL1StartBlock(),
113
156
  rollup.getL1GenesisTime(),
@@ -116,6 +159,8 @@ export class EpochCache implements EpochCacheInterface {
116
159
  rollup.getEpochDuration(),
117
160
  rollup.getLagInEpochsForValidatorSet(),
118
161
  rollup.getLagInEpochsForRandao(),
162
+ rollup.getTargetCommitteeSize(),
163
+ rollup.getManaLimit(),
119
164
  ] as const);
120
165
 
121
166
  const l1RollupConstants = {
@@ -127,42 +172,81 @@ export class EpochCache implements EpochCacheInterface {
127
172
  ethereumSlotDuration: config.ethereumSlotDuration,
128
173
  lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
129
174
  lagInEpochsForRandao: Number(lagInEpochsForRandao),
175
+ targetCommitteeSize: Number(targetCommitteeSize),
176
+ rollupManaLimit: Number(rollupManaLimit),
130
177
  };
131
178
 
132
- return new EpochCache(rollup, l1RollupConstants, deps.dateProvider);
179
+ return new EpochCache(rollup, l1RollupConstants, deps.dateProvider, {
180
+ cacheSize: 12,
181
+ validatorRefreshIntervalSeconds: 60,
182
+ enableProposerPipelining: config.enableProposerPipelining,
183
+ });
133
184
  }
134
185
 
135
186
  public getL1Constants(): L1RollupConstants {
136
187
  return this.l1constants;
137
188
  }
138
189
 
139
- public getEpochAndSlotNow(): EpochAndSlot & { now: bigint } {
140
- const now = this.nowInSeconds();
141
- return { ...this.getEpochAndSlotAtTimestamp(now), now };
190
+ public isProposerPipeliningEnabled(): boolean {
191
+ return this.enableProposerPipelining;
192
+ }
193
+
194
+ public pipeliningOffset(): number {
195
+ return this.enableProposerPipelining ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
142
196
  }
143
197
 
144
- public nowInSeconds(): bigint {
145
- return BigInt(Math.floor(this.dateProvider.now() / 1000));
198
+ public getSlotNow(): SlotNumber {
199
+ return this.getEpochAndSlotNow().slot;
200
+ }
201
+
202
+ public getTargetSlot(): SlotNumber {
203
+ const slotNow = this.getSlotNow();
204
+ const offset = this.isProposerPipeliningEnabled() ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
205
+ return SlotNumber(slotNow + offset);
206
+ }
207
+
208
+ public getEpochNow(): EpochNumber {
209
+ return this.getEpochAndSlotNow().epoch;
210
+ }
211
+
212
+ public getTargetEpoch(): EpochNumber {
213
+ return getEpochAtSlot(this.getTargetSlot(), this.l1constants);
214
+ }
215
+
216
+ public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
217
+ const nowMs = BigInt(this.dateProvider.now());
218
+ const nowSeconds = nowMs / 1000n;
219
+ return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
146
220
  }
147
221
 
148
222
  private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
149
- const epoch = getEpochAtSlot(slot, this.l1constants);
150
- const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
151
- return { epoch, ts, slot };
223
+ return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
152
224
  }
153
225
 
154
- public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
155
- const now = this.nowInSeconds();
156
- const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
157
- return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), now };
226
+ public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
227
+ const nowSeconds = this.dateProvider.nowInSeconds();
228
+ const nextSlotTs = getNextL1SlotTimestamp(nowSeconds, this.l1constants);
229
+ return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), nowSeconds: BigInt(nowSeconds) };
230
+ }
231
+
232
+ public getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
233
+ if (!this.isProposerPipeliningEnabled()) {
234
+ return this.getEpochAndSlotInNextL1Slot();
235
+ }
236
+
237
+ const result = this.getEpochAndSlotInNextL1Slot();
238
+ const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
239
+ const targetSlot = SlotNumber(result.slot + offset);
240
+ return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1constants) };
158
241
  }
159
242
 
160
243
  private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
161
244
  const slot = getSlotAtTimestamp(ts, this.l1constants);
245
+ const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
162
246
  return {
163
- epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
164
- ts: getTimestampForSlot(slot, this.l1constants),
165
247
  slot,
248
+ epoch,
249
+ ts: getTimestampForSlot(slot, this.l1constants),
166
250
  };
167
251
  }
168
252
 
@@ -171,34 +255,76 @@ export class EpochCache implements EpochCacheInterface {
171
255
  return this.getCommittee(startSlot);
172
256
  }
173
257
 
258
+ /** Returns whether the escape hatch is open for the given epoch. */
259
+ public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
260
+ const info = await this.getCommitteeForEpoch(epoch);
261
+ return info.isEscapeHatchOpen;
262
+ }
263
+
264
+ /**
265
+ * Returns whether the escape hatch is open for the epoch containing the given slot.
266
+ *
267
+ * This is a lightweight helper intended for callers that already have a slot number and only
268
+ * need the escape hatch flag (without pulling full committee info).
269
+ */
270
+ public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
271
+ const epoch =
272
+ slot === 'now'
273
+ ? this.getEpochNow()
274
+ : slot === 'next'
275
+ ? this.getEpochAndSlotInNextL1Slot().epoch
276
+ : getEpochAtSlot(slot, this.l1constants);
277
+
278
+ return await this.isEscapeHatchOpen(epoch);
279
+ }
280
+
174
281
  /**
175
- * Get the current validator set
176
- * @param nextSlot - If true, get the validator set for the next slot.
177
- * @returns The current validator set.
282
+ * Get the current validator set.
283
+ *
284
+ * Returns cached data if the entry is finalized or still fresh (queried less than one
285
+ * Ethereum slot ago). Stale non-finalized entries are re-queried, and concurrent callers
286
+ * coalesce on the same in-flight promise so the L1 query happens only once.
178
287
  */
179
288
  public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
180
289
  const { epoch, ts } = this.getEpochAndTimestamp(slot);
181
290
 
182
- if (this.cache.has(epoch)) {
183
- return this.cache.get(epoch)!;
291
+ const cached = this.cache.get(epoch);
292
+
293
+ // In-flight promise: another caller is already fetching this epoch — just await it.
294
+ if (cached instanceof Promise) {
295
+ return (await cached).data;
184
296
  }
185
297
 
186
- const epochData = await this.computeCommittee({ epoch, ts });
187
- // If the committee size is 0 or undefined, then do not cache
188
- if (!epochData.committee || epochData.committee.length === 0) {
189
- return epochData;
298
+ // Resolved entry: return it if finalized or still fresh.
299
+ if (cached && (cached.finalized || !this.isStale(cached))) {
300
+ return cached.data;
190
301
  }
191
- this.cache.set(epoch, epochData);
192
302
 
193
- const toPurge = Array.from(this.cache.keys())
194
- .sort((a, b) => Number(b - a))
195
- .slice(this.config.cacheSize);
196
- toPurge.forEach(key => this.cache.delete(key));
303
+ // Stale non-finalized entry: do a lightweight refresh first (check block hash + finalized ts).
304
+ // Only fall back to a full re-fetch if the L1 block was reorged.
305
+ if (cached) {
306
+ const promise = this.refreshStaleEntry(cached, epoch, ts);
307
+ this.cache.set(epoch, promise);
308
+ try {
309
+ return (await promise).data;
310
+ } catch (err) {
311
+ this.cache.set(epoch, cached);
312
+ throw err;
313
+ }
314
+ }
197
315
 
198
- return epochData;
316
+ // No entry at all: full fetch.
317
+ const promise = this.fetchAndCache(epoch, ts);
318
+ this.cache.set(epoch, promise);
319
+ try {
320
+ return (await promise).data;
321
+ } catch (err) {
322
+ this.cache.delete(epoch);
323
+ throw err;
324
+ }
199
325
  }
200
326
 
201
- private getEpochAndTimestamp(slot: SlotTag = 'now') {
327
+ private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
202
328
  if (slot === 'now') {
203
329
  return this.getEpochAndSlotNow();
204
330
  } else if (slot === 'next') {
@@ -208,22 +334,140 @@ export class EpochCache implements EpochCacheInterface {
208
334
  }
209
335
  }
210
336
 
211
- private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
212
- const { ts, epoch } = when;
213
- const [committeeHex, seed, l1Timestamp] = await Promise.all([
337
+ /** Evicts oldest cache entries (resolved or in-flight) beyond cacheSize. */
338
+ private purgeCache(): void {
339
+ if (this.cache.size <= this.config.cacheSize) {
340
+ return;
341
+ }
342
+ const toPurge = Array.from(this.cache.keys())
343
+ .sort((a, b) => Number(b - a))
344
+ .slice(this.config.cacheSize);
345
+ toPurge.forEach(key => this.cache.delete(key));
346
+ }
347
+
348
+ /** Returns true if a non-finalized cache entry is older than one Ethereum slot. */
349
+ private isStale(entry: CachedEpochEntry): boolean {
350
+ const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
351
+ return nowSeconds - entry.lastRefreshL1Timestamp >= BigInt(this.l1constants.ethereumSlotDuration);
352
+ }
353
+
354
+ /** Whether a cached epoch entry has been marked as finalized. Returns undefined if not cached or still in-flight. */
355
+ public isFinalized(epoch: EpochNumber): boolean | undefined {
356
+ const entry = this.cache.get(epoch);
357
+ if (!entry || entry instanceof Promise) {
358
+ return undefined;
359
+ }
360
+ return entry.finalized;
361
+ }
362
+
363
+ /** Returns the latest L1 timestamp stored in the cached entry. Undefined if not cached or in-flight. */
364
+ public getCachedLastRefreshL1Timestamp(epoch: EpochNumber): bigint | undefined {
365
+ const entry = this.cache.get(epoch);
366
+ if (!entry || entry instanceof Promise) {
367
+ return undefined;
368
+ }
369
+ return entry.lastRefreshL1Timestamp;
370
+ }
371
+
372
+ /** Computes the sampling timestamp for an epoch's committee data. */
373
+ private getSamplingTimestamp(epoch: EpochNumber): bigint {
374
+ const { lagInEpochsForRandao, epochDuration, slotDuration } = this.l1constants;
375
+ const epochStartTs = getStartTimestampForEpoch(epoch, this.l1constants);
376
+ return epochStartTs - BigInt(lagInEpochsForRandao) * BigInt(epochDuration) * BigInt(slotDuration);
377
+ }
378
+
379
+ /**
380
+ * Lightweight refresh for a stale non-finalized entry. Queries only the block hash at
381
+ * the original block number and the finalized block timestamp — avoids the expensive
382
+ * getCommitteeAt and getSampleSeedAt calls on the rollup contract.
383
+ *
384
+ * If the block hash still matches (no L1 reorg), we keep the existing data and just
385
+ * update the provenance timestamp. If the finalized block has caught up, we promote the
386
+ * entry to finalized. If there was a reorg (hash mismatch), we fall back to a full fetch.
387
+ */
388
+ private async refreshStaleEntry(stale: CachedEpochEntry, epoch: EpochNumber, ts: bigint): Promise<CachedEpochEntry> {
389
+ const [blockAtOriginal, l1FinalizedBlock, latestBlock] = await Promise.all([
390
+ this.rollup.client.getBlock({ blockNumber: stale.lastQueryL1BlockNumber, includeTransactions: false }),
391
+ getFinalizedL1Block(this.rollup.client),
392
+ this.rollup.client.getBlock({ includeTransactions: false }),
393
+ ]);
394
+
395
+ if (blockAtOriginal.hash === stale.lastQueryL1BlockHash) {
396
+ // No reorg: the data is still valid. Check if we can now mark it as finalized.
397
+ const samplingTs = this.getSamplingTimestamp(epoch);
398
+ const finalized =
399
+ !!(stale.data.committee && stale.data.committee.length > 0) &&
400
+ l1FinalizedBlock !== undefined &&
401
+ samplingTs <= l1FinalizedBlock.timestamp;
402
+
403
+ const refreshed: CachedEpochEntry = {
404
+ ...stale,
405
+ lastRefreshL1Timestamp: latestBlock.timestamp,
406
+ finalized,
407
+ };
408
+ this.cache.set(epoch, refreshed);
409
+ return refreshed;
410
+ }
411
+
412
+ // Reorg detected: block hash mismatch. Do a full re-fetch.
413
+ // Pass the already-fetched block timestamps to avoid redundant queries.
414
+ this.log.warn(`L1 reorg detected for epoch ${epoch}: block ${stale.lastQueryL1BlockNumber} hash changed`, {
415
+ epoch,
416
+ expectedHash: stale.lastQueryL1BlockHash,
417
+ actualHash: blockAtOriginal.hash,
418
+ });
419
+ return this.fetchAndCache(epoch, ts, { latestBlock, finalizedBlock: l1FinalizedBlock });
420
+ }
421
+
422
+ /**
423
+ * Fetches committee data from L1, determines finalization status, and stores in the cache.
424
+ *
425
+ * Uses `lagInEpochsForRandao` (the binding constraint, always <= lagInEpochsForValidatorSet)
426
+ * and computes the sampling timestamp from the epoch start to match the L1 contract's logic.
427
+ *
428
+ * When called from refreshStaleEntry after a reorg, the latest and finalized blocks are
429
+ * passed in to avoid redundant L1 queries.
430
+ */
431
+ private async fetchAndCache(
432
+ epoch: EpochNumber,
433
+ ts: bigint,
434
+ prefetched?: { latestBlock: L1BlockInfo; finalizedBlock: { timestamp: bigint } | undefined },
435
+ ): Promise<CachedEpochEntry> {
436
+ const [committee, seedBuffer, latestBlock, finalizedBlock, isEscapeHatchOpen] = await Promise.all([
214
437
  this.rollup.getCommitteeAt(ts),
215
438
  this.rollup.getSampleSeedAt(ts),
216
- this.rollup.client.getBlock({ includeTransactions: false }).then(b => b.timestamp),
439
+ prefetched?.latestBlock ?? this.rollup.client.getBlock({ includeTransactions: false }),
440
+ prefetched !== undefined ? prefetched.finalizedBlock : getFinalizedL1Block(this.rollup.client),
441
+ this.rollup.isEscapeHatchOpen(epoch),
217
442
  ]);
218
- const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
219
- const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
220
- if (ts - sub > l1Timestamp) {
443
+
444
+ const samplingTs = this.getSamplingTimestamp(epoch);
445
+
446
+ if (samplingTs > latestBlock.timestamp) {
221
447
  throw new Error(
222
- `Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`,
448
+ `Cannot query committee for future epoch ${epoch}: ` +
449
+ `sampling timestamp ${samplingTs} is beyond latest L1 block at ${latestBlock.timestamp}. ` +
450
+ `Check your Ethereum node is synced.`,
223
451
  );
224
452
  }
225
- const committee = committeeHex?.map((v: `0x${string}`) => EthAddress.fromString(v));
226
- return { committee, seed, epoch };
453
+
454
+ // Empty committees are never marked finalized so they always get re-queried after TTL.
455
+ // If L1 has no finalized block yet (devnet startup), entries stay unfinalized.
456
+ const hasCommittee = !!(committee && committee.length > 0);
457
+ const finalized = hasCommittee && finalizedBlock !== undefined && samplingTs <= finalizedBlock.timestamp;
458
+ const data: EpochCommitteeInfo = { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
459
+ const entry: CachedEpochEntry = {
460
+ data,
461
+ lastQueryL1BlockNumber: latestBlock.number!,
462
+ lastQueryL1BlockHash: latestBlock.hash!,
463
+ lastRefreshL1Timestamp: latestBlock.timestamp,
464
+ finalized,
465
+ };
466
+
467
+ this.cache.set(epoch, entry);
468
+ this.purgeCache();
469
+
470
+ return entry;
227
471
  }
228
472
 
229
473
  /**
@@ -248,29 +492,31 @@ export class EpochCache implements EpochCacheInterface {
248
492
  return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
249
493
  }
250
494
 
251
- /**
252
- * Returns the current and next proposer's attester address
253
- *
254
- * We return the next proposer's attester address as the node will check if it is the proposer at the next ethereum block,
255
- * which can be the next slot. If this is the case, then it will send proposals early.
256
- */
257
- public async getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
258
- currentSlot: SlotNumber;
259
- nextSlot: SlotNumber;
260
- currentProposer: EthAddress | undefined;
261
- nextProposer: EthAddress | undefined;
262
- }> {
263
- const current = this.getEpochAndSlotNow();
495
+ /** Returns the current and next L2 slot in next eth L1 Slot. */
496
+ public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
497
+ const currentSlot = this.getSlotNow();
264
498
  const next = this.getEpochAndSlotInNextL1Slot();
265
499
 
266
500
  return {
267
- currentProposer: await this.getProposerAttesterAddressAt(current),
268
- nextProposer: await this.getProposerAttesterAddressAt(next),
269
- currentSlot: current.slot,
501
+ currentSlot,
270
502
  nextSlot: next.slot,
271
503
  };
272
504
  }
273
505
 
506
+ /** Returns the target and next L2 slot in the next L1 slot. */
507
+ public getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
508
+ const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
509
+ const offset = this.isProposerPipeliningEnabled() ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
510
+
511
+ const currentSlot = getSlotAtTimestamp(nowSeconds, this.l1constants);
512
+ const targetSlot = SlotNumber(currentSlot + offset);
513
+
514
+ const nextL2SlotOnL1 = getSlotAtNextL1Block(nowSeconds, this.l1constants);
515
+ const nextSlot = SlotNumber(nextL2SlotOnL1 + offset);
516
+
517
+ return { targetSlot, nextSlot };
518
+ }
519
+
274
520
  /**
275
521
  * Get the proposer attester address in the given L2 slot
276
522
  * @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
@@ -349,11 +595,12 @@ export class EpochCache implements EpochCacheInterface {
349
595
  async getRegisteredValidators(): Promise<EthAddress[]> {
350
596
  const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
351
597
  const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
352
- if (validatorRefreshTime < this.dateProvider.now()) {
353
- const currentSet = await this.rollup.getAttesters();
354
- this.allValidators = new Set(currentSet);
355
- this.lastValidatorRefresh = this.dateProvider.now();
598
+ const now = this.dateProvider.now();
599
+ if (validatorRefreshTime < now) {
600
+ const currentSet = await this.rollup.getAttesters(BigInt(Math.floor(now / 1000)));
601
+ this.allValidators = new Set(currentSet.map(v => v.toString()));
602
+ this.lastValidatorRefresh = now;
356
603
  }
357
- return Array.from(this.allValidators.keys().map(v => EthAddress.fromString(v)));
604
+ return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
358
605
  }
359
606
  }
@@ -0,0 +1 @@
1
+ export * from './test_epoch_cache.js';