@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.
- package/README.md +16 -0
- package/dest/archiver/archiver.d.ts +197 -0
- package/dest/archiver/archiver.d.ts.map +1 -0
- package/dest/archiver/archiver.js +900 -0
- package/dest/archiver/archiver_store.d.ts +220 -0
- package/dest/archiver/archiver_store.d.ts.map +1 -0
- package/dest/archiver/archiver_store.js +4 -0
- package/dest/archiver/archiver_store_test_suite.d.ts +8 -0
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -0
- package/dest/archiver/archiver_store_test_suite.js +794 -0
- package/dest/archiver/config.d.ts +37 -0
- package/dest/archiver/config.d.ts.map +1 -0
- package/dest/archiver/config.js +46 -0
- package/dest/archiver/data_retrieval.d.ts +74 -0
- package/dest/archiver/data_retrieval.d.ts.map +1 -0
- package/dest/archiver/data_retrieval.js +283 -0
- package/dest/archiver/errors.d.ts +4 -0
- package/dest/archiver/errors.d.ts.map +1 -0
- package/dest/archiver/errors.js +5 -0
- package/dest/archiver/index.d.ts +8 -0
- package/dest/archiver/index.d.ts.map +1 -0
- package/dest/archiver/index.js +5 -0
- package/dest/archiver/instrumentation.d.ts +29 -0
- package/dest/archiver/instrumentation.d.ts.map +1 -0
- package/dest/archiver/instrumentation.js +99 -0
- package/dest/archiver/kv_archiver_store/block_store.d.ts +87 -0
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/block_store.js +217 -0
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +18 -0
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/contract_class_store.js +126 -0
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +21 -0
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/contract_instance_store.js +63 -0
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +153 -0
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +254 -0
- package/dest/archiver/kv_archiver_store/log_store.d.ts +49 -0
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/log_store.js +364 -0
- package/dest/archiver/kv_archiver_store/message_store.d.ts +33 -0
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/message_store.js +85 -0
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +12 -0
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/nullifier_store.js +73 -0
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +23 -0
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +1 -0
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +49 -0
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +175 -0
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +1 -0
- package/dest/archiver/memory_archiver_store/memory_archiver_store.js +636 -0
- package/dest/archiver/structs/data_retrieval.d.ts +27 -0
- package/dest/archiver/structs/data_retrieval.d.ts.map +1 -0
- package/dest/archiver/structs/data_retrieval.js +5 -0
- package/dest/archiver/structs/published.d.ts +11 -0
- package/dest/archiver/structs/published.d.ts.map +1 -0
- package/dest/archiver/structs/published.js +1 -0
- package/dest/factory.d.ts +24 -0
- package/dest/factory.d.ts.map +1 -0
- package/dest/factory.js +85 -0
- package/dest/index.d.ts +5 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +4 -0
- package/dest/rpc/index.d.ts +10 -0
- package/dest/rpc/index.d.ts.map +1 -0
- package/dest/rpc/index.js +18 -0
- package/dest/test/index.d.ts +4 -0
- package/dest/test/index.d.ts.map +1 -0
- package/dest/test/index.js +3 -0
- package/dest/test/mock_archiver.d.ts +23 -0
- package/dest/test/mock_archiver.d.ts.map +1 -0
- package/dest/test/mock_archiver.js +40 -0
- package/dest/test/mock_l1_to_l2_message_source.d.ts +16 -0
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -0
- package/dest/test/mock_l1_to_l2_message_source.js +25 -0
- package/dest/test/mock_l2_block_source.d.ts +79 -0
- package/dest/test/mock_l2_block_source.d.ts.map +1 -0
- package/dest/test/mock_l2_block_source.js +163 -0
- package/package.json +110 -0
- package/src/archiver/archiver.ts +1181 -0
- package/src/archiver/archiver_store.ts +263 -0
- package/src/archiver/archiver_store_test_suite.ts +810 -0
- package/src/archiver/config.ts +92 -0
- package/src/archiver/data_retrieval.ts +422 -0
- package/src/archiver/errors.ts +5 -0
- package/src/archiver/index.ts +7 -0
- package/src/archiver/instrumentation.ts +132 -0
- package/src/archiver/kv_archiver_store/block_store.ts +283 -0
- package/src/archiver/kv_archiver_store/contract_class_store.ts +186 -0
- package/src/archiver/kv_archiver_store/contract_instance_store.ts +107 -0
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +358 -0
- package/src/archiver/kv_archiver_store/log_store.ts +444 -0
- package/src/archiver/kv_archiver_store/message_store.ts +102 -0
- package/src/archiver/kv_archiver_store/nullifier_store.ts +97 -0
- package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +61 -0
- package/src/archiver/memory_archiver_store/memory_archiver_store.ts +801 -0
- package/src/archiver/structs/data_retrieval.ts +27 -0
- package/src/archiver/structs/published.ts +11 -0
- package/src/factory.ts +107 -0
- package/src/index.ts +5 -0
- package/src/rpc/index.ts +20 -0
- package/src/test/index.ts +3 -0
- package/src/test/mock_archiver.ts +57 -0
- package/src/test/mock_l1_to_l2_message_source.ts +31 -0
- package/src/test/mock_l2_block_source.ts +204 -0
|
@@ -0,0 +1,283 @@
|
|
|
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 type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
|
|
5
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
|
+
import { Body, type InBlock, L2Block, L2BlockHash } from '@aztec/stdlib/block';
|
|
7
|
+
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
8
|
+
import { BlockHeader, TxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
|
|
9
|
+
|
|
10
|
+
import type { L1Published, L1PublishedData } from '../structs/published.js';
|
|
11
|
+
|
|
12
|
+
export { type TxEffect, type TxHash, TxReceipt } from '@aztec/stdlib/tx';
|
|
13
|
+
|
|
14
|
+
type BlockIndexValue = [blockNumber: number, index: number];
|
|
15
|
+
|
|
16
|
+
type BlockStorage = {
|
|
17
|
+
header: Buffer;
|
|
18
|
+
archive: Buffer;
|
|
19
|
+
l1: L1PublishedData;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* LMDB implementation of the ArchiverDataStore interface.
|
|
24
|
+
*/
|
|
25
|
+
export class BlockStore {
|
|
26
|
+
/** Map block number to block data */
|
|
27
|
+
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
28
|
+
|
|
29
|
+
/** Map block hash to block body */
|
|
30
|
+
#blockBodies: AztecAsyncMap<string, Buffer>;
|
|
31
|
+
|
|
32
|
+
/** Stores L1 block number in which the last processed L2 block was included */
|
|
33
|
+
#lastSynchedL1Block: AztecAsyncSingleton<bigint>;
|
|
34
|
+
|
|
35
|
+
/** Stores l2 block number of the last proven block */
|
|
36
|
+
#lastProvenL2Block: AztecAsyncSingleton<number>;
|
|
37
|
+
|
|
38
|
+
/** Stores l2 epoch number of the last proven epoch */
|
|
39
|
+
#lastProvenL2Epoch: AztecAsyncSingleton<number>;
|
|
40
|
+
|
|
41
|
+
/** Index mapping transaction hash (as a string) to its location in a block */
|
|
42
|
+
#txIndex: AztecAsyncMap<string, BlockIndexValue>;
|
|
43
|
+
|
|
44
|
+
/** Index mapping a contract's address (as a string) to its location in a block */
|
|
45
|
+
#contractIndex: AztecAsyncMap<string, BlockIndexValue>;
|
|
46
|
+
|
|
47
|
+
#log = createLogger('archiver:block_store');
|
|
48
|
+
|
|
49
|
+
constructor(private db: AztecAsyncKVStore) {
|
|
50
|
+
this.#blocks = db.openMap('archiver_blocks');
|
|
51
|
+
this.#blockBodies = db.openMap('archiver_block_bodies');
|
|
52
|
+
this.#txIndex = db.openMap('archiver_tx_index');
|
|
53
|
+
this.#contractIndex = db.openMap('archiver_contract_index');
|
|
54
|
+
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
55
|
+
this.#lastProvenL2Block = db.openSingleton('archiver_last_proven_l2_block');
|
|
56
|
+
this.#lastProvenL2Epoch = db.openSingleton('archiver_last_proven_l2_epoch');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Append new blocks to the store's list.
|
|
61
|
+
* @param blocks - The L2 blocks to be added to the store.
|
|
62
|
+
* @returns True if the operation is successful.
|
|
63
|
+
*/
|
|
64
|
+
async addBlocks(blocks: L1Published<L2Block>[]): Promise<boolean> {
|
|
65
|
+
if (blocks.length === 0) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return await this.db.transactionAsync(async () => {
|
|
70
|
+
for (const block of blocks) {
|
|
71
|
+
await this.#blocks.set(block.data.number, {
|
|
72
|
+
header: block.data.header.toBuffer(),
|
|
73
|
+
archive: block.data.archive.toBuffer(),
|
|
74
|
+
l1: block.l1,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < block.data.body.txEffects.length; i++) {
|
|
78
|
+
const txEffect = block.data.body.txEffects[i];
|
|
79
|
+
await this.#txIndex.set(txEffect.txHash.toString(), [block.data.number, i]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
await this.#blockBodies.set((await block.data.hash()).toString(), block.data.body.toBuffer());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
await this.#lastSynchedL1Block.set(blocks[blocks.length - 1].l1.blockNumber);
|
|
86
|
+
return true;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Unwinds blocks from the database
|
|
92
|
+
* @param from - The tip of the chain, passed for verification purposes,
|
|
93
|
+
* ensuring that we don't end up deleting something we did not intend
|
|
94
|
+
* @param blocksToUnwind - The number of blocks we are to unwind
|
|
95
|
+
* @returns True if the operation is successful
|
|
96
|
+
*/
|
|
97
|
+
async unwindBlocks(from: number, blocksToUnwind: number) {
|
|
98
|
+
return await this.db.transactionAsync(async () => {
|
|
99
|
+
const last = await this.getSynchedL2BlockNumber();
|
|
100
|
+
if (from !== last) {
|
|
101
|
+
throw new Error(`Can only unwind blocks from the tip (requested ${from} but current tip is ${last})`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
for (let i = 0; i < blocksToUnwind; i++) {
|
|
105
|
+
const blockNumber = from - i;
|
|
106
|
+
const block = await this.getBlock(blockNumber);
|
|
107
|
+
|
|
108
|
+
if (block === undefined) {
|
|
109
|
+
this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
await this.#blocks.delete(block.data.number);
|
|
113
|
+
await Promise.all(block.data.body.txEffects.map(tx => this.#txIndex.delete(tx.txHash.toString())));
|
|
114
|
+
const blockHash = (await block.data.hash()).toString();
|
|
115
|
+
await this.#blockBodies.delete(blockHash);
|
|
116
|
+
this.#log.debug(`Unwound block ${blockNumber} ${blockHash}`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return true;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
125
|
+
* @param start - Number of the first block to return (inclusive).
|
|
126
|
+
* @param limit - The number of blocks to return.
|
|
127
|
+
* @returns The requested L2 blocks
|
|
128
|
+
*/
|
|
129
|
+
async *getBlocks(start: number, limit: number): AsyncIterableIterator<L1Published<L2Block>> {
|
|
130
|
+
for await (const blockStorage of this.#blocks.valuesAsync(this.#computeBlockRange(start, limit))) {
|
|
131
|
+
const block = await this.getBlockFromBlockStorage(blockStorage);
|
|
132
|
+
yield block;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Gets an L2 block.
|
|
138
|
+
* @param blockNumber - The number of the block to return.
|
|
139
|
+
* @returns The requested L2 block.
|
|
140
|
+
*/
|
|
141
|
+
async getBlock(blockNumber: number): Promise<L1Published<L2Block> | undefined> {
|
|
142
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
143
|
+
if (!blockStorage || !blockStorage.header) {
|
|
144
|
+
return Promise.resolve(undefined);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return this.getBlockFromBlockStorage(blockStorage);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Gets the headers for a sequence of L2 blocks.
|
|
152
|
+
* @param start - Number of the first block to return (inclusive).
|
|
153
|
+
* @param limit - The number of blocks to return.
|
|
154
|
+
* @returns The requested L2 block headers
|
|
155
|
+
*/
|
|
156
|
+
async *getBlockHeaders(start: number, limit: number): AsyncIterableIterator<BlockHeader> {
|
|
157
|
+
for await (const blockStorage of this.#blocks.valuesAsync(this.#computeBlockRange(start, limit))) {
|
|
158
|
+
yield BlockHeader.fromBuffer(blockStorage.header);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private async getBlockFromBlockStorage(blockStorage: BlockStorage) {
|
|
163
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
164
|
+
const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
|
|
165
|
+
const blockHash = (await header.hash()).toString();
|
|
166
|
+
const blockBodyBuffer = await this.#blockBodies.getAsync(blockHash);
|
|
167
|
+
if (blockBodyBuffer === undefined) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`Could not retrieve body for block ${header.globalVariables.blockNumber.toNumber()} ${blockHash}`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
const body = Body.fromBuffer(blockBodyBuffer);
|
|
173
|
+
|
|
174
|
+
const l2Block = new L2Block(archive, header, body);
|
|
175
|
+
return { data: l2Block, l1: blockStorage.l1 };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Gets a tx effect.
|
|
180
|
+
* @param txHash - The txHash of the tx corresponding to the tx effect.
|
|
181
|
+
* @returns The requested tx effect (or undefined if not found).
|
|
182
|
+
*/
|
|
183
|
+
async getTxEffect(txHash: TxHash): Promise<InBlock<TxEffect> | undefined> {
|
|
184
|
+
const [blockNumber, txIndex] = (await this.getTxLocation(txHash)) ?? [];
|
|
185
|
+
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const block = await this.getBlock(blockNumber);
|
|
190
|
+
if (!block) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
data: block.data.body.txEffects[txIndex],
|
|
196
|
+
l2BlockNumber: block.data.number,
|
|
197
|
+
l2BlockHash: (await block.data.hash()).toString(),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Gets a receipt of a settled tx.
|
|
203
|
+
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
204
|
+
* @returns The requested tx receipt (or undefined if not found).
|
|
205
|
+
*/
|
|
206
|
+
async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
|
|
207
|
+
const [blockNumber, txIndex] = (await this.getTxLocation(txHash)) ?? [];
|
|
208
|
+
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const block = (await this.getBlock(blockNumber))!;
|
|
213
|
+
const tx = block.data.body.txEffects[txIndex];
|
|
214
|
+
|
|
215
|
+
return new TxReceipt(
|
|
216
|
+
txHash,
|
|
217
|
+
TxReceipt.statusFromRevertCode(tx.revertCode),
|
|
218
|
+
'',
|
|
219
|
+
tx.transactionFee.toBigInt(),
|
|
220
|
+
L2BlockHash.fromField(await block.data.hash()),
|
|
221
|
+
block.data.number,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Looks up which block included the requested tx effect.
|
|
227
|
+
* @param txHash - The txHash of the tx.
|
|
228
|
+
* @returns The block number and index of the tx.
|
|
229
|
+
*/
|
|
230
|
+
getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
|
|
231
|
+
return this.#txIndex.getAsync(txHash.toString());
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Looks up which block deployed a particular contract.
|
|
236
|
+
* @param contractAddress - The address of the contract to look up.
|
|
237
|
+
* @returns The block number and index of the contract.
|
|
238
|
+
*/
|
|
239
|
+
getContractLocation(contractAddress: AztecAddress): Promise<[blockNumber: number, index: number] | undefined> {
|
|
240
|
+
return this.#contractIndex.getAsync(contractAddress.toString());
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Gets the number of the latest L2 block processed.
|
|
245
|
+
* @returns The number of the latest L2 block processed.
|
|
246
|
+
*/
|
|
247
|
+
async getSynchedL2BlockNumber(): Promise<number> {
|
|
248
|
+
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
249
|
+
return typeof lastBlockNumber === 'number' ? lastBlockNumber : INITIAL_L2_BLOCK_NUM - 1;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Gets the most recent L1 block processed.
|
|
254
|
+
* @returns The L1 block that published the latest L2 block
|
|
255
|
+
*/
|
|
256
|
+
getSynchedL1BlockNumber(): Promise<bigint | undefined> {
|
|
257
|
+
return this.#lastSynchedL1Block.getAsync();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
setSynchedL1BlockNumber(l1BlockNumber: bigint) {
|
|
261
|
+
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async getProvenL2BlockNumber(): Promise<number> {
|
|
265
|
+
return (await this.#lastProvenL2Block.getAsync()) ?? 0;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
setProvenL2BlockNumber(blockNumber: number) {
|
|
269
|
+
return this.#lastProvenL2Block.set(blockNumber);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#computeBlockRange(start: number, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
273
|
+
if (limit < 1) {
|
|
274
|
+
throw new Error(`Invalid limit: ${limit}`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (start < INITIAL_L2_BLOCK_NUM) {
|
|
278
|
+
throw new Error(`Invalid start: ${start}`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return { start, limit };
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
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 type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
5
|
+
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
6
|
+
import type {
|
|
7
|
+
ContractClassPublic,
|
|
8
|
+
ContractClassPublicWithBlockNumber,
|
|
9
|
+
ExecutablePrivateFunctionWithMembershipProof,
|
|
10
|
+
UnconstrainedFunctionWithMembershipProof,
|
|
11
|
+
} from '@aztec/stdlib/contract';
|
|
12
|
+
import { Vector } from '@aztec/stdlib/types';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* LMDB implementation of the ArchiverDataStore interface.
|
|
16
|
+
*/
|
|
17
|
+
export class ContractClassStore {
|
|
18
|
+
#contractClasses: AztecAsyncMap<string, Buffer>;
|
|
19
|
+
#bytecodeCommitments: AztecAsyncMap<string, Buffer>;
|
|
20
|
+
|
|
21
|
+
constructor(private db: AztecAsyncKVStore) {
|
|
22
|
+
this.#contractClasses = db.openMap('archiver_contract_classes');
|
|
23
|
+
this.#bytecodeCommitments = db.openMap('archiver_bytecode_commitments');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async addContractClass(
|
|
27
|
+
contractClass: ContractClassPublic,
|
|
28
|
+
bytecodeCommitment: Fr,
|
|
29
|
+
blockNumber: number,
|
|
30
|
+
): 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());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async deleteContractClasses(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
|
|
39
|
+
const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
|
|
40
|
+
if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
|
|
41
|
+
await this.#contractClasses.delete(contractClass.id.toString());
|
|
42
|
+
await this.#bytecodeCommitments.delete(contractClass.id.toString());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
|
|
47
|
+
const contractClass = await this.#contractClasses.getAsync(id.toString());
|
|
48
|
+
return contractClass && { ...deserializeContractClassPublic(contractClass), id };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
|
|
52
|
+
const value = await this.#bytecodeCommitments.getAsync(id.toString());
|
|
53
|
+
return value === undefined ? undefined : Fr.fromBuffer(value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async getContractClassIds(): Promise<Fr[]> {
|
|
57
|
+
return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async addFunctions(
|
|
61
|
+
contractClassId: Fr,
|
|
62
|
+
newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
|
|
63
|
+
newUnconstrainedFunctions: UnconstrainedFunctionWithMembershipProof[],
|
|
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, unconstrainedFunctions: existingUnconstrainedFns } = 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
|
+
unconstrainedFunctions: [
|
|
81
|
+
...existingUnconstrainedFns,
|
|
82
|
+
...newUnconstrainedFunctions.filter(
|
|
83
|
+
newFn => !existingUnconstrainedFns.some(f => f.selector.equals(newFn.selector)),
|
|
84
|
+
),
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
|
|
95
|
+
return serializeToBuffer(
|
|
96
|
+
contractClass.l2BlockNumber,
|
|
97
|
+
numToUInt8(contractClass.version),
|
|
98
|
+
contractClass.artifactHash,
|
|
99
|
+
contractClass.publicFunctions.length,
|
|
100
|
+
contractClass.publicFunctions?.map(f => serializeToBuffer(f.selector, f.bytecode.length, f.bytecode)) ?? [],
|
|
101
|
+
contractClass.privateFunctions.length,
|
|
102
|
+
contractClass.privateFunctions.map(serializePrivateFunction),
|
|
103
|
+
contractClass.unconstrainedFunctions.length,
|
|
104
|
+
contractClass.unconstrainedFunctions.map(serializeUnconstrainedFunction),
|
|
105
|
+
contractClass.packedBytecode.length,
|
|
106
|
+
contractClass.packedBytecode,
|
|
107
|
+
contractClass.privateFunctionsRoot,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
|
|
112
|
+
return serializeToBuffer(
|
|
113
|
+
fn.selector,
|
|
114
|
+
fn.vkHash,
|
|
115
|
+
fn.bytecode.length,
|
|
116
|
+
fn.bytecode,
|
|
117
|
+
fn.functionMetadataHash,
|
|
118
|
+
fn.artifactMetadataHash,
|
|
119
|
+
fn.unconstrainedFunctionsArtifactTreeRoot,
|
|
120
|
+
new Vector(fn.privateFunctionTreeSiblingPath),
|
|
121
|
+
fn.privateFunctionTreeLeafIndex,
|
|
122
|
+
new Vector(fn.artifactTreeSiblingPath),
|
|
123
|
+
fn.artifactTreeLeafIndex,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function serializeUnconstrainedFunction(fn: UnconstrainedFunctionWithMembershipProof): Buffer {
|
|
128
|
+
return serializeToBuffer(
|
|
129
|
+
fn.selector,
|
|
130
|
+
fn.bytecode.length,
|
|
131
|
+
fn.bytecode,
|
|
132
|
+
fn.functionMetadataHash,
|
|
133
|
+
fn.artifactMetadataHash,
|
|
134
|
+
fn.privateFunctionsArtifactTreeRoot,
|
|
135
|
+
new Vector(fn.artifactTreeSiblingPath),
|
|
136
|
+
fn.artifactTreeLeafIndex,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
|
|
141
|
+
const reader = BufferReader.asReader(buffer);
|
|
142
|
+
return {
|
|
143
|
+
l2BlockNumber: reader.readNumber(),
|
|
144
|
+
version: reader.readUInt8() as 1,
|
|
145
|
+
artifactHash: reader.readObject(Fr),
|
|
146
|
+
publicFunctions: reader.readVector({
|
|
147
|
+
fromBuffer: reader => ({
|
|
148
|
+
selector: reader.readObject(FunctionSelector),
|
|
149
|
+
bytecode: reader.readBuffer(),
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
|
|
153
|
+
unconstrainedFunctions: reader.readVector({ fromBuffer: deserializeUnconstrainedFunction }),
|
|
154
|
+
packedBytecode: reader.readBuffer(),
|
|
155
|
+
privateFunctionsRoot: reader.readObject(Fr),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
|
|
160
|
+
const reader = BufferReader.asReader(buffer);
|
|
161
|
+
return {
|
|
162
|
+
selector: reader.readObject(FunctionSelector),
|
|
163
|
+
vkHash: reader.readObject(Fr),
|
|
164
|
+
bytecode: reader.readBuffer(),
|
|
165
|
+
functionMetadataHash: reader.readObject(Fr),
|
|
166
|
+
artifactMetadataHash: reader.readObject(Fr),
|
|
167
|
+
unconstrainedFunctionsArtifactTreeRoot: reader.readObject(Fr),
|
|
168
|
+
privateFunctionTreeSiblingPath: reader.readVector(Fr),
|
|
169
|
+
privateFunctionTreeLeafIndex: reader.readNumber(),
|
|
170
|
+
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
171
|
+
artifactTreeLeafIndex: reader.readNumber(),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function deserializeUnconstrainedFunction(buffer: Buffer | BufferReader): UnconstrainedFunctionWithMembershipProof {
|
|
176
|
+
const reader = BufferReader.asReader(buffer);
|
|
177
|
+
return {
|
|
178
|
+
selector: reader.readObject(FunctionSelector),
|
|
179
|
+
bytecode: reader.readBuffer(),
|
|
180
|
+
functionMetadataHash: reader.readObject(Fr),
|
|
181
|
+
artifactMetadataHash: reader.readObject(Fr),
|
|
182
|
+
privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
|
|
183
|
+
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
184
|
+
artifactTreeLeafIndex: reader.readNumber(),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
3
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
+
import {
|
|
5
|
+
type ContractInstanceUpdateWithAddress,
|
|
6
|
+
type ContractInstanceWithAddress,
|
|
7
|
+
SerializableContractInstance,
|
|
8
|
+
SerializableContractInstanceUpdate,
|
|
9
|
+
} from '@aztec/stdlib/contract';
|
|
10
|
+
|
|
11
|
+
type ContractInstanceUpdateKey = [string, number] | [string, number, number];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* LMDB implementation of the ArchiverDataStore interface.
|
|
15
|
+
*/
|
|
16
|
+
export class ContractInstanceStore {
|
|
17
|
+
#contractInstances: AztecAsyncMap<string, Buffer>;
|
|
18
|
+
#contractInstanceUpdates: AztecAsyncMap<ContractInstanceUpdateKey, Buffer>;
|
|
19
|
+
|
|
20
|
+
constructor(db: AztecAsyncKVStore) {
|
|
21
|
+
this.#contractInstances = db.openMap('archiver_contract_instances');
|
|
22
|
+
this.#contractInstanceUpdates = db.openMap('archiver_contract_instance_updates');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
addContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void> {
|
|
26
|
+
return this.#contractInstances.set(
|
|
27
|
+
contractInstance.address.toString(),
|
|
28
|
+
new SerializableContractInstance(contractInstance).toBuffer(),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
deleteContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void> {
|
|
33
|
+
return this.#contractInstances.delete(contractInstance.address.toString());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getUpdateKey(contractAddress: AztecAddress, blockNumber: number, logIndex?: number): ContractInstanceUpdateKey {
|
|
37
|
+
if (logIndex === undefined) {
|
|
38
|
+
return [contractAddress.toString(), blockNumber];
|
|
39
|
+
} else {
|
|
40
|
+
return [contractAddress.toString(), blockNumber, logIndex];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
addContractInstanceUpdate(
|
|
45
|
+
contractInstanceUpdate: ContractInstanceUpdateWithAddress,
|
|
46
|
+
blockNumber: number,
|
|
47
|
+
logIndex: number,
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
return this.#contractInstanceUpdates.set(
|
|
50
|
+
this.getUpdateKey(contractInstanceUpdate.address, blockNumber, logIndex),
|
|
51
|
+
new SerializableContractInstanceUpdate(contractInstanceUpdate).toBuffer(),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
deleteContractInstanceUpdate(
|
|
56
|
+
contractInstanceUpdate: ContractInstanceUpdateWithAddress,
|
|
57
|
+
blockNumber: number,
|
|
58
|
+
logIndex: number,
|
|
59
|
+
): Promise<void> {
|
|
60
|
+
return this.#contractInstanceUpdates.delete(
|
|
61
|
+
this.getUpdateKey(contractInstanceUpdate.address, blockNumber, logIndex),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async getCurrentContractInstanceClassId(
|
|
66
|
+
address: AztecAddress,
|
|
67
|
+
blockNumber: number,
|
|
68
|
+
originalClassId: Fr,
|
|
69
|
+
): Promise<Fr> {
|
|
70
|
+
// We need to find the last update before the given block number
|
|
71
|
+
const queryResult = await this.#contractInstanceUpdates
|
|
72
|
+
.valuesAsync({
|
|
73
|
+
reverse: true,
|
|
74
|
+
end: this.getUpdateKey(address, blockNumber + 1), // No update can match this key since it doesn't have a log index. We want the highest key <= blockNumber
|
|
75
|
+
limit: 1,
|
|
76
|
+
})
|
|
77
|
+
.next();
|
|
78
|
+
if (queryResult.done) {
|
|
79
|
+
return originalClassId;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const serializedUpdate = queryResult.value;
|
|
83
|
+
const update = SerializableContractInstanceUpdate.fromBuffer(serializedUpdate);
|
|
84
|
+
if (blockNumber < update.blockOfChange) {
|
|
85
|
+
return update.prevContractClassId.isZero() ? originalClassId : update.prevContractClassId;
|
|
86
|
+
}
|
|
87
|
+
return update.newContractClassId;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async getContractInstance(
|
|
91
|
+
address: AztecAddress,
|
|
92
|
+
blockNumber: number,
|
|
93
|
+
): Promise<ContractInstanceWithAddress | undefined> {
|
|
94
|
+
const contractInstance = await this.#contractInstances.getAsync(address.toString());
|
|
95
|
+
if (!contractInstance) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const instance = SerializableContractInstance.fromBuffer(contractInstance).withAddress(address);
|
|
100
|
+
instance.currentContractClassId = await this.getCurrentContractInstanceClassId(
|
|
101
|
+
address,
|
|
102
|
+
blockNumber,
|
|
103
|
+
instance.originalContractClassId,
|
|
104
|
+
);
|
|
105
|
+
return instance;
|
|
106
|
+
}
|
|
107
|
+
}
|