@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.0208eb9
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 +164 -9
- package/dest/archiver.d.ts +136 -0
- package/dest/archiver.d.ts.map +1 -0
- package/dest/archiver.js +781 -0
- package/dest/config.d.ts +30 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +71 -0
- package/dest/errors.d.ts +41 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +62 -0
- package/dest/factory.d.ts +13 -16
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +103 -55
- package/dest/index.d.ts +10 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +8 -3
- package/dest/interfaces.d.ts +9 -0
- package/dest/interfaces.d.ts.map +1 -0
- package/dest/interfaces.js +3 -0
- package/dest/l1/bin/retrieve-calldata.d.ts +3 -0
- package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
- package/dest/l1/bin/retrieve-calldata.js +149 -0
- package/dest/l1/calldata_retriever.d.ts +112 -0
- package/dest/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/l1/calldata_retriever.js +471 -0
- package/dest/l1/data_retrieval.d.ts +88 -0
- package/dest/l1/data_retrieval.d.ts.map +1 -0
- package/dest/l1/data_retrieval.js +312 -0
- package/dest/l1/debug_tx.d.ts +19 -0
- package/dest/l1/debug_tx.d.ts.map +1 -0
- package/dest/l1/debug_tx.js +73 -0
- package/dest/l1/spire_proposer.d.ts +70 -0
- package/dest/l1/spire_proposer.d.ts.map +1 -0
- package/dest/l1/spire_proposer.js +157 -0
- package/dest/l1/trace_tx.d.ts +97 -0
- package/dest/l1/trace_tx.d.ts.map +1 -0
- package/dest/l1/trace_tx.js +91 -0
- package/dest/l1/types.d.ts +12 -0
- package/dest/l1/types.d.ts.map +1 -0
- package/dest/l1/types.js +3 -0
- package/dest/l1/validate_trace.d.ts +32 -0
- package/dest/l1/validate_trace.d.ts.map +1 -0
- package/dest/l1/validate_trace.js +154 -0
- package/dest/modules/data_source_base.d.ts +84 -0
- package/dest/modules/data_source_base.d.ts.map +1 -0
- package/dest/modules/data_source_base.js +260 -0
- package/dest/modules/data_store_updater.d.ts +73 -0
- package/dest/modules/data_store_updater.d.ts.map +1 -0
- package/dest/modules/data_store_updater.js +302 -0
- package/dest/modules/instrumentation.d.ts +37 -0
- package/dest/modules/instrumentation.d.ts.map +1 -0
- package/dest/modules/instrumentation.js +110 -0
- package/dest/modules/l1_synchronizer.d.ts +75 -0
- package/dest/modules/l1_synchronizer.d.ts.map +1 -0
- package/dest/modules/l1_synchronizer.js +1112 -0
- package/dest/modules/validation.d.ts +17 -0
- package/dest/modules/validation.d.ts.map +1 -0
- package/dest/modules/validation.js +104 -0
- package/dest/store/block_store.d.ts +192 -0
- package/dest/store/block_store.d.ts.map +1 -0
- package/dest/store/block_store.js +721 -0
- package/dest/store/contract_class_store.d.ts +18 -0
- package/dest/store/contract_class_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +25 -27
- package/dest/store/contract_instance_store.d.ts +24 -0
- package/dest/store/contract_instance_store.d.ts.map +1 -0
- package/dest/store/contract_instance_store.js +77 -0
- package/dest/store/kv_archiver_store.d.ts +340 -0
- package/dest/store/kv_archiver_store.d.ts.map +1 -0
- package/dest/store/kv_archiver_store.js +446 -0
- package/dest/store/log_store.d.ts +54 -0
- package/dest/store/log_store.d.ts.map +1 -0
- package/dest/store/log_store.js +436 -0
- package/dest/store/message_store.d.ts +40 -0
- package/dest/store/message_store.d.ts.map +1 -0
- package/dest/store/message_store.js +188 -0
- package/dest/structs/data_retrieval.d.ts +27 -0
- package/dest/structs/data_retrieval.d.ts.map +1 -0
- package/dest/structs/inbox_message.d.ts +15 -0
- package/dest/structs/inbox_message.d.ts.map +1 -0
- package/dest/structs/inbox_message.js +39 -0
- package/dest/structs/published.d.ts +2 -0
- package/dest/structs/published.d.ts.map +1 -0
- package/dest/structs/published.js +1 -0
- package/dest/test/fake_l1_state.d.ts +190 -0
- package/dest/test/fake_l1_state.d.ts.map +1 -0
- package/dest/test/fake_l1_state.js +383 -0
- package/dest/test/index.d.ts +2 -1
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +4 -1
- package/dest/test/mock_archiver.d.ts +16 -8
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +18 -14
- package/dest/test/mock_l1_to_l2_message_source.d.ts +9 -6
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +30 -7
- package/dest/test/mock_l2_block_source.d.ts +62 -16
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +263 -32
- package/dest/test/mock_structs.d.ts +85 -0
- package/dest/test/mock_structs.d.ts.map +1 -0
- package/dest/test/mock_structs.js +171 -0
- package/dest/test/noop_l1_archiver.d.ts +23 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +68 -0
- package/package.json +31 -33
- package/src/archiver.ts +543 -0
- package/src/config.ts +95 -0
- package/src/errors.ts +102 -0
- package/src/factory.ts +144 -71
- package/src/index.ts +10 -3
- package/src/interfaces.ts +9 -0
- package/src/l1/README.md +98 -0
- package/src/l1/bin/retrieve-calldata.ts +187 -0
- package/src/l1/calldata_retriever.ts +641 -0
- package/src/l1/data_retrieval.ts +495 -0
- package/src/l1/debug_tx.ts +99 -0
- package/src/l1/spire_proposer.ts +160 -0
- package/src/l1/trace_tx.ts +128 -0
- package/src/l1/types.ts +13 -0
- package/src/l1/validate_trace.ts +229 -0
- package/src/modules/data_source_base.ts +367 -0
- package/src/modules/data_store_updater.ts +423 -0
- package/src/modules/instrumentation.ts +157 -0
- package/src/modules/l1_synchronizer.ts +930 -0
- package/src/modules/validation.ts +129 -0
- package/src/store/block_store.ts +966 -0
- package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +26 -32
- package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +37 -29
- package/src/store/kv_archiver_store.ts +639 -0
- package/src/store/log_store.ts +575 -0
- package/src/store/message_store.ts +261 -0
- package/src/structs/inbox_message.ts +41 -0
- package/src/structs/published.ts +1 -0
- package/src/test/fake_l1_state.ts +599 -0
- package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
- package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
- package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
- package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
- package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
- package/src/test/fixtures/trace_transaction-proxied.json +128 -0
- package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
- package/src/test/index.ts +4 -0
- package/src/test/mock_archiver.ts +22 -16
- package/src/test/mock_l1_to_l2_message_source.ts +26 -8
- package/src/test/mock_l2_block_source.ts +313 -42
- package/src/test/mock_structs.ts +311 -0
- package/src/test/noop_l1_archiver.ts +109 -0
- package/dest/archiver/archiver.d.ts +0 -197
- package/dest/archiver/archiver.d.ts.map +0 -1
- package/dest/archiver/archiver.js +0 -900
- package/dest/archiver/archiver_store.d.ts +0 -220
- package/dest/archiver/archiver_store.d.ts.map +0 -1
- package/dest/archiver/archiver_store.js +0 -4
- package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
- package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
- package/dest/archiver/archiver_store_test_suite.js +0 -794
- package/dest/archiver/config.d.ts +0 -37
- package/dest/archiver/config.d.ts.map +0 -1
- package/dest/archiver/config.js +0 -46
- package/dest/archiver/data_retrieval.d.ts +0 -74
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/data_retrieval.js +0 -283
- package/dest/archiver/errors.d.ts +0 -4
- package/dest/archiver/errors.d.ts.map +0 -1
- package/dest/archiver/errors.js +0 -5
- package/dest/archiver/index.d.ts +0 -8
- package/dest/archiver/index.d.ts.map +0 -1
- package/dest/archiver/index.js +0 -5
- package/dest/archiver/instrumentation.d.ts +0 -29
- package/dest/archiver/instrumentation.d.ts.map +0 -1
- package/dest/archiver/instrumentation.js +0 -99
- package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -87
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/block_store.js +0 -217
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -21
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.js +0 -63
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -153
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -254
- package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/log_store.js +0 -364
- package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -33
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/message_store.js +0 -85
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
- package/dest/archiver/structs/data_retrieval.d.ts +0 -27
- package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/structs/published.d.ts +0 -11
- package/dest/archiver/structs/published.d.ts.map +0 -1
- package/dest/archiver/structs/published.js +0 -1
- package/dest/rpc/index.d.ts +0 -10
- package/dest/rpc/index.d.ts.map +0 -1
- package/dest/rpc/index.js +0 -18
- package/src/archiver/archiver.ts +0 -1181
- package/src/archiver/archiver_store.ts +0 -263
- package/src/archiver/archiver_store_test_suite.ts +0 -810
- package/src/archiver/config.ts +0 -92
- package/src/archiver/data_retrieval.ts +0 -422
- package/src/archiver/errors.ts +0 -5
- package/src/archiver/index.ts +0 -7
- package/src/archiver/instrumentation.ts +0 -132
- package/src/archiver/kv_archiver_store/block_store.ts +0 -283
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -358
- package/src/archiver/kv_archiver_store/log_store.ts +0 -444
- package/src/archiver/kv_archiver_store/message_store.ts +0 -102
- package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
- package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
- package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
- package/src/archiver/structs/published.ts +0 -11
- package/src/rpc/index.ts +0 -20
- /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
- /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { filterAsync } from '@aztec/foundation/collection';
|
|
4
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
5
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
6
|
+
import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
|
|
7
|
+
import { BlockHash } from '@aztec/stdlib/block';
|
|
8
|
+
import { MAX_LOGS_PER_TAG } from '@aztec/stdlib/interfaces/api-limit';
|
|
9
|
+
import { ContractClassLog, ExtendedContractClassLog, ExtendedPublicLog, LogId, PublicLog, TxScopedL2Log } from '@aztec/stdlib/logs';
|
|
10
|
+
/**
|
|
11
|
+
* A store for logs
|
|
12
|
+
*/ export class LogStore {
|
|
13
|
+
db;
|
|
14
|
+
blockStore;
|
|
15
|
+
// `tag` --> private logs
|
|
16
|
+
#privateLogsByTag;
|
|
17
|
+
// `{contractAddress}_${tag}` --> public logs
|
|
18
|
+
#publicLogsByContractAndTag;
|
|
19
|
+
#privateLogKeysByBlock;
|
|
20
|
+
#publicLogKeysByBlock;
|
|
21
|
+
#publicLogsByBlock;
|
|
22
|
+
#contractClassLogsByBlock;
|
|
23
|
+
#logsMaxPageSize;
|
|
24
|
+
#log;
|
|
25
|
+
constructor(db, blockStore, logsMaxPageSize = 1000){
|
|
26
|
+
this.db = db;
|
|
27
|
+
this.blockStore = blockStore;
|
|
28
|
+
this.#log = createLogger('archiver:log_store');
|
|
29
|
+
this.#privateLogsByTag = db.openMap('archiver_private_tagged_logs_by_tag');
|
|
30
|
+
this.#publicLogsByContractAndTag = db.openMap('archiver_public_tagged_logs_by_tag');
|
|
31
|
+
this.#privateLogKeysByBlock = db.openMap('archiver_private_log_keys_by_block');
|
|
32
|
+
this.#publicLogKeysByBlock = db.openMap('archiver_public_log_keys_by_block');
|
|
33
|
+
this.#publicLogsByBlock = db.openMap('archiver_public_logs_by_block');
|
|
34
|
+
this.#contractClassLogsByBlock = db.openMap('archiver_contract_class_logs_by_block');
|
|
35
|
+
this.#logsMaxPageSize = logsMaxPageSize;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Extracts tagged logs from a single block, grouping them into private and public maps.
|
|
39
|
+
*
|
|
40
|
+
* @param block - The L2 block to extract logs from.
|
|
41
|
+
* @returns An object containing the private and public tagged logs for the block.
|
|
42
|
+
*/ #extractTaggedLogsFromBlock(block) {
|
|
43
|
+
// SiloedTag (as string) -> array of log buffers.
|
|
44
|
+
const privateTaggedLogs = new Map();
|
|
45
|
+
// "{contractAddress}_{tag}" (as string) -> array of log buffers.
|
|
46
|
+
const publicTaggedLogs = new Map();
|
|
47
|
+
block.body.txEffects.forEach((txEffect)=>{
|
|
48
|
+
const txHash = txEffect.txHash;
|
|
49
|
+
txEffect.privateLogs.forEach((log)=>{
|
|
50
|
+
// Private logs use SiloedTag (already siloed by kernel)
|
|
51
|
+
const tag = log.fields[0];
|
|
52
|
+
this.#log.debug(`Found private log with tag ${tag.toString()} in block ${block.number}`);
|
|
53
|
+
const currentLogs = privateTaggedLogs.get(tag.toString()) ?? [];
|
|
54
|
+
currentLogs.push(new TxScopedL2Log(txHash, block.number, block.timestamp, log.getEmittedFields(), txEffect.noteHashes, txEffect.nullifiers[0]).toBuffer());
|
|
55
|
+
privateTaggedLogs.set(tag.toString(), currentLogs);
|
|
56
|
+
});
|
|
57
|
+
txEffect.publicLogs.forEach((log)=>{
|
|
58
|
+
// Public logs use Tag directly (not siloed) and are stored with contract address
|
|
59
|
+
const tag = log.fields[0];
|
|
60
|
+
const contractAddress = log.contractAddress;
|
|
61
|
+
const key = `${contractAddress.toString()}_${tag.toString()}`;
|
|
62
|
+
this.#log.debug(`Found public log with tag ${tag.toString()} from contract ${contractAddress.toString()} in block ${block.number}`);
|
|
63
|
+
const currentLogs = publicTaggedLogs.get(key) ?? [];
|
|
64
|
+
currentLogs.push(new TxScopedL2Log(txHash, block.number, block.timestamp, log.getEmittedFields(), txEffect.noteHashes, txEffect.nullifiers[0]).toBuffer());
|
|
65
|
+
publicTaggedLogs.set(key, currentLogs);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
privateTaggedLogs,
|
|
70
|
+
publicTaggedLogs
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Extracts and aggregates tagged logs from a list of blocks.
|
|
75
|
+
* @param blocks - The blocks to extract logs from.
|
|
76
|
+
* @returns A map from tag (as string) to an array of serialized private logs belonging to that tag, and a map from
|
|
77
|
+
* "{contractAddress}_{tag}" (as string) to an array of serialized public logs belonging to that key.
|
|
78
|
+
*/ #extractTaggedLogs(blocks) {
|
|
79
|
+
const taggedLogsInBlocks = blocks.map((block)=>this.#extractTaggedLogsFromBlock(block));
|
|
80
|
+
// Now we merge the maps from each block into a single map.
|
|
81
|
+
const privateTaggedLogs = taggedLogsInBlocks.reduce((acc, { privateTaggedLogs })=>{
|
|
82
|
+
for (const [tag, logs] of privateTaggedLogs.entries()){
|
|
83
|
+
const currentLogs = acc.get(tag) ?? [];
|
|
84
|
+
acc.set(tag, currentLogs.concat(logs));
|
|
85
|
+
}
|
|
86
|
+
return acc;
|
|
87
|
+
}, new Map());
|
|
88
|
+
const publicTaggedLogs = taggedLogsInBlocks.reduce((acc, { publicTaggedLogs })=>{
|
|
89
|
+
for (const [key, logs] of publicTaggedLogs.entries()){
|
|
90
|
+
const currentLogs = acc.get(key) ?? [];
|
|
91
|
+
acc.set(key, currentLogs.concat(logs));
|
|
92
|
+
}
|
|
93
|
+
return acc;
|
|
94
|
+
}, new Map());
|
|
95
|
+
return {
|
|
96
|
+
privateTaggedLogs,
|
|
97
|
+
publicTaggedLogs
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async #addPrivateLogs(blocks) {
|
|
101
|
+
const newBlocks = await filterAsync(blocks, async (block)=>!await this.#privateLogKeysByBlock.hasAsync(block.number));
|
|
102
|
+
const { privateTaggedLogs } = this.#extractTaggedLogs(newBlocks);
|
|
103
|
+
const keysOfPrivateLogsToUpdate = Array.from(privateTaggedLogs.keys());
|
|
104
|
+
const currentPrivateTaggedLogs = await Promise.all(keysOfPrivateLogsToUpdate.map(async (key)=>({
|
|
105
|
+
tag: key,
|
|
106
|
+
logBuffers: await this.#privateLogsByTag.getAsync(key)
|
|
107
|
+
})));
|
|
108
|
+
for (const taggedLogBuffer of currentPrivateTaggedLogs){
|
|
109
|
+
if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
|
|
110
|
+
privateTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(privateTaggedLogs.get(taggedLogBuffer.tag)));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const block of newBlocks){
|
|
114
|
+
const privateTagsInBlock = [];
|
|
115
|
+
for (const [tag, logs] of privateTaggedLogs.entries()){
|
|
116
|
+
await this.#privateLogsByTag.set(tag, logs);
|
|
117
|
+
privateTagsInBlock.push(tag);
|
|
118
|
+
}
|
|
119
|
+
await this.#privateLogKeysByBlock.set(block.number, privateTagsInBlock);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async #addPublicLogs(blocks) {
|
|
123
|
+
const newBlocks = await filterAsync(blocks, async (block)=>!await this.#publicLogKeysByBlock.hasAsync(block.number));
|
|
124
|
+
const { publicTaggedLogs } = this.#extractTaggedLogs(newBlocks);
|
|
125
|
+
const keysOfPublicLogsToUpdate = Array.from(publicTaggedLogs.keys());
|
|
126
|
+
const currentPublicTaggedLogs = await Promise.all(keysOfPublicLogsToUpdate.map(async (key)=>({
|
|
127
|
+
tag: key,
|
|
128
|
+
logBuffers: await this.#publicLogsByContractAndTag.getAsync(key)
|
|
129
|
+
})));
|
|
130
|
+
for (const taggedLogBuffer of currentPublicTaggedLogs){
|
|
131
|
+
if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
|
|
132
|
+
publicTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(publicTaggedLogs.get(taggedLogBuffer.tag)));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const block of newBlocks){
|
|
136
|
+
const blockHash = await block.hash();
|
|
137
|
+
const publicTagsInBlock = [];
|
|
138
|
+
for (const [tag, logs] of publicTaggedLogs.entries()){
|
|
139
|
+
await this.#publicLogsByContractAndTag.set(tag, logs);
|
|
140
|
+
publicTagsInBlock.push(tag);
|
|
141
|
+
}
|
|
142
|
+
await this.#publicLogKeysByBlock.set(block.number, publicTagsInBlock);
|
|
143
|
+
const publicLogsInBlock = block.body.txEffects.map((txEffect, txIndex)=>[
|
|
144
|
+
numToUInt32BE(txIndex),
|
|
145
|
+
numToUInt32BE(txEffect.publicLogs.length),
|
|
146
|
+
txEffect.publicLogs.map((log)=>log.toBuffer())
|
|
147
|
+
].flat()).flat();
|
|
148
|
+
await this.#publicLogsByBlock.set(block.number, this.#packWithBlockHash(blockHash, publicLogsInBlock));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async #addContractClassLogs(blocks) {
|
|
152
|
+
const newBlocks = await filterAsync(blocks, async (block)=>!await this.#contractClassLogsByBlock.hasAsync(block.number));
|
|
153
|
+
for (const block of newBlocks){
|
|
154
|
+
const blockHash = await block.hash();
|
|
155
|
+
const contractClassLogsInBlock = block.body.txEffects.map((txEffect, txIndex)=>[
|
|
156
|
+
numToUInt32BE(txIndex),
|
|
157
|
+
numToUInt32BE(txEffect.contractClassLogs.length),
|
|
158
|
+
txEffect.contractClassLogs.map((log)=>log.toBuffer())
|
|
159
|
+
].flat()).flat();
|
|
160
|
+
await this.#contractClassLogsByBlock.set(block.number, this.#packWithBlockHash(blockHash, contractClassLogsInBlock));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Append new logs to the store's list.
|
|
165
|
+
* @param blocks - The blocks for which to add the logs.
|
|
166
|
+
* @returns True if the operation is successful.
|
|
167
|
+
*/ addLogs(blocks) {
|
|
168
|
+
return this.db.transactionAsync(async ()=>{
|
|
169
|
+
await Promise.all([
|
|
170
|
+
this.#addPrivateLogs(blocks),
|
|
171
|
+
this.#addPublicLogs(blocks),
|
|
172
|
+
this.#addContractClassLogs(blocks)
|
|
173
|
+
]);
|
|
174
|
+
return true;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
#packWithBlockHash(blockHash, data) {
|
|
178
|
+
return Buffer.concat([
|
|
179
|
+
blockHash.toBuffer(),
|
|
180
|
+
...data
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
#unpackBlockHash(reader) {
|
|
184
|
+
const blockHash = reader.remainingBytes() > 0 ? reader.readObject(Fr) : undefined;
|
|
185
|
+
if (!blockHash) {
|
|
186
|
+
throw new Error('Failed to read block hash from log entry buffer');
|
|
187
|
+
}
|
|
188
|
+
return new BlockHash(blockHash);
|
|
189
|
+
}
|
|
190
|
+
deleteLogs(blocks) {
|
|
191
|
+
return this.db.transactionAsync(async ()=>{
|
|
192
|
+
await Promise.all(blocks.map(async (block)=>{
|
|
193
|
+
// Delete private logs
|
|
194
|
+
const privateKeys = await this.#privateLogKeysByBlock.getAsync(block.number) ?? [];
|
|
195
|
+
await Promise.all(privateKeys.map((tag)=>this.#privateLogsByTag.delete(tag)));
|
|
196
|
+
// Delete public logs
|
|
197
|
+
const publicKeys = await this.#publicLogKeysByBlock.getAsync(block.number) ?? [];
|
|
198
|
+
await Promise.all(publicKeys.map((key)=>this.#publicLogsByContractAndTag.delete(key)));
|
|
199
|
+
}));
|
|
200
|
+
await Promise.all(blocks.map((block)=>Promise.all([
|
|
201
|
+
this.#publicLogsByBlock.delete(block.number),
|
|
202
|
+
this.#privateLogKeysByBlock.delete(block.number),
|
|
203
|
+
this.#publicLogKeysByBlock.delete(block.number),
|
|
204
|
+
this.#contractClassLogsByBlock.delete(block.number)
|
|
205
|
+
])));
|
|
206
|
+
return true;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Gets private logs that match any of the `tags`. For each tag, an array of matching logs is returned. An empty
|
|
211
|
+
* array implies no logs match that tag.
|
|
212
|
+
* @param tags - The tags to search for.
|
|
213
|
+
* @param page - The page number (0-indexed) for pagination.
|
|
214
|
+
* @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
|
|
215
|
+
* MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
|
|
216
|
+
*/ async getPrivateLogsByTags(tags, page = 0) {
|
|
217
|
+
const logs = await Promise.all(tags.map((tag)=>this.#privateLogsByTag.getAsync(tag.toString())));
|
|
218
|
+
const start = page * MAX_LOGS_PER_TAG;
|
|
219
|
+
const end = start + MAX_LOGS_PER_TAG;
|
|
220
|
+
return logs.map((logBuffers)=>logBuffers?.slice(start, end).map((logBuffer)=>TxScopedL2Log.fromBuffer(logBuffer)) ?? []);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Gets public logs that match any of the `tags` from the specified contract. For each tag, an array of matching
|
|
224
|
+
* logs is returned. An empty array implies no logs match that tag.
|
|
225
|
+
* @param contractAddress - The contract address to search logs for.
|
|
226
|
+
* @param tags - The tags to search for.
|
|
227
|
+
* @param page - The page number (0-indexed) for pagination.
|
|
228
|
+
* @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
|
|
229
|
+
* MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
|
|
230
|
+
*/ async getPublicLogsByTagsFromContract(contractAddress, tags, page = 0) {
|
|
231
|
+
const logs = await Promise.all(tags.map((tag)=>{
|
|
232
|
+
const key = `${contractAddress.toString()}_${tag.value.toString()}`;
|
|
233
|
+
return this.#publicLogsByContractAndTag.getAsync(key);
|
|
234
|
+
}));
|
|
235
|
+
const start = page * MAX_LOGS_PER_TAG;
|
|
236
|
+
const end = start + MAX_LOGS_PER_TAG;
|
|
237
|
+
return logs.map((logBuffers)=>logBuffers?.slice(start, end).map((logBuffer)=>TxScopedL2Log.fromBuffer(logBuffer)) ?? []);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Gets public logs based on the provided filter.
|
|
241
|
+
* @param filter - The filter to apply to the logs.
|
|
242
|
+
* @returns The requested logs.
|
|
243
|
+
*/ getPublicLogs(filter) {
|
|
244
|
+
if (filter.afterLog) {
|
|
245
|
+
return this.#filterPublicLogsBetweenBlocks(filter);
|
|
246
|
+
} else if (filter.txHash) {
|
|
247
|
+
return this.#filterPublicLogsOfTx(filter);
|
|
248
|
+
} else {
|
|
249
|
+
return this.#filterPublicLogsBetweenBlocks(filter);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async #filterPublicLogsOfTx(filter) {
|
|
253
|
+
if (!filter.txHash) {
|
|
254
|
+
throw new Error('Missing txHash');
|
|
255
|
+
}
|
|
256
|
+
const [blockNumber, txIndex] = await this.blockStore.getTxLocation(filter.txHash) ?? [];
|
|
257
|
+
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
|
|
258
|
+
return {
|
|
259
|
+
logs: [],
|
|
260
|
+
maxLogsHit: false
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const buffer = await this.#publicLogsByBlock.getAsync(blockNumber) ?? Buffer.alloc(0);
|
|
264
|
+
const publicLogsInBlock = [
|
|
265
|
+
[]
|
|
266
|
+
];
|
|
267
|
+
const reader = new BufferReader(buffer);
|
|
268
|
+
const blockHash = this.#unpackBlockHash(reader);
|
|
269
|
+
while(reader.remainingBytes() > 0){
|
|
270
|
+
const indexOfTx = reader.readNumber();
|
|
271
|
+
const numLogsInTx = reader.readNumber();
|
|
272
|
+
publicLogsInBlock[indexOfTx] = [];
|
|
273
|
+
for(let i = 0; i < numLogsInTx; i++){
|
|
274
|
+
publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const txLogs = publicLogsInBlock[txIndex];
|
|
278
|
+
const logs = [];
|
|
279
|
+
const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
|
|
280
|
+
return {
|
|
281
|
+
logs,
|
|
282
|
+
maxLogsHit
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
async #filterPublicLogsBetweenBlocks(filter) {
|
|
286
|
+
const start = filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
|
|
287
|
+
const end = filter.toBlock;
|
|
288
|
+
if (typeof end === 'number' && end < start) {
|
|
289
|
+
return {
|
|
290
|
+
logs: [],
|
|
291
|
+
maxLogsHit: true
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
const logs = [];
|
|
295
|
+
let maxLogsHit = false;
|
|
296
|
+
loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#publicLogsByBlock.entriesAsync({
|
|
297
|
+
start,
|
|
298
|
+
end
|
|
299
|
+
})){
|
|
300
|
+
const publicLogsInBlock = [
|
|
301
|
+
[]
|
|
302
|
+
];
|
|
303
|
+
const reader = new BufferReader(logBuffer);
|
|
304
|
+
const blockHash = this.#unpackBlockHash(reader);
|
|
305
|
+
while(reader.remainingBytes() > 0){
|
|
306
|
+
const indexOfTx = reader.readNumber();
|
|
307
|
+
const numLogsInTx = reader.readNumber();
|
|
308
|
+
publicLogsInBlock[indexOfTx] = [];
|
|
309
|
+
for(let i = 0; i < numLogsInTx; i++){
|
|
310
|
+
publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
for(let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < publicLogsInBlock.length; txIndex++){
|
|
314
|
+
const txLogs = publicLogsInBlock[txIndex];
|
|
315
|
+
maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
|
|
316
|
+
if (maxLogsHit) {
|
|
317
|
+
this.#log.debug(`Max logs hit at block ${blockNumber}`);
|
|
318
|
+
break loopOverBlocks;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
logs,
|
|
324
|
+
maxLogsHit
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Gets contract class logs based on the provided filter.
|
|
329
|
+
* @param filter - The filter to apply to the logs.
|
|
330
|
+
* @returns The requested logs.
|
|
331
|
+
*/ getContractClassLogs(filter) {
|
|
332
|
+
if (filter.afterLog) {
|
|
333
|
+
return this.#filterContractClassLogsBetweenBlocks(filter);
|
|
334
|
+
} else if (filter.txHash) {
|
|
335
|
+
return this.#filterContractClassLogsOfTx(filter);
|
|
336
|
+
} else {
|
|
337
|
+
return this.#filterContractClassLogsBetweenBlocks(filter);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
async #filterContractClassLogsOfTx(filter) {
|
|
341
|
+
if (!filter.txHash) {
|
|
342
|
+
throw new Error('Missing txHash');
|
|
343
|
+
}
|
|
344
|
+
const [blockNumber, txIndex] = await this.blockStore.getTxLocation(filter.txHash) ?? [];
|
|
345
|
+
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
|
|
346
|
+
return {
|
|
347
|
+
logs: [],
|
|
348
|
+
maxLogsHit: false
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
const contractClassLogsBuffer = await this.#contractClassLogsByBlock.getAsync(blockNumber) ?? Buffer.alloc(0);
|
|
352
|
+
const contractClassLogsInBlock = [
|
|
353
|
+
[]
|
|
354
|
+
];
|
|
355
|
+
const reader = new BufferReader(contractClassLogsBuffer);
|
|
356
|
+
const blockHash = this.#unpackBlockHash(reader);
|
|
357
|
+
while(reader.remainingBytes() > 0){
|
|
358
|
+
const indexOfTx = reader.readNumber();
|
|
359
|
+
const numLogsInTx = reader.readNumber();
|
|
360
|
+
contractClassLogsInBlock[indexOfTx] = [];
|
|
361
|
+
for(let i = 0; i < numLogsInTx; i++){
|
|
362
|
+
contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const txLogs = contractClassLogsInBlock[txIndex];
|
|
366
|
+
const logs = [];
|
|
367
|
+
const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
|
|
368
|
+
return {
|
|
369
|
+
logs,
|
|
370
|
+
maxLogsHit
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
async #filterContractClassLogsBetweenBlocks(filter) {
|
|
374
|
+
const start = filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
|
|
375
|
+
const end = filter.toBlock;
|
|
376
|
+
if (typeof end === 'number' && end < start) {
|
|
377
|
+
return {
|
|
378
|
+
logs: [],
|
|
379
|
+
maxLogsHit: true
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const logs = [];
|
|
383
|
+
let maxLogsHit = false;
|
|
384
|
+
loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#contractClassLogsByBlock.entriesAsync({
|
|
385
|
+
start,
|
|
386
|
+
end
|
|
387
|
+
})){
|
|
388
|
+
const contractClassLogsInBlock = [
|
|
389
|
+
[]
|
|
390
|
+
];
|
|
391
|
+
const reader = new BufferReader(logBuffer);
|
|
392
|
+
const blockHash = this.#unpackBlockHash(reader);
|
|
393
|
+
while(reader.remainingBytes() > 0){
|
|
394
|
+
const indexOfTx = reader.readNumber();
|
|
395
|
+
const numLogsInTx = reader.readNumber();
|
|
396
|
+
contractClassLogsInBlock[indexOfTx] = [];
|
|
397
|
+
for(let i = 0; i < numLogsInTx; i++){
|
|
398
|
+
contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
for(let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < contractClassLogsInBlock.length; txIndex++){
|
|
402
|
+
const txLogs = contractClassLogsInBlock[txIndex];
|
|
403
|
+
maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
|
|
404
|
+
if (maxLogsHit) {
|
|
405
|
+
this.#log.debug(`Max logs hit at block ${blockNumber}`);
|
|
406
|
+
break loopOverBlocks;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
logs,
|
|
412
|
+
maxLogsHit
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
#accumulateLogs(results, blockNumber, blockHash, txIndex, txLogs, filter = {}) {
|
|
416
|
+
let maxLogsHit = false;
|
|
417
|
+
let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
|
|
418
|
+
for(; logIndex < txLogs.length; logIndex++){
|
|
419
|
+
const log = txLogs[logIndex];
|
|
420
|
+
if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
|
|
421
|
+
if (log instanceof ContractClassLog) {
|
|
422
|
+
results.push(new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txIndex, logIndex), log));
|
|
423
|
+
} else if (log instanceof PublicLog) {
|
|
424
|
+
results.push(new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txIndex, logIndex), log));
|
|
425
|
+
} else {
|
|
426
|
+
throw new Error('Unknown log type');
|
|
427
|
+
}
|
|
428
|
+
if (results.length >= this.#logsMaxPageSize) {
|
|
429
|
+
maxLogsHit = true;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return maxLogsHit;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { L1BlockId } from '@aztec/ethereum/l1-types';
|
|
2
|
+
import { CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
+
import { type AztecAsyncKVStore, type CustomRange } from '@aztec/kv-store';
|
|
5
|
+
import { type InboxMessage } from '../structs/inbox_message.js';
|
|
6
|
+
export declare class MessageStoreError extends Error {
|
|
7
|
+
readonly inboxMessage: InboxMessage;
|
|
8
|
+
constructor(message: string, inboxMessage: InboxMessage);
|
|
9
|
+
}
|
|
10
|
+
export declare class MessageStore {
|
|
11
|
+
#private;
|
|
12
|
+
private db;
|
|
13
|
+
constructor(db: AztecAsyncKVStore);
|
|
14
|
+
getTotalL1ToL2MessageCount(): Promise<bigint>;
|
|
15
|
+
/** Gets the last L1 block synced. */
|
|
16
|
+
getSynchedL1Block(): Promise<L1BlockId | undefined>;
|
|
17
|
+
/** Sets the last L1 block synced */
|
|
18
|
+
setSynchedL1Block(l1Block: L1BlockId): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Append L1 to L2 messages to the store.
|
|
21
|
+
* Requires new messages to be in order and strictly after the last message added.
|
|
22
|
+
* Throws if out of order messages are added or if the rolling hash is invalid.
|
|
23
|
+
*/
|
|
24
|
+
addL1ToL2Messages(messages: InboxMessage[]): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Gets the L1 to L2 message index in the L1 to L2 message tree.
|
|
27
|
+
* @param l1ToL2Message - The L1 to L2 message.
|
|
28
|
+
* @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
|
|
29
|
+
*/
|
|
30
|
+
getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined>;
|
|
31
|
+
getLastMessage(): Promise<InboxMessage | undefined>;
|
|
32
|
+
getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]>;
|
|
33
|
+
iterateL1ToL2Messages(range?: CustomRange<bigint>): AsyncIterableIterator<InboxMessage>;
|
|
34
|
+
removeL1ToL2Messages(startIndex: bigint): Promise<void>;
|
|
35
|
+
rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void>;
|
|
36
|
+
private indexToKey;
|
|
37
|
+
private leafToIndexKey;
|
|
38
|
+
private increaseTotalMessageCount;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVzc2FnZV9zdG9yZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3N0b3JlL21lc3NhZ2Vfc3RvcmUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDMUQsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFFbkUsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLGdDQUFnQyxDQUFDO0FBSXBELE9BQU8sRUFDTCxLQUFLLGlCQUFpQixFQUd0QixLQUFLLFdBQVcsRUFFakIsTUFBTSxpQkFBaUIsQ0FBQztBQUd6QixPQUFPLEVBQ0wsS0FBSyxZQUFZLEVBSWxCLE1BQU0sNkJBQTZCLENBQUM7QUFFckMscUJBQWEsaUJBQWtCLFNBQVEsS0FBSzthQUd4QixZQUFZLEVBQUUsWUFBWTtJQUY1QyxZQUNFLE9BQU8sRUFBRSxNQUFNLEVBQ0MsWUFBWSxFQUFFLFlBQVksRUFJM0M7Q0FDRjtBQUVELHFCQUFhLFlBQVk7O0lBWVgsT0FBTyxDQUFDLEVBQUU7SUFBdEIsWUFBb0IsRUFBRSxFQUFFLGlCQUFpQixFQUt4QztJQUVZLDBCQUEwQixJQUFJLE9BQU8sQ0FBQyxNQUFNLENBQUMsQ0FFekQ7SUFFRCxxQ0FBcUM7SUFDeEIsaUJBQWlCLElBQUksT0FBTyxDQUFDLFNBQVMsR0FBRyxTQUFTLENBQUMsQ0FRL0Q7SUFFRCxvQ0FBb0M7SUFDdkIsaUJBQWlCLENBQUMsT0FBTyxFQUFFLFNBQVMsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBR2hFO0lBRUQ7Ozs7T0FJRztJQUNJLGlCQUFpQixDQUFDLFFBQVEsRUFBRSxZQUFZLEVBQUUsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBNkZoRTtJQUVEOzs7O09BSUc7SUFDSSxxQkFBcUIsQ0FBQyxhQUFhLEVBQUUsRUFBRSxHQUFHLE9BQU8sQ0FBQyxNQUFNLEdBQUcsU0FBUyxDQUFDLENBRTNFO0lBRVksY0FBYyxJQUFJLE9BQU8sQ0FBQyxZQUFZLEdBQUcsU0FBUyxDQUFDLENBRy9EO0lBRVksaUJBQWlCLENBQUMsZ0JBQWdCLEVBQUUsZ0JBQWdCLEdBQUcsT0FBTyxDQUFDLEVBQUUsRUFBRSxDQUFDLENBdUJoRjtJQUVhLHFCQUFxQixDQUFDLEtBQUssR0FBRSxXQUFXLENBQUMsTUFBTSxDQUFNLEdBQUcscUJBQXFCLENBQUMsWUFBWSxDQUFDLENBS3hHO0lBRU0sb0JBQW9CLENBQUMsVUFBVSxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBZ0I3RDtJQUVNLGtDQUFrQyxDQUFDLHNCQUFzQixFQUFFLGdCQUFnQixHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FJakc7SUFFRCxPQUFPLENBQUMsVUFBVTtJQUlsQixPQUFPLENBQUMsY0FBYztZQUlSLHlCQUF5QjtDQVN4QyJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message_store.d.ts","sourceRoot":"","sources":["../../src/store/message_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAEnE,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AAIpD,OAAO,EACL,KAAK,iBAAiB,EAGtB,KAAK,WAAW,EAEjB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,KAAK,YAAY,EAIlB,MAAM,6BAA6B,CAAC;AAErC,qBAAa,iBAAkB,SAAQ,KAAK;aAGxB,YAAY,EAAE,YAAY;IAF5C,YACE,OAAO,EAAE,MAAM,EACC,YAAY,EAAE,YAAY,EAI3C;CACF;AAED,qBAAa,YAAY;;IAYX,OAAO,CAAC,EAAE;IAAtB,YAAoB,EAAE,EAAE,iBAAiB,EAKxC;IAEY,0BAA0B,IAAI,OAAO,CAAC,MAAM,CAAC,CAEzD;IAED,qCAAqC;IACxB,iBAAiB,IAAI,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC,CAQ/D;IAED,oCAAoC;IACvB,iBAAiB,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAGhE;IAED;;;;OAIG;IACI,iBAAiB,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA6FhE;IAED;;;;OAIG;IACI,qBAAqB,CAAC,aAAa,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAE3E;IAEY,cAAc,IAAI,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC,CAG/D;IAEY,iBAAiB,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,EAAE,EAAE,CAAC,CAuBhF;IAEa,qBAAqB,CAAC,KAAK,GAAE,WAAW,CAAC,MAAM,CAAM,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAKxG;IAEM,oBAAoB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAgB7D;IAEM,kCAAkC,CAAC,sBAAsB,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAIjG;IAED,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,cAAc;YAIR,yBAAyB;CASxC"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
3
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
4
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
6
|
+
import { mapRange } from '@aztec/kv-store';
|
|
7
|
+
import { InboxLeaf } from '@aztec/stdlib/messaging';
|
|
8
|
+
import { deserializeInboxMessage, serializeInboxMessage, updateRollingHash } from '../structs/inbox_message.js';
|
|
9
|
+
export class MessageStoreError extends Error {
|
|
10
|
+
inboxMessage;
|
|
11
|
+
constructor(message, inboxMessage){
|
|
12
|
+
super(message), this.inboxMessage = inboxMessage;
|
|
13
|
+
this.name = 'MessageStoreError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export class MessageStore {
|
|
17
|
+
db;
|
|
18
|
+
/** Maps from message index to serialized InboxMessage */ #l1ToL2Messages;
|
|
19
|
+
/** Maps from hex-stringified message leaf to its index */ #l1ToL2MessageIndices;
|
|
20
|
+
/** Stores L1 block number and hash of the L1 synchpoint */ #lastSynchedL1Block;
|
|
21
|
+
/** Stores total messages stored */ #totalMessageCount;
|
|
22
|
+
#log;
|
|
23
|
+
constructor(db){
|
|
24
|
+
this.db = db;
|
|
25
|
+
this.#log = createLogger('archiver:message_store');
|
|
26
|
+
this.#l1ToL2Messages = db.openMap('archiver_l1_to_l2_messages');
|
|
27
|
+
this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
|
|
28
|
+
this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
|
|
29
|
+
this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
|
|
30
|
+
}
|
|
31
|
+
async getTotalL1ToL2MessageCount() {
|
|
32
|
+
return await this.#totalMessageCount.getAsync() ?? 0n;
|
|
33
|
+
}
|
|
34
|
+
/** Gets the last L1 block synced. */ async getSynchedL1Block() {
|
|
35
|
+
const buffer = await this.#lastSynchedL1Block.getAsync();
|
|
36
|
+
if (!buffer) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const reader = BufferReader.asReader(buffer);
|
|
40
|
+
return {
|
|
41
|
+
l1BlockNumber: reader.readUInt256(),
|
|
42
|
+
l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE))
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Sets the last L1 block synced */ async setSynchedL1Block(l1Block) {
|
|
46
|
+
const buffer = serializeToBuffer([
|
|
47
|
+
l1Block.l1BlockNumber,
|
|
48
|
+
l1Block.l1BlockHash
|
|
49
|
+
]);
|
|
50
|
+
await this.#lastSynchedL1Block.set(buffer);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Append L1 to L2 messages to the store.
|
|
54
|
+
* Requires new messages to be in order and strictly after the last message added.
|
|
55
|
+
* Throws if out of order messages are added or if the rolling hash is invalid.
|
|
56
|
+
*/ addL1ToL2Messages(messages) {
|
|
57
|
+
if (messages.length === 0) {
|
|
58
|
+
return Promise.resolve();
|
|
59
|
+
}
|
|
60
|
+
return this.db.transactionAsync(async ()=>{
|
|
61
|
+
let lastMessage = await this.getLastMessage();
|
|
62
|
+
let messageCount = 0;
|
|
63
|
+
for (const message of messages){
|
|
64
|
+
// Check messages are inserted in increasing order, but allow reinserting messages.
|
|
65
|
+
if (lastMessage && message.index <= lastMessage.index) {
|
|
66
|
+
const existing = await this.#l1ToL2Messages.getAsync(this.indexToKey(message.index));
|
|
67
|
+
if (existing && deserializeInboxMessage(existing).rollingHash.equals(message.rollingHash)) {
|
|
68
|
+
// We reinsert instead of skipping in case the message was re-orged and got added in a different L1 block.
|
|
69
|
+
this.#log.trace(`Reinserting message with index ${message.index} in the store`);
|
|
70
|
+
await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
throw new MessageStoreError(`Cannot insert L1 to L2 message with index ${message.index} before last message with index ${lastMessage.index}`, message);
|
|
74
|
+
}
|
|
75
|
+
// Check rolling hash is valid.
|
|
76
|
+
const previousRollingHash = lastMessage?.rollingHash ?? Buffer16.ZERO;
|
|
77
|
+
const expectedRollingHash = updateRollingHash(previousRollingHash, message.leaf);
|
|
78
|
+
if (!expectedRollingHash.equals(message.rollingHash)) {
|
|
79
|
+
throw new MessageStoreError(`Invalid rolling hash for incoming L1 to L2 message ${message.leaf.toString()} ` + `with index ${message.index} ` + `(expected ${expectedRollingHash.toString()} from previous hash ${previousRollingHash} but got ${message.rollingHash.toString()})`, message);
|
|
80
|
+
}
|
|
81
|
+
// Check index corresponds to the checkpoint number.
|
|
82
|
+
const [expectedStart, expectedEnd] = InboxLeaf.indexRangeForCheckpoint(message.checkpointNumber);
|
|
83
|
+
if (message.index < expectedStart || message.index >= expectedEnd) {
|
|
84
|
+
throw new MessageStoreError(`Invalid index ${message.index} for incoming L1 to L2 message ${message.leaf.toString()} ` + `at checkpoint ${message.checkpointNumber} (expected value in range [${expectedStart}, ${expectedEnd}))`, message);
|
|
85
|
+
}
|
|
86
|
+
// Check there are no gaps in the indices within the same checkpoint.
|
|
87
|
+
if (lastMessage && message.checkpointNumber === lastMessage.checkpointNumber && message.index !== lastMessage.index + 1n) {
|
|
88
|
+
throw new MessageStoreError(`Missing prior message for incoming L1 to L2 message ${message.leaf.toString()} ` + `with index ${message.index}`, message);
|
|
89
|
+
}
|
|
90
|
+
// Check the first message in a block has the correct index.
|
|
91
|
+
if ((!lastMessage || message.checkpointNumber > lastMessage.checkpointNumber) && message.index !== expectedStart) {
|
|
92
|
+
throw new MessageStoreError(`Message ${message.leaf.toString()} for checkpoint ${message.checkpointNumber} has wrong index ` + `${message.index} (expected ${expectedStart})`, message);
|
|
93
|
+
}
|
|
94
|
+
// Perform the insertions.
|
|
95
|
+
await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
|
|
96
|
+
await this.#l1ToL2MessageIndices.set(this.leafToIndexKey(message.leaf), message.index);
|
|
97
|
+
messageCount++;
|
|
98
|
+
this.#log.trace(`Inserted L1 to L2 message ${message.leaf} with index ${message.index} into the store`);
|
|
99
|
+
lastMessage = message;
|
|
100
|
+
}
|
|
101
|
+
// Update the L1 sync point to that of the last message added.
|
|
102
|
+
const currentSyncPoint = await this.getSynchedL1Block();
|
|
103
|
+
if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage.l1BlockNumber) {
|
|
104
|
+
await this.setSynchedL1Block({
|
|
105
|
+
l1BlockNumber: lastMessage.l1BlockNumber,
|
|
106
|
+
l1BlockHash: lastMessage.l1BlockHash
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
// Update total message count with the number of inserted messages.
|
|
110
|
+
await this.increaseTotalMessageCount(messageCount);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Gets the L1 to L2 message index in the L1 to L2 message tree.
|
|
115
|
+
* @param l1ToL2Message - The L1 to L2 message.
|
|
116
|
+
* @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
|
|
117
|
+
*/ getL1ToL2MessageIndex(l1ToL2Message) {
|
|
118
|
+
return this.#l1ToL2MessageIndices.getAsync(this.leafToIndexKey(l1ToL2Message));
|
|
119
|
+
}
|
|
120
|
+
async getLastMessage() {
|
|
121
|
+
const [msg] = await toArray(this.#l1ToL2Messages.valuesAsync({
|
|
122
|
+
reverse: true,
|
|
123
|
+
limit: 1
|
|
124
|
+
}));
|
|
125
|
+
return msg ? deserializeInboxMessage(msg) : undefined;
|
|
126
|
+
}
|
|
127
|
+
async getL1ToL2Messages(checkpointNumber) {
|
|
128
|
+
const messages = [];
|
|
129
|
+
const [startIndex, endIndex] = InboxLeaf.indexRangeForCheckpoint(checkpointNumber);
|
|
130
|
+
let lastIndex = startIndex - 1n;
|
|
131
|
+
for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync({
|
|
132
|
+
start: this.indexToKey(startIndex),
|
|
133
|
+
end: this.indexToKey(endIndex)
|
|
134
|
+
})){
|
|
135
|
+
const msg = deserializeInboxMessage(msgBuffer);
|
|
136
|
+
if (msg.checkpointNumber !== checkpointNumber) {
|
|
137
|
+
throw new Error(`L1 to L2 message with index ${msg.index} has invalid checkpoint number ${msg.checkpointNumber}`);
|
|
138
|
+
} else if (msg.index !== lastIndex + 1n) {
|
|
139
|
+
throw new Error(`Expected L1 to L2 message with index ${lastIndex + 1n} but got ${msg.index}`);
|
|
140
|
+
}
|
|
141
|
+
lastIndex = msg.index;
|
|
142
|
+
messages.push(msg.leaf);
|
|
143
|
+
}
|
|
144
|
+
return messages;
|
|
145
|
+
}
|
|
146
|
+
async *iterateL1ToL2Messages(range = {}) {
|
|
147
|
+
const entriesRange = mapRange(range, this.indexToKey);
|
|
148
|
+
for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync(entriesRange)){
|
|
149
|
+
yield deserializeInboxMessage(msgBuffer);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
removeL1ToL2Messages(startIndex) {
|
|
153
|
+
this.#log.debug(`Deleting L1 to L2 messages from index ${startIndex}`);
|
|
154
|
+
let deleteCount = 0;
|
|
155
|
+
return this.db.transactionAsync(async ()=>{
|
|
156
|
+
for await (const [key, msgBuffer] of this.#l1ToL2Messages.entriesAsync({
|
|
157
|
+
start: this.indexToKey(startIndex)
|
|
158
|
+
})){
|
|
159
|
+
this.#log.trace(`Deleting L1 to L2 message with index ${key - 1} from the store`);
|
|
160
|
+
await this.#l1ToL2Messages.delete(key);
|
|
161
|
+
await this.#l1ToL2MessageIndices.delete(this.leafToIndexKey(deserializeInboxMessage(msgBuffer).leaf));
|
|
162
|
+
deleteCount++;
|
|
163
|
+
}
|
|
164
|
+
await this.increaseTotalMessageCount(-deleteCount);
|
|
165
|
+
this.#log.warn(`Deleted ${deleteCount} L1 to L2 messages from index ${startIndex} from the store`);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
|
|
169
|
+
this.#log.debug(`Deleting L1 to L2 messages up to target checkpoint ${targetCheckpointNumber}`);
|
|
170
|
+
const startIndex = InboxLeaf.smallestIndexForCheckpoint(CheckpointNumber(targetCheckpointNumber + 1));
|
|
171
|
+
return this.removeL1ToL2Messages(startIndex);
|
|
172
|
+
}
|
|
173
|
+
indexToKey(index) {
|
|
174
|
+
return Number(index);
|
|
175
|
+
}
|
|
176
|
+
leafToIndexKey(leaf) {
|
|
177
|
+
return leaf.toString();
|
|
178
|
+
}
|
|
179
|
+
async increaseTotalMessageCount(count) {
|
|
180
|
+
if (count === 0) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
return await this.db.transactionAsync(async ()=>{
|
|
184
|
+
const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
|
|
185
|
+
await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(count));
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|