@aztec/epoch-cache 0.0.1-commit.8c0b8ff → 0.0.1-commit.8cb2d04d8
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 +211 -0
- package/dest/config.d.ts +3 -2
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +3 -1
- package/dest/epoch_cache.d.ts +77 -16
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +218 -58
- package/dest/test/test_epoch_cache.d.ts +19 -3
- package/dest/test/test_epoch_cache.d.ts.map +1 -1
- package/dest/test/test_epoch_cache.js +57 -11
- package/package.json +5 -6
- package/src/config.ts +9 -3
- package/src/epoch_cache.ts +273 -54
- package/src/test/test_epoch_cache.ts +83 -12
package/src/epoch_cache.ts
CHANGED
|
@@ -10,19 +10,24 @@ import {
|
|
|
10
10
|
getEpochAtSlot,
|
|
11
11
|
getEpochNumberAtTimestamp,
|
|
12
12
|
getNextL1SlotTimestamp,
|
|
13
|
+
getSlotAtNextL1Block,
|
|
13
14
|
getSlotAtTimestamp,
|
|
14
15
|
getSlotRangeForEpoch,
|
|
16
|
+
getStartTimestampForEpoch,
|
|
15
17
|
getTimestampForSlot,
|
|
16
|
-
getTimestampRangeForEpoch,
|
|
17
18
|
} from '@aztec/stdlib/epoch-helpers';
|
|
18
19
|
|
|
19
20
|
import { createPublicClient, encodeAbiParameters, keccak256 } from 'viem';
|
|
20
21
|
|
|
21
22
|
import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
|
|
22
23
|
|
|
24
|
+
/** When proposer pipelining is enabled, the proposer builds one slot ahead. */
|
|
25
|
+
export const PROPOSER_PIPELINING_SLOT_OFFSET = 1;
|
|
26
|
+
|
|
27
|
+
/** Flat return type for compound epoch/slot getters. */
|
|
23
28
|
export type EpochAndSlot = {
|
|
24
|
-
epoch: EpochNumber;
|
|
25
29
|
slot: SlotNumber;
|
|
30
|
+
epoch: EpochNumber;
|
|
26
31
|
ts: bigint;
|
|
27
32
|
};
|
|
28
33
|
|
|
@@ -36,13 +41,40 @@ export type EpochCommitteeInfo = {
|
|
|
36
41
|
|
|
37
42
|
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
38
43
|
|
|
44
|
+
/** Minimal L1 block info used for cache provenance. */
|
|
45
|
+
type L1BlockInfo = { number: bigint; hash: `0x${string}`; timestamp: bigint };
|
|
46
|
+
|
|
47
|
+
/** Resolved cache entry with L1 provenance metadata. */
|
|
48
|
+
type CachedEpochEntry = {
|
|
49
|
+
data: EpochCommitteeInfo;
|
|
50
|
+
/** L1 block number at which the committee data was originally queried. */
|
|
51
|
+
lastQueryL1BlockNumber: bigint;
|
|
52
|
+
/** L1 block hash at which the committee data was originally queried. Used to detect reorgs. */
|
|
53
|
+
lastQueryL1BlockHash: `0x${string}`;
|
|
54
|
+
/** Latest L1 block timestamp at the time of the most recent refresh (full fetch or lightweight check). */
|
|
55
|
+
lastRefreshL1Timestamp: bigint;
|
|
56
|
+
/** Whether the epoch's sampling data falls within finalized L1 history. */
|
|
57
|
+
finalized: boolean;
|
|
58
|
+
};
|
|
59
|
+
|
|
39
60
|
export interface EpochCacheInterface {
|
|
40
61
|
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
62
|
+
getSlotNow(): SlotNumber;
|
|
63
|
+
getTargetSlot(): SlotNumber;
|
|
64
|
+
getEpochNow(): EpochNumber;
|
|
65
|
+
getTargetEpoch(): EpochNumber;
|
|
41
66
|
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint };
|
|
42
|
-
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
67
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
|
|
68
|
+
/** Returns epoch/slot info for the next L1 slot with pipeline offset applied. */
|
|
69
|
+
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
|
|
70
|
+
isProposerPipeliningEnabled(): boolean;
|
|
71
|
+
pipeliningOffset(): number;
|
|
72
|
+
isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean>;
|
|
73
|
+
isEscapeHatchOpenAtSlot(slot: SlotTag): Promise<boolean>;
|
|
43
74
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
44
75
|
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
45
76
|
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
|
|
77
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber };
|
|
46
78
|
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
47
79
|
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
48
80
|
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
@@ -60,12 +92,18 @@ export interface EpochCacheInterface {
|
|
|
60
92
|
* Note: This class is very dependent on the system clock being in sync.
|
|
61
93
|
*/
|
|
62
94
|
export class EpochCache implements EpochCacheInterface {
|
|
95
|
+
/**
|
|
96
|
+
* Single map holding both resolved entries and in-flight promises.
|
|
97
|
+
* A `Promise` value means a fetch is in progress; concurrent callers await it.
|
|
98
|
+
*/
|
|
63
99
|
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
64
|
-
protected cache: Map<EpochNumber,
|
|
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
|
|
|
@@ -136,13 +176,43 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
136
176
|
rollupManaLimit: Number(rollupManaLimit),
|
|
137
177
|
};
|
|
138
178
|
|
|
139
|
-
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
|
+
});
|
|
140
184
|
}
|
|
141
185
|
|
|
142
186
|
public getL1Constants(): L1RollupConstants {
|
|
143
187
|
return this.l1constants;
|
|
144
188
|
}
|
|
145
189
|
|
|
190
|
+
public isProposerPipeliningEnabled(): boolean {
|
|
191
|
+
return this.enableProposerPipelining;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public pipeliningOffset(): number {
|
|
195
|
+
return this.enableProposerPipelining ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
|
|
196
|
+
}
|
|
197
|
+
|
|
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
|
+
|
|
146
216
|
public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
147
217
|
const nowMs = BigInt(this.dateProvider.now());
|
|
148
218
|
const nowSeconds = nowMs / 1000n;
|
|
@@ -150,23 +220,33 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
150
220
|
}
|
|
151
221
|
|
|
152
222
|
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
223
|
+
return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
|
|
224
|
+
}
|
|
225
|
+
|
|
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) };
|
|
156
230
|
}
|
|
157
231
|
|
|
158
|
-
public
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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) };
|
|
162
241
|
}
|
|
163
242
|
|
|
164
243
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
165
244
|
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
245
|
+
const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
|
|
166
246
|
return {
|
|
167
|
-
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
168
|
-
ts: getTimestampForSlot(slot, this.l1constants),
|
|
169
247
|
slot,
|
|
248
|
+
epoch,
|
|
249
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
170
250
|
};
|
|
171
251
|
}
|
|
172
252
|
|
|
@@ -175,17 +255,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
175
255
|
return this.getCommittee(startSlot);
|
|
176
256
|
}
|
|
177
257
|
|
|
178
|
-
/**
|
|
179
|
-
* Returns whether the escape hatch is open for the given epoch.
|
|
180
|
-
*
|
|
181
|
-
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
182
|
-
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
183
|
-
*/
|
|
258
|
+
/** Returns whether the escape hatch is open for the given epoch. */
|
|
184
259
|
public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
|
|
185
|
-
const cached = this.cache.get(epoch);
|
|
186
|
-
if (cached) {
|
|
187
|
-
return cached.isEscapeHatchOpen;
|
|
188
|
-
}
|
|
189
260
|
const info = await this.getCommitteeForEpoch(epoch);
|
|
190
261
|
return info.isEscapeHatchOpen;
|
|
191
262
|
}
|
|
@@ -199,7 +270,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
199
270
|
public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
|
|
200
271
|
const epoch =
|
|
201
272
|
slot === 'now'
|
|
202
|
-
? this.
|
|
273
|
+
? this.getEpochNow()
|
|
203
274
|
: slot === 'next'
|
|
204
275
|
? this.getEpochAndSlotInNextL1Slot().epoch
|
|
205
276
|
: getEpochAtSlot(slot, this.l1constants);
|
|
@@ -208,33 +279,52 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
208
279
|
}
|
|
209
280
|
|
|
210
281
|
/**
|
|
211
|
-
* Get the current validator set
|
|
212
|
-
*
|
|
213
|
-
*
|
|
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.
|
|
214
287
|
*/
|
|
215
288
|
public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
|
|
216
289
|
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
217
290
|
|
|
218
|
-
|
|
219
|
-
|
|
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;
|
|
220
296
|
}
|
|
221
297
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return epochData;
|
|
298
|
+
// Resolved entry: return it if finalized or still fresh.
|
|
299
|
+
if (cached && (cached.finalized || !this.isStale(cached))) {
|
|
300
|
+
return cached.data;
|
|
226
301
|
}
|
|
227
|
-
this.cache.set(epoch, epochData);
|
|
228
302
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
+
}
|
|
233
315
|
|
|
234
|
-
|
|
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
|
+
}
|
|
235
325
|
}
|
|
236
326
|
|
|
237
|
-
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
327
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
|
|
238
328
|
if (slot === 'now') {
|
|
239
329
|
return this.getEpochAndSlotNow();
|
|
240
330
|
} else if (slot === 'next') {
|
|
@@ -244,22 +334,137 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
244
334
|
}
|
|
245
335
|
}
|
|
246
336
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
+
this.rollup.client.getBlock({ blockTag: 'finalized', includeTransactions: false }),
|
|
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) && samplingTs <= l1FinalizedBlock.timestamp;
|
|
400
|
+
|
|
401
|
+
const refreshed: CachedEpochEntry = {
|
|
402
|
+
...stale,
|
|
403
|
+
lastRefreshL1Timestamp: latestBlock.timestamp,
|
|
404
|
+
finalized,
|
|
405
|
+
};
|
|
406
|
+
this.cache.set(epoch, refreshed);
|
|
407
|
+
return refreshed;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Reorg detected: block hash mismatch. Do a full re-fetch.
|
|
411
|
+
// Pass the already-fetched block timestamps to avoid redundant queries.
|
|
412
|
+
this.log.warn(`L1 reorg detected for epoch ${epoch}: block ${stale.lastQueryL1BlockNumber} hash changed`, {
|
|
413
|
+
epoch,
|
|
414
|
+
expectedHash: stale.lastQueryL1BlockHash,
|
|
415
|
+
actualHash: blockAtOriginal.hash,
|
|
416
|
+
});
|
|
417
|
+
return this.fetchAndCache(epoch, ts, { latestBlock, finalizedBlock: l1FinalizedBlock });
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Fetches committee data from L1, determines finalization status, and stores in the cache.
|
|
422
|
+
*
|
|
423
|
+
* Uses `lagInEpochsForRandao` (the binding constraint, always <= lagInEpochsForValidatorSet)
|
|
424
|
+
* and computes the sampling timestamp from the epoch start to match the L1 contract's logic.
|
|
425
|
+
*
|
|
426
|
+
* When called from refreshStaleEntry after a reorg, the latest and finalized blocks are
|
|
427
|
+
* passed in to avoid redundant L1 queries.
|
|
428
|
+
*/
|
|
429
|
+
private async fetchAndCache(
|
|
430
|
+
epoch: EpochNumber,
|
|
431
|
+
ts: bigint,
|
|
432
|
+
prefetched?: { latestBlock: L1BlockInfo; finalizedBlock: { timestamp: bigint } },
|
|
433
|
+
): Promise<CachedEpochEntry> {
|
|
434
|
+
const [committee, seedBuffer, latestBlock, finalizedBlock, isEscapeHatchOpen] = await Promise.all([
|
|
250
435
|
this.rollup.getCommitteeAt(ts),
|
|
251
436
|
this.rollup.getSampleSeedAt(ts),
|
|
252
|
-
this.rollup.client.getBlock({ includeTransactions: false })
|
|
437
|
+
prefetched?.latestBlock ?? this.rollup.client.getBlock({ includeTransactions: false }),
|
|
438
|
+
prefetched?.finalizedBlock ?? this.rollup.client.getBlock({ blockTag: 'finalized', includeTransactions: false }),
|
|
253
439
|
this.rollup.isEscapeHatchOpen(epoch),
|
|
254
440
|
]);
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
441
|
+
|
|
442
|
+
const samplingTs = this.getSamplingTimestamp(epoch);
|
|
443
|
+
|
|
444
|
+
if (samplingTs > latestBlock.timestamp) {
|
|
258
445
|
throw new Error(
|
|
259
|
-
`Cannot query committee for future epoch ${epoch}
|
|
446
|
+
`Cannot query committee for future epoch ${epoch}: ` +
|
|
447
|
+
`sampling timestamp ${samplingTs} is beyond latest L1 block at ${latestBlock.timestamp}. ` +
|
|
448
|
+
`Check your Ethereum node is synced.`,
|
|
260
449
|
);
|
|
261
450
|
}
|
|
262
|
-
|
|
451
|
+
|
|
452
|
+
// Empty committees are never marked finalized so they always get re-queried after TTL.
|
|
453
|
+
const hasCommittee = !!(committee && committee.length > 0);
|
|
454
|
+
const finalized = hasCommittee && samplingTs <= finalizedBlock.timestamp;
|
|
455
|
+
const data: EpochCommitteeInfo = { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
|
|
456
|
+
const entry: CachedEpochEntry = {
|
|
457
|
+
data,
|
|
458
|
+
lastQueryL1BlockNumber: latestBlock.number!,
|
|
459
|
+
lastQueryL1BlockHash: latestBlock.hash!,
|
|
460
|
+
lastRefreshL1Timestamp: latestBlock.timestamp,
|
|
461
|
+
finalized,
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
this.cache.set(epoch, entry);
|
|
465
|
+
this.purgeCache();
|
|
466
|
+
|
|
467
|
+
return entry;
|
|
263
468
|
}
|
|
264
469
|
|
|
265
470
|
/**
|
|
@@ -284,17 +489,31 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
284
489
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
285
490
|
}
|
|
286
491
|
|
|
287
|
-
/** Returns the current and next L2 slot
|
|
492
|
+
/** Returns the current and next L2 slot in next eth L1 Slot. */
|
|
288
493
|
public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
289
|
-
const
|
|
494
|
+
const currentSlot = this.getSlotNow();
|
|
290
495
|
const next = this.getEpochAndSlotInNextL1Slot();
|
|
291
496
|
|
|
292
497
|
return {
|
|
293
|
-
currentSlot
|
|
498
|
+
currentSlot,
|
|
294
499
|
nextSlot: next.slot,
|
|
295
500
|
};
|
|
296
501
|
}
|
|
297
502
|
|
|
503
|
+
/** Returns the target and next L2 slot in the next L1 slot. */
|
|
504
|
+
public getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
505
|
+
const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
|
|
506
|
+
const offset = this.isProposerPipeliningEnabled() ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
|
|
507
|
+
|
|
508
|
+
const currentSlot = getSlotAtTimestamp(nowSeconds, this.l1constants);
|
|
509
|
+
const targetSlot = SlotNumber(currentSlot + offset);
|
|
510
|
+
|
|
511
|
+
const nextL2SlotOnL1 = getSlotAtNextL1Block(nowSeconds, this.l1constants);
|
|
512
|
+
const nextSlot = SlotNumber(nextL2SlotOnL1 + offset);
|
|
513
|
+
|
|
514
|
+
return { targetSlot, nextSlot };
|
|
515
|
+
}
|
|
516
|
+
|
|
298
517
|
/**
|
|
299
518
|
* Get the proposer attester address in the given L2 slot
|
|
300
519
|
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
@@ -3,7 +3,13 @@ import { EthAddress } from '@aztec/foundation/eth-address';
|
|
|
3
3
|
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
4
4
|
import { getEpochAtSlot, getSlotAtTimestamp, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import {
|
|
7
|
+
type EpochAndSlot,
|
|
8
|
+
type EpochCacheInterface,
|
|
9
|
+
type EpochCommitteeInfo,
|
|
10
|
+
PROPOSER_PIPELINING_SLOT_OFFSET,
|
|
11
|
+
type SlotTag,
|
|
12
|
+
} from '../epoch_cache.js';
|
|
7
13
|
|
|
8
14
|
/** Default L1 constants for testing. */
|
|
9
15
|
const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
|
|
@@ -32,6 +38,7 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
32
38
|
private seed: bigint = 0n;
|
|
33
39
|
private registeredValidators: EthAddress[] = [];
|
|
34
40
|
private l1Constants: L1RollupConstants;
|
|
41
|
+
private proposerPipeliningEnabled = false;
|
|
35
42
|
|
|
36
43
|
constructor(l1Constants: Partial<L1RollupConstants> = {}) {
|
|
37
44
|
this.l1Constants = { ...DEFAULT_L1_CONSTANTS, ...l1Constants };
|
|
@@ -104,6 +111,10 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
104
111
|
return this.l1Constants;
|
|
105
112
|
}
|
|
106
113
|
|
|
114
|
+
setProposerPipeliningEnabled(enabled: boolean): void {
|
|
115
|
+
this.proposerPipeliningEnabled = enabled;
|
|
116
|
+
}
|
|
117
|
+
|
|
107
118
|
getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo> {
|
|
108
119
|
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
109
120
|
return Promise.resolve({
|
|
@@ -114,19 +125,62 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
114
125
|
});
|
|
115
126
|
}
|
|
116
127
|
|
|
128
|
+
getSlotNow(): SlotNumber {
|
|
129
|
+
return this.currentSlot;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getTargetSlot(): SlotNumber {
|
|
133
|
+
return this.proposerPipeliningEnabled
|
|
134
|
+
? SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET)
|
|
135
|
+
: this.currentSlot;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
getEpochNow(): EpochNumber {
|
|
139
|
+
return getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getTargetEpoch(): EpochNumber {
|
|
143
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1Constants);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
isProposerPipeliningEnabled(): boolean {
|
|
147
|
+
return this.proposerPipeliningEnabled;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
pipeliningOffset(): number {
|
|
151
|
+
return this.proposerPipeliningEnabled ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
117
154
|
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
118
|
-
const
|
|
119
|
-
const ts = getTimestampRangeForEpoch(
|
|
120
|
-
return {
|
|
155
|
+
const epochNow = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
156
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
157
|
+
return {
|
|
158
|
+
epoch: epochNow,
|
|
159
|
+
slot: this.currentSlot,
|
|
160
|
+
ts,
|
|
161
|
+
nowMs: ts * 1000n,
|
|
162
|
+
};
|
|
121
163
|
}
|
|
122
164
|
|
|
123
|
-
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
124
|
-
const
|
|
125
|
-
const nextSlotTs =
|
|
165
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
166
|
+
const nowTs = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
|
|
167
|
+
const nextSlotTs = nowTs + BigInt(this.l1Constants.ethereumSlotDuration);
|
|
126
168
|
const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
|
|
127
|
-
const
|
|
128
|
-
const ts = getTimestampRangeForEpoch(
|
|
129
|
-
return {
|
|
169
|
+
const epochNow = getEpochAtSlot(nextSlot, this.l1Constants);
|
|
170
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
171
|
+
return {
|
|
172
|
+
epoch: epochNow,
|
|
173
|
+
slot: nextSlot,
|
|
174
|
+
ts,
|
|
175
|
+
nowSeconds: nowTs,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
180
|
+
const result = this.getEpochAndSlotInNextL1Slot();
|
|
181
|
+
const offset = this.isProposerPipeliningEnabled() ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
|
|
182
|
+
const targetSlot = SlotNumber(result.slot + offset);
|
|
183
|
+
return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1Constants) };
|
|
130
184
|
}
|
|
131
185
|
|
|
132
186
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
|
|
@@ -142,9 +196,22 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
142
196
|
}
|
|
143
197
|
|
|
144
198
|
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
199
|
+
const currentSlot = this.getSlotNow();
|
|
200
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
201
|
+
|
|
145
202
|
return {
|
|
146
|
-
currentSlot
|
|
147
|
-
nextSlot:
|
|
203
|
+
currentSlot,
|
|
204
|
+
nextSlot: next.slot,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
209
|
+
const targetSlot = this.getTargetSlot();
|
|
210
|
+
const next = this.getTargetEpochAndSlotInNextL1Slot();
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
targetSlot,
|
|
214
|
+
nextSlot: next.slot,
|
|
148
215
|
};
|
|
149
216
|
}
|
|
150
217
|
|
|
@@ -165,6 +232,10 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
165
232
|
return Promise.resolve(validators.filter(v => committeeSet.has(v.toString())));
|
|
166
233
|
}
|
|
167
234
|
|
|
235
|
+
isEscapeHatchOpen(_epoch: EpochNumber): Promise<boolean> {
|
|
236
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
237
|
+
}
|
|
238
|
+
|
|
168
239
|
isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean> {
|
|
169
240
|
return Promise.resolve(this.escapeHatchOpen);
|
|
170
241
|
}
|