@aztec/epoch-cache 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d240

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,26 +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 };
47
79
  getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
48
80
  getRegisteredValidators(): Promise<EthAddress[]>;
49
81
  isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
50
82
  filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
83
+ getL1Constants(): L1RollupConstants;
51
84
  }
52
85
 
53
86
  /**
@@ -60,12 +93,17 @@ export interface EpochCacheInterface {
60
93
  * Note: This class is very dependent on the system clock being in sync.
61
94
  */
62
95
  export class EpochCache implements EpochCacheInterface {
63
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
64
- 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();
65
101
  private allValidators: Set<string> = new Set();
66
102
  private lastValidatorRefresh = 0;
67
103
  private readonly log: Logger = createLogger('epoch-cache');
68
104
 
105
+ protected enableProposerPipelining: boolean;
106
+
69
107
  constructor(
70
108
  private rollup: RollupContract,
71
109
  private readonly l1constants: L1RollupConstants & {
@@ -73,10 +111,12 @@ export class EpochCache implements EpochCacheInterface {
73
111
  lagInEpochsForRandao: number;
74
112
  },
75
113
  private readonly dateProvider: DateProvider = new DateProvider(),
76
- protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
114
+ protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60, enableProposerPipelining: false },
77
115
  ) {
116
+ this.enableProposerPipelining = this.config.enableProposerPipelining;
78
117
  this.log.debug(`Initialized EpochCache`, {
79
118
  l1constants,
119
+ enableProposerPipelining: this.enableProposerPipelining,
80
120
  });
81
121
  }
82
122
 
@@ -95,7 +135,7 @@ export class EpochCache implements EpochCacheInterface {
95
135
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
96
136
  const publicClient = createPublicClient({
97
137
  chain: chain.chainInfo,
98
- transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
138
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
99
139
  pollingInterval: config.viemPollingIntervalMS,
100
140
  });
101
141
  rollup = new RollupContract(publicClient, rollupOrAddress.toString());
@@ -109,6 +149,8 @@ export class EpochCache implements EpochCacheInterface {
109
149
  epochDuration,
110
150
  lagInEpochsForValidatorSet,
111
151
  lagInEpochsForRandao,
152
+ targetCommitteeSize,
153
+ rollupManaLimit,
112
154
  ] = await Promise.all([
113
155
  rollup.getL1StartBlock(),
114
156
  rollup.getL1GenesisTime(),
@@ -117,6 +159,8 @@ export class EpochCache implements EpochCacheInterface {
117
159
  rollup.getEpochDuration(),
118
160
  rollup.getLagInEpochsForValidatorSet(),
119
161
  rollup.getLagInEpochsForRandao(),
162
+ rollup.getTargetCommitteeSize(),
163
+ rollup.getManaLimit(),
120
164
  ] as const);
121
165
 
122
166
  const l1RollupConstants = {
@@ -128,42 +172,81 @@ export class EpochCache implements EpochCacheInterface {
128
172
  ethereumSlotDuration: config.ethereumSlotDuration,
129
173
  lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
130
174
  lagInEpochsForRandao: Number(lagInEpochsForRandao),
175
+ targetCommitteeSize: Number(targetCommitteeSize),
176
+ rollupManaLimit: Number(rollupManaLimit),
131
177
  };
132
178
 
133
- 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
+ });
134
184
  }
135
185
 
136
186
  public getL1Constants(): L1RollupConstants {
137
187
  return this.l1constants;
138
188
  }
139
189
 
140
- public getEpochAndSlotNow(): EpochAndSlot & { now: bigint } {
141
- const now = this.nowInSeconds();
142
- 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;
143
196
  }
144
197
 
145
- public nowInSeconds(): bigint {
146
- 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 };
147
220
  }
148
221
 
149
222
  private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
150
- const epoch = getEpochAtSlot(slot, this.l1constants);
151
- const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
152
- return { epoch, ts, slot };
223
+ return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
153
224
  }
154
225
 
155
- public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
156
- const now = this.nowInSeconds();
157
- const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
158
- 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) };
159
241
  }
160
242
 
161
243
  private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
162
244
  const slot = getSlotAtTimestamp(ts, this.l1constants);
245
+ const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
163
246
  return {
164
- epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
165
- ts: getTimestampForSlot(slot, this.l1constants),
166
247
  slot,
248
+ epoch,
249
+ ts: getTimestampForSlot(slot, this.l1constants),
167
250
  };
168
251
  }
169
252
 
@@ -172,34 +255,76 @@ export class EpochCache implements EpochCacheInterface {
172
255
  return this.getCommittee(startSlot);
173
256
  }
174
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
+
175
281
  /**
176
- * Get the current validator set
177
- * @param nextSlot - If true, get the validator set for the next slot.
178
- * @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.
179
287
  */
180
288
  public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
181
289
  const { epoch, ts } = this.getEpochAndTimestamp(slot);
182
290
 
183
- if (this.cache.has(epoch)) {
184
- 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;
185
296
  }
186
297
 
187
- const epochData = await this.computeCommittee({ epoch, ts });
188
- // If the committee size is 0 or undefined, then do not cache
189
- if (!epochData.committee || epochData.committee.length === 0) {
190
- return epochData;
298
+ // Resolved entry: return it if finalized or still fresh.
299
+ if (cached && (cached.finalized || !this.isStale(cached))) {
300
+ return cached.data;
191
301
  }
192
- this.cache.set(epoch, epochData);
193
302
 
194
- const toPurge = Array.from(this.cache.keys())
195
- .sort((a, b) => Number(b - a))
196
- .slice(this.config.cacheSize);
197
- 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
+ }
198
315
 
199
- 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
+ }
200
325
  }
201
326
 
202
- private getEpochAndTimestamp(slot: SlotTag = 'now') {
327
+ private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
203
328
  if (slot === 'now') {
204
329
  return this.getEpochAndSlotNow();
205
330
  } else if (slot === 'next') {
@@ -209,21 +334,140 @@ export class EpochCache implements EpochCacheInterface {
209
334
  }
210
335
  }
211
336
 
212
- private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
213
- const { ts, epoch } = when;
214
- const [committee, seedBuffer, 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([
215
437
  this.rollup.getCommitteeAt(ts),
216
438
  this.rollup.getSampleSeedAt(ts),
217
- 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),
218
442
  ]);
219
- const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
220
- const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
221
- if (ts - sub > l1Timestamp) {
443
+
444
+ const samplingTs = this.getSamplingTimestamp(epoch);
445
+
446
+ if (samplingTs > latestBlock.timestamp) {
222
447
  throw new Error(
223
- `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.`,
224
451
  );
225
452
  }
226
- return { committee, seed: seedBuffer.toBigInt(), 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,10 +595,11 @@ 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();
598
+ const now = this.dateProvider.now();
599
+ if (validatorRefreshTime < now) {
600
+ const currentSet = await this.rollup.getAttesters(BigInt(Math.floor(now / 1000)));
354
601
  this.allValidators = new Set(currentSet.map(v => v.toString()));
355
- this.lastValidatorRefresh = this.dateProvider.now();
602
+ this.lastValidatorRefresh = now;
356
603
  }
357
604
  return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
358
605
  }
@@ -0,0 +1 @@
1
+ export * from './test_epoch_cache.js';