@aztec/aztec-node 0.82.2-alpha-testnet.4 → 0.82.3

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.
@@ -0,0 +1,246 @@
1
+ import { countWhile } from '@aztec/foundation/collection';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { RunningPromise } from '@aztec/foundation/running-promise';
5
+ import { L2TipsMemoryStore } from '@aztec/kv-store/stores';
6
+ import { L2BlockStream } from '@aztec/stdlib/block';
7
+ import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
8
+ export class Sentinel {
9
+ epochCache;
10
+ archiver;
11
+ p2p;
12
+ store;
13
+ logger;
14
+ runningPromise;
15
+ blockStream;
16
+ l2TipsStore;
17
+ initialSlot;
18
+ lastProcessedSlot;
19
+ slotNumberToArchive;
20
+ constructor(epochCache, archiver, p2p, store, logger = createLogger('node:sentinel')){
21
+ this.epochCache = epochCache;
22
+ this.archiver = archiver;
23
+ this.p2p = p2p;
24
+ this.store = store;
25
+ this.logger = logger;
26
+ this.slotNumberToArchive = new Map();
27
+ this.l2TipsStore = new L2TipsMemoryStore();
28
+ const interval = epochCache.getL1Constants().ethereumSlotDuration * 1000 / 4;
29
+ this.runningPromise = new RunningPromise(this.work.bind(this), logger, interval);
30
+ }
31
+ async start() {
32
+ await this.init();
33
+ this.runningPromise.start();
34
+ }
35
+ /** Loads initial slot and initializes blockstream. We will not process anything at or before the initial slot. */ async init() {
36
+ this.initialSlot = this.epochCache.getEpochAndSlotNow().slot;
37
+ const startingBlock = await this.archiver.getBlockNumber();
38
+ this.blockStream = new L2BlockStream(this.archiver, this.l2TipsStore, this, this.logger, {
39
+ startingBlock
40
+ });
41
+ }
42
+ stop() {
43
+ return this.runningPromise.stop();
44
+ }
45
+ async handleBlockStreamEvent(event) {
46
+ await this.l2TipsStore.handleBlockStreamEvent(event);
47
+ if (event.type === 'blocks-added') {
48
+ // Store mapping from slot to archive
49
+ for (const block of event.blocks){
50
+ this.slotNumberToArchive.set(block.block.header.getSlot(), block.block.archive.root.toString());
51
+ }
52
+ // Prune the archive map to only keep at most N entries
53
+ const historyLength = this.store.getHistoryLength();
54
+ if (this.slotNumberToArchive.size > historyLength) {
55
+ const toDelete = Array.from(this.slotNumberToArchive.keys()).sort((a, b)=>Number(a - b)).slice(0, this.slotNumberToArchive.size - historyLength);
56
+ for (const key of toDelete){
57
+ this.slotNumberToArchive.delete(key);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ /**
63
+ * Process data for two L2 slots ago.
64
+ * Note that we do not process historical data, since we rely on p2p data for processing,
65
+ * and we don't have that data if we were offline during the period.
66
+ */ async work() {
67
+ const { slot: currentSlot } = this.epochCache.getEpochAndSlotNow();
68
+ try {
69
+ // Manually sync the block stream to ensure we have the latest data.
70
+ // Note we never `start` the blockstream, so it loops at the same pace as we do.
71
+ await this.blockStream.sync();
72
+ // Check if we are ready to process data for two L2 slots ago.
73
+ const targetSlot = await this.isReadyToProcess(currentSlot);
74
+ // And process it if we are.
75
+ if (targetSlot !== false) {
76
+ await this.processSlot(targetSlot);
77
+ }
78
+ } catch (err) {
79
+ this.logger.error(`Failed to process slot ${currentSlot}`, err);
80
+ }
81
+ }
82
+ /**
83
+ * Check if we are ready to process data for two L2 slots ago, so we allow plenty of time for p2p to process all in-flight attestations.
84
+ * We also don't move past the archiver last synced L2 slot, as we don't want to process data that is not yet available.
85
+ * Last, we check the p2p is synced with the archiver, so it has pulled all attestations from it.
86
+ */ async isReadyToProcess(currentSlot) {
87
+ const targetSlot = currentSlot - 2n;
88
+ if (this.lastProcessedSlot && this.lastProcessedSlot >= targetSlot) {
89
+ this.logger.trace(`Already processed slot ${targetSlot}`, {
90
+ lastProcessedSlot: this.lastProcessedSlot
91
+ });
92
+ return false;
93
+ }
94
+ if (this.initialSlot === undefined) {
95
+ this.logger.error(`Initial slot not loaded.`);
96
+ return false;
97
+ }
98
+ if (targetSlot <= this.initialSlot) {
99
+ this.logger.debug(`Refusing to process slot ${targetSlot} given initial slot ${this.initialSlot}`);
100
+ return false;
101
+ }
102
+ const archiverSlot = await this.archiver.getL2SlotNumber();
103
+ if (archiverSlot < targetSlot) {
104
+ this.logger.debug(`Waiting for archiver to sync with L2 slot ${targetSlot}`, {
105
+ archiverSlot,
106
+ targetSlot
107
+ });
108
+ return false;
109
+ }
110
+ const archiverLastBlockHash = await this.l2TipsStore.getL2Tips().then((tip)=>tip.latest.hash);
111
+ const p2pLastBlockHash = await this.p2p.getL2Tips().then((tips)=>tips.latest.hash);
112
+ const isP2pSynced = archiverLastBlockHash === p2pLastBlockHash;
113
+ if (!isP2pSynced) {
114
+ this.logger.debug(`Waiting for P2P client to sync with archiver`, {
115
+ archiverLastBlockHash,
116
+ p2pLastBlockHash
117
+ });
118
+ return false;
119
+ }
120
+ return targetSlot;
121
+ }
122
+ /**
123
+ * Gathers committee and proposer data for a given slot, computes slot stats,
124
+ * and updates overall stats.
125
+ */ async processSlot(slot) {
126
+ const { epoch, seed, committee } = await this.epochCache.getCommittee(slot);
127
+ if (committee.length === 0) {
128
+ this.logger.warn(`No committee found for slot ${slot} at epoch ${epoch}`);
129
+ this.lastProcessedSlot = slot;
130
+ return;
131
+ }
132
+ const proposerIndex = this.epochCache.computeProposerIndex(slot, epoch, seed, BigInt(committee.length));
133
+ const proposer = committee[Number(proposerIndex)];
134
+ const stats = await this.getSlotActivity(slot, epoch, proposer, committee);
135
+ this.logger.verbose(`Updating L2 slot ${slot} observed activity`, stats);
136
+ await this.updateValidators(slot, stats);
137
+ this.lastProcessedSlot = slot;
138
+ }
139
+ /** Computes activity for a given slot. */ async getSlotActivity(slot, epoch, proposer, committee) {
140
+ this.logger.debug(`Computing stats for slot ${slot} at epoch ${epoch}`, {
141
+ slot,
142
+ epoch,
143
+ proposer,
144
+ committee
145
+ });
146
+ // Check if there is an L2 block in L1 for this L2 slot
147
+ // Here we get all attestations for the block mined at the given slot,
148
+ // or all attestations for all proposals in the slot if no block was mined.
149
+ const archive = this.slotNumberToArchive.get(slot);
150
+ const attested = await this.p2p.getAttestationsForSlot(slot, archive);
151
+ const attestors = new Set(await Promise.all(attested.map((a)=>a.getSender().then((a)=>a.toString()))));
152
+ // We assume that there was a block proposal if at least one of the validators attested to it.
153
+ // It could be the case that every single validator failed, and we could differentiate it by having
154
+ // this node re-execute every block proposal it sees and storing it in the attestation pool.
155
+ // But we'll leave that corner case out to reduce pressure on the node.
156
+ const blockStatus = archive ? 'mined' : attestors.size > 0 ? 'proposed' : 'missed';
157
+ this.logger.debug(`Block for slot ${slot} was ${blockStatus}`, {
158
+ archive,
159
+ slot
160
+ });
161
+ // Get attestors that failed their duties for this block, but only if there was a block proposed
162
+ const missedAttestors = new Set(blockStatus === 'missed' ? [] : committee.filter((v)=>!attestors.has(v.toString()) && !proposer.equals(v)).map((v)=>v.toString()));
163
+ this.logger.debug(`Retrieved ${attestors.size} attestors out of ${committee.length} for slot ${slot}`, {
164
+ blockStatus,
165
+ proposer: proposer.toString(),
166
+ archive,
167
+ slot,
168
+ attestors: [
169
+ ...attestors
170
+ ],
171
+ missedAttestors: [
172
+ ...missedAttestors
173
+ ],
174
+ committee: committee.map((c)=>c.toString())
175
+ });
176
+ // Compute the status for each validator in the committee
177
+ const statusFor = (who)=>{
178
+ if (who === proposer.toString()) {
179
+ return `block-${blockStatus}`;
180
+ } else if (attestors.has(who)) {
181
+ return 'attestation-sent';
182
+ } else if (missedAttestors.has(who)) {
183
+ return 'attestation-missed';
184
+ } else {
185
+ return undefined;
186
+ }
187
+ };
188
+ return Object.fromEntries(committee.map((v)=>v.toString()).map((who)=>[
189
+ who,
190
+ statusFor(who)
191
+ ]));
192
+ }
193
+ /** Push the status for each slot for each validator. */ updateValidators(slot, stats) {
194
+ return this.store.updateValidators(slot, stats);
195
+ }
196
+ /** Computes stats to be returned based on stored data. */ async computeStats() {
197
+ const histories = await this.store.getHistories();
198
+ const slotNow = this.epochCache.getEpochAndSlotNow().slot;
199
+ const fromSlot = (this.lastProcessedSlot ?? slotNow) - BigInt(this.store.getHistoryLength());
200
+ const result = {};
201
+ for (const [address, history] of Object.entries(histories)){
202
+ const validatorAddress = address;
203
+ result[validatorAddress] = this.computeStatsForValidator(validatorAddress, history, fromSlot);
204
+ }
205
+ return {
206
+ stats: result,
207
+ lastProcessedSlot: this.lastProcessedSlot,
208
+ initialSlot: this.initialSlot,
209
+ slotWindow: this.store.getHistoryLength()
210
+ };
211
+ }
212
+ computeStatsForValidator(address, allHistory, fromSlot) {
213
+ const history = fromSlot ? allHistory.filter((h)=>h.slot >= fromSlot) : allHistory;
214
+ return {
215
+ address: EthAddress.fromString(address),
216
+ lastProposal: this.computeFromSlot(history.filter((h)=>h.status === 'block-proposed' || h.status === 'block-mined').at(-1)?.slot),
217
+ lastAttestation: this.computeFromSlot(history.filter((h)=>h.status === 'attestation-sent').at(-1)?.slot),
218
+ totalSlots: history.length,
219
+ missedProposals: this.computeMissed(history, 'block', 'block-missed'),
220
+ missedAttestations: this.computeMissed(history, 'attestation', 'attestation-missed'),
221
+ history
222
+ };
223
+ }
224
+ computeMissed(history, computeOverPrefix, filter) {
225
+ const relevantHistory = history.filter((h)=>h.status.startsWith(computeOverPrefix));
226
+ const filteredHistory = relevantHistory.filter((h)=>h.status === filter);
227
+ return {
228
+ currentStreak: countWhile([
229
+ ...relevantHistory
230
+ ].reverse(), (h)=>h.status === filter),
231
+ rate: filteredHistory.length / relevantHistory.length,
232
+ count: filteredHistory.length
233
+ };
234
+ }
235
+ computeFromSlot(slot) {
236
+ if (slot === undefined) {
237
+ return undefined;
238
+ }
239
+ const timestamp = getTimestampForSlot(slot, this.epochCache.getL1Constants());
240
+ return {
241
+ timestamp,
242
+ slot,
243
+ date: new Date(Number(timestamp) * 1000).toISOString()
244
+ };
245
+ }
246
+ }
@@ -0,0 +1,21 @@
1
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
2
+ import type { ValidatorStatusHistory, ValidatorStatusInSlot } from '@aztec/stdlib/validators';
3
+ export declare class SentinelStore {
4
+ private store;
5
+ private config;
6
+ static readonly SCHEMA_VERSION = 1;
7
+ private readonly map;
8
+ constructor(store: AztecAsyncKVStore, config: {
9
+ historyLength: number;
10
+ });
11
+ getHistoryLength(): number;
12
+ updateValidators(slot: bigint, statuses: Record<`0x${string}`, ValidatorStatusInSlot | undefined>): Promise<void>;
13
+ private pushValidatorStatusForSlot;
14
+ getHistories(): Promise<Record<`0x${string}`, ValidatorStatusHistory>>;
15
+ private getHistory;
16
+ private serializeHistory;
17
+ private deserializeHistory;
18
+ private statusToNumber;
19
+ private statusFromNumber;
20
+ }
21
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/sentinel/store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAiB,MAAM,iBAAiB,CAAC;AACxE,OAAO,KAAK,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAE9F,qBAAa,aAAa;IAKZ,OAAO,CAAC,KAAK;IAAqB,OAAO,CAAC,MAAM;IAJ5D,gBAAuB,cAAc,KAAK;IAE1C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAuC;gBAEvC,KAAK,EAAE,iBAAiB,EAAU,MAAM,EAAE;QAAE,aAAa,EAAE,MAAM,CAAA;KAAE;IAIhF,gBAAgB;IAIV,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,KAAK,MAAM,EAAE,EAAE,qBAAqB,GAAG,SAAS,CAAC;YAUhG,0BAA0B;IAU3B,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,MAAM,EAAE,EAAE,sBAAsB,CAAC,CAAC;YAQrE,UAAU;IAKxB,OAAO,CAAC,gBAAgB;IAMxB,OAAO,CAAC,kBAAkB;IAW1B,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,gBAAgB;CAgBzB"}
@@ -0,0 +1,100 @@
1
+ import { BufferReader, numToUInt8, numToUInt32BE, serializeToBuffer } from '@aztec/foundation/serialize';
2
+ export class SentinelStore {
3
+ store;
4
+ config;
5
+ static SCHEMA_VERSION = 1;
6
+ map;
7
+ constructor(store, config){
8
+ this.store = store;
9
+ this.config = config;
10
+ this.map = store.openMap('sentinel-validator-status');
11
+ }
12
+ getHistoryLength() {
13
+ return this.config.historyLength;
14
+ }
15
+ async updateValidators(slot, statuses) {
16
+ await this.store.transactionAsync(async ()=>{
17
+ for (const [who, status] of Object.entries(statuses)){
18
+ if (status) {
19
+ await this.pushValidatorStatusForSlot(who, slot, status);
20
+ }
21
+ }
22
+ });
23
+ }
24
+ async pushValidatorStatusForSlot(who, slot, status) {
25
+ const currentHistory = await this.getHistory(who) ?? [];
26
+ const newHistory = [
27
+ ...currentHistory,
28
+ {
29
+ slot,
30
+ status
31
+ }
32
+ ].slice(-this.config.historyLength);
33
+ await this.map.set(who, this.serializeHistory(newHistory));
34
+ }
35
+ async getHistories() {
36
+ const histories = {};
37
+ for await (const [address, history] of this.map.entriesAsync()){
38
+ histories[address] = this.deserializeHistory(history);
39
+ }
40
+ return histories;
41
+ }
42
+ async getHistory(address) {
43
+ const data = await this.map.getAsync(address);
44
+ return data && this.deserializeHistory(data);
45
+ }
46
+ serializeHistory(history) {
47
+ return serializeToBuffer(history.map((h)=>[
48
+ numToUInt32BE(Number(h.slot)),
49
+ numToUInt8(this.statusToNumber(h.status))
50
+ ]));
51
+ }
52
+ deserializeHistory(buffer) {
53
+ const reader = new BufferReader(buffer);
54
+ const history = [];
55
+ while(!reader.isEmpty()){
56
+ const slot = BigInt(reader.readNumber());
57
+ const status = this.statusFromNumber(reader.readUInt8());
58
+ history.push({
59
+ slot,
60
+ status
61
+ });
62
+ }
63
+ return history;
64
+ }
65
+ statusToNumber(status) {
66
+ switch(status){
67
+ case 'block-mined':
68
+ return 1;
69
+ case 'block-proposed':
70
+ return 2;
71
+ case 'block-missed':
72
+ return 3;
73
+ case 'attestation-sent':
74
+ return 4;
75
+ case 'attestation-missed':
76
+ return 5;
77
+ default:
78
+ {
79
+ const _exhaustive = status;
80
+ throw new Error(`Unknown status: ${status}`);
81
+ }
82
+ }
83
+ }
84
+ statusFromNumber(status) {
85
+ switch(status){
86
+ case 1:
87
+ return 'block-mined';
88
+ case 2:
89
+ return 'block-proposed';
90
+ case 3:
91
+ return 'block-missed';
92
+ case 4:
93
+ return 'attestation-sent';
94
+ case 5:
95
+ return 'attestation-missed';
96
+ default:
97
+ throw new Error(`Unknown status: ${status}`);
98
+ }
99
+ }
100
+ }
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@aztec/aztec-node",
3
- "version": "0.82.2-alpha-testnet.4",
3
+ "version": "0.82.3",
4
4
  "main": "dest/index.js",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./dest/index.js",
8
- "./config": "./dest/aztec-node/config.js"
8
+ "./config": "./dest/aztec-node/config.js",
9
+ "./sentinel": "./dest/aztec-node/sentinel.js"
9
10
  },
10
11
  "bin": "./dest/bin/index.js",
11
12
  "typedocOptions": {
@@ -62,24 +63,25 @@
62
63
  ]
63
64
  },
64
65
  "dependencies": {
65
- "@aztec/archiver": "0.82.2-alpha-testnet.4",
66
- "@aztec/bb-prover": "0.82.2-alpha-testnet.4",
67
- "@aztec/blob-sink": "0.82.2-alpha-testnet.4",
68
- "@aztec/constants": "0.82.2-alpha-testnet.4",
69
- "@aztec/epoch-cache": "0.82.2-alpha-testnet.4",
70
- "@aztec/ethereum": "0.82.2-alpha-testnet.4",
71
- "@aztec/foundation": "0.82.2-alpha-testnet.4",
72
- "@aztec/kv-store": "0.82.2-alpha-testnet.4",
73
- "@aztec/merkle-tree": "0.82.2-alpha-testnet.4",
74
- "@aztec/p2p": "0.82.2-alpha-testnet.4",
75
- "@aztec/protocol-contracts": "0.82.2-alpha-testnet.4",
76
- "@aztec/prover-client": "0.82.2-alpha-testnet.4",
77
- "@aztec/sequencer-client": "0.82.2-alpha-testnet.4",
78
- "@aztec/simulator": "0.82.2-alpha-testnet.4",
79
- "@aztec/stdlib": "0.82.2-alpha-testnet.4",
80
- "@aztec/telemetry-client": "0.82.2-alpha-testnet.4",
81
- "@aztec/validator-client": "0.82.2-alpha-testnet.4",
82
- "@aztec/world-state": "0.82.2-alpha-testnet.4",
66
+ "@aztec/archiver": "0.82.3",
67
+ "@aztec/bb-prover": "0.82.3",
68
+ "@aztec/blob-sink": "0.82.3",
69
+ "@aztec/constants": "0.82.3",
70
+ "@aztec/epoch-cache": "0.82.3",
71
+ "@aztec/ethereum": "0.82.3",
72
+ "@aztec/foundation": "0.82.3",
73
+ "@aztec/kv-store": "0.82.3",
74
+ "@aztec/merkle-tree": "0.82.3",
75
+ "@aztec/node-lib": "0.82.3",
76
+ "@aztec/p2p": "0.82.3",
77
+ "@aztec/protocol-contracts": "0.82.3",
78
+ "@aztec/prover-client": "0.82.3",
79
+ "@aztec/sequencer-client": "0.82.3",
80
+ "@aztec/simulator": "0.82.3",
81
+ "@aztec/stdlib": "0.82.3",
82
+ "@aztec/telemetry-client": "0.82.3",
83
+ "@aztec/validator-client": "0.82.3",
84
+ "@aztec/world-state": "0.82.3",
83
85
  "koa": "^2.14.2",
84
86
  "koa-router": "^12.0.0",
85
87
  "tslib": "^2.4.0"
@@ -1,12 +1,8 @@
1
1
  import { type ArchiverConfig, archiverConfigMappings } from '@aztec/archiver/config';
2
- import {
3
- type GenesisStateConfig,
4
- type L1ContractAddresses,
5
- genesisStateConfigMappings,
6
- l1ContractAddressesMapping,
7
- } from '@aztec/ethereum';
2
+ import { type L1ContractAddresses, l1ContractAddressesMapping } from '@aztec/ethereum';
8
3
  import { type ConfigMappingsType, booleanConfigHelper, getConfigFromMappings } from '@aztec/foundation/config';
9
4
  import { type DataStoreConfig, dataConfigMappings } from '@aztec/kv-store/config';
5
+ import { type SharedNodeConfig, sharedNodeConfigMappings } from '@aztec/node-lib/config';
10
6
  import { type P2PConfig, p2pConfigMappings } from '@aztec/p2p/config';
11
7
  import { type ProverClientConfig, proverClientConfigMappings } from '@aztec/prover-client/config';
12
8
  import { type SequencerClientConfig, sequencerClientConfigMappings } from '@aztec/sequencer-client/config';
@@ -17,6 +13,8 @@ import { readFileSync } from 'fs';
17
13
  import { dirname, resolve } from 'path';
18
14
  import { fileURLToPath } from 'url';
19
15
 
16
+ import { type SentinelConfig, sentinelConfigMappings } from '../sentinel/config.js';
17
+
20
18
  export { sequencerClientConfigMappings, type SequencerClientConfig };
21
19
 
22
20
  /**
@@ -30,11 +28,12 @@ export type AztecNodeConfig = ArchiverConfig &
30
28
  Pick<ProverClientConfig, 'bbBinaryPath' | 'bbWorkingDirectory' | 'realProofs'> &
31
29
  P2PConfig &
32
30
  DataStoreConfig &
33
- GenesisStateConfig & {
31
+ SentinelConfig &
32
+ SharedNodeConfig & {
33
+ /** L1 contracts addresses */
34
+ l1Contracts: L1ContractAddresses;
34
35
  /** Whether the validator is disabled for this node */
35
36
  disableValidator: boolean;
36
- } & {
37
- l1Contracts: L1ContractAddresses;
38
37
  };
39
38
 
40
39
  export const aztecNodeConfigMappings: ConfigMappingsType<AztecNodeConfig> = {
@@ -45,7 +44,8 @@ export const aztecNodeConfigMappings: ConfigMappingsType<AztecNodeConfig> = {
45
44
  ...proverClientConfigMappings,
46
45
  ...worldStateConfigMappings,
47
46
  ...p2pConfigMappings,
48
- ...genesisStateConfigMappings,
47
+ ...sentinelConfigMappings,
48
+ ...sharedNodeConfigMappings,
49
49
  l1Contracts: {
50
50
  description: 'The deployed L1 contract addresses',
51
51
  nested: l1ContractAddressesMapping,
@@ -1,4 +1,4 @@
1
- import { createArchiver } from '@aztec/archiver';
1
+ import { Archiver, createArchiver } from '@aztec/archiver';
2
2
  import { BBCircuitVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
3
3
  import { type BlobSinkClientInterface, createBlobSinkClient } from '@aztec/blob-sink/client';
4
4
  import {
@@ -21,6 +21,7 @@ import { SiblingPath } from '@aztec/foundation/trees';
21
21
  import type { AztecKVStore } from '@aztec/kv-store';
22
22
  import { openTmpStore } from '@aztec/kv-store/lmdb';
23
23
  import { SHA256Trunc, StandardTree, UnbalancedTree } from '@aztec/merkle-tree';
24
+ import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
24
25
  import { type P2P, createP2PClient } from '@aztec/p2p';
25
26
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
26
27
  import {
@@ -74,6 +75,7 @@ import {
74
75
  TxStatus,
75
76
  type TxValidationResult,
76
77
  } from '@aztec/stdlib/tx';
78
+ import type { ValidatorsStats } from '@aztec/stdlib/validators';
77
79
  import {
78
80
  Attributes,
79
81
  type TelemetryClient,
@@ -85,6 +87,8 @@ import {
85
87
  import { createValidatorClient } from '@aztec/validator-client';
86
88
  import { createWorldStateSynchronizer } from '@aztec/world-state';
87
89
 
90
+ import { createSentinel } from '../sentinel/factory.js';
91
+ import { Sentinel } from '../sentinel/sentinel.js';
88
92
  import { type AztecNodeConfig, getPackageVersion } from './config.js';
89
93
  import { NodeMetrics } from './node_metrics.js';
90
94
 
@@ -95,6 +99,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
95
99
  private packageVersion: string;
96
100
  private metrics: NodeMetrics;
97
101
 
102
+ // Prevent two snapshot operations to happen simultaneously
103
+ private isUploadingSnapshot = false;
104
+
98
105
  // Serial queue to ensure that we only send one tx at a time
99
106
  private txQueue: SerialQueue = new SerialQueue();
100
107
 
@@ -109,6 +116,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
109
116
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
110
117
  protected readonly worldStateSynchronizer: WorldStateSynchronizer,
111
118
  protected readonly sequencer: SequencerClient | undefined,
119
+ protected readonly validatorsSentinel: Sentinel | undefined,
112
120
  protected readonly l1ChainId: number,
113
121
  protected readonly version: number,
114
122
  protected readonly globalVariableBuilder: GlobalVariableBuilder,
@@ -157,13 +165,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
157
165
  const dateProvider = deps.dateProvider ?? new DateProvider();
158
166
  const blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config);
159
167
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
160
- //validate that the actual chain id matches that specified in configuration
168
+
169
+ // validate that the actual chain id matches that specified in configuration
161
170
  if (config.l1ChainId !== ethereumChain.chainInfo.id) {
162
171
  throw new Error(
163
172
  `RPC URL configured for chain id ${ethereumChain.chainInfo.id} but expected id ${config.l1ChainId}`,
164
173
  );
165
174
  }
166
175
 
176
+ // attempt snapshot sync if possible
177
+ await trySnapshotSync(config, log);
178
+
167
179
  const archiver = await createArchiver(config, blobSinkClient, { blockUntilSync: true }, telemetry);
168
180
 
169
181
  // now create the merkle trees and the world state synchronizer
@@ -199,6 +211,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
199
211
 
200
212
  const validatorClient = createValidatorClient(config, { p2pClient, telemetry, dateProvider, epochCache });
201
213
 
214
+ const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
215
+ await validatorsSentinel?.start();
216
+
202
217
  // now create the sequencer
203
218
  const sequencer = config.disableValidator
204
219
  ? undefined
@@ -225,6 +240,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
225
240
  archiver,
226
241
  worldStateSynchronizer,
227
242
  sequencer,
243
+ validatorsSentinel,
228
244
  ethereumChain.chainInfo.id,
229
245
  config.version,
230
246
  new GlobalVariableBuilder(config),
@@ -471,6 +487,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
471
487
  public async stop() {
472
488
  this.log.info(`Stopping`);
473
489
  await this.txQueue.end();
490
+ await this.validatorsSentinel?.stop();
474
491
  await this.sequencer?.stop();
475
492
  await this.p2pClient.stop();
476
493
  await this.worldStateSynchronizer.stop();
@@ -978,6 +995,47 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
978
995
  return Promise.resolve();
979
996
  }
980
997
 
998
+ public getValidatorsStats(): Promise<ValidatorsStats> {
999
+ return this.validatorsSentinel?.computeStats() ?? Promise.resolve({ stats: {}, slotWindow: 0 });
1000
+ }
1001
+
1002
+ public async startSnapshotUpload(location: string): Promise<void> {
1003
+ // Note that we are forcefully casting the blocksource as an archiver
1004
+ // We break support for archiver running remotely to the node
1005
+ const archiver = this.blockSource as Archiver;
1006
+ if (!('backupTo' in archiver)) {
1007
+ throw new Error('Archiver implementation does not support backups. Cannot generate snapshot.');
1008
+ }
1009
+
1010
+ // Test that the archiver has done an initial sync.
1011
+ if (!archiver.isInitialSyncComplete()) {
1012
+ throw new Error(`Archiver initial sync not complete. Cannot start snapshot.`);
1013
+ }
1014
+
1015
+ // And it has an L2 block hash
1016
+ const l2BlockHash = await archiver.getL2Tips().then(tips => tips.latest.hash);
1017
+ if (!l2BlockHash) {
1018
+ throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
1019
+ }
1020
+
1021
+ if (this.isUploadingSnapshot) {
1022
+ throw new Error(`Snapshot upload already in progress. Cannot start another one until complete.`);
1023
+ }
1024
+
1025
+ // Do not wait for the upload to be complete to return to the caller, but flag that an operation is in progress
1026
+ this.isUploadingSnapshot = true;
1027
+ void uploadSnapshot(location, this.blockSource as Archiver, this.worldStateSynchronizer, this.config, this.log)
1028
+ .then(() => {
1029
+ this.isUploadingSnapshot = false;
1030
+ })
1031
+ .catch(err => {
1032
+ this.isUploadingSnapshot = false;
1033
+ this.log.error(`Error uploading snapshot: ${err}`);
1034
+ });
1035
+
1036
+ return Promise.resolve();
1037
+ }
1038
+
981
1039
  /**
982
1040
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
983
1041
  * @param blockNumber - The block number at which to get the data.
@@ -0,0 +1,19 @@
1
+ import { type ConfigMappingsType, booleanConfigHelper, numberConfigHelper } from '@aztec/foundation/config';
2
+
3
+ export type SentinelConfig = {
4
+ sentinelHistoryLengthInEpochs: number;
5
+ sentinelEnabled: boolean;
6
+ };
7
+
8
+ export const sentinelConfigMappings: ConfigMappingsType<SentinelConfig> = {
9
+ sentinelHistoryLengthInEpochs: {
10
+ description: 'The number of L2 epochs kept of history for each validator for computing their stats.',
11
+ env: 'SENTINEL_HISTORY_LENGTH_IN_EPOCHS',
12
+ ...numberConfigHelper(24),
13
+ },
14
+ sentinelEnabled: {
15
+ description: 'Whether the sentinel is enabled or not.',
16
+ env: 'SENTINEL_ENABLED',
17
+ ...booleanConfigHelper(true),
18
+ },
19
+ };
@@ -0,0 +1,31 @@
1
+ import type { EpochCache } from '@aztec/epoch-cache';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
4
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
5
+ import type { P2PClient } from '@aztec/p2p';
6
+ import type { L2BlockSource } from '@aztec/stdlib/block';
7
+
8
+ import type { SentinelConfig } from './config.js';
9
+ import { Sentinel } from './sentinel.js';
10
+ import { SentinelStore } from './store.js';
11
+
12
+ export async function createSentinel(
13
+ epochCache: EpochCache,
14
+ archiver: L2BlockSource,
15
+ p2p: P2PClient,
16
+ config: SentinelConfig & DataStoreConfig,
17
+ logger = createLogger('node:sentinel'),
18
+ ): Promise<Sentinel | undefined> {
19
+ if (!config.sentinelEnabled) {
20
+ return undefined;
21
+ }
22
+ const kvStore = await createStore(
23
+ 'sentinel',
24
+ SentinelStore.SCHEMA_VERSION,
25
+ config,
26
+ createLogger('node:sentinel:lmdb'),
27
+ );
28
+ const storeHistoryLength = config.sentinelHistoryLengthInEpochs * epochCache.getL1Constants().epochDuration;
29
+ const sentinelStore = new SentinelStore(kvStore, { historyLength: storeHistoryLength });
30
+ return new Sentinel(epochCache, archiver, p2p, sentinelStore, logger);
31
+ }