@aztec/archiver 0.0.1-commit.b2a5d0dd1 → 0.0.1-commit.b3d3157a
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 +7 -5
- package/dest/archiver.d.ts +49 -14
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +216 -68
- package/dest/config.d.ts +4 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +11 -16
- package/dest/errors.d.ts +19 -13
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +32 -16
- package/dest/factory.d.ts +12 -6
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +31 -21
- package/dest/index.d.ts +9 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +8 -1
- package/dest/l1/calldata_retriever.d.ts +2 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +13 -4
- package/dest/l1/trace_tx.d.ts +12 -66
- package/dest/l1/trace_tx.d.ts.map +1 -1
- package/dest/modules/contract_data_source_adapter.d.ts +25 -0
- package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
- package/dest/modules/contract_data_source_adapter.js +40 -0
- package/dest/modules/data_source_base.d.ts +71 -49
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +269 -143
- package/dest/modules/data_store_updater.d.ts +18 -6
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +108 -59
- package/dest/modules/instrumentation.js +3 -1
- package/dest/modules/l1_synchronizer.d.ts +6 -5
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +203 -100
- package/dest/modules/outbox_trees_resolver.d.ts +62 -0
- package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
- package/dest/modules/outbox_trees_resolver.js +162 -0
- package/dest/modules/validation.d.ts +4 -3
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +4 -4
- package/dest/store/block_store.d.ts +155 -98
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +473 -265
- package/dest/store/contract_class_store.d.ts +17 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +17 -1
- package/dest/store/contract_instance_store.d.ts +28 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +31 -0
- package/dest/store/data_stores.d.ts +68 -0
- package/dest/store/data_stores.d.ts.map +1 -0
- package/dest/store/data_stores.js +54 -0
- package/dest/store/function_names_cache.d.ts +17 -0
- package/dest/store/function_names_cache.d.ts.map +1 -0
- package/dest/store/function_names_cache.js +30 -0
- package/dest/store/l2_tips_cache.d.ts +13 -8
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +13 -96
- package/dest/store/log_store.d.ts +42 -40
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +257 -478
- package/dest/store/log_store_codec.d.ts +70 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +101 -0
- package/dest/store/message_store.d.ts +9 -3
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +31 -1
- package/dest/test/fake_l1_state.d.ts +2 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +25 -8
- package/dest/test/mock_l2_block_source.d.ts +47 -51
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +223 -185
- package/dest/test/noop_l1_archiver.d.ts +10 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +19 -8
- package/package.json +14 -14
- package/src/archiver.ts +265 -72
- package/src/config.ts +12 -19
- package/src/errors.ts +46 -23
- package/src/factory.ts +38 -20
- package/src/index.ts +16 -1
- package/src/l1/calldata_retriever.ts +14 -4
- package/src/modules/contract_data_source_adapter.ts +55 -0
- package/src/modules/data_source_base.ts +335 -187
- package/src/modules/data_store_updater.ts +118 -61
- package/src/modules/instrumentation.ts +1 -1
- package/src/modules/l1_synchronizer.ts +278 -120
- package/src/modules/outbox_trees_resolver.ts +199 -0
- package/src/modules/validation.ts +8 -7
- package/src/store/block_store.ts +566 -323
- package/src/store/contract_class_store.ts +28 -2
- package/src/store/contract_instance_store.ts +43 -0
- package/src/store/data_stores.ts +104 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +16 -115
- package/src/store/log_store.ts +296 -650
- package/src/store/log_store_codec.ts +132 -0
- package/src/store/message_store.ts +35 -2
- package/src/test/fake_l1_state.ts +24 -14
- package/src/test/mock_l2_block_source.ts +271 -239
- package/src/test/noop_l1_archiver.ts +39 -8
- package/dest/store/kv_archiver_store.d.ts +0 -383
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -501
- package/src/store/kv_archiver_store.ts +0 -728
package/src/store/log_store.ts
CHANGED
|
@@ -1,733 +1,379 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
-
import {
|
|
3
|
-
import { compactArray, filterAsync } from '@aztec/foundation/collection';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
-
import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
|
|
6
4
|
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
7
|
-
import type {
|
|
8
|
-
import { BlockHash, L2Block } from '@aztec/stdlib/block';
|
|
5
|
+
import type { BlockHash, L2Block } from '@aztec/stdlib/block';
|
|
9
6
|
import { MAX_LOGS_PER_TAG } from '@aztec/stdlib/interfaces/api-limit';
|
|
10
|
-
import type {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
LogId,
|
|
17
|
-
PublicLog,
|
|
18
|
-
type SiloedTag,
|
|
7
|
+
import type {
|
|
8
|
+
LogCursor,
|
|
9
|
+
LogResult,
|
|
10
|
+
PrivateLogsQuery,
|
|
11
|
+
PublicLogsQuery,
|
|
12
|
+
SiloedTag,
|
|
19
13
|
Tag,
|
|
20
|
-
|
|
14
|
+
TagQuery,
|
|
21
15
|
} from '@aztec/stdlib/logs';
|
|
22
16
|
import { TxHash } from '@aztec/stdlib/tx';
|
|
23
17
|
|
|
24
|
-
import { OutOfOrderLogInsertionError } from '../errors.js';
|
|
25
18
|
import type { BlockStore } from './block_store.js';
|
|
19
|
+
import {
|
|
20
|
+
decodeKeyTail,
|
|
21
|
+
decodeValue,
|
|
22
|
+
encodeKey,
|
|
23
|
+
encodePublicPrefix,
|
|
24
|
+
encodeValue,
|
|
25
|
+
endOfTagRange,
|
|
26
|
+
endOfTxRange,
|
|
27
|
+
fieldHex,
|
|
28
|
+
incKey,
|
|
29
|
+
} from './log_store_codec.js';
|
|
26
30
|
|
|
27
31
|
/**
|
|
28
|
-
*
|
|
32
|
+
* Indexes every emitted private and public log under a composite hex-string key
|
|
33
|
+
* `[contractAddress (public only)]-tag-blockNumber-txIndexWithinBlock-logIndexWithinTx`,
|
|
34
|
+
* where each numeric segment is zero-padded to 8 lowercase hex digits (4 bytes BE) and
|
|
35
|
+
* `contractAddress` / `tag` are the bare 64-hex-char field representations (no `0x` prefix). The
|
|
36
|
+
* fixed-width zero-padded hex segments sort lexicographically in the same order as the canonical
|
|
37
|
+
* `(contract, tag, blockNumber, txIndexWithinBlock, logIndexWithinTx)` tuple, so a single ordered
|
|
38
|
+
* range scan answers every {@link PrivateLogsQuery} / {@link PublicLogsQuery}.
|
|
39
|
+
*
|
|
40
|
+
* Per-block secondary indices (`#privateKeysByBlock`, `#publicKeysByBlock`) record the exact primary
|
|
41
|
+
* keys written for each block so {@link deleteLogs} can drop them on reorg without having to range
|
|
42
|
+
* scan by block (block isn't the leading key segment).
|
|
43
|
+
*
|
|
44
|
+
* Contract-class logs are no longer stored or served by the log store.
|
|
29
45
|
*/
|
|
30
46
|
export class LogStore {
|
|
31
|
-
|
|
32
|
-
#
|
|
33
|
-
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
|
|
47
|
+
/** Primary map: composite private key (tag + tail = 96 hex chars + separators) -> serialized {@link StoredLogValue}. */
|
|
48
|
+
#privateLogs: AztecAsyncMap<string, Buffer>;
|
|
49
|
+
/** Primary map: composite public key (contract + tag + tail) -> serialized {@link StoredLogValue}. */
|
|
50
|
+
#publicLogs: AztecAsyncMap<string, Buffer>;
|
|
51
|
+
|
|
52
|
+
/** Secondary deletion index: blockNumber -> the exact primary keys written for that block. */
|
|
53
|
+
#privateKeysByBlock: AztecAsyncMap<number, string[]>;
|
|
54
|
+
#publicKeysByBlock: AztecAsyncMap<number, string[]>;
|
|
55
|
+
|
|
40
56
|
#log = createLogger('archiver:log_store');
|
|
41
57
|
|
|
58
|
+
/**
|
|
59
|
+
* @param genesisBlockHash - Hash of the synthetic genesis block. During early sync the PXE anchors to
|
|
60
|
+
* genesis and passes its hash as a query `referenceBlock`; since the archiver never indexes the
|
|
61
|
+
* genesis block, the store recognizes this hash directly and resolves it to the genesis block number
|
|
62
|
+
* rather than mistaking it for a reorg.
|
|
63
|
+
*/
|
|
42
64
|
constructor(
|
|
43
65
|
private db: AztecAsyncKVStore,
|
|
44
66
|
private blockStore: BlockStore,
|
|
45
|
-
|
|
67
|
+
private readonly genesisBlockHash: BlockHash,
|
|
46
68
|
) {
|
|
47
|
-
this.#
|
|
48
|
-
this.#
|
|
49
|
-
this.#
|
|
50
|
-
this.#
|
|
51
|
-
this.#publicLogsByBlock = db.openMap('archiver_public_logs_by_block');
|
|
52
|
-
this.#contractClassLogsByBlock = db.openMap('archiver_contract_class_logs_by_block');
|
|
53
|
-
|
|
54
|
-
this.#logsMaxPageSize = logsMaxPageSize;
|
|
69
|
+
this.#privateLogs = db.openMap('archiver_private_logs');
|
|
70
|
+
this.#publicLogs = db.openMap('archiver_public_logs');
|
|
71
|
+
this.#privateKeysByBlock = db.openMap('archiver_private_log_keys_by_block');
|
|
72
|
+
this.#publicKeysByBlock = db.openMap('archiver_public_log_keys_by_block');
|
|
55
73
|
}
|
|
56
74
|
|
|
57
75
|
/**
|
|
58
|
-
*
|
|
76
|
+
* Indexes every emitted private and public log from the given blocks. Wraps the write in a single
|
|
77
|
+
* `db.transactionAsync` so the primary entries and the per-block secondary indices stay consistent.
|
|
59
78
|
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*/
|
|
63
|
-
#extractTaggedLogsFromBlock(block: L2Block) {
|
|
64
|
-
// SiloedTag (as string) -> array of log buffers.
|
|
65
|
-
const privateTaggedLogs = new Map<string, Buffer[]>();
|
|
66
|
-
// "{contractAddress}_{tag}" (as string) -> array of log buffers.
|
|
67
|
-
const publicTaggedLogs = new Map<string, Buffer[]>();
|
|
68
|
-
|
|
69
|
-
block.body.txEffects.forEach(txEffect => {
|
|
70
|
-
const txHash = txEffect.txHash;
|
|
71
|
-
|
|
72
|
-
txEffect.privateLogs.forEach(log => {
|
|
73
|
-
// Private logs use SiloedTag (already siloed by kernel)
|
|
74
|
-
const tag = log.fields[0];
|
|
75
|
-
this.#log.debug(`Found private log with tag ${tag.toString()} in block ${block.number}`);
|
|
76
|
-
|
|
77
|
-
const currentLogs = privateTaggedLogs.get(tag.toString()) ?? [];
|
|
78
|
-
currentLogs.push(
|
|
79
|
-
new TxScopedL2Log(
|
|
80
|
-
txHash,
|
|
81
|
-
block.number,
|
|
82
|
-
block.timestamp,
|
|
83
|
-
log.getEmittedFields(),
|
|
84
|
-
txEffect.noteHashes,
|
|
85
|
-
txEffect.nullifiers[0],
|
|
86
|
-
).toBuffer(),
|
|
87
|
-
);
|
|
88
|
-
privateTaggedLogs.set(tag.toString(), currentLogs);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
txEffect.publicLogs.forEach(log => {
|
|
92
|
-
// Public logs use Tag directly (not siloed) and are stored with contract address
|
|
93
|
-
const tag = log.fields[0];
|
|
94
|
-
const contractAddress = log.contractAddress;
|
|
95
|
-
const key = `${contractAddress.toString()}_${tag.toString()}`;
|
|
96
|
-
this.#log.debug(
|
|
97
|
-
`Found public log with tag ${tag.toString()} from contract ${contractAddress.toString()} in block ${block.number}`,
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
const currentLogs = publicTaggedLogs.get(key) ?? [];
|
|
101
|
-
currentLogs.push(
|
|
102
|
-
new TxScopedL2Log(
|
|
103
|
-
txHash,
|
|
104
|
-
block.number,
|
|
105
|
-
block.timestamp,
|
|
106
|
-
log.getEmittedFields(),
|
|
107
|
-
txEffect.noteHashes,
|
|
108
|
-
txEffect.nullifiers[0],
|
|
109
|
-
).toBuffer(),
|
|
110
|
-
);
|
|
111
|
-
publicTaggedLogs.set(key, currentLogs);
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
return { privateTaggedLogs, publicTaggedLogs };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Extracts and aggregates tagged logs from a list of blocks.
|
|
120
|
-
* @param blocks - The blocks to extract logs from.
|
|
121
|
-
* @returns A map from tag (as string) to an array of serialized private logs belonging to that tag, and a map from
|
|
122
|
-
* "{contractAddress}_{tag}" (as string) to an array of serialized public logs belonging to that key.
|
|
79
|
+
* A block is only ever added once; on reorg the archiver calls {@link deleteLogs} first, so we write
|
|
80
|
+
* the secondary index entries with a plain `set` (overwrite) rather than read-modify-append.
|
|
123
81
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
taggedLogBuffer.tag,
|
|
178
|
-
lastExisting.blockNumber,
|
|
179
|
-
firstNew.blockNumber,
|
|
180
|
-
);
|
|
82
|
+
addLogs(blocks: L2Block[]): Promise<boolean> {
|
|
83
|
+
return this.db.transactionAsync(async () => {
|
|
84
|
+
for (const block of blocks) {
|
|
85
|
+
const blockHash = await block.hash();
|
|
86
|
+
const blockNumber = block.number;
|
|
87
|
+
const blockTimestamp = block.timestamp;
|
|
88
|
+
|
|
89
|
+
const privateKeys: string[] = [];
|
|
90
|
+
const privateValues: Buffer[] = [];
|
|
91
|
+
const publicKeys: string[] = [];
|
|
92
|
+
const publicValues: Buffer[] = [];
|
|
93
|
+
|
|
94
|
+
for (let txIndexWithinBlock = 0; txIndexWithinBlock < block.body.txEffects.length; txIndexWithinBlock++) {
|
|
95
|
+
const txEffect = block.body.txEffects[txIndexWithinBlock];
|
|
96
|
+
const txHash = txEffect.txHash;
|
|
97
|
+
|
|
98
|
+
// Private and public log indices are counted independently per tx, each starting at 0.
|
|
99
|
+
let privateLogIndexWithinTx = 0;
|
|
100
|
+
let publicLogIndexWithinTx = 0;
|
|
101
|
+
|
|
102
|
+
for (const log of txEffect.privateLogs) {
|
|
103
|
+
const tagHex = fieldHex(log.fields[0]);
|
|
104
|
+
const key = encodeKey(tagHex, blockNumber, txIndexWithinBlock, privateLogIndexWithinTx);
|
|
105
|
+
const value = encodeValue({
|
|
106
|
+
txHash,
|
|
107
|
+
blockHash,
|
|
108
|
+
blockTimestamp,
|
|
109
|
+
logData: log.getEmittedFields(),
|
|
110
|
+
});
|
|
111
|
+
privateKeys.push(key);
|
|
112
|
+
privateValues.push(value);
|
|
113
|
+
privateLogIndexWithinTx++;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const log of txEffect.publicLogs) {
|
|
117
|
+
const contractHex = fieldHex(log.contractAddress);
|
|
118
|
+
const tagHex = fieldHex(log.fields[0]);
|
|
119
|
+
const key = encodeKey(
|
|
120
|
+
encodePublicPrefix(contractHex, tagHex),
|
|
121
|
+
blockNumber,
|
|
122
|
+
txIndexWithinBlock,
|
|
123
|
+
publicLogIndexWithinTx,
|
|
124
|
+
);
|
|
125
|
+
const value = encodeValue({
|
|
126
|
+
txHash,
|
|
127
|
+
blockHash,
|
|
128
|
+
blockTimestamp,
|
|
129
|
+
logData: log.getEmittedFields(),
|
|
130
|
+
});
|
|
131
|
+
publicKeys.push(key);
|
|
132
|
+
publicValues.push(value);
|
|
133
|
+
publicLogIndexWithinTx++;
|
|
134
|
+
}
|
|
181
135
|
}
|
|
182
|
-
privateTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(newLogs));
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
136
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
for (const [tag, logs] of privateTaggedLogs.entries()) {
|
|
189
|
-
await this.#privateLogsByTag.set(tag, logs);
|
|
190
|
-
privateTagsInBlock.push(tag);
|
|
191
|
-
}
|
|
192
|
-
await this.#privateLogKeysByBlock.set(block.number, privateTagsInBlock);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
async #addPublicLogs(blocks: L2Block[]): Promise<void> {
|
|
197
|
-
const newBlocks = await filterAsync(
|
|
198
|
-
blocks,
|
|
199
|
-
async block => !(await this.#publicLogKeysByBlock.hasAsync(block.number)),
|
|
200
|
-
);
|
|
201
|
-
|
|
202
|
-
const { publicTaggedLogs } = this.#extractTaggedLogs(newBlocks);
|
|
203
|
-
const keysOfPublicLogsToUpdate = Array.from(publicTaggedLogs.keys());
|
|
204
|
-
|
|
205
|
-
const currentPublicTaggedLogs = await Promise.all(
|
|
206
|
-
keysOfPublicLogsToUpdate.map(async key => ({
|
|
207
|
-
tag: key,
|
|
208
|
-
logBuffers: await this.#publicLogsByContractAndTag.getAsync(key),
|
|
209
|
-
})),
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
for (const taggedLogBuffer of currentPublicTaggedLogs) {
|
|
213
|
-
if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
|
|
214
|
-
const newLogs = publicTaggedLogs.get(taggedLogBuffer.tag)!;
|
|
215
|
-
if (newLogs.length === 0) {
|
|
216
|
-
continue;
|
|
137
|
+
for (let i = 0; i < privateKeys.length; i++) {
|
|
138
|
+
await this.#privateLogs.set(privateKeys[i], privateValues[i]);
|
|
217
139
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
if (lastExisting.blockNumber > firstNew.blockNumber) {
|
|
221
|
-
throw new OutOfOrderLogInsertionError(
|
|
222
|
-
'public',
|
|
223
|
-
taggedLogBuffer.tag,
|
|
224
|
-
lastExisting.blockNumber,
|
|
225
|
-
firstNew.blockNumber,
|
|
226
|
-
);
|
|
140
|
+
for (let i = 0; i < publicKeys.length; i++) {
|
|
141
|
+
await this.#publicLogs.set(publicKeys[i], publicValues[i]);
|
|
227
142
|
}
|
|
228
|
-
publicTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(newLogs));
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
143
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const publicTagsInBlock: string[] = [];
|
|
235
|
-
for (const [tag, logs] of publicTaggedLogs.entries()) {
|
|
236
|
-
await this.#publicLogsByContractAndTag.set(tag, logs);
|
|
237
|
-
publicTagsInBlock.push(tag);
|
|
238
|
-
}
|
|
239
|
-
await this.#publicLogKeysByBlock.set(block.number, publicTagsInBlock);
|
|
240
|
-
|
|
241
|
-
const publicLogsInBlock = block.body.txEffects
|
|
242
|
-
.map((txEffect, txIndex) =>
|
|
243
|
-
[
|
|
244
|
-
numToUInt32BE(txIndex),
|
|
245
|
-
txEffect.txHash.toBuffer(),
|
|
246
|
-
numToUInt32BE(txEffect.publicLogs.length),
|
|
247
|
-
txEffect.publicLogs.map(log => log.toBuffer()),
|
|
248
|
-
].flat(),
|
|
249
|
-
)
|
|
250
|
-
.flat();
|
|
251
|
-
|
|
252
|
-
await this.#publicLogsByBlock.set(block.number, this.#packWithBlockHash(blockHash, publicLogsInBlock));
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async #addContractClassLogs(blocks: L2Block[]): Promise<void> {
|
|
257
|
-
const newBlocks = await filterAsync(
|
|
258
|
-
blocks,
|
|
259
|
-
async block => !(await this.#contractClassLogsByBlock.hasAsync(block.number)),
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
for (const block of newBlocks) {
|
|
263
|
-
const blockHash = await block.hash();
|
|
264
|
-
|
|
265
|
-
const contractClassLogsInBlock = block.body.txEffects
|
|
266
|
-
.map((txEffect, txIndex) =>
|
|
267
|
-
[
|
|
268
|
-
numToUInt32BE(txIndex),
|
|
269
|
-
txEffect.txHash.toBuffer(),
|
|
270
|
-
numToUInt32BE(txEffect.contractClassLogs.length),
|
|
271
|
-
txEffect.contractClassLogs.map(log => log.toBuffer()),
|
|
272
|
-
].flat(),
|
|
273
|
-
)
|
|
274
|
-
.flat();
|
|
275
|
-
|
|
276
|
-
await this.#contractClassLogsByBlock.set(
|
|
277
|
-
block.number,
|
|
278
|
-
this.#packWithBlockHash(blockHash, contractClassLogsInBlock),
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
144
|
+
await this.#privateKeysByBlock.set(blockNumber, privateKeys);
|
|
145
|
+
await this.#publicKeysByBlock.set(blockNumber, publicKeys);
|
|
282
146
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
return this.db.transactionAsync(async () => {
|
|
290
|
-
await Promise.all([
|
|
291
|
-
this.#addPrivateLogs(blocks),
|
|
292
|
-
this.#addPublicLogs(blocks),
|
|
293
|
-
this.#addContractClassLogs(blocks),
|
|
294
|
-
]);
|
|
147
|
+
this.#log.debug(`Indexed logs for block ${blockNumber}`, {
|
|
148
|
+
blockNumber,
|
|
149
|
+
privateCount: privateKeys.length,
|
|
150
|
+
publicCount: publicKeys.length,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
295
153
|
return true;
|
|
296
154
|
});
|
|
297
155
|
}
|
|
298
156
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
#unpackBlockHash(reader: BufferReader): BlockHash {
|
|
304
|
-
if (reader.remainingBytes() === 0) {
|
|
305
|
-
throw new Error('Failed to read block hash from log entry buffer');
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return BlockHash.fromBuffer(reader);
|
|
309
|
-
}
|
|
310
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Deletes every log indexed under any of the given blocks. Secondary-index driven, so it doesn't
|
|
159
|
+
* have to range-scan the primary maps.
|
|
160
|
+
*/
|
|
311
161
|
deleteLogs(blocks: L2Block[]): Promise<boolean> {
|
|
312
162
|
return this.db.transactionAsync(async () => {
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
continue;
|
|
163
|
+
for (const block of blocks) {
|
|
164
|
+
const blockNumber = block.number;
|
|
165
|
+
|
|
166
|
+
const [privateKeys, publicKeys] = await Promise.all([
|
|
167
|
+
this.#privateKeysByBlock.getAsync(blockNumber),
|
|
168
|
+
this.#publicKeysByBlock.getAsync(blockNumber),
|
|
169
|
+
]);
|
|
170
|
+
|
|
171
|
+
if (privateKeys) {
|
|
172
|
+
for (const key of privateKeys) {
|
|
173
|
+
await this.#privateLogs.delete(key);
|
|
174
|
+
}
|
|
175
|
+
await this.#privateKeysByBlock.delete(blockNumber);
|
|
327
176
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// Collect all unique public keys across all blocks being deleted
|
|
336
|
-
const allPublicKeys = new Set(
|
|
337
|
-
compactArray(await Promise.all(blocks.map(block => this.#publicLogKeysByBlock.getAsync(block.number)))).flat(),
|
|
338
|
-
);
|
|
339
|
-
|
|
340
|
-
// And do the same as we did with private logs
|
|
341
|
-
for (const key of allPublicKeys) {
|
|
342
|
-
const existing = await this.#publicLogsByContractAndTag.getAsync(key);
|
|
343
|
-
if (existing === undefined || existing.length === 0) {
|
|
344
|
-
continue;
|
|
177
|
+
if (publicKeys) {
|
|
178
|
+
for (const key of publicKeys) {
|
|
179
|
+
await this.#publicLogs.delete(key);
|
|
180
|
+
}
|
|
181
|
+
await this.#publicKeysByBlock.delete(blockNumber);
|
|
345
182
|
}
|
|
346
|
-
const lastIndexToKeep = existing.findLastIndex(
|
|
347
|
-
buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
|
|
348
|
-
);
|
|
349
|
-
const remaining = existing.slice(0, lastIndexToKeep + 1);
|
|
350
|
-
await (remaining.length > 0
|
|
351
|
-
? this.#publicLogsByContractAndTag.set(key, remaining)
|
|
352
|
-
: this.#publicLogsByContractAndTag.delete(key));
|
|
353
183
|
}
|
|
354
|
-
|
|
355
|
-
// After trimming the tagged logs, we can delete the block-level keys that track which tags are in which blocks.
|
|
356
|
-
await Promise.all(
|
|
357
|
-
blocks.map(block =>
|
|
358
|
-
Promise.all([
|
|
359
|
-
this.#publicLogsByBlock.delete(block.number),
|
|
360
|
-
this.#privateLogKeysByBlock.delete(block.number),
|
|
361
|
-
this.#publicLogKeysByBlock.delete(block.number),
|
|
362
|
-
this.#contractClassLogsByBlock.delete(block.number),
|
|
363
|
-
]),
|
|
364
|
-
),
|
|
365
|
-
);
|
|
366
|
-
|
|
367
184
|
return true;
|
|
368
185
|
});
|
|
369
186
|
}
|
|
370
187
|
|
|
371
|
-
/**
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
* @param page - The page number (0-indexed) for pagination.
|
|
376
|
-
* @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
|
|
377
|
-
* @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
|
|
378
|
-
* MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
|
|
379
|
-
*/
|
|
380
|
-
async getPrivateLogsByTags(
|
|
381
|
-
tags: SiloedTag[],
|
|
382
|
-
page: number = 0,
|
|
383
|
-
upToBlockNumber?: BlockNumber,
|
|
384
|
-
): Promise<TxScopedL2Log[][]> {
|
|
385
|
-
const logs = await Promise.all(tags.map(tag => this.#privateLogsByTag.getAsync(tag.toString())));
|
|
386
|
-
|
|
387
|
-
const start = page * MAX_LOGS_PER_TAG;
|
|
388
|
-
const end = start + MAX_LOGS_PER_TAG;
|
|
389
|
-
|
|
390
|
-
return logs.map(logBuffers => {
|
|
391
|
-
const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
|
|
392
|
-
if (upToBlockNumber !== undefined) {
|
|
393
|
-
const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
|
|
394
|
-
if (cutoff !== -1) {
|
|
395
|
-
return deserialized.slice(0, cutoff);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
return deserialized;
|
|
399
|
-
});
|
|
188
|
+
/** Returns one inner array per element of `query.tags`, in input order. */
|
|
189
|
+
getPrivateLogsByTags(query: PrivateLogsQuery): Promise<LogResult[][]> {
|
|
190
|
+
LogStore.#validateQuery(query);
|
|
191
|
+
return this.db.transactionAsync(() => this.#runQuery(query, /* contractHex */ undefined));
|
|
400
192
|
}
|
|
401
193
|
|
|
402
|
-
/**
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
* @param tags - The tags to search for.
|
|
407
|
-
* @param page - The page number (0-indexed) for pagination.
|
|
408
|
-
* @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
|
|
409
|
-
* @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
|
|
410
|
-
* MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
|
|
411
|
-
*/
|
|
412
|
-
async getPublicLogsByTagsFromContract(
|
|
413
|
-
contractAddress: AztecAddress,
|
|
414
|
-
tags: Tag[],
|
|
415
|
-
page: number = 0,
|
|
416
|
-
upToBlockNumber?: BlockNumber,
|
|
417
|
-
): Promise<TxScopedL2Log[][]> {
|
|
418
|
-
const logs = await Promise.all(
|
|
419
|
-
tags.map(tag => {
|
|
420
|
-
const key = `${contractAddress.toString()}_${tag.value.toString()}`;
|
|
421
|
-
return this.#publicLogsByContractAndTag.getAsync(key);
|
|
422
|
-
}),
|
|
423
|
-
);
|
|
424
|
-
const start = page * MAX_LOGS_PER_TAG;
|
|
425
|
-
const end = start + MAX_LOGS_PER_TAG;
|
|
426
|
-
|
|
427
|
-
return logs.map(logBuffers => {
|
|
428
|
-
const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
|
|
429
|
-
if (upToBlockNumber !== undefined) {
|
|
430
|
-
const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
|
|
431
|
-
if (cutoff !== -1) {
|
|
432
|
-
return deserialized.slice(0, cutoff);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
return deserialized;
|
|
436
|
-
});
|
|
194
|
+
/** Returns one inner array per element of `query.tags`, in input order. */
|
|
195
|
+
getPublicLogsByTags(query: PublicLogsQuery): Promise<LogResult[][]> {
|
|
196
|
+
LogStore.#validateQuery(query);
|
|
197
|
+
return this.db.transactionAsync(() => this.#runQuery(query, fieldHex(query.contractAddress)));
|
|
437
198
|
}
|
|
438
199
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
* @returns The requested logs.
|
|
443
|
-
*/
|
|
444
|
-
getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
|
|
445
|
-
if (filter.afterLog) {
|
|
446
|
-
return this.#filterPublicLogsBetweenBlocks(filter);
|
|
447
|
-
} else if (filter.txHash) {
|
|
448
|
-
return this.#filterPublicLogsOfTx(filter);
|
|
449
|
-
} else {
|
|
450
|
-
return this.#filterPublicLogsBetweenBlocks(filter);
|
|
200
|
+
static #validateQuery(query: { txHash?: TxHash; fromBlock?: unknown; toBlock?: unknown }): void {
|
|
201
|
+
if (query.txHash !== undefined && (query.fromBlock !== undefined || query.toBlock !== undefined)) {
|
|
202
|
+
throw new Error('`txHash` is mutually exclusive with `fromBlock`/`toBlock`');
|
|
451
203
|
}
|
|
452
204
|
}
|
|
453
205
|
|
|
454
|
-
async #
|
|
455
|
-
|
|
456
|
-
|
|
206
|
+
async #runQuery(query: PrivateLogsQuery | PublicLogsQuery, contractHex: string | undefined): Promise<LogResult[][]> {
|
|
207
|
+
const isPublic = contractHex !== undefined;
|
|
208
|
+
const tags = (query.tags as ReadonlyArray<TagQuery<Tag | SiloedTag>>) ?? [];
|
|
209
|
+
const primaryMap = isPublic ? this.#publicLogs : this.#privateLogs;
|
|
210
|
+
|
|
211
|
+
// referenceBlock reorg check, in-transaction, against the same db the log primary maps live on. The
|
|
212
|
+
// genesis block is a valid anchor during early sync but is synthetic and never indexed in the block
|
|
213
|
+
// store, so resolve it directly to the genesis block number rather than mistaking it for a reorg.
|
|
214
|
+
let referenceBlockNumber: number | undefined;
|
|
215
|
+
if (query.referenceBlock) {
|
|
216
|
+
if (query.referenceBlock.equals(this.genesisBlockHash)) {
|
|
217
|
+
referenceBlockNumber = INITIAL_L2_BLOCK_NUM - 1;
|
|
218
|
+
} else {
|
|
219
|
+
const refBlk = await this.blockStore.getBlockData({ hash: query.referenceBlock });
|
|
220
|
+
if (!refBlk) {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`Reference block ${query.referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
referenceBlockNumber = refBlk.header.globalVariables.blockNumber;
|
|
226
|
+
}
|
|
457
227
|
}
|
|
458
228
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
229
|
+
// Compute the exclusive upper-block bound across `toBlock` and `referenceBlock`.
|
|
230
|
+
// `toBlock` is already exclusive; `referenceBlock` caps inclusively, so its exclusive form is +1.
|
|
231
|
+
let upperExclusive: number | undefined;
|
|
232
|
+
if (query.toBlock !== undefined) {
|
|
233
|
+
upperExclusive = query.toBlock;
|
|
234
|
+
}
|
|
235
|
+
if (referenceBlockNumber !== undefined) {
|
|
236
|
+
const refExclusive = referenceBlockNumber + 1;
|
|
237
|
+
upperExclusive = upperExclusive === undefined ? refExclusive : Math.min(upperExclusive, refExclusive);
|
|
462
238
|
}
|
|
463
239
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
|
|
475
|
-
for (let i = 0; i < numLogsInTx; i++) {
|
|
476
|
-
publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
|
|
240
|
+
// Resolve txHash -> (blockNumber, txIndexInBlock) once for the whole query.
|
|
241
|
+
let txLocation: [number, number] | undefined;
|
|
242
|
+
if (query.txHash) {
|
|
243
|
+
const loc = await this.blockStore.getTxLocation(query.txHash);
|
|
244
|
+
if (!loc) {
|
|
245
|
+
return tags.map(() => []);
|
|
246
|
+
}
|
|
247
|
+
txLocation = loc;
|
|
248
|
+
if (upperExclusive !== undefined && txLocation[0] >= upperExclusive) {
|
|
249
|
+
return tags.map(() => []);
|
|
477
250
|
}
|
|
478
251
|
}
|
|
479
252
|
|
|
480
|
-
const
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
const
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
253
|
+
const fromBlock = query.fromBlock ?? INITIAL_L2_BLOCK_NUM;
|
|
254
|
+
const includeEffects = query.includeEffects === true;
|
|
255
|
+
|
|
256
|
+
const perTagResults: LogResult[][] = [];
|
|
257
|
+
for (const tagEntry of tags) {
|
|
258
|
+
const { tagHex, afterLog } = normalizeTagEntry(tagEntry);
|
|
259
|
+
const prefix = contractHex !== undefined ? encodePublicPrefix(contractHex, tagHex) : tagHex;
|
|
260
|
+
|
|
261
|
+
const end = txLocation
|
|
262
|
+
? endOfTxRange(prefix, txLocation[0], txLocation[1])
|
|
263
|
+
: endOfTagRange(prefix, upperExclusive);
|
|
264
|
+
|
|
265
|
+
let start: string;
|
|
266
|
+
if (afterLog) {
|
|
267
|
+
// Cursor wins as the start; `fromBlock` is ignored (fine if the cursor sits below it). The cursor
|
|
268
|
+
// carries `(blockNumber, txIndexWithinBlock, logIndexWithinTx)`, which slot directly into the
|
|
269
|
+
// composite key — no tx-hash lookup needed.
|
|
270
|
+
start = incKey(encodeKey(prefix, afterLog.blockNumber, afterLog.txIndexWithinBlock, afterLog.logIndexWithinTx));
|
|
271
|
+
} else if (txLocation) {
|
|
272
|
+
start = encodeKey(prefix, txLocation[0], txLocation[1], 0);
|
|
273
|
+
} else {
|
|
274
|
+
start = encodeKey(prefix, fromBlock, 0, 0);
|
|
275
|
+
}
|
|
500
276
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
277
|
+
const limit = query.limitPerTag ?? MAX_LOGS_PER_TAG;
|
|
278
|
+
const out: LogResult[] = [];
|
|
279
|
+
for await (const [rawKey, rawVal] of primaryMap.entriesAsync({ start, end, limit })) {
|
|
280
|
+
const tail = decodeKeyTail(rawKey);
|
|
281
|
+
const value = decodeValue(rawVal);
|
|
282
|
+
out.push({
|
|
283
|
+
logData: value.logData,
|
|
284
|
+
blockNumber: tail.blockNumber,
|
|
285
|
+
blockHash: value.blockHash,
|
|
286
|
+
blockTimestamp: value.blockTimestamp,
|
|
287
|
+
txHash: value.txHash,
|
|
288
|
+
txIndexWithinBlock: tail.txIndexWithinBlock,
|
|
289
|
+
logIndexWithinTx: tail.logIndexWithinTx,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
perTagResults.push(out);
|
|
506
293
|
}
|
|
507
294
|
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
const blockHash = this.#unpackBlockHash(reader);
|
|
516
|
-
|
|
517
|
-
while (reader.remainingBytes() > 0) {
|
|
518
|
-
const indexOfTx = reader.readNumber();
|
|
519
|
-
const txHash = reader.readObject(TxHash);
|
|
520
|
-
const numLogsInTx = reader.readNumber();
|
|
521
|
-
publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
|
|
522
|
-
for (let i = 0; i < numLogsInTx; i++) {
|
|
523
|
-
publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
|
|
295
|
+
if (includeEffects) {
|
|
296
|
+
// Dedupe by txHash across the entire page so a tx with many tagged logs costs one fetch.
|
|
297
|
+
const txHashByKey = new Map<string, TxHash>();
|
|
298
|
+
for (const arr of perTagResults) {
|
|
299
|
+
for (const log of arr) {
|
|
300
|
+
txHashByKey.set(log.txHash.toString(), log.txHash);
|
|
524
301
|
}
|
|
525
302
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
);
|
|
537
|
-
if (maxLogsHit) {
|
|
538
|
-
this.#log.debug(`Max logs hit at block ${blockNumber}`);
|
|
539
|
-
break loopOverBlocks;
|
|
303
|
+
const uniqueTxs = Array.from(txHashByKey.values());
|
|
304
|
+
if (uniqueTxs.length > 0) {
|
|
305
|
+
const effects = await this.blockStore.getNoteHashesAndNullifiers(uniqueTxs);
|
|
306
|
+
const byTxHash = new Map<string, [Fr[], Fr[]]>();
|
|
307
|
+
uniqueTxs.forEach((tx, i) => byTxHash.set(tx.toString(), effects[i]));
|
|
308
|
+
for (let i = 0; i < perTagResults.length; i++) {
|
|
309
|
+
perTagResults[i] = perTagResults[i].map(log => {
|
|
310
|
+
const [noteHashes, nullifiers] = byTxHash.get(log.txHash.toString()) ?? [[], []];
|
|
311
|
+
return { ...log, noteHashes, nullifiers };
|
|
312
|
+
});
|
|
540
313
|
}
|
|
541
314
|
}
|
|
542
315
|
}
|
|
543
316
|
|
|
544
|
-
return
|
|
317
|
+
return perTagResults;
|
|
545
318
|
}
|
|
546
319
|
|
|
547
320
|
/**
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
321
|
+
* Reads back every private log indexed for the given block via the per-block secondary index. Order
|
|
322
|
+
* matches the canonical composite-key order (`tag`, `blockNumber`, `txIndexWithinBlock`,
|
|
323
|
+
* `logIndexWithinTx`). Used by the data-store-updater test suite to verify the indexed-vs-block-body
|
|
324
|
+
* counts without depending on the removed `getPublicLogs(LogFilter)` API.
|
|
551
325
|
*/
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
} else if (filter.txHash) {
|
|
556
|
-
return this.#filterContractClassLogsOfTx(filter);
|
|
557
|
-
} else {
|
|
558
|
-
return this.#filterContractClassLogsBetweenBlocks(filter);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
async #filterContractClassLogsOfTx(filter: LogFilter): Promise<GetContractClassLogsResponse> {
|
|
563
|
-
if (!filter.txHash) {
|
|
564
|
-
throw new Error('Missing txHash');
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
const [blockNumber, txIndex] = (await this.blockStore.getTxLocation(filter.txHash)) ?? [];
|
|
568
|
-
if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
|
|
569
|
-
return { logs: [], maxLogsHit: false };
|
|
570
|
-
}
|
|
571
|
-
const contractClassLogsBuffer = (await this.#contractClassLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
|
|
572
|
-
const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
|
|
573
|
-
|
|
574
|
-
const reader = new BufferReader(contractClassLogsBuffer);
|
|
575
|
-
const blockHash = this.#unpackBlockHash(reader);
|
|
576
|
-
|
|
577
|
-
while (reader.remainingBytes() > 0) {
|
|
578
|
-
const indexOfTx = reader.readNumber();
|
|
579
|
-
const txHash = reader.readObject(TxHash);
|
|
580
|
-
const numLogsInTx = reader.readNumber();
|
|
581
|
-
contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
|
|
582
|
-
for (let i = 0; i < numLogsInTx; i++) {
|
|
583
|
-
contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
const txData = contractClassLogsInBlock[txIndex];
|
|
588
|
-
|
|
589
|
-
const logs: ExtendedContractClassLog[] = [];
|
|
590
|
-
const maxLogsHit = this.#accumulateContractClassLogs(
|
|
591
|
-
logs,
|
|
592
|
-
blockNumber,
|
|
593
|
-
blockHash,
|
|
594
|
-
txIndex,
|
|
595
|
-
txData.txHash,
|
|
596
|
-
txData.logs,
|
|
597
|
-
filter,
|
|
326
|
+
getPrivateLogsForBlock(blockNumber: number): Promise<LogResult[]> {
|
|
327
|
+
return this.db.transactionAsync(() =>
|
|
328
|
+
this.#readBlockLogs(this.#privateKeysByBlock, this.#privateLogs, blockNumber),
|
|
598
329
|
);
|
|
599
|
-
|
|
600
|
-
return { logs, maxLogsHit };
|
|
601
330
|
}
|
|
602
331
|
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
const end = filter.toBlock;
|
|
607
|
-
|
|
608
|
-
if (typeof end === 'number' && end < start) {
|
|
609
|
-
return {
|
|
610
|
-
logs: [],
|
|
611
|
-
maxLogsHit: true,
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
const logs: ExtendedContractClassLog[] = [];
|
|
616
|
-
|
|
617
|
-
let maxLogsHit = false;
|
|
618
|
-
loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#contractClassLogsByBlock.entriesAsync({
|
|
619
|
-
start,
|
|
620
|
-
end,
|
|
621
|
-
})) {
|
|
622
|
-
const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
|
|
623
|
-
const reader = new BufferReader(logBuffer);
|
|
624
|
-
const blockHash = this.#unpackBlockHash(reader);
|
|
625
|
-
while (reader.remainingBytes() > 0) {
|
|
626
|
-
const indexOfTx = reader.readNumber();
|
|
627
|
-
const txHash = reader.readObject(TxHash);
|
|
628
|
-
const numLogsInTx = reader.readNumber();
|
|
629
|
-
contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
|
|
630
|
-
for (let i = 0; i < numLogsInTx; i++) {
|
|
631
|
-
contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < contractClassLogsInBlock.length; txIndex++) {
|
|
635
|
-
const txData = contractClassLogsInBlock[txIndex];
|
|
636
|
-
maxLogsHit = this.#accumulateContractClassLogs(
|
|
637
|
-
logs,
|
|
638
|
-
blockNumber,
|
|
639
|
-
blockHash,
|
|
640
|
-
txIndex,
|
|
641
|
-
txData.txHash,
|
|
642
|
-
txData.logs,
|
|
643
|
-
filter,
|
|
644
|
-
);
|
|
645
|
-
if (maxLogsHit) {
|
|
646
|
-
this.#log.debug(`Max logs hit at block ${blockNumber}`);
|
|
647
|
-
break loopOverBlocks;
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
return { logs, maxLogsHit };
|
|
332
|
+
/** {@inheritDoc LogStore.getPrivateLogsForBlock} */
|
|
333
|
+
getPublicLogsForBlock(blockNumber: number): Promise<LogResult[]> {
|
|
334
|
+
return this.db.transactionAsync(() => this.#readBlockLogs(this.#publicKeysByBlock, this.#publicLogs, blockNumber));
|
|
653
335
|
}
|
|
654
336
|
|
|
655
|
-
#
|
|
656
|
-
|
|
337
|
+
async #readBlockLogs(
|
|
338
|
+
keysByBlock: AztecAsyncMap<number, string[]>,
|
|
339
|
+
primaryMap: AztecAsyncMap<string, Buffer>,
|
|
657
340
|
blockNumber: number,
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
filter: LogFilter = {},
|
|
663
|
-
): boolean {
|
|
664
|
-
if (filter.fromBlock && blockNumber < filter.fromBlock) {
|
|
665
|
-
return false;
|
|
666
|
-
}
|
|
667
|
-
if (filter.toBlock && blockNumber >= filter.toBlock) {
|
|
668
|
-
return false;
|
|
669
|
-
}
|
|
670
|
-
if (filter.txHash && !txHash.equals(filter.txHash)) {
|
|
671
|
-
return false;
|
|
341
|
+
): Promise<LogResult[]> {
|
|
342
|
+
const keys = await keysByBlock.getAsync(blockNumber);
|
|
343
|
+
if (!keys || keys.length === 0) {
|
|
344
|
+
return [];
|
|
672
345
|
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
if (
|
|
679
|
-
(!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) &&
|
|
680
|
-
(!filter.tag || log.fields[0]?.equals(filter.tag))
|
|
681
|
-
) {
|
|
682
|
-
results.push(
|
|
683
|
-
new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
|
|
684
|
-
);
|
|
685
|
-
|
|
686
|
-
if (results.length >= this.#logsMaxPageSize) {
|
|
687
|
-
maxLogsHit = true;
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
346
|
+
const results: LogResult[] = [];
|
|
347
|
+
for (const key of keys) {
|
|
348
|
+
const raw = await primaryMap.getAsync(key);
|
|
349
|
+
if (!raw) {
|
|
350
|
+
continue;
|
|
690
351
|
}
|
|
352
|
+
const tail = decodeKeyTail(key);
|
|
353
|
+
const value = decodeValue(raw);
|
|
354
|
+
results.push({
|
|
355
|
+
logData: value.logData,
|
|
356
|
+
blockNumber: tail.blockNumber,
|
|
357
|
+
blockHash: value.blockHash,
|
|
358
|
+
blockTimestamp: value.blockTimestamp,
|
|
359
|
+
txHash: value.txHash,
|
|
360
|
+
txIndexWithinBlock: tail.txIndexWithinBlock,
|
|
361
|
+
logIndexWithinTx: tail.logIndexWithinTx,
|
|
362
|
+
});
|
|
691
363
|
}
|
|
692
|
-
|
|
693
|
-
return maxLogsHit;
|
|
364
|
+
return results;
|
|
694
365
|
}
|
|
366
|
+
}
|
|
695
367
|
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
if (filter.fromBlock && blockNumber < filter.fromBlock) {
|
|
706
|
-
return false;
|
|
707
|
-
}
|
|
708
|
-
if (filter.toBlock && blockNumber >= filter.toBlock) {
|
|
709
|
-
return false;
|
|
710
|
-
}
|
|
711
|
-
if (filter.txHash && !txHash.equals(filter.txHash)) {
|
|
712
|
-
return false;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
let maxLogsHit = false;
|
|
716
|
-
let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
|
|
717
|
-
for (; logIndex < txLogs.length; logIndex++) {
|
|
718
|
-
const log = txLogs[logIndex];
|
|
719
|
-
if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
|
|
720
|
-
results.push(
|
|
721
|
-
new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
|
|
722
|
-
);
|
|
723
|
-
|
|
724
|
-
if (results.length >= this.#logsMaxPageSize) {
|
|
725
|
-
maxLogsHit = true;
|
|
726
|
-
break;
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
return maxLogsHit;
|
|
368
|
+
/** Pulls `{ tagHex, afterLog }` out of a {@link TagQuery}, normalizing the bare-tag form. */
|
|
369
|
+
function normalizeTagEntry<T extends Tag | SiloedTag>(
|
|
370
|
+
entry: TagQuery<T>,
|
|
371
|
+
): {
|
|
372
|
+
tagHex: string;
|
|
373
|
+
afterLog: LogCursor | undefined;
|
|
374
|
+
} {
|
|
375
|
+
if (typeof entry === 'object' && entry !== null && 'tag' in entry) {
|
|
376
|
+
return { tagHex: fieldHex(entry.tag.value), afterLog: entry.afterLog };
|
|
732
377
|
}
|
|
378
|
+
return { tagHex: fieldHex((entry as T).value), afterLog: undefined };
|
|
733
379
|
}
|