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