@aztec/epoch-cache 5.0.0-private.20260319 → 5.0.0-rc.1
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/README.md +210 -0
- package/dest/config.d.ts +2 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +1 -3
- package/dest/epoch_cache.d.ts +39 -20
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +164 -68
- package/dest/test/test_epoch_cache.d.ts +1 -4
- package/dest/test/test_epoch_cache.d.ts.map +1 -1
- package/dest/test/test_epoch_cache.js +7 -11
- package/package.json +6 -6
- package/src/config.ts +3 -9
- package/src/epoch_cache.ts +207 -70
- package/src/test/test_epoch_cache.ts +12 -15
package/src/epoch_cache.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
2
2
|
import { makeL1HttpTransport } from '@aztec/ethereum/client';
|
|
3
3
|
import { NoCommitteeError, RollupContract } from '@aztec/ethereum/contracts';
|
|
4
|
+
import { getFinalizedL1Block } from '@aztec/ethereum/queries';
|
|
4
5
|
import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
5
6
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
6
7
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
@@ -9,8 +10,11 @@ import {
|
|
|
9
10
|
type L1RollupConstants,
|
|
10
11
|
getEpochAtSlot,
|
|
11
12
|
getEpochNumberAtTimestamp,
|
|
13
|
+
getNextL1SlotTimestamp,
|
|
14
|
+
getSlotAtNextL1Block,
|
|
12
15
|
getSlotAtTimestamp,
|
|
13
16
|
getSlotRangeForEpoch,
|
|
17
|
+
getStartTimestampForEpoch,
|
|
14
18
|
getTimestampForSlot,
|
|
15
19
|
} from '@aztec/stdlib/epoch-helpers';
|
|
16
20
|
|
|
@@ -18,7 +22,7 @@ import { createPublicClient, encodeAbiParameters, keccak256 } from 'viem';
|
|
|
18
22
|
|
|
19
23
|
import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
|
|
20
24
|
|
|
21
|
-
/**
|
|
25
|
+
/** The proposer pipelines by building one slot ahead. */
|
|
22
26
|
export const PROPOSER_PIPELINING_SLOT_OFFSET = 1;
|
|
23
27
|
|
|
24
28
|
/** Flat return type for compound epoch/slot getters. */
|
|
@@ -38,6 +42,22 @@ export type EpochCommitteeInfo = {
|
|
|
38
42
|
|
|
39
43
|
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
40
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
|
+
|
|
41
61
|
export interface EpochCacheInterface {
|
|
42
62
|
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
43
63
|
getSlotNow(): SlotNumber;
|
|
@@ -48,7 +68,6 @@ export interface EpochCacheInterface {
|
|
|
48
68
|
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
|
|
49
69
|
/** Returns epoch/slot info for the next L1 slot with pipeline offset applied. */
|
|
50
70
|
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
|
|
51
|
-
isProposerPipeliningEnabled(): boolean;
|
|
52
71
|
isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean>;
|
|
53
72
|
isEscapeHatchOpenAtSlot(slot: SlotTag): Promise<boolean>;
|
|
54
73
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
@@ -72,14 +91,15 @@ export interface EpochCacheInterface {
|
|
|
72
91
|
* Note: This class is very dependent on the system clock being in sync.
|
|
73
92
|
*/
|
|
74
93
|
export class EpochCache implements EpochCacheInterface {
|
|
75
|
-
|
|
76
|
-
|
|
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();
|
|
77
99
|
private allValidators: Set<string> = new Set();
|
|
78
100
|
private lastValidatorRefresh = 0;
|
|
79
101
|
private readonly log: Logger = createLogger('epoch-cache');
|
|
80
102
|
|
|
81
|
-
protected enableProposerPipelining: boolean;
|
|
82
|
-
|
|
83
103
|
constructor(
|
|
84
104
|
private rollup: RollupContract,
|
|
85
105
|
private readonly l1constants: L1RollupConstants & {
|
|
@@ -87,12 +107,10 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
87
107
|
lagInEpochsForRandao: number;
|
|
88
108
|
},
|
|
89
109
|
private readonly dateProvider: DateProvider = new DateProvider(),
|
|
90
|
-
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60
|
|
110
|
+
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
|
|
91
111
|
) {
|
|
92
|
-
this.enableProposerPipelining = this.config.enableProposerPipelining;
|
|
93
112
|
this.log.debug(`Initialized EpochCache`, {
|
|
94
113
|
l1constants,
|
|
95
|
-
enableProposerPipelining: this.enableProposerPipelining,
|
|
96
114
|
});
|
|
97
115
|
}
|
|
98
116
|
|
|
@@ -155,7 +173,6 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
155
173
|
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider, {
|
|
156
174
|
cacheSize: 12,
|
|
157
175
|
validatorRefreshIntervalSeconds: 60,
|
|
158
|
-
enableProposerPipelining: config.enableProposerPipelining,
|
|
159
176
|
});
|
|
160
177
|
}
|
|
161
178
|
|
|
@@ -163,17 +180,13 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
163
180
|
return this.l1constants;
|
|
164
181
|
}
|
|
165
182
|
|
|
166
|
-
public isProposerPipeliningEnabled(): boolean {
|
|
167
|
-
return this.enableProposerPipelining;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
183
|
public getSlotNow(): SlotNumber {
|
|
171
184
|
return this.getEpochAndSlotNow().slot;
|
|
172
185
|
}
|
|
173
186
|
|
|
174
187
|
public getTargetSlot(): SlotNumber {
|
|
175
188
|
const slotNow = this.getSlotNow();
|
|
176
|
-
const offset =
|
|
189
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
177
190
|
return SlotNumber(slotNow + offset);
|
|
178
191
|
}
|
|
179
192
|
|
|
@@ -191,25 +204,17 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
191
204
|
return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
|
|
192
205
|
}
|
|
193
206
|
|
|
194
|
-
public nowInSeconds(): bigint {
|
|
195
|
-
return BigInt(Math.floor(this.dateProvider.now() / 1000));
|
|
196
|
-
}
|
|
197
|
-
|
|
198
207
|
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
199
208
|
return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
|
|
200
209
|
}
|
|
201
210
|
|
|
202
211
|
public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
203
|
-
const nowSeconds = this.nowInSeconds();
|
|
204
|
-
const nextSlotTs = nowSeconds
|
|
205
|
-
return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), nowSeconds };
|
|
212
|
+
const nowSeconds = this.dateProvider.nowInSeconds();
|
|
213
|
+
const nextSlotTs = getNextL1SlotTimestamp(nowSeconds, this.l1constants);
|
|
214
|
+
return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), nowSeconds: BigInt(nowSeconds) };
|
|
206
215
|
}
|
|
207
216
|
|
|
208
217
|
public getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
209
|
-
if (!this.isProposerPipeliningEnabled()) {
|
|
210
|
-
return this.getEpochAndSlotInNextL1Slot();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
218
|
const result = this.getEpochAndSlotInNextL1Slot();
|
|
214
219
|
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
215
220
|
const targetSlot = SlotNumber(result.slot + offset);
|
|
@@ -231,17 +236,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
231
236
|
return this.getCommittee(startSlot);
|
|
232
237
|
}
|
|
233
238
|
|
|
234
|
-
/**
|
|
235
|
-
* Returns whether the escape hatch is open for the given epoch.
|
|
236
|
-
*
|
|
237
|
-
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
238
|
-
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
239
|
-
*/
|
|
239
|
+
/** Returns whether the escape hatch is open for the given epoch. */
|
|
240
240
|
public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
|
|
241
|
-
const cached = this.cache.get(epoch);
|
|
242
|
-
if (cached) {
|
|
243
|
-
return cached.isEscapeHatchOpen;
|
|
244
|
-
}
|
|
245
241
|
const info = await this.getCommitteeForEpoch(epoch);
|
|
246
242
|
return info.isEscapeHatchOpen;
|
|
247
243
|
}
|
|
@@ -264,30 +260,49 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
264
260
|
}
|
|
265
261
|
|
|
266
262
|
/**
|
|
267
|
-
* Get the current validator set
|
|
268
|
-
*
|
|
269
|
-
*
|
|
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.
|
|
270
268
|
*/
|
|
271
269
|
public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
|
|
272
270
|
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
273
271
|
|
|
274
|
-
|
|
275
|
-
|
|
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;
|
|
276
277
|
}
|
|
277
278
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
return epochData;
|
|
279
|
+
// Resolved entry: return it if finalized or still fresh.
|
|
280
|
+
if (cached && (cached.finalized || !this.isStale(cached))) {
|
|
281
|
+
return cached.data;
|
|
282
282
|
}
|
|
283
|
-
this.cache.set(epoch, epochData);
|
|
284
283
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
+
}
|
|
289
296
|
|
|
290
|
-
|
|
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
|
+
}
|
|
291
306
|
}
|
|
292
307
|
|
|
293
308
|
private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
|
|
@@ -300,22 +315,140 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
300
315
|
}
|
|
301
316
|
}
|
|
302
317
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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([
|
|
306
418
|
this.rollup.getCommitteeAt(ts),
|
|
307
419
|
this.rollup.getSampleSeedAt(ts),
|
|
308
|
-
this.rollup.client.getBlock({ includeTransactions: false })
|
|
420
|
+
prefetched?.latestBlock ?? this.rollup.client.getBlock({ includeTransactions: false }),
|
|
421
|
+
prefetched !== undefined ? prefetched.finalizedBlock : getFinalizedL1Block(this.rollup.client),
|
|
309
422
|
this.rollup.isEscapeHatchOpen(epoch),
|
|
310
423
|
]);
|
|
311
|
-
|
|
312
|
-
const
|
|
313
|
-
|
|
424
|
+
|
|
425
|
+
const samplingTs = this.getSamplingTimestamp(epoch);
|
|
426
|
+
|
|
427
|
+
if (samplingTs > latestBlock.timestamp) {
|
|
314
428
|
throw new Error(
|
|
315
|
-
`Cannot query committee for future epoch ${epoch}
|
|
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.`,
|
|
316
432
|
);
|
|
317
433
|
}
|
|
318
|
-
|
|
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;
|
|
319
452
|
}
|
|
320
453
|
|
|
321
454
|
/**
|
|
@@ -351,15 +484,18 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
351
484
|
};
|
|
352
485
|
}
|
|
353
486
|
|
|
354
|
-
/** Returns the
|
|
487
|
+
/** Returns the target and next L2 slot in the next L1 slot. */
|
|
355
488
|
public getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
356
|
-
const
|
|
357
|
-
const
|
|
489
|
+
const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
|
|
490
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
358
491
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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 };
|
|
363
499
|
}
|
|
364
500
|
|
|
365
501
|
/**
|
|
@@ -440,10 +576,11 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
440
576
|
async getRegisteredValidators(): Promise<EthAddress[]> {
|
|
441
577
|
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
442
578
|
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
443
|
-
|
|
444
|
-
|
|
579
|
+
const now = this.dateProvider.now();
|
|
580
|
+
if (validatorRefreshTime < now) {
|
|
581
|
+
const currentSet = await this.rollup.getAttesters(BigInt(Math.floor(now / 1000)));
|
|
445
582
|
this.allValidators = new Set(currentSet.map(v => v.toString()));
|
|
446
|
-
this.lastValidatorRefresh =
|
|
583
|
+
this.lastValidatorRefresh = now;
|
|
447
584
|
}
|
|
448
585
|
return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
|
|
449
586
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
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 {
|
|
4
|
+
import {
|
|
5
|
+
getEpochAtSlot,
|
|
6
|
+
getSlotAtTimestamp,
|
|
7
|
+
getTimestampForSlot,
|
|
8
|
+
getTimestampRangeForEpoch,
|
|
9
|
+
} from '@aztec/stdlib/epoch-helpers';
|
|
5
10
|
|
|
6
11
|
import {
|
|
7
12
|
type EpochAndSlot,
|
|
@@ -38,7 +43,6 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
38
43
|
private seed: bigint = 0n;
|
|
39
44
|
private registeredValidators: EthAddress[] = [];
|
|
40
45
|
private l1Constants: L1RollupConstants;
|
|
41
|
-
private proposerPipeliningEnabled = false;
|
|
42
46
|
|
|
43
47
|
constructor(l1Constants: Partial<L1RollupConstants> = {}) {
|
|
44
48
|
this.l1Constants = { ...DEFAULT_L1_CONSTANTS, ...l1Constants };
|
|
@@ -111,10 +115,6 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
111
115
|
return this.l1Constants;
|
|
112
116
|
}
|
|
113
117
|
|
|
114
|
-
setProposerPipeliningEnabled(enabled: boolean): void {
|
|
115
|
-
this.proposerPipeliningEnabled = enabled;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
118
|
getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo> {
|
|
119
119
|
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
120
120
|
return Promise.resolve({
|
|
@@ -130,9 +130,7 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
getTargetSlot(): SlotNumber {
|
|
133
|
-
return this.
|
|
134
|
-
? SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET)
|
|
135
|
-
: this.currentSlot;
|
|
133
|
+
return SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET);
|
|
136
134
|
}
|
|
137
135
|
|
|
138
136
|
getEpochNow(): EpochNumber {
|
|
@@ -143,13 +141,12 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
143
141
|
return getEpochAtSlot(this.getTargetSlot(), this.l1Constants);
|
|
144
142
|
}
|
|
145
143
|
|
|
146
|
-
isProposerPipeliningEnabled(): boolean {
|
|
147
|
-
return this.proposerPipeliningEnabled;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
144
|
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
145
|
+
// Model "now" as the start of the current slot (mirroring the real EpochCache, which derives nowMs
|
|
146
|
+
// from the wall clock). Using the slot start rather than the epoch start keeps nowMs consistent with
|
|
147
|
+
// currentSlot, which the pipelining receive-window check (clock_tolerance) relies on.
|
|
151
148
|
const epochNow = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
152
|
-
const ts =
|
|
149
|
+
const ts = getTimestampForSlot(this.currentSlot, this.l1Constants);
|
|
153
150
|
return {
|
|
154
151
|
epoch: epochNow,
|
|
155
152
|
slot: this.currentSlot,
|
|
@@ -174,7 +171,7 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
174
171
|
|
|
175
172
|
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
176
173
|
const result = this.getEpochAndSlotInNextL1Slot();
|
|
177
|
-
const offset =
|
|
174
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
178
175
|
const targetSlot = SlotNumber(result.slot + offset);
|
|
179
176
|
return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1Constants) };
|
|
180
177
|
}
|