@aztec/epoch-cache 0.0.1-commit.b655e406 → 0.0.1-commit.b9865e97

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