@aztec/archiver 0.0.0-test.0

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 (106) hide show
  1. package/README.md +16 -0
  2. package/dest/archiver/archiver.d.ts +197 -0
  3. package/dest/archiver/archiver.d.ts.map +1 -0
  4. package/dest/archiver/archiver.js +900 -0
  5. package/dest/archiver/archiver_store.d.ts +220 -0
  6. package/dest/archiver/archiver_store.d.ts.map +1 -0
  7. package/dest/archiver/archiver_store.js +4 -0
  8. package/dest/archiver/archiver_store_test_suite.d.ts +8 -0
  9. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -0
  10. package/dest/archiver/archiver_store_test_suite.js +794 -0
  11. package/dest/archiver/config.d.ts +37 -0
  12. package/dest/archiver/config.d.ts.map +1 -0
  13. package/dest/archiver/config.js +46 -0
  14. package/dest/archiver/data_retrieval.d.ts +74 -0
  15. package/dest/archiver/data_retrieval.d.ts.map +1 -0
  16. package/dest/archiver/data_retrieval.js +283 -0
  17. package/dest/archiver/errors.d.ts +4 -0
  18. package/dest/archiver/errors.d.ts.map +1 -0
  19. package/dest/archiver/errors.js +5 -0
  20. package/dest/archiver/index.d.ts +8 -0
  21. package/dest/archiver/index.d.ts.map +1 -0
  22. package/dest/archiver/index.js +5 -0
  23. package/dest/archiver/instrumentation.d.ts +29 -0
  24. package/dest/archiver/instrumentation.d.ts.map +1 -0
  25. package/dest/archiver/instrumentation.js +99 -0
  26. package/dest/archiver/kv_archiver_store/block_store.d.ts +87 -0
  27. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -0
  28. package/dest/archiver/kv_archiver_store/block_store.js +217 -0
  29. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +18 -0
  30. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -0
  31. package/dest/archiver/kv_archiver_store/contract_class_store.js +126 -0
  32. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +21 -0
  33. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -0
  34. package/dest/archiver/kv_archiver_store/contract_instance_store.js +63 -0
  35. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +153 -0
  36. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -0
  37. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +254 -0
  38. package/dest/archiver/kv_archiver_store/log_store.d.ts +49 -0
  39. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -0
  40. package/dest/archiver/kv_archiver_store/log_store.js +364 -0
  41. package/dest/archiver/kv_archiver_store/message_store.d.ts +33 -0
  42. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -0
  43. package/dest/archiver/kv_archiver_store/message_store.js +85 -0
  44. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +12 -0
  45. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +1 -0
  46. package/dest/archiver/kv_archiver_store/nullifier_store.js +73 -0
  47. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +23 -0
  48. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +1 -0
  49. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +49 -0
  50. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +175 -0
  51. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +1 -0
  52. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +636 -0
  53. package/dest/archiver/structs/data_retrieval.d.ts +27 -0
  54. package/dest/archiver/structs/data_retrieval.d.ts.map +1 -0
  55. package/dest/archiver/structs/data_retrieval.js +5 -0
  56. package/dest/archiver/structs/published.d.ts +11 -0
  57. package/dest/archiver/structs/published.d.ts.map +1 -0
  58. package/dest/archiver/structs/published.js +1 -0
  59. package/dest/factory.d.ts +24 -0
  60. package/dest/factory.d.ts.map +1 -0
  61. package/dest/factory.js +85 -0
  62. package/dest/index.d.ts +5 -0
  63. package/dest/index.d.ts.map +1 -0
  64. package/dest/index.js +4 -0
  65. package/dest/rpc/index.d.ts +10 -0
  66. package/dest/rpc/index.d.ts.map +1 -0
  67. package/dest/rpc/index.js +18 -0
  68. package/dest/test/index.d.ts +4 -0
  69. package/dest/test/index.d.ts.map +1 -0
  70. package/dest/test/index.js +3 -0
  71. package/dest/test/mock_archiver.d.ts +23 -0
  72. package/dest/test/mock_archiver.d.ts.map +1 -0
  73. package/dest/test/mock_archiver.js +40 -0
  74. package/dest/test/mock_l1_to_l2_message_source.d.ts +16 -0
  75. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -0
  76. package/dest/test/mock_l1_to_l2_message_source.js +25 -0
  77. package/dest/test/mock_l2_block_source.d.ts +79 -0
  78. package/dest/test/mock_l2_block_source.d.ts.map +1 -0
  79. package/dest/test/mock_l2_block_source.js +163 -0
  80. package/package.json +110 -0
  81. package/src/archiver/archiver.ts +1181 -0
  82. package/src/archiver/archiver_store.ts +263 -0
  83. package/src/archiver/archiver_store_test_suite.ts +810 -0
  84. package/src/archiver/config.ts +92 -0
  85. package/src/archiver/data_retrieval.ts +422 -0
  86. package/src/archiver/errors.ts +5 -0
  87. package/src/archiver/index.ts +7 -0
  88. package/src/archiver/instrumentation.ts +132 -0
  89. package/src/archiver/kv_archiver_store/block_store.ts +283 -0
  90. package/src/archiver/kv_archiver_store/contract_class_store.ts +186 -0
  91. package/src/archiver/kv_archiver_store/contract_instance_store.ts +107 -0
  92. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +358 -0
  93. package/src/archiver/kv_archiver_store/log_store.ts +444 -0
  94. package/src/archiver/kv_archiver_store/message_store.ts +102 -0
  95. package/src/archiver/kv_archiver_store/nullifier_store.ts +97 -0
  96. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +61 -0
  97. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +801 -0
  98. package/src/archiver/structs/data_retrieval.ts +27 -0
  99. package/src/archiver/structs/published.ts +11 -0
  100. package/src/factory.ts +107 -0
  101. package/src/index.ts +5 -0
  102. package/src/rpc/index.ts +20 -0
  103. package/src/test/index.ts +3 -0
  104. package/src/test/mock_archiver.ts +57 -0
  105. package/src/test/mock_l1_to_l2_message_source.ts +31 -0
  106. package/src/test/mock_l2_block_source.ts +204 -0
@@ -0,0 +1,87 @@
1
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
2
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
+ import { type InBlock, L2Block } from '@aztec/stdlib/block';
4
+ import { BlockHeader, TxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
5
+ import type { L1Published } from '../structs/published.js';
6
+ export { type TxEffect, type TxHash, TxReceipt } from '@aztec/stdlib/tx';
7
+ /**
8
+ * LMDB implementation of the ArchiverDataStore interface.
9
+ */
10
+ export declare class BlockStore {
11
+ #private;
12
+ private db;
13
+ constructor(db: AztecAsyncKVStore);
14
+ /**
15
+ * Append new blocks to the store's list.
16
+ * @param blocks - The L2 blocks to be added to the store.
17
+ * @returns True if the operation is successful.
18
+ */
19
+ addBlocks(blocks: L1Published<L2Block>[]): Promise<boolean>;
20
+ /**
21
+ * Unwinds blocks from the database
22
+ * @param from - The tip of the chain, passed for verification purposes,
23
+ * ensuring that we don't end up deleting something we did not intend
24
+ * @param blocksToUnwind - The number of blocks we are to unwind
25
+ * @returns True if the operation is successful
26
+ */
27
+ unwindBlocks(from: number, blocksToUnwind: number): Promise<boolean>;
28
+ /**
29
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
30
+ * @param start - Number of the first block to return (inclusive).
31
+ * @param limit - The number of blocks to return.
32
+ * @returns The requested L2 blocks
33
+ */
34
+ getBlocks(start: number, limit: number): AsyncIterableIterator<L1Published<L2Block>>;
35
+ /**
36
+ * Gets an L2 block.
37
+ * @param blockNumber - The number of the block to return.
38
+ * @returns The requested L2 block.
39
+ */
40
+ getBlock(blockNumber: number): Promise<L1Published<L2Block> | undefined>;
41
+ /**
42
+ * Gets the headers for a sequence of L2 blocks.
43
+ * @param start - Number of the first block to return (inclusive).
44
+ * @param limit - The number of blocks to return.
45
+ * @returns The requested L2 block headers
46
+ */
47
+ getBlockHeaders(start: number, limit: number): AsyncIterableIterator<BlockHeader>;
48
+ private getBlockFromBlockStorage;
49
+ /**
50
+ * Gets a tx effect.
51
+ * @param txHash - The txHash of the tx corresponding to the tx effect.
52
+ * @returns The requested tx effect (or undefined if not found).
53
+ */
54
+ getTxEffect(txHash: TxHash): Promise<InBlock<TxEffect> | undefined>;
55
+ /**
56
+ * Gets a receipt of a settled tx.
57
+ * @param txHash - The hash of a tx we try to get the receipt for.
58
+ * @returns The requested tx receipt (or undefined if not found).
59
+ */
60
+ getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined>;
61
+ /**
62
+ * Looks up which block included the requested tx effect.
63
+ * @param txHash - The txHash of the tx.
64
+ * @returns The block number and index of the tx.
65
+ */
66
+ getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined>;
67
+ /**
68
+ * Looks up which block deployed a particular contract.
69
+ * @param contractAddress - The address of the contract to look up.
70
+ * @returns The block number and index of the contract.
71
+ */
72
+ getContractLocation(contractAddress: AztecAddress): Promise<[blockNumber: number, index: number] | undefined>;
73
+ /**
74
+ * Gets the number of the latest L2 block processed.
75
+ * @returns The number of the latest L2 block processed.
76
+ */
77
+ getSynchedL2BlockNumber(): Promise<number>;
78
+ /**
79
+ * Gets the most recent L1 block processed.
80
+ * @returns The L1 block that published the latest L2 block
81
+ */
82
+ getSynchedL1BlockNumber(): Promise<bigint | undefined>;
83
+ setSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<boolean>;
84
+ getProvenL2BlockNumber(): Promise<number>;
85
+ setProvenL2BlockNumber(blockNumber: number): Promise<boolean>;
86
+ }
87
+ //# sourceMappingURL=block_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block_store.d.ts","sourceRoot":"","sources":["../../../src/archiver/kv_archiver_store/block_store.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAA6C,MAAM,iBAAiB,CAAC;AACpG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAQ,KAAK,OAAO,EAAE,OAAO,EAAe,MAAM,qBAAqB,CAAC;AAE/E,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE5E,OAAO,KAAK,EAAE,WAAW,EAAmB,MAAM,yBAAyB,CAAC;AAE5E,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAUzE;;GAEG;AACH,qBAAa,UAAU;;IAwBT,OAAO,CAAC,EAAE;gBAAF,EAAE,EAAE,iBAAiB;IAUzC;;;;OAIG;IACG,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IA0BjE;;;;;;OAMG;IACG,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM;IA0BvD;;;;;OAKG;IACI,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,qBAAqB,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAO3F;;;;OAIG;IACG,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC;IAS9E;;;;;OAKG;IACI,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,qBAAqB,CAAC,WAAW,CAAC;YAM1E,wBAAwB;IAgBtC;;;;OAIG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,SAAS,CAAC;IAkBzE;;;;OAIG;IACG,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IAmBzE;;;;OAIG;IACH,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;IAI1F;;;;OAIG;IACH,mBAAmB,CAAC,eAAe,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;IAI7G;;;OAGG;IACG,uBAAuB,IAAI,OAAO,CAAC,MAAM,CAAC;IAKhD;;;OAGG;IACH,uBAAuB,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAItD,uBAAuB,CAAC,aAAa,EAAE,MAAM;IAIvC,sBAAsB,IAAI,OAAO,CAAC,MAAM,CAAC;IAI/C,sBAAsB,CAAC,WAAW,EAAE,MAAM;CAe3C"}
@@ -0,0 +1,217 @@
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { toArray } from '@aztec/foundation/iterable';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { Body, L2Block, L2BlockHash } from '@aztec/stdlib/block';
5
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
6
+ import { BlockHeader, TxReceipt } from '@aztec/stdlib/tx';
7
+ export { TxReceipt } from '@aztec/stdlib/tx';
8
+ /**
9
+ * LMDB implementation of the ArchiverDataStore interface.
10
+ */ export class BlockStore {
11
+ db;
12
+ /** Map block number to block data */ #blocks;
13
+ /** Map block hash to block body */ #blockBodies;
14
+ /** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
15
+ /** Stores l2 block number of the last proven block */ #lastProvenL2Block;
16
+ /** Stores l2 epoch number of the last proven epoch */ #lastProvenL2Epoch;
17
+ /** Index mapping transaction hash (as a string) to its location in a block */ #txIndex;
18
+ /** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
19
+ #log;
20
+ constructor(db){
21
+ this.db = db;
22
+ this.#log = createLogger('archiver:block_store');
23
+ this.#blocks = db.openMap('archiver_blocks');
24
+ this.#blockBodies = db.openMap('archiver_block_bodies');
25
+ this.#txIndex = db.openMap('archiver_tx_index');
26
+ this.#contractIndex = db.openMap('archiver_contract_index');
27
+ this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
28
+ this.#lastProvenL2Block = db.openSingleton('archiver_last_proven_l2_block');
29
+ this.#lastProvenL2Epoch = db.openSingleton('archiver_last_proven_l2_epoch');
30
+ }
31
+ /**
32
+ * Append new blocks to the store's list.
33
+ * @param blocks - The L2 blocks to be added to the store.
34
+ * @returns True if the operation is successful.
35
+ */ async addBlocks(blocks) {
36
+ if (blocks.length === 0) {
37
+ return true;
38
+ }
39
+ return await this.db.transactionAsync(async ()=>{
40
+ for (const block of blocks){
41
+ await this.#blocks.set(block.data.number, {
42
+ header: block.data.header.toBuffer(),
43
+ archive: block.data.archive.toBuffer(),
44
+ l1: block.l1
45
+ });
46
+ for(let i = 0; i < block.data.body.txEffects.length; i++){
47
+ const txEffect = block.data.body.txEffects[i];
48
+ await this.#txIndex.set(txEffect.txHash.toString(), [
49
+ block.data.number,
50
+ i
51
+ ]);
52
+ }
53
+ await this.#blockBodies.set((await block.data.hash()).toString(), block.data.body.toBuffer());
54
+ }
55
+ await this.#lastSynchedL1Block.set(blocks[blocks.length - 1].l1.blockNumber);
56
+ return true;
57
+ });
58
+ }
59
+ /**
60
+ * Unwinds blocks from the database
61
+ * @param from - The tip of the chain, passed for verification purposes,
62
+ * ensuring that we don't end up deleting something we did not intend
63
+ * @param blocksToUnwind - The number of blocks we are to unwind
64
+ * @returns True if the operation is successful
65
+ */ async unwindBlocks(from, blocksToUnwind) {
66
+ return await this.db.transactionAsync(async ()=>{
67
+ const last = await this.getSynchedL2BlockNumber();
68
+ if (from !== last) {
69
+ throw new Error(`Can only unwind blocks from the tip (requested ${from} but current tip is ${last})`);
70
+ }
71
+ for(let i = 0; i < blocksToUnwind; i++){
72
+ const blockNumber = from - i;
73
+ const block = await this.getBlock(blockNumber);
74
+ if (block === undefined) {
75
+ this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
76
+ continue;
77
+ }
78
+ await this.#blocks.delete(block.data.number);
79
+ await Promise.all(block.data.body.txEffects.map((tx)=>this.#txIndex.delete(tx.txHash.toString())));
80
+ const blockHash = (await block.data.hash()).toString();
81
+ await this.#blockBodies.delete(blockHash);
82
+ this.#log.debug(`Unwound block ${blockNumber} ${blockHash}`);
83
+ }
84
+ return true;
85
+ });
86
+ }
87
+ /**
88
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
89
+ * @param start - Number of the first block to return (inclusive).
90
+ * @param limit - The number of blocks to return.
91
+ * @returns The requested L2 blocks
92
+ */ async *getBlocks(start, limit) {
93
+ for await (const blockStorage of this.#blocks.valuesAsync(this.#computeBlockRange(start, limit))){
94
+ const block = await this.getBlockFromBlockStorage(blockStorage);
95
+ yield block;
96
+ }
97
+ }
98
+ /**
99
+ * Gets an L2 block.
100
+ * @param blockNumber - The number of the block to return.
101
+ * @returns The requested L2 block.
102
+ */ async getBlock(blockNumber) {
103
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
104
+ if (!blockStorage || !blockStorage.header) {
105
+ return Promise.resolve(undefined);
106
+ }
107
+ return this.getBlockFromBlockStorage(blockStorage);
108
+ }
109
+ /**
110
+ * Gets the headers for a sequence of L2 blocks.
111
+ * @param start - Number of the first block to return (inclusive).
112
+ * @param limit - The number of blocks to return.
113
+ * @returns The requested L2 block headers
114
+ */ async *getBlockHeaders(start, limit) {
115
+ for await (const blockStorage of this.#blocks.valuesAsync(this.#computeBlockRange(start, limit))){
116
+ yield BlockHeader.fromBuffer(blockStorage.header);
117
+ }
118
+ }
119
+ async getBlockFromBlockStorage(blockStorage) {
120
+ const header = BlockHeader.fromBuffer(blockStorage.header);
121
+ const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
122
+ const blockHash = (await header.hash()).toString();
123
+ const blockBodyBuffer = await this.#blockBodies.getAsync(blockHash);
124
+ if (blockBodyBuffer === undefined) {
125
+ throw new Error(`Could not retrieve body for block ${header.globalVariables.blockNumber.toNumber()} ${blockHash}`);
126
+ }
127
+ const body = Body.fromBuffer(blockBodyBuffer);
128
+ const l2Block = new L2Block(archive, header, body);
129
+ return {
130
+ data: l2Block,
131
+ l1: blockStorage.l1
132
+ };
133
+ }
134
+ /**
135
+ * Gets a tx effect.
136
+ * @param txHash - The txHash of the tx corresponding to the tx effect.
137
+ * @returns The requested tx effect (or undefined if not found).
138
+ */ async getTxEffect(txHash) {
139
+ const [blockNumber, txIndex] = await this.getTxLocation(txHash) ?? [];
140
+ if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
141
+ return undefined;
142
+ }
143
+ const block = await this.getBlock(blockNumber);
144
+ if (!block) {
145
+ return undefined;
146
+ }
147
+ return {
148
+ data: block.data.body.txEffects[txIndex],
149
+ l2BlockNumber: block.data.number,
150
+ l2BlockHash: (await block.data.hash()).toString()
151
+ };
152
+ }
153
+ /**
154
+ * Gets a receipt of a settled tx.
155
+ * @param txHash - The hash of a tx we try to get the receipt for.
156
+ * @returns The requested tx receipt (or undefined if not found).
157
+ */ async getSettledTxReceipt(txHash) {
158
+ const [blockNumber, txIndex] = await this.getTxLocation(txHash) ?? [];
159
+ if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
160
+ return undefined;
161
+ }
162
+ const block = await this.getBlock(blockNumber);
163
+ const tx = block.data.body.txEffects[txIndex];
164
+ return new TxReceipt(txHash, TxReceipt.statusFromRevertCode(tx.revertCode), '', tx.transactionFee.toBigInt(), L2BlockHash.fromField(await block.data.hash()), block.data.number);
165
+ }
166
+ /**
167
+ * Looks up which block included the requested tx effect.
168
+ * @param txHash - The txHash of the tx.
169
+ * @returns The block number and index of the tx.
170
+ */ getTxLocation(txHash) {
171
+ return this.#txIndex.getAsync(txHash.toString());
172
+ }
173
+ /**
174
+ * Looks up which block deployed a particular contract.
175
+ * @param contractAddress - The address of the contract to look up.
176
+ * @returns The block number and index of the contract.
177
+ */ getContractLocation(contractAddress) {
178
+ return this.#contractIndex.getAsync(contractAddress.toString());
179
+ }
180
+ /**
181
+ * Gets the number of the latest L2 block processed.
182
+ * @returns The number of the latest L2 block processed.
183
+ */ async getSynchedL2BlockNumber() {
184
+ const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
185
+ reverse: true,
186
+ limit: 1
187
+ }));
188
+ return typeof lastBlockNumber === 'number' ? lastBlockNumber : INITIAL_L2_BLOCK_NUM - 1;
189
+ }
190
+ /**
191
+ * Gets the most recent L1 block processed.
192
+ * @returns The L1 block that published the latest L2 block
193
+ */ getSynchedL1BlockNumber() {
194
+ return this.#lastSynchedL1Block.getAsync();
195
+ }
196
+ setSynchedL1BlockNumber(l1BlockNumber) {
197
+ return this.#lastSynchedL1Block.set(l1BlockNumber);
198
+ }
199
+ async getProvenL2BlockNumber() {
200
+ return await this.#lastProvenL2Block.getAsync() ?? 0;
201
+ }
202
+ setProvenL2BlockNumber(blockNumber) {
203
+ return this.#lastProvenL2Block.set(blockNumber);
204
+ }
205
+ #computeBlockRange(start, limit) {
206
+ if (limit < 1) {
207
+ throw new Error(`Invalid limit: ${limit}`);
208
+ }
209
+ if (start < INITIAL_L2_BLOCK_NUM) {
210
+ throw new Error(`Invalid start: ${start}`);
211
+ }
212
+ return {
213
+ start,
214
+ limit
215
+ };
216
+ }
217
+ }
@@ -0,0 +1,18 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
3
+ import type { ContractClassPublic, ExecutablePrivateFunctionWithMembershipProof, UnconstrainedFunctionWithMembershipProof } from '@aztec/stdlib/contract';
4
+ /**
5
+ * LMDB implementation of the ArchiverDataStore interface.
6
+ */
7
+ export declare class ContractClassStore {
8
+ #private;
9
+ private db;
10
+ constructor(db: AztecAsyncKVStore);
11
+ addContractClass(contractClass: ContractClassPublic, bytecodeCommitment: Fr, blockNumber: number): Promise<void>;
12
+ deleteContractClasses(contractClass: ContractClassPublic, blockNumber: number): Promise<void>;
13
+ getContractClass(id: Fr): Promise<ContractClassPublic | undefined>;
14
+ getBytecodeCommitment(id: Fr): Promise<Fr | undefined>;
15
+ getContractClassIds(): Promise<Fr[]>;
16
+ addFunctions(contractClassId: Fr, newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[], newUnconstrainedFunctions: UnconstrainedFunctionWithMembershipProof[]): Promise<boolean>;
17
+ }
18
+ //# sourceMappingURL=contract_class_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract_class_store.d.ts","sourceRoot":"","sources":["../../../src/archiver/kv_archiver_store/contract_class_store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAG9C,OAAO,KAAK,EAAE,iBAAiB,EAAiB,MAAM,iBAAiB,CAAC;AAExE,OAAO,KAAK,EACV,mBAAmB,EAEnB,4CAA4C,EAC5C,wCAAwC,EACzC,MAAM,wBAAwB,CAAC;AAGhC;;GAEG;AACH,qBAAa,kBAAkB;;IAIjB,OAAO,CAAC,EAAE;gBAAF,EAAE,EAAE,iBAAiB;IAKnC,gBAAgB,CACpB,aAAa,EAAE,mBAAmB,EAClC,kBAAkB,EAAE,EAAE,EACtB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC;IAQV,qBAAqB,CAAC,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ7F,gBAAgB,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,mBAAmB,GAAG,SAAS,CAAC;IAKlE,qBAAqB,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC;IAKtD,mBAAmB,IAAI,OAAO,CAAC,EAAE,EAAE,CAAC;IAIpC,YAAY,CAChB,eAAe,EAAE,EAAE,EACnB,mBAAmB,EAAE,4CAA4C,EAAE,EACnE,yBAAyB,EAAE,wCAAwC,EAAE,GACpE,OAAO,CAAC,OAAO,CAAC;CA4BpB"}
@@ -0,0 +1,126 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import { toArray } from '@aztec/foundation/iterable';
3
+ import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
4
+ import { FunctionSelector } from '@aztec/stdlib/abi';
5
+ import { Vector } from '@aztec/stdlib/types';
6
+ /**
7
+ * LMDB implementation of the ArchiverDataStore interface.
8
+ */ export class ContractClassStore {
9
+ db;
10
+ #contractClasses;
11
+ #bytecodeCommitments;
12
+ constructor(db){
13
+ this.db = db;
14
+ this.#contractClasses = db.openMap('archiver_contract_classes');
15
+ this.#bytecodeCommitments = db.openMap('archiver_bytecode_commitments');
16
+ }
17
+ async addContractClass(contractClass, bytecodeCommitment, blockNumber) {
18
+ await this.#contractClasses.setIfNotExists(contractClass.id.toString(), serializeContractClassPublic({
19
+ ...contractClass,
20
+ l2BlockNumber: blockNumber
21
+ }));
22
+ await this.#bytecodeCommitments.setIfNotExists(contractClass.id.toString(), bytecodeCommitment.toBuffer());
23
+ }
24
+ async deleteContractClasses(contractClass, blockNumber) {
25
+ const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
26
+ if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
27
+ await this.#contractClasses.delete(contractClass.id.toString());
28
+ await this.#bytecodeCommitments.delete(contractClass.id.toString());
29
+ }
30
+ }
31
+ async getContractClass(id) {
32
+ const contractClass = await this.#contractClasses.getAsync(id.toString());
33
+ return contractClass && {
34
+ ...deserializeContractClassPublic(contractClass),
35
+ id
36
+ };
37
+ }
38
+ async getBytecodeCommitment(id) {
39
+ const value = await this.#bytecodeCommitments.getAsync(id.toString());
40
+ return value === undefined ? undefined : Fr.fromBuffer(value);
41
+ }
42
+ async getContractClassIds() {
43
+ return (await toArray(this.#contractClasses.keysAsync())).map((key)=>Fr.fromHexString(key));
44
+ }
45
+ async addFunctions(contractClassId, newPrivateFunctions, newUnconstrainedFunctions) {
46
+ await this.db.transactionAsync(async ()=>{
47
+ const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
48
+ if (!existingClassBuffer) {
49
+ throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
50
+ }
51
+ const existingClass = deserializeContractClassPublic(existingClassBuffer);
52
+ const { privateFunctions: existingPrivateFns, unconstrainedFunctions: existingUnconstrainedFns } = existingClass;
53
+ const updatedClass = {
54
+ ...existingClass,
55
+ privateFunctions: [
56
+ ...existingPrivateFns,
57
+ ...newPrivateFunctions.filter((newFn)=>!existingPrivateFns.some((f)=>f.selector.equals(newFn.selector)))
58
+ ],
59
+ unconstrainedFunctions: [
60
+ ...existingUnconstrainedFns,
61
+ ...newUnconstrainedFunctions.filter((newFn)=>!existingUnconstrainedFns.some((f)=>f.selector.equals(newFn.selector)))
62
+ ]
63
+ };
64
+ await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
65
+ });
66
+ return true;
67
+ }
68
+ }
69
+ function serializeContractClassPublic(contractClass) {
70
+ return serializeToBuffer(contractClass.l2BlockNumber, numToUInt8(contractClass.version), contractClass.artifactHash, contractClass.publicFunctions.length, contractClass.publicFunctions?.map((f)=>serializeToBuffer(f.selector, f.bytecode.length, f.bytecode)) ?? [], contractClass.privateFunctions.length, contractClass.privateFunctions.map(serializePrivateFunction), contractClass.unconstrainedFunctions.length, contractClass.unconstrainedFunctions.map(serializeUnconstrainedFunction), contractClass.packedBytecode.length, contractClass.packedBytecode, contractClass.privateFunctionsRoot);
71
+ }
72
+ function serializePrivateFunction(fn) {
73
+ return serializeToBuffer(fn.selector, fn.vkHash, fn.bytecode.length, fn.bytecode, fn.functionMetadataHash, fn.artifactMetadataHash, fn.unconstrainedFunctionsArtifactTreeRoot, new Vector(fn.privateFunctionTreeSiblingPath), fn.privateFunctionTreeLeafIndex, new Vector(fn.artifactTreeSiblingPath), fn.artifactTreeLeafIndex);
74
+ }
75
+ function serializeUnconstrainedFunction(fn) {
76
+ return serializeToBuffer(fn.selector, fn.bytecode.length, fn.bytecode, fn.functionMetadataHash, fn.artifactMetadataHash, fn.privateFunctionsArtifactTreeRoot, new Vector(fn.artifactTreeSiblingPath), fn.artifactTreeLeafIndex);
77
+ }
78
+ function deserializeContractClassPublic(buffer) {
79
+ const reader = BufferReader.asReader(buffer);
80
+ return {
81
+ l2BlockNumber: reader.readNumber(),
82
+ version: reader.readUInt8(),
83
+ artifactHash: reader.readObject(Fr),
84
+ publicFunctions: reader.readVector({
85
+ fromBuffer: (reader)=>({
86
+ selector: reader.readObject(FunctionSelector),
87
+ bytecode: reader.readBuffer()
88
+ })
89
+ }),
90
+ privateFunctions: reader.readVector({
91
+ fromBuffer: deserializePrivateFunction
92
+ }),
93
+ unconstrainedFunctions: reader.readVector({
94
+ fromBuffer: deserializeUnconstrainedFunction
95
+ }),
96
+ packedBytecode: reader.readBuffer(),
97
+ privateFunctionsRoot: reader.readObject(Fr)
98
+ };
99
+ }
100
+ function deserializePrivateFunction(buffer) {
101
+ const reader = BufferReader.asReader(buffer);
102
+ return {
103
+ selector: reader.readObject(FunctionSelector),
104
+ vkHash: reader.readObject(Fr),
105
+ bytecode: reader.readBuffer(),
106
+ functionMetadataHash: reader.readObject(Fr),
107
+ artifactMetadataHash: reader.readObject(Fr),
108
+ unconstrainedFunctionsArtifactTreeRoot: reader.readObject(Fr),
109
+ privateFunctionTreeSiblingPath: reader.readVector(Fr),
110
+ privateFunctionTreeLeafIndex: reader.readNumber(),
111
+ artifactTreeSiblingPath: reader.readVector(Fr),
112
+ artifactTreeLeafIndex: reader.readNumber()
113
+ };
114
+ }
115
+ function deserializeUnconstrainedFunction(buffer) {
116
+ const reader = BufferReader.asReader(buffer);
117
+ return {
118
+ selector: reader.readObject(FunctionSelector),
119
+ bytecode: reader.readBuffer(),
120
+ functionMetadataHash: reader.readObject(Fr),
121
+ artifactMetadataHash: reader.readObject(Fr),
122
+ privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
123
+ artifactTreeSiblingPath: reader.readVector(Fr),
124
+ artifactTreeLeafIndex: reader.readNumber()
125
+ };
126
+ }
@@ -0,0 +1,21 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
+ import { type ContractInstanceUpdateWithAddress, type ContractInstanceWithAddress } from '@aztec/stdlib/contract';
5
+ type ContractInstanceUpdateKey = [string, number] | [string, number, number];
6
+ /**
7
+ * LMDB implementation of the ArchiverDataStore interface.
8
+ */
9
+ export declare class ContractInstanceStore {
10
+ #private;
11
+ constructor(db: AztecAsyncKVStore);
12
+ addContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void>;
13
+ deleteContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void>;
14
+ getUpdateKey(contractAddress: AztecAddress, blockNumber: number, logIndex?: number): ContractInstanceUpdateKey;
15
+ addContractInstanceUpdate(contractInstanceUpdate: ContractInstanceUpdateWithAddress, blockNumber: number, logIndex: number): Promise<void>;
16
+ deleteContractInstanceUpdate(contractInstanceUpdate: ContractInstanceUpdateWithAddress, blockNumber: number, logIndex: number): Promise<void>;
17
+ getCurrentContractInstanceClassId(address: AztecAddress, blockNumber: number, originalClassId: Fr): Promise<Fr>;
18
+ getContractInstance(address: AztecAddress, blockNumber: number): Promise<ContractInstanceWithAddress | undefined>;
19
+ }
20
+ export {};
21
+ //# sourceMappingURL=contract_instance_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract_instance_store.d.ts","sourceRoot":"","sources":["../../../src/archiver/kv_archiver_store/contract_instance_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,KAAK,EAAE,iBAAiB,EAAiB,MAAM,iBAAiB,CAAC;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EACL,KAAK,iCAAiC,EACtC,KAAK,2BAA2B,EAGjC,MAAM,wBAAwB,CAAC;AAEhC,KAAK,yBAAyB,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE7E;;GAEG;AACH,qBAAa,qBAAqB;;gBAIpB,EAAE,EAAE,iBAAiB;IAKjC,mBAAmB,CAAC,gBAAgB,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;IAOjF,sBAAsB,CAAC,gBAAgB,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpF,YAAY,CAAC,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,yBAAyB;IAQ9G,yBAAyB,CACvB,sBAAsB,EAAE,iCAAiC,EACzD,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAOhB,4BAA4B,CAC1B,sBAAsB,EAAE,iCAAiC,EACzD,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAMV,iCAAiC,CACrC,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,EAAE,GAClB,OAAO,CAAC,EAAE,CAAC;IAqBR,mBAAmB,CACvB,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,2BAA2B,GAAG,SAAS,CAAC;CAcpD"}
@@ -0,0 +1,63 @@
1
+ import { SerializableContractInstance, SerializableContractInstanceUpdate } from '@aztec/stdlib/contract';
2
+ /**
3
+ * LMDB implementation of the ArchiverDataStore interface.
4
+ */ export class ContractInstanceStore {
5
+ #contractInstances;
6
+ #contractInstanceUpdates;
7
+ constructor(db){
8
+ this.#contractInstances = db.openMap('archiver_contract_instances');
9
+ this.#contractInstanceUpdates = db.openMap('archiver_contract_instance_updates');
10
+ }
11
+ addContractInstance(contractInstance) {
12
+ return this.#contractInstances.set(contractInstance.address.toString(), new SerializableContractInstance(contractInstance).toBuffer());
13
+ }
14
+ deleteContractInstance(contractInstance) {
15
+ return this.#contractInstances.delete(contractInstance.address.toString());
16
+ }
17
+ getUpdateKey(contractAddress, blockNumber, logIndex) {
18
+ if (logIndex === undefined) {
19
+ return [
20
+ contractAddress.toString(),
21
+ blockNumber
22
+ ];
23
+ } else {
24
+ return [
25
+ contractAddress.toString(),
26
+ blockNumber,
27
+ logIndex
28
+ ];
29
+ }
30
+ }
31
+ addContractInstanceUpdate(contractInstanceUpdate, blockNumber, logIndex) {
32
+ return this.#contractInstanceUpdates.set(this.getUpdateKey(contractInstanceUpdate.address, blockNumber, logIndex), new SerializableContractInstanceUpdate(contractInstanceUpdate).toBuffer());
33
+ }
34
+ deleteContractInstanceUpdate(contractInstanceUpdate, blockNumber, logIndex) {
35
+ return this.#contractInstanceUpdates.delete(this.getUpdateKey(contractInstanceUpdate.address, blockNumber, logIndex));
36
+ }
37
+ async getCurrentContractInstanceClassId(address, blockNumber, originalClassId) {
38
+ // We need to find the last update before the given block number
39
+ const queryResult = await this.#contractInstanceUpdates.valuesAsync({
40
+ reverse: true,
41
+ end: this.getUpdateKey(address, blockNumber + 1),
42
+ limit: 1
43
+ }).next();
44
+ if (queryResult.done) {
45
+ return originalClassId;
46
+ }
47
+ const serializedUpdate = queryResult.value;
48
+ const update = SerializableContractInstanceUpdate.fromBuffer(serializedUpdate);
49
+ if (blockNumber < update.blockOfChange) {
50
+ return update.prevContractClassId.isZero() ? originalClassId : update.prevContractClassId;
51
+ }
52
+ return update.newContractClassId;
53
+ }
54
+ async getContractInstance(address, blockNumber) {
55
+ const contractInstance = await this.#contractInstances.getAsync(address.toString());
56
+ if (!contractInstance) {
57
+ return undefined;
58
+ }
59
+ const instance = SerializableContractInstance.fromBuffer(contractInstance).withAddress(address);
60
+ instance.currentContractClassId = await this.getCurrentContractInstanceClassId(address, blockNumber, instance.originalContractClassId);
61
+ return instance;
62
+ }
63
+ }