@aztec/archiver 0.0.1-commit.86469d5 → 0.0.1-commit.8655d4a

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 (132) hide show
  1. package/README.md +19 -11
  2. package/dest/archiver.d.ts +39 -17
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +260 -160
  5. package/dest/config.d.ts +6 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +23 -15
  8. package/dest/errors.d.ts +55 -9
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +81 -14
  11. package/dest/factory.d.ts +13 -9
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +51 -37
  14. package/dest/index.d.ts +12 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +11 -2
  17. package/dest/l1/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +74 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +201 -260
  21. package/dest/l1/data_retrieval.d.ts +26 -17
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +42 -47
  24. package/dest/l1/spire_proposer.d.ts +5 -5
  25. package/dest/l1/spire_proposer.d.ts.map +1 -1
  26. package/dest/l1/spire_proposer.js +9 -17
  27. package/dest/l1/trace_tx.d.ts +12 -66
  28. package/dest/l1/trace_tx.d.ts.map +1 -1
  29. package/dest/l1/validate_historical_logs.d.ts +23 -0
  30. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  31. package/dest/l1/validate_historical_logs.js +108 -0
  32. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  33. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  34. package/dest/modules/contract_data_source_adapter.js +40 -0
  35. package/dest/modules/data_source_base.d.ts +71 -42
  36. package/dest/modules/data_source_base.d.ts.map +1 -1
  37. package/dest/modules/data_source_base.js +270 -179
  38. package/dest/modules/data_store_updater.d.ts +49 -17
  39. package/dest/modules/data_store_updater.d.ts.map +1 -1
  40. package/dest/modules/data_store_updater.js +211 -121
  41. package/dest/modules/instrumentation.d.ts +21 -3
  42. package/dest/modules/instrumentation.d.ts.map +1 -1
  43. package/dest/modules/instrumentation.js +44 -9
  44. package/dest/modules/l1_synchronizer.d.ts +14 -12
  45. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  46. package/dest/modules/l1_synchronizer.js +443 -211
  47. package/dest/modules/validation.d.ts +4 -3
  48. package/dest/modules/validation.d.ts.map +1 -1
  49. package/dest/modules/validation.js +6 -6
  50. package/dest/store/block_store.d.ts +174 -66
  51. package/dest/store/block_store.d.ts.map +1 -1
  52. package/dest/store/block_store.js +743 -245
  53. package/dest/store/contract_class_store.d.ts +17 -4
  54. package/dest/store/contract_class_store.d.ts.map +1 -1
  55. package/dest/store/contract_class_store.js +24 -68
  56. package/dest/store/contract_instance_store.d.ts +28 -1
  57. package/dest/store/contract_instance_store.d.ts.map +1 -1
  58. package/dest/store/contract_instance_store.js +37 -2
  59. package/dest/store/data_stores.d.ts +68 -0
  60. package/dest/store/data_stores.d.ts.map +1 -0
  61. package/dest/store/data_stores.js +54 -0
  62. package/dest/store/function_names_cache.d.ts +17 -0
  63. package/dest/store/function_names_cache.d.ts.map +1 -0
  64. package/dest/store/function_names_cache.js +30 -0
  65. package/dest/store/l2_tips_cache.d.ts +25 -0
  66. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  67. package/dest/store/l2_tips_cache.js +26 -0
  68. package/dest/store/log_store.d.ts +42 -37
  69. package/dest/store/log_store.d.ts.map +1 -1
  70. package/dest/store/log_store.js +262 -388
  71. package/dest/store/log_store_codec.d.ts +70 -0
  72. package/dest/store/log_store_codec.d.ts.map +1 -0
  73. package/dest/store/log_store_codec.js +101 -0
  74. package/dest/store/message_store.d.ts +11 -1
  75. package/dest/store/message_store.d.ts.map +1 -1
  76. package/dest/store/message_store.js +51 -9
  77. package/dest/test/fake_l1_state.d.ts +25 -1
  78. package/dest/test/fake_l1_state.d.ts.map +1 -1
  79. package/dest/test/fake_l1_state.js +166 -32
  80. package/dest/test/mock_archiver.d.ts +1 -1
  81. package/dest/test/mock_archiver.d.ts.map +1 -1
  82. package/dest/test/mock_archiver.js +3 -2
  83. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  84. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  85. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  86. package/dest/test/mock_l2_block_source.d.ts +62 -41
  87. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  88. package/dest/test/mock_l2_block_source.js +321 -202
  89. package/dest/test/mock_structs.d.ts +4 -1
  90. package/dest/test/mock_structs.d.ts.map +1 -1
  91. package/dest/test/mock_structs.js +13 -1
  92. package/dest/test/noop_l1_archiver.d.ts +12 -6
  93. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  94. package/dest/test/noop_l1_archiver.js +26 -9
  95. package/package.json +14 -14
  96. package/src/archiver.ts +319 -181
  97. package/src/config.ts +32 -12
  98. package/src/errors.ts +122 -21
  99. package/src/factory.ts +75 -36
  100. package/src/index.ts +19 -2
  101. package/src/l1/README.md +25 -68
  102. package/src/l1/bin/retrieve-calldata.ts +46 -39
  103. package/src/l1/calldata_retriever.ts +260 -379
  104. package/src/l1/data_retrieval.ts +58 -69
  105. package/src/l1/spire_proposer.ts +7 -15
  106. package/src/l1/validate_historical_logs.ts +140 -0
  107. package/src/modules/contract_data_source_adapter.ts +55 -0
  108. package/src/modules/data_source_base.ts +347 -221
  109. package/src/modules/data_store_updater.ts +248 -153
  110. package/src/modules/instrumentation.ts +56 -9
  111. package/src/modules/l1_synchronizer.ts +585 -258
  112. package/src/modules/validation.ts +10 -9
  113. package/src/store/block_store.ts +924 -300
  114. package/src/store/contract_class_store.ts +31 -103
  115. package/src/store/contract_instance_store.ts +51 -5
  116. package/src/store/data_stores.ts +104 -0
  117. package/src/store/function_names_cache.ts +37 -0
  118. package/src/store/l2_tips_cache.ts +35 -0
  119. package/src/store/log_store.ts +303 -499
  120. package/src/store/log_store_codec.ts +132 -0
  121. package/src/store/message_store.ts +60 -10
  122. package/src/structs/inbox_message.ts +1 -1
  123. package/src/test/fake_l1_state.ts +213 -42
  124. package/src/test/mock_archiver.ts +3 -2
  125. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  126. package/src/test/mock_l2_block_source.ts +394 -210
  127. package/src/test/mock_structs.ts +20 -6
  128. package/src/test/noop_l1_archiver.ts +39 -9
  129. package/dest/store/kv_archiver_store.d.ts +0 -340
  130. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  131. package/dest/store/kv_archiver_store.js +0 -446
  132. package/src/store/kv_archiver_store.ts +0 -639
@@ -1,7 +1,8 @@
1
1
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
- import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
3
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
4
- import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
2
+ import type { CommitteeAttestation, L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
3
+ import { type L1PublishedData, type ProposedCheckpointInput, type PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
4
+ import type { ArchiverDataStores } from '../store/data_stores.js';
5
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
5
6
  /** Result of adding checkpoints with information about any pruned blocks. */
6
7
  type ReconcileCheckpointsResult = {
7
8
  /** Blocks that were pruned due to conflict with L1 checkpoints. */
@@ -11,32 +12,42 @@ type ReconcileCheckpointsResult = {
11
12
  };
12
13
  /** Archiver helper module to handle updates to the data store. */
13
14
  export declare class ArchiverDataStoreUpdater {
14
- private store;
15
+ private stores;
16
+ private l2TipsCache?;
17
+ private opts;
15
18
  private readonly log;
16
- constructor(store: KVArchiverDataStore);
19
+ constructor(stores: ArchiverDataStores, l2TipsCache?: L2TipsCache | undefined, opts?: {
20
+ rollupManaLimit?: number;
21
+ });
17
22
  /**
18
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
19
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
20
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
21
- * and individually broadcasted functions from the block logs.
23
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
24
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
25
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
22
26
  *
23
- * @param blocks - The proposed L2 blocks to add.
27
+ * @param block - The proposed L2 block to add.
24
28
  * @param pendingChainValidationStatus - Optional validation status to set.
25
29
  * @returns True if the operation is successful.
26
30
  */
27
- addProposedBlocks(blocks: L2Block[], pendingChainValidationStatus?: ValidateCheckpointResult): Promise<boolean>;
31
+ addProposedBlock(block: L2Block, pendingChainValidationStatus?: ValidateCheckpointResult): Promise<boolean>;
28
32
  /**
29
33
  * Reconciles local blocks with incoming checkpoints from L1.
30
34
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
31
35
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
32
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
33
- * and individually broadcasted functions from the checkpoint block logs.
36
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
37
+ * If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
38
+ * as the added checkpoints so both updates are applied atomically.
34
39
  *
35
- * @param checkpoints - The published checkpoints to add.
40
+ * @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
36
41
  * @param pendingChainValidationStatus - Optional validation status to set.
42
+ * @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
37
43
  * @returns Result with information about any pruned blocks.
38
44
  */
39
- addCheckpoints(checkpoints: PublishedCheckpoint[], pendingChainValidationStatus?: ValidateCheckpointResult): Promise<ReconcileCheckpointsResult>;
45
+ addCheckpoints(checkpoints: PublishedCheckpoint[], pendingChainValidationStatus?: ValidateCheckpointResult, promoteProposed?: {
46
+ l1: L1PublishedData;
47
+ attestations: CommitteeAttestation[];
48
+ checkpoint: PublishedCheckpoint;
49
+ }, evictProposedFrom?: CheckpointNumber): Promise<ReconcileCheckpointsResult>;
50
+ addProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput): Promise<void>;
40
51
  private pruneMismatchingLocalBlocks;
41
52
  /**
42
53
  * Removes all uncheckpointed blocks strictly after the specified block number and cleans up associated contract data.
@@ -48,6 +59,18 @@ export declare class ArchiverDataStoreUpdater {
48
59
  * @throws Error if any block to be removed is checkpointed.
49
60
  */
50
61
  removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]>;
62
+ /**
63
+ * Removes all blocks without a proposed checkpoint strictly after the specified block number and cleans up associated contract data.
64
+ * This handles removal of provisionally added blocks along with their contract classes/instances.
65
+ * Verifies that each block being removed is not part of a stored checkpoint (proposed or not).
66
+ * This differs from `removeUncheckpointedBlocksAfter` in that it also checks proposed checkpoints.
67
+ *
68
+ * @param blockNumber - Remove all blocks with number greater than this.
69
+ * @returns The removed blocks.
70
+ * @throws Error if any block to be removed is checkpointed.
71
+ */
72
+ removeBlocksWithoutProposedCheckpointAfter(blockNumber: BlockNumber): Promise<L2Block[]>;
73
+ private evictProposedCheckpointsForPrunedBlocks;
51
74
  private removeBlocksAfter;
52
75
  /**
53
76
  * Removes all checkpoints after the given checkpoint number.
@@ -59,6 +82,16 @@ export declare class ArchiverDataStoreUpdater {
59
82
  * @returns True if the operation is successful.
60
83
  */
61
84
  removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean>;
85
+ /**
86
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
87
+ * @param checkpointNumber - The checkpoint number to set as proven.
88
+ */
89
+ setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void>;
90
+ /**
91
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
92
+ * @param checkpointNumber - The checkpoint number to set as finalized.
93
+ */
94
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void>;
62
95
  /** Extracts and stores contract data from a single block. */
63
96
  private addContractDataToDb;
64
97
  /** Removes contract data associated with a block. */
@@ -67,7 +100,6 @@ export declare class ArchiverDataStoreUpdater {
67
100
  private updatePublishedContractClasses;
68
101
  private updateDeployedContractInstances;
69
102
  private updateUpdatedContractInstances;
70
- private storeBroadcastedIndividualFunctions;
71
103
  }
72
104
  export {};
73
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGF0YV9zdG9yZV91cGRhdGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbW9kdWxlcy9kYXRhX3N0b3JlX3VwZGF0ZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBWWhGLE9BQU8sS0FBSyxFQUFFLE9BQU8sRUFBRSx3QkFBd0IsRUFBRSxNQUFNLHFCQUFxQixDQUFDO0FBQzdFLE9BQU8sS0FBSyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFhcEUsT0FBTyxLQUFLLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQVF6RSw2RUFBNkU7QUFDN0UsS0FBSywwQkFBMEIsR0FBRztJQUNoQyxtRUFBbUU7SUFDbkUsWUFBWSxFQUFFLE9BQU8sRUFBRSxHQUFHLFNBQVMsQ0FBQztJQUNwQyxpRkFBaUY7SUFDakYsOEJBQThCLEVBQUUsV0FBVyxHQUFHLFNBQVMsQ0FBQztDQUN6RCxDQUFDO0FBRUYsa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AED,6DAA6D;IAC7D,OAAO,CAAC,mBAAmB;IAI3B,qDAAqD;IACrD,OAAO,CAAC,wBAAwB;YAKlB,sBAAsB;YAiBtB,8BAA8B;YA+C9B,+BAA+B;YA0C/B,8BAA8B;CAsB7C"}
@@ -1,85 +1,125 @@
1
1
  import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { filterAsync } from '@aztec/foundation/collection';
3
3
  import { createLogger } from '@aztec/foundation/log';
4
- import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
4
+ import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
5
5
  import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
6
- import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
7
- import groupBy from 'lodash.groupby';
6
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
7
+ import { computeContractAddressFromInstance, computeContractClassId } from '@aztec/stdlib/contract';
8
8
  /** Operation type for contract data updates. */ var Operation = /*#__PURE__*/ function(Operation) {
9
9
  Operation[Operation["Store"] = 0] = "Store";
10
10
  Operation[Operation["Delete"] = 1] = "Delete";
11
11
  return Operation;
12
12
  }(Operation || {});
13
13
  /** Archiver helper module to handle updates to the data store. */ export class ArchiverDataStoreUpdater {
14
- store;
14
+ stores;
15
+ l2TipsCache;
16
+ opts;
15
17
  log;
16
- constructor(store){
17
- this.store = store;
18
+ constructor(stores, l2TipsCache, opts = {}){
19
+ this.stores = stores;
20
+ this.l2TipsCache = l2TipsCache;
21
+ this.opts = opts;
18
22
  this.log = createLogger('archiver:store_updater');
19
23
  }
20
24
  /**
21
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
22
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
23
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
24
- * and individually broadcasted functions from the block logs.
25
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
26
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
27
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
25
28
  *
26
- * @param blocks - The proposed L2 blocks to add.
29
+ * @param block - The proposed L2 block to add.
27
30
  * @param pendingChainValidationStatus - Optional validation status to set.
28
31
  * @returns True if the operation is successful.
29
- */ addProposedBlocks(blocks, pendingChainValidationStatus) {
30
- return this.store.transactionAsync(async ()=>{
31
- await this.store.addProposedBlocks(blocks);
32
+ */ async addProposedBlock(block, pendingChainValidationStatus) {
33
+ const result = await this.stores.db.transactionAsync(async ()=>{
34
+ await this.stores.blocks.addProposedBlock(block);
32
35
  const opResults = await Promise.all([
33
36
  // Update the pending chain validation status if provided
34
- pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
35
- // Add any logs emitted during the retrieved blocks
36
- this.store.addLogs(blocks),
37
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
38
- ...blocks.map((block)=>this.addContractDataToDb(block))
37
+ pendingChainValidationStatus && this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
38
+ // Add any logs emitted during the retrieved block
39
+ this.stores.logs.addLogs([
40
+ block
41
+ ]),
42
+ // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
43
+ this.addContractDataToDb(block)
39
44
  ]);
40
45
  return opResults.every(Boolean);
41
46
  });
47
+ await this.l2TipsCache?.refresh();
48
+ return result;
42
49
  }
43
50
  /**
44
51
  * Reconciles local blocks with incoming checkpoints from L1.
45
52
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
46
53
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
47
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
48
- * and individually broadcasted functions from the checkpoint block logs.
54
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
55
+ * If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
56
+ * as the added checkpoints so both updates are applied atomically.
49
57
  *
50
- * @param checkpoints - The published checkpoints to add.
58
+ * @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
51
59
  * @param pendingChainValidationStatus - Optional validation status to set.
60
+ * @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
52
61
  * @returns Result with information about any pruned blocks.
53
- */ addCheckpoints(checkpoints, pendingChainValidationStatus) {
54
- return this.store.transactionAsync(async ()=>{
62
+ */ async addCheckpoints(checkpoints, pendingChainValidationStatus, promoteProposed, evictProposedFrom) {
63
+ for (const checkpoint of checkpoints){
64
+ validateCheckpoint(checkpoint.checkpoint, {
65
+ rollupManaLimit: this.opts?.rollupManaLimit
66
+ });
67
+ }
68
+ if (promoteProposed) {
69
+ validateCheckpoint(promoteProposed.checkpoint.checkpoint, {
70
+ rollupManaLimit: this.opts?.rollupManaLimit
71
+ });
72
+ }
73
+ const result = await this.stores.db.transactionAsync(async ()=>{
55
74
  // Before adding checkpoints, check for conflicts with local blocks if any
56
75
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
57
- await this.store.addCheckpoints(checkpoints);
58
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
76
+ await this.stores.blocks.addCheckpoints(checkpoints);
77
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
59
78
  const newBlocks = checkpoints.flatMap((ch)=>ch.checkpoint.blocks).filter((b)=>lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
60
79
  await Promise.all([
61
80
  // Update the pending chain validation status if provided
62
- pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
81
+ pendingChainValidationStatus && this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
63
82
  // Add any logs emitted during the retrieved blocks
64
- this.store.addLogs(newBlocks),
83
+ this.stores.logs.addLogs(newBlocks),
65
84
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
66
- ...newBlocks.map((block)=>this.addContractDataToDb(block))
85
+ ...newBlocks.map((block)=>this.addContractDataToDb(block)),
86
+ // Promote the proposed checkpoint if requested (uses explicit checkpoint number)
87
+ promoteProposed ? this.stores.blocks.promoteProposedToCheckpointed(promoteProposed.checkpoint.checkpoint.number, promoteProposed.l1, promoteProposed.attestations, promoteProposed.checkpoint.checkpoint.archive.root) : undefined,
88
+ // Evict pending checkpoints that diverged from what L1 mined
89
+ evictProposedFrom !== undefined ? this.stores.blocks.evictProposedCheckpointsFrom(evictProposedFrom) : undefined
67
90
  ]);
68
91
  return {
69
92
  prunedBlocks,
70
93
  lastAlreadyInsertedBlockNumber
71
94
  };
72
95
  });
96
+ await this.l2TipsCache?.refresh();
97
+ return result;
98
+ }
99
+ async addProposedCheckpoint(proposedCheckpoint) {
100
+ const result = await this.stores.db.transactionAsync(async ()=>{
101
+ await this.stores.blocks.addProposedCheckpoint(proposedCheckpoint);
102
+ });
103
+ await this.l2TipsCache?.refresh();
104
+ return result;
73
105
  }
74
106
  /**
75
107
  * Checks for local proposed blocks that do not match the ones to be checkpointed and prunes them.
76
- * This method handles multiple checkpoints but returns after pruning the first conflict found.
77
- * This is correct because pruning from the first conflict point removes all subsequent blocks,
78
- * and when checkpoints are added afterward, they include all the correct blocks.
108
+ * Conflict detection is keyed on `blockNumber`: when a local proposed block and an L1
109
+ * checkpointed block share a block number but live at different slots (e.g. a different proposer
110
+ * mined the same block number one slot earlier), we still treat them as a conflict and prune.
111
+ * The trailing per-checkpoint prune that handles "local has extra trailing blocks within the
112
+ * same slot as the published checkpoint" remains scoped by slot to preserve pipelining: local
113
+ * blocks that live at a later slot than the checkpoint being processed represent speculation
114
+ * atop the just-confirmed tip (and may be referenced by a pending proposed checkpoint), so we
115
+ * leave them in place. This method handles multiple checkpoints but returns after pruning the
116
+ * first conflict found. This is correct because pruning from the first conflict point removes
117
+ * all subsequent blocks, and when checkpoints are added afterward, they include all the correct
118
+ * blocks.
79
119
  */ async pruneMismatchingLocalBlocks(checkpoints) {
80
120
  const [lastCheckpointedBlockNumber, lastBlockNumber] = await Promise.all([
81
- this.store.getCheckpointedL2BlockNumber(),
82
- this.store.getLatestBlockNumber()
121
+ this.stores.blocks.getCheckpointedL2BlockNumber(),
122
+ this.stores.blocks.getLatestL2BlockNumber()
83
123
  ]);
84
124
  // Exit early if there are no local uncheckpointed blocks
85
125
  if (lastBlockNumber === lastCheckpointedBlockNumber) {
@@ -89,22 +129,25 @@ import groupBy from 'lodash.groupby';
89
129
  };
90
130
  }
91
131
  // Get all uncheckpointed local blocks
92
- const uncheckpointedLocalBlocks = await this.store.getBlocks(BlockNumber.add(lastCheckpointedBlockNumber, 1), lastBlockNumber - lastCheckpointedBlockNumber);
132
+ const uncheckpointedLocalBlocks = await this.stores.blocks.getBlocksData({
133
+ from: BlockNumber.add(lastCheckpointedBlockNumber, 1),
134
+ limit: lastBlockNumber - lastCheckpointedBlockNumber
135
+ });
93
136
  let lastAlreadyInsertedBlockNumber;
94
137
  for (const publishedCheckpoint of checkpoints){
95
138
  const checkpointBlocks = publishedCheckpoint.checkpoint.blocks;
96
139
  const slot = publishedCheckpoint.checkpoint.slot;
97
- const localBlocksInSlot = uncheckpointedLocalBlocks.filter((b)=>b.slot === slot);
98
140
  if (checkpointBlocks.length === 0) {
99
141
  this.log.warn(`Checkpoint ${publishedCheckpoint.checkpoint.number} for slot ${slot} has no blocks`);
100
142
  continue;
101
143
  }
102
- // Find the first checkpoint block that conflicts with an existing local block and prune local afterwards
144
+ // Find the first checkpoint block that conflicts with an existing local block and prune local afterwards.
145
+ // Conflict detection joins on block number only — same block number at a different slot is still a conflict.
103
146
  for (const checkpointBlock of checkpointBlocks){
104
147
  const blockNumber = checkpointBlock.number;
105
- const existingBlock = localBlocksInSlot.find((b)=>b.number === blockNumber);
148
+ const existingBlock = uncheckpointedLocalBlocks.find((b)=>b.header.getBlockNumber() === blockNumber);
106
149
  const blockInfos = {
107
- existingBlock: existingBlock?.toBlockInfo(),
150
+ existingBlock: existingBlock?.header.toInspect(),
108
151
  checkpointBlock: checkpointBlock.toBlockInfo()
109
152
  };
110
153
  if (!existingBlock) {
@@ -113,21 +156,25 @@ import groupBy from 'lodash.groupby';
113
156
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
114
157
  lastAlreadyInsertedBlockNumber = blockNumber;
115
158
  } else {
116
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
159
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
117
160
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
161
+ await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
118
162
  return {
119
163
  prunedBlocks,
120
164
  lastAlreadyInsertedBlockNumber
121
165
  };
122
166
  }
123
167
  }
124
- // If local has more blocks than the checkpoint (e.g., local has [2,3,4] but checkpoint has [2,3]),
125
- // we need to prune the extra local blocks so they match what was checkpointed
168
+ // If the sequencer locally proposed extra blocks within this checkpoint's slot (e.g. local has
169
+ // [N, N+1] but L1 confirmed just [N]), prune the extras. Scoped to the checkpoint's slot so we
170
+ // do not throw away speculative blocks at later slots that belong to a pending proposed checkpoint.
126
171
  const lastCheckpointBlockNumber = checkpointBlocks.at(-1).number;
127
- const lastLocalBlockNumber = localBlocksInSlot.at(-1)?.number;
172
+ const localBlocksInSlot = uncheckpointedLocalBlocks.filter((b)=>b.header.getSlot() === slot);
173
+ const lastLocalBlockNumber = localBlocksInSlot.at(-1)?.header.getBlockNumber();
128
174
  if (lastLocalBlockNumber !== undefined && lastLocalBlockNumber > lastCheckpointBlockNumber) {
129
175
  this.log.warn(`Local chain for slot ${slot} ends at block ${lastLocalBlockNumber} but checkpoint ends at ${lastCheckpointBlockNumber}. Pruning blocks after block ${lastCheckpointBlockNumber}.`);
130
176
  const prunedBlocks = await this.removeBlocksAfter(lastCheckpointBlockNumber);
177
+ await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
131
178
  return {
132
179
  prunedBlocks,
133
180
  lastAlreadyInsertedBlockNumber
@@ -147,25 +194,61 @@ import groupBy from 'lodash.groupby';
147
194
  * @param blockNumber - Remove all blocks with number greater than this.
148
195
  * @returns The removed blocks.
149
196
  * @throws Error if any block to be removed is checkpointed.
150
- */ removeUncheckpointedBlocksAfter(blockNumber) {
151
- return this.store.transactionAsync(async ()=>{
197
+ */ async removeUncheckpointedBlocksAfter(blockNumber) {
198
+ const result = await this.stores.db.transactionAsync(async ()=>{
152
199
  // Verify we're only removing uncheckpointed blocks
153
- const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
200
+ const lastCheckpointedBlockNumber = await this.stores.blocks.getCheckpointedL2BlockNumber();
154
201
  if (blockNumber < lastCheckpointedBlockNumber) {
155
202
  throw new Error(`Cannot remove blocks after ${blockNumber} because checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`);
156
203
  }
204
+ const prunedBlocks = await this.removeBlocksAfter(blockNumber);
205
+ await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
206
+ return prunedBlocks;
207
+ });
208
+ await this.l2TipsCache?.refresh();
209
+ return result;
210
+ }
211
+ /**
212
+ * Removes all blocks without a proposed checkpoint strictly after the specified block number and cleans up associated contract data.
213
+ * This handles removal of provisionally added blocks along with their contract classes/instances.
214
+ * Verifies that each block being removed is not part of a stored checkpoint (proposed or not).
215
+ * This differs from `removeUncheckpointedBlocksAfter` in that it also checks proposed checkpoints.
216
+ *
217
+ * @param blockNumber - Remove all blocks with number greater than this.
218
+ * @returns The removed blocks.
219
+ * @throws Error if any block to be removed is checkpointed.
220
+ */ async removeBlocksWithoutProposedCheckpointAfter(blockNumber) {
221
+ const result = await this.stores.db.transactionAsync(async ()=>{
222
+ // Verify we're only removing uncheckpointed blocks
223
+ const lastCheckpointedBlockNumber = await this.stores.blocks.getProposedCheckpointL2BlockNumber();
224
+ if (blockNumber < lastCheckpointedBlockNumber) {
225
+ throw new Error(`Cannot remove blocks after ${blockNumber} because proposed checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`);
226
+ }
157
227
  return await this.removeBlocksAfter(blockNumber);
158
228
  });
229
+ await this.l2TipsCache?.refresh();
230
+ return result;
231
+ }
232
+ /**
233
+ * Evicts pending proposed checkpoints that referenced any of the just-pruned blocks. Pruned
234
+ * blocks invalidate all proposed checkpoints from the lowest pruned block's checkpoint number
235
+ * onwards: those checkpoints either reference the pruned blocks directly or chain off them.
236
+ */ async evictProposedCheckpointsForPrunedBlocks(prunedBlocks) {
237
+ if (prunedBlocks.length === 0) {
238
+ return;
239
+ }
240
+ const fromCheckpointNumber = prunedBlocks[0].checkpointNumber;
241
+ await this.stores.blocks.evictProposedCheckpointsFrom(fromCheckpointNumber);
159
242
  }
160
243
  /**
161
244
  * Removes all blocks strictly after the given block number along with any logs and contract data.
162
245
  * Does not remove their checkpoints.
163
246
  */ async removeBlocksAfter(blockNumber) {
164
247
  // First get the blocks to be removed so we can clean up contract data
165
- const removedBlocks = await this.store.removeBlocksAfter(blockNumber);
248
+ const removedBlocks = await this.stores.blocks.removeBlocksAfter(blockNumber);
166
249
  // Clean up contract data and logs for the removed blocks
167
250
  await Promise.all([
168
- this.store.deleteLogs(removedBlocks),
251
+ this.stores.logs.deleteLogs(removedBlocks),
169
252
  ...removedBlocks.map((block)=>this.removeContractDataFromDb(block))
170
253
  ]);
171
254
  return removedBlocks;
@@ -179,17 +262,39 @@ import groupBy from 'lodash.groupby';
179
262
  * @param checkpointNumber - Remove all checkpoints strictly after this one.
180
263
  * @returns True if the operation is successful.
181
264
  */ async removeCheckpointsAfter(checkpointNumber) {
182
- const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
183
- const opResults = await Promise.all([
184
- // Prune rolls back to the last proven block, which is by definition valid
185
- this.store.setPendingChainValidationStatus({
186
- valid: true
187
- }),
188
- // Remove contract data for all blocks being removed
189
- ...blocksRemoved.map((block)=>this.removeContractDataFromDb(block)),
190
- this.store.deleteLogs(blocksRemoved)
191
- ]);
192
- return opResults.every(Boolean);
265
+ const result = await this.stores.db.transactionAsync(async ()=>{
266
+ const { blocksRemoved = [] } = await this.stores.blocks.removeCheckpointsAfter(checkpointNumber);
267
+ const opResults = await Promise.all([
268
+ // Prune rolls back to the last proven block, which is by definition valid
269
+ this.stores.blocks.setPendingChainValidationStatus({
270
+ valid: true
271
+ }),
272
+ // Remove contract data for all blocks being removed
273
+ ...blocksRemoved.map((block)=>this.removeContractDataFromDb(block)),
274
+ this.stores.logs.deleteLogs(blocksRemoved)
275
+ ]);
276
+ return opResults.every(Boolean);
277
+ });
278
+ await this.l2TipsCache?.refresh();
279
+ return result;
280
+ }
281
+ /**
282
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
283
+ * @param checkpointNumber - The checkpoint number to set as proven.
284
+ */ async setProvenCheckpointNumber(checkpointNumber) {
285
+ await this.stores.db.transactionAsync(async ()=>{
286
+ await this.stores.blocks.setProvenCheckpointNumber(checkpointNumber);
287
+ });
288
+ await this.l2TipsCache?.refresh();
289
+ }
290
+ /**
291
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
292
+ * @param checkpointNumber - The checkpoint number to set as finalized.
293
+ */ async setFinalizedCheckpointNumber(checkpointNumber) {
294
+ await this.stores.db.transactionAsync(async ()=>{
295
+ await this.stores.blocks.setFinalizedCheckpointNumber(checkpointNumber);
296
+ });
297
+ await this.l2TipsCache?.refresh();
193
298
  }
194
299
  /** Extracts and stores contract data from a single block. */ addContractDataToDb(block) {
195
300
  return this.updateContractDataOnDb(block, 0);
@@ -204,37 +309,68 @@ import groupBy from 'lodash.groupby';
204
309
  return (await Promise.all([
205
310
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
206
311
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
207
- this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
208
- operation === 0 ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number) : Promise.resolve(true)
312
+ this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation)
209
313
  ])).every(Boolean);
210
314
  }
211
315
  /**
212
316
  * Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
213
317
  */ async updatePublishedContractClasses(allLogs, blockNum, operation) {
214
318
  const contractClassPublishedEvents = allLogs.filter((log)=>ContractClassPublishedEvent.isContractClassPublishedEvent(log)).map((log)=>ContractClassPublishedEvent.fromLog(log));
215
- const contractClasses = await Promise.all(contractClassPublishedEvents.map((e)=>e.toContractClassPublic()));
319
+ if (operation == 1) {
320
+ const contractClasses = contractClassPublishedEvents.map((e)=>e.toContractClassPublic());
321
+ if (contractClasses.length > 0) {
322
+ contractClasses.forEach((c)=>this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
323
+ return await this.stores.contractClasses.deleteContractClasses(contractClasses, blockNum);
324
+ }
325
+ return true;
326
+ }
327
+ // Compute bytecode commitments and validate class IDs in a single pass.
328
+ const contractClasses = [];
329
+ for (const event of contractClassPublishedEvents){
330
+ const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
331
+ const computedClassId = await computeContractClassId({
332
+ artifactHash: contractClass.artifactHash,
333
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
334
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment
335
+ });
336
+ if (!computedClassId.equals(contractClass.id)) {
337
+ this.log.warn(`Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`, {
338
+ blockNum,
339
+ contractClassId: event.contractClassId.toString()
340
+ });
341
+ continue;
342
+ }
343
+ contractClasses.push(contractClass);
344
+ }
216
345
  if (contractClasses.length > 0) {
217
346
  contractClasses.forEach((c)=>this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
218
- if (operation == 0) {
219
- // TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
220
- const commitments = await Promise.all(contractClasses.map((c)=>computePublicBytecodeCommitment(c.packedBytecode)));
221
- return await this.store.addContractClasses(contractClasses, commitments, blockNum);
222
- } else if (operation == 1) {
223
- return await this.store.deleteContractClasses(contractClasses, blockNum);
224
- }
347
+ return await this.stores.contractClasses.addContractClasses(contractClasses, blockNum);
225
348
  }
226
349
  return true;
227
350
  }
228
351
  /**
229
352
  * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
230
353
  */ async updateDeployedContractInstances(allLogs, blockNum, operation) {
231
- const contractInstances = allLogs.filter((log)=>ContractInstancePublishedEvent.isContractInstancePublishedEvent(log)).map((log)=>ContractInstancePublishedEvent.fromLog(log)).map((e)=>e.toContractInstance());
354
+ const allInstances = allLogs.filter((log)=>ContractInstancePublishedEvent.isContractInstancePublishedEvent(log)).map((log)=>ContractInstancePublishedEvent.fromLog(log)).map((e)=>e.toContractInstance());
355
+ // Verify that each instance's address matches the one derived from its fields if we're adding
356
+ const contractInstances = operation === 1 ? allInstances : await filterAsync(allInstances, async (instance)=>{
357
+ const computedAddress = await computeContractAddressFromInstance(instance);
358
+ if (!computedAddress.equals(instance.address)) {
359
+ this.log.warn(`Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`, {
360
+ instanceAddress: instance.address.toString(),
361
+ computedAddress: computedAddress.toString(),
362
+ blockNum
363
+ });
364
+ return false;
365
+ }
366
+ return true;
367
+ });
232
368
  if (contractInstances.length > 0) {
233
369
  contractInstances.forEach((c)=>this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`));
234
370
  if (operation == 0) {
235
- return await this.store.addContractInstances(contractInstances, blockNum);
371
+ return await this.stores.contractInstances.addContractInstances(contractInstances, blockNum);
236
372
  } else if (operation == 1) {
237
- return await this.store.deleteContractInstances(contractInstances, blockNum);
373
+ return await this.stores.contractInstances.deleteContractInstances(contractInstances);
238
374
  }
239
375
  }
240
376
  return true;
@@ -246,56 +382,10 @@ import groupBy from 'lodash.groupby';
246
382
  if (contractUpdates.length > 0) {
247
383
  contractUpdates.forEach((c)=>this.log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`));
248
384
  if (operation == 0) {
249
- return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
385
+ return await this.stores.contractInstances.addContractInstanceUpdates(contractUpdates, timestamp);
250
386
  } else if (operation == 1) {
251
- return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
252
- }
253
- }
254
- return true;
255
- }
256
- /**
257
- * Stores the functions that were broadcasted individually.
258
- *
259
- * @dev Beware that there is not a delete variant of this, since they are added to contract classes
260
- * and will be deleted as part of the class if needed.
261
- */ async storeBroadcastedIndividualFunctions(allLogs, _blockNum) {
262
- // Filter out private and utility function broadcast events
263
- const privateFnEvents = allLogs.filter((log)=>PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log)).map((log)=>PrivateFunctionBroadcastedEvent.fromLog(log));
264
- const utilityFnEvents = allLogs.filter((log)=>UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log)).map((log)=>UtilityFunctionBroadcastedEvent.fromLog(log));
265
- // Group all events by contract class id
266
- for (const [classIdString, classEvents] of Object.entries(groupBy([
267
- ...privateFnEvents,
268
- ...utilityFnEvents
269
- ], (e)=>e.contractClassId.toString()))){
270
- const contractClassId = Fr.fromHexString(classIdString);
271
- const contractClass = await this.store.getContractClass(contractClassId);
272
- if (!contractClass) {
273
- this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
274
- continue;
275
- }
276
- // Split private and utility functions, and filter out invalid ones
277
- const allFns = classEvents.map((e)=>e.toFunctionWithMembershipProof());
278
- const privateFns = allFns.filter((fn)=>'utilityFunctionsTreeRoot' in fn);
279
- const utilityFns = allFns.filter((fn)=>'privateFunctionsArtifactTreeRoot' in fn);
280
- const privateFunctionsWithValidity = await Promise.all(privateFns.map(async (fn)=>({
281
- fn,
282
- valid: await isValidPrivateFunctionMembershipProof(fn, contractClass)
283
- })));
284
- const validPrivateFns = privateFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
285
- const utilityFunctionsWithValidity = await Promise.all(utilityFns.map(async (fn)=>({
286
- fn,
287
- valid: await isValidUtilityFunctionMembershipProof(fn, contractClass)
288
- })));
289
- const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
290
- const validFnCount = validPrivateFns.length + validUtilityFns.length;
291
- if (validFnCount !== allFns.length) {
292
- this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
293
- }
294
- // Store the functions in the contract class in a single operation
295
- if (validFnCount > 0) {
296
- this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
387
+ return await this.stores.contractInstances.deleteContractInstanceUpdates(contractUpdates, timestamp);
297
388
  }
298
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
299
389
  }
300
390
  return true;
301
391
  }