@aztec/archiver 0.0.1-commit.29c6b1a3 → 0.0.1-commit.2c0ee1788

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 (134) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +26 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +169 -148
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +16 -4
  8. package/dest/errors.d.ts +61 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -14
  11. package/dest/factory.d.ts +6 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +41 -34
  14. package/dest/index.d.ts +11 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +10 -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 +197 -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 +43 -48
  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/validate_historical_logs.d.ts +23 -0
  28. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  29. package/dest/l1/validate_historical_logs.js +108 -0
  30. package/dest/l1/validate_trace.d.ts +6 -3
  31. package/dest/l1/validate_trace.d.ts.map +1 -1
  32. package/dest/l1/validate_trace.js +13 -9
  33. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  34. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  35. package/dest/modules/contract_data_source_adapter.js +42 -0
  36. package/dest/modules/data_source_base.d.ts +30 -17
  37. package/dest/modules/data_source_base.d.ts.map +1 -1
  38. package/dest/modules/data_source_base.js +98 -125
  39. package/dest/modules/data_store_updater.d.ts +37 -17
  40. package/dest/modules/data_store_updater.d.ts.map +1 -1
  41. package/dest/modules/data_store_updater.js +155 -112
  42. package/dest/modules/instrumentation.d.ts +21 -3
  43. package/dest/modules/instrumentation.d.ts.map +1 -1
  44. package/dest/modules/instrumentation.js +58 -18
  45. package/dest/modules/l1_synchronizer.d.ts +13 -11
  46. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  47. package/dest/modules/l1_synchronizer.js +334 -181
  48. package/dest/modules/validation.d.ts +4 -3
  49. package/dest/modules/validation.d.ts.map +1 -1
  50. package/dest/modules/validation.js +6 -6
  51. package/dest/store/block_store.d.ts +108 -32
  52. package/dest/store/block_store.d.ts.map +1 -1
  53. package/dest/store/block_store.js +477 -141
  54. package/dest/store/contract_class_store.d.ts +17 -4
  55. package/dest/store/contract_class_store.d.ts.map +1 -1
  56. package/dest/store/contract_class_store.js +33 -73
  57. package/dest/store/contract_instance_store.d.ts +28 -1
  58. package/dest/store/contract_instance_store.d.ts.map +1 -1
  59. package/dest/store/contract_instance_store.js +37 -2
  60. package/dest/store/data_stores.d.ts +68 -0
  61. package/dest/store/data_stores.d.ts.map +1 -0
  62. package/dest/store/data_stores.js +50 -0
  63. package/dest/store/function_names_cache.d.ts +17 -0
  64. package/dest/store/function_names_cache.d.ts.map +1 -0
  65. package/dest/store/function_names_cache.js +30 -0
  66. package/dest/store/l2_tips_cache.d.ts +20 -0
  67. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  68. package/dest/store/l2_tips_cache.js +109 -0
  69. package/dest/store/log_store.d.ts +6 -3
  70. package/dest/store/log_store.d.ts.map +1 -1
  71. package/dest/store/log_store.js +150 -55
  72. package/dest/store/message_store.d.ts +5 -1
  73. package/dest/store/message_store.d.ts.map +1 -1
  74. package/dest/store/message_store.js +21 -9
  75. package/dest/test/fake_l1_state.d.ts +25 -1
  76. package/dest/test/fake_l1_state.d.ts.map +1 -1
  77. package/dest/test/fake_l1_state.js +166 -32
  78. package/dest/test/index.d.ts +1 -2
  79. package/dest/test/index.d.ts.map +1 -1
  80. package/dest/test/index.js +3 -2
  81. package/dest/test/mock_archiver.d.ts +1 -1
  82. package/dest/test/mock_archiver.d.ts.map +1 -1
  83. package/dest/test/mock_archiver.js +3 -2
  84. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  85. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  86. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  87. package/dest/test/mock_l2_block_source.d.ts +38 -8
  88. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  89. package/dest/test/mock_l2_block_source.js +183 -90
  90. package/dest/test/mock_structs.d.ts +4 -1
  91. package/dest/test/mock_structs.d.ts.map +1 -1
  92. package/dest/test/mock_structs.js +13 -1
  93. package/dest/test/noop_l1_archiver.d.ts +7 -4
  94. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  95. package/dest/test/noop_l1_archiver.js +14 -8
  96. package/package.json +14 -13
  97. package/src/archiver.ts +215 -170
  98. package/src/config.ts +23 -2
  99. package/src/errors.ts +133 -22
  100. package/src/factory.ts +54 -32
  101. package/src/index.ts +18 -2
  102. package/src/l1/README.md +25 -68
  103. package/src/l1/bin/retrieve-calldata.ts +46 -39
  104. package/src/l1/calldata_retriever.ts +261 -379
  105. package/src/l1/data_retrieval.ts +59 -70
  106. package/src/l1/spire_proposer.ts +7 -15
  107. package/src/l1/validate_historical_logs.ts +140 -0
  108. package/src/l1/validate_trace.ts +24 -6
  109. package/src/modules/contract_data_source_adapter.ts +59 -0
  110. package/src/modules/data_source_base.ts +145 -147
  111. package/src/modules/data_store_updater.ts +187 -141
  112. package/src/modules/instrumentation.ts +71 -19
  113. package/src/modules/l1_synchronizer.ts +419 -219
  114. package/src/modules/validation.ts +10 -9
  115. package/src/store/block_store.ts +589 -179
  116. package/src/store/contract_class_store.ts +39 -107
  117. package/src/store/contract_instance_store.ts +51 -5
  118. package/src/store/data_stores.ts +108 -0
  119. package/src/store/function_names_cache.ts +37 -0
  120. package/src/store/l2_tips_cache.ts +134 -0
  121. package/src/store/log_store.ts +222 -64
  122. package/src/store/message_store.ts +27 -10
  123. package/src/structs/inbox_message.ts +1 -1
  124. package/src/test/fake_l1_state.ts +213 -42
  125. package/src/test/index.ts +3 -1
  126. package/src/test/mock_archiver.ts +3 -2
  127. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  128. package/src/test/mock_l2_block_source.ts +235 -87
  129. package/src/test/mock_structs.ts +20 -6
  130. package/src/test/noop_l1_archiver.ts +16 -8
  131. package/dest/store/kv_archiver_store.d.ts +0 -340
  132. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  133. package/dest/store/kv_archiver_store.js +0 -447
  134. package/src/store/kv_archiver_store.ts +0 -639
@@ -2,14 +2,11 @@ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { toArray } from '@aztec/foundation/iterable';
3
3
  import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
4
4
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
5
- import { FunctionSelector } from '@aztec/stdlib/abi';
6
5
  import type {
7
6
  ContractClassPublic,
8
7
  ContractClassPublicWithBlockNumber,
9
- ExecutablePrivateFunctionWithMembershipProof,
10
- UtilityFunctionWithMembershipProof,
8
+ ContractClassPublicWithCommitment,
11
9
  } from '@aztec/stdlib/contract';
12
- import { Vector } from '@aztec/stdlib/types';
13
10
 
14
11
  /**
15
12
  * LMDB-based contract class storage for the archiver.
@@ -23,23 +20,53 @@ export class ContractClassStore {
23
20
  this.#bytecodeCommitments = db.openMap('archiver_bytecode_commitments');
24
21
  }
25
22
 
23
+ /**
24
+ * Adds multiple contract classes to the store.
25
+ * @param data - Contract classes (with bytecode commitments) to add.
26
+ * @param blockNumber - L2 block number where the classes were registered.
27
+ * @returns True if every insert succeeded.
28
+ */
29
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: number): Promise<boolean> {
30
+ return (await Promise.all(data.map(c => this.addContractClass(c, c.publicBytecodeCommitment, blockNumber)))).every(
31
+ Boolean,
32
+ );
33
+ }
34
+
35
+ /**
36
+ * Removes multiple contract classes from the store, but only if they were registered at or after the given block.
37
+ * @param data - Contract classes to delete.
38
+ * @param blockNumber - Lower bound on the block number at which the classes were registered.
39
+ * @returns True if every delete succeeded.
40
+ */
41
+ async deleteContractClasses(data: ContractClassPublic[], blockNumber: number): Promise<boolean> {
42
+ return (await Promise.all(data.map(c => this.deleteContractClass(c, blockNumber)))).every(Boolean);
43
+ }
44
+
26
45
  async addContractClass(
27
46
  contractClass: ContractClassPublic,
28
47
  bytecodeCommitment: Fr,
29
48
  blockNumber: number,
30
49
  ): Promise<void> {
31
- await this.#contractClasses.setIfNotExists(
32
- contractClass.id.toString(),
33
- serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
34
- );
35
- await this.#bytecodeCommitments.setIfNotExists(contractClass.id.toString(), bytecodeCommitment.toBuffer());
50
+ await this.db.transactionAsync(async () => {
51
+ const key = contractClass.id.toString();
52
+ if (await this.#contractClasses.hasAsync(key)) {
53
+ throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
54
+ }
55
+ await this.#contractClasses.set(
56
+ key,
57
+ serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
58
+ );
59
+ await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
60
+ });
36
61
  }
37
62
 
38
- async deleteContractClasses(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
63
+ async deleteContractClass(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
39
64
  const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
40
65
  if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
41
- await this.#contractClasses.delete(contractClass.id.toString());
42
- await this.#bytecodeCommitments.delete(contractClass.id.toString());
66
+ await this.db.transactionAsync(async () => {
67
+ await this.#contractClasses.delete(contractClass.id.toString());
68
+ await this.#bytecodeCommitments.delete(contractClass.id.toString());
69
+ });
43
70
  }
44
71
  }
45
72
 
@@ -56,37 +83,6 @@ export class ContractClassStore {
56
83
  async getContractClassIds(): Promise<Fr[]> {
57
84
  return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
58
85
  }
59
-
60
- async addFunctions(
61
- contractClassId: Fr,
62
- newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
63
- newUtilityFunctions: UtilityFunctionWithMembershipProof[],
64
- ): Promise<boolean> {
65
- await this.db.transactionAsync(async () => {
66
- const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
67
- if (!existingClassBuffer) {
68
- throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
69
- }
70
-
71
- const existingClass = deserializeContractClassPublic(existingClassBuffer);
72
- const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
73
-
74
- const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
75
- ...existingClass,
76
- privateFunctions: [
77
- ...existingPrivateFns,
78
- ...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
79
- ],
80
- utilityFunctions: [
81
- ...existingUtilityFns,
82
- ...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
83
- ],
84
- };
85
- await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
86
- });
87
-
88
- return true;
89
- }
90
86
  }
91
87
 
92
88
  function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
@@ -94,83 +90,19 @@ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWit
94
90
  contractClass.l2BlockNumber,
95
91
  numToUInt8(contractClass.version),
96
92
  contractClass.artifactHash,
97
- contractClass.privateFunctions.length,
98
- contractClass.privateFunctions.map(serializePrivateFunction),
99
- contractClass.utilityFunctions.length,
100
- contractClass.utilityFunctions.map(serializeUtilityFunction),
101
93
  contractClass.packedBytecode.length,
102
94
  contractClass.packedBytecode,
103
95
  contractClass.privateFunctionsRoot,
104
96
  );
105
97
  }
106
98
 
107
- function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
108
- return serializeToBuffer(
109
- fn.selector,
110
- fn.vkHash,
111
- fn.bytecode.length,
112
- fn.bytecode,
113
- fn.functionMetadataHash,
114
- fn.artifactMetadataHash,
115
- fn.utilityFunctionsTreeRoot,
116
- new Vector(fn.privateFunctionTreeSiblingPath),
117
- fn.privateFunctionTreeLeafIndex,
118
- new Vector(fn.artifactTreeSiblingPath),
119
- fn.artifactTreeLeafIndex,
120
- );
121
- }
122
-
123
- function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
124
- return serializeToBuffer(
125
- fn.selector,
126
- fn.bytecode.length,
127
- fn.bytecode,
128
- fn.functionMetadataHash,
129
- fn.artifactMetadataHash,
130
- fn.privateFunctionsArtifactTreeRoot,
131
- new Vector(fn.artifactTreeSiblingPath),
132
- fn.artifactTreeLeafIndex,
133
- );
134
- }
135
-
136
99
  function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
137
100
  const reader = BufferReader.asReader(buffer);
138
101
  return {
139
102
  l2BlockNumber: reader.readNumber(),
140
103
  version: reader.readUInt8() as 1,
141
104
  artifactHash: reader.readObject(Fr),
142
- privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
143
- utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
144
105
  packedBytecode: reader.readBuffer(),
145
106
  privateFunctionsRoot: reader.readObject(Fr),
146
107
  };
147
108
  }
148
-
149
- function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
150
- const reader = BufferReader.asReader(buffer);
151
- return {
152
- selector: reader.readObject(FunctionSelector),
153
- vkHash: reader.readObject(Fr),
154
- bytecode: reader.readBuffer(),
155
- functionMetadataHash: reader.readObject(Fr),
156
- artifactMetadataHash: reader.readObject(Fr),
157
- utilityFunctionsTreeRoot: reader.readObject(Fr),
158
- privateFunctionTreeSiblingPath: reader.readVector(Fr),
159
- privateFunctionTreeLeafIndex: reader.readNumber(),
160
- artifactTreeSiblingPath: reader.readVector(Fr),
161
- artifactTreeLeafIndex: reader.readNumber(),
162
- };
163
- }
164
-
165
- function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
166
- const reader = BufferReader.asReader(buffer);
167
- return {
168
- selector: reader.readObject(FunctionSelector),
169
- bytecode: reader.readBuffer(),
170
- functionMetadataHash: reader.readObject(Fr),
171
- artifactMetadataHash: reader.readObject(Fr),
172
- privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
173
- artifactTreeSiblingPath: reader.readVector(Fr),
174
- artifactTreeLeafIndex: reader.readNumber(),
175
- };
176
- }
@@ -25,13 +25,59 @@ export class ContractInstanceStore {
25
25
  this.#contractInstanceUpdates = db.openMap('archiver_contract_instance_updates');
26
26
  }
27
27
 
28
+ /**
29
+ * Adds multiple contract instances to the store.
30
+ * @param data - Contract instances to add.
31
+ * @param blockNumber - L2 block number where the instances were deployed.
32
+ * @returns True if every insert succeeded.
33
+ */
34
+ async addContractInstances(data: ContractInstanceWithAddress[], blockNumber: number): Promise<boolean> {
35
+ return (await Promise.all(data.map(c => this.addContractInstance(c, blockNumber)))).every(Boolean);
36
+ }
37
+
38
+ /**
39
+ * Removes multiple contract instances from the store.
40
+ * @param data - Contract instances to delete.
41
+ * @returns True if every delete succeeded.
42
+ */
43
+ async deleteContractInstances(data: ContractInstanceWithAddress[]): Promise<boolean> {
44
+ return (await Promise.all(data.map(c => this.deleteContractInstance(c)))).every(Boolean);
45
+ }
46
+
47
+ /**
48
+ * Adds multiple contract instance updates to the store.
49
+ * @param data - Contract instance updates to add.
50
+ * @param timestamp - Timestamp at which the updates were scheduled.
51
+ * @returns True if every insert succeeded.
52
+ */
53
+ async addContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
54
+ return (
55
+ await Promise.all(data.map((update, logIndex) => this.addContractInstanceUpdate(update, timestamp, logIndex)))
56
+ ).every(Boolean);
57
+ }
58
+
59
+ /**
60
+ * Removes multiple contract instance updates from the store.
61
+ * @param data - Contract instance updates to delete.
62
+ * @param timestamp - Timestamp at which the updates were scheduled.
63
+ * @returns True if every delete succeeded.
64
+ */
65
+ async deleteContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
66
+ return (
67
+ await Promise.all(data.map((update, logIndex) => this.deleteContractInstanceUpdate(update, timestamp, logIndex)))
68
+ ).every(Boolean);
69
+ }
70
+
28
71
  addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
29
72
  return this.db.transactionAsync(async () => {
30
- await this.#contractInstances.set(
31
- contractInstance.address.toString(),
32
- new SerializableContractInstance(contractInstance).toBuffer(),
33
- );
34
- await this.#contractInstancePublishedAt.set(contractInstance.address.toString(), blockNumber);
73
+ const key = contractInstance.address.toString();
74
+ if (await this.#contractInstances.hasAsync(key)) {
75
+ throw new Error(
76
+ `Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
77
+ );
78
+ }
79
+ await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
80
+ await this.#contractInstancePublishedAt.set(key, blockNumber);
35
81
  });
36
82
  }
37
83
 
@@ -0,0 +1,108 @@
1
+ import type { L1BlockId } from '@aztec/ethereum/l1-types';
2
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
3
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
4
+
5
+ import { join } from 'path';
6
+
7
+ import { ArchiverContractDataSourceAdapter } from '../modules/contract_data_source_adapter.js';
8
+ import { BlockStore } from './block_store.js';
9
+ import { ContractClassStore } from './contract_class_store.js';
10
+ import { ContractInstanceStore } from './contract_instance_store.js';
11
+ import { FunctionNamesCache } from './function_names_cache.js';
12
+ import { LogStore } from './log_store.js';
13
+ import { MessageStore } from './message_store.js';
14
+
15
+ export const ARCHIVER_DB_VERSION = 6;
16
+
17
+ /**
18
+ * Represents the latest L1 block processed by the archiver for various objects in L2.
19
+ */
20
+ export type ArchiverL1SynchPoint = {
21
+ /** Number of the last L1 block that added a new L2 checkpoint metadata. */
22
+ blocksSynchedTo?: bigint;
23
+ /** Last L1 block checked for L1 to L2 messages. */
24
+ messagesSynchedTo?: L1BlockId;
25
+ };
26
+
27
+ /**
28
+ * Bundle of archiver-owned LMDB substores plus the in-memory caches that span them.
29
+ *
30
+ * Replaces the former `KVArchiverDataStore` pass-through wrapper. Callers reach into
31
+ * the relevant substore directly (e.g. `stores.blocks.getBlock`) and use
32
+ * {@link createArchiverDataStores} to wire them up against a shared KV store.
33
+ */
34
+ export type ArchiverDataStores = {
35
+ /** The underlying key-value store. Use {@link AztecAsyncKVStore.transactionAsync} to compose updates atomically. */
36
+ db: AztecAsyncKVStore;
37
+ /** Blocks, checkpoints, tx effects, proven/finalized state. */
38
+ blocks: BlockStore;
39
+ /** Public, private and contract class logs. */
40
+ logs: LogStore;
41
+ /** L1 to L2 messages and message sync state. */
42
+ messages: MessageStore;
43
+ /** Contract classes (with bytecode commitments). */
44
+ contractClasses: ContractClassStore;
45
+ /** Contract instances and contract instance updates. */
46
+ contractInstances: ContractInstanceStore;
47
+ /** In-memory cache of public function selectors -> names. */
48
+ functionNames: FunctionNamesCache;
49
+ };
50
+
51
+ /** Options used by {@link createArchiverDataStores}. */
52
+ export type CreateArchiverDataStoresOptions = {
53
+ /** Maximum number of logs returned per page when paginating tagged log queries. */
54
+ logsMaxPageSize?: number;
55
+ };
56
+
57
+ /**
58
+ * Wires up the archiver substores against a shared KV store and returns the
59
+ * {@link ArchiverDataStores} bundle.
60
+ */
61
+ export function createArchiverDataStores(
62
+ db: AztecAsyncKVStore,
63
+ opts: CreateArchiverDataStoresOptions = {},
64
+ ): ArchiverDataStores {
65
+ const blocks = new BlockStore(db);
66
+ return {
67
+ db,
68
+ blocks,
69
+ logs: new LogStore(db, blocks, opts.logsMaxPageSize ?? 1000),
70
+ messages: new MessageStore(db),
71
+ contractClasses: new ContractClassStore(db),
72
+ contractInstances: new ContractInstanceStore(db),
73
+ functionNames: new FunctionNamesCache(),
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Returns the L1 sync point of the archiver, combining the block sync point from {@link BlockStore}
79
+ * and the message sync point from {@link MessageStore}.
80
+ */
81
+ export async function getArchiverSynchPoint(stores: ArchiverDataStores): Promise<ArchiverL1SynchPoint> {
82
+ const [blocksSynchedTo, messagesSynchedTo] = await Promise.all([
83
+ stores.blocks.getSynchedL1BlockNumber(),
84
+ stores.messages.getSynchedL1Block(),
85
+ ]);
86
+ return { blocksSynchedTo, messagesSynchedTo };
87
+ }
88
+
89
+ /**
90
+ * Backs up the underlying KV store to the given folder. Returns the path to the resulting db file.
91
+ */
92
+ export async function backupArchiverDataStores(
93
+ stores: ArchiverDataStores,
94
+ path: string,
95
+ compress = true,
96
+ ): Promise<string> {
97
+ await stores.db.backupTo(path, compress);
98
+ return join(path, 'data.mdb');
99
+ }
100
+
101
+ /**
102
+ * Returns a {@link ContractDataSource} adapter over {@link ArchiverDataStores}.
103
+ * Used by contexts (e.g. offline epoch re-prover tools) that need a ContractDataSource
104
+ * but do not need a full archiver instance.
105
+ */
106
+ export function createContractDataSource(stores: ArchiverDataStores): ContractDataSource {
107
+ return new ArchiverContractDataSourceAdapter(stores);
108
+ }
@@ -0,0 +1,37 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import { FunctionSelector } from '@aztec/stdlib/abi';
3
+
4
+ const MAX_FUNCTION_SIGNATURES = 1000;
5
+ const MAX_FUNCTION_NAME_LEN = 256;
6
+
7
+ /**
8
+ * In-memory cache mapping public function selectors to function names.
9
+ *
10
+ * Populated opportunistically (e.g. by PXE registering signatures from artifacts) so the
11
+ * archiver can attach human-readable names to logs and traces. Bounded by
12
+ * {@link MAX_FUNCTION_SIGNATURES} to avoid unbounded growth from untrusted callers.
13
+ */
14
+ export class FunctionNamesCache {
15
+ private readonly log = createLogger('archiver:data-stores');
16
+ private readonly names: Map<string, string> = new Map();
17
+
18
+ /** Adds the given public function signatures to the cache. */
19
+ public async register(signatures: string[]): Promise<void> {
20
+ for (const sig of signatures) {
21
+ if (this.names.size > MAX_FUNCTION_SIGNATURES) {
22
+ return;
23
+ }
24
+ try {
25
+ const selector = await FunctionSelector.fromSignature(sig);
26
+ this.names.set(selector.toString(), sig.slice(0, sig.indexOf('(')).slice(0, MAX_FUNCTION_NAME_LEN));
27
+ } catch {
28
+ this.log.warn(`Failed to parse signature: ${sig}. Ignoring`);
29
+ }
30
+ }
31
+ }
32
+
33
+ /** Looks up a function name for the given selector, or returns undefined if not registered. */
34
+ public get(selector: FunctionSelector): string | undefined {
35
+ return this.names.get(selector.toString());
36
+ }
37
+ }
@@ -0,0 +1,134 @@
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import {
4
+ type BlockData,
5
+ type CheckpointId,
6
+ GENESIS_BLOCK_HEADER_HASH,
7
+ GENESIS_CHECKPOINT_HEADER_HASH,
8
+ type L2Tips,
9
+ } from '@aztec/stdlib/block';
10
+
11
+ import type { BlockStore } from './block_store.js';
12
+
13
+ /**
14
+ * In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
15
+ * Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
16
+ * Refresh calls should happen within the store transaction that mutates block data to ensure consistency.
17
+ */
18
+ export class L2TipsCache {
19
+ #tipsPromise: Promise<L2Tips> | undefined;
20
+
21
+ constructor(private blockStore: BlockStore) {}
22
+
23
+ /** Returns the cached L2 tips. Loads from the block store on first call. */
24
+ public getL2Tips(): Promise<L2Tips> {
25
+ return (this.#tipsPromise ??= this.loadFromStore());
26
+ }
27
+
28
+ /** Reloads the L2 tips from the block store. Should be called within the store transaction that mutates data. */
29
+ public async refresh(): Promise<void> {
30
+ this.#tipsPromise = this.loadFromStore();
31
+ await this.#tipsPromise;
32
+ }
33
+
34
+ private async loadFromStore(): Promise<L2Tips> {
35
+ const [
36
+ latestBlockNumber,
37
+ provenBlockNumber,
38
+ proposedCheckpointBlockNumber,
39
+ checkpointedBlockNumber,
40
+ finalizedBlockNumber,
41
+ ] = await Promise.all([
42
+ this.blockStore.getLatestL2BlockNumber(),
43
+ this.blockStore.getProvenBlockNumber(),
44
+ this.blockStore.getProposedCheckpointL2BlockNumber(),
45
+ this.blockStore.getCheckpointedL2BlockNumber(),
46
+ this.blockStore.getFinalizedL2BlockNumber(),
47
+ ]);
48
+
49
+ const genesisBlockHeader = {
50
+ blockHash: GENESIS_BLOCK_HEADER_HASH,
51
+ checkpointNumber: CheckpointNumber.ZERO,
52
+ } as const;
53
+ const beforeInitialBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
54
+
55
+ const getBlockData = (blockNumber: BlockNumber) =>
56
+ blockNumber > beforeInitialBlockNumber ? this.blockStore.getBlockData(blockNumber) : genesisBlockHeader;
57
+
58
+ const [latestBlockData, provenBlockData, proposedCheckpointBlockData, checkpointedBlockData, finalizedBlockData] =
59
+ await Promise.all(
60
+ [
61
+ latestBlockNumber,
62
+ provenBlockNumber,
63
+ proposedCheckpointBlockNumber,
64
+ checkpointedBlockNumber,
65
+ finalizedBlockNumber,
66
+ ].map(getBlockData),
67
+ );
68
+
69
+ if (
70
+ !latestBlockData ||
71
+ !provenBlockData ||
72
+ !finalizedBlockData ||
73
+ !checkpointedBlockData ||
74
+ !proposedCheckpointBlockData
75
+ ) {
76
+ throw new Error('Failed to load block data for L2 tips');
77
+ }
78
+
79
+ const [provenCheckpointId, finalizedCheckpointId, proposedCheckpointId, checkpointedCheckpointId] =
80
+ await Promise.all([
81
+ this.getCheckpointIdForBlock(provenBlockData),
82
+ this.getCheckpointIdForBlock(finalizedBlockData),
83
+ this.getCheckpointIdForProposedCheckpoint(checkpointedBlockData),
84
+ this.getCheckpointIdForBlock(checkpointedBlockData),
85
+ ]);
86
+
87
+ return {
88
+ proposed: { number: latestBlockNumber, hash: latestBlockData.blockHash.toString() },
89
+ proven: {
90
+ block: { number: provenBlockNumber, hash: provenBlockData.blockHash.toString() },
91
+ checkpoint: provenCheckpointId,
92
+ },
93
+ proposedCheckpoint: {
94
+ block: { number: proposedCheckpointBlockNumber, hash: proposedCheckpointBlockData.blockHash.toString() },
95
+ checkpoint: proposedCheckpointId,
96
+ },
97
+ finalized: {
98
+ block: { number: finalizedBlockNumber, hash: finalizedBlockData.blockHash.toString() },
99
+ checkpoint: finalizedCheckpointId,
100
+ },
101
+ checkpointed: {
102
+ block: { number: checkpointedBlockNumber, hash: checkpointedBlockData.blockHash.toString() },
103
+ checkpoint: checkpointedCheckpointId,
104
+ },
105
+ };
106
+ }
107
+
108
+ private async getCheckpointIdForProposedCheckpoint(
109
+ checkpointedBlockData: Pick<BlockData, 'checkpointNumber'>,
110
+ ): Promise<CheckpointId> {
111
+ const checkpointData = await this.blockStore.getLastProposedCheckpoint();
112
+ if (!checkpointData) {
113
+ return this.getCheckpointIdForBlock(checkpointedBlockData);
114
+ }
115
+ return {
116
+ number: checkpointData.checkpointNumber,
117
+ hash: checkpointData.header.hash().toString(),
118
+ };
119
+ }
120
+
121
+ private async getCheckpointIdForBlock(blockData: Pick<BlockData, 'checkpointNumber'>): Promise<CheckpointId> {
122
+ const checkpointData = await this.blockStore.getCheckpointData(blockData.checkpointNumber);
123
+ if (!checkpointData) {
124
+ return {
125
+ number: CheckpointNumber.ZERO,
126
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
127
+ };
128
+ }
129
+ return {
130
+ number: checkpointData.checkpointNumber,
131
+ hash: checkpointData.header.hash().toString(),
132
+ };
133
+ }
134
+ }