@aztec/epoch-cache 0.0.1-commit.5daedc8 → 0.0.1-commit.5de5ca79e
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/dest/config.d.ts +5 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +5 -2
- package/dest/epoch_cache.d.ts +66 -21
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +111 -43
- package/dest/test/index.d.ts +2 -0
- package/dest/test/index.d.ts.map +1 -0
- package/dest/test/index.js +1 -0
- package/dest/test/test_epoch_cache.d.ts +91 -0
- package/dest/test/test_epoch_cache.d.ts.map +1 -0
- package/dest/test/test_epoch_cache.js +195 -0
- package/package.json +9 -10
- package/src/config.ts +11 -9
- package/src/epoch_cache.ts +141 -49
- package/src/test/index.ts +1 -0
- package/src/test/test_epoch_cache.ts +238 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { getEpochAtSlot, getSlotAtTimestamp, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
3
|
+
import { PROPOSER_PIPELINING_SLOT_OFFSET } from '../epoch_cache.js';
|
|
4
|
+
/** Default L1 constants for testing. */ const DEFAULT_L1_CONSTANTS = {
|
|
5
|
+
l1StartBlock: 0n,
|
|
6
|
+
l1GenesisTime: 0n,
|
|
7
|
+
slotDuration: 24,
|
|
8
|
+
epochDuration: 16,
|
|
9
|
+
ethereumSlotDuration: 12,
|
|
10
|
+
proofSubmissionEpochs: 2,
|
|
11
|
+
targetCommitteeSize: 48,
|
|
12
|
+
rollupManaLimit: Number.MAX_SAFE_INTEGER
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A test implementation of EpochCacheInterface that allows manual configuration
|
|
16
|
+
* of committee, proposer, slot, and escape hatch state for use in tests.
|
|
17
|
+
*
|
|
18
|
+
* Unlike the real EpochCache, this class doesn't require any RPC connections
|
|
19
|
+
* or mock setup. Simply use the setter methods to configure the test state.
|
|
20
|
+
*/ export class TestEpochCache {
|
|
21
|
+
committee = [];
|
|
22
|
+
proposerAddress;
|
|
23
|
+
currentSlot = SlotNumber(0);
|
|
24
|
+
escapeHatchOpen = false;
|
|
25
|
+
seed = 0n;
|
|
26
|
+
registeredValidators = [];
|
|
27
|
+
l1Constants;
|
|
28
|
+
proposerPipeliningEnabled = false;
|
|
29
|
+
constructor(l1Constants = {}){
|
|
30
|
+
this.l1Constants = {
|
|
31
|
+
...DEFAULT_L1_CONSTANTS,
|
|
32
|
+
...l1Constants
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sets the committee members. Used in validation and attestation flows.
|
|
37
|
+
* @param committee - Array of committee member addresses.
|
|
38
|
+
*/ setCommittee(committee) {
|
|
39
|
+
this.committee = committee;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Sets the proposer address returned by getProposerAttesterAddressInSlot.
|
|
44
|
+
* @param proposer - The address of the current proposer.
|
|
45
|
+
*/ setProposer(proposer) {
|
|
46
|
+
this.proposerAddress = proposer;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Sets the current slot number.
|
|
51
|
+
* @param slot - The slot number to set.
|
|
52
|
+
*/ setCurrentSlot(slot) {
|
|
53
|
+
this.currentSlot = slot;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sets whether the escape hatch is open.
|
|
58
|
+
* @param open - True if escape hatch should be open.
|
|
59
|
+
*/ setEscapeHatchOpen(open) {
|
|
60
|
+
this.escapeHatchOpen = open;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Sets the randomness seed used for proposer selection.
|
|
65
|
+
* @param seed - The seed value.
|
|
66
|
+
*/ setSeed(seed) {
|
|
67
|
+
this.seed = seed;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Sets the list of registered validators (all validators, not just committee).
|
|
72
|
+
* @param validators - Array of validator addresses.
|
|
73
|
+
*/ setRegisteredValidators(validators) {
|
|
74
|
+
this.registeredValidators = validators;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Sets the L1 constants used for epoch/slot calculations.
|
|
79
|
+
* @param constants - Partial constants to override defaults.
|
|
80
|
+
*/ setL1Constants(constants) {
|
|
81
|
+
this.l1Constants = {
|
|
82
|
+
...this.l1Constants,
|
|
83
|
+
...constants
|
|
84
|
+
};
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
getL1Constants() {
|
|
88
|
+
return this.l1Constants;
|
|
89
|
+
}
|
|
90
|
+
setProposerPipeliningEnabled(enabled) {
|
|
91
|
+
this.proposerPipeliningEnabled = enabled;
|
|
92
|
+
}
|
|
93
|
+
getCommittee(_slot) {
|
|
94
|
+
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
95
|
+
return Promise.resolve({
|
|
96
|
+
committee: this.committee,
|
|
97
|
+
epoch,
|
|
98
|
+
seed: this.seed,
|
|
99
|
+
isEscapeHatchOpen: this.escapeHatchOpen
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
getSlotNow() {
|
|
103
|
+
return this.currentSlot;
|
|
104
|
+
}
|
|
105
|
+
getTargetSlot() {
|
|
106
|
+
return this.proposerPipeliningEnabled ? SlotNumber(this.currentSlot + PROPOSER_PIPELINING_SLOT_OFFSET) : this.currentSlot;
|
|
107
|
+
}
|
|
108
|
+
getEpochNow() {
|
|
109
|
+
return getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
110
|
+
}
|
|
111
|
+
getTargetEpoch() {
|
|
112
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1Constants);
|
|
113
|
+
}
|
|
114
|
+
isProposerPipeliningEnabled() {
|
|
115
|
+
return this.proposerPipeliningEnabled;
|
|
116
|
+
}
|
|
117
|
+
getEpochAndSlotNow() {
|
|
118
|
+
const epochNow = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
119
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
120
|
+
return {
|
|
121
|
+
epoch: epochNow,
|
|
122
|
+
slot: this.currentSlot,
|
|
123
|
+
ts,
|
|
124
|
+
nowMs: ts * 1000n
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
getEpochAndSlotInNextL1Slot() {
|
|
128
|
+
const nowTs = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
|
|
129
|
+
const nextSlotTs = nowTs + BigInt(this.l1Constants.ethereumSlotDuration);
|
|
130
|
+
const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
|
|
131
|
+
const epochNow = getEpochAtSlot(nextSlot, this.l1Constants);
|
|
132
|
+
const ts = getTimestampRangeForEpoch(epochNow, this.l1Constants)[0];
|
|
133
|
+
return {
|
|
134
|
+
epoch: epochNow,
|
|
135
|
+
slot: nextSlot,
|
|
136
|
+
ts,
|
|
137
|
+
nowSeconds: nowTs
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
getTargetEpochAndSlotInNextL1Slot() {
|
|
141
|
+
const result = this.getEpochAndSlotInNextL1Slot();
|
|
142
|
+
const offset = this.isProposerPipeliningEnabled() ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
|
|
143
|
+
const targetSlot = SlotNumber(result.slot + offset);
|
|
144
|
+
return {
|
|
145
|
+
...result,
|
|
146
|
+
slot: targetSlot,
|
|
147
|
+
epoch: getEpochAtSlot(targetSlot, this.l1Constants)
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
getProposerIndexEncoding(epoch, slot, seed) {
|
|
151
|
+
// Simple encoding for testing purposes
|
|
152
|
+
return `0x${epoch.toString(16).padStart(64, '0')}${slot.toString(16).padStart(64, '0')}${seed.toString(16).padStart(64, '0')}`;
|
|
153
|
+
}
|
|
154
|
+
computeProposerIndex(slot, _epoch, _seed, size) {
|
|
155
|
+
if (size === 0n) {
|
|
156
|
+
return 0n;
|
|
157
|
+
}
|
|
158
|
+
return BigInt(slot) % size;
|
|
159
|
+
}
|
|
160
|
+
getCurrentAndNextSlot() {
|
|
161
|
+
const currentSlot = this.getSlotNow();
|
|
162
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
163
|
+
return {
|
|
164
|
+
currentSlot,
|
|
165
|
+
nextSlot: next.slot
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
getTargetAndNextSlot() {
|
|
169
|
+
const targetSlot = this.getTargetSlot();
|
|
170
|
+
const next = this.getTargetEpochAndSlotInNextL1Slot();
|
|
171
|
+
return {
|
|
172
|
+
targetSlot,
|
|
173
|
+
nextSlot: next.slot
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
getProposerAttesterAddressInSlot(_slot) {
|
|
177
|
+
return Promise.resolve(this.proposerAddress);
|
|
178
|
+
}
|
|
179
|
+
getRegisteredValidators() {
|
|
180
|
+
return Promise.resolve(this.registeredValidators);
|
|
181
|
+
}
|
|
182
|
+
isInCommittee(_slot, validator) {
|
|
183
|
+
return Promise.resolve(this.committee.some((v)=>v.equals(validator)));
|
|
184
|
+
}
|
|
185
|
+
filterInCommittee(_slot, validators) {
|
|
186
|
+
const committeeSet = new Set(this.committee.map((v)=>v.toString()));
|
|
187
|
+
return Promise.resolve(validators.filter((v)=>committeeSet.has(v.toString())));
|
|
188
|
+
}
|
|
189
|
+
isEscapeHatchOpen(_epoch) {
|
|
190
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
191
|
+
}
|
|
192
|
+
isEscapeHatchOpenAtSlot(_slot) {
|
|
193
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
194
|
+
}
|
|
195
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/epoch-cache",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.5de5ca79e",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
"tsconfig": "./tsconfig.json"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "yarn clean &&
|
|
19
|
-
"build:dev": "
|
|
18
|
+
"build": "yarn clean && ../scripts/tsc.sh",
|
|
19
|
+
"build:dev": "../scripts/tsc.sh --watch",
|
|
20
20
|
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
21
|
-
"start:dev": "concurrently -k \"
|
|
21
|
+
"start:dev": "concurrently -k \"../scripts/tsc.sh --watch\" \"nodemon --watch dest --exec yarn start\"",
|
|
22
22
|
"start": "node ./dest/index.js",
|
|
23
23
|
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
24
24
|
},
|
|
@@ -26,11 +26,10 @@
|
|
|
26
26
|
"../package.common.json"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
30
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
31
|
-
"@aztec/l1-artifacts": "0.0.1-commit.
|
|
32
|
-
"@aztec/stdlib": "0.0.1-commit.
|
|
33
|
-
"@viem/anvil": "^0.0.10",
|
|
29
|
+
"@aztec/ethereum": "0.0.1-commit.5de5ca79e",
|
|
30
|
+
"@aztec/foundation": "0.0.1-commit.5de5ca79e",
|
|
31
|
+
"@aztec/l1-artifacts": "0.0.1-commit.5de5ca79e",
|
|
32
|
+
"@aztec/stdlib": "0.0.1-commit.5de5ca79e",
|
|
34
33
|
"dotenv": "^16.0.3",
|
|
35
34
|
"get-port": "^7.1.0",
|
|
36
35
|
"jest-mock-extended": "^4.0.0",
|
|
@@ -42,7 +41,7 @@
|
|
|
42
41
|
"@jest/globals": "^30.0.0",
|
|
43
42
|
"@types/jest": "^30.0.0",
|
|
44
43
|
"@types/node": "^22.15.17",
|
|
45
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
44
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
46
45
|
"jest": "^30.0.0",
|
|
47
46
|
"ts-node": "^10.9.1",
|
|
48
47
|
"typescript": "^5.3.3"
|
package/src/config.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
getL1ContractsConfigEnvVars,
|
|
5
|
-
getL1ReaderConfigFromEnv,
|
|
6
|
-
} from '@aztec/ethereum';
|
|
1
|
+
import { type L1ContractsConfig, getL1ContractsConfigEnvVars } from '@aztec/ethereum/config';
|
|
2
|
+
import { type L1ReaderConfig, getL1ReaderConfigFromEnv } from '@aztec/ethereum/l1-reader';
|
|
3
|
+
import { type PipelineConfig, getPipelineConfigEnvVars } from '@aztec/stdlib/config';
|
|
7
4
|
|
|
8
5
|
export type EpochCacheConfig = Pick<
|
|
9
|
-
L1ReaderConfig & L1ContractsConfig,
|
|
10
|
-
|
|
6
|
+
L1ReaderConfig & L1ContractsConfig & PipelineConfig,
|
|
7
|
+
| 'l1RpcUrls'
|
|
8
|
+
| 'l1ChainId'
|
|
9
|
+
| 'viemPollingIntervalMS'
|
|
10
|
+
| 'ethereumSlotDuration'
|
|
11
|
+
| 'l1HttpTimeoutMS'
|
|
12
|
+
| 'enableProposerPipelining'
|
|
11
13
|
>;
|
|
12
14
|
|
|
13
15
|
export function getEpochCacheConfigEnvVars(): EpochCacheConfig {
|
|
14
|
-
return { ...getL1ReaderConfigFromEnv(), ...getL1ContractsConfigEnvVars() };
|
|
16
|
+
return { ...getL1ReaderConfigFromEnv(), ...getL1ContractsConfigEnvVars(), ...getPipelineConfigEnvVars() };
|
|
15
17
|
}
|
package/src/epoch_cache.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
2
|
+
import { makeL1HttpTransport } from '@aztec/ethereum/client';
|
|
3
|
+
import { NoCommitteeError, RollupContract } from '@aztec/ethereum/contracts';
|
|
2
4
|
import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
6
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
@@ -10,16 +12,19 @@ import {
|
|
|
10
12
|
getSlotAtTimestamp,
|
|
11
13
|
getSlotRangeForEpoch,
|
|
12
14
|
getTimestampForSlot,
|
|
13
|
-
getTimestampRangeForEpoch,
|
|
14
15
|
} from '@aztec/stdlib/epoch-helpers';
|
|
15
16
|
|
|
16
|
-
import { createPublicClient, encodeAbiParameters,
|
|
17
|
+
import { createPublicClient, encodeAbiParameters, keccak256 } from 'viem';
|
|
17
18
|
|
|
18
19
|
import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
|
|
19
20
|
|
|
21
|
+
/** When proposer pipelining is enabled, the proposer builds one slot ahead. */
|
|
22
|
+
export const PROPOSER_PIPELINING_SLOT_OFFSET = 1;
|
|
23
|
+
|
|
24
|
+
/** Flat return type for compound epoch/slot getters. */
|
|
20
25
|
export type EpochAndSlot = {
|
|
21
|
-
epoch: EpochNumber;
|
|
22
26
|
slot: SlotNumber;
|
|
27
|
+
epoch: EpochNumber;
|
|
23
28
|
ts: bigint;
|
|
24
29
|
};
|
|
25
30
|
|
|
@@ -27,25 +32,34 @@ export type EpochCommitteeInfo = {
|
|
|
27
32
|
committee: EthAddress[] | undefined;
|
|
28
33
|
seed: bigint;
|
|
29
34
|
epoch: EpochNumber;
|
|
35
|
+
/** True if the epoch is within an open escape hatch window. */
|
|
36
|
+
isEscapeHatchOpen: boolean;
|
|
30
37
|
};
|
|
31
38
|
|
|
32
39
|
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
33
40
|
|
|
34
41
|
export interface EpochCacheInterface {
|
|
35
42
|
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
getSlotNow(): SlotNumber;
|
|
44
|
+
getTargetSlot(): SlotNumber;
|
|
45
|
+
getEpochNow(): EpochNumber;
|
|
46
|
+
getTargetEpoch(): EpochNumber;
|
|
47
|
+
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint };
|
|
48
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
|
|
49
|
+
/** Returns epoch/slot info for the next L1 slot with pipeline offset applied. */
|
|
50
|
+
getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint };
|
|
51
|
+
isProposerPipeliningEnabled(): boolean;
|
|
52
|
+
isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean>;
|
|
53
|
+
isEscapeHatchOpenAtSlot(slot: SlotTag): Promise<boolean>;
|
|
38
54
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
39
55
|
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
currentSlot: SlotNumber;
|
|
44
|
-
nextSlot: SlotNumber;
|
|
45
|
-
}>;
|
|
56
|
+
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
|
|
57
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber };
|
|
58
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
46
59
|
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
47
60
|
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
48
61
|
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
62
|
+
getL1Constants(): L1RollupConstants;
|
|
49
63
|
}
|
|
50
64
|
|
|
51
65
|
/**
|
|
@@ -64,6 +78,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
64
78
|
private lastValidatorRefresh = 0;
|
|
65
79
|
private readonly log: Logger = createLogger('epoch-cache');
|
|
66
80
|
|
|
81
|
+
protected enableProposerPipelining: boolean;
|
|
82
|
+
|
|
67
83
|
constructor(
|
|
68
84
|
private rollup: RollupContract,
|
|
69
85
|
private readonly l1constants: L1RollupConstants & {
|
|
@@ -71,10 +87,12 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
71
87
|
lagInEpochsForRandao: number;
|
|
72
88
|
},
|
|
73
89
|
private readonly dateProvider: DateProvider = new DateProvider(),
|
|
74
|
-
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
|
|
90
|
+
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60, enableProposerPipelining: false },
|
|
75
91
|
) {
|
|
92
|
+
this.enableProposerPipelining = this.config.enableProposerPipelining;
|
|
76
93
|
this.log.debug(`Initialized EpochCache`, {
|
|
77
94
|
l1constants,
|
|
95
|
+
enableProposerPipelining: this.enableProposerPipelining,
|
|
78
96
|
});
|
|
79
97
|
}
|
|
80
98
|
|
|
@@ -93,7 +111,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
93
111
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
94
112
|
const publicClient = createPublicClient({
|
|
95
113
|
chain: chain.chainInfo,
|
|
96
|
-
transport:
|
|
114
|
+
transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
|
|
97
115
|
pollingInterval: config.viemPollingIntervalMS,
|
|
98
116
|
});
|
|
99
117
|
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
@@ -107,6 +125,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
107
125
|
epochDuration,
|
|
108
126
|
lagInEpochsForValidatorSet,
|
|
109
127
|
lagInEpochsForRandao,
|
|
128
|
+
targetCommitteeSize,
|
|
129
|
+
rollupManaLimit,
|
|
110
130
|
] = await Promise.all([
|
|
111
131
|
rollup.getL1StartBlock(),
|
|
112
132
|
rollup.getL1GenesisTime(),
|
|
@@ -115,6 +135,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
115
135
|
rollup.getEpochDuration(),
|
|
116
136
|
rollup.getLagInEpochsForValidatorSet(),
|
|
117
137
|
rollup.getLagInEpochsForRandao(),
|
|
138
|
+
rollup.getTargetCommitteeSize(),
|
|
139
|
+
rollup.getManaLimit(),
|
|
118
140
|
] as const);
|
|
119
141
|
|
|
120
142
|
const l1RollupConstants = {
|
|
@@ -126,18 +148,47 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
126
148
|
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
127
149
|
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
128
150
|
lagInEpochsForRandao: Number(lagInEpochsForRandao),
|
|
151
|
+
targetCommitteeSize: Number(targetCommitteeSize),
|
|
152
|
+
rollupManaLimit: Number(rollupManaLimit),
|
|
129
153
|
};
|
|
130
154
|
|
|
131
|
-
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider
|
|
155
|
+
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider, {
|
|
156
|
+
cacheSize: 12,
|
|
157
|
+
validatorRefreshIntervalSeconds: 60,
|
|
158
|
+
enableProposerPipelining: config.enableProposerPipelining,
|
|
159
|
+
});
|
|
132
160
|
}
|
|
133
161
|
|
|
134
162
|
public getL1Constants(): L1RollupConstants {
|
|
135
163
|
return this.l1constants;
|
|
136
164
|
}
|
|
137
165
|
|
|
138
|
-
public
|
|
139
|
-
|
|
140
|
-
|
|
166
|
+
public isProposerPipeliningEnabled(): boolean {
|
|
167
|
+
return this.enableProposerPipelining;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public getSlotNow(): SlotNumber {
|
|
171
|
+
return this.getEpochAndSlotNow().slot;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public getTargetSlot(): SlotNumber {
|
|
175
|
+
const slotNow = this.getSlotNow();
|
|
176
|
+
const offset = this.isProposerPipeliningEnabled() ? PROPOSER_PIPELINING_SLOT_OFFSET : 0;
|
|
177
|
+
return SlotNumber(slotNow + offset);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public getEpochNow(): EpochNumber {
|
|
181
|
+
return this.getEpochAndSlotNow().epoch;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public getTargetEpoch(): EpochNumber {
|
|
185
|
+
return getEpochAtSlot(this.getTargetSlot(), this.l1constants);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
189
|
+
const nowMs = BigInt(this.dateProvider.now());
|
|
190
|
+
const nowSeconds = nowMs / 1000n;
|
|
191
|
+
return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
|
|
141
192
|
}
|
|
142
193
|
|
|
143
194
|
public nowInSeconds(): bigint {
|
|
@@ -145,23 +196,33 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
145
196
|
}
|
|
146
197
|
|
|
147
198
|
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
199
|
+
return this.getEpochAndSlotAtTimestamp(getTimestampForSlot(slot, this.l1constants));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
203
|
+
const nowSeconds = this.nowInSeconds();
|
|
204
|
+
const nextSlotTs = nowSeconds + BigInt(this.l1constants.ethereumSlotDuration);
|
|
205
|
+
return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), nowSeconds };
|
|
151
206
|
}
|
|
152
207
|
|
|
153
|
-
public
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
208
|
+
public getTargetEpochAndSlotInNextL1Slot(): EpochAndSlot & { nowSeconds: bigint } {
|
|
209
|
+
if (!this.isProposerPipeliningEnabled()) {
|
|
210
|
+
return this.getEpochAndSlotInNextL1Slot();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const result = this.getEpochAndSlotInNextL1Slot();
|
|
214
|
+
const offset = PROPOSER_PIPELINING_SLOT_OFFSET;
|
|
215
|
+
const targetSlot = SlotNumber(result.slot + offset);
|
|
216
|
+
return { ...result, slot: targetSlot, epoch: getEpochAtSlot(targetSlot, this.l1constants) };
|
|
157
217
|
}
|
|
158
218
|
|
|
159
219
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
160
220
|
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
221
|
+
const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
|
|
161
222
|
return {
|
|
162
|
-
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
163
|
-
ts: getTimestampForSlot(slot, this.l1constants),
|
|
164
223
|
slot,
|
|
224
|
+
epoch,
|
|
225
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
165
226
|
};
|
|
166
227
|
}
|
|
167
228
|
|
|
@@ -170,6 +231,38 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
170
231
|
return this.getCommittee(startSlot);
|
|
171
232
|
}
|
|
172
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Returns whether the escape hatch is open for the given epoch.
|
|
236
|
+
*
|
|
237
|
+
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
238
|
+
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
239
|
+
*/
|
|
240
|
+
public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
|
|
241
|
+
const cached = this.cache.get(epoch);
|
|
242
|
+
if (cached) {
|
|
243
|
+
return cached.isEscapeHatchOpen;
|
|
244
|
+
}
|
|
245
|
+
const info = await this.getCommitteeForEpoch(epoch);
|
|
246
|
+
return info.isEscapeHatchOpen;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Returns whether the escape hatch is open for the epoch containing the given slot.
|
|
251
|
+
*
|
|
252
|
+
* This is a lightweight helper intended for callers that already have a slot number and only
|
|
253
|
+
* need the escape hatch flag (without pulling full committee info).
|
|
254
|
+
*/
|
|
255
|
+
public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
|
|
256
|
+
const epoch =
|
|
257
|
+
slot === 'now'
|
|
258
|
+
? this.getEpochNow()
|
|
259
|
+
: slot === 'next'
|
|
260
|
+
? this.getEpochAndSlotInNextL1Slot().epoch
|
|
261
|
+
: getEpochAtSlot(slot, this.l1constants);
|
|
262
|
+
|
|
263
|
+
return await this.isEscapeHatchOpen(epoch);
|
|
264
|
+
}
|
|
265
|
+
|
|
173
266
|
/**
|
|
174
267
|
* Get the current validator set
|
|
175
268
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
@@ -197,7 +290,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
197
290
|
return epochData;
|
|
198
291
|
}
|
|
199
292
|
|
|
200
|
-
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
293
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
|
|
201
294
|
if (slot === 'now') {
|
|
202
295
|
return this.getEpochAndSlotNow();
|
|
203
296
|
} else if (slot === 'next') {
|
|
@@ -209,10 +302,11 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
209
302
|
|
|
210
303
|
private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
|
|
211
304
|
const { ts, epoch } = when;
|
|
212
|
-
const [
|
|
305
|
+
const [committee, seedBuffer, l1Timestamp, isEscapeHatchOpen] = await Promise.all([
|
|
213
306
|
this.rollup.getCommitteeAt(ts),
|
|
214
307
|
this.rollup.getSampleSeedAt(ts),
|
|
215
308
|
this.rollup.client.getBlock({ includeTransactions: false }).then(b => b.timestamp),
|
|
309
|
+
this.rollup.isEscapeHatchOpen(epoch),
|
|
216
310
|
]);
|
|
217
311
|
const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
|
|
218
312
|
const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
@@ -221,8 +315,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
221
315
|
`Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`,
|
|
222
316
|
);
|
|
223
317
|
}
|
|
224
|
-
|
|
225
|
-
return { committee, seed, epoch };
|
|
318
|
+
return { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
|
|
226
319
|
}
|
|
227
320
|
|
|
228
321
|
/**
|
|
@@ -247,25 +340,24 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
247
340
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
248
341
|
}
|
|
249
342
|
|
|
250
|
-
/**
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
* We return the next proposer's attester address as the node will check if it is the proposer at the next ethereum block,
|
|
254
|
-
* which can be the next slot. If this is the case, then it will send proposals early.
|
|
255
|
-
*/
|
|
256
|
-
public async getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
|
|
257
|
-
currentSlot: SlotNumber;
|
|
258
|
-
nextSlot: SlotNumber;
|
|
259
|
-
currentProposer: EthAddress | undefined;
|
|
260
|
-
nextProposer: EthAddress | undefined;
|
|
261
|
-
}> {
|
|
262
|
-
const current = this.getEpochAndSlotNow();
|
|
343
|
+
/** Returns the current and next L2 slot in next eth L1 Slot. */
|
|
344
|
+
public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
345
|
+
const currentSlot = this.getSlotNow();
|
|
263
346
|
const next = this.getEpochAndSlotInNextL1Slot();
|
|
264
347
|
|
|
265
348
|
return {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
349
|
+
currentSlot,
|
|
350
|
+
nextSlot: next.slot,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Returns the taget and next L2 slot in the next L1 slot */
|
|
355
|
+
public getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
356
|
+
const targetSlot = this.getTargetSlot();
|
|
357
|
+
const next = this.getTargetEpochAndSlotInNextL1Slot();
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
targetSlot,
|
|
269
361
|
nextSlot: next.slot,
|
|
270
362
|
};
|
|
271
363
|
}
|
|
@@ -350,9 +442,9 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
350
442
|
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
351
443
|
if (validatorRefreshTime < this.dateProvider.now()) {
|
|
352
444
|
const currentSet = await this.rollup.getAttesters();
|
|
353
|
-
this.allValidators = new Set(currentSet);
|
|
445
|
+
this.allValidators = new Set(currentSet.map(v => v.toString()));
|
|
354
446
|
this.lastValidatorRefresh = this.dateProvider.now();
|
|
355
447
|
}
|
|
356
|
-
return Array.from(this.allValidators.keys().map(v => EthAddress.fromString(v))
|
|
448
|
+
return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
|
|
357
449
|
}
|
|
358
450
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './test_epoch_cache.js';
|