@aztec/archiver 0.0.1-commit.2ed92850 → 0.0.1-commit.343b43af6

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 (96) hide show
  1. package/dest/archiver.d.ts +9 -5
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +76 -111
  4. package/dest/errors.d.ts +7 -9
  5. package/dest/errors.d.ts.map +1 -1
  6. package/dest/errors.js +9 -14
  7. package/dest/factory.d.ts +5 -4
  8. package/dest/factory.d.ts.map +1 -1
  9. package/dest/factory.js +17 -16
  10. package/dest/index.d.ts +2 -1
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -0
  13. package/dest/l1/bin/retrieve-calldata.js +36 -33
  14. package/dest/l1/calldata_retriever.d.ts +73 -50
  15. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  16. package/dest/l1/calldata_retriever.js +190 -259
  17. package/dest/l1/data_retrieval.d.ts +9 -9
  18. package/dest/l1/data_retrieval.d.ts.map +1 -1
  19. package/dest/l1/data_retrieval.js +22 -20
  20. package/dest/l1/spire_proposer.d.ts +5 -5
  21. package/dest/l1/spire_proposer.d.ts.map +1 -1
  22. package/dest/l1/spire_proposer.js +9 -17
  23. package/dest/l1/validate_trace.d.ts +6 -3
  24. package/dest/l1/validate_trace.d.ts.map +1 -1
  25. package/dest/l1/validate_trace.js +13 -9
  26. package/dest/modules/data_source_base.d.ts +13 -8
  27. package/dest/modules/data_source_base.d.ts.map +1 -1
  28. package/dest/modules/data_source_base.js +28 -72
  29. package/dest/modules/data_store_updater.d.ts +22 -7
  30. package/dest/modules/data_store_updater.d.ts.map +1 -1
  31. package/dest/modules/data_store_updater.js +69 -29
  32. package/dest/modules/instrumentation.d.ts +15 -2
  33. package/dest/modules/instrumentation.d.ts.map +1 -1
  34. package/dest/modules/instrumentation.js +36 -12
  35. package/dest/modules/l1_synchronizer.d.ts +5 -8
  36. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  37. package/dest/modules/l1_synchronizer.js +43 -13
  38. package/dest/store/block_store.d.ts +30 -28
  39. package/dest/store/block_store.d.ts.map +1 -1
  40. package/dest/store/block_store.js +125 -76
  41. package/dest/store/contract_class_store.d.ts +1 -1
  42. package/dest/store/contract_class_store.d.ts.map +1 -1
  43. package/dest/store/contract_class_store.js +11 -7
  44. package/dest/store/kv_archiver_store.d.ts +36 -14
  45. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  46. package/dest/store/kv_archiver_store.js +39 -10
  47. package/dest/store/l2_tips_cache.d.ts +19 -0
  48. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  49. package/dest/store/l2_tips_cache.js +89 -0
  50. package/dest/store/log_store.d.ts +1 -1
  51. package/dest/store/log_store.d.ts.map +1 -1
  52. package/dest/store/log_store.js +57 -37
  53. package/dest/store/message_store.js +1 -1
  54. package/dest/test/fake_l1_state.d.ts +13 -1
  55. package/dest/test/fake_l1_state.d.ts.map +1 -1
  56. package/dest/test/fake_l1_state.js +84 -20
  57. package/dest/test/index.js +3 -1
  58. package/dest/test/mock_archiver.d.ts +1 -1
  59. package/dest/test/mock_archiver.d.ts.map +1 -1
  60. package/dest/test/mock_archiver.js +3 -2
  61. package/dest/test/mock_l2_block_source.d.ts +25 -9
  62. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  63. package/dest/test/mock_l2_block_source.js +132 -86
  64. package/dest/test/mock_structs.d.ts +6 -2
  65. package/dest/test/mock_structs.d.ts.map +1 -1
  66. package/dest/test/mock_structs.js +20 -6
  67. package/dest/test/noop_l1_archiver.d.ts +26 -0
  68. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  69. package/dest/test/noop_l1_archiver.js +72 -0
  70. package/package.json +14 -13
  71. package/src/archiver.ts +96 -132
  72. package/src/errors.ts +10 -24
  73. package/src/factory.ts +32 -17
  74. package/src/index.ts +1 -0
  75. package/src/l1/README.md +25 -68
  76. package/src/l1/bin/retrieve-calldata.ts +46 -39
  77. package/src/l1/calldata_retriever.ts +249 -379
  78. package/src/l1/data_retrieval.ts +24 -26
  79. package/src/l1/spire_proposer.ts +7 -15
  80. package/src/l1/validate_trace.ts +24 -6
  81. package/src/modules/data_source_base.ts +58 -97
  82. package/src/modules/data_store_updater.ts +71 -30
  83. package/src/modules/instrumentation.ts +44 -12
  84. package/src/modules/l1_synchronizer.ts +48 -17
  85. package/src/store/block_store.ts +151 -108
  86. package/src/store/contract_class_store.ts +11 -7
  87. package/src/store/kv_archiver_store.ts +62 -16
  88. package/src/store/l2_tips_cache.ts +89 -0
  89. package/src/store/log_store.ts +98 -36
  90. package/src/store/message_store.ts +1 -1
  91. package/src/test/fake_l1_state.ts +110 -21
  92. package/src/test/index.ts +3 -0
  93. package/src/test/mock_archiver.ts +3 -2
  94. package/src/test/mock_l2_block_source.ts +172 -86
  95. package/src/test/mock_structs.ts +42 -12
  96. package/src/test/noop_l1_archiver.ts +115 -0
@@ -11,7 +11,7 @@ import {
11
11
  ContractInstanceUpdatedEvent,
12
12
  } from '@aztec/protocol-contracts/instance-registry';
13
13
  import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
14
+ import { type PublishedCheckpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
15
15
  import {
16
16
  type ExecutablePrivateFunctionWithMembershipProof,
17
17
  type UtilityFunctionWithMembershipProof,
@@ -25,6 +25,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
25
25
  import groupBy from 'lodash.groupby';
26
26
 
27
27
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
28
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
28
29
 
29
30
  /** Operation type for contract data updates. */
30
31
  enum Operation {
@@ -44,36 +45,42 @@ type ReconcileCheckpointsResult = {
44
45
  export class ArchiverDataStoreUpdater {
45
46
  private readonly log = createLogger('archiver:store_updater');
46
47
 
47
- constructor(private store: KVArchiverDataStore) {}
48
+ constructor(
49
+ private store: KVArchiverDataStore,
50
+ private l2TipsCache?: L2TipsCache,
51
+ private opts: { rollupManaLimit?: number } = {},
52
+ ) {}
48
53
 
49
54
  /**
50
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
51
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
55
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
56
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
52
57
  * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
53
58
  * and individually broadcasted functions from the block logs.
54
59
  *
55
- * @param blocks - The proposed L2 blocks to add.
60
+ * @param block - The proposed L2 block to add.
56
61
  * @param pendingChainValidationStatus - Optional validation status to set.
57
62
  * @returns True if the operation is successful.
58
63
  */
59
- public addProposedBlocks(
60
- blocks: L2Block[],
64
+ public async addProposedBlock(
65
+ block: L2Block,
61
66
  pendingChainValidationStatus?: ValidateCheckpointResult,
62
67
  ): Promise<boolean> {
63
- return this.store.transactionAsync(async () => {
64
- await this.store.addProposedBlocks(blocks);
68
+ const result = await this.store.transactionAsync(async () => {
69
+ await this.store.addProposedBlock(block);
65
70
 
66
71
  const opResults = await Promise.all([
67
72
  // Update the pending chain validation status if provided
68
73
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
69
- // Add any logs emitted during the retrieved blocks
70
- this.store.addLogs(blocks),
71
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
72
- ...blocks.map(block => this.addContractDataToDb(block)),
74
+ // Add any logs emitted during the retrieved block
75
+ this.store.addLogs([block]),
76
+ // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
77
+ this.addContractDataToDb(block),
73
78
  ]);
74
79
 
80
+ await this.l2TipsCache?.refresh();
75
81
  return opResults.every(Boolean);
76
82
  });
83
+ return result;
77
84
  }
78
85
 
79
86
  /**
@@ -87,17 +94,21 @@ export class ArchiverDataStoreUpdater {
87
94
  * @param pendingChainValidationStatus - Optional validation status to set.
88
95
  * @returns Result with information about any pruned blocks.
89
96
  */
90
- public addCheckpoints(
97
+ public async addCheckpoints(
91
98
  checkpoints: PublishedCheckpoint[],
92
99
  pendingChainValidationStatus?: ValidateCheckpointResult,
93
100
  ): Promise<ReconcileCheckpointsResult> {
94
- return this.store.transactionAsync(async () => {
101
+ for (const checkpoint of checkpoints) {
102
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
103
+ }
104
+
105
+ const result = await this.store.transactionAsync(async () => {
95
106
  // Before adding checkpoints, check for conflicts with local blocks if any
96
107
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
97
108
 
98
109
  await this.store.addCheckpoints(checkpoints);
99
110
 
100
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
111
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
101
112
  const newBlocks = checkpoints
102
113
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
103
114
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
@@ -111,8 +122,10 @@ export class ArchiverDataStoreUpdater {
111
122
  ...newBlocks.map(block => this.addContractDataToDb(block)),
112
123
  ]);
113
124
 
125
+ await this.l2TipsCache?.refresh();
114
126
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
115
127
  });
128
+ return result;
116
129
  }
117
130
 
118
131
  /**
@@ -165,7 +178,7 @@ export class ArchiverDataStoreUpdater {
165
178
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
166
179
  lastAlreadyInsertedBlockNumber = blockNumber;
167
180
  } else {
168
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
181
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
169
182
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
170
183
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
171
184
  }
@@ -197,8 +210,8 @@ export class ArchiverDataStoreUpdater {
197
210
  * @returns The removed blocks.
198
211
  * @throws Error if any block to be removed is checkpointed.
199
212
  */
200
- public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
201
- return this.store.transactionAsync(async () => {
213
+ public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
214
+ const result = await this.store.transactionAsync(async () => {
202
215
  // Verify we're only removing uncheckpointed blocks
203
216
  const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
204
217
  if (blockNumber < lastCheckpointedBlockNumber) {
@@ -207,8 +220,11 @@ export class ArchiverDataStoreUpdater {
207
220
  );
208
221
  }
209
222
 
210
- return await this.removeBlocksAfter(blockNumber);
223
+ const result = await this.removeBlocksAfter(blockNumber);
224
+ await this.l2TipsCache?.refresh();
225
+ return result;
211
226
  });
227
+ return result;
212
228
  }
213
229
 
214
230
  /**
@@ -238,17 +254,42 @@ export class ArchiverDataStoreUpdater {
238
254
  * @returns True if the operation is successful.
239
255
  */
240
256
  public async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
241
- const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
242
-
243
- const opResults = await Promise.all([
244
- // Prune rolls back to the last proven block, which is by definition valid
245
- this.store.setPendingChainValidationStatus({ valid: true }),
246
- // Remove contract data for all blocks being removed
247
- ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
248
- this.store.deleteLogs(blocksRemoved),
249
- ]);
257
+ return await this.store.transactionAsync(async () => {
258
+ const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
259
+
260
+ const opResults = await Promise.all([
261
+ // Prune rolls back to the last proven block, which is by definition valid
262
+ this.store.setPendingChainValidationStatus({ valid: true }),
263
+ // Remove contract data for all blocks being removed
264
+ ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
265
+ this.store.deleteLogs(blocksRemoved),
266
+ ]);
267
+
268
+ await this.l2TipsCache?.refresh();
269
+ return opResults.every(Boolean);
270
+ });
271
+ }
272
+
273
+ /**
274
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
275
+ * @param checkpointNumber - The checkpoint number to set as proven.
276
+ */
277
+ public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
278
+ await this.store.transactionAsync(async () => {
279
+ await this.store.setProvenCheckpointNumber(checkpointNumber);
280
+ await this.l2TipsCache?.refresh();
281
+ });
282
+ }
250
283
 
251
- return opResults.every(Boolean);
284
+ /**
285
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
286
+ * @param checkpointNumber - The checkpoint number to set as finalized.
287
+ */
288
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
289
+ await this.store.transactionAsync(async () => {
290
+ await this.store.setFinalizedCheckpointNumber(checkpointNumber);
291
+ await this.l2TipsCache?.refresh();
292
+ });
252
293
  }
253
294
 
254
295
  /** Extracts and stores contract data from a single block. */
@@ -1,5 +1,9 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
1
2
  import { createLogger } from '@aztec/foundation/log';
2
3
  import type { L2Block } from '@aztec/stdlib/block';
4
+ import type { CheckpointData } from '@aztec/stdlib/checkpoint';
5
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
6
+ import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
3
7
  import {
4
8
  Attributes,
5
9
  type Gauge,
@@ -10,12 +14,14 @@ import {
10
14
  type TelemetryClient,
11
15
  type Tracer,
12
16
  type UpDownCounter,
17
+ createUpDownCounterWithDefault,
13
18
  } from '@aztec/telemetry-client';
14
19
 
15
20
  export class ArchiverInstrumentation {
16
21
  public readonly tracer: Tracer;
17
22
 
18
23
  private blockHeight: Gauge;
24
+ private checkpointHeight: Gauge;
19
25
  private txCount: UpDownCounter;
20
26
  private l1BlockHeight: Gauge;
21
27
  private proofsSubmittedDelay: Histogram;
@@ -35,6 +41,8 @@ export class ArchiverInstrumentation {
35
41
 
36
42
  private blockProposalTxTargetCount: UpDownCounter;
37
43
 
44
+ private checkpointL1InclusionDelay: Histogram;
45
+
38
46
  private log = createLogger('archiver:instrumentation');
39
47
 
40
48
  private constructor(
@@ -46,17 +54,21 @@ export class ArchiverInstrumentation {
46
54
 
47
55
  this.blockHeight = meter.createGauge(Metrics.ARCHIVER_BLOCK_HEIGHT);
48
56
 
57
+ this.checkpointHeight = meter.createGauge(Metrics.ARCHIVER_CHECKPOINT_HEIGHT);
58
+
49
59
  this.l1BlockHeight = meter.createGauge(Metrics.ARCHIVER_L1_BLOCK_HEIGHT);
50
60
 
51
- this.txCount = meter.createUpDownCounter(Metrics.ARCHIVER_TOTAL_TXS);
61
+ this.txCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_TOTAL_TXS);
52
62
 
53
- this.proofsSubmittedCount = meter.createUpDownCounter(Metrics.ARCHIVER_ROLLUP_PROOF_COUNT);
63
+ this.proofsSubmittedCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_ROLLUP_PROOF_COUNT, {
64
+ [Attributes.PROOF_TIMED_OUT]: [true, false],
65
+ });
54
66
 
55
67
  this.proofsSubmittedDelay = meter.createHistogram(Metrics.ARCHIVER_ROLLUP_PROOF_DELAY);
56
68
 
57
69
  this.syncDurationPerBlock = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_BLOCK);
58
70
 
59
- this.syncBlockCount = meter.createUpDownCounter(Metrics.ARCHIVER_SYNC_BLOCK_COUNT);
71
+ this.syncBlockCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_SYNC_BLOCK_COUNT);
60
72
 
61
73
  this.manaPerBlock = meter.createHistogram(Metrics.ARCHIVER_MANA_PER_BLOCK);
62
74
 
@@ -64,13 +76,21 @@ export class ArchiverInstrumentation {
64
76
 
65
77
  this.syncDurationPerMessage = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_MESSAGE);
66
78
 
67
- this.syncMessageCount = meter.createUpDownCounter(Metrics.ARCHIVER_SYNC_MESSAGE_COUNT);
79
+ this.syncMessageCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_SYNC_MESSAGE_COUNT);
68
80
 
69
81
  this.pruneDuration = meter.createHistogram(Metrics.ARCHIVER_PRUNE_DURATION);
70
82
 
71
- this.pruneCount = meter.createUpDownCounter(Metrics.ARCHIVER_PRUNE_COUNT);
83
+ this.pruneCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_PRUNE_COUNT);
72
84
 
73
- this.blockProposalTxTargetCount = meter.createUpDownCounter(Metrics.ARCHIVER_BLOCK_PROPOSAL_TX_TARGET_COUNT);
85
+ this.blockProposalTxTargetCount = createUpDownCounterWithDefault(
86
+ meter,
87
+ Metrics.ARCHIVER_BLOCK_PROPOSAL_TX_TARGET_COUNT,
88
+ {
89
+ [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: [true, false],
90
+ },
91
+ );
92
+
93
+ this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
74
94
 
75
95
  this.dbMetrics = new LmdbMetrics(
76
96
  meter,
@@ -84,10 +104,6 @@ export class ArchiverInstrumentation {
84
104
  public static async new(telemetry: TelemetryClient, lmdbStats?: LmdbStatsCallback) {
85
105
  const instance = new ArchiverInstrumentation(telemetry, lmdbStats);
86
106
 
87
- instance.syncBlockCount.add(0);
88
- instance.syncMessageCount.add(0);
89
- instance.pruneCount.add(0);
90
-
91
107
  await instance.telemetry.flush();
92
108
 
93
109
  return instance;
@@ -100,6 +116,7 @@ export class ArchiverInstrumentation {
100
116
  public processNewBlocks(syncTimePerBlock: number, blocks: L2Block[]) {
101
117
  this.syncDurationPerBlock.record(Math.ceil(syncTimePerBlock));
102
118
  this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
119
+ this.checkpointHeight.record(Math.max(...blocks.map(b => b.checkpointNumber)));
103
120
  this.syncBlockCount.add(blocks.length);
104
121
 
105
122
  for (const block of blocks) {
@@ -122,8 +139,10 @@ export class ArchiverInstrumentation {
122
139
  this.pruneDuration.record(Math.ceil(duration));
123
140
  }
124
141
 
125
- public updateLastProvenBlock(blockNumber: number) {
126
- this.blockHeight.record(blockNumber, { [Attributes.STATUS]: 'proven' });
142
+ public updateLastProvenCheckpoint(checkpoint: CheckpointData) {
143
+ const lastBlockNumberInCheckpoint = checkpoint.startBlock + checkpoint.blockCount - 1;
144
+ this.blockHeight.record(lastBlockNumberInCheckpoint, { [Attributes.STATUS]: 'proven' });
145
+ this.checkpointHeight.record(checkpoint.checkpointNumber, { [Attributes.STATUS]: 'proven' });
127
146
  }
128
147
 
129
148
  public processProofsVerified(logs: { proverId: string; l2BlockNumber: bigint; delay: bigint }[]) {
@@ -149,4 +168,17 @@ export class ArchiverInstrumentation {
149
168
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
150
169
  });
151
170
  }
171
+
172
+ /**
173
+ * Records L1 inclusion timing for a checkpoint observed on L1 (seconds into the L2 slot).
174
+ */
175
+ public processCheckpointL1Timing(data: {
176
+ slotNumber: SlotNumber;
177
+ l1Timestamp: bigint;
178
+ l1Constants: Pick<L1RollupConstants, 'l1GenesisTime' | 'slotDuration'>;
179
+ }): void {
180
+ const slotStartTs = getTimestampForSlot(data.slotNumber, data.l1Constants);
181
+ const inclusionDelaySeconds = Number(data.l1Timestamp - slotStartTs);
182
+ this.checkpointL1InclusionDelay.record(inclusionDelaySeconds);
183
+ }
152
184
  }
@@ -1,7 +1,6 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { EpochCache } from '@aztec/epoch-cache';
3
3
  import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
4
- import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
5
4
  import type { L1BlockId } from '@aztec/ethereum/l1-types';
6
5
  import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
7
6
  import { maxBigint } from '@aztec/foundation/bigint';
@@ -9,14 +8,13 @@ import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/br
9
8
  import { Buffer32 } from '@aztec/foundation/buffer';
10
9
  import { pick } from '@aztec/foundation/collection';
11
10
  import { Fr } from '@aztec/foundation/curves/bn254';
12
- import { EthAddress } from '@aztec/foundation/eth-address';
13
11
  import { type Logger, createLogger } from '@aztec/foundation/log';
14
12
  import { count } from '@aztec/foundation/string';
15
13
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
16
14
  import { isDefined } from '@aztec/foundation/types';
17
15
  import { type ArchiverEmitter, L2BlockSourceEvents, type ValidateCheckpointResult } from '@aztec/stdlib/block';
18
16
  import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
19
- import { type L1RollupConstants, getEpochAtSlot, getSlotAtTimestamp } from '@aztec/stdlib/epoch-helpers';
17
+ import { type L1RollupConstants, getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
20
18
  import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
21
19
  import { type Traceable, type Tracer, execInSpan, trackSpan } from '@aztec/telemetry-client';
22
20
 
@@ -28,6 +26,7 @@ import {
28
26
  retrievedToPublishedCheckpoint,
29
27
  } from '../l1/data_retrieval.js';
30
28
  import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
29
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
31
30
  import type { InboxMessage } from '../structs/inbox_message.js';
32
31
  import { ArchiverDataStoreUpdater } from './data_store_updater.js';
33
32
  import type { ArchiverInstrumentation } from './instrumentation.js';
@@ -60,10 +59,6 @@ export class ArchiverL1Synchronizer implements Traceable {
60
59
  private readonly debugClient: ViemPublicDebugClient,
61
60
  private readonly rollup: RollupContract,
62
61
  private readonly inbox: InboxContract,
63
- private readonly l1Addresses: Pick<
64
- L1ContractAddresses,
65
- 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
66
- > & { slashingProposerAddress: EthAddress },
67
62
  private readonly store: KVArchiverDataStore,
68
63
  private config: {
69
64
  batchSize: number;
@@ -74,12 +69,18 @@ export class ArchiverL1Synchronizer implements Traceable {
74
69
  private readonly epochCache: EpochCache,
75
70
  private readonly dateProvider: DateProvider,
76
71
  private readonly instrumentation: ArchiverInstrumentation,
77
- private readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
72
+ private readonly l1Constants: L1RollupConstants & {
73
+ l1StartBlockHash: Buffer32;
74
+ genesisArchiveRoot: Fr;
75
+ },
78
76
  private readonly events: ArchiverEmitter,
79
77
  tracer: Tracer,
78
+ l2TipsCache?: L2TipsCache,
80
79
  private readonly log: Logger = createLogger('archiver:l1-sync'),
81
80
  ) {
82
- this.updater = new ArchiverDataStoreUpdater(this.store);
81
+ this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
82
+ rollupManaLimit: l1Constants.rollupManaLimit,
83
+ });
83
84
  this.tracer = tracer;
84
85
  }
85
86
 
@@ -215,6 +216,9 @@ export class ArchiverL1Synchronizer implements Traceable {
215
216
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
216
217
  }
217
218
 
219
+ // Update the finalized L2 checkpoint based on L1 finality.
220
+ await this.updateFinalizedCheckpoint();
221
+
218
222
  // After syncing has completed, update the current l1 block number and timestamp,
219
223
  // otherwise we risk announcing to the world that we've synced to a given point,
220
224
  // but the corresponding blocks have not been processed (see #12631).
@@ -230,6 +234,27 @@ export class ArchiverL1Synchronizer implements Traceable {
230
234
  });
231
235
  }
232
236
 
237
+ /** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
238
+ private async updateFinalizedCheckpoint(): Promise<void> {
239
+ try {
240
+ const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
241
+ const finalizedL1BlockNumber = finalizedL1Block.number;
242
+ const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
243
+ blockNumber: finalizedL1BlockNumber,
244
+ });
245
+ const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
246
+ if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
247
+ await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
248
+ this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
249
+ finalizedCheckpointNumber,
250
+ finalizedL1BlockNumber,
251
+ });
252
+ }
253
+ } catch (err) {
254
+ this.log.warn(`Failed to update finalized checkpoint: ${err}`);
255
+ }
256
+ }
257
+
233
258
  /** Prune all proposed local blocks that should have been checkpointed by now. */
234
259
  private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
235
260
  const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
@@ -249,8 +274,7 @@ export class ArchiverL1Synchronizer implements Traceable {
249
274
  const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
250
275
 
251
276
  // What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
252
- const nextL1BlockTimestamp = currentL1Timestamp + BigInt(this.l1Constants.ethereumSlotDuration);
253
- const slotAtNextL1Block = getSlotAtTimestamp(nextL1BlockTimestamp, this.l1Constants);
277
+ const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
254
278
 
255
279
  // Prune provisional blocks from slots that have ended without being checkpointed
256
280
  if (firstUncheckpointedBlockSlot !== undefined && firstUncheckpointedBlockSlot < slotAtNextL1Block) {
@@ -551,7 +575,7 @@ export class ArchiverL1Synchronizer implements Traceable {
551
575
  if (provenCheckpointNumber === 0) {
552
576
  const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
553
577
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
554
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
578
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
555
579
  this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
556
580
  }
557
581
  }
@@ -583,13 +607,13 @@ export class ArchiverL1Synchronizer implements Traceable {
583
607
  ) {
584
608
  const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
585
609
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
586
- await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
610
+ await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
587
611
  this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
588
612
  const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
589
613
  const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1Constants);
590
614
  const lastBlockNumberInCheckpoint =
591
615
  localCheckpointForDestinationProvenCheckpointNumber.startBlock +
592
- localCheckpointForDestinationProvenCheckpointNumber.numBlocks -
616
+ localCheckpointForDestinationProvenCheckpointNumber.blockCount -
593
617
  1;
594
618
 
595
619
  this.events.emit(L2BlockSourceEvents.L2BlockProven, {
@@ -598,7 +622,7 @@ export class ArchiverL1Synchronizer implements Traceable {
598
622
  slotNumber: provenSlotNumber,
599
623
  epochNumber: provenEpochNumber,
600
624
  });
601
- this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
625
+ this.instrumentation.updateLastProvenCheckpoint(localCheckpointForDestinationProvenCheckpointNumber);
602
626
  } else {
603
627
  this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
604
628
  }
@@ -707,7 +731,6 @@ export class ArchiverL1Synchronizer implements Traceable {
707
731
  this.blobClient,
708
732
  searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
709
733
  searchEndBlock,
710
- this.l1Addresses,
711
734
  this.instrumentation,
712
735
  this.log,
713
736
  !initialSyncComplete, // isHistoricalSync
@@ -802,6 +825,14 @@ export class ArchiverL1Synchronizer implements Traceable {
802
825
  );
803
826
  }
804
827
 
828
+ for (const published of validCheckpoints) {
829
+ this.instrumentation.processCheckpointL1Timing({
830
+ slotNumber: published.checkpoint.header.slotNumber,
831
+ l1Timestamp: published.l1.timestamp,
832
+ l1Constants: this.l1Constants,
833
+ });
834
+ }
835
+
805
836
  try {
806
837
  const updatedValidationResult =
807
838
  rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
@@ -820,7 +851,7 @@ export class ArchiverL1Synchronizer implements Traceable {
820
851
  const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
821
852
  const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
822
853
 
823
- this.log.warn(
854
+ this.log.info(
824
855
  `Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
825
856
  { prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
826
857
  );