@aztec/archiver 0.23.0 → 0.26.1
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/dest/archiver/archiver.d.ts +15 -11
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +67 -72
- package/dest/archiver/archiver_store.d.ts +37 -15
- package/dest/archiver/archiver_store.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.js +70 -54
- package/dest/archiver/data_retrieval.d.ts +18 -8
- package/dest/archiver/data_retrieval.d.ts.map +1 -1
- package/dest/archiver/data_retrieval.js +39 -14
- package/dest/archiver/eth_log_handlers.d.ts +25 -23
- package/dest/archiver/eth_log_handlers.d.ts.map +1 -1
- package/dest/archiver/eth_log_handlers.js +98 -57
- package/dest/archiver/kv_archiver_store/block_body_store.d.ts +27 -0
- package/dest/archiver/kv_archiver_store/block_body_store.d.ts.map +1 -0
- package/dest/archiver/kv_archiver_store/block_body_store.js +47 -0
- package/dest/archiver/kv_archiver_store/block_store.d.ts +19 -11
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +58 -30
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +1 -0
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.js +4 -1
- package/dest/archiver/kv_archiver_store/contract_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_store.js +6 -3
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +36 -16
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +52 -22
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +4 -3
- package/dest/archiver/kv_archiver_store/message_store.d.ts +9 -9
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/message_store.js +34 -31
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +6 -6
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +1 -1
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +18 -18
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +41 -16
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +1 -1
- package/dest/archiver/memory_archiver_store/memory_archiver_store.js +78 -29
- package/dest/index.js +2 -2
- package/dest/rpc/archiver_client.d.ts.map +1 -1
- package/dest/rpc/archiver_client.js +3 -3
- package/dest/rpc/archiver_server.d.ts.map +1 -1
- package/dest/rpc/archiver_server.js +4 -3
- package/package.json +10 -10
- package/src/archiver/archiver.ts +625 -0
- package/src/archiver/archiver_store.ts +226 -0
- package/src/archiver/archiver_store_test_suite.ts +676 -0
- package/src/archiver/config.ts +89 -0
- package/src/archiver/data_retrieval.ts +232 -0
- package/src/archiver/eth_log_handlers.ts +342 -0
- package/src/archiver/index.ts +5 -0
- package/src/archiver/kv_archiver_store/block_body_store.ts +54 -0
- package/src/archiver/kv_archiver_store/block_store.ts +199 -0
- package/src/archiver/kv_archiver_store/contract_class_store.ts +68 -0
- package/src/archiver/kv_archiver_store/contract_instance_store.ts +26 -0
- package/src/archiver/kv_archiver_store/contract_store.ts +98 -0
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +300 -0
- package/src/archiver/kv_archiver_store/log_store.ts +174 -0
- package/src/archiver/kv_archiver_store/message_store.ts +174 -0
- package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +91 -0
- package/src/archiver/memory_archiver_store/memory_archiver_store.ts +506 -0
- package/src/index.ts +55 -0
- package/src/rpc/archiver_client.ts +35 -0
- package/src/rpc/archiver_server.ts +41 -0
- package/src/rpc/index.ts +2 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Body,
|
|
3
|
+
ContractData,
|
|
4
|
+
ExtendedContractData,
|
|
5
|
+
GetUnencryptedLogsResponse,
|
|
6
|
+
L1ToL2Message,
|
|
7
|
+
L2Block,
|
|
8
|
+
L2BlockL2Logs,
|
|
9
|
+
LogFilter,
|
|
10
|
+
LogType,
|
|
11
|
+
TxEffect,
|
|
12
|
+
TxHash,
|
|
13
|
+
TxReceipt,
|
|
14
|
+
} from '@aztec/circuit-types';
|
|
15
|
+
import { Fr } from '@aztec/circuits.js';
|
|
16
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
17
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
18
|
+
import { AztecKVStore } from '@aztec/kv-store';
|
|
19
|
+
import { ContractClassPublic, ContractInstanceWithAddress } from '@aztec/types/contracts';
|
|
20
|
+
|
|
21
|
+
import { ArchiverDataStore, ArchiverL1SynchPoint } from '../archiver_store.js';
|
|
22
|
+
import { BlockBodyStore } from './block_body_store.js';
|
|
23
|
+
import { BlockStore } from './block_store.js';
|
|
24
|
+
import { ContractClassStore } from './contract_class_store.js';
|
|
25
|
+
import { ContractInstanceStore } from './contract_instance_store.js';
|
|
26
|
+
import { ContractStore } from './contract_store.js';
|
|
27
|
+
import { LogStore } from './log_store.js';
|
|
28
|
+
import { MessageStore } from './message_store.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* LMDB implementation of the ArchiverDataStore interface.
|
|
32
|
+
*/
|
|
33
|
+
export class KVArchiverDataStore implements ArchiverDataStore {
|
|
34
|
+
#blockStore: BlockStore;
|
|
35
|
+
#blockBodyStore: BlockBodyStore;
|
|
36
|
+
#logStore: LogStore;
|
|
37
|
+
#contractStore: ContractStore;
|
|
38
|
+
#messageStore: MessageStore;
|
|
39
|
+
#contractClassStore: ContractClassStore;
|
|
40
|
+
#contractInstanceStore: ContractInstanceStore;
|
|
41
|
+
|
|
42
|
+
#log = createDebugLogger('aztec:archiver:data-store');
|
|
43
|
+
|
|
44
|
+
constructor(db: AztecKVStore, logsMaxPageSize: number = 1000) {
|
|
45
|
+
this.#blockBodyStore = new BlockBodyStore(db);
|
|
46
|
+
this.#blockStore = new BlockStore(db, this.#blockBodyStore);
|
|
47
|
+
this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
|
|
48
|
+
this.#contractStore = new ContractStore(db, this.#blockStore);
|
|
49
|
+
this.#messageStore = new MessageStore(db);
|
|
50
|
+
this.#contractClassStore = new ContractClassStore(db);
|
|
51
|
+
this.#contractInstanceStore = new ContractInstanceStore(db);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
|
|
55
|
+
return Promise.resolve(this.#contractClassStore.getContractClass(id));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getContractClassIds(): Promise<Fr[]> {
|
|
59
|
+
return Promise.resolve(this.#contractClassStore.getContractClassIds());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
|
|
63
|
+
return Promise.resolve(this.#contractInstanceStore.getContractInstance(address));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async addContractClasses(data: ContractClassPublic[], _blockNumber: number): Promise<boolean> {
|
|
67
|
+
return (await Promise.all(data.map(c => this.#contractClassStore.addContractClass(c)))).every(Boolean);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async addContractInstances(data: ContractInstanceWithAddress[], _blockNumber: number): Promise<boolean> {
|
|
71
|
+
return (await Promise.all(data.map(c => this.#contractInstanceStore.addContractInstance(c)))).every(Boolean);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Append new block bodies to the store's list.
|
|
76
|
+
* @param blockBodies - The L2 block bodies to be added to the store.
|
|
77
|
+
* @returns True if the operation is successful.
|
|
78
|
+
*/
|
|
79
|
+
addBlockBodies(blockBodies: Body[]): Promise<boolean> {
|
|
80
|
+
return this.#blockBodyStore.addBlockBodies(blockBodies);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Gets block bodies that have the same txHashes as we supply.
|
|
85
|
+
*
|
|
86
|
+
* @param txsHashes - A list of txsHashes (body hashes).
|
|
87
|
+
* @returns The requested L2 block bodies
|
|
88
|
+
*/
|
|
89
|
+
getBlockBodies(txsHashes: Buffer[]): Promise<Body[]> {
|
|
90
|
+
return this.#blockBodyStore.getBlockBodies(txsHashes);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Append new blocks to the store's list.
|
|
95
|
+
* @param blocks - The L2 blocks to be added to the store.
|
|
96
|
+
* @returns True if the operation is successful.
|
|
97
|
+
*/
|
|
98
|
+
addBlocks(blocks: L2Block[]): Promise<boolean> {
|
|
99
|
+
return this.#blockStore.addBlocks(blocks);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
104
|
+
*
|
|
105
|
+
* @param start - Number of the first block to return (inclusive).
|
|
106
|
+
* @param limit - The number of blocks to return.
|
|
107
|
+
* @returns The requested L2 blocks
|
|
108
|
+
*/
|
|
109
|
+
getBlocks(start: number, limit: number): Promise<L2Block[]> {
|
|
110
|
+
try {
|
|
111
|
+
return Promise.resolve(Array.from(this.#blockStore.getBlocks(start, limit)));
|
|
112
|
+
} catch (err) {
|
|
113
|
+
// this function is sync so if any errors are thrown we need to make sure they're passed on as rejected Promises
|
|
114
|
+
return Promise.reject(err);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets a tx effect.
|
|
120
|
+
* @param txHash - The txHash of the tx corresponding to the tx effect.
|
|
121
|
+
* @returns The requested tx effect (or undefined if not found).
|
|
122
|
+
*/
|
|
123
|
+
getTxEffect(txHash: TxHash): Promise<TxEffect | undefined> {
|
|
124
|
+
return Promise.resolve(this.#blockStore.getTxEffect(txHash));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Gets a receipt of a settled tx.
|
|
129
|
+
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
130
|
+
* @returns The requested tx receipt (or undefined if not found).
|
|
131
|
+
*/
|
|
132
|
+
getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
|
|
133
|
+
return Promise.resolve(this.#blockStore.getSettledTxReceipt(txHash));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Append new logs to the store's list.
|
|
138
|
+
* @param encryptedLogs - The logs to be added to the store.
|
|
139
|
+
* @param unencryptedLogs - The type of the logs to be added to the store.
|
|
140
|
+
* @param blockNumber - The block for which to add the logs.
|
|
141
|
+
* @returns True if the operation is successful.
|
|
142
|
+
*/
|
|
143
|
+
addLogs(
|
|
144
|
+
encryptedLogs: L2BlockL2Logs | undefined,
|
|
145
|
+
unencryptedLogs: L2BlockL2Logs | undefined,
|
|
146
|
+
blockNumber: number,
|
|
147
|
+
): Promise<boolean> {
|
|
148
|
+
return this.#logStore.addLogs(encryptedLogs, unencryptedLogs, blockNumber);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Append new pending L1 to L2 messages to the store.
|
|
153
|
+
* @param messages - The L1 to L2 messages to be added to the store.
|
|
154
|
+
* @param l1BlockNumber - The L1 block number for which to add the messages.
|
|
155
|
+
* @returns True if the operation is successful.
|
|
156
|
+
*/
|
|
157
|
+
addPendingL1ToL2Messages(messages: L1ToL2Message[], l1BlockNumber: bigint): Promise<boolean> {
|
|
158
|
+
return Promise.resolve(this.#messageStore.addPendingMessages(messages, l1BlockNumber));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Remove pending L1 to L2 messages from the store (if they were cancelled).
|
|
163
|
+
* @param messages - The entry keys to be removed from the store.
|
|
164
|
+
* @param l1BlockNumber - The L1 block number for which to remove the messages.
|
|
165
|
+
* @returns True if the operation is successful.
|
|
166
|
+
*/
|
|
167
|
+
cancelPendingL1ToL2EntryKeys(messages: Fr[], l1BlockNumber: bigint): Promise<boolean> {
|
|
168
|
+
return Promise.resolve(this.#messageStore.cancelPendingMessages(messages, l1BlockNumber));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Messages that have been published in an L2 block are confirmed.
|
|
173
|
+
* Add them to the confirmed store, also remove them from the pending store.
|
|
174
|
+
* @param entryKeys - The entry keys to be removed from the store.
|
|
175
|
+
* @param blockNumber - The block for which to add the messages.
|
|
176
|
+
* @returns True if the operation is successful.
|
|
177
|
+
*/
|
|
178
|
+
confirmL1ToL2EntryKeys(entryKeys: Fr[]): Promise<boolean> {
|
|
179
|
+
return this.#messageStore.confirmPendingMessages(entryKeys);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Gets up to `limit` amount of pending L1 to L2 messages, sorted by fee
|
|
184
|
+
* @param limit - The number of messages to return (by default NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).
|
|
185
|
+
* @returns The requested L1 to L2 entry keys.
|
|
186
|
+
*/
|
|
187
|
+
getPendingL1ToL2EntryKeys(limit: number): Promise<Fr[]> {
|
|
188
|
+
return Promise.resolve(this.#messageStore.getPendingEntryKeysByFee(limit));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Gets the confirmed L1 to L2 message corresponding to the given entry key.
|
|
193
|
+
* @param entryKey - The entry key to look up.
|
|
194
|
+
* @returns The requested L1 to L2 message or throws if not found.
|
|
195
|
+
*/
|
|
196
|
+
getConfirmedL1ToL2Message(entryKey: Fr): Promise<L1ToL2Message> {
|
|
197
|
+
try {
|
|
198
|
+
return Promise.resolve(this.#messageStore.getConfirmedMessage(entryKey));
|
|
199
|
+
} catch (err) {
|
|
200
|
+
return Promise.reject(err);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Gets up to `limit` amount of logs starting from `from`.
|
|
206
|
+
* @param start - Number of the L2 block to which corresponds the first logs to be returned.
|
|
207
|
+
* @param limit - The number of logs to return.
|
|
208
|
+
* @param logType - Specifies whether to return encrypted or unencrypted logs.
|
|
209
|
+
* @returns The requested logs.
|
|
210
|
+
*/
|
|
211
|
+
getLogs(start: number, limit: number, logType: LogType): Promise<L2BlockL2Logs[]> {
|
|
212
|
+
try {
|
|
213
|
+
return Promise.resolve(Array.from(this.#logStore.getLogs(start, limit, logType)));
|
|
214
|
+
} catch (err) {
|
|
215
|
+
return Promise.reject(err);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Gets unencrypted logs based on the provided filter.
|
|
221
|
+
* @param filter - The filter to apply to the logs.
|
|
222
|
+
* @returns The requested logs.
|
|
223
|
+
*/
|
|
224
|
+
getUnencryptedLogs(filter: LogFilter): Promise<GetUnencryptedLogsResponse> {
|
|
225
|
+
try {
|
|
226
|
+
return Promise.resolve(this.#logStore.getUnencryptedLogs(filter));
|
|
227
|
+
} catch (err) {
|
|
228
|
+
return Promise.reject(err);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Add new extended contract data from an L2 block to the store's list.
|
|
234
|
+
* @param data - List of contracts' data to be added.
|
|
235
|
+
* @param blockNum - Number of the L2 block the contract data was deployed in.
|
|
236
|
+
* @returns True if the operation is successful.
|
|
237
|
+
*/
|
|
238
|
+
addExtendedContractData(data: ExtendedContractData[], blockNum: number): Promise<boolean> {
|
|
239
|
+
return this.#contractStore.addExtendedContractData(data, blockNum);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Get the extended contract data for this contract.
|
|
244
|
+
* @param contractAddress - The contract data address.
|
|
245
|
+
* @returns The extended contract data or undefined if not found.
|
|
246
|
+
*/
|
|
247
|
+
getExtendedContractData(contractAddress: AztecAddress): Promise<ExtendedContractData | undefined> {
|
|
248
|
+
return Promise.resolve(this.#contractStore.getExtendedContractData(contractAddress));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Lookup all extended contract data in an L2 block.
|
|
253
|
+
* @param blockNumber - The block number to get all contract data from.
|
|
254
|
+
* @returns All extended contract data in the block (if found).
|
|
255
|
+
*/
|
|
256
|
+
getExtendedContractDataInBlock(blockNumber: number): Promise<ExtendedContractData[]> {
|
|
257
|
+
return Promise.resolve(Array.from(this.#contractStore.getExtendedContractDataInBlock(blockNumber)));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Get basic info for an L2 contract.
|
|
262
|
+
* Contains contract address & the ethereum portal address.
|
|
263
|
+
* @param contractAddress - The contract data address.
|
|
264
|
+
* @returns ContractData with the portal address (if we didn't throw an error).
|
|
265
|
+
*/
|
|
266
|
+
getContractData(contractAddress: AztecAddress): Promise<ContractData | undefined> {
|
|
267
|
+
return Promise.resolve(this.#contractStore.getContractData(contractAddress));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get basic info for an all L2 contracts deployed in a block.
|
|
272
|
+
* Contains contract address & the ethereum portal address.
|
|
273
|
+
* @param blockNumber - Number of the L2 block where contracts were deployed.
|
|
274
|
+
* @returns ContractData with the portal address (if we didn't throw an error).
|
|
275
|
+
*/
|
|
276
|
+
getContractDataInBlock(blockNumber: number): Promise<ContractData[]> {
|
|
277
|
+
return Promise.resolve(Array.from(this.#contractStore.getContractDataInBlock(blockNumber)));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Gets the number of the latest L2 block processed.
|
|
282
|
+
* @returns The number of the latest L2 block processed.
|
|
283
|
+
*/
|
|
284
|
+
getBlockNumber(): Promise<number> {
|
|
285
|
+
return Promise.resolve(this.#blockStore.getBlockNumber());
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Gets the last L1 block number processed by the archiver
|
|
290
|
+
*/
|
|
291
|
+
getL1BlockNumber(): Promise<ArchiverL1SynchPoint> {
|
|
292
|
+
const addedBlock = this.#blockStore.getL1BlockNumber();
|
|
293
|
+
const { addedMessages, cancelledMessages } = this.#messageStore.getL1BlockNumber();
|
|
294
|
+
return Promise.resolve({
|
|
295
|
+
addedBlock,
|
|
296
|
+
addedMessages,
|
|
297
|
+
cancelledMessages,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExtendedUnencryptedL2Log,
|
|
3
|
+
GetUnencryptedLogsResponse,
|
|
4
|
+
L2BlockL2Logs,
|
|
5
|
+
LogFilter,
|
|
6
|
+
LogId,
|
|
7
|
+
LogType,
|
|
8
|
+
UnencryptedL2Log,
|
|
9
|
+
} from '@aztec/circuit-types';
|
|
10
|
+
import { INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js/constants';
|
|
11
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
12
|
+
import { AztecKVStore, AztecMap } from '@aztec/kv-store';
|
|
13
|
+
|
|
14
|
+
import { BlockStore } from './block_store.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A store for logs
|
|
18
|
+
*/
|
|
19
|
+
export class LogStore {
|
|
20
|
+
#encryptedLogs: AztecMap<number, Buffer>;
|
|
21
|
+
#unencryptedLogs: AztecMap<number, Buffer>;
|
|
22
|
+
#logsMaxPageSize: number;
|
|
23
|
+
#log = createDebugLogger('aztec:archiver:log_store');
|
|
24
|
+
|
|
25
|
+
constructor(private db: AztecKVStore, private blockStore: BlockStore, logsMaxPageSize: number = 1000) {
|
|
26
|
+
this.#encryptedLogs = db.openMap('archiver_encrypted_logs');
|
|
27
|
+
this.#unencryptedLogs = db.openMap('archiver_unencrypted_logs');
|
|
28
|
+
|
|
29
|
+
this.#logsMaxPageSize = logsMaxPageSize;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Append new logs to the store's list.
|
|
34
|
+
* @param encryptedLogs - The logs to be added to the store.
|
|
35
|
+
* @param unencryptedLogs - The type of the logs to be added to the store.
|
|
36
|
+
* @param blockNumber - The block for which to add the logs.
|
|
37
|
+
* @returns True if the operation is successful.
|
|
38
|
+
*/
|
|
39
|
+
addLogs(
|
|
40
|
+
encryptedLogs: L2BlockL2Logs | undefined,
|
|
41
|
+
unencryptedLogs: L2BlockL2Logs | undefined,
|
|
42
|
+
blockNumber: number,
|
|
43
|
+
): Promise<boolean> {
|
|
44
|
+
return this.db.transaction(() => {
|
|
45
|
+
if (encryptedLogs) {
|
|
46
|
+
void this.#encryptedLogs.set(blockNumber, encryptedLogs.toBuffer());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (unencryptedLogs) {
|
|
50
|
+
void this.#unencryptedLogs.set(blockNumber, unencryptedLogs.toBuffer());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return true;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Gets up to `limit` amount of logs starting from `from`.
|
|
59
|
+
* @param start - Number of the L2 block to which corresponds the first logs to be returned.
|
|
60
|
+
* @param limit - The number of logs to return.
|
|
61
|
+
* @param logType - Specifies whether to return encrypted or unencrypted logs.
|
|
62
|
+
* @returns The requested logs.
|
|
63
|
+
*/
|
|
64
|
+
*getLogs(start: number, limit: number, logType: LogType): IterableIterator<L2BlockL2Logs> {
|
|
65
|
+
const logMap = logType === LogType.ENCRYPTED ? this.#encryptedLogs : this.#unencryptedLogs;
|
|
66
|
+
for (const buffer of logMap.values({ start, limit })) {
|
|
67
|
+
yield L2BlockL2Logs.fromBuffer(buffer);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Gets unencrypted logs based on the provided filter.
|
|
73
|
+
* @param filter - The filter to apply to the logs.
|
|
74
|
+
* @returns The requested logs.
|
|
75
|
+
*/
|
|
76
|
+
getUnencryptedLogs(filter: LogFilter): GetUnencryptedLogsResponse {
|
|
77
|
+
if (filter.afterLog) {
|
|
78
|
+
return this.#filterUnencryptedLogsBetweenBlocks(filter);
|
|
79
|
+
} else if (filter.txHash) {
|
|
80
|
+
return this.#filterUnencryptedLogsOfTx(filter);
|
|
81
|
+
} else {
|
|
82
|
+
return this.#filterUnencryptedLogsBetweenBlocks(filter);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#filterUnencryptedLogsOfTx(filter: LogFilter): GetUnencryptedLogsResponse {
|
|
87
|
+
if (!filter.txHash) {
|
|
88
|
+
throw new Error('Missing txHash');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const [blockNumber, txIndex] = this.blockStore.getTxLocation(filter.txHash) ?? [];
|
|
92
|
+
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
|
|
93
|
+
return { logs: [], maxLogsHit: false };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const unencryptedLogsInBlock = this.#getBlockLogs(blockNumber, LogType.UNENCRYPTED);
|
|
97
|
+
const txLogs = unencryptedLogsInBlock.txLogs[txIndex].unrollLogs().map(log => UnencryptedL2Log.fromBuffer(log));
|
|
98
|
+
|
|
99
|
+
const logs: ExtendedUnencryptedL2Log[] = [];
|
|
100
|
+
const maxLogsHit = this.#accumulateLogs(logs, blockNumber, txIndex, txLogs, filter);
|
|
101
|
+
|
|
102
|
+
return { logs, maxLogsHit };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
#filterUnencryptedLogsBetweenBlocks(filter: LogFilter): GetUnencryptedLogsResponse {
|
|
106
|
+
const start =
|
|
107
|
+
filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
|
|
108
|
+
const end = filter.toBlock;
|
|
109
|
+
|
|
110
|
+
if (typeof end === 'number' && end < start) {
|
|
111
|
+
return {
|
|
112
|
+
logs: [],
|
|
113
|
+
maxLogsHit: true,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const logs: ExtendedUnencryptedL2Log[] = [];
|
|
118
|
+
|
|
119
|
+
let maxLogsHit = false;
|
|
120
|
+
loopOverBlocks: for (const [blockNumber, logBuffer] of this.#unencryptedLogs.entries({ start, end })) {
|
|
121
|
+
const unencryptedLogsInBlock = L2BlockL2Logs.fromBuffer(logBuffer);
|
|
122
|
+
for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < unencryptedLogsInBlock.txLogs.length; txIndex++) {
|
|
123
|
+
const txLogs = unencryptedLogsInBlock.txLogs[txIndex].unrollLogs().map(log => UnencryptedL2Log.fromBuffer(log));
|
|
124
|
+
maxLogsHit = this.#accumulateLogs(logs, blockNumber, txIndex, txLogs, filter);
|
|
125
|
+
if (maxLogsHit) {
|
|
126
|
+
this.#log(`Max logs hit at block ${blockNumber}`);
|
|
127
|
+
break loopOverBlocks;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { logs, maxLogsHit };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#accumulateLogs(
|
|
136
|
+
results: ExtendedUnencryptedL2Log[],
|
|
137
|
+
blockNumber: number,
|
|
138
|
+
txIndex: number,
|
|
139
|
+
txLogs: UnencryptedL2Log[],
|
|
140
|
+
filter: LogFilter,
|
|
141
|
+
): boolean {
|
|
142
|
+
let maxLogsHit = false;
|
|
143
|
+
let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
|
|
144
|
+
for (; logIndex < txLogs.length; logIndex++) {
|
|
145
|
+
const log = txLogs[logIndex];
|
|
146
|
+
if (filter.contractAddress && !log.contractAddress.equals(filter.contractAddress)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (filter.selector && !log.selector.equals(filter.selector)) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
results.push(new ExtendedUnencryptedL2Log(new LogId(blockNumber, txIndex, logIndex), log));
|
|
155
|
+
if (results.length >= this.#logsMaxPageSize) {
|
|
156
|
+
maxLogsHit = true;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return maxLogsHit;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#getBlockLogs(blockNumber: number, logType: LogType): L2BlockL2Logs {
|
|
165
|
+
const logMap = logType === LogType.ENCRYPTED ? this.#encryptedLogs : this.#unencryptedLogs;
|
|
166
|
+
const buffer = logMap.get(blockNumber);
|
|
167
|
+
|
|
168
|
+
if (!buffer) {
|
|
169
|
+
return new L2BlockL2Logs([]);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return L2BlockL2Logs.fromBuffer(buffer);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { L1ToL2Message } from '@aztec/circuit-types';
|
|
2
|
+
import { Fr } from '@aztec/circuits.js';
|
|
3
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { AztecCounter, AztecKVStore, AztecMap, AztecSingleton } from '@aztec/kv-store';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A message stored in the database
|
|
8
|
+
*/
|
|
9
|
+
type Message = {
|
|
10
|
+
/** The L1ToL2Message */
|
|
11
|
+
message: Buffer;
|
|
12
|
+
/** The message's fee */
|
|
13
|
+
fee: number;
|
|
14
|
+
/** Has it _ever_ been confirmed? */
|
|
15
|
+
confirmed: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* LMDB implementation of the ArchiverDataStore interface.
|
|
20
|
+
*/
|
|
21
|
+
export class MessageStore {
|
|
22
|
+
#messages: AztecMap<string, Message>;
|
|
23
|
+
#pendingMessagesByFee: AztecCounter<[number, string]>;
|
|
24
|
+
#lastL1BlockAddingMessages: AztecSingleton<bigint>;
|
|
25
|
+
#lastL1BlockCancellingMessages: AztecSingleton<bigint>;
|
|
26
|
+
|
|
27
|
+
#log = createDebugLogger('aztec:archiver:message_store');
|
|
28
|
+
|
|
29
|
+
constructor(private db: AztecKVStore) {
|
|
30
|
+
this.#messages = db.openMap('archiver_l1_to_l2_messages');
|
|
31
|
+
this.#pendingMessagesByFee = db.openCounter('archiver_messages_by_fee');
|
|
32
|
+
this.#lastL1BlockAddingMessages = db.openSingleton('archiver_last_l1_block_adding_messages');
|
|
33
|
+
this.#lastL1BlockCancellingMessages = db.openSingleton('archiver_last_l1_block_cancelling_messages');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets the last L1 block number that emitted new messages and the block that cancelled messages.
|
|
38
|
+
* @returns The last L1 block number processed
|
|
39
|
+
*/
|
|
40
|
+
getL1BlockNumber() {
|
|
41
|
+
return {
|
|
42
|
+
addedMessages: this.#lastL1BlockAddingMessages.get() ?? 0n,
|
|
43
|
+
cancelledMessages: this.#lastL1BlockCancellingMessages.get() ?? 0n,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Append new pending L1 to L2 messages to the store.
|
|
49
|
+
* @param messages - The L1 to L2 messages to be added to the store.
|
|
50
|
+
* @param l1BlockNumber - The L1 block number for which to add the messages.
|
|
51
|
+
* @returns True if the operation is successful.
|
|
52
|
+
*/
|
|
53
|
+
addPendingMessages(messages: L1ToL2Message[], l1BlockNumber: bigint): Promise<boolean> {
|
|
54
|
+
return this.db.transaction(() => {
|
|
55
|
+
const lastL1BlockNumber = this.#lastL1BlockAddingMessages.get() ?? 0n;
|
|
56
|
+
if (lastL1BlockNumber >= l1BlockNumber) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
void this.#lastL1BlockAddingMessages.set(l1BlockNumber);
|
|
61
|
+
|
|
62
|
+
for (const message of messages) {
|
|
63
|
+
const entryKey = message.entryKey?.toString();
|
|
64
|
+
if (!entryKey) {
|
|
65
|
+
throw new Error('Message does not have an entry key');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void this.#messages.setIfNotExists(entryKey, {
|
|
69
|
+
message: message.toBuffer(),
|
|
70
|
+
fee: message.fee,
|
|
71
|
+
confirmed: false,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
void this.#pendingMessagesByFee.update([message.fee, entryKey], 1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return true;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Remove pending L1 to L2 messages from the store (if they were cancelled).
|
|
83
|
+
* @param entryKeys - The entry keys to be removed from the store.
|
|
84
|
+
* @param l1BlockNumber - The L1 block number for which to remove the messages.
|
|
85
|
+
* @returns True if the operation is successful.
|
|
86
|
+
*/
|
|
87
|
+
cancelPendingMessages(entryKeys: Fr[], l1BlockNumber: bigint): Promise<boolean> {
|
|
88
|
+
return this.db.transaction(() => {
|
|
89
|
+
const lastL1BlockNumber = this.#lastL1BlockCancellingMessages.get() ?? 0n;
|
|
90
|
+
if (lastL1BlockNumber >= l1BlockNumber) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
void this.#lastL1BlockCancellingMessages.set(l1BlockNumber);
|
|
95
|
+
|
|
96
|
+
for (const entryKey of entryKeys) {
|
|
97
|
+
const messageCtx = this.#messages.get(entryKey.toString());
|
|
98
|
+
if (!messageCtx) {
|
|
99
|
+
throw new Error(`Message ${entryKey.toString()} not found`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void this.#pendingMessagesByFee.update([messageCtx.fee, entryKey.toString()], -1);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return true;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Messages that have been published in an L2 block are confirmed.
|
|
111
|
+
* Add them to the confirmed store, also remove them from the pending store.
|
|
112
|
+
* @param entryKeys - The entry keys to be removed from the store.
|
|
113
|
+
* @returns True if the operation is successful.
|
|
114
|
+
*/
|
|
115
|
+
confirmPendingMessages(entryKeys: Fr[]): Promise<boolean> {
|
|
116
|
+
return this.db.transaction(() => {
|
|
117
|
+
for (const entryKey of entryKeys) {
|
|
118
|
+
if (entryKey.equals(Fr.ZERO)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const messageCtx = this.#messages.get(entryKey.toString());
|
|
123
|
+
if (!messageCtx) {
|
|
124
|
+
throw new Error(`Message ${entryKey.toString()} not found`);
|
|
125
|
+
}
|
|
126
|
+
messageCtx.confirmed = true;
|
|
127
|
+
|
|
128
|
+
void this.#messages.set(entryKey.toString(), messageCtx);
|
|
129
|
+
void this.#pendingMessagesByFee.update([messageCtx.fee, entryKey.toString()], -1);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return true;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Gets the confirmed L1 to L2 message corresponding to the given entry key.
|
|
138
|
+
* @param entryKey - The entry key to look up.
|
|
139
|
+
* @returns The requested L1 to L2 message or throws if not found.
|
|
140
|
+
*/
|
|
141
|
+
getConfirmedMessage(entryKey: Fr): L1ToL2Message {
|
|
142
|
+
const messageCtx = this.#messages.get(entryKey.toString());
|
|
143
|
+
if (!messageCtx) {
|
|
144
|
+
throw new Error(`Message ${entryKey.toString()} not found`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!messageCtx.confirmed) {
|
|
148
|
+
throw new Error(`Message ${entryKey.toString()} not confirmed`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return L1ToL2Message.fromBuffer(messageCtx.message);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Gets up to `limit` amount of pending L1 to L2 messages, sorted by fee
|
|
156
|
+
* @param limit - The number of messages to return (by default NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).
|
|
157
|
+
* @returns The requested L1 to L2 entry keys.
|
|
158
|
+
*/
|
|
159
|
+
getPendingEntryKeysByFee(limit: number): Fr[] {
|
|
160
|
+
const entryKeys: Fr[] = [];
|
|
161
|
+
|
|
162
|
+
for (const [[_, entryKey], count] of this.#pendingMessagesByFee.entries({
|
|
163
|
+
reverse: true,
|
|
164
|
+
})) {
|
|
165
|
+
// put `count` copies of this message in the result list
|
|
166
|
+
entryKeys.push(...Array(count).fill(Fr.fromString(entryKey)));
|
|
167
|
+
if (entryKeys.length >= limit) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return entryKeys;
|
|
173
|
+
}
|
|
174
|
+
}
|