@aztec/epoch-cache 0.0.1-commit.e558bd1c → 0.0.1-commit.e57c76e
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 -2
- package/dest/config.d.ts.map +1 -1
- package/dest/epoch_cache.d.ts +71 -17
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +214 -70
- package/dest/test/test_epoch_cache.d.ts +15 -3
- package/dest/test/test_epoch_cache.d.ts.map +1 -1
- package/dest/test/test_epoch_cache.js +50 -12
- package/package.json +6 -7
- package/src/config.ts +1 -1
- package/src/epoch_cache.ts +271 -62
- package/src/test/test_epoch_cache.ts +70 -12
package/src/epoch_cache.ts
CHANGED
|
@@ -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,
|
|
21
|
+
import { createPublicClient, encodeAbiParameters, keccak256 } from 'viem';
|
|
18
22
|
|
|
19
23
|
import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
|
|
20
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. */
|
|
21
29
|
export type EpochAndSlot = {
|
|
22
|
-
epoch: EpochNumber;
|
|
23
30
|
slot: SlotNumber;
|
|
31
|
+
epoch: EpochNumber;
|
|
24
32
|
ts: bigint;
|
|
25
33
|
};
|
|
26
34
|
|
|
@@ -34,17 +42,43 @@ export type EpochCommitteeInfo = {
|
|
|
34
42
|
|
|
35
43
|
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
36
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
|
+
|
|
37
61
|
export interface EpochCacheInterface {
|
|
38
62
|
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
63
|
+
getSlotNow(): SlotNumber;
|
|
64
|
+
getTargetSlot(): SlotNumber;
|
|
65
|
+
getEpochNow(): EpochNumber;
|
|
66
|
+
getTargetEpoch(): EpochNumber;
|
|
39
67
|
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint };
|
|
40
|
-
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
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>;
|
|
41
73
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
42
74
|
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
43
75
|
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
|
|
76
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber };
|
|
44
77
|
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
45
78
|
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
46
79
|
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
47
80
|
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
81
|
+
getL1Constants(): L1RollupConstants;
|
|
48
82
|
}
|
|
49
83
|
|
|
50
84
|
/**
|
|
@@ -57,8 +91,11 @@ export interface EpochCacheInterface {
|
|
|
57
91
|
* Note: This class is very dependent on the system clock being in sync.
|
|
58
92
|
*/
|
|
59
93
|
export class EpochCache implements EpochCacheInterface {
|
|
60
|
-
|
|
61
|
-
|
|
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');
|
|
@@ -92,7 +129,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
92
129
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
93
130
|
const publicClient = createPublicClient({
|
|
94
131
|
chain: chain.chainInfo,
|
|
95
|
-
transport:
|
|
132
|
+
transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
|
|
96
133
|
pollingInterval: config.viemPollingIntervalMS,
|
|
97
134
|
});
|
|
98
135
|
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
@@ -106,6 +143,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
106
143
|
epochDuration,
|
|
107
144
|
lagInEpochsForValidatorSet,
|
|
108
145
|
lagInEpochsForRandao,
|
|
146
|
+
targetCommitteeSize,
|
|
147
|
+
rollupManaLimit,
|
|
109
148
|
] = await Promise.all([
|
|
110
149
|
rollup.getL1StartBlock(),
|
|
111
150
|
rollup.getL1GenesisTime(),
|
|
@@ -114,6 +153,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
114
153
|
rollup.getEpochDuration(),
|
|
115
154
|
rollup.getLagInEpochsForValidatorSet(),
|
|
116
155
|
rollup.getLagInEpochsForRandao(),
|
|
156
|
+
rollup.getTargetCommitteeSize(),
|
|
157
|
+
rollup.getManaLimit(),
|
|
117
158
|
] as const);
|
|
118
159
|
|
|
119
160
|
const l1RollupConstants = {
|
|
@@ -125,43 +166,68 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
125
166
|
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
126
167
|
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
127
168
|
lagInEpochsForRandao: Number(lagInEpochsForRandao),
|
|
169
|
+
targetCommitteeSize: Number(targetCommitteeSize),
|
|
170
|
+
rollupManaLimit: Number(rollupManaLimit),
|
|
128
171
|
};
|
|
129
172
|
|
|
130
|
-
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider
|
|
173
|
+
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider, {
|
|
174
|
+
cacheSize: 12,
|
|
175
|
+
validatorRefreshIntervalSeconds: 60,
|
|
176
|
+
});
|
|
131
177
|
}
|
|
132
178
|
|
|
133
179
|
public getL1Constants(): L1RollupConstants {
|
|
134
180
|
return this.l1constants;
|
|
135
181
|
}
|
|
136
182
|
|
|
183
|
+
public getSlotNow(): SlotNumber {
|
|
184
|
+
return this.getEpochAndSlotNow().slot;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public getTargetSlot(): SlotNumber {
|
|
188
|
+
const slotNow = this.getSlotNow();
|
|
189
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
190
|
+
return SlotNumber(slotNow + offset);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public getEpochNow(): EpochNumber {
|
|
194
|
+
return this.getEpochAndSlotNow().epoch;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
public getTargetEpoch(): EpochNumber {
|
|
198
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1constants);
|
|
199
|
+
}
|
|
200
|
+
|
|
137
201
|
public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
138
202
|
const nowMs = BigInt(this.dateProvider.now());
|
|
139
203
|
const nowSeconds = nowMs / 1000n;
|
|
140
204
|
return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
|
|
141
205
|
}
|
|
142
206
|
|
|
143
|
-
|
|
144
|
-
return
|
|
207
|
+
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
208
|
+
return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
|
|
145
209
|
}
|
|
146
210
|
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
return {
|
|
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) };
|
|
151
215
|
}
|
|
152
216
|
|
|
153
|
-
public
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
|
|
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) };
|
|
157
222
|
}
|
|
158
223
|
|
|
159
224
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
160
225
|
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
226
|
+
const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
|
|
161
227
|
return {
|
|
162
|
-
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
163
|
-
ts: getTimestampForSlot(slot, this.l1constants),
|
|
164
228
|
slot,
|
|
229
|
+
epoch,
|
|
230
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
165
231
|
};
|
|
166
232
|
}
|
|
167
233
|
|
|
@@ -170,17 +236,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
170
236
|
return this.getCommittee(startSlot);
|
|
171
237
|
}
|
|
172
238
|
|
|
173
|
-
/**
|
|
174
|
-
* Returns whether the escape hatch is open for the given epoch.
|
|
175
|
-
*
|
|
176
|
-
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
177
|
-
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
178
|
-
*/
|
|
239
|
+
/** Returns whether the escape hatch is open for the given epoch. */
|
|
179
240
|
public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
|
|
180
|
-
const cached = this.cache.get(epoch);
|
|
181
|
-
if (cached) {
|
|
182
|
-
return cached.isEscapeHatchOpen;
|
|
183
|
-
}
|
|
184
241
|
const info = await this.getCommitteeForEpoch(epoch);
|
|
185
242
|
return info.isEscapeHatchOpen;
|
|
186
243
|
}
|
|
@@ -194,7 +251,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
194
251
|
public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
|
|
195
252
|
const epoch =
|
|
196
253
|
slot === 'now'
|
|
197
|
-
? this.
|
|
254
|
+
? this.getEpochNow()
|
|
198
255
|
: slot === 'next'
|
|
199
256
|
? this.getEpochAndSlotInNextL1Slot().epoch
|
|
200
257
|
: getEpochAtSlot(slot, this.l1constants);
|
|
@@ -203,33 +260,52 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
203
260
|
}
|
|
204
261
|
|
|
205
262
|
/**
|
|
206
|
-
* Get the current validator set
|
|
207
|
-
*
|
|
208
|
-
*
|
|
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.
|
|
209
268
|
*/
|
|
210
269
|
public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
|
|
211
270
|
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
212
271
|
|
|
213
|
-
|
|
214
|
-
|
|
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;
|
|
215
277
|
}
|
|
216
278
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return epochData;
|
|
279
|
+
// Resolved entry: return it if finalized or still fresh.
|
|
280
|
+
if (cached && (cached.finalized || !this.isStale(cached))) {
|
|
281
|
+
return cached.data;
|
|
221
282
|
}
|
|
222
|
-
this.cache.set(epoch, epochData);
|
|
223
283
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
+
}
|
|
228
296
|
|
|
229
|
-
|
|
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
|
+
}
|
|
230
306
|
}
|
|
231
307
|
|
|
232
|
-
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
308
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
|
|
233
309
|
if (slot === 'now') {
|
|
234
310
|
return this.getEpochAndSlotNow();
|
|
235
311
|
} else if (slot === 'next') {
|
|
@@ -239,22 +315,140 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
239
315
|
}
|
|
240
316
|
}
|
|
241
317
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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([
|
|
245
418
|
this.rollup.getCommitteeAt(ts),
|
|
246
419
|
this.rollup.getSampleSeedAt(ts),
|
|
247
|
-
this.rollup.client.getBlock({ includeTransactions: false })
|
|
420
|
+
prefetched?.latestBlock ?? this.rollup.client.getBlock({ includeTransactions: false }),
|
|
421
|
+
prefetched !== undefined ? prefetched.finalizedBlock : getFinalizedL1Block(this.rollup.client),
|
|
248
422
|
this.rollup.isEscapeHatchOpen(epoch),
|
|
249
423
|
]);
|
|
250
|
-
|
|
251
|
-
const
|
|
252
|
-
|
|
424
|
+
|
|
425
|
+
const samplingTs = this.getSamplingTimestamp(epoch);
|
|
426
|
+
|
|
427
|
+
if (samplingTs > latestBlock.timestamp) {
|
|
253
428
|
throw new Error(
|
|
254
|
-
`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.`,
|
|
255
432
|
);
|
|
256
433
|
}
|
|
257
|
-
|
|
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;
|
|
258
452
|
}
|
|
259
453
|
|
|
260
454
|
/**
|
|
@@ -279,17 +473,31 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
279
473
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
280
474
|
}
|
|
281
475
|
|
|
282
|
-
/** Returns the current and next L2 slot
|
|
476
|
+
/** Returns the current and next L2 slot in next eth L1 Slot. */
|
|
283
477
|
public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
284
|
-
const
|
|
478
|
+
const currentSlot = this.getSlotNow();
|
|
285
479
|
const next = this.getEpochAndSlotInNextL1Slot();
|
|
286
480
|
|
|
287
481
|
return {
|
|
288
|
-
currentSlot
|
|
482
|
+
currentSlot,
|
|
289
483
|
nextSlot: next.slot,
|
|
290
484
|
};
|
|
291
485
|
}
|
|
292
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
|
+
|
|
293
501
|
/**
|
|
294
502
|
* Get the proposer attester address in the given L2 slot
|
|
295
503
|
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
@@ -368,10 +576,11 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
368
576
|
async getRegisteredValidators(): Promise<EthAddress[]> {
|
|
369
577
|
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
370
578
|
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
371
|
-
|
|
372
|
-
|
|
579
|
+
const now = this.dateProvider.now();
|
|
580
|
+
if (validatorRefreshTime < now) {
|
|
581
|
+
const currentSet = await this.rollup.getAttesters(BigInt(Math.floor(now / 1000)));
|
|
373
582
|
this.allValidators = new Set(currentSet.map(v => v.toString()));
|
|
374
|
-
this.lastValidatorRefresh =
|
|
583
|
+
this.lastValidatorRefresh = now;
|
|
375
584
|
}
|
|
376
585
|
return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
|
|
377
586
|
}
|
|
@@ -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 = {
|
|
@@ -13,6 +19,8 @@ const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
|
|
|
13
19
|
epochDuration: 16,
|
|
14
20
|
ethereumSlotDuration: 12,
|
|
15
21
|
proofSubmissionEpochs: 2,
|
|
22
|
+
targetCommitteeSize: 48,
|
|
23
|
+
rollupManaLimit: Number.MAX_SAFE_INTEGER,
|
|
16
24
|
};
|
|
17
25
|
|
|
18
26
|
/**
|
|
@@ -112,19 +120,52 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
112
120
|
});
|
|
113
121
|
}
|
|
114
122
|
|
|
123
|
+
getSlotNow(): SlotNumber {
|
|
124
|
+
return this.currentSlot;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getTargetSlot(): SlotNumber {
|
|
128
|
+
return SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getEpochNow(): EpochNumber {
|
|
132
|
+
return getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
getTargetEpoch(): EpochNumber {
|
|
136
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1Constants);
|
|
137
|
+
}
|
|
138
|
+
|
|
115
139
|
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
116
|
-
const
|
|
117
|
-
const ts = getTimestampRangeForEpoch(
|
|
118
|
-
return {
|
|
140
|
+
const epochNow = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
141
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
142
|
+
return {
|
|
143
|
+
epoch: epochNow,
|
|
144
|
+
slot: this.currentSlot,
|
|
145
|
+
ts,
|
|
146
|
+
nowMs: ts * 1000n,
|
|
147
|
+
};
|
|
119
148
|
}
|
|
120
149
|
|
|
121
|
-
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
122
|
-
const
|
|
123
|
-
const nextSlotTs =
|
|
150
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
151
|
+
const nowTs = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
|
|
152
|
+
const nextSlotTs = nowTs + BigInt(this.l1Constants.ethereumSlotDuration);
|
|
124
153
|
const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
|
|
125
|
-
const
|
|
126
|
-
const ts = getTimestampRangeForEpoch(
|
|
127
|
-
return {
|
|
154
|
+
const epochNow = getEpochAtSlot(nextSlot, this.l1Constants);
|
|
155
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
156
|
+
return {
|
|
157
|
+
epoch: epochNow,
|
|
158
|
+
slot: nextSlot,
|
|
159
|
+
ts,
|
|
160
|
+
nowSeconds: nowTs,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
165
|
+
const result = this.getEpochAndSlotInNextL1Slot();
|
|
166
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
167
|
+
const targetSlot = SlotNumber(result.slot + offset);
|
|
168
|
+
return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1Constants) };
|
|
128
169
|
}
|
|
129
170
|
|
|
130
171
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
|
|
@@ -140,9 +181,22 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
140
181
|
}
|
|
141
182
|
|
|
142
183
|
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
184
|
+
const currentSlot = this.getSlotNow();
|
|
185
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
currentSlot,
|
|
189
|
+
nextSlot: next.slot,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
194
|
+
const targetSlot = this.getTargetSlot();
|
|
195
|
+
const next = this.getTargetEpochAndSlotInNextL1Slot();
|
|
196
|
+
|
|
143
197
|
return {
|
|
144
|
-
|
|
145
|
-
nextSlot:
|
|
198
|
+
targetSlot,
|
|
199
|
+
nextSlot: next.slot,
|
|
146
200
|
};
|
|
147
201
|
}
|
|
148
202
|
|
|
@@ -163,6 +217,10 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
163
217
|
return Promise.resolve(validators.filter(v => committeeSet.has(v.toString())));
|
|
164
218
|
}
|
|
165
219
|
|
|
220
|
+
isEscapeHatchOpen(_epoch: EpochNumber): Promise<boolean> {
|
|
221
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
222
|
+
}
|
|
223
|
+
|
|
166
224
|
isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean> {
|
|
167
225
|
return Promise.resolve(this.escapeHatchOpen);
|
|
168
226
|
}
|