@aztec/epoch-cache 0.0.0-test.1 → 0.0.1-commit.1142ef1
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 +84 -47
- package/dest/epoch_cache.d.ts.map +1 -1
- package/dest/epoch_cache.js +207 -89
- package/dest/index.d.ts +1 -1
- package/package.json +21 -18
- package/src/config.ts +3 -12
- package/src/epoch_cache.ts +253 -125
- 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,124 @@
|
|
|
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
|
+
};
|
|
17
|
+
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
13
18
|
export interface EpochCacheInterface {
|
|
14
|
-
getCommittee(
|
|
19
|
+
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
15
20
|
getEpochAndSlotNow(): EpochAndSlot;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
22
|
+
now: bigint;
|
|
23
|
+
};
|
|
24
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
25
|
+
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
26
|
+
getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
|
|
27
|
+
currentProposer: EthAddress | undefined;
|
|
28
|
+
nextProposer: EthAddress | undefined;
|
|
29
|
+
currentSlot: SlotNumber;
|
|
30
|
+
nextSlot: SlotNumber;
|
|
23
31
|
}>;
|
|
24
|
-
|
|
32
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
33
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
34
|
+
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
35
|
+
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
25
36
|
}
|
|
26
37
|
/**
|
|
27
38
|
* Epoch cache
|
|
28
39
|
*
|
|
29
40
|
* This class is responsible for managing traffic to the l1 node, by caching the validator set.
|
|
41
|
+
* Keeps the last N epochs in cache.
|
|
30
42
|
* It also provides a method to get the current or next proposer, and to check who is in the current slot.
|
|
31
43
|
*
|
|
32
|
-
* If the epoch changes, then we update the stored validator set.
|
|
33
|
-
*
|
|
34
44
|
* Note: This class is very dependent on the system clock being in sync.
|
|
35
45
|
*/
|
|
36
|
-
export declare class EpochCache
|
|
37
|
-
committeeChanged: [EthAddress[], bigint];
|
|
38
|
-
}> implements EpochCacheInterface {
|
|
46
|
+
export declare class EpochCache implements EpochCacheInterface {
|
|
39
47
|
private rollup;
|
|
40
48
|
private readonly l1constants;
|
|
41
49
|
private readonly dateProvider;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
protected readonly config: {
|
|
51
|
+
cacheSize: number;
|
|
52
|
+
validatorRefreshIntervalSeconds: number;
|
|
53
|
+
};
|
|
54
|
+
protected cache: Map<EpochNumber, EpochCommitteeInfo>;
|
|
55
|
+
private allValidators;
|
|
56
|
+
private lastValidatorRefresh;
|
|
45
57
|
private readonly log;
|
|
46
|
-
constructor(rollup: RollupContract,
|
|
47
|
-
|
|
58
|
+
constructor(rollup: RollupContract, l1constants: L1RollupConstants & {
|
|
59
|
+
lagInEpochsForValidatorSet: number;
|
|
60
|
+
lagInEpochsForRandao: number;
|
|
61
|
+
}, dateProvider?: DateProvider, config?: {
|
|
62
|
+
cacheSize: number;
|
|
63
|
+
validatorRefreshIntervalSeconds: number;
|
|
64
|
+
});
|
|
65
|
+
static create(rollupOrAddress: EthAddress | RollupContract, config?: EpochCacheConfig, deps?: {
|
|
48
66
|
dateProvider?: DateProvider;
|
|
49
67
|
}): Promise<EpochCache>;
|
|
50
|
-
|
|
51
|
-
getEpochAndSlotNow(): EpochAndSlot
|
|
52
|
-
|
|
68
|
+
getL1Constants(): L1RollupConstants;
|
|
69
|
+
getEpochAndSlotNow(): EpochAndSlot & {
|
|
70
|
+
now: bigint;
|
|
71
|
+
};
|
|
72
|
+
nowInSeconds(): bigint;
|
|
73
|
+
private getEpochAndSlotAtSlot;
|
|
74
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & {
|
|
75
|
+
now: bigint;
|
|
76
|
+
};
|
|
53
77
|
private getEpochAndSlotAtTimestamp;
|
|
78
|
+
getCommitteeForEpoch(epoch: EpochNumber): Promise<EpochCommitteeInfo>;
|
|
54
79
|
/**
|
|
55
80
|
* Get the current validator set
|
|
56
|
-
*
|
|
57
81
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
58
82
|
* @returns The current validator set.
|
|
59
83
|
*/
|
|
60
|
-
getCommittee(
|
|
84
|
+
getCommittee(slot?: SlotTag): Promise<EpochCommitteeInfo>;
|
|
85
|
+
private getEpochAndTimestamp;
|
|
86
|
+
private computeCommittee;
|
|
61
87
|
/**
|
|
62
88
|
* Get the ABI encoding of the proposer index - see ValidatorSelectionLib.sol computeProposerIndex
|
|
63
89
|
*/
|
|
64
|
-
getProposerIndexEncoding(epoch:
|
|
65
|
-
computeProposerIndex(slot:
|
|
90
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
91
|
+
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
66
92
|
/**
|
|
67
|
-
* Returns the current and next proposer
|
|
93
|
+
* Returns the current and next proposer's attester address
|
|
68
94
|
*
|
|
69
|
-
* We return the next proposer as the node will check if it is the proposer at the next ethereum block,
|
|
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.
|
|
95
|
+
* We return the next proposer's attester address as the node will check if it is the proposer at the next ethereum block,
|
|
96
|
+
* which can be the next slot. If this is the case, then it will send proposals early.
|
|
74
97
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
|
|
99
|
+
currentSlot: SlotNumber;
|
|
100
|
+
nextSlot: SlotNumber;
|
|
101
|
+
currentProposer: EthAddress | undefined;
|
|
102
|
+
nextProposer: EthAddress | undefined;
|
|
80
103
|
}>;
|
|
81
104
|
/**
|
|
82
|
-
*
|
|
105
|
+
* Get the proposer attester address in the given L2 slot
|
|
106
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
107
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
108
|
+
*/
|
|
109
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
110
|
+
/**
|
|
111
|
+
* Get the proposer attester address in the next slot
|
|
112
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
113
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
83
114
|
*/
|
|
84
|
-
|
|
115
|
+
getProposerAttesterAddressInNextSlot(): Promise<EthAddress | undefined>;
|
|
116
|
+
private getProposerAttesterAddressAt;
|
|
117
|
+
getProposerFromEpochCommittee(epochCommitteeInfo: EpochCommitteeInfo, slot: SlotNumber): EthAddress | undefined;
|
|
118
|
+
/** Check if a validator is in the given slot's committee */
|
|
119
|
+
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
120
|
+
/** From the set of given addresses, return all that are on the committee for the given slot */
|
|
121
|
+
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
122
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
85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GO0lBRUQ7Ozs7T0FJRztJQUNJLG9DQUFvQyxJQUFJLE9BQU8sQ0FBQyxVQUFVLEdBQUcsU0FBUyxDQUFDLENBRzdFO1lBUWEsNEJBQTRCO0lBYW5DLDZCQUE2QixDQUNsQyxrQkFBa0IsRUFBRSxrQkFBa0IsRUFDdEMsSUFBSSxFQUFFLFVBQVUsR0FDZixVQUFVLEdBQUcsU0FBUyxDQVl4QjtJQUVELDREQUE0RDtJQUN0RCxhQUFhLENBQUMsSUFBSSxFQUFFLE9BQU8sRUFBRSxTQUFTLEVBQUUsVUFBVSxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FNMUU7SUFFRCwrRkFBK0Y7SUFDekYsaUJBQWlCLENBQUMsSUFBSSxFQUFFLE9BQU8sRUFBRSxVQUFVLEVBQUUsVUFBVSxFQUFFLEdBQUcsT0FBTyxDQUFDLFVBQVUsRUFBRSxDQUFDLENBT3RGO0lBRUssdUJBQXVCLElBQUksT0FBTyxDQUFDLFVBQVUsRUFBRSxDQUFDLENBU3JEO0NBQ0YifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"epoch_cache.d.ts","sourceRoot":"","sources":["../src/epoch_cache.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"epoch_cache.d.ts","sourceRoot":"","sources":["../src/epoch_cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAE3D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EACL,KAAK,iBAAiB,EAOvB,MAAM,6BAA6B,CAAC;AAIrC,OAAO,EAAE,KAAK,gBAAgB,EAA8B,MAAM,aAAa,CAAC;AAEhF,MAAM,MAAM,YAAY,GAAG;IACzB,KA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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 now = this.nowInSeconds();
|
|
86
|
+
return {
|
|
87
|
+
...this.getEpochAndSlotAtTimestamp(now),
|
|
88
|
+
now
|
|
57
89
|
};
|
|
58
|
-
return new EpochCache(rollup, initialValidators.map((v)=>EthAddress.fromString(v)), sampleSeed, l1RollupConstants, deps.dateProvider);
|
|
59
90
|
}
|
|
60
91
|
nowInSeconds() {
|
|
61
92
|
return BigInt(Math.floor(this.dateProvider.now() / 1000));
|
|
62
93
|
}
|
|
63
|
-
|
|
64
|
-
|
|
94
|
+
getEpochAndSlotAtSlot(slot) {
|
|
95
|
+
const epoch = getEpochAtSlot(slot, this.l1constants);
|
|
96
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
|
|
97
|
+
return {
|
|
98
|
+
epoch,
|
|
99
|
+
ts,
|
|
100
|
+
slot
|
|
101
|
+
};
|
|
65
102
|
}
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
103
|
+
getEpochAndSlotInNextL1Slot() {
|
|
104
|
+
const now = this.nowInSeconds();
|
|
105
|
+
const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
|
|
106
|
+
return {
|
|
107
|
+
...this.getEpochAndSlotAtTimestamp(nextSlotTs),
|
|
108
|
+
now
|
|
109
|
+
};
|
|
69
110
|
}
|
|
70
111
|
getEpochAndSlotAtTimestamp(ts) {
|
|
112
|
+
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
71
113
|
return {
|
|
72
114
|
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
73
|
-
|
|
74
|
-
|
|
115
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
116
|
+
slot
|
|
75
117
|
};
|
|
76
118
|
}
|
|
119
|
+
getCommitteeForEpoch(epoch) {
|
|
120
|
+
const [startSlot] = getSlotRangeForEpoch(epoch, this.l1constants);
|
|
121
|
+
return this.getCommittee(startSlot);
|
|
122
|
+
}
|
|
77
123
|
/**
|
|
78
124
|
* Get the current validator set
|
|
79
|
-
*
|
|
80
125
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
81
126
|
* @returns The current validator set.
|
|
82
|
-
*/ async getCommittee(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
this.committee = committeeAtTs.map((v)=>EthAddress.fromString(v));
|
|
95
|
-
this.cachedEpoch = calculatedEpoch;
|
|
96
|
-
this.cachedSampleSeed = sampleSeedAtTs;
|
|
97
|
-
this.log.debug(`Updated validator set for epoch ${calculatedEpoch}`, {
|
|
98
|
-
commitee: this.committee
|
|
99
|
-
});
|
|
100
|
-
this.emit('committeeChanged', this.committee, calculatedEpoch);
|
|
127
|
+
*/ async getCommittee(slot = 'now') {
|
|
128
|
+
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
129
|
+
if (this.cache.has(epoch)) {
|
|
130
|
+
return this.cache.get(epoch);
|
|
131
|
+
}
|
|
132
|
+
const epochData = await this.computeCommittee({
|
|
133
|
+
epoch,
|
|
134
|
+
ts
|
|
135
|
+
});
|
|
136
|
+
// If the committee size is 0 or undefined, then do not cache
|
|
137
|
+
if (!epochData.committee || epochData.committee.length === 0) {
|
|
138
|
+
return epochData;
|
|
101
139
|
}
|
|
102
|
-
|
|
140
|
+
this.cache.set(epoch, epochData);
|
|
141
|
+
const toPurge = Array.from(this.cache.keys()).sort((a, b)=>Number(b - a)).slice(this.config.cacheSize);
|
|
142
|
+
toPurge.forEach((key)=>this.cache.delete(key));
|
|
143
|
+
return epochData;
|
|
144
|
+
}
|
|
145
|
+
getEpochAndTimestamp(slot = 'now') {
|
|
146
|
+
if (slot === 'now') {
|
|
147
|
+
return this.getEpochAndSlotNow();
|
|
148
|
+
} else if (slot === 'next') {
|
|
149
|
+
return this.getEpochAndSlotInNextL1Slot();
|
|
150
|
+
} else {
|
|
151
|
+
return this.getEpochAndSlotAtSlot(slot);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async computeCommittee(when) {
|
|
155
|
+
const { ts, epoch } = when;
|
|
156
|
+
const [committee, seedBuffer, l1Timestamp] = await Promise.all([
|
|
157
|
+
this.rollup.getCommitteeAt(ts),
|
|
158
|
+
this.rollup.getSampleSeedAt(ts),
|
|
159
|
+
this.rollup.client.getBlock({
|
|
160
|
+
includeTransactions: false
|
|
161
|
+
}).then((b)=>b.timestamp)
|
|
162
|
+
]);
|
|
163
|
+
const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
|
|
164
|
+
const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
165
|
+
if (ts - sub > l1Timestamp) {
|
|
166
|
+
throw new Error(`Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`);
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
committee,
|
|
170
|
+
seed: seedBuffer.toBigInt(),
|
|
171
|
+
epoch
|
|
172
|
+
};
|
|
103
173
|
}
|
|
104
174
|
/**
|
|
105
175
|
* Get the ABI encoding of the proposer index - see ValidatorSelectionLib.sol computeProposerIndex
|
|
@@ -118,47 +188,95 @@ import { getEpochCacheConfigEnvVars } from './config.js';
|
|
|
118
188
|
name: 'seed'
|
|
119
189
|
}
|
|
120
190
|
], [
|
|
121
|
-
epoch,
|
|
122
|
-
slot,
|
|
191
|
+
BigInt(epoch),
|
|
192
|
+
BigInt(slot),
|
|
123
193
|
seed
|
|
124
194
|
]);
|
|
125
195
|
}
|
|
126
196
|
computeProposerIndex(slot, epoch, seed, size) {
|
|
197
|
+
// if committe size is 0, then mod 1 is 0
|
|
198
|
+
if (size === 0n) {
|
|
199
|
+
return 0n;
|
|
200
|
+
}
|
|
127
201
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
128
202
|
}
|
|
129
203
|
/**
|
|
130
|
-
* Returns the current and next proposer
|
|
204
|
+
* Returns the current and next proposer's attester address
|
|
131
205
|
*
|
|
132
|
-
* We return the next proposer as the node will check if it is the proposer at the next ethereum block,
|
|
133
|
-
* can be the next slot. If this is the case, then it will send proposals early.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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)];
|
|
206
|
+
* We return the next proposer's attester address as the node will check if it is the proposer at the next ethereum block,
|
|
207
|
+
* which can be the next slot. If this is the case, then it will send proposals early.
|
|
208
|
+
*/ async getProposerAttesterAddressInCurrentOrNextSlot() {
|
|
209
|
+
const current = this.getEpochAndSlotNow();
|
|
210
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
151
211
|
return {
|
|
152
|
-
currentProposer,
|
|
153
|
-
nextProposer,
|
|
154
|
-
currentSlot,
|
|
155
|
-
nextSlot
|
|
212
|
+
currentProposer: await this.getProposerAttesterAddressAt(current),
|
|
213
|
+
nextProposer: await this.getProposerAttesterAddressAt(next),
|
|
214
|
+
currentSlot: current.slot,
|
|
215
|
+
nextSlot: next.slot
|
|
156
216
|
};
|
|
157
217
|
}
|
|
158
218
|
/**
|
|
159
|
-
*
|
|
160
|
-
|
|
161
|
-
|
|
219
|
+
* Get the proposer attester address in the given L2 slot
|
|
220
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
221
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
222
|
+
*/ getProposerAttesterAddressInSlot(slot) {
|
|
223
|
+
const epochAndSlot = this.getEpochAndSlotAtSlot(slot);
|
|
224
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Get the proposer attester address in the next slot
|
|
228
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
229
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
230
|
+
*/ getProposerAttesterAddressInNextSlot() {
|
|
231
|
+
const epochAndSlot = this.getEpochAndSlotInNextL1Slot();
|
|
232
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Get the proposer attester address at a given epoch and slot
|
|
236
|
+
* @param when - The epoch and slot to get the proposer attester address at
|
|
237
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
238
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
239
|
+
*/ async getProposerAttesterAddressAt(when) {
|
|
240
|
+
const { epoch, slot } = when;
|
|
241
|
+
const { committee, seed } = await this.getCommittee(slot);
|
|
242
|
+
if (!committee) {
|
|
243
|
+
throw new NoCommitteeError();
|
|
244
|
+
} else if (committee.length === 0) {
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
const proposerIndex = this.computeProposerIndex(slot, epoch, seed, BigInt(committee.length));
|
|
248
|
+
return committee[Number(proposerIndex)];
|
|
249
|
+
}
|
|
250
|
+
getProposerFromEpochCommittee(epochCommitteeInfo, slot) {
|
|
251
|
+
if (!epochCommitteeInfo.committee || epochCommitteeInfo.committee.length === 0) {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
const proposerIndex = this.computeProposerIndex(slot, epochCommitteeInfo.epoch, epochCommitteeInfo.seed, BigInt(epochCommitteeInfo.committee.length));
|
|
255
|
+
return epochCommitteeInfo.committee[Number(proposerIndex)];
|
|
256
|
+
}
|
|
257
|
+
/** Check if a validator is in the given slot's committee */ async isInCommittee(slot, validator) {
|
|
258
|
+
const { committee } = await this.getCommittee(slot);
|
|
259
|
+
if (!committee) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
162
262
|
return committee.some((v)=>v.equals(validator));
|
|
163
263
|
}
|
|
264
|
+
/** From the set of given addresses, return all that are on the committee for the given slot */ async filterInCommittee(slot, validators) {
|
|
265
|
+
const { committee } = await this.getCommittee(slot);
|
|
266
|
+
if (!committee) {
|
|
267
|
+
return [];
|
|
268
|
+
}
|
|
269
|
+
const committeeSet = new Set(committee.map((v)=>v.toString()));
|
|
270
|
+
return validators.filter((v)=>committeeSet.has(v.toString()));
|
|
271
|
+
}
|
|
272
|
+
async getRegisteredValidators() {
|
|
273
|
+
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
274
|
+
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
275
|
+
if (validatorRefreshTime < this.dateProvider.now()) {
|
|
276
|
+
const currentSet = await this.rollup.getAttesters();
|
|
277
|
+
this.allValidators = new Set(currentSet.map((v)=>v.toString()));
|
|
278
|
+
this.lastValidatorRefresh = this.dateProvider.now();
|
|
279
|
+
}
|
|
280
|
+
return Array.from(this.allValidators.keys()).map((v)=>EthAddress.fromString(v));
|
|
281
|
+
}
|
|
164
282
|
}
|
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.1142ef1",
|
|
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 \"tsgo -b -w\" \"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.1142ef1",
|
|
30
|
+
"@aztec/foundation": "0.0.1-commit.1142ef1",
|
|
31
|
+
"@aztec/l1-artifacts": "0.0.1-commit.1142ef1",
|
|
32
|
+
"@aztec/stdlib": "0.0.1-commit.1142ef1",
|
|
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.20251126.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,359 @@
|
|
|
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
|
+
};
|
|
32
|
+
|
|
33
|
+
export type SlotTag = 'now' | 'next' | SlotNumber;
|
|
34
|
+
|
|
23
35
|
export interface EpochCacheInterface {
|
|
24
|
-
getCommittee(
|
|
36
|
+
getCommittee(slot: SlotTag | undefined): Promise<EpochCommitteeInfo>;
|
|
25
37
|
getEpochAndSlotNow(): EpochAndSlot;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint };
|
|
39
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}`;
|
|
40
|
+
computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint;
|
|
41
|
+
getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
|
|
42
|
+
currentProposer: EthAddress | undefined;
|
|
43
|
+
nextProposer: EthAddress | undefined;
|
|
44
|
+
currentSlot: SlotNumber;
|
|
45
|
+
nextSlot: SlotNumber;
|
|
33
46
|
}>;
|
|
34
|
-
|
|
47
|
+
getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined>;
|
|
48
|
+
getRegisteredValidators(): Promise<EthAddress[]>;
|
|
49
|
+
isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean>;
|
|
50
|
+
filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]>;
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
/**
|
|
38
54
|
* Epoch cache
|
|
39
55
|
*
|
|
40
56
|
* This class is responsible for managing traffic to the l1 node, by caching the validator set.
|
|
57
|
+
* Keeps the last N epochs in cache.
|
|
41
58
|
* It also provides a method to get the current or next proposer, and to check who is in the current slot.
|
|
42
59
|
*
|
|
43
|
-
* If the epoch changes, then we update the stored validator set.
|
|
44
|
-
*
|
|
45
60
|
* Note: This class is very dependent on the system clock being in sync.
|
|
46
61
|
*/
|
|
47
|
-
export class EpochCache
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
private
|
|
52
|
-
private cachedEpoch: bigint;
|
|
53
|
-
private cachedSampleSeed: bigint;
|
|
62
|
+
export class EpochCache implements EpochCacheInterface {
|
|
63
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
64
|
+
protected cache: Map<EpochNumber, EpochCommitteeInfo> = new Map();
|
|
65
|
+
private allValidators: Set<string> = new Set();
|
|
66
|
+
private lastValidatorRefresh = 0;
|
|
54
67
|
private readonly log: Logger = createLogger('epoch-cache');
|
|
55
68
|
|
|
56
69
|
constructor(
|
|
57
70
|
private rollup: RollupContract,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
private readonly l1constants: L1RollupConstants & {
|
|
72
|
+
lagInEpochsForValidatorSet: number;
|
|
73
|
+
lagInEpochsForRandao: number;
|
|
74
|
+
},
|
|
61
75
|
private readonly dateProvider: DateProvider = new DateProvider(),
|
|
76
|
+
protected readonly config = { cacheSize: 12, validatorRefreshIntervalSeconds: 60 },
|
|
62
77
|
) {
|
|
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);
|
|
78
|
+
this.log.debug(`Initialized EpochCache`, {
|
|
79
|
+
l1constants,
|
|
80
|
+
});
|
|
70
81
|
}
|
|
71
82
|
|
|
72
83
|
static async create(
|
|
73
|
-
|
|
84
|
+
rollupOrAddress: EthAddress | RollupContract,
|
|
74
85
|
config?: EpochCacheConfig,
|
|
75
86
|
deps: { dateProvider?: DateProvider } = {},
|
|
76
87
|
) {
|
|
77
88
|
config = config ?? getEpochCacheConfigEnvVars();
|
|
78
89
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
// Load the rollup contract if we were given an address
|
|
91
|
+
let rollup: RollupContract;
|
|
92
|
+
if ('address' in rollupOrAddress) {
|
|
93
|
+
rollup = rollupOrAddress;
|
|
94
|
+
} else {
|
|
95
|
+
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
96
|
+
const publicClient = createPublicClient({
|
|
97
|
+
chain: chain.chainInfo,
|
|
98
|
+
transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
|
|
99
|
+
pollingInterval: config.viemPollingIntervalMS,
|
|
100
|
+
});
|
|
101
|
+
rollup = new RollupContract(publicClient, rollupOrAddress.toString());
|
|
102
|
+
}
|
|
85
103
|
|
|
86
|
-
const
|
|
87
|
-
|
|
104
|
+
const [
|
|
105
|
+
l1StartBlock,
|
|
106
|
+
l1GenesisTime,
|
|
107
|
+
proofSubmissionEpochs,
|
|
108
|
+
slotDuration,
|
|
109
|
+
epochDuration,
|
|
110
|
+
lagInEpochsForValidatorSet,
|
|
111
|
+
lagInEpochsForRandao,
|
|
112
|
+
] = await Promise.all([
|
|
88
113
|
rollup.getL1StartBlock(),
|
|
89
114
|
rollup.getL1GenesisTime(),
|
|
90
|
-
rollup.
|
|
91
|
-
rollup.
|
|
115
|
+
rollup.getProofSubmissionEpochs(),
|
|
116
|
+
rollup.getSlotDuration(),
|
|
117
|
+
rollup.getEpochDuration(),
|
|
118
|
+
rollup.getLagInEpochsForValidatorSet(),
|
|
119
|
+
rollup.getLagInEpochsForRandao(),
|
|
92
120
|
] as const);
|
|
93
121
|
|
|
94
|
-
const l1RollupConstants
|
|
122
|
+
const l1RollupConstants = {
|
|
95
123
|
l1StartBlock,
|
|
96
124
|
l1GenesisTime,
|
|
97
|
-
|
|
98
|
-
|
|
125
|
+
proofSubmissionEpochs: Number(proofSubmissionEpochs),
|
|
126
|
+
slotDuration: Number(slotDuration),
|
|
127
|
+
epochDuration: Number(epochDuration),
|
|
99
128
|
ethereumSlotDuration: config.ethereumSlotDuration,
|
|
129
|
+
lagInEpochsForValidatorSet: Number(lagInEpochsForValidatorSet),
|
|
130
|
+
lagInEpochsForRandao: Number(lagInEpochsForRandao),
|
|
100
131
|
};
|
|
101
132
|
|
|
102
|
-
return new EpochCache(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
133
|
+
return new EpochCache(rollup, l1RollupConstants, deps.dateProvider);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public getL1Constants(): L1RollupConstants {
|
|
137
|
+
return this.l1constants;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public getEpochAndSlotNow(): EpochAndSlot & { now: bigint } {
|
|
141
|
+
const now = this.nowInSeconds();
|
|
142
|
+
return { ...this.getEpochAndSlotAtTimestamp(now), now };
|
|
109
143
|
}
|
|
110
144
|
|
|
111
|
-
|
|
145
|
+
public nowInSeconds(): bigint {
|
|
112
146
|
return BigInt(Math.floor(this.dateProvider.now() / 1000));
|
|
113
147
|
}
|
|
114
148
|
|
|
115
|
-
|
|
116
|
-
|
|
149
|
+
private getEpochAndSlotAtSlot(slot: SlotNumber): EpochAndSlot {
|
|
150
|
+
const epoch = getEpochAtSlot(slot, this.l1constants);
|
|
151
|
+
const ts = getTimestampRangeForEpoch(epoch, this.l1constants)[0];
|
|
152
|
+
return { epoch, ts, slot };
|
|
117
153
|
}
|
|
118
154
|
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
155
|
+
public getEpochAndSlotInNextL1Slot(): EpochAndSlot & { now: bigint } {
|
|
156
|
+
const now = this.nowInSeconds();
|
|
157
|
+
const nextSlotTs = now + BigInt(this.l1constants.ethereumSlotDuration);
|
|
158
|
+
return { ...this.getEpochAndSlotAtTimestamp(nextSlotTs), now };
|
|
122
159
|
}
|
|
123
160
|
|
|
124
161
|
private getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot {
|
|
162
|
+
const slot = getSlotAtTimestamp(ts, this.l1constants);
|
|
125
163
|
return {
|
|
126
164
|
epoch: getEpochNumberAtTimestamp(ts, this.l1constants),
|
|
127
|
-
|
|
128
|
-
|
|
165
|
+
ts: getTimestampForSlot(slot, this.l1constants),
|
|
166
|
+
slot,
|
|
129
167
|
};
|
|
130
168
|
}
|
|
131
169
|
|
|
170
|
+
public getCommitteeForEpoch(epoch: EpochNumber): Promise<EpochCommitteeInfo> {
|
|
171
|
+
const [startSlot] = getSlotRangeForEpoch(epoch, this.l1constants);
|
|
172
|
+
return this.getCommittee(startSlot);
|
|
173
|
+
}
|
|
174
|
+
|
|
132
175
|
/**
|
|
133
176
|
* Get the current validator set
|
|
134
|
-
*
|
|
135
177
|
* @param nextSlot - If true, get the validator set for the next slot.
|
|
136
178
|
* @returns The current validator set.
|
|
137
179
|
*/
|
|
138
|
-
async getCommittee(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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);
|
|
180
|
+
public async getCommittee(slot: SlotTag = 'now'): Promise<EpochCommitteeInfo> {
|
|
181
|
+
const { epoch, ts } = this.getEpochAndTimestamp(slot);
|
|
182
|
+
|
|
183
|
+
if (this.cache.has(epoch)) {
|
|
184
|
+
return this.cache.get(epoch)!;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const epochData = await this.computeCommittee({ epoch, ts });
|
|
188
|
+
// If the committee size is 0 or undefined, then do not cache
|
|
189
|
+
if (!epochData.committee || epochData.committee.length === 0) {
|
|
190
|
+
return epochData;
|
|
156
191
|
}
|
|
192
|
+
this.cache.set(epoch, epochData);
|
|
157
193
|
|
|
158
|
-
|
|
194
|
+
const toPurge = Array.from(this.cache.keys())
|
|
195
|
+
.sort((a, b) => Number(b - a))
|
|
196
|
+
.slice(this.config.cacheSize);
|
|
197
|
+
toPurge.forEach(key => this.cache.delete(key));
|
|
198
|
+
|
|
199
|
+
return epochData;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private getEpochAndTimestamp(slot: SlotTag = 'now') {
|
|
203
|
+
if (slot === 'now') {
|
|
204
|
+
return this.getEpochAndSlotNow();
|
|
205
|
+
} else if (slot === 'next') {
|
|
206
|
+
return this.getEpochAndSlotInNextL1Slot();
|
|
207
|
+
} else {
|
|
208
|
+
return this.getEpochAndSlotAtSlot(slot);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async computeCommittee(when: { epoch: EpochNumber; ts: bigint }): Promise<EpochCommitteeInfo> {
|
|
213
|
+
const { ts, epoch } = when;
|
|
214
|
+
const [committee, seedBuffer, l1Timestamp] = await Promise.all([
|
|
215
|
+
this.rollup.getCommitteeAt(ts),
|
|
216
|
+
this.rollup.getSampleSeedAt(ts),
|
|
217
|
+
this.rollup.client.getBlock({ includeTransactions: false }).then(b => b.timestamp),
|
|
218
|
+
]);
|
|
219
|
+
const { lagInEpochsForValidatorSet, epochDuration, slotDuration } = this.l1constants;
|
|
220
|
+
const sub = BigInt(lagInEpochsForValidatorSet) * BigInt(epochDuration) * BigInt(slotDuration);
|
|
221
|
+
if (ts - sub > l1Timestamp) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
`Cannot query committee for future epoch ${epoch} with timestamp ${ts} (current L1 time is ${l1Timestamp}). Check your Ethereum node is synced.`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
return { committee, seed: seedBuffer.toBigInt(), epoch };
|
|
159
227
|
}
|
|
160
228
|
|
|
161
229
|
/**
|
|
162
230
|
* Get the ABI encoding of the proposer index - see ValidatorSelectionLib.sol computeProposerIndex
|
|
163
231
|
*/
|
|
164
|
-
getProposerIndexEncoding(epoch:
|
|
232
|
+
getProposerIndexEncoding(epoch: EpochNumber, slot: SlotNumber, seed: bigint): `0x${string}` {
|
|
165
233
|
return encodeAbiParameters(
|
|
166
234
|
[
|
|
167
235
|
{ type: 'uint256', name: 'epoch' },
|
|
168
236
|
{ type: 'uint256', name: 'slot' },
|
|
169
237
|
{ type: 'uint256', name: 'seed' },
|
|
170
238
|
],
|
|
171
|
-
[epoch, slot, seed],
|
|
239
|
+
[BigInt(epoch), BigInt(slot), seed],
|
|
172
240
|
);
|
|
173
241
|
}
|
|
174
242
|
|
|
175
|
-
computeProposerIndex(slot:
|
|
243
|
+
public computeProposerIndex(slot: SlotNumber, epoch: EpochNumber, seed: bigint, size: bigint): bigint {
|
|
244
|
+
// if committe size is 0, then mod 1 is 0
|
|
245
|
+
if (size === 0n) {
|
|
246
|
+
return 0n;
|
|
247
|
+
}
|
|
176
248
|
return BigInt(keccak256(this.getProposerIndexEncoding(epoch, slot, seed))) % size;
|
|
177
249
|
}
|
|
178
250
|
|
|
179
251
|
/**
|
|
180
|
-
* Returns the current and next proposer
|
|
252
|
+
* Returns the current and next proposer's attester address
|
|
181
253
|
*
|
|
182
|
-
* We return the next proposer as the node will check if it is the proposer at the next ethereum block,
|
|
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.
|
|
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.
|
|
187
256
|
*/
|
|
188
|
-
async
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
257
|
+
public async getProposerAttesterAddressInCurrentOrNextSlot(): Promise<{
|
|
258
|
+
currentSlot: SlotNumber;
|
|
259
|
+
nextSlot: SlotNumber;
|
|
260
|
+
currentProposer: EthAddress | undefined;
|
|
261
|
+
nextProposer: EthAddress | undefined;
|
|
193
262
|
}> {
|
|
194
|
-
|
|
195
|
-
const
|
|
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
|
-
);
|
|
263
|
+
const current = this.getEpochAndSlotNow();
|
|
264
|
+
const next = this.getEpochAndSlotInNextL1Slot();
|
|
206
265
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
await this.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
this.cachedSampleSeed,
|
|
215
|
-
BigInt(committee.length),
|
|
216
|
-
);
|
|
266
|
+
return {
|
|
267
|
+
currentProposer: await this.getProposerAttesterAddressAt(current),
|
|
268
|
+
nextProposer: await this.getProposerAttesterAddressAt(next),
|
|
269
|
+
currentSlot: current.slot,
|
|
270
|
+
nextSlot: next.slot,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
217
273
|
|
|
218
|
-
|
|
219
|
-
|
|
274
|
+
/**
|
|
275
|
+
* Get the proposer attester address in the given L2 slot
|
|
276
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
277
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
278
|
+
*/
|
|
279
|
+
public getProposerAttesterAddressInSlot(slot: SlotNumber): Promise<EthAddress | undefined> {
|
|
280
|
+
const epochAndSlot = this.getEpochAndSlotAtSlot(slot);
|
|
281
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
282
|
+
}
|
|
220
283
|
|
|
221
|
-
|
|
284
|
+
/**
|
|
285
|
+
* Get the proposer attester address in the next slot
|
|
286
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
287
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
288
|
+
*/
|
|
289
|
+
public getProposerAttesterAddressInNextSlot(): Promise<EthAddress | undefined> {
|
|
290
|
+
const epochAndSlot = this.getEpochAndSlotInNextL1Slot();
|
|
291
|
+
return this.getProposerAttesterAddressAt(epochAndSlot);
|
|
222
292
|
}
|
|
223
293
|
|
|
224
294
|
/**
|
|
225
|
-
*
|
|
295
|
+
* Get the proposer attester address at a given epoch and slot
|
|
296
|
+
* @param when - The epoch and slot to get the proposer attester address at
|
|
297
|
+
* @returns The proposer attester address. If the committee does not exist, we throw a NoCommitteeError.
|
|
298
|
+
* If the committee is empty (i.e. target committee size is 0, and anyone can propose), we return undefined.
|
|
226
299
|
*/
|
|
227
|
-
async
|
|
228
|
-
const
|
|
300
|
+
private async getProposerAttesterAddressAt(when: EpochAndSlot) {
|
|
301
|
+
const { epoch, slot } = when;
|
|
302
|
+
const { committee, seed } = await this.getCommittee(slot);
|
|
303
|
+
if (!committee) {
|
|
304
|
+
throw new NoCommitteeError();
|
|
305
|
+
} else if (committee.length === 0) {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const proposerIndex = this.computeProposerIndex(slot, epoch, seed, BigInt(committee.length));
|
|
310
|
+
return committee[Number(proposerIndex)];
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
public getProposerFromEpochCommittee(
|
|
314
|
+
epochCommitteeInfo: EpochCommitteeInfo,
|
|
315
|
+
slot: SlotNumber,
|
|
316
|
+
): EthAddress | undefined {
|
|
317
|
+
if (!epochCommitteeInfo.committee || epochCommitteeInfo.committee.length === 0) {
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
const proposerIndex = this.computeProposerIndex(
|
|
321
|
+
slot,
|
|
322
|
+
epochCommitteeInfo.epoch,
|
|
323
|
+
epochCommitteeInfo.seed,
|
|
324
|
+
BigInt(epochCommitteeInfo.committee.length),
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
return epochCommitteeInfo.committee[Number(proposerIndex)];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Check if a validator is in the given slot's committee */
|
|
331
|
+
async isInCommittee(slot: SlotTag, validator: EthAddress): Promise<boolean> {
|
|
332
|
+
const { committee } = await this.getCommittee(slot);
|
|
333
|
+
if (!committee) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
229
336
|
return committee.some(v => v.equals(validator));
|
|
230
337
|
}
|
|
338
|
+
|
|
339
|
+
/** From the set of given addresses, return all that are on the committee for the given slot */
|
|
340
|
+
async filterInCommittee(slot: SlotTag, validators: EthAddress[]): Promise<EthAddress[]> {
|
|
341
|
+
const { committee } = await this.getCommittee(slot);
|
|
342
|
+
if (!committee) {
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
345
|
+
const committeeSet = new Set(committee.map(v => v.toString()));
|
|
346
|
+
return validators.filter(v => committeeSet.has(v.toString()));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async getRegisteredValidators(): Promise<EthAddress[]> {
|
|
350
|
+
const validatorRefreshIntervalMs = this.config.validatorRefreshIntervalSeconds * 1000;
|
|
351
|
+
const validatorRefreshTime = this.lastValidatorRefresh + validatorRefreshIntervalMs;
|
|
352
|
+
if (validatorRefreshTime < this.dateProvider.now()) {
|
|
353
|
+
const currentSet = await this.rollup.getAttesters();
|
|
354
|
+
this.allValidators = new Set(currentSet.map(v => v.toString()));
|
|
355
|
+
this.lastValidatorRefresh = this.dateProvider.now();
|
|
356
|
+
}
|
|
357
|
+
return Array.from(this.allValidators.keys()).map(v => EthAddress.fromString(v));
|
|
358
|
+
}
|
|
231
359
|
}
|
|
@@ -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
|