@aztec/epoch-cache 0.0.1-commit.e61ad554 → 0.0.1-commit.f146247c
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/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 +76 -0
- package/dest/test/test_epoch_cache.d.ts.map +1 -0
- package/dest/test/test_epoch_cache.js +150 -0
- package/package.json +5 -5
- package/src/test/index.ts +1 -0
- package/src/test/test_epoch_cache.ts +169 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/test/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './test_epoch_cache.js';
|
|
@@ -0,0 +1,76 @@
|
|
|
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 type { EpochAndSlot, EpochCacheInterface, EpochCommitteeInfo, SlotTag } from '../epoch_cache.js';
|
|
5
|
+
/**
|
|
6
|
+
* A test implementation of EpochCacheInterface that allows manual configuration
|
|
7
|
+
* of committee, proposer, slot, and escape hatch state for use in tests.
|
|
8
|
+
*
|
|
9
|
+
* Unlike the real EpochCache, this class doesn't require any RPC connections
|
|
10
|
+
* or mock setup. Simply use the setter methods to configure the test state.
|
|
11
|
+
*/
|
|
12
|
+
export declare class TestEpochCache implements EpochCacheInterface {
|
|
13
|
+
private committee;
|
|
14
|
+
private proposerAddress;
|
|
15
|
+
private currentSlot;
|
|
16
|
+
private escapeHatchOpen;
|
|
17
|
+
private seed;
|
|
18
|
+
private registeredValidators;
|
|
19
|
+
private l1Constants;
|
|
20
|
+
constructor(l1Constants?: Partial<L1RollupConstants>);
|
|
21
|
+
/**
|
|
22
|
+
* Sets the committee members. Used in validation and attestation flows.
|
|
23
|
+
* @param committee - Array of committee member addresses.
|
|
24
|
+
*/
|
|
25
|
+
setCommittee(committee: EthAddress[]): this;
|
|
26
|
+
/**
|
|
27
|
+
* Sets the proposer address returned by getProposerAttesterAddressInSlot.
|
|
28
|
+
* @param proposer - The address of the current proposer.
|
|
29
|
+
*/
|
|
30
|
+
setProposer(proposer: EthAddress | undefined): this;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the current slot number.
|
|
33
|
+
* @param slot - The slot number to set.
|
|
34
|
+
*/
|
|
35
|
+
setCurrentSlot(slot: SlotNumber): this;
|
|
36
|
+
/**
|
|
37
|
+
* Sets whether the escape hatch is open.
|
|
38
|
+
* @param open - True if escape hatch should be open.
|
|
39
|
+
*/
|
|
40
|
+
setEscapeHatchOpen(open: boolean): this;
|
|
41
|
+
/**
|
|
42
|
+
* Sets the randomness seed used for proposer selection.
|
|
43
|
+
* @param seed - The seed value.
|
|
44
|
+
*/
|
|
45
|
+
setSeed(seed: bigint): this;
|
|
46
|
+
/**
|
|
47
|
+
* Sets the list of registered validators (all validators, not just committee).
|
|
48
|
+
* @param validators - Array of validator addresses.
|
|
49
|
+
*/
|
|
50
|
+
setRegisteredValidators(validators: EthAddress[]): this;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the L1 constants used for epoch/slot calculations.
|
|
53
|
+
* @param constants - Partial constants to override defaults.
|
|
54
|
+
*/
|
|
55
|
+
setL1Constants(constants: Partial<L1RollupConstants>): this;
|
|
56
|
+
getL1Constants(): L1RollupConstants;
|
|
57
|
+
getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo>;
|
|
58
|
+
getEpochAndSlotNow(): EpochAndSlot & {
|
|
59
|
+
nowMs: bigint;
|
|
60
|
+
};
|
|
61
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
62
|
+
now: bigint;
|
|
63
|
+
};
|
|
64
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
65
|
+
computeProposerIndex(slot: SlotNumber, _epoch: EpochNumber, _seed: bigint, size: bigint): bigint;
|
|
66
|
+
getCurrentAndNextSlot(): {
|
|
67
|
+
currentSlot: SlotNumber;
|
|
68
|
+
nextSlot: SlotNumber;
|
|
69
|
+
};
|
|
70
|
+
getProposerAttesterAddressInSlot(_slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
71
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
72
|
+
isInCommittee(_slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
73
|
+
filterInCommittee(_slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
74
|
+
isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean>;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdF9lcG9jaF9jYWNoZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3Rlc3QvdGVzdF9lcG9jaF9jYWNoZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsV0FBVyxFQUFFLFVBQVUsRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBQzFFLE9BQU8sRUFBRSxVQUFVLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUMzRCxPQUFPLEtBQUssRUFBRSxpQkFBaUIsRUFBRSxNQUFNLDZCQUE2QixDQUFDO0FBR3JFLE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBRSxtQkFBbUIsRUFBRSxrQkFBa0IsRUFBRSxPQUFPLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQVl4Rzs7Ozs7O0dBTUc7QUFDSCxxQkFBYSxjQUFlLFlBQVcsbUJBQW1CO0lBQ3hELE9BQU8sQ0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IAED,cAAc,IAAI,iBAAiB,CAElC;IAED,YAAY,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAQzD;IAED,kBAAkB,IAAI,YAAY,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAIrD;IAED,2BAA2B,IAAI,YAAY,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAO5D;IAED,wBAAwB,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK,MAAM,EAAE,CAG1F;IAED,oBAAoB,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAK/F;IAED,qBAAqB,IAAI;QAAE,WAAW,EAAE,UAAU,CAAC;QAAC,QAAQ,EAAE,UAAU,CAAA;KAAE,CAKzE;IAED,gCAAgC,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAEnF;IAED,uBAAuB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,CAE/C;IAED,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAErE;IAED,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAGjF;IAED,uBAAuB,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAEzD;CACF"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { getEpochAtSlot, getSlotAtTimestamp, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
3
|
+
/** Default L1 constants for testing. */ const DEFAULT_L1_CONSTANTS = {
|
|
4
|
+
l1StartBlock: 0n,
|
|
5
|
+
l1GenesisTime: 0n,
|
|
6
|
+
slotDuration: 24,
|
|
7
|
+
epochDuration: 16,
|
|
8
|
+
ethereumSlotDuration: 12,
|
|
9
|
+
proofSubmissionEpochs: 2
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A test implementation of EpochCacheInterface that allows manual configuration
|
|
13
|
+
* of committee, proposer, slot, and escape hatch state for use in tests.
|
|
14
|
+
*
|
|
15
|
+
* Unlike the real EpochCache, this class doesn't require any RPC connections
|
|
16
|
+
* or mock setup. Simply use the setter methods to configure the test state.
|
|
17
|
+
*/ export class TestEpochCache {
|
|
18
|
+
committee = [];
|
|
19
|
+
proposerAddress;
|
|
20
|
+
currentSlot = SlotNumber(0);
|
|
21
|
+
escapeHatchOpen = false;
|
|
22
|
+
seed = 0n;
|
|
23
|
+
registeredValidators = [];
|
|
24
|
+
l1Constants;
|
|
25
|
+
constructor(l1Constants = {}){
|
|
26
|
+
this.l1Constants = {
|
|
27
|
+
...DEFAULT_L1_CONSTANTS,
|
|
28
|
+
...l1Constants
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Sets the committee members. Used in validation and attestation flows.
|
|
33
|
+
* @param committee - Array of committee member addresses.
|
|
34
|
+
*/ setCommittee(committee) {
|
|
35
|
+
this.committee = committee;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sets the proposer address returned by getProposerAttesterAddressInSlot.
|
|
40
|
+
* @param proposer - The address of the current proposer.
|
|
41
|
+
*/ setProposer(proposer) {
|
|
42
|
+
this.proposerAddress = proposer;
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sets the current slot number.
|
|
47
|
+
* @param slot - The slot number to set.
|
|
48
|
+
*/ setCurrentSlot(slot) {
|
|
49
|
+
this.currentSlot = slot;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Sets whether the escape hatch is open.
|
|
54
|
+
* @param open - True if escape hatch should be open.
|
|
55
|
+
*/ setEscapeHatchOpen(open) {
|
|
56
|
+
this.escapeHatchOpen = open;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sets the randomness seed used for proposer selection.
|
|
61
|
+
* @param seed - The seed value.
|
|
62
|
+
*/ setSeed(seed) {
|
|
63
|
+
this.seed = seed;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Sets the list of registered validators (all validators, not just committee).
|
|
68
|
+
* @param validators - Array of validator addresses.
|
|
69
|
+
*/ setRegisteredValidators(validators) {
|
|
70
|
+
this.registeredValidators = validators;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Sets the L1 constants used for epoch/slot calculations.
|
|
75
|
+
* @param constants - Partial constants to override defaults.
|
|
76
|
+
*/ setL1Constants(constants) {
|
|
77
|
+
this.l1Constants = {
|
|
78
|
+
...this.l1Constants,
|
|
79
|
+
...constants
|
|
80
|
+
};
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
getL1Constants() {
|
|
84
|
+
return this.l1Constants;
|
|
85
|
+
}
|
|
86
|
+
getCommittee(_slot) {
|
|
87
|
+
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
88
|
+
return Promise.resolve({
|
|
89
|
+
committee: this.committee,
|
|
90
|
+
epoch,
|
|
91
|
+
seed: this.seed,
|
|
92
|
+
isEscapeHatchOpen: this.escapeHatchOpen
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
getEpochAndSlotNow() {
|
|
96
|
+
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
97
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1Constants)[0];
|
|
98
|
+
return {
|
|
99
|
+
epoch,
|
|
100
|
+
slot: this.currentSlot,
|
|
101
|
+
ts,
|
|
102
|
+
nowMs: ts * 1000n
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
getEpochAndSlotInNextL1Slot() {
|
|
106
|
+
const now = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
|
|
107
|
+
const nextSlotTs = now + BigInt(this.l1Constants.ethereumSlotDuration);
|
|
108
|
+
const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
|
|
109
|
+
const epoch = getEpochAtSlot(nextSlot, this.l1Constants);
|
|
110
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1Constants)[0];
|
|
111
|
+
return {
|
|
112
|
+
epoch,
|
|
113
|
+
slot: nextSlot,
|
|
114
|
+
ts,
|
|
115
|
+
now
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
getProposerIndexEncoding(epoch, slot, seed) {
|
|
119
|
+
// Simple encoding for testing purposes
|
|
120
|
+
return `0x${epoch.toString(16).padStart(64, '0')}${slot.toString(16).padStart(64, '0')}${seed.toString(16).padStart(64, '0')}`;
|
|
121
|
+
}
|
|
122
|
+
computeProposerIndex(slot, _epoch, _seed, size) {
|
|
123
|
+
if (size === 0n) {
|
|
124
|
+
return 0n;
|
|
125
|
+
}
|
|
126
|
+
return BigInt(slot) % size;
|
|
127
|
+
}
|
|
128
|
+
getCurrentAndNextSlot() {
|
|
129
|
+
return {
|
|
130
|
+
currentSlot: this.currentSlot,
|
|
131
|
+
nextSlot: SlotNumber(this.currentSlot + 1)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
getProposerAttesterAddressInSlot(_slot) {
|
|
135
|
+
return Promise.resolve(this.proposerAddress);
|
|
136
|
+
}
|
|
137
|
+
getRegisteredValidators() {
|
|
138
|
+
return Promise.resolve(this.registeredValidators);
|
|
139
|
+
}
|
|
140
|
+
isInCommittee(_slot, validator) {
|
|
141
|
+
return Promise.resolve(this.committee.some((v)=>v.equals(validator)));
|
|
142
|
+
}
|
|
143
|
+
filterInCommittee(_slot, validators) {
|
|
144
|
+
const committeeSet = new Set(this.committee.map((v)=>v.toString()));
|
|
145
|
+
return Promise.resolve(validators.filter((v)=>committeeSet.has(v.toString())));
|
|
146
|
+
}
|
|
147
|
+
isEscapeHatchOpenAtSlot(_slot) {
|
|
148
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
149
|
+
}
|
|
150
|
+
}
|
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.f146247c",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -26,10 +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.
|
|
29
|
+
"@aztec/ethereum": "0.0.1-commit.f146247c",
|
|
30
|
+
"@aztec/foundation": "0.0.1-commit.f146247c",
|
|
31
|
+
"@aztec/l1-artifacts": "0.0.1-commit.f146247c",
|
|
32
|
+
"@aztec/stdlib": "0.0.1-commit.f146247c",
|
|
33
33
|
"@viem/anvil": "^0.0.10",
|
|
34
34
|
"dotenv": "^16.0.3",
|
|
35
35
|
"get-port": "^7.1.0",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './test_epoch_cache.js';
|
|
@@ -0,0 +1,169 @@
|
|
|
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 type { EpochAndSlot, EpochCacheInterface, EpochCommitteeInfo, SlotTag } from '../epoch_cache.js';
|
|
7
|
+
|
|
8
|
+
/** Default L1 constants for testing. */
|
|
9
|
+
const DEFAULT_L1_CONSTANTS: L1RollupConstants = {
|
|
10
|
+
l1StartBlock: 0n,
|
|
11
|
+
l1GenesisTime: 0n,
|
|
12
|
+
slotDuration: 24,
|
|
13
|
+
epochDuration: 16,
|
|
14
|
+
ethereumSlotDuration: 12,
|
|
15
|
+
proofSubmissionEpochs: 2,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A test implementation of EpochCacheInterface that allows manual configuration
|
|
20
|
+
* of committee, proposer, slot, and escape hatch state for use in tests.
|
|
21
|
+
*
|
|
22
|
+
* Unlike the real EpochCache, this class doesn't require any RPC connections
|
|
23
|
+
* or mock setup. Simply use the setter methods to configure the test state.
|
|
24
|
+
*/
|
|
25
|
+
export class TestEpochCache implements EpochCacheInterface {
|
|
26
|
+
private committee: EthAddress[] = [];
|
|
27
|
+
private proposerAddress: EthAddress | undefined;
|
|
28
|
+
private currentSlot: SlotNumber = SlotNumber(0);
|
|
29
|
+
private escapeHatchOpen: boolean = false;
|
|
30
|
+
private seed: bigint = 0n;
|
|
31
|
+
private registeredValidators: EthAddress[] = [];
|
|
32
|
+
private l1Constants: L1RollupConstants;
|
|
33
|
+
|
|
34
|
+
constructor(l1Constants: Partial<L1RollupConstants> = {}) {
|
|
35
|
+
this.l1Constants = { ...DEFAULT_L1_CONSTANTS, ...l1Constants };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Sets the committee members. Used in validation and attestation flows.
|
|
40
|
+
* @param committee - Array of committee member addresses.
|
|
41
|
+
*/
|
|
42
|
+
setCommittee(committee: EthAddress[]): this {
|
|
43
|
+
this.committee = committee;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Sets the proposer address returned by getProposerAttesterAddressInSlot.
|
|
49
|
+
* @param proposer - The address of the current proposer.
|
|
50
|
+
*/
|
|
51
|
+
setProposer(proposer: EthAddress | undefined): this {
|
|
52
|
+
this.proposerAddress = proposer;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Sets the current slot number.
|
|
58
|
+
* @param slot - The slot number to set.
|
|
59
|
+
*/
|
|
60
|
+
setCurrentSlot(slot: SlotNumber): this {
|
|
61
|
+
this.currentSlot = slot;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sets whether the escape hatch is open.
|
|
67
|
+
* @param open - True if escape hatch should be open.
|
|
68
|
+
*/
|
|
69
|
+
setEscapeHatchOpen(open: boolean): this {
|
|
70
|
+
this.escapeHatchOpen = open;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Sets the randomness seed used for proposer selection.
|
|
76
|
+
* @param seed - The seed value.
|
|
77
|
+
*/
|
|
78
|
+
setSeed(seed: bigint): this {
|
|
79
|
+
this.seed = seed;
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Sets the list of registered validators (all validators, not just committee).
|
|
85
|
+
* @param validators - Array of validator addresses.
|
|
86
|
+
*/
|
|
87
|
+
setRegisteredValidators(validators: EthAddress[]): this {
|
|
88
|
+
this.registeredValidators = validators;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sets the L1 constants used for epoch/slot calculations.
|
|
94
|
+
* @param constants - Partial constants to override defaults.
|
|
95
|
+
*/
|
|
96
|
+
setL1Constants(constants: Partial<L1RollupConstants>): this {
|
|
97
|
+
this.l1Constants = { ...this.l1Constants, ...constants };
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getL1Constants(): L1RollupConstants {
|
|
102
|
+
return this.l1Constants;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getCommittee(_slot?: SlotTag): Promise<EpochCommitteeInfo> {
|
|
106
|
+
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
107
|
+
return Promise.resolve({
|
|
108
|
+
committee: this.committee,
|
|
109
|
+
epoch,
|
|
110
|
+
seed: this.seed,
|
|
111
|
+
isEscapeHatchOpen: this.escapeHatchOpen,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
116
|
+
const epoch = getEpochAtSlot(this.currentSlot, this.l1Constants);
|
|
117
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1Constants)[0];
|
|
118
|
+
return { epoch, slot: this.currentSlot, ts, nowMs: ts * 1000n };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
|
|
122
|
+
const now = getTimestampRangeForEpoch(getEpochAtSlot(this.currentSlot, this.l1Constants), this.l1Constants)[0];
|
|
123
|
+
const nextSlotTs = now + BigInt(this.l1Constants.ethereumSlotDuration);
|
|
124
|
+
const nextSlot = getSlotAtTimestamp(nextSlotTs, this.l1Constants);
|
|
125
|
+
const epoch = getEpochAtSlot(nextSlot, this.l1Constants);
|
|
126
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1Constants)[0];
|
|
127
|
+
return { epoch, slot: nextSlot, ts, now };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
|
|
131
|
+
// Simple encoding for testing purposes
|
|
132
|
+
return `0x${epoch.toString(16).padStart(64, '0')}${slot.toString(16).padStart(64, '0')}${seed.toString(16).padStart(64, '0')}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
computeProposerIndex(slot: SlotNumber, _epoch: EpochNumber, _seed: bigint, size: bigint): bigint {
|
|
136
|
+
if (size === 0n) {
|
|
137
|
+
return 0n;
|
|
138
|
+
}
|
|
139
|
+
return BigInt(slot) % size;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
143
|
+
return {
|
|
144
|
+
currentSlot: this.currentSlot,
|
|
145
|
+
nextSlot: SlotNumber(this.currentSlot + 1),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getProposerAttesterAddressInSlot(_slot: SlotNumber): Promise<EthAddress | undefined> {
|
|
150
|
+
return Promise.resolve(this.proposerAddress);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
getRegisteredValidators(): Promise<EthAddress[]> {
|
|
154
|
+
return Promise.resolve(this.registeredValidators);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
isInCommittee(_slot: SlotTag, validator: EthAddress): Promise<boolean> {
|
|
158
|
+
return Promise.resolve(this.committee.some(v => v.equals(validator)));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
filterInCommittee(_slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]> {
|
|
162
|
+
const committeeSet = new Set(this.committee.map(v => v.toString()));
|
|
163
|
+
return Promise.resolve(validators.filter(v => committeeSet.has(v.toString())));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
isEscapeHatchOpenAtSlot(_slot?: SlotTag): Promise<boolean> {
|
|
167
|
+
return Promise.resolve(this.escapeHatchOpen);
|
|
168
|
+
}
|
|
169
|
+
}
|