@aztec/validator-client 0.0.1-commit.cd76b27 → 0.0.1-commit.ce4f8c4f2

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.
Files changed (48) hide show
  1. package/README.md +41 -0
  2. package/dest/block_proposal_handler.d.ts +4 -3
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +112 -30
  5. package/dest/checkpoint_builder.d.ts +19 -6
  6. package/dest/checkpoint_builder.d.ts.map +1 -1
  7. package/dest/checkpoint_builder.js +115 -39
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +22 -1
  11. package/dest/duties/validation_service.d.ts +1 -1
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +3 -9
  14. package/dest/factory.d.ts +3 -1
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +3 -2
  17. package/dest/index.d.ts +1 -2
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +0 -1
  20. package/dest/key_store/ha_key_store.js +1 -1
  21. package/dest/metrics.d.ts +9 -1
  22. package/dest/metrics.d.ts.map +1 -1
  23. package/dest/metrics.js +12 -0
  24. package/dest/validator.d.ts +7 -5
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +78 -45
  27. package/package.json +19 -19
  28. package/src/block_proposal_handler.ts +134 -37
  29. package/src/checkpoint_builder.ts +135 -39
  30. package/src/config.ts +22 -1
  31. package/src/duties/validation_service.ts +3 -9
  32. package/src/factory.ts +4 -0
  33. package/src/index.ts +0 -1
  34. package/src/key_store/ha_key_store.ts +1 -1
  35. package/src/metrics.ts +18 -0
  36. package/src/validator.ts +87 -52
  37. package/dest/tx_validator/index.d.ts +0 -3
  38. package/dest/tx_validator/index.d.ts.map +0 -1
  39. package/dest/tx_validator/index.js +0 -2
  40. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  41. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  42. package/dest/tx_validator/nullifier_cache.js +0 -24
  43. package/dest/tx_validator/tx_validator_factory.d.ts +0 -19
  44. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  45. package/dest/tx_validator/tx_validator_factory.js +0 -54
  46. package/src/tx_validator/index.ts +0 -2
  47. package/src/tx_validator/nullifier_cache.ts +0 -30
  48. package/src/tx_validator/tx_validator_factory.ts +0 -154
package/dest/validator.js CHANGED
@@ -9,11 +9,12 @@ import { sleep } from '@aztec/foundation/sleep';
9
9
  import { DateProvider } from '@aztec/foundation/timer';
10
10
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
11
11
  import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
12
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
12
13
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
13
14
  import { accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
14
15
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
15
16
  import { getTelemetryClient } from '@aztec/telemetry-client';
16
- import { createHASigner } from '@aztec/validator-ha-signer/factory';
17
+ import { createHASigner, createLocalSignerWithProtection, createSignerFromSharedDb } from '@aztec/validator-ha-signer/factory';
17
18
  import { DutyType } from '@aztec/validator-ha-signer/types';
18
19
  import { EventEmitter } from 'events';
19
20
  import { BlockProposalHandler } from './block_proposal_handler.js';
@@ -42,7 +43,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
42
43
  l1ToL2MessageSource;
43
44
  config;
44
45
  blobClient;
45
- haSigner;
46
+ slashingProtectionSigner;
46
47
  dateProvider;
47
48
  tracer;
48
49
  validationService;
@@ -54,10 +55,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
54
55
  /** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
55
56
  lastEpochForCommitteeUpdateLoop;
56
57
  epochCacheUpdateLoop;
58
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */ lastAttestedEpochByAttester;
57
59
  proposersOfInvalidBlocks;
58
60
  /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
59
- constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
60
- super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.haSigner = haSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
61
+ constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
62
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.slashingProtectionSigner = slashingProtectionSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.lastAttestedEpochByAttester = new Map(), this.proposersOfInvalidBlocks = new Set();
61
63
  // Create child logger with fisherman prefix if in fisherman mode
62
64
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
63
65
  this.tracer = telemetry.getTracer('Validator');
@@ -96,6 +98,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
96
98
  this.log.trace(`No committee found for slot`);
97
99
  return;
98
100
  }
101
+ this.metrics.setCurrentEpoch(epoch);
99
102
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
100
103
  const me = this.getValidatorAddresses();
101
104
  const committeeSet = new Set(committee.map((v)=>v.toString()));
@@ -111,26 +114,42 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
111
114
  this.log.error(`Error updating epoch committee`, err);
112
115
  }
113
116
  }
114
- static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
117
+ static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), slashingProtectionDb) {
115
118
  const metrics = new ValidatorMetrics(telemetry);
116
119
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
117
- txsPermitted: !config.disableTransactions
120
+ txsPermitted: !config.disableTransactions,
121
+ maxTxsPerBlock: config.validateMaxTxsPerBlock
118
122
  });
119
123
  const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
120
124
  const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
121
- let validatorKeyStore = nodeKeystoreAdapter;
122
- let haSigner;
123
- if (config.haSigningEnabled) {
125
+ let slashingProtectionSigner;
126
+ if (slashingProtectionDb) {
127
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
128
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
129
+ telemetryClient: telemetry,
130
+ dateProvider
131
+ }));
132
+ } else if (config.haSigningEnabled) {
133
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
124
134
  // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
125
135
  const haConfig = {
126
136
  ...config,
127
137
  maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
128
138
  };
129
- const { signer } = await createHASigner(haConfig);
130
- haSigner = signer;
131
- validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, signer);
132
- }
133
- const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider, telemetry);
139
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
140
+ telemetryClient: telemetry,
141
+ dateProvider
142
+ }));
143
+ } else {
144
+ // Single-node mode: use LMDB-backed local signing protection.
145
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
146
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
147
+ telemetryClient: telemetry,
148
+ dateProvider
149
+ }));
150
+ }
151
+ const validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
152
+ const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider, telemetry);
134
153
  return validator;
135
154
  }
136
155
  getValidatorAddresses() {
@@ -158,17 +177,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
158
177
  };
159
178
  }
160
179
  reloadKeystore(newManager) {
161
- if (this.config.haSigningEnabled && !this.haSigner) {
162
- this.log.warn('HA signing is enabled in config but was not initialized at startup. ' + 'Restart the node to enable HA signing.');
163
- } else if (!this.config.haSigningEnabled && this.haSigner) {
164
- this.log.warn('HA signing was disabled via config update but the HA signer is still active. ' + 'Restart the node to fully disable HA signing.');
165
- }
166
180
  const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
167
- if (this.haSigner) {
168
- this.keyStore = new HAKeyStore(newAdapter, this.haSigner);
169
- } else {
170
- this.keyStore = newAdapter;
171
- }
181
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
172
182
  this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
173
183
  }
174
184
  async start() {
@@ -231,13 +241,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
231
241
  this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
232
242
  return false;
233
243
  }
234
- // Ignore proposals from ourselves (may happen in HA setups)
244
+ // Log self-proposals from HA peers (same validator key on different nodes)
235
245
  if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
236
- this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
246
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
237
247
  proposer: proposer.toString(),
238
248
  slotNumber
239
249
  });
240
- return false;
241
250
  }
242
251
  // Check if we're in the committee (for metrics purposes)
243
252
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
@@ -257,8 +266,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
257
266
  const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
258
267
  const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
259
268
  if (!validationResult.isValid) {
260
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
261
269
  const reason = validationResult.reason || 'unknown';
270
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
262
271
  // Classify failure reason: bad proposal vs node issue
263
272
  const badProposalReasons = [
264
273
  'invalid_proposal',
@@ -298,48 +307,46 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
298
307
  * the lastBlock is extracted and processed separately via the block handler.
299
308
  * @returns Checkpoint attestations if valid, undefined otherwise
300
309
  */ async attestToCheckpointProposal(proposal, _proposalSender) {
301
- const slotNumber = proposal.slotNumber;
310
+ const proposalSlotNumber = proposal.slotNumber;
302
311
  const proposer = proposal.getSender();
303
312
  // If escape hatch is open for this slot's epoch, do not attest.
304
- if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
305
- this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
313
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
314
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
306
315
  return undefined;
307
316
  }
308
317
  // Reject proposals with invalid signatures
309
318
  if (!proposer) {
310
- this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
319
+ this.log.warn(`Received checkpoint proposal with invalid signature for proposal slot ${proposalSlotNumber}`);
311
320
  return undefined;
312
321
  }
313
322
  // Ignore proposals from ourselves (may happen in HA setups)
314
323
  if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
315
- this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
324
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
316
325
  proposer: proposer.toString(),
317
- slotNumber
326
+ proposalSlotNumber
318
327
  });
319
328
  return undefined;
320
329
  }
321
330
  // Validate fee asset price modifier is within allowed range
322
331
  if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
323
- this.log.warn(`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${slotNumber}`);
332
+ this.log.warn(`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposalSlotNumber}`);
324
333
  return undefined;
325
334
  }
326
- // Check that I have any address in current committee before attesting
327
- const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
335
+ // Check that I have any address in the committee where this checkpoint will land before attesting
336
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
328
337
  const partOfCommittee = inCommittee.length > 0;
329
338
  const proposalInfo = {
330
- slotNumber,
339
+ proposalSlotNumber,
331
340
  archive: proposal.archive.toString(),
332
- proposer: proposer.toString(),
333
- txCount: proposal.txHashes.length
341
+ proposer: proposer.toString()
334
342
  };
335
- this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
343
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
336
344
  ...proposalInfo,
337
- txHashes: proposal.txHashes.map((t)=>t.toString()),
338
345
  fishermanMode: this.config.fishermanMode || false
339
346
  });
340
347
  // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
341
348
  if (this.config.skipCheckpointProposalValidation) {
342
- this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
349
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
343
350
  } else {
344
351
  const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
345
352
  if (!validationResult.isValid) {
@@ -358,12 +365,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
358
365
  return undefined;
359
366
  }
360
367
  // Provided all of the above checks pass, we can attest to the proposal
361
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
368
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`, {
362
369
  ...proposalInfo,
363
370
  inCommittee: partOfCommittee,
364
371
  fishermanMode: this.config.fishermanMode || false
365
372
  });
366
373
  this.metrics.incSuccessfulAttestations(inCommittee.length);
374
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
375
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
376
+ for (const attester of inCommittee){
377
+ const key = attester.toString();
378
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
379
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
380
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
381
+ this.metrics.incAttestedEpochCount(attester);
382
+ }
383
+ }
367
384
  // Determine which validators should attest
368
385
  let attestors;
369
386
  if (partOfCommittee) {
@@ -380,7 +397,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
380
397
  }
381
398
  if (this.config.fishermanMode) {
382
399
  // bail out early and don't save attestations to the pool in fisherman mode
383
- this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
400
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
384
401
  ...proposalInfo,
385
402
  attestors: attestors.map((a)=>a.toString())
386
403
  });
@@ -534,6 +551,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
534
551
  reason: 'out_hash_mismatch'
535
552
  };
536
553
  }
554
+ // Final round of validations on the checkpoint, just in case.
555
+ try {
556
+ validateCheckpoint(computedCheckpoint, {
557
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
558
+ maxDABlockGas: this.config.validateMaxDABlockGas,
559
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
560
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
561
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint
562
+ });
563
+ } catch (err) {
564
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
565
+ return {
566
+ isValid: false,
567
+ reason: 'checkpoint_validation_failed'
568
+ };
569
+ }
537
570
  this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
538
571
  return {
539
572
  isValid: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/validator-client",
3
- "version": "0.0.1-commit.cd76b27",
3
+ "version": "0.0.1-commit.ce4f8c4f2",
4
4
  "main": "dest/index.js",
5
5
  "type": "module",
6
6
  "exports": {
@@ -64,30 +64,30 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/blob-client": "0.0.1-commit.cd76b27",
68
- "@aztec/blob-lib": "0.0.1-commit.cd76b27",
69
- "@aztec/constants": "0.0.1-commit.cd76b27",
70
- "@aztec/epoch-cache": "0.0.1-commit.cd76b27",
71
- "@aztec/ethereum": "0.0.1-commit.cd76b27",
72
- "@aztec/foundation": "0.0.1-commit.cd76b27",
73
- "@aztec/node-keystore": "0.0.1-commit.cd76b27",
74
- "@aztec/noir-protocol-circuits-types": "0.0.1-commit.cd76b27",
75
- "@aztec/p2p": "0.0.1-commit.cd76b27",
76
- "@aztec/protocol-contracts": "0.0.1-commit.cd76b27",
77
- "@aztec/prover-client": "0.0.1-commit.cd76b27",
78
- "@aztec/simulator": "0.0.1-commit.cd76b27",
79
- "@aztec/slasher": "0.0.1-commit.cd76b27",
80
- "@aztec/stdlib": "0.0.1-commit.cd76b27",
81
- "@aztec/telemetry-client": "0.0.1-commit.cd76b27",
82
- "@aztec/validator-ha-signer": "0.0.1-commit.cd76b27",
67
+ "@aztec/blob-client": "0.0.1-commit.ce4f8c4f2",
68
+ "@aztec/blob-lib": "0.0.1-commit.ce4f8c4f2",
69
+ "@aztec/constants": "0.0.1-commit.ce4f8c4f2",
70
+ "@aztec/epoch-cache": "0.0.1-commit.ce4f8c4f2",
71
+ "@aztec/ethereum": "0.0.1-commit.ce4f8c4f2",
72
+ "@aztec/foundation": "0.0.1-commit.ce4f8c4f2",
73
+ "@aztec/node-keystore": "0.0.1-commit.ce4f8c4f2",
74
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.ce4f8c4f2",
75
+ "@aztec/p2p": "0.0.1-commit.ce4f8c4f2",
76
+ "@aztec/protocol-contracts": "0.0.1-commit.ce4f8c4f2",
77
+ "@aztec/prover-client": "0.0.1-commit.ce4f8c4f2",
78
+ "@aztec/simulator": "0.0.1-commit.ce4f8c4f2",
79
+ "@aztec/slasher": "0.0.1-commit.ce4f8c4f2",
80
+ "@aztec/stdlib": "0.0.1-commit.ce4f8c4f2",
81
+ "@aztec/telemetry-client": "0.0.1-commit.ce4f8c4f2",
82
+ "@aztec/validator-ha-signer": "0.0.1-commit.ce4f8c4f2",
83
83
  "koa": "^2.16.1",
84
84
  "koa-router": "^13.1.1",
85
85
  "tslib": "^2.4.0",
86
86
  "viem": "npm:@aztec/viem@2.38.2"
87
87
  },
88
88
  "devDependencies": {
89
- "@aztec/archiver": "0.0.1-commit.cd76b27",
90
- "@aztec/world-state": "0.0.1-commit.cd76b27",
89
+ "@aztec/archiver": "0.0.1-commit.ce4f8c4f2",
90
+ "@aztec/world-state": "0.0.1-commit.ce4f8c4f2",
91
91
  "@electric-sql/pglite": "^0.3.14",
92
92
  "@jest/globals": "^30.0.0",
93
93
  "@types/jest": "^30.0.0",
@@ -1,6 +1,7 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import type { EpochCache } from '@aztec/epoch-cache';
3
3
  import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { pick } from '@aztec/foundation/collection';
4
5
  import { Fr } from '@aztec/foundation/curves/bn254';
5
6
  import { TimeoutError } from '@aztec/foundation/error';
6
7
  import { createLogger } from '@aztec/foundation/log';
@@ -10,12 +11,15 @@ import type { P2P, PeerId } from '@aztec/p2p';
10
11
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
11
12
  import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
12
13
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
14
+ import { Gas } from '@aztec/stdlib/gas';
13
15
  import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
14
16
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
15
17
  import type { BlockProposal } from '@aztec/stdlib/p2p';
18
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
16
19
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
17
20
  import {
18
21
  ReExFailedTxsError,
22
+ ReExInitialStateMismatchError,
19
23
  ReExStateMismatchError,
20
24
  ReExTimeoutError,
21
25
  TransactionsNotAvailableError,
@@ -28,6 +32,7 @@ import type { ValidatorMetrics } from './metrics.js';
28
32
  export type BlockProposalValidationFailureReason =
29
33
  | 'invalid_proposal'
30
34
  | 'parent_block_not_found'
35
+ | 'block_source_not_synced'
31
36
  | 'parent_block_wrong_slot'
32
37
  | 'in_hash_mismatch'
33
38
  | 'global_variables_mismatch'
@@ -35,6 +40,7 @@ export type BlockProposalValidationFailureReason =
35
40
  | 'txs_not_available'
36
41
  | 'state_mismatch'
37
42
  | 'failed_txs'
43
+ | 'initial_state_mismatch'
38
44
  | 'timeout'
39
45
  | 'unknown_error';
40
46
 
@@ -87,25 +93,28 @@ export class BlockProposalHandler {
87
93
  this.tracer = telemetry.getTracer('BlockProposalHandler');
88
94
  }
89
95
 
90
- registerForReexecution(p2pClient: P2P): BlockProposalHandler {
91
- // Non-validator handler that re-executes for monitoring but does not attest.
96
+ register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
97
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
92
98
  // Returns boolean indicating whether the proposal was valid.
93
99
  const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
94
100
  try {
95
- const result = await this.handleBlockProposal(proposal, proposalSender, true);
101
+ const { slotNumber, blockNumber } = proposal;
102
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
96
103
  if (result.isValid) {
97
- this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
104
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
98
105
  blockNumber: result.blockNumber,
106
+ slotNumber,
99
107
  reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
100
108
  totalManaUsed: result.reexecutionResult?.totalManaUsed,
101
109
  numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
110
+ reexecuted: shouldReexecute,
102
111
  });
103
112
  return true;
104
113
  } else {
105
- this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
106
- blockNumber: result.blockNumber,
107
- reason: result.reason,
108
- });
114
+ this.log.warn(
115
+ `Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
116
+ { blockNumber: result.blockNumber, slotNumber, reason: result.reason },
117
+ );
109
118
  return false;
110
119
  }
111
120
  } catch (error) {
@@ -133,7 +142,13 @@ export class BlockProposalHandler {
133
142
  return { isValid: false, reason: 'invalid_proposal' };
134
143
  }
135
144
 
136
- const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
145
+ const proposalInfo = {
146
+ ...proposal.toBlockInfo(),
147
+ proposer: proposer.toString(),
148
+ blockNumber: undefined as BlockNumber | undefined,
149
+ checkpointNumber: undefined as CheckpointNumber | undefined,
150
+ };
151
+
137
152
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
138
153
  ...proposalInfo,
139
154
  txHashes: proposal.txHashes.map(t => t.toString()),
@@ -147,7 +162,24 @@ export class BlockProposalHandler {
147
162
  return { isValid: false, reason: 'invalid_proposal' };
148
163
  }
149
164
 
150
- // Check that the parent proposal is a block we know, otherwise reexecution would fail
165
+ // Ensure the block source is synced before checking for existing blocks,
166
+ // since a pending checkpoint prune may remove blocks we'd otherwise find.
167
+ // This affects mostly the block_number_already_exists check, since a pending
168
+ // checkpoint prune could remove a block that would conflict with this proposal.
169
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
170
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
171
+ // avoid eating into the attestation window.
172
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
173
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
174
+ if (!blockSourceSync) {
175
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
176
+ return { isValid: false, reason: 'block_source_not_synced' };
177
+ }
178
+ }
179
+
180
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
181
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
182
+ // need to process other block proposals to get to it.
151
183
  const parentBlock = await this.getParentBlock(proposal);
152
184
  if (parentBlock === undefined) {
153
185
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
@@ -169,6 +201,7 @@ export class BlockProposalHandler {
169
201
  parentBlock === 'genesis'
170
202
  ? BlockNumber(INITIAL_L2_BLOCK_NUM)
171
203
  : BlockNumber(parentBlock.header.getBlockNumber() + 1);
204
+ proposalInfo.blockNumber = blockNumber;
172
205
 
173
206
  // Check that this block number does not exist already
174
207
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
@@ -184,12 +217,22 @@ export class BlockProposalHandler {
184
217
  deadline: this.getReexecutionDeadline(slotNumber, config),
185
218
  });
186
219
 
220
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
221
+ if (!shouldReexecute) {
222
+ this.log.info(
223
+ `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
224
+ proposalInfo,
225
+ );
226
+ return { isValid: true, blockNumber };
227
+ }
228
+
187
229
  // Compute the checkpoint number for this block and validate checkpoint consistency
188
230
  const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
189
231
  if (checkpointResult.reason) {
190
232
  return { isValid: false, blockNumber, reason: checkpointResult.reason };
191
233
  }
192
234
  const checkpointNumber = checkpointResult.checkpointNumber;
235
+ proposalInfo.checkpointNumber = checkpointNumber;
193
236
 
194
237
  // Check that I have the same set of l1ToL2Messages as the proposal
195
238
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
@@ -210,30 +253,28 @@ export class BlockProposalHandler {
210
253
  return { isValid: false, blockNumber, reason: 'txs_not_available' };
211
254
  }
212
255
 
256
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
257
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
258
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
259
+ .filter(c => c.checkpointNumber < checkpointNumber)
260
+ .map(c => c.checkpointOutHash);
261
+
213
262
  // Try re-executing the transactions in the proposal if needed
214
263
  let reexecutionResult;
215
- if (shouldReexecute) {
216
- // Collect the out hashes of all the checkpoints before this one in the same epoch
217
- const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
218
- const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
219
- .filter(c => c.checkpointNumber < checkpointNumber)
220
- .map(c => c.checkpointOutHash);
221
-
222
- try {
223
- this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
224
- reexecutionResult = await this.reexecuteTransactions(
225
- proposal,
226
- blockNumber,
227
- checkpointNumber,
228
- txs,
229
- l1ToL2Messages,
230
- previousCheckpointOutHashes,
231
- );
232
- } catch (error) {
233
- this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
234
- const reason = this.getReexecuteFailureReason(error);
235
- return { isValid: false, blockNumber, reason, reexecutionResult };
236
- }
264
+ try {
265
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
266
+ reexecutionResult = await this.reexecuteTransactions(
267
+ proposal,
268
+ blockNumber,
269
+ checkpointNumber,
270
+ txs,
271
+ l1ToL2Messages,
272
+ previousCheckpointOutHashes,
273
+ );
274
+ } catch (error) {
275
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
276
+ const reason = this.getReexecuteFailureReason(error);
277
+ return { isValid: false, blockNumber, reason, reexecutionResult };
237
278
  }
238
279
 
239
280
  // If we succeeded, push this block into the archiver (unless disabled)
@@ -242,8 +283,8 @@ export class BlockProposalHandler {
242
283
  }
243
284
 
244
285
  this.log.info(
245
- `Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
246
- proposalInfo,
286
+ `Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
287
+ { ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
247
288
  );
248
289
 
249
290
  return { isValid: true, blockNumber, reexecutionResult };
@@ -413,8 +454,48 @@ export class BlockProposalHandler {
413
454
  return new Date(nextSlotTimestampSeconds * 1000);
414
455
  }
415
456
 
416
- private getReexecuteFailureReason(err: any) {
417
- if (err instanceof ReExStateMismatchError) {
457
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
458
+ private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
459
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
460
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
461
+ if (slot === 0) {
462
+ return true;
463
+ }
464
+
465
+ // Make a quick check before triggering an archiver sync
466
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
467
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
468
+ return true;
469
+ }
470
+
471
+ try {
472
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
473
+ return await retryUntil(
474
+ async () => {
475
+ await this.blockSource.syncImmediate();
476
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
477
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
478
+ },
479
+ 'wait for block source sync',
480
+ timeoutMs / 1000,
481
+ 0.5,
482
+ );
483
+ } catch (err) {
484
+ if (err instanceof TimeoutError) {
485
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
486
+ return false;
487
+ } else {
488
+ throw err;
489
+ }
490
+ }
491
+ }
492
+
493
+ private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
494
+ if (err instanceof TransactionsNotAvailableError) {
495
+ return 'txs_not_available';
496
+ } else if (err instanceof ReExInitialStateMismatchError) {
497
+ return 'initial_state_mismatch';
498
+ } else if (err instanceof ReExStateMismatchError) {
418
499
  return 'state_mismatch';
419
500
  } else if (err instanceof ReExFailedTxsError) {
420
501
  return 'failed_txs';
@@ -455,6 +536,13 @@ export class BlockProposalHandler {
455
536
  await this.worldState.syncImmediate(parentBlockNumber);
456
537
  await using fork = await this.worldState.fork(parentBlockNumber);
457
538
 
539
+ // Verify the fork's archive root matches the proposal's expected last archive.
540
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
541
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
542
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
543
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
544
+ }
545
+
458
546
  // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
459
547
  const constants: CheckpointGlobalVariables = {
460
548
  chainId: new Fr(config.l1ChainId),
@@ -480,18 +568,27 @@ export class BlockProposalHandler {
480
568
 
481
569
  // Build the new block
482
570
  const deadline = this.getReexecutionDeadline(slot, config);
571
+ const maxBlockGas =
572
+ this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
573
+ ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
574
+ : undefined;
483
575
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
576
+ isBuildingProposal: false,
577
+ minValidTxs: 0,
484
578
  deadline,
485
579
  expectedEndState: blockHeader.state,
580
+ maxTransactions: this.config.validateMaxTxsPerBlock,
581
+ maxBlockGas,
486
582
  });
487
583
 
488
584
  const { block, failedTxs } = result;
489
585
  const numFailedTxs = failedTxs.length;
490
586
 
491
- this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
587
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
492
588
  numFailedTxs,
493
589
  numProposalTxs: txHashes.length,
494
590
  numProcessedTxs: block.body.txEffects.length,
591
+ blockNumber,
495
592
  slot,
496
593
  });
497
594