@aztec/epoch-cache 0.0.0-test.1 → 0.0.1-commit.0b941701
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 +4 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +2 -1
- package/dest/epoch_cache.d.ts +97 -51
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +227 -91
- package/dest/index.d.ts +1 -1
- package/package.json +21 -18
- package/src/config.ts +3 -12
- package/src/epoch_cache.ts +277 -130
- package/dest/timestamp_provider.d.ts +0 -2
- package/dest/timestamp_provider.d.ts.map +0 -1
- package/dest/timestamp_provider.js +0 -0
- package/src/timestamp_provider.ts +0 -0
package/dest/config.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type L1ContractsConfig
|
|
2
|
-
|
|
1
|
+
import { type L1ContractsConfig } from '@aztec/ethereum/config';
|
|
2
|
+
import { type L1ReaderConfig } from '@aztec/ethereum/l1-reader';
|
|
3
|
+
export type EpochCacheConfig = Pick<L1ReaderConfig & L1ContractsConfig, 'l1RpcUrls' | 'l1ChainId' | 'viemPollingIntervalMS' | 'ethereumSlotDuration'>;
|
|
3
4
|
export declare function getEpochCacheConfigEnvVars(): EpochCacheConfig;
|
|
4
|
-
//# sourceMappingURL=
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxLQUFLLGlCQUFpQixFQUErQixNQUFNLHdCQUF3QixDQUFDO0FBQzdGLE9BQU8sRUFBRSxLQUFLLGNBQWMsRUFBNEIsTUFBTSwyQkFBMkIsQ0FBQztBQUUxRixNQUFNLE1BQU0sZ0JBQWdCLEdBQUcsSUFBSSxDQUNqQyxjQUFjLEdBQUcsaUJBQWlCLEVBQ2xDLFdBQVcsR0FBRyxXQUFXLEdBQUcsdUJBQXVCLEdBQUcsc0JBQXNCLENBQzdFLENBQUM7QUFFRix3QkFBZ0IsMEJBQTBCLElBQUksZ0JBQWdCLENBRTdEIn0=
|
package/dest/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAA+B,MAAM,wBAAwB,CAAC;AAC7F,OAAO,EAAE,KAAK,cAAc,EAA4B,MAAM,2BAA2B,CAAC;AAE1F,MAAM,MAAM,gBAAgB,GAAG,IAAI,CACjC,cAAc,GAAG,iBAAiB,EAClC,WAAW,GAAG,WAAW,GAAG,uBAAuB,GAAG,sBAAsB,CAC7E,CAAC;AAEF,wBAAgB,0BAA0B,IAAI,gBAAgB,CAE7D"}
|
package/dest/config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getL1ContractsConfigEnvVars
|
|
1
|
+
import { getL1ContractsConfigEnvVars } from '@aztec/ethereum/config';
|
|
2
|
+
import { getL1ReaderConfigFromEnv } from '@aztec/ethereum/l1-reader';
|
|
2
3
|
export function getEpochCacheConfigEnvVars() {
|
|
3
4
|
return {
|
|
4
5
|
...getL1ReaderConfigFromEnv(),
|
package/dest/epoch_cache.d.ts
CHANGED
|
@@ -1,87 +1,133 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
2
|
+
import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
3
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
4
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
5
|
import { type L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
6
|
-
import { EventEmitter } from 'node:events';
|
|
7
6
|
import { type EpochCacheConfig } from './config.js';
|
|
8
|
-
type EpochAndSlot = {
|
|
9
|
-
epoch:
|
|
10
|
-
slot:
|
|
7
|
+
export type EpochAndSlot = {
|
|
8
|
+
epoch: EpochNumber;
|
|
9
|
+
slot: SlotNumber;
|
|
11
10
|
ts: bigint;
|
|
12
11
|
};
|
|
12
|
+
export type EpochCommitteeInfo = {
|
|
13
|
+
committee: EthAddress[] | undefined;
|
|
14
|
+
seed: bigint;
|
|
15
|
+
epoch: EpochNumber;
|
|
16
|
+
/** True if the epoch is within an open escape hatch window. */
|
|
17
|
+
isEscapeHatchOpen: boolean;
|
|
18
|
+
};
|
|
19
|
+
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
13
20
|
export interface EpochCacheInterface {
|
|
14
|
-
getCommittee(
|
|
15
|
-
getEpochAndSlotNow(): EpochAndSlot
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
22
|
+
getEpochAndSlotNow(): EpochAndSlot & {
|
|
23
|
+
nowMs: bigint;
|
|
24
|
+
};
|
|
25
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
26
|
+
now: bigint;
|
|
27
|
+
};
|
|
28
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
29
|
+
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
30
|
+
getCurrentAndNextSlot(): {
|
|
31
|
+
currentSlot: SlotNumber;
|
|
32
|
+
nextSlot: SlotNumber;
|
|
33
|
+
};
|
|
34
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
35
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
36
|
+
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
37
|
+
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
25
38
|
}
|
|
26
39
|
/**
|
|
27
40
|
* Epoch cache
|
|
28
41
|
*
|
|
29
42
|
* This class is responsible for managing traffic to the l1 node, by caching the validator set.
|
|
43
|
+
* Keeps the last N epochs in cache.
|
|
30
44
|
* It also provides a method to get the current or next proposer, and to check who is in the current slot.
|
|
31
45
|
*
|
|
32
|
-
* If the epoch changes, then we update the stored validator set.
|
|
33
|
-
*
|
|
34
46
|
* Note: This class is very dependent on the system clock being in sync.
|
|
35
47
|
*/
|
|
36
|
-
export declare class EpochCache
|
|
37
|
-
committeeChanged: [EthAddress[], bigint];
|
|
38
|
-
}> implements EpochCacheInterface {
|
|
48
|
+
export declare class EpochCache implements EpochCacheInterface {
|
|
39
49
|
private rollup;
|
|
40
50
|
private readonly l1constants;
|
|
41
51
|
private readonly dateProvider;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
protected readonly config: {
|
|
53
|
+
cacheSize: number;
|
|
54
|
+
validatorRefreshIntervalSeconds: number;
|
|
55
|
+
};
|
|
56
|
+
protected cache: Map<EpochNumber, EpochCommitteeInfo>;
|
|
57
|
+
private allValidators;
|
|
58
|
+
private lastValidatorRefresh;
|
|
45
59
|
private readonly log;
|
|
46
|
-
constructor(rollup: RollupContract,
|
|
47
|
-
|
|
60
|
+
constructor(rollup: RollupContract, l1constants: L1RollupConstants & {
|
|
61
|
+
lagInEpochsForValidatorSet: number;
|
|
62
|
+
lagInEpochsForRandao: number;
|
|
63
|
+
}, dateProvider?: DateProvider, config?: {
|
|
64
|
+
cacheSize: number;
|
|
65
|
+
validatorRefreshIntervalSeconds: number;
|
|
66
|
+
});
|
|
67
|
+
static create(rollupOrAddress: EthAddress | RollupContract, config?: EpochCacheConfig, deps?: {
|
|
48
68
|
dateProvider?: DateProvider;
|
|
49
69
|
}): Promise<EpochCache>;
|
|
50
|
-
|
|
51
|
-
getEpochAndSlotNow(): EpochAndSlot
|
|
52
|
-
|
|
70
|
+
getL1Constants(): L1RollupConstants;
|
|
71
|
+
getEpochAndSlotNow(): EpochAndSlot & {
|
|
72
|
+
nowMs: bigint;
|
|
73
|
+
};
|
|
74
|
+
nowInSeconds(): bigint;
|
|
75
|
+
private getEpochAndSlotAtSlot;
|
|
76
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
77
|
+
now: bigint;
|
|
78
|
+
};
|
|
53
79
|
private getEpochAndSlotAtTimestamp;
|
|
80
|
+
getCommitteeForEpoch(epoch: EpochNumber): Promise<EpochCommitteeInfo>;
|
|
54
81
|
/**
|
|
55
|
-
*
|
|
82
|
+
* Returns whether the escape hatch is open for the given epoch.
|
|
83
|
+
*
|
|
84
|
+
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
85
|
+
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
86
|
+
*/
|
|
87
|
+
isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean>;
|
|
88
|
+
/**
|
|
89
|
+
* Returns whether the escape hatch is open for the epoch containing the given slot.
|
|
56
90
|
*
|
|
91
|
+
* This is a lightweight helper intended for callers that already have a slot number and only
|
|
92
|
+
* need the escape hatch flag (without pulling full committee info).
|
|
93
|
+
*/
|
|
94
|
+
isEscapeHatchOpenAtSlot(slot?: SlotTag): Promise<boolean>;
|
|
95
|
+
/**
|
|
96
|
+
* Get the current validator set
|
|
57
97
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
58
98
|
* @returns The current validator set.
|
|
59
99
|
*/
|
|
60
|
-
getCommittee(
|
|
100
|
+
getCommittee(slot?: SlotTag): Promise<EpochCommitteeInfo>;
|
|
101
|
+
private getEpochAndTimestamp;
|
|
102
|
+
private computeCommittee;
|
|
61
103
|
/**
|
|
62
104
|
* Get the ABI encoding of the proposer index - see ValidatorSelectionLib.sol computeProposerIndex
|
|
63
105
|
*/
|
|
64
|
-
getProposerIndexEncoding(epoch:
|
|
65
|
-
computeProposerIndex(slot:
|
|
106
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
107
|
+
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
108
|
+
/** Returns the current and next L2 slot numbers. */
|
|
109
|
+
getCurrentAndNextSlot(): {
|
|
110
|
+
currentSlot: SlotNumber;
|
|
111
|
+
nextSlot: SlotNumber;
|
|
112
|
+
};
|
|
66
113
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* can be the next slot. If this is the case, then it will send proposals early.
|
|
71
|
-
*
|
|
72
|
-
* If we are at an epoch boundary, then we can update the cache for the next epoch, this is the last check
|
|
73
|
-
* we do in the validator client, so we can update the cache here.
|
|
114
|
+
* Get the proposer attester address in the given L2 slot
|
|
115
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
116
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
74
117
|
*/
|
|
75
|
-
|
|
76
|
-
currentProposer: EthAddress;
|
|
77
|
-
nextProposer: EthAddress;
|
|
78
|
-
currentSlot: bigint;
|
|
79
|
-
nextSlot: bigint;
|
|
80
|
-
}>;
|
|
118
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
81
119
|
/**
|
|
82
|
-
*
|
|
120
|
+
* Get the proposer attester address in the next slot
|
|
121
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
122
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
83
123
|
*/
|
|
84
|
-
|
|
124
|
+
getProposerAttesterAddressInNextSlot(): Promise<EthAddress | undefined>;
|
|
125
|
+
private getProposerAttesterAddressAt;
|
|
126
|
+
getProposerFromEpochCommittee(epochCommitteeInfo: EpochCommitteeInfo, slot: SlotNumber): EthAddress | undefined;
|
|
127
|
+
/** Check if a validator is in the given slot's committee */
|
|
128
|
+
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
129
|
+
/** From the set of given addresses, return all that are on the committee for the given slot */
|
|
130
|
+
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
131
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
85
132
|
}
|
|
86
|
-
|
|
87
|
-
//# sourceMappingURL=epoch_cache.d.ts.map
|
|
133
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXBvY2hfY2FjaGUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9lcG9jaF9jYWNoZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQSxPQUFPLEVBQW9CLGNBQWMsRUFBRSxNQUFNLDJCQUEyQixDQUFDO0FBQzdFLE9BQU8sRUFBRSxXQUFXLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFDMUUsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBRTNELE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUN2RCxPQUFPLEVBQ0wsS0FBSyxpQkFBaUIsRUFPdkIsTUFBTSw2QkFBNkIsQ0FBQztBQUlyQyxPQUFPLEVBQUUsS0FBSyxnQkFBZ0IsRUFBOEIsTUFBTSxhQUFhLENBQUM7QUFFaEYsTUFBTSxNQUFNLFlBQVksR0FBRztJQUN6QixLQUFLLEVBQUUsV0FBVyxDQUFDO0lBQ25CLElBQUksRUFBRSxVQUFVLENBQUM7SUFDakIsRUFBRSxFQUFFLE1BQU0s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AA4B;IAanC,6BAA6B,CAClC,kBAAkB,EAAE,kBAAkB,EACtC,IAAI,EAAE,UAAU,GACf,UAAU,GAAG,SAAS,CAYxB;IAED,4DAA4D;IACtD,aAAa,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAM1E;IAED,+FAA+F;IACzF,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAOtF;IAEK,uBAAuB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,CASrD;CACF"}
|
package/dest/epoch_cache.js
CHANGED
|
@@ -1,105 +1,200 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
2
|
+
import { NoCommitteeError, RollupContract } from '@aztec/ethereum/contracts';
|
|
2
3
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
5
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
|
-
import {
|
|
6
|
-
import { EventEmitter } from 'node:events';
|
|
6
|
+
import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampForSlot, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
7
7
|
import { createPublicClient, encodeAbiParameters, fallback, http, keccak256 } from 'viem';
|
|
8
8
|
import { getEpochCacheConfigEnvVars } from './config.js';
|
|
9
9
|
/**
|
|
10
10
|
* Epoch cache
|
|
11
11
|
*
|
|
12
12
|
* This class is responsible for managing traffic to the l1 node, by caching the validator set.
|
|
13
|
+
* Keeps the last N epochs in cache.
|
|
13
14
|
* It also provides a method to get the current or next proposer, and to check who is in the current slot.
|
|
14
15
|
*
|
|
15
|
-
* If the epoch changes, then we update the stored validator set.
|
|
16
|
-
*
|
|
17
16
|
* Note: This class is very dependent on the system clock being in sync.
|
|
18
|
-
*/ export class EpochCache
|
|
17
|
+
*/ export class EpochCache {
|
|
19
18
|
rollup;
|
|
20
19
|
l1constants;
|
|
21
20
|
dateProvider;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
config;
|
|
22
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
23
|
+
cache;
|
|
24
|
+
allValidators;
|
|
25
|
+
lastValidatorRefresh;
|
|
25
26
|
log;
|
|
26
|
-
constructor(rollup,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
constructor(rollup, l1constants, dateProvider = new DateProvider(), config = {
|
|
28
|
+
cacheSize: 12,
|
|
29
|
+
validatorRefreshIntervalSeconds: 60
|
|
30
|
+
}){
|
|
31
|
+
this.rollup = rollup;
|
|
32
|
+
this.l1constants = l1constants;
|
|
33
|
+
this.dateProvider = dateProvider;
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.cache = new Map();
|
|
36
|
+
this.allValidators = new Set();
|
|
37
|
+
this.lastValidatorRefresh = 0;
|
|
38
|
+
this.log = createLogger('epoch-cache');
|
|
39
|
+
this.log.debug(`Initialized EpochCache`, {
|
|
40
|
+
l1constants
|
|
33
41
|
});
|
|
34
|
-
this.cachedEpoch = getEpochNumberAtTimestamp(this.nowInSeconds(), this.l1constants);
|
|
35
42
|
}
|
|
36
|
-
static async create(
|
|
43
|
+
static async create(rollupOrAddress, config, deps = {}) {
|
|
37
44
|
config = config ?? getEpochCacheConfigEnvVars();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
// Load the rollup contract if we were given an address
|
|
46
|
+
let rollup;
|
|
47
|
+
if ('address' in rollupOrAddress) {
|
|
48
|
+
rollup = rollupOrAddress;
|
|
49
|
+
} else {
|
|
50
|
+
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
51
|
+
const publicClient = createPublicClient({
|
|
52
|
+
chain: chain.chainInfo,
|
|
53
|
+
transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
|
|
54
|
+
batch: false
|
|
55
|
+
}))),
|
|
56
|
+
pollingInterval: config.viemPollingIntervalMS
|
|
57
|
+
});
|
|
58
|
+
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
59
|
+
}
|
|
60
|
+
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, slotDuration, epochDuration, lagInEpochsForValidatorSet, lagInEpochsForRandao] = await Promise.all([
|
|
46
61
|
rollup.getL1StartBlock(),
|
|
47
62
|
rollup.getL1GenesisTime(),
|
|
48
|
-
rollup.
|
|
49
|
-
rollup.
|
|
63
|
+
rollup.getProofSubmissionEpochs(),
|
|
64
|
+
rollup.getSlotDuration(),
|
|
65
|
+
rollup.getEpochDuration(),
|
|
66
|
+
rollup.getLagInEpochsForValidatorSet(),
|
|
67
|
+
rollup.getLagInEpochsForRandao()
|
|
50
68
|
]);
|
|
51
69
|
const l1RollupConstants = {
|
|
52
70
|
l1StartBlock,
|
|
53
71
|
l1GenesisTime,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
72
|
+
proofSubmissionEpochs: Number(proofSubmissionEpochs),
|
|
73
|
+
slotDuration: Number(slotDuration),
|
|
74
|
+
epochDuration: Number(epochDuration),
|
|
75
|
+
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
76
|
+
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
77
|
+
lagInEpochsForRandao: Number(lagInEpochsForRandao)
|
|
78
|
+
};
|
|
79
|
+
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider);
|
|
80
|
+
}
|
|
81
|
+
getL1Constants() {
|
|
82
|
+
return this.l1constants;
|
|
83
|
+
}
|
|
84
|
+
getEpochAndSlotNow() {
|
|
85
|
+
const nowMs = BigInt(this.dateProvider.now());
|
|
86
|
+
const nowSeconds = nowMs / 1000n;
|
|
87
|
+
return {
|
|
88
|
+
...this.getEpochAndSlotAtTimestamp(nowSeconds),
|
|
89
|
+
nowMs
|
|
57
90
|
};
|
|
58
|
-
return new EpochCache(rollup, initialValidators.map((v)=>EthAddress.fromString(v)), sampleSeed, l1RollupConstants, deps.dateProvider);
|
|
59
91
|
}
|
|
60
92
|
nowInSeconds() {
|
|
61
93
|
return BigInt(Math.floor(this.dateProvider.now() / 1000));
|
|
62
94
|
}
|
|
63
|
-
|
|
64
|
-
|
|
95
|
+
getEpochAndSlotAtSlot(slot) {
|
|
96
|
+
const epoch = getEpochAtSlot(slot, this.l1constants);
|
|
97
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
|
|
98
|
+
return {
|
|
99
|
+
epoch,
|
|
100
|
+
ts,
|
|
101
|
+
slot
|
|
102
|
+
};
|
|
65
103
|
}
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
104
|
+
getEpochAndSlotInNextL1Slot() {
|
|
105
|
+
const now = this.nowInSeconds();
|
|
106
|
+
const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
|
|
107
|
+
return {
|
|
108
|
+
...this.getEpochAndSlotAtTimestamp(nextSlotTs),
|
|
109
|
+
now
|
|
110
|
+
};
|
|
69
111
|
}
|
|
70
112
|
getEpochAndSlotAtTimestamp(ts) {
|
|
113
|
+
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
71
114
|
return {
|
|
72
115
|
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
73
|
-
|
|
74
|
-
|
|
116
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
117
|
+
slot
|
|
75
118
|
};
|
|
76
119
|
}
|
|
120
|
+
getCommitteeForEpoch(epoch) {
|
|
121
|
+
const [startSlot] = getSlotRangeForEpoch(epoch, this.l1constants);
|
|
122
|
+
return this.getCommittee(startSlot);
|
|
123
|
+
}
|
|
77
124
|
/**
|
|
78
|
-
*
|
|
125
|
+
* Returns whether the escape hatch is open for the given epoch.
|
|
126
|
+
*
|
|
127
|
+
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
128
|
+
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
129
|
+
*/ async isEscapeHatchOpen(epoch) {
|
|
130
|
+
const cached = this.cache.get(epoch);
|
|
131
|
+
if (cached) {
|
|
132
|
+
return cached.isEscapeHatchOpen;
|
|
133
|
+
}
|
|
134
|
+
const info = await this.getCommitteeForEpoch(epoch);
|
|
135
|
+
return info.isEscapeHatchOpen;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Returns whether the escape hatch is open for the epoch containing the given slot.
|
|
79
139
|
*
|
|
140
|
+
* This is a lightweight helper intended for callers that already have a slot number and only
|
|
141
|
+
* need the escape hatch flag (without pulling full committee info).
|
|
142
|
+
*/ async isEscapeHatchOpenAtSlot(slot = 'now') {
|
|
143
|
+
const epoch = slot === 'now' ? this.getEpochAndSlotNow().epoch : slot === 'next' ? this.getEpochAndSlotInNextL1Slot().epoch : getEpochAtSlot(slot, this.l1constants);
|
|
144
|
+
return await this.isEscapeHatchOpen(epoch);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get the current validator set
|
|
80
148
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
81
149
|
* @returns The current validator set.
|
|
82
|
-
*/ async getCommittee(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
150
|
+
*/ async getCommittee(slot = 'now') {
|
|
151
|
+
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
152
|
+
if (this.cache.has(epoch)) {
|
|
153
|
+
return this.cache.get(epoch);
|
|
154
|
+
}
|
|
155
|
+
const epochData = await this.computeCommittee({
|
|
156
|
+
epoch,
|
|
157
|
+
ts
|
|
158
|
+
});
|
|
159
|
+
// If the committee size is 0 or undefined, then do not cache
|
|
160
|
+
if (!epochData.committee || epochData.committee.length === 0) {
|
|
161
|
+
return epochData;
|
|
162
|
+
}
|
|
163
|
+
this.cache.set(epoch, epochData);
|
|
164
|
+
const toPurge = Array.from(this.cache.keys()).sort((a, b)=>Number(b - a)).slice(this.config.cacheSize);
|
|
165
|
+
toPurge.forEach((key)=>this.cache.delete(key));
|
|
166
|
+
return epochData;
|
|
167
|
+
}
|
|
168
|
+
getEpochAndTimestamp(slot = 'now') {
|
|
169
|
+
if (slot === 'now') {
|
|
170
|
+
return this.getEpochAndSlotNow();
|
|
171
|
+
} else if (slot === 'next') {
|
|
172
|
+
return this.getEpochAndSlotInNextL1Slot();
|
|
173
|
+
} else {
|
|
174
|
+
return this.getEpochAndSlotAtSlot(slot);
|
|
101
175
|
}
|
|
102
|
-
|
|
176
|
+
}
|
|
177
|
+
async computeCommittee(when) {
|
|
178
|
+
const { ts, epoch } = when;
|
|
179
|
+
const [committee, seedBuffer, l1Timestamp, isEscapeHatchOpen] = await Promise.all([
|
|
180
|
+
this.rollup.getCommitteeAt(ts),
|
|
181
|
+
this.rollup.getSampleSeedAt(ts),
|
|
182
|
+
this.rollup.client.getBlock({
|
|
183
|
+
includeTransactions: false
|
|
184
|
+
}).then((b)=>b.timestamp),
|
|
185
|
+
this.rollup.isEscapeHatchOpen(epoch)
|
|
186
|
+
]);
|
|
187
|
+
const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
|
|
188
|
+
const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
189
|
+
if (ts - sub > l1Timestamp) {
|
|
190
|
+
throw new Error(`Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`);
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
committee,
|
|
194
|
+
seed: seedBuffer.toBigInt(),
|
|
195
|
+
epoch,
|
|
196
|
+
isEscapeHatchOpen
|
|
197
|
+
};
|
|
103
198
|
}
|
|
104
199
|
/**
|
|
105
200
|
* Get the ABI encoding of the proposer index - see ValidatorSelectionLib.sol computeProposerIndex
|
|
@@ -118,47 +213,88 @@ import { getEpochCacheConfigEnvVars } from './config.js';
|
|
|
118
213
|
name: 'seed'
|
|
119
214
|
}
|
|
120
215
|
], [
|
|
121
|
-
epoch,
|
|
122
|
-
slot,
|
|
216
|
+
BigInt(epoch),
|
|
217
|
+
BigInt(slot),
|
|
123
218
|
seed
|
|
124
219
|
]);
|
|
125
220
|
}
|
|
126
221
|
computeProposerIndex(slot, epoch, seed, size) {
|
|
222
|
+
// if committe size is 0, then mod 1 is 0
|
|
223
|
+
if (size === 0n) {
|
|
224
|
+
return 0n;
|
|
225
|
+
}
|
|
127
226
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
128
227
|
}
|
|
129
|
-
/**
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
* We return the next proposer as the node will check if it is the proposer at the next ethereum block, which
|
|
133
|
-
* can be the next slot. If this is the case, then it will send proposals early.
|
|
134
|
-
*
|
|
135
|
-
* If we are at an epoch boundary, then we can update the cache for the next epoch, this is the last check
|
|
136
|
-
* we do in the validator client, so we can update the cache here.
|
|
137
|
-
*/ async getProposerInCurrentOrNextSlot() {
|
|
138
|
-
// Validators are sorted by their index in the committee, and getValidatorSet will cache
|
|
139
|
-
const committee = await this.getCommittee();
|
|
140
|
-
const { slot: currentSlot, epoch: currentEpoch } = this.getEpochAndSlotNow();
|
|
141
|
-
const { slot: nextSlot, epoch: nextEpoch } = this.getEpochAndSlotInNextSlot();
|
|
142
|
-
// Compute the proposer in this and the next slot
|
|
143
|
-
const proposerIndex = this.computeProposerIndex(currentSlot, this.cachedEpoch, this.cachedSampleSeed, BigInt(committee.length));
|
|
144
|
-
// Check if the next proposer is in the next epoch
|
|
145
|
-
if (nextEpoch !== currentEpoch) {
|
|
146
|
-
await this.getCommittee(/*next slot*/ true);
|
|
147
|
-
}
|
|
148
|
-
const nextProposerIndex = this.computeProposerIndex(nextSlot, this.cachedEpoch, this.cachedSampleSeed, BigInt(committee.length));
|
|
149
|
-
const currentProposer = committee[Number(proposerIndex)];
|
|
150
|
-
const nextProposer = committee[Number(nextProposerIndex)];
|
|
228
|
+
/** Returns the current and next L2 slot numbers. */ getCurrentAndNextSlot() {
|
|
229
|
+
const current = this.getEpochAndSlotNow();
|
|
230
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
151
231
|
return {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
currentSlot,
|
|
155
|
-
nextSlot
|
|
232
|
+
currentSlot: current.slot,
|
|
233
|
+
nextSlot: next.slot
|
|
156
234
|
};
|
|
157
235
|
}
|
|
158
236
|
/**
|
|
159
|
-
*
|
|
160
|
-
|
|
161
|
-
|
|
237
|
+
* Get the proposer attester address in the given L2 slot
|
|
238
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
239
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
240
|
+
*/ getProposerAttesterAddressInSlot(slot) {
|
|
241
|
+
const epochAndSlot = this.getEpochAndSlotAtSlot(slot);
|
|
242
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Get the proposer attester address in the next slot
|
|
246
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
247
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
248
|
+
*/ getProposerAttesterAddressInNextSlot() {
|
|
249
|
+
const epochAndSlot = this.getEpochAndSlotInNextL1Slot();
|
|
250
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get the proposer attester address at a given epoch and slot
|
|
254
|
+
* @param when - The epoch and slot to get the proposer attester address at
|
|
255
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
256
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
257
|
+
*/ async getProposerAttesterAddressAt(when) {
|
|
258
|
+
const { epoch, slot } = when;
|
|
259
|
+
const { committee, seed } = await this.getCommittee(slot);
|
|
260
|
+
if (!committee) {
|
|
261
|
+
throw new NoCommitteeError();
|
|
262
|
+
} else if (committee.length === 0) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
const proposerIndex = this.computeProposerIndex(slot, epoch, seed, BigInt(committee.length));
|
|
266
|
+
return committee[Number(proposerIndex)];
|
|
267
|
+
}
|
|
268
|
+
getProposerFromEpochCommittee(epochCommitteeInfo, slot) {
|
|
269
|
+
if (!epochCommitteeInfo.committee || epochCommitteeInfo.committee.length === 0) {
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
const proposerIndex = this.computeProposerIndex(slot, epochCommitteeInfo.epoch, epochCommitteeInfo.seed, BigInt(epochCommitteeInfo.committee.length));
|
|
273
|
+
return epochCommitteeInfo.committee[Number(proposerIndex)];
|
|
274
|
+
}
|
|
275
|
+
/** Check if a validator is in the given slot's committee */ async isInCommittee(slot, validator) {
|
|
276
|
+
const { committee } = await this.getCommittee(slot);
|
|
277
|
+
if (!committee) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
162
280
|
return committee.some((v)=>v.equals(validator));
|
|
163
281
|
}
|
|
282
|
+
/** From the set of given addresses, return all that are on the committee for the given slot */ async filterInCommittee(slot, validators) {
|
|
283
|
+
const { committee } = await this.getCommittee(slot);
|
|
284
|
+
if (!committee) {
|
|
285
|
+
return [];
|
|
286
|
+
}
|
|
287
|
+
const committeeSet = new Set(committee.map((v)=>v.toString()));
|
|
288
|
+
return validators.filter((v)=>committeeSet.has(v.toString()));
|
|
289
|
+
}
|
|
290
|
+
async getRegisteredValidators() {
|
|
291
|
+
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
292
|
+
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
293
|
+
if (validatorRefreshTime < this.dateProvider.now()) {
|
|
294
|
+
const currentSet = await this.rollup.getAttesters();
|
|
295
|
+
this.allValidators = new Set(currentSet.map((v)=>v.toString()));
|
|
296
|
+
this.lastValidatorRefresh = this.dateProvider.now();
|
|
297
|
+
}
|
|
298
|
+
return Array.from(this.allValidators.keys()).map((v)=>EthAddress.fromString(v));
|
|
299
|
+
}
|
|
164
300
|
}
|
package/dest/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from './epoch_cache.js';
|
|
2
2
|
export * from './config.js';
|
|
3
|
-
//# sourceMappingURL=
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLGtCQUFrQixDQUFDO0FBQ2pDLGNBQWMsYUFBYSxDQUFDIn0=
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/epoch-cache",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1-commit.0b941701",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -15,12 +15,10 @@
|
|
|
15
15
|
"tsconfig": "./tsconfig.json"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "yarn clean && tsc
|
|
19
|
-
"build:dev": "tsc
|
|
18
|
+
"build": "yarn clean && ../scripts/tsc.sh",
|
|
19
|
+
"build:dev": "../scripts/tsc.sh --watch",
|
|
20
20
|
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
21
|
-
"
|
|
22
|
-
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
|
|
23
|
-
"start:dev": "tsc-watch -p tsconfig.json --onSuccess 'yarn start'",
|
|
21
|
+
"start:dev": "concurrently -k \"../scripts/tsc.sh --watch\" \"nodemon --watch dest --exec yarn start\"",
|
|
24
22
|
"start": "node ./dest/index.js",
|
|
25
23
|
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
26
24
|
},
|
|
@@ -28,25 +26,26 @@
|
|
|
28
26
|
"../package.common.json"
|
|
29
27
|
],
|
|
30
28
|
"dependencies": {
|
|
31
|
-
"@aztec/ethereum": "0.0.
|
|
32
|
-
"@aztec/foundation": "0.0.
|
|
33
|
-
"@aztec/l1-artifacts": "0.0.
|
|
34
|
-
"@aztec/stdlib": "0.0.
|
|
29
|
+
"@aztec/ethereum": "0.0.1-commit.0b941701",
|
|
30
|
+
"@aztec/foundation": "0.0.1-commit.0b941701",
|
|
31
|
+
"@aztec/l1-artifacts": "0.0.1-commit.0b941701",
|
|
32
|
+
"@aztec/stdlib": "0.0.1-commit.0b941701",
|
|
35
33
|
"@viem/anvil": "^0.0.10",
|
|
36
34
|
"dotenv": "^16.0.3",
|
|
37
35
|
"get-port": "^7.1.0",
|
|
38
|
-
"jest-mock-extended": "^
|
|
36
|
+
"jest-mock-extended": "^4.0.0",
|
|
39
37
|
"tslib": "^2.4.0",
|
|
40
|
-
"viem": "2.
|
|
38
|
+
"viem": "npm:@aztec/viem@2.38.2",
|
|
41
39
|
"zod": "^3.23.8"
|
|
42
40
|
},
|
|
43
41
|
"devDependencies": {
|
|
44
|
-
"@jest/globals": "^
|
|
45
|
-
"@types/jest": "^
|
|
46
|
-
"@types/node": "^
|
|
47
|
-
"
|
|
42
|
+
"@jest/globals": "^30.0.0",
|
|
43
|
+
"@types/jest": "^30.0.0",
|
|
44
|
+
"@types/node": "^22.15.17",
|
|
45
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
46
|
+
"jest": "^30.0.0",
|
|
48
47
|
"ts-node": "^10.9.1",
|
|
49
|
-
"typescript": "^5.
|
|
48
|
+
"typescript": "^5.3.3"
|
|
50
49
|
},
|
|
51
50
|
"files": [
|
|
52
51
|
"dest",
|
|
@@ -85,9 +84,13 @@
|
|
|
85
84
|
"testTimeout": 120000,
|
|
86
85
|
"setupFiles": [
|
|
87
86
|
"../../foundation/src/jest/setup.mjs"
|
|
87
|
+
],
|
|
88
|
+
"testEnvironment": "../../foundation/src/jest/env.mjs",
|
|
89
|
+
"setupFilesAfterEnv": [
|
|
90
|
+
"../../foundation/src/jest/setupAfterEnv.mjs"
|
|
88
91
|
]
|
|
89
92
|
},
|
|
90
93
|
"engines": {
|
|
91
|
-
"node": ">=
|
|
94
|
+
"node": ">=20.10"
|
|
92
95
|
}
|
|
93
96
|
}
|
package/src/config.ts
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type L1ReaderConfig,
|
|
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';
|
|
7
3
|
|
|
8
4
|
export type EpochCacheConfig = Pick<
|
|
9
5
|
L1ReaderConfig & L1ContractsConfig,
|
|
10
|
-
| '
|
|
11
|
-
| 'l1ChainId'
|
|
12
|
-
| 'viemPollingIntervalMS'
|
|
13
|
-
| 'aztecSlotDuration'
|
|
14
|
-
| 'ethereumSlotDuration'
|
|
15
|
-
| 'aztecEpochDuration'
|
|
6
|
+
'l1RpcUrls' | 'l1ChainId' | 'viemPollingIntervalMS' | 'ethereumSlotDuration'
|
|
16
7
|
>;
|
|
17
8
|
|
|
18
9
|
export function getEpochCacheConfigEnvVars(): EpochCacheConfig {
|
package/src/epoch_cache.ts
CHANGED
|
@@ -1,231 +1,378 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
2
|
+
import { NoCommitteeError, RollupContract } from '@aztec/ethereum/contracts';
|
|
3
|
+
import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
4
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
5
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
6
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
7
|
import {
|
|
6
|
-
EmptyL1RollupConstants,
|
|
7
8
|
type L1RollupConstants,
|
|
9
|
+
getEpochAtSlot,
|
|
8
10
|
getEpochNumberAtTimestamp,
|
|
9
11
|
getSlotAtTimestamp,
|
|
12
|
+
getSlotRangeForEpoch,
|
|
13
|
+
getTimestampForSlot,
|
|
14
|
+
getTimestampRangeForEpoch,
|
|
10
15
|
} from '@aztec/stdlib/epoch-helpers';
|
|
11
16
|
|
|
12
|
-
import { EventEmitter } from 'node:events';
|
|
13
17
|
import { createPublicClient, encodeAbiParameters, fallback, http, keccak256 } from 'viem';
|
|
14
18
|
|
|
15
19
|
import { type EpochCacheConfig, getEpochCacheConfigEnvVars } from './config.js';
|
|
16
20
|
|
|
17
|
-
type EpochAndSlot = {
|
|
18
|
-
epoch:
|
|
19
|
-
slot:
|
|
21
|
+
export type EpochAndSlot = {
|
|
22
|
+
epoch: EpochNumber;
|
|
23
|
+
slot: SlotNumber;
|
|
20
24
|
ts: bigint;
|
|
21
25
|
};
|
|
22
26
|
|
|
27
|
+
export type EpochCommitteeInfo = {
|
|
28
|
+
committee: EthAddress[] | undefined;
|
|
29
|
+
seed: bigint;
|
|
30
|
+
epoch: EpochNumber;
|
|
31
|
+
/** True if the epoch is within an open escape hatch window. */
|
|
32
|
+
isEscapeHatchOpen: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
36
|
+
|
|
23
37
|
export interface EpochCacheInterface {
|
|
24
|
-
getCommittee(
|
|
25
|
-
getEpochAndSlotNow(): EpochAndSlot;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
isInCommittee(validator: EthAddress): Promise<boolean>;
|
|
38
|
+
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
39
|
+
getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint };
|
|
40
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint };
|
|
41
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
42
|
+
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
43
|
+
getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber };
|
|
44
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
45
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
46
|
+
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
47
|
+
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
35
48
|
}
|
|
36
49
|
|
|
37
50
|
/**
|
|
38
51
|
* Epoch cache
|
|
39
52
|
*
|
|
40
53
|
* This class is responsible for managing traffic to the l1 node, by caching the validator set.
|
|
54
|
+
* Keeps the last N epochs in cache.
|
|
41
55
|
* It also provides a method to get the current or next proposer, and to check who is in the current slot.
|
|
42
56
|
*
|
|
43
|
-
* If the epoch changes, then we update the stored validator set.
|
|
44
|
-
*
|
|
45
57
|
* Note: This class is very dependent on the system clock being in sync.
|
|
46
58
|
*/
|
|
47
|
-
export class EpochCache
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
private
|
|
52
|
-
private cachedEpoch: bigint;
|
|
53
|
-
private cachedSampleSeed: bigint;
|
|
59
|
+
export class EpochCache implements EpochCacheInterface {
|
|
60
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
61
|
+
protected cache: Map<EpochNumber, EpochCommitteeInfo> = new Map();
|
|
62
|
+
private allValidators: Set<string> = new Set();
|
|
63
|
+
private lastValidatorRefresh = 0;
|
|
54
64
|
private readonly log: Logger = createLogger('epoch-cache');
|
|
55
65
|
|
|
56
66
|
constructor(
|
|
57
67
|
private rollup: RollupContract,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
private readonly l1constants: L1RollupConstants & {
|
|
69
|
+
lagInEpochsForValidatorSet: number;
|
|
70
|
+
lagInEpochsForRandao: number;
|
|
71
|
+
},
|
|
61
72
|
private readonly dateProvider: DateProvider = new DateProvider(),
|
|
73
|
+
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
|
|
62
74
|
) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.log.debug(`Initialized EpochCache with constants and validators`, { l1constants, initialValidators });
|
|
68
|
-
|
|
69
|
-
this.cachedEpoch = getEpochNumberAtTimestamp(this.nowInSeconds(), this.l1constants);
|
|
75
|
+
this.log.debug(`Initialized EpochCache`, {
|
|
76
|
+
l1constants,
|
|
77
|
+
});
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
static async create(
|
|
73
|
-
|
|
81
|
+
rollupOrAddress: EthAddress | RollupContract,
|
|
74
82
|
config?: EpochCacheConfig,
|
|
75
83
|
deps: { dateProvider?: DateProvider } = {},
|
|
76
84
|
) {
|
|
77
85
|
config = config ?? getEpochCacheConfigEnvVars();
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
// Load the rollup contract if we were given an address
|
|
88
|
+
let rollup: RollupContract;
|
|
89
|
+
if ('address' in rollupOrAddress) {
|
|
90
|
+
rollup = rollupOrAddress;
|
|
91
|
+
} else {
|
|
92
|
+
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
93
|
+
const publicClient = createPublicClient({
|
|
94
|
+
chain: chain.chainInfo,
|
|
95
|
+
transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
|
|
96
|
+
pollingInterval: config.viemPollingIntervalMS,
|
|
97
|
+
});
|
|
98
|
+
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
99
|
+
}
|
|
85
100
|
|
|
86
|
-
const
|
|
87
|
-
|
|
101
|
+
const [
|
|
102
|
+
l1StartBlock,
|
|
103
|
+
l1GenesisTime,
|
|
104
|
+
proofSubmissionEpochs,
|
|
105
|
+
slotDuration,
|
|
106
|
+
epochDuration,
|
|
107
|
+
lagInEpochsForValidatorSet,
|
|
108
|
+
lagInEpochsForRandao,
|
|
109
|
+
] = await Promise.all([
|
|
88
110
|
rollup.getL1StartBlock(),
|
|
89
111
|
rollup.getL1GenesisTime(),
|
|
90
|
-
rollup.
|
|
91
|
-
rollup.
|
|
112
|
+
rollup.getProofSubmissionEpochs(),
|
|
113
|
+
rollup.getSlotDuration(),
|
|
114
|
+
rollup.getEpochDuration(),
|
|
115
|
+
rollup.getLagInEpochsForValidatorSet(),
|
|
116
|
+
rollup.getLagInEpochsForRandao(),
|
|
92
117
|
] as const);
|
|
93
118
|
|
|
94
|
-
const l1RollupConstants
|
|
119
|
+
const l1RollupConstants = {
|
|
95
120
|
l1StartBlock,
|
|
96
121
|
l1GenesisTime,
|
|
97
|
-
|
|
98
|
-
|
|
122
|
+
proofSubmissionEpochs: Number(proofSubmissionEpochs),
|
|
123
|
+
slotDuration: Number(slotDuration),
|
|
124
|
+
epochDuration: Number(epochDuration),
|
|
99
125
|
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
126
|
+
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
127
|
+
lagInEpochsForRandao: Number(lagInEpochsForRandao),
|
|
100
128
|
};
|
|
101
129
|
|
|
102
|
-
return new EpochCache(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
deps.dateProvider,
|
|
108
|
-
);
|
|
130
|
+
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public getL1Constants(): L1RollupConstants {
|
|
134
|
+
return this.l1constants;
|
|
109
135
|
}
|
|
110
136
|
|
|
111
|
-
|
|
137
|
+
public getEpochAndSlotNow(): EpochAndSlot & { nowMs: bigint } {
|
|
138
|
+
const nowMs = BigInt(this.dateProvider.now());
|
|
139
|
+
const nowSeconds = nowMs / 1000n;
|
|
140
|
+
return { ...this.getEpochAndSlotAtTimestamp(nowSeconds), nowMs };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public nowInSeconds(): bigint {
|
|
112
144
|
return BigInt(Math.floor(this.dateProvider.now() / 1000));
|
|
113
145
|
}
|
|
114
146
|
|
|
115
|
-
|
|
116
|
-
|
|
147
|
+
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
148
|
+
const epoch = getEpochAtSlot(slot, this.l1constants);
|
|
149
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
|
|
150
|
+
return { epoch, ts, slot };
|
|
117
151
|
}
|
|
118
152
|
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
153
|
+
public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
|
|
154
|
+
const now = this.nowInSeconds();
|
|
155
|
+
const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
|
|
156
|
+
return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), now };
|
|
122
157
|
}
|
|
123
158
|
|
|
124
159
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
160
|
+
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
125
161
|
return {
|
|
126
162
|
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
127
|
-
|
|
128
|
-
|
|
163
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
164
|
+
slot,
|
|
129
165
|
};
|
|
130
166
|
}
|
|
131
167
|
|
|
168
|
+
public getCommitteeForEpoch(epoch: EpochNumber): Promise<EpochCommitteeInfo> {
|
|
169
|
+
const [startSlot] = getSlotRangeForEpoch(epoch, this.l1constants);
|
|
170
|
+
return this.getCommittee(startSlot);
|
|
171
|
+
}
|
|
172
|
+
|
|
132
173
|
/**
|
|
133
|
-
*
|
|
174
|
+
* Returns whether the escape hatch is open for the given epoch.
|
|
175
|
+
*
|
|
176
|
+
* Uses the already-cached EpochCommitteeInfo when available. If not cached, it will fetch
|
|
177
|
+
* the epoch committee info (which includes the escape hatch flag) and return it.
|
|
178
|
+
*/
|
|
179
|
+
public async isEscapeHatchOpen(epoch: EpochNumber): Promise<boolean> {
|
|
180
|
+
const cached = this.cache.get(epoch);
|
|
181
|
+
if (cached) {
|
|
182
|
+
return cached.isEscapeHatchOpen;
|
|
183
|
+
}
|
|
184
|
+
const info = await this.getCommitteeForEpoch(epoch);
|
|
185
|
+
return info.isEscapeHatchOpen;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Returns whether the escape hatch is open for the epoch containing the given slot.
|
|
134
190
|
*
|
|
191
|
+
* This is a lightweight helper intended for callers that already have a slot number and only
|
|
192
|
+
* need the escape hatch flag (without pulling full committee info).
|
|
193
|
+
*/
|
|
194
|
+
public async isEscapeHatchOpenAtSlot(slot: SlotTag = 'now'): Promise<boolean> {
|
|
195
|
+
const epoch =
|
|
196
|
+
slot === 'now'
|
|
197
|
+
? this.getEpochAndSlotNow().epoch
|
|
198
|
+
: slot === 'next'
|
|
199
|
+
? this.getEpochAndSlotInNextL1Slot().epoch
|
|
200
|
+
: getEpochAtSlot(slot, this.l1constants);
|
|
201
|
+
|
|
202
|
+
return await this.isEscapeHatchOpen(epoch);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Get the current validator set
|
|
135
207
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
136
208
|
* @returns The current validator set.
|
|
137
209
|
*/
|
|
138
|
-
async getCommittee(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
this.log.debug(`Updating validator set for new epoch ${calculatedEpoch}`, {
|
|
144
|
-
epoch: calculatedEpoch,
|
|
145
|
-
previousEpoch: this.cachedEpoch,
|
|
146
|
-
});
|
|
147
|
-
const [committeeAtTs, sampleSeedAtTs] = await Promise.all([
|
|
148
|
-
this.rollup.getCommitteeAt(ts),
|
|
149
|
-
this.rollup.getSampleSeedAt(ts),
|
|
150
|
-
]);
|
|
151
|
-
this.committee = committeeAtTs.map((v: `0x${string}`) => EthAddress.fromString(v));
|
|
152
|
-
this.cachedEpoch = calculatedEpoch;
|
|
153
|
-
this.cachedSampleSeed = sampleSeedAtTs;
|
|
154
|
-
this.log.debug(`Updated validator set for epoch ${calculatedEpoch}`, { commitee: this.committee });
|
|
155
|
-
this.emit('committeeChanged', this.committee, calculatedEpoch);
|
|
210
|
+
public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
|
|
211
|
+
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
212
|
+
|
|
213
|
+
if (this.cache.has(epoch)) {
|
|
214
|
+
return this.cache.get(epoch)!;
|
|
156
215
|
}
|
|
157
216
|
|
|
158
|
-
|
|
217
|
+
const epochData = await this.computeCommittee({ epoch, ts });
|
|
218
|
+
// If the committee size is 0 or undefined, then do not cache
|
|
219
|
+
if (!epochData.committee || epochData.committee.length === 0) {
|
|
220
|
+
return epochData;
|
|
221
|
+
}
|
|
222
|
+
this.cache.set(epoch, epochData);
|
|
223
|
+
|
|
224
|
+
const toPurge = Array.from(this.cache.keys())
|
|
225
|
+
.sort((a, b) => Number(b - a))
|
|
226
|
+
.slice(this.config.cacheSize);
|
|
227
|
+
toPurge.forEach(key => this.cache.delete(key));
|
|
228
|
+
|
|
229
|
+
return epochData;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
233
|
+
if (slot === 'now') {
|
|
234
|
+
return this.getEpochAndSlotNow();
|
|
235
|
+
} else if (slot === 'next') {
|
|
236
|
+
return this.getEpochAndSlotInNextL1Slot();
|
|
237
|
+
} else {
|
|
238
|
+
return this.getEpochAndSlotAtSlot(slot);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
|
|
243
|
+
const { ts, epoch } = when;
|
|
244
|
+
const [committee, seedBuffer, l1Timestamp, isEscapeHatchOpen] = await Promise.all([
|
|
245
|
+
this.rollup.getCommitteeAt(ts),
|
|
246
|
+
this.rollup.getSampleSeedAt(ts),
|
|
247
|
+
this.rollup.client.getBlock({ includeTransactions: false }).then(b => b.timestamp),
|
|
248
|
+
this.rollup.isEscapeHatchOpen(epoch),
|
|
249
|
+
]);
|
|
250
|
+
const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
|
|
251
|
+
const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
252
|
+
if (ts - sub > l1Timestamp) {
|
|
253
|
+
throw new Error(
|
|
254
|
+
`Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
return { committee, seed: seedBuffer.toBigInt(), epoch, isEscapeHatchOpen };
|
|
159
258
|
}
|
|
160
259
|
|
|
161
260
|
/**
|
|
162
261
|
* Get the ABI encoding of the proposer index - see ValidatorSelectionLib.sol computeProposerIndex
|
|
163
262
|
*/
|
|
164
|
-
getProposerIndexEncoding(epoch:
|
|
263
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
|
|
165
264
|
return encodeAbiParameters(
|
|
166
265
|
[
|
|
167
266
|
{ type: 'uint256', name: 'epoch' },
|
|
168
267
|
{ type: 'uint256', name: 'slot' },
|
|
169
268
|
{ type: 'uint256', name: 'seed' },
|
|
170
269
|
],
|
|
171
|
-
[epoch, slot, seed],
|
|
270
|
+
[BigInt(epoch), BigInt(slot), seed],
|
|
172
271
|
);
|
|
173
272
|
}
|
|
174
273
|
|
|
175
|
-
computeProposerIndex(slot:
|
|
274
|
+
public computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint {
|
|
275
|
+
// if committe size is 0, then mod 1 is 0
|
|
276
|
+
if (size === 0n) {
|
|
277
|
+
return 0n;
|
|
278
|
+
}
|
|
176
279
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
177
280
|
}
|
|
178
281
|
|
|
282
|
+
/** Returns the current and next L2 slot numbers. */
|
|
283
|
+
public getCurrentAndNextSlot(): { currentSlot: SlotNumber; nextSlot: SlotNumber } {
|
|
284
|
+
const current = this.getEpochAndSlotNow();
|
|
285
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
286
|
+
|
|
287
|
+
return {
|
|
288
|
+
currentSlot: current.slot,
|
|
289
|
+
nextSlot: next.slot,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
179
293
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* can be the next slot. If this is the case, then it will send proposals early.
|
|
184
|
-
*
|
|
185
|
-
* If we are at an epoch boundary, then we can update the cache for the next epoch, this is the last check
|
|
186
|
-
* we do in the validator client, so we can update the cache here.
|
|
294
|
+
* Get the proposer attester address in the given L2 slot
|
|
295
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
296
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
187
297
|
*/
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
nextSlot: bigint;
|
|
193
|
-
}> {
|
|
194
|
-
// Validators are sorted by their index in the committee, and getValidatorSet will cache
|
|
195
|
-
const committee = await this.getCommittee();
|
|
196
|
-
const { slot: currentSlot, epoch: currentEpoch } = this.getEpochAndSlotNow();
|
|
197
|
-
const { slot: nextSlot, epoch: nextEpoch } = this.getEpochAndSlotInNextSlot();
|
|
198
|
-
|
|
199
|
-
// Compute the proposer in this and the next slot
|
|
200
|
-
const proposerIndex = this.computeProposerIndex(
|
|
201
|
-
currentSlot,
|
|
202
|
-
this.cachedEpoch,
|
|
203
|
-
this.cachedSampleSeed,
|
|
204
|
-
BigInt(committee.length),
|
|
205
|
-
);
|
|
298
|
+
public getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined> {
|
|
299
|
+
const epochAndSlot = this.getEpochAndSlotAtSlot(slot);
|
|
300
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
301
|
+
}
|
|
206
302
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
303
|
+
/**
|
|
304
|
+
* Get the proposer attester address in the next slot
|
|
305
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
306
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
307
|
+
*/
|
|
308
|
+
public getProposerAttesterAddressInNextSlot(): Promise<EthAddress | undefined> {
|
|
309
|
+
const epochAndSlot = this.getEpochAndSlotInNextL1Slot();
|
|
310
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Get the proposer attester address at a given epoch and slot
|
|
315
|
+
* @param when - The epoch and slot to get the proposer attester address at
|
|
316
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
317
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
318
|
+
*/
|
|
319
|
+
private async getProposerAttesterAddressAt(when: EpochAndSlot) {
|
|
320
|
+
const { epoch, slot } = when;
|
|
321
|
+
const { committee, seed } = await this.getCommittee(slot);
|
|
322
|
+
if (!committee) {
|
|
323
|
+
throw new NoCommitteeError();
|
|
324
|
+
} else if (committee.length === 0) {
|
|
325
|
+
return undefined;
|
|
210
326
|
}
|
|
211
|
-
const nextProposerIndex = this.computeProposerIndex(
|
|
212
|
-
nextSlot,
|
|
213
|
-
this.cachedEpoch,
|
|
214
|
-
this.cachedSampleSeed,
|
|
215
|
-
BigInt(committee.length),
|
|
216
|
-
);
|
|
217
327
|
|
|
218
|
-
const
|
|
219
|
-
|
|
328
|
+
const proposerIndex = this.computeProposerIndex(slot, epoch, seed, BigInt(committee.length));
|
|
329
|
+
return committee[Number(proposerIndex)];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
public getProposerFromEpochCommittee(
|
|
333
|
+
epochCommitteeInfo: EpochCommitteeInfo,
|
|
334
|
+
slot: SlotNumber,
|
|
335
|
+
): EthAddress | undefined {
|
|
336
|
+
if (!epochCommitteeInfo.committee || epochCommitteeInfo.committee.length === 0) {
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
const proposerIndex = this.computeProposerIndex(
|
|
340
|
+
slot,
|
|
341
|
+
epochCommitteeInfo.epoch,
|
|
342
|
+
epochCommitteeInfo.seed,
|
|
343
|
+
BigInt(epochCommitteeInfo.committee.length),
|
|
344
|
+
);
|
|
220
345
|
|
|
221
|
-
return
|
|
346
|
+
return epochCommitteeInfo.committee[Number(proposerIndex)];
|
|
222
347
|
}
|
|
223
348
|
|
|
224
|
-
/**
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
349
|
+
/** Check if a validator is in the given slot's committee */
|
|
350
|
+
async isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean> {
|
|
351
|
+
const { committee } = await this.getCommittee(slot);
|
|
352
|
+
if (!committee) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
229
355
|
return committee.some(v => v.equals(validator));
|
|
230
356
|
}
|
|
357
|
+
|
|
358
|
+
/** From the set of given addresses, return all that are on the committee for the given slot */
|
|
359
|
+
async filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]> {
|
|
360
|
+
const { committee } = await this.getCommittee(slot);
|
|
361
|
+
if (!committee) {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
const committeeSet = new Set(committee.map(v => v.toString()));
|
|
365
|
+
return validators.filter(v => committeeSet.has(v.toString()));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async getRegisteredValidators(): Promise<EthAddress[]> {
|
|
369
|
+
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
370
|
+
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
371
|
+
if (validatorRefreshTime < this.dateProvider.now()) {
|
|
372
|
+
const currentSet = await this.rollup.getAttesters();
|
|
373
|
+
this.allValidators = new Set(currentSet.map(v => v.toString()));
|
|
374
|
+
this.lastValidatorRefresh = this.dateProvider.now();
|
|
375
|
+
}
|
|
376
|
+
return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
|
|
377
|
+
}
|
|
231
378
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"timestamp_provider.d.ts","sourceRoot":"","sources":["../src/timestamp_provider.ts"],"names":[],"mappings":""}
|
|
File without changes
|
|
File without changes
|