@aztec/epoch-cache 0.0.1-commit.6d3c34e → 0.0.1-commit.7035c9bd6
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 +3 -2
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +3 -1
- package/dest/epoch_cache.d.ts +64 -20
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +106 -40
- 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 +7 -8
- package/src/config.ts +9 -3
- package/src/epoch_cache.ts +136 -45
- 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.7035c9bd6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"build": "yarn clean && ../scripts/tsc.sh",
|
|
19
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.7035c9bd6",
|
|
30
|
+
"@aztec/foundation": "0.0.1-commit.7035c9bd6",
|
|
31
|
+
"@aztec/l1-artifacts": "0.0.1-commit.7035c9bd6",
|
|
32
|
+
"@aztec/stdlib": "0.0.1-commit.7035c9bd6",
|
|
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,11 +1,17 @@
|
|
|
1
1
|
import { type L1ContractsConfig, getL1ContractsConfigEnvVars } from '@aztec/ethereum/config';
|
|
2
2
|
import { type L1ReaderConfig, getL1ReaderConfigFromEnv } from '@aztec/ethereum/l1-reader';
|
|
3
|
+
import { type PipelineConfig, getPipelineConfigEnvVars } from '@aztec/stdlib/config';
|
|
3
4
|
|
|
4
5
|
export type EpochCacheConfig = Pick<
|
|
5
|
-
L1ReaderConfig & L1ContractsConfig,
|
|
6
|
-
|
|
6
|
+
L1ReaderConfig & L1ContractsConfig & PipelineConfig,
|
|
7
|
+
| 'l1RpcUrls'
|
|
8
|
+
| 'l1ChainId'
|
|
9
|
+
| 'viemPollingIntervalMS'
|
|
10
|
+
| 'ethereumSlotDuration'
|
|
11
|
+
| 'l1HttpTimeoutMS'
|
|
12
|
+
| 'enableProposerPipelining'
|
|
7
13
|
>;
|
|
8
14
|
|
|
9
15
|
export function getEpochCacheConfigEnvVars(): EpochCacheConfig {
|
|
10
|
-
return { ...getL1ReaderConfigFromEnv(), ...getL1ContractsConfigEnvVars() };
|
|
16
|
+
return { ...getL1ReaderConfigFromEnv(), ...getL1ContractsConfigEnvVars(), ...getPipelineConfigEnvVars() };
|
|
11
17
|
}
|
package/src/epoch_cache.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
2
|
+
import { makeL1HttpTransport } from '@aztec/ethereum/client';
|
|
2
3
|
import { NoCommitteeError, RollupContract } from '@aztec/ethereum/contracts';
|
|
3
4
|
import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
@@ -11,16 +12,19 @@ import {
|
|
|
11
12
|
getSlotAtTimestamp,
|
|
12
13
|
getSlotRangeForEpoch,
|
|
13
14
|
getTimestampForSlot,
|
|
14
|
-
getTimestampRangeForEpoch,
|
|
15
15
|
} from '@aztec/stdlib/epoch-helpers';
|
|
16
16
|
|
|
17
|
-
import { createPublicClient, encodeAbiParameters,
|
|
17
|
+
import { createPublicClient, encodeAbiParameters, keccak256 } from 'viem';
|
|
18
18
|
|
|
19
19
|
import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
|
|
20
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. */
|
|
21
25
|
export type EpochAndSlot = {
|
|
22
|
-
epoch: EpochNumber;
|
|
23
26
|
slot: SlotNumber;
|
|
27
|
+
epoch: EpochNumber;
|
|
24
28
|
ts: bigint;
|
|
25
29
|
};
|
|
26
30
|
|
|
@@ -28,26 +32,34 @@ export type EpochCommitteeInfo = {
|
|
|
28
32
|
committee: EthAddress[] | undefined;
|
|
29
33
|
seed: bigint;
|
|
30
34
|
epoch: EpochNumber;
|
|
35
|
+
/** True if the epoch is within an open escape hatch window. */
|
|
36
|
+
isEscapeHatchOpen: boolean;
|
|
31
37
|
};
|
|
32
38
|
|
|
33
39
|
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
34
40
|
|
|
35
41
|
export interface EpochCacheInterface {
|
|
36
42
|
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
37
|
-
|
|
38
|
-
|
|
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>;
|
|
39
54
|
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
40
55
|
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
nextProposer: EthAddress | undefined;
|
|
44
|
-
currentSlot: SlotNumber;
|
|
45
|
-
nextSlot: SlotNumber;
|
|
46
|
-
}>;
|
|
56
|
+
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
|
|
57
|
+
getTargetAndNextSlot(): { targetSlot: SlotNumber; nextSlot: SlotNumber };
|
|
47
58
|
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
48
59
|
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
49
60
|
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
50
61
|
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
62
|
+
getL1Constants(): L1RollupConstants;
|
|
51
63
|
}
|
|
52
64
|
|
|
53
65
|
/**
|
|
@@ -66,6 +78,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
66
78
|
private lastValidatorRefresh = 0;
|
|
67
79
|
private readonly log: Logger = createLogger('epoch-cache');
|
|
68
80
|
|
|
81
|
+
protected enableProposerPipelining: boolean;
|
|
82
|
+
|
|
69
83
|
constructor(
|
|
70
84
|
private rollup: RollupContract,
|
|
71
85
|
private readonly l1constants: L1RollupConstants & {
|
|
@@ -73,10 +87,12 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
73
87
|
lagInEpochsForRandao: number;
|
|
74
88
|
},
|
|
75
89
|
private readonly dateProvider: DateProvider = new DateProvider(),
|
|
76
|
-
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
|
|
90
|
+
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60, enableProposerPipelining: false },
|
|
77
91
|
) {
|
|
92
|
+
this.enableProposerPipelining = this.config.enableProposerPipelining;
|
|
78
93
|
this.log.debug(`Initialized EpochCache`, {
|
|
79
94
|
l1constants,
|
|
95
|
+
enableProposerPipelining: this.enableProposerPipelining,
|
|
80
96
|
});
|
|
81
97
|
}
|
|
82
98
|
|
|
@@ -95,7 +111,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
95
111
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
96
112
|
const publicClient = createPublicClient({
|
|
97
113
|
chain: chain.chainInfo,
|
|
98
|
-
transport:
|
|
114
|
+
transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
|
|
99
115
|
pollingInterval: config.viemPollingIntervalMS,
|
|
100
116
|
});
|
|
101
117
|
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
@@ -109,6 +125,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
109
125
|
epochDuration,
|
|
110
126
|
lagInEpochsForValidatorSet,
|
|
111
127
|
lagInEpochsForRandao,
|
|
128
|
+
targetCommitteeSize,
|
|
129
|
+
rollupManaLimit,
|
|
112
130
|
] = await Promise.all([
|
|
113
131
|
rollup.getL1StartBlock(),
|
|
114
132
|
rollup.getL1GenesisTime(),
|
|
@@ -117,6 +135,8 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
117
135
|
rollup.getEpochDuration(),
|
|
118
136
|
rollup.getLagInEpochsForValidatorSet(),
|
|
119
137
|
rollup.getLagInEpochsForRandao(),
|
|
138
|
+
rollup.getTargetCommitteeSize(),
|
|
139
|
+
rollup.getManaLimit(),
|
|
120
140
|
] as const);
|
|
121
141
|
|
|
122
142
|
const l1RollupConstants = {
|
|
@@ -128,18 +148,47 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
128
148
|
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
129
149
|
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
130
150
|
lagInEpochsForRandao: Number(lagInEpochsForRandao),
|
|
151
|
+
targetCommitteeSize: Number(targetCommitteeSize),
|
|
152
|
+
rollupManaLimit: Number(rollupManaLimit),
|
|
131
153
|
};
|
|
132
154
|
|
|
133
|
-
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
|
+
});
|
|
134
160
|
}
|
|
135
161
|
|
|
136
162
|
public getL1Constants(): L1RollupConstants {
|
|
137
163
|
return this.l1constants;
|
|
138
164
|
}
|
|
139
165
|
|
|
140
|
-
public
|
|
141
|
-
|
|
142
|
-
|
|
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 };
|
|
143
192
|
}
|
|
144
193
|
|
|
145
194
|
public nowInSeconds(): bigint {
|
|
@@ -147,23 +196,33 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
147
196
|
}
|
|
148
197
|
|
|
149
198
|
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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 };
|
|
153
206
|
}
|
|
154
207
|
|
|
155
|
-
public
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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) };
|
|
159
217
|
}
|
|
160
218
|
|
|
161
219
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
162
220
|
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
221
|
+
const epoch = getEpochNumberAtTimestamp(ts, this.l1constants);
|
|
163
222
|
return {
|
|
164
|
-
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
165
|
-
ts: getTimestampForSlot(slot, this.l1constants),
|
|
166
223
|
slot,
|
|
224
|
+
epoch,
|
|
225
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
167
226
|
};
|
|
168
227
|
}
|
|
169
228
|
|
|
@@ -172,6 +231,38 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
172
231
|
return this.getCommittee(startSlot);
|
|
173
232
|
}
|
|
174
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
|
+
|
|
175
266
|
/**
|
|
176
267
|
* Get the current validator set
|
|
177
268
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
@@ -199,7 +290,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
199
290
|
return epochData;
|
|
200
291
|
}
|
|
201
292
|
|
|
202
|
-
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
293
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now'): { epoch: EpochNumber; ts: bigint } {
|
|
203
294
|
if (slot === 'now') {
|
|
204
295
|
return this.getEpochAndSlotNow();
|
|
205
296
|
} else if (slot === 'next') {
|
|
@@ -211,10 +302,11 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
211
302
|
|
|
212
303
|
private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
|
|
213
304
|
const { ts, epoch } = when;
|
|
214
|
-
const [committee, seedBuffer, l1Timestamp] = await Promise.all([
|
|
305
|
+
const [committee, seedBuffer, l1Timestamp, isEscapeHatchOpen] = await Promise.all([
|
|
215
306
|
this.rollup.getCommitteeAt(ts),
|
|
216
307
|
this.rollup.getSampleSeedAt(ts),
|
|
217
308
|
this.rollup.client.getBlock({ includeTransactions: false }).then(b => b.timestamp),
|
|
309
|
+
this.rollup.isEscapeHatchOpen(epoch),
|
|
218
310
|
]);
|
|
219
311
|
const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
|
|
220
312
|
const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
@@ -223,7 +315,7 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
223
315
|
`Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`,
|
|
224
316
|
);
|
|
225
317
|
}
|
|
226
|
-
return { committee, seed: seedBuffer.toBigInt(), epoch };
|
|
318
|
+
return { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
|
|
227
319
|
}
|
|
228
320
|
|
|
229
321
|
/**
|
|
@@ -248,25 +340,24 @@ export class EpochCache implements EpochCacheInterface {
|
|
|
248
340
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
249
341
|
}
|
|
250
342
|
|
|
251
|
-
/**
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
* We return the next proposer's attester address as the node will check if it is the proposer at the next ethereum block,
|
|
255
|
-
* which can be the next slot. If this is the case, then it will send proposals early.
|
|
256
|
-
*/
|
|
257
|
-
public async getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
|
|
258
|
-
currentSlot: SlotNumber;
|
|
259
|
-
nextSlot: SlotNumber;
|
|
260
|
-
currentProposer: EthAddress | undefined;
|
|
261
|
-
nextProposer: EthAddress | undefined;
|
|
262
|
-
}> {
|
|
263
|
-
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();
|
|
264
346
|
const next = this.getEpochAndSlotInNextL1Slot();
|
|
265
347
|
|
|
266
348
|
return {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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,
|
|
270
361
|
nextSlot: next.slot,
|
|
271
362
|
};
|
|
272
363
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './test_epoch_cache.js';
|