@aztec/epoch-cache 0.0.1-commit.d1f2d6c → 0.0.1-commit.d20b825a7

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.
@@ -0,0 +1,242 @@
1
+ import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
4
+ import { getEpochAtSlot, getSlotAtTimestamp, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
5
+
6
+ import {
7
+ type EpochAndSlot,
8
+ type EpochCacheInterface,
9
+ type EpochCommitteeInfo,
10
+ PROPOSER_PIPELINING_SLOT_OFFSET,
11
+ type SlotTag,
12
+ } from '../epoch_cache.js';
13
+
14
+ /** Default L1 constants for testing. */
15
+ const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
16
+ l1StartBlock: 0n,
17
+ l1GenesisTime: 0n,
18
+ slotDuration: 24,
19
+ epochDuration: 16,
20
+ ethereumSlotDuration: 12,
21
+ proofSubmissionEpochs: 2,
22
+ targetCommitteeSize: 48,
23
+ rollupManaLimit: Number.MAX_SAFE_INTEGER,
24
+ };
25
+
26
+ /**
27
+ * A test implementation of EpochCacheInterface that allows manual configuration
28
+ * of committee, proposer, slot, and escape hatch state for use in tests.
29
+ *
30
+ * Unlike the real EpochCache, this class doesn't require any RPC connections
31
+ * or mock setup. Simply use the setter methods to configure the test state.
32
+ */
33
+ export class TestEpochCache implements EpochCacheInterface {
34
+ private committee: EthAddress[] = [];
35
+ private proposerAddress: EthAddress | undefined;
36
+ private currentSlot: SlotNumber = SlotNumber(0);
37
+ private escapeHatchOpen: boolean = false;
38
+ private seed: bigint = 0n;
39
+ private registeredValidators: EthAddress[] = [];
40
+ private l1Constants: L1RollupConstants;
41
+ private proposerPipeliningEnabled = false;
42
+
43
+ constructor(l1Constants: Partial<L1RollupConstants> = {}) {
44
+ this.l1Constants = { ...DEFAULT_L1_CONSTANTS, ...l1Constants };
45
+ }
46
+
47
+ /**
48
+ * Sets the committee members. Used in validation and attestation flows.
49
+ * @param committee - Array of committee member addresses.
50
+ */
51
+ setCommittee(committee: EthAddress[]): this {
52
+ this.committee = committee;
53
+ return this;
54
+ }
55
+
56
+ /**
57
+ * Sets the proposer address returned by getProposerAttesterAddressInSlot.
58
+ * @param proposer - The address of the current proposer.
59
+ */
60
+ setProposer(proposer: EthAddress | undefined): this {
61
+ this.proposerAddress = proposer;
62
+ return this;
63
+ }
64
+
65
+ /**
66
+ * Sets the current slot number.
67
+ * @param slot - The slot number to set.
68
+ */
69
+ setCurrentSlot(slot: SlotNumber): this {
70
+ this.currentSlot = slot;
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * Sets whether the escape hatch is open.
76
+ * @param open - True if escape hatch should be open.
77
+ */
78
+ setEscapeHatchOpen(open: boolean): this {
79
+ this.escapeHatchOpen = open;
80
+ return this;
81
+ }
82
+
83
+ /**
84
+ * Sets the randomness seed used for proposer selection.
85
+ * @param seed - The seed value.
86
+ */
87
+ setSeed(seed: bigint): this {
88
+ this.seed = seed;
89
+ return this;
90
+ }
91
+
92
+ /**
93
+ * Sets the list of registered validators (all validators, not just committee).
94
+ * @param validators - Array of validator addresses.
95
+ */
96
+ setRegisteredValidators(validators: EthAddress[]): this {
97
+ this.registeredValidators = validators;
98
+ return this;
99
+ }
100
+
101
+ /**
102
+ * Sets the L1 constants used for epoch/slot calculations.
103
+ * @param constants - Partial constants to override defaults.
104
+ */
105
+ setL1Constants(constants: Partial<L1RollupConstants>): this {
106
+ this.l1Constants = { ...this.l1Constants, ...constants };
107
+ return this;
108
+ }
109
+
110
+ getL1Constants(): L1RollupConstants {
111
+ return this.l1Constants;
112
+ }
113
+
114
+ setProposerPipeliningEnabled(enabled: boolean): void {
115
+ this.proposerPipeliningEnabled = enabled;
116
+ }
117
+
118
+ getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo> {
119
+ const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
120
+ return Promise.resolve({
121
+ committee: this.committee,
122
+ epoch,
123
+ seed: this.seed,
124
+ isEscapeHatchOpen: this.escapeHatchOpen,
125
+ });
126
+ }
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
+
154
+ getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
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
+ };
163
+ }
164
+
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);
168
+ const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
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) };
184
+ }
185
+
186
+ getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
187
+ // Simple encoding for testing purposes
188
+ return `0x${epoch.toString(16).padStart(64, '0')}${slot.toString(16).padStart(64, '0')}${seed.toString(16).padStart(64, '0')}`;
189
+ }
190
+
191
+ computeProposerIndex(slot: SlotNumber, _epoch: EpochNumber, _seed: bigint, size: bigint): bigint {
192
+ if (size === 0n) {
193
+ return 0n;
194
+ }
195
+ return BigInt(slot) % size;
196
+ }
197
+
198
+ getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
199
+ const currentSlot = this.getSlotNow();
200
+ const next = this.getEpochAndSlotInNextL1Slot();
201
+
202
+ return {
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,
215
+ };
216
+ }
217
+
218
+ getProposerAttesterAddressInSlot(_slot: SlotNumber): Promise<EthAddress | undefined> {
219
+ return Promise.resolve(this.proposerAddress);
220
+ }
221
+
222
+ getRegisteredValidators(): Promise<EthAddress[]> {
223
+ return Promise.resolve(this.registeredValidators);
224
+ }
225
+
226
+ isInCommittee(_slot: SlotTag, validator: EthAddress): Promise<boolean> {
227
+ return Promise.resolve(this.committee.some(v => v.equals(validator)));
228
+ }
229
+
230
+ filterInCommittee(_slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]> {
231
+ const committeeSet = new Set(this.committee.map(v => v.toString()));
232
+ return Promise.resolve(validators.filter(v => committeeSet.has(v.toString())));
233
+ }
234
+
235
+ isEscapeHatchOpen(_epoch: EpochNumber): Promise<boolean> {
236
+ return Promise.resolve(this.escapeHatchOpen);
237
+ }
238
+
239
+ isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean> {
240
+ return Promise.resolve(this.escapeHatchOpen);
241
+ }
242
+ }