@aztec/epoch-cache 0.0.1-commit.993d52e → 0.0.1-commit.9a89641
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 +78 -17
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +220 -70
- package/dest/test/test_epoch_cache.d.ts +18 -3
- package/dest/test/test_epoch_cache.d.ts.map +1 -1
- package/dest/test/test_epoch_cache.js +61 -13
- package/package.json +6 -7
- package/src/config.ts +1 -1
- package/src/epoch_cache.ts +278 -62
- package/src/test/test_epoch_cache.ts +89 -14
package/src/config.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type L1ReaderConfig, getL1ReaderConfigFromEnv } from '@aztec/ethereum/l
|
|
|
3
3
|
|
|
4
4
|
export type EpochCacheConfig = Pick<
|
|
5
5
|
L1ReaderConfig & L1ContractsConfig,
|
|
6
|
-
'l1RpcUrls' | 'l1ChainId' | 'viemPollingIntervalMS' | 'ethereumSlotDuration'
|
|
6
|
+
'l1RpcUrls' | 'l1ChainId' | 'viemPollingIntervalMS' | 'ethereumSlotDuration' | 'l1HttpTimeoutMS'
|
|
7
7
|
>;
|
|
8
8
|
|
|
9
9
|
export function getEpochCacheConfigEnvVars(): EpochCacheConfig {
|
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,18 +42,44 @@ 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[]>;
|
|
48
81
|
getL1Constants(): L1RollupConstants;
|
|
82
|
+
getLagInEpochsForValidatorSet(): number;
|
|
49
83
|
}
|
|
50
84
|
|
|
51
85
|
/**
|
|
@@ -58,8 +92,11 @@ export interface EpochCacheInterface {
|
|
|
58
92
|
* Note: This class is very dependent on the system clock being in sync.
|
|
59
93
|
*/
|
|
60
94
|
export class EpochCache implements EpochCacheInterface {
|
|
61
|
-
|
|
62
|
-
|
|
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
|
+
*/
|
|
99
|
+
protected cache: Map<EpochNumber, CachedEpochEntry | Promise<CachedEpochEntry>> = new Map();
|
|
63
100
|
private allValidators: Set<string> = new Set();
|
|
64
101
|
private lastValidatorRefresh = 0;
|
|
65
102
|
private readonly log: Logger = createLogger('epoch-cache');
|
|
@@ -93,7 +130,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
93
130
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
94
131
|
const publicClient = createPublicClient({
|
|
95
132
|
chain: chain.chainInfo,
|
|
96
|
-
transport:
|
|
133
|
+
transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
|
|
97
134
|
pollingInterval: config.viemPollingIntervalMS,
|
|
98
135
|
});
|
|
99
136
|
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
@@ -108,6 +145,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
108
145
|
lagInEpochsForValidatorSet,
|
|
109
146
|
lagInEpochsForRandao,
|
|
110
147
|
targetCommitteeSize,
|
|
148
|
+
rollupManaLimit,
|
|
111
149
|
] = await Promise.all([
|
|
112
150
|
rollup.getL1StartBlock(),
|
|
113
151
|
rollup.getL1GenesisTime(),
|
|
@@ -117,6 +155,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
117
155
|
rollup.getLagInEpochsForValidatorSet(),
|
|
118
156
|
rollup.getLagInEpochsForRandao(),
|
|
119
157
|
rollup.getTargetCommitteeSize(),
|
|
158
|
+
rollup.getManaLimit(),
|
|
120
159
|
] as const);
|
|
121
160
|
|
|
122
161
|
const l1RollupConstants = {
|
|
@@ -129,43 +168,77 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
129
168
|
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
130
169
|
lagInEpochsForRandao: Number(lagInEpochsForRandao),
|
|
131
170
|
targetCommitteeSize: Number(targetCommitteeSize),
|
|
171
|
+
rollupManaLimit: Number(rollupManaLimit),
|
|
132
172
|
};
|
|
133
173
|
|
|
134
|
-
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider
|
|
174
|
+
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider, {
|
|
175
|
+
cacheSize: 12,
|
|
176
|
+
validatorRefreshIntervalSeconds: 60,
|
|
177
|
+
});
|
|
135
178
|
}
|
|
136
179
|
|
|
137
180
|
public getL1Constants(): L1RollupConstants {
|
|
138
181
|
return this.l1constants;
|
|
139
182
|
}
|
|
140
183
|
|
|
184
|
+
/**
|
|
185
|
+
* The number of epochs by which validator-set sampling lags. The committee for epoch `E` samples the
|
|
186
|
+
* validator set as of `lagInEpochsForValidatorSet` epochs before `E` (see
|
|
187
|
+
* `ValidatorSelectionLib.stableEpochToValidatorSetSampleTime` on L1), so a newly staked validator only
|
|
188
|
+
* becomes eligible for a committee this many epochs after staking.
|
|
189
|
+
*/
|
|
190
|
+
public getLagInEpochsForValidatorSet(): number {
|
|
191
|
+
return this.l1constants.lagInEpochsForValidatorSet;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public getSlotNow(): SlotNumber {
|
|
195
|
+
return this.getEpochAndSlotNow().slot;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public getTargetSlot(): SlotNumber {
|
|
199
|
+
const slotNow = this.getSlotNow();
|
|
200
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
201
|
+
return SlotNumber(slotNow + offset);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public getEpochNow(): EpochNumber {
|
|
205
|
+
return this.getEpochAndSlotNow().epoch;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
public getTargetEpoch(): EpochNumber {
|
|
209
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1constants);
|
|
210
|
+
}
|
|
211
|
+
|
|
141
212
|
public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
142
213
|
const nowMs = BigInt(this.dateProvider.now());
|
|
143
214
|
const nowSeconds = nowMs / 1000n;
|
|
144
215
|
return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
|
|
145
216
|
}
|
|
146
217
|
|
|
147
|
-
|
|
148
|
-
return
|
|
218
|
+
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
219
|
+
return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
|
|
149
220
|
}
|
|
150
221
|
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
const
|
|
154
|
-
return {
|
|
222
|
+
public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
223
|
+
const nowSeconds = this.dateProvider.nowInSeconds();
|
|
224
|
+
const nextSlotTs = getNextL1SlotTimestamp(nowSeconds, this.l1constants);
|
|
225
|
+
return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), nowSeconds: BigInt(nowSeconds) };
|
|
155
226
|
}
|
|
156
227
|
|
|
157
|
-
public
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
|
|
228
|
+
public getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
229
|
+
const result = this.getEpochAndSlotInNextL1Slot();
|
|
230
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
231
|
+
const targetSlot = SlotNumber(result.slot + offset);
|
|
232
|
+
return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1constants) };
|
|
161
233
|
}
|
|
162
234
|
|
|
163
235
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
164
236
|
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
237
|
+
const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
|
|
165
238
|
return {
|
|
166
|
-
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
167
|
-
ts: getTimestampForSlot(slot, this.l1constants),
|
|
168
239
|
slot,
|
|
240
|
+
epoch,
|
|
241
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
169
242
|
};
|
|
170
243
|
}
|
|
171
244
|
|
|
@@ -174,17 +247,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
174
247
|
return this.getCommittee(startSlot);
|
|
175
248
|
}
|
|
176
249
|
|
|
177
|
-
/**
|
|
178
|
-
* Returns whether the escape hatch is open for the given epoch.
|
|
179
|
-
*
|
|
180
|
-
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
181
|
-
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
182
|
-
*/
|
|
250
|
+
/** Returns whether the escape hatch is open for the given epoch. */
|
|
183
251
|
public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
|
|
184
|
-
const cached = this.cache.get(epoch);
|
|
185
|
-
if (cached) {
|
|
186
|
-
return cached.isEscapeHatchOpen;
|
|
187
|
-
}
|
|
188
252
|
const info = await this.getCommitteeForEpoch(epoch);
|
|
189
253
|
return info.isEscapeHatchOpen;
|
|
190
254
|
}
|
|
@@ -198,7 +262,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
198
262
|
public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
|
|
199
263
|
const epoch =
|
|
200
264
|
slot === 'now'
|
|
201
|
-
? this.
|
|
265
|
+
? this.getEpochNow()
|
|
202
266
|
: slot === 'next'
|
|
203
267
|
? this.getEpochAndSlotInNextL1Slot().epoch
|
|
204
268
|
: getEpochAtSlot(slot, this.l1constants);
|
|
@@ -207,33 +271,52 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
207
271
|
}
|
|
208
272
|
|
|
209
273
|
/**
|
|
210
|
-
* Get the current validator set
|
|
211
|
-
*
|
|
212
|
-
*
|
|
274
|
+
* Get the current validator set.
|
|
275
|
+
*
|
|
276
|
+
* Returns cached data if the entry is finalized or still fresh (queried less than one
|
|
277
|
+
* Ethereum slot ago). Stale non-finalized entries are re-queried, and concurrent callers
|
|
278
|
+
* coalesce on the same in-flight promise so the L1 query happens only once.
|
|
213
279
|
*/
|
|
214
280
|
public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
|
|
215
281
|
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
216
282
|
|
|
217
|
-
|
|
218
|
-
|
|
283
|
+
const cached = this.cache.get(epoch);
|
|
284
|
+
|
|
285
|
+
// In-flight promise: another caller is already fetching this epoch — just await it.
|
|
286
|
+
if (cached instanceof Promise) {
|
|
287
|
+
return (await cached).data;
|
|
219
288
|
}
|
|
220
289
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return epochData;
|
|
290
|
+
// Resolved entry: return it if finalized or still fresh.
|
|
291
|
+
if (cached && (cached.finalized || !this.isStale(cached))) {
|
|
292
|
+
return cached.data;
|
|
225
293
|
}
|
|
226
|
-
this.cache.set(epoch, epochData);
|
|
227
294
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
295
|
+
// Stale non-finalized entry: do a lightweight refresh first (check block hash + finalized ts).
|
|
296
|
+
// Only fall back to a full re-fetch if the L1 block was reorged.
|
|
297
|
+
if (cached) {
|
|
298
|
+
const promise = this.refreshStaleEntry(cached, epoch, ts);
|
|
299
|
+
this.cache.set(epoch, promise);
|
|
300
|
+
try {
|
|
301
|
+
return (await promise).data;
|
|
302
|
+
} catch (err) {
|
|
303
|
+
this.cache.set(epoch, cached);
|
|
304
|
+
throw err;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
232
307
|
|
|
233
|
-
|
|
308
|
+
// No entry at all: full fetch.
|
|
309
|
+
const promise = this.fetchAndCache(epoch, ts);
|
|
310
|
+
this.cache.set(epoch, promise);
|
|
311
|
+
try {
|
|
312
|
+
return (await promise).data;
|
|
313
|
+
} catch (err) {
|
|
314
|
+
this.cache.delete(epoch);
|
|
315
|
+
throw err;
|
|
316
|
+
}
|
|
234
317
|
}
|
|
235
318
|
|
|
236
|
-
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
319
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
|
|
237
320
|
if (slot === 'now') {
|
|
238
321
|
return this.getEpochAndSlotNow();
|
|
239
322
|
} else if (slot === 'next') {
|
|
@@ -243,22 +326,140 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
243
326
|
}
|
|
244
327
|
}
|
|
245
328
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
329
|
+
/** Evicts oldest cache entries (resolved or in-flight) beyond cacheSize. */
|
|
330
|
+
private purgeCache(): void {
|
|
331
|
+
if (this.cache.size <= this.config.cacheSize) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const toPurge = Array.from(this.cache.keys())
|
|
335
|
+
.sort((a, b) => Number(b - a))
|
|
336
|
+
.slice(this.config.cacheSize);
|
|
337
|
+
toPurge.forEach(key => this.cache.delete(key));
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Returns true if a non-finalized cache entry is older than one Ethereum slot. */
|
|
341
|
+
private isStale(entry: CachedEpochEntry): boolean {
|
|
342
|
+
const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
|
|
343
|
+
return nowSeconds - entry.lastRefreshL1Timestamp >= BigInt(this.l1constants.ethereumSlotDuration);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Whether a cached epoch entry has been marked as finalized. Returns undefined if not cached or still in-flight. */
|
|
347
|
+
public isFinalized(epoch: EpochNumber): boolean | undefined {
|
|
348
|
+
const entry = this.cache.get(epoch);
|
|
349
|
+
if (!entry || entry instanceof Promise) {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
return entry.finalized;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Returns the latest L1 timestamp stored in the cached entry. Undefined if not cached or in-flight. */
|
|
356
|
+
public getCachedLastRefreshL1Timestamp(epoch: EpochNumber): bigint | undefined {
|
|
357
|
+
const entry = this.cache.get(epoch);
|
|
358
|
+
if (!entry || entry instanceof Promise) {
|
|
359
|
+
return undefined;
|
|
360
|
+
}
|
|
361
|
+
return entry.lastRefreshL1Timestamp;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Computes the sampling timestamp for an epoch's committee data. */
|
|
365
|
+
private getSamplingTimestamp(epoch: EpochNumber): bigint {
|
|
366
|
+
const { lagInEpochsForRandao, epochDuration, slotDuration } = this.l1constants;
|
|
367
|
+
const epochStartTs = getStartTimestampForEpoch(epoch, this.l1constants);
|
|
368
|
+
return epochStartTs - BigInt(lagInEpochsForRandao) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Lightweight refresh for a stale non-finalized entry. Queries only the block hash at
|
|
373
|
+
* the original block number and the finalized block timestamp — avoids the expensive
|
|
374
|
+
* getCommitteeAt and getSampleSeedAt calls on the rollup contract.
|
|
375
|
+
*
|
|
376
|
+
* If the block hash still matches (no L1 reorg), we keep the existing data and just
|
|
377
|
+
* update the provenance timestamp. If the finalized block has caught up, we promote the
|
|
378
|
+
* entry to finalized. If there was a reorg (hash mismatch), we fall back to a full fetch.
|
|
379
|
+
*/
|
|
380
|
+
private async refreshStaleEntry(stale: CachedEpochEntry, epoch: EpochNumber, ts: bigint): Promise<CachedEpochEntry> {
|
|
381
|
+
const [blockAtOriginal, l1FinalizedBlock, latestBlock] = await Promise.all([
|
|
382
|
+
this.rollup.client.getBlock({ blockNumber: stale.lastQueryL1BlockNumber, includeTransactions: false }),
|
|
383
|
+
getFinalizedL1Block(this.rollup.client),
|
|
384
|
+
this.rollup.client.getBlock({ includeTransactions: false }),
|
|
385
|
+
]);
|
|
386
|
+
|
|
387
|
+
if (blockAtOriginal.hash === stale.lastQueryL1BlockHash) {
|
|
388
|
+
// No reorg: the data is still valid. Check if we can now mark it as finalized.
|
|
389
|
+
const samplingTs = this.getSamplingTimestamp(epoch);
|
|
390
|
+
const finalized =
|
|
391
|
+
!!(stale.data.committee && stale.data.committee.length > 0) &&
|
|
392
|
+
l1FinalizedBlock !== undefined &&
|
|
393
|
+
samplingTs <= l1FinalizedBlock.timestamp;
|
|
394
|
+
|
|
395
|
+
const refreshed: CachedEpochEntry = {
|
|
396
|
+
...stale,
|
|
397
|
+
lastRefreshL1Timestamp: latestBlock.timestamp,
|
|
398
|
+
finalized,
|
|
399
|
+
};
|
|
400
|
+
this.cache.set(epoch, refreshed);
|
|
401
|
+
return refreshed;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Reorg detected: block hash mismatch. Do a full re-fetch.
|
|
405
|
+
// Pass the already-fetched block timestamps to avoid redundant queries.
|
|
406
|
+
this.log.warn(`L1 reorg detected for epoch ${epoch}: block ${stale.lastQueryL1BlockNumber} hash changed`, {
|
|
407
|
+
epoch,
|
|
408
|
+
expectedHash: stale.lastQueryL1BlockHash,
|
|
409
|
+
actualHash: blockAtOriginal.hash,
|
|
410
|
+
});
|
|
411
|
+
return this.fetchAndCache(epoch, ts, { latestBlock, finalizedBlock: l1FinalizedBlock });
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Fetches committee data from L1, determines finalization status, and stores in the cache.
|
|
416
|
+
*
|
|
417
|
+
* Uses `lagInEpochsForRandao` (the binding constraint, always <= lagInEpochsForValidatorSet)
|
|
418
|
+
* and computes the sampling timestamp from the epoch start to match the L1 contract's logic.
|
|
419
|
+
*
|
|
420
|
+
* When called from refreshStaleEntry after a reorg, the latest and finalized blocks are
|
|
421
|
+
* passed in to avoid redundant L1 queries.
|
|
422
|
+
*/
|
|
423
|
+
private async fetchAndCache(
|
|
424
|
+
epoch: EpochNumber,
|
|
425
|
+
ts: bigint,
|
|
426
|
+
prefetched?: { latestBlock: L1BlockInfo; finalizedBlock: { timestamp: bigint } | undefined },
|
|
427
|
+
): Promise<CachedEpochEntry> {
|
|
428
|
+
const [committee, seedBuffer, latestBlock, finalizedBlock, isEscapeHatchOpen] = await Promise.all([
|
|
249
429
|
this.rollup.getCommitteeAt(ts),
|
|
250
430
|
this.rollup.getSampleSeedAt(ts),
|
|
251
|
-
this.rollup.client.getBlock({ includeTransactions: false })
|
|
431
|
+
prefetched?.latestBlock ?? this.rollup.client.getBlock({ includeTransactions: false }),
|
|
432
|
+
prefetched !== undefined ? prefetched.finalizedBlock : getFinalizedL1Block(this.rollup.client),
|
|
252
433
|
this.rollup.isEscapeHatchOpen(epoch),
|
|
253
434
|
]);
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
|
|
435
|
+
|
|
436
|
+
const samplingTs = this.getSamplingTimestamp(epoch);
|
|
437
|
+
|
|
438
|
+
if (samplingTs > latestBlock.timestamp) {
|
|
257
439
|
throw new Error(
|
|
258
|
-
`Cannot query committee for future epoch ${epoch}
|
|
440
|
+
`Cannot query committee for future epoch ${epoch}: ` +
|
|
441
|
+
`sampling timestamp ${samplingTs} is beyond latest L1 block at ${latestBlock.timestamp}. ` +
|
|
442
|
+
`Check your Ethereum node is synced.`,
|
|
259
443
|
);
|
|
260
444
|
}
|
|
261
|
-
|
|
445
|
+
|
|
446
|
+
// Empty committees are never marked finalized so they always get re-queried after TTL.
|
|
447
|
+
// If L1 has no finalized block yet (devnet startup), entries stay unfinalized.
|
|
448
|
+
const hasCommittee = !!(committee && committee.length > 0);
|
|
449
|
+
const finalized = hasCommittee && finalizedBlock !== undefined && samplingTs <= finalizedBlock.timestamp;
|
|
450
|
+
const data: EpochCommitteeInfo = { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
|
|
451
|
+
const entry: CachedEpochEntry = {
|
|
452
|
+
data,
|
|
453
|
+
lastQueryL1BlockNumber: latestBlock.number!,
|
|
454
|
+
lastQueryL1BlockHash: latestBlock.hash!,
|
|
455
|
+
lastRefreshL1Timestamp: latestBlock.timestamp,
|
|
456
|
+
finalized,
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
this.cache.set(epoch, entry);
|
|
460
|
+
this.purgeCache();
|
|
461
|
+
|
|
462
|
+
return entry;
|
|
262
463
|
}
|
|
263
464
|
|
|
264
465
|
/**
|
|
@@ -283,17 +484,31 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
283
484
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
284
485
|
}
|
|
285
486
|
|
|
286
|
-
/** Returns the current and next L2 slot
|
|
487
|
+
/** Returns the current and next L2 slot in next eth L1 Slot. */
|
|
287
488
|
public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
288
|
-
const
|
|
489
|
+
const currentSlot = this.getSlotNow();
|
|
289
490
|
const next = this.getEpochAndSlotInNextL1Slot();
|
|
290
491
|
|
|
291
492
|
return {
|
|
292
|
-
currentSlot
|
|
493
|
+
currentSlot,
|
|
293
494
|
nextSlot: next.slot,
|
|
294
495
|
};
|
|
295
496
|
}
|
|
296
497
|
|
|
498
|
+
/** Returns the target and next L2 slot in the next L1 slot. */
|
|
499
|
+
public getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
500
|
+
const nowSeconds = BigInt(this.dateProvider.nowInSeconds());
|
|
501
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
502
|
+
|
|
503
|
+
const currentSlot = getSlotAtTimestamp(nowSeconds, this.l1constants);
|
|
504
|
+
const targetSlot = SlotNumber(currentSlot + offset);
|
|
505
|
+
|
|
506
|
+
const nextL2SlotOnL1 = getSlotAtNextL1Block(nowSeconds, this.l1constants);
|
|
507
|
+
const nextSlot = SlotNumber(nextL2SlotOnL1 + offset);
|
|
508
|
+
|
|
509
|
+
return { targetSlot, nextSlot };
|
|
510
|
+
}
|
|
511
|
+
|
|
297
512
|
/**
|
|
298
513
|
* Get the proposer attester address in the given L2 slot
|
|
299
514
|
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
@@ -372,10 +587,11 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
372
587
|
async getRegisteredValidators(): Promise<EthAddress[]> {
|
|
373
588
|
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
374
589
|
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
375
|
-
|
|
376
|
-
|
|
590
|
+
const now = this.dateProvider.now();
|
|
591
|
+
if (validatorRefreshTime < now) {
|
|
592
|
+
const currentSet = await this.rollup.getAttesters(BigInt(Math.floor(now / 1000)));
|
|
377
593
|
this.allValidators = new Set(currentSet.map(v => v.toString()));
|
|
378
|
-
this.lastValidatorRefresh =
|
|
594
|
+
this.lastValidatorRefresh = now;
|
|
379
595
|
}
|
|
380
596
|
return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
|
|
381
597
|
}
|
|
@@ -1,9 +1,20 @@
|
|
|
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 {
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import {
|
|
5
|
+
getEpochAtSlot,
|
|
6
|
+
getSlotAtTimestamp,
|
|
7
|
+
getTimestampForSlot,
|
|
8
|
+
getTimestampRangeForEpoch,
|
|
9
|
+
} from '@aztec/stdlib/epoch-helpers';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
type EpochAndSlot,
|
|
13
|
+
type EpochCacheInterface,
|
|
14
|
+
type EpochCommitteeInfo,
|
|
15
|
+
PROPOSER_PIPELINING_SLOT_OFFSET,
|
|
16
|
+
type SlotTag,
|
|
17
|
+
} from '../epoch_cache.js';
|
|
7
18
|
|
|
8
19
|
/** Default L1 constants for testing. */
|
|
9
20
|
const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
|
|
@@ -14,6 +25,7 @@ const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
|
|
|
14
25
|
ethereumSlotDuration: 12,
|
|
15
26
|
proofSubmissionEpochs: 2,
|
|
16
27
|
targetCommitteeSize: 48,
|
|
28
|
+
rollupManaLimit: Number.MAX_SAFE_INTEGER,
|
|
17
29
|
};
|
|
18
30
|
|
|
19
31
|
/**
|
|
@@ -31,11 +43,17 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
31
43
|
private seed: bigint = 0n;
|
|
32
44
|
private registeredValidators: EthAddress[] = [];
|
|
33
45
|
private l1Constants: L1RollupConstants;
|
|
46
|
+
private lagInEpochsForValidatorSet = 2;
|
|
34
47
|
|
|
35
48
|
constructor(l1Constants: Partial<L1RollupConstants> = {}) {
|
|
36
49
|
this.l1Constants = { ...DEFAULT_L1_CONSTANTS, ...l1Constants };
|
|
37
50
|
}
|
|
38
51
|
|
|
52
|
+
setLagInEpochsForValidatorSet(lag: number): this {
|
|
53
|
+
this.lagInEpochsForValidatorSet = lag;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
|
|
39
57
|
/**
|
|
40
58
|
* Sets the committee members. Used in validation and attestation flows.
|
|
41
59
|
* @param committee - Array of committee member addresses.
|
|
@@ -103,6 +121,10 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
103
121
|
return this.l1Constants;
|
|
104
122
|
}
|
|
105
123
|
|
|
124
|
+
getLagInEpochsForValidatorSet(): number {
|
|
125
|
+
return this.lagInEpochsForValidatorSet;
|
|
126
|
+
}
|
|
127
|
+
|
|
106
128
|
getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo> {
|
|
107
129
|
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
108
130
|
return Promise.resolve({
|
|
@@ -113,19 +135,55 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
113
135
|
});
|
|
114
136
|
}
|
|
115
137
|
|
|
138
|
+
getSlotNow(): SlotNumber {
|
|
139
|
+
return this.currentSlot;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getTargetSlot(): SlotNumber {
|
|
143
|
+
return SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getEpochNow(): EpochNumber {
|
|
147
|
+
return getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
getTargetEpoch(): EpochNumber {
|
|
151
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1Constants);
|
|
152
|
+
}
|
|
153
|
+
|
|
116
154
|
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
155
|
+
// Model "now" as the start of the current slot (mirroring the real EpochCache, which derives nowMs
|
|
156
|
+
// from the wall clock). Using the slot start rather than the epoch start keeps nowMs consistent with
|
|
157
|
+
// currentSlot, which the pipelining receive-window check (clock_tolerance) relies on.
|
|
158
|
+
const epochNow = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
159
|
+
const ts = getTimestampForSlot(this.currentSlot, this.l1Constants);
|
|
160
|
+
return {
|
|
161
|
+
epoch: epochNow,
|
|
162
|
+
slot: this.currentSlot,
|
|
163
|
+
ts,
|
|
164
|
+
nowMs: ts * 1000n,
|
|
165
|
+
};
|
|
120
166
|
}
|
|
121
167
|
|
|
122
|
-
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
123
|
-
const
|
|
124
|
-
const nextSlotTs =
|
|
168
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
169
|
+
const nowTs = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
|
|
170
|
+
const nextSlotTs = nowTs + BigInt(this.l1Constants.ethereumSlotDuration);
|
|
125
171
|
const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
|
|
126
|
-
const
|
|
127
|
-
const ts = getTimestampRangeForEpoch(
|
|
128
|
-
return {
|
|
172
|
+
const epochNow = getEpochAtSlot(nextSlot, this.l1Constants);
|
|
173
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
174
|
+
return {
|
|
175
|
+
epoch: epochNow,
|
|
176
|
+
slot: nextSlot,
|
|
177
|
+
ts,
|
|
178
|
+
nowSeconds: nowTs,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
183
|
+
const result = this.getEpochAndSlotInNextL1Slot();
|
|
184
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
185
|
+
const targetSlot = SlotNumber(result.slot + offset);
|
|
186
|
+
return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1Constants) };
|
|
129
187
|
}
|
|
130
188
|
|
|
131
189
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
|
|
@@ -141,9 +199,22 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
141
199
|
}
|
|
142
200
|
|
|
143
201
|
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
202
|
+
const currentSlot = this.getSlotNow();
|
|
203
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
204
|
+
|
|
144
205
|
return {
|
|
145
|
-
currentSlot
|
|
146
|
-
nextSlot:
|
|
206
|
+
currentSlot,
|
|
207
|
+
nextSlot: next.slot,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
212
|
+
const targetSlot = this.getTargetSlot();
|
|
213
|
+
const next = this.getTargetEpochAndSlotInNextL1Slot();
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
targetSlot,
|
|
217
|
+
nextSlot: next.slot,
|
|
147
218
|
};
|
|
148
219
|
}
|
|
149
220
|
|
|
@@ -164,6 +235,10 @@ export class TestEpochCache implements EpochCacheInterface {
|
|
|
164
235
|
return Promise.resolve(validators.filter(v => committeeSet.has(v.toString())));
|
|
165
236
|
}
|
|
166
237
|
|
|
238
|
+
isEscapeHatchOpen(_epoch: EpochNumber): Promise<boolean> {
|
|
239
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
240
|
+
}
|
|
241
|
+
|
|
167
242
|
isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean> {
|
|
168
243
|
return Promise.resolve(this.escapeHatchOpen);
|
|
169
244
|
}
|