@aztec/pxe 0.0.1-commit.dbf9cec → 0.0.1-commit.e0f15ab9b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/config/index.d.ts +2 -2
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.d.ts +9 -3
- package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.js +28 -6
- package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts +5 -5
- package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts.map +1 -1
- package/dest/contract_function_simulator/execution_tagging_index_cache.js +17 -9
- package/dest/contract_function_simulator/index.d.ts +2 -1
- package/dest/contract_function_simulator/index.d.ts.map +1 -1
- package/dest/contract_function_simulator/index.js +1 -0
- package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts +2 -3
- package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/event_validation_request.js +5 -4
- package/dest/contract_function_simulator/noir-structs/log_retrieval_response.d.ts +1 -1
- package/dest/contract_function_simulator/noir-structs/log_retrieval_response.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/log_retrieval_response.js +1 -3
- package/dest/contract_function_simulator/noir-structs/message_tx_context.d.ts +16 -0
- package/dest/contract_function_simulator/noir-structs/message_tx_context.d.ts.map +1 -0
- package/dest/contract_function_simulator/noir-structs/message_tx_context.js +57 -0
- package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +2 -4
- package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/note_validation_request.js +3 -5
- package/dest/contract_function_simulator/oracle/interfaces.d.ts +50 -45
- package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/legacy_oracle_mappings.d.ts +9 -0
- package/dest/contract_function_simulator/oracle/legacy_oracle_mappings.d.ts.map +1 -0
- package/dest/contract_function_simulator/oracle/legacy_oracle_mappings.js +42 -0
- package/dest/contract_function_simulator/oracle/oracle.d.ts +45 -44
- package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/oracle.js +163 -94
- package/dest/contract_function_simulator/oracle/private_execution.js +5 -3
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +22 -47
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.js +30 -62
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +53 -35
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +101 -43
- package/dest/contract_sync/contract_sync_service.d.ts +5 -3
- package/dest/contract_sync/contract_sync_service.d.ts.map +1 -1
- package/dest/contract_sync/contract_sync_service.js +47 -30
- package/dest/messages/message_context_service.d.ts +17 -0
- package/dest/messages/message_context_service.d.ts.map +1 -0
- package/dest/messages/message_context_service.js +36 -0
- package/dest/oracle_version.d.ts +2 -2
- package/dest/oracle_version.js +3 -3
- package/dest/pxe.d.ts +8 -4
- package/dest/pxe.d.ts.map +1 -1
- package/dest/pxe.js +39 -22
- package/dest/storage/metadata.d.ts +1 -1
- package/dest/storage/metadata.js +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.d.ts +26 -25
- package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.js +141 -115
- package/dest/tagging/index.d.ts +2 -2
- package/dest/tagging/index.d.ts.map +1 -1
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +1 -1
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +10 -1
- package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts +4 -3
- package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts.map +1 -1
- package/dest/tagging/sender_sync/utils/get_status_change_of_pending.js +20 -10
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +2 -1
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +24 -11
- package/package.json +16 -16
- package/src/config/index.ts +1 -1
- package/src/contract_function_simulator/contract_function_simulator.ts +39 -7
- package/src/contract_function_simulator/execution_tagging_index_cache.ts +16 -11
- package/src/contract_function_simulator/index.ts +1 -0
- package/src/contract_function_simulator/noir-structs/event_validation_request.ts +8 -5
- package/src/contract_function_simulator/noir-structs/log_retrieval_response.ts +1 -4
- package/src/contract_function_simulator/noir-structs/message_tx_context.ts +55 -0
- package/src/contract_function_simulator/noir-structs/note_validation_request.ts +3 -6
- package/src/contract_function_simulator/oracle/interfaces.ts +54 -54
- package/src/contract_function_simulator/oracle/legacy_oracle_mappings.ts +135 -0
- package/src/contract_function_simulator/oracle/oracle.ts +176 -138
- package/src/contract_function_simulator/oracle/private_execution.ts +4 -4
- package/src/contract_function_simulator/oracle/private_execution_oracle.ts +32 -80
- package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +136 -53
- package/src/contract_sync/contract_sync_service.ts +67 -38
- package/src/messages/message_context_service.ts +45 -0
- package/src/oracle_version.ts +3 -3
- package/src/pxe.ts +57 -21
- package/src/storage/metadata.ts +1 -1
- package/src/storage/tagging_store/sender_tagging_store.ts +182 -135
- package/src/tagging/index.ts +1 -1
- package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +19 -1
- package/src/tagging/sender_sync/utils/get_status_change_of_pending.ts +26 -11
- package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +19 -9
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
2
|
-
import
|
|
3
|
-
import { TxHash } from '@aztec/stdlib/tx';
|
|
2
|
+
import { ExtendedDirectionalAppTaggingSecret, SiloedTag, type TaggingIndexRange } from '@aztec/stdlib/logs';
|
|
3
|
+
import { TxEffect, TxHash } from '@aztec/stdlib/tx';
|
|
4
4
|
|
|
5
5
|
import type { StagedStore } from '../../job_coordinator/job_coordinator.js';
|
|
6
6
|
import { UNFINALIZED_TAGGING_INDEXES_WINDOW_LEN } from '../../tagging/constants.js';
|
|
7
7
|
|
|
8
|
+
/** Internal representation of a pending index range entry. */
|
|
9
|
+
type PendingIndexesEntry = { lowestIndex: number; highestIndex: number; txHash: string };
|
|
10
|
+
|
|
8
11
|
/**
|
|
9
12
|
* Data provider of tagging data used when syncing the sender tagging indexes. The recipient counterpart of this class
|
|
10
13
|
* is called RecipientTaggingStore. We have the data stores separate for sender and recipient because
|
|
@@ -15,20 +18,19 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
15
18
|
|
|
16
19
|
#store: AztecAsyncKVStore;
|
|
17
20
|
|
|
18
|
-
// Stores the pending
|
|
19
|
-
// the private logs with tags corresponding to these indexes has not been finalized yet.
|
|
20
|
-
//
|
|
21
|
-
// We don't store just the highest index because if their transaction is dropped we'd then need the information about
|
|
22
|
-
// the lower pending indexes. For each secret-tx pair however we only store the largest index used in that tx, since
|
|
23
|
-
// the smaller ones are irrelevant due to tx atomicity.
|
|
21
|
+
// Stores the pending index ranges for each directional app tagging secret. Pending here means that the tx that
|
|
22
|
+
// contained the private logs with tags corresponding to these indexes has not been finalized yet.
|
|
24
23
|
//
|
|
25
|
-
//
|
|
24
|
+
// We store the full range (lowestIndex, highestIndex) for each secret-tx pair because transactions can partially
|
|
25
|
+
// revert, in which case only some logs (from the non-revertible phase) survive onchain. By storing the range,
|
|
26
|
+
// we can expand it and check each individual siloed tag against the TxEffect to determine which indexes made it
|
|
27
|
+
// onchain.
|
|
26
28
|
//
|
|
27
|
-
// directional app tagging secret => {
|
|
28
|
-
#pendingIndexes: AztecAsyncMap<string,
|
|
29
|
+
// directional app tagging secret => { lowestIndex, highestIndex, txHash }[]
|
|
30
|
+
#pendingIndexes: AztecAsyncMap<string, PendingIndexesEntry[]>;
|
|
29
31
|
|
|
30
|
-
// jobId => directional app tagging secret => {
|
|
31
|
-
#pendingIndexesForJob: Map<string, Map<string,
|
|
32
|
+
// jobId => directional app tagging secret => { lowestIndex, highestIndex, txHash }[]
|
|
33
|
+
#pendingIndexesForJob: Map<string, Map<string, PendingIndexesEntry[]>>;
|
|
32
34
|
|
|
33
35
|
// Stores the last (highest) finalized index for each directional app tagging secret. We care only about the last
|
|
34
36
|
// index because unlike the pending indexes, it will never happen that a finalized index would be removed and hence
|
|
@@ -50,7 +52,7 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
50
52
|
this.#lastFinalizedIndexesForJob = new Map();
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
#getPendingIndexesForJob(jobId: string): Map<string,
|
|
55
|
+
#getPendingIndexesForJob(jobId: string): Map<string, PendingIndexesEntry[]> {
|
|
54
56
|
let pendingIndexesForJob = this.#pendingIndexesForJob.get(jobId);
|
|
55
57
|
if (!pendingIndexesForJob) {
|
|
56
58
|
pendingIndexesForJob = new Map();
|
|
@@ -68,7 +70,7 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
68
70
|
return jobStagedLastFinalizedIndexes;
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
async #readPendingIndexes(jobId: string, secret: string): Promise<
|
|
73
|
+
async #readPendingIndexes(jobId: string, secret: string): Promise<PendingIndexesEntry[]> {
|
|
72
74
|
// Always issue DB read to keep IndexedDB transaction alive (they auto-commit when a new micro-task starts and there
|
|
73
75
|
// are no pending read requests). The staged value still takes precedence if it exists.
|
|
74
76
|
const dbValue = await this.#pendingIndexes.getAsync(secret);
|
|
@@ -76,7 +78,7 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
76
78
|
return staged !== undefined ? staged : (dbValue ?? []);
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
#writePendingIndexes(jobId: string, secret: string, pendingIndexes:
|
|
81
|
+
#writePendingIndexes(jobId: string, secret: string, pendingIndexes: PendingIndexesEntry[]) {
|
|
80
82
|
this.#getPendingIndexesForJob(jobId).set(secret, pendingIndexes);
|
|
81
83
|
}
|
|
82
84
|
|
|
@@ -126,57 +128,37 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
/**
|
|
129
|
-
* Stores pending
|
|
130
|
-
* @remarks
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* @param
|
|
134
|
-
* stored in the db.
|
|
135
|
-
* @param txHash - The tx in which the
|
|
131
|
+
* Stores pending index ranges.
|
|
132
|
+
* @remarks If the same (secret, txHash) pair already exists in the db with an equal range, it's a no-op. This is
|
|
133
|
+
* expected to happen because whenever we start sync we start from the last finalized index and we can have pending
|
|
134
|
+
* ranges already stored from previous syncs. If the ranges differ, it throws an error as that indicates a bug.
|
|
135
|
+
* @param ranges - The tagging index ranges containing the directional app tagging secrets and the index ranges that are
|
|
136
|
+
* to be stored in the db.
|
|
137
|
+
* @param txHash - The tx in which the tagging indexes were used in private logs.
|
|
136
138
|
* @param jobId - job context for staged writes to this store. See `JobCoordinator` for more details.
|
|
137
|
-
* @throws If
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* @throws If the newly stored pending index is further than window length from the highest finalized index for the
|
|
141
|
-
* same secret. This is enforced in order to give a guarantee to a recipient that he doesn't need to look further than
|
|
142
|
-
* window length ahead of the highest finalized index.
|
|
143
|
-
* @throws If a secret + txHash pair already exists in the db with a different index value. It should never happen
|
|
144
|
-
* that we would attempt to store a different index for a given secret-txHash pair because we always store just the
|
|
145
|
-
* highest index for a given secret-txHash pair. Hence this is a good way to catch bugs.
|
|
146
|
-
* @throws If the newly stored pending index is lower than or equal to the last finalized index for the same secret.
|
|
147
|
-
* This is enforced because this should never happen if the syncing is done correctly as we look for logs from higher
|
|
148
|
-
* indexes than finalized ones.
|
|
139
|
+
* @throws If the highestIndex is further than window length from the highest finalized index for the same secret.
|
|
140
|
+
* @throws If the lowestIndex is lower than or equal to the last finalized index for the same secret.
|
|
141
|
+
* @throws If a different range already exists for the same (secret, txHash) pair.
|
|
149
142
|
*/
|
|
150
|
-
storePendingIndexes(
|
|
151
|
-
if (
|
|
143
|
+
storePendingIndexes(ranges: TaggingIndexRange[], txHash: TxHash, jobId: string): Promise<void> {
|
|
144
|
+
if (ranges.length === 0) {
|
|
152
145
|
return Promise.resolve();
|
|
153
146
|
}
|
|
154
147
|
|
|
155
|
-
// The secrets in pre-tags should be unique because we always store just the highest index per given secret-txHash
|
|
156
|
-
// pair. Below we check that this is the case.
|
|
157
|
-
const secretsSet = new Set(preTags.map(preTag => preTag.extendedSecret.toString()));
|
|
158
|
-
if (secretsSet.size !== preTags.length) {
|
|
159
|
-
return Promise.reject(new Error(`Duplicate secrets found when storing pending indexes`));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
148
|
const txHashStr = txHash.toString();
|
|
163
149
|
|
|
164
150
|
return this.#store.transactionAsync(async () => {
|
|
165
151
|
// Prefetch all data, start reads during iteration to keep IndexedDB transaction alive
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
pending: this.#readPendingIndexes(jobId, secretStr),
|
|
173
|
-
finalized: this.#readLastFinalizedIndex(jobId, secretStr),
|
|
174
|
-
};
|
|
175
|
-
});
|
|
152
|
+
const rangeReadPromises = ranges.map(range => ({
|
|
153
|
+
range,
|
|
154
|
+
secretStr: range.extendedSecret.toString(),
|
|
155
|
+
pending: this.#readPendingIndexes(jobId, range.extendedSecret.toString()),
|
|
156
|
+
finalized: this.#readLastFinalizedIndex(jobId, range.extendedSecret.toString()),
|
|
157
|
+
}));
|
|
176
158
|
|
|
177
159
|
// Await all reads together
|
|
178
|
-
const
|
|
179
|
-
|
|
160
|
+
const rangeData = await Promise.all(
|
|
161
|
+
rangeReadPromises.map(async item => ({
|
|
180
162
|
...item,
|
|
181
163
|
pendingData: await item.pending,
|
|
182
164
|
finalizedIndex: await item.finalized,
|
|
@@ -184,48 +166,51 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
184
166
|
);
|
|
185
167
|
|
|
186
168
|
// Process in memory and validate
|
|
187
|
-
for (const {
|
|
188
|
-
//
|
|
189
|
-
|
|
190
|
-
if (index > (finalizedIndex ?? 0) + UNFINALIZED_TAGGING_INDEXES_WINDOW_LEN) {
|
|
169
|
+
for (const { range, secretStr, pendingData, finalizedIndex } of rangeData) {
|
|
170
|
+
// Check that the highest index is not further than window length from the highest finalized index.
|
|
171
|
+
if (range.highestIndex > (finalizedIndex ?? 0) + UNFINALIZED_TAGGING_INDEXES_WINDOW_LEN) {
|
|
191
172
|
throw new Error(
|
|
192
|
-
`Highest used index ${
|
|
173
|
+
`Highest used index ${range.highestIndex} is further than window length from the highest finalized index ${finalizedIndex ?? 0}.
|
|
193
174
|
Tagging window length ${UNFINALIZED_TAGGING_INDEXES_WINDOW_LEN} is configured too low. Contact the Aztec team
|
|
194
175
|
to increase it!`,
|
|
195
176
|
);
|
|
196
177
|
}
|
|
197
178
|
|
|
198
|
-
// Throw if the
|
|
199
|
-
if (finalizedIndex !== undefined &&
|
|
179
|
+
// Throw if the lowest index is lower than or equal to the last finalized index
|
|
180
|
+
if (finalizedIndex !== undefined && range.lowestIndex <= finalizedIndex) {
|
|
200
181
|
throw new Error(
|
|
201
|
-
`Cannot store pending index ${
|
|
202
|
-
`
|
|
182
|
+
`Cannot store pending index range [${range.lowestIndex}, ${range.highestIndex}] for secret ${secretStr}: ` +
|
|
183
|
+
`lowestIndex is lower than or equal to the last finalized index ${finalizedIndex}`,
|
|
203
184
|
);
|
|
204
185
|
}
|
|
205
186
|
|
|
206
|
-
// Check if
|
|
207
|
-
const
|
|
187
|
+
// Check if an entry with the same txHash already exists
|
|
188
|
+
const existingEntry = pendingData.find(entry => entry.txHash === txHashStr);
|
|
208
189
|
|
|
209
|
-
if (
|
|
210
|
-
//
|
|
211
|
-
if (
|
|
190
|
+
if (existingEntry) {
|
|
191
|
+
// Assert that the ranges are equal — different ranges for the same (secret, txHash) indicates a bug
|
|
192
|
+
if (existingEntry.lowestIndex !== range.lowestIndex || existingEntry.highestIndex !== range.highestIndex) {
|
|
212
193
|
throw new Error(
|
|
213
|
-
`
|
|
214
|
-
`
|
|
194
|
+
`Conflicting range for secret ${secretStr} and txHash ${txHashStr}: ` +
|
|
195
|
+
`existing [${existingEntry.lowestIndex}, ${existingEntry.highestIndex}] vs ` +
|
|
196
|
+
`new [${range.lowestIndex}, ${range.highestIndex}]`,
|
|
215
197
|
);
|
|
216
198
|
}
|
|
217
|
-
//
|
|
199
|
+
// Exact duplicate — skip
|
|
218
200
|
} else {
|
|
219
|
-
|
|
220
|
-
|
|
201
|
+
this.#writePendingIndexes(jobId, secretStr, [
|
|
202
|
+
...pendingData,
|
|
203
|
+
{ lowestIndex: range.lowestIndex, highestIndex: range.highestIndex, txHash: txHashStr },
|
|
204
|
+
]);
|
|
221
205
|
}
|
|
222
206
|
}
|
|
223
207
|
});
|
|
224
208
|
}
|
|
225
209
|
|
|
226
210
|
/**
|
|
227
|
-
* Returns the transaction hashes of all pending transactions that contain indexes within a specified range
|
|
228
|
-
* for a given directional app tagging secret.
|
|
211
|
+
* Returns the transaction hashes of all pending transactions that contain highest indexes within a specified range
|
|
212
|
+
* for a given directional app tagging secret. We check based on the highest indexes only as that is the relevant
|
|
213
|
+
* information for the caller of this function.
|
|
229
214
|
* @param secret - The directional app tagging secret to query pending indexes for.
|
|
230
215
|
* @param startIndex - The lower bound of the index range (inclusive).
|
|
231
216
|
* @param endIndex - The upper bound of the index range (exclusive).
|
|
@@ -241,7 +226,7 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
241
226
|
return this.#store.transactionAsync(async () => {
|
|
242
227
|
const existing = await this.#readPendingIndexes(jobId, secret.toString());
|
|
243
228
|
const txHashes = existing
|
|
244
|
-
.filter(entry => entry.
|
|
229
|
+
.filter(entry => entry.highestIndex >= startIndex && entry.highestIndex < endIndex)
|
|
245
230
|
.map(entry => entry.txHash);
|
|
246
231
|
return Array.from(new Set(txHashes)).map(TxHash.fromString);
|
|
247
232
|
});
|
|
@@ -269,16 +254,15 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
269
254
|
const pendingPromise = this.#readPendingIndexes(jobId, secretStr);
|
|
270
255
|
const finalizedPromise = this.#readLastFinalizedIndex(jobId, secretStr);
|
|
271
256
|
|
|
272
|
-
const [
|
|
273
|
-
const pendingIndexes = pendingTxScopedIndexes.map(entry => entry.index);
|
|
257
|
+
const [pendingEntries, lastFinalized] = await Promise.all([pendingPromise, finalizedPromise]);
|
|
274
258
|
|
|
275
|
-
if (
|
|
259
|
+
if (pendingEntries.length === 0) {
|
|
276
260
|
return lastFinalized;
|
|
277
261
|
}
|
|
278
262
|
|
|
279
|
-
// As the last used index we return the highest one from the pending
|
|
280
|
-
// higher than the last finalized index because we prune lower pending indexes when a tx is finalized.
|
|
281
|
-
return Math.max(...
|
|
263
|
+
// As the last used index we return the highest one from the pending index ranges. Note that this value will be
|
|
264
|
+
// always higher than the last finalized index because we prune lower pending indexes when a tx is finalized.
|
|
265
|
+
return Math.max(...pendingEntries.map(entry => entry.highestIndex));
|
|
282
266
|
});
|
|
283
267
|
}
|
|
284
268
|
|
|
@@ -294,7 +278,7 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
294
278
|
|
|
295
279
|
return this.#store.transactionAsync(async () => {
|
|
296
280
|
// Prefetch all data, start reads during iteration to keep IndexedDB transaction alive
|
|
297
|
-
const secretReadPromises: Map<string, Promise<
|
|
281
|
+
const secretReadPromises: Map<string, Promise<PendingIndexesEntry[]>> = new Map();
|
|
298
282
|
|
|
299
283
|
for await (const secret of this.#pendingIndexes.keysAsync()) {
|
|
300
284
|
secretReadPromises.set(secret, this.#readPendingIndexes(jobId, secret));
|
|
@@ -330,22 +314,15 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
330
314
|
});
|
|
331
315
|
}
|
|
332
316
|
|
|
333
|
-
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
finalizePendingIndexes(txHashes: TxHash[], jobId: string): Promise<void> {
|
|
338
|
-
if (txHashes.length === 0) {
|
|
339
|
-
return Promise.resolve();
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
const txHashStrings = new Set(txHashes.map(tx => tx.toString()));
|
|
343
|
-
|
|
317
|
+
/** Prefetches all pending and finalized index data for every secret (from both DB and staged writes). */
|
|
318
|
+
#getSecretsWithPendingData(
|
|
319
|
+
jobId: string,
|
|
320
|
+
): Promise<{ secret: string; pendingData: PendingIndexesEntry[]; lastFinalized: number | undefined }[]> {
|
|
344
321
|
return this.#store.transactionAsync(async () => {
|
|
345
322
|
// Prefetch all data, start reads during iteration to keep IndexedDB transaction alive
|
|
346
323
|
const secretDataPromises: Map<
|
|
347
324
|
string,
|
|
348
|
-
{ pending: Promise<
|
|
325
|
+
{ pending: Promise<PendingIndexesEntry[]>; finalized: Promise<number | undefined> }
|
|
349
326
|
> = new Map();
|
|
350
327
|
|
|
351
328
|
for await (const secret of this.#pendingIndexes.keysAsync()) {
|
|
@@ -375,55 +352,125 @@ export class SenderTaggingStore implements StagedStore {
|
|
|
375
352
|
})),
|
|
376
353
|
);
|
|
377
354
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
355
|
+
return dataResults.filter(r => r.pendingData.length > 0);
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Updates pending indexes corresponding to the given transaction hashes to be finalized and prunes any lower pending
|
|
361
|
+
* indexes.
|
|
362
|
+
*/
|
|
363
|
+
async finalizePendingIndexes(txHashes: TxHash[], jobId: string): Promise<void> {
|
|
364
|
+
if (txHashes.length === 0) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const txHashStrings = new Set(txHashes.map(tx => tx.toString()));
|
|
369
|
+
const secretsWithData = await this.#getSecretsWithPendingData(jobId);
|
|
370
|
+
|
|
371
|
+
for (const { secret, pendingData, lastFinalized } of secretsWithData) {
|
|
372
|
+
let currentPending = pendingData;
|
|
373
|
+
let currentFinalized = lastFinalized;
|
|
374
|
+
|
|
375
|
+
// Process all txHashes for this secret
|
|
376
|
+
for (const txHashStr of txHashStrings) {
|
|
377
|
+
const matchingEntries = currentPending.filter(item => item.txHash === txHashStr);
|
|
378
|
+
if (matchingEntries.length === 0) {
|
|
379
|
+
// This is expected as a higher index might have already been finalized which would lead to pruning of
|
|
380
|
+
// pending entries.
|
|
381
381
|
continue;
|
|
382
382
|
}
|
|
383
383
|
|
|
384
|
-
|
|
385
|
-
|
|
384
|
+
if (matchingEntries.length > 1) {
|
|
385
|
+
// We should always just store the highest pending index for a given tx hash and secret because the lower
|
|
386
|
+
// values are irrelevant.
|
|
387
|
+
throw new Error(`Multiple pending entries found for tx hash ${txHashStr} and secret ${secret}`);
|
|
388
|
+
}
|
|
386
389
|
|
|
387
|
-
|
|
388
|
-
for (const txHashStr of txHashStrings) {
|
|
389
|
-
const matchingIndexes = currentPending.filter(item => item.txHash === txHashStr).map(item => item.index);
|
|
390
|
-
if (matchingIndexes.length === 0) {
|
|
391
|
-
continue;
|
|
392
|
-
}
|
|
390
|
+
const newFinalized = matchingEntries[0].highestIndex;
|
|
393
391
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
392
|
+
if (newFinalized < (currentFinalized ?? 0)) {
|
|
393
|
+
// This should never happen because when last finalized index was finalized we should have pruned the lower
|
|
394
|
+
// pending indexes.
|
|
395
|
+
throw new Error(
|
|
396
|
+
`New finalized index ${newFinalized} is smaller than the current last finalized index ${currentFinalized}`,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
399
|
|
|
400
|
-
|
|
400
|
+
currentFinalized = newFinalized;
|
|
401
401
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
402
|
+
// When we add pending indexes, we ensure they are higher than the last finalized index. However, because we
|
|
403
|
+
// cannot control the order in which transactions are finalized, there may be pending indexes that are now
|
|
404
|
+
// obsolete because they are lower than the most recently finalized index. For this reason, we prune these
|
|
405
|
+
// outdated pending indexes.
|
|
406
|
+
currentPending = currentPending.filter(item => item.highestIndex > currentFinalized!);
|
|
407
|
+
}
|
|
409
408
|
|
|
410
|
-
|
|
409
|
+
// Write final state if changed
|
|
410
|
+
if (currentFinalized !== lastFinalized) {
|
|
411
|
+
this.#writeLastFinalizedIndex(jobId, secret, currentFinalized!);
|
|
412
|
+
}
|
|
413
|
+
if (currentPending !== pendingData) {
|
|
414
|
+
this.#writePendingIndexes(jobId, secret, currentPending);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
411
418
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Handles finalization of pending indexes for a transaction whose execution was partially reverted.
|
|
421
|
+
* Recomputes the siloed tags for each pending index of the given tx and checks which ones appear in the
|
|
422
|
+
* TxEffect's private logs (i.e., which ones made it onchain). Those that survived are finalized; those that
|
|
423
|
+
* didn't are dropped.
|
|
424
|
+
* @param txEffect - The tx effect of the partially reverted transaction.
|
|
425
|
+
* @param jobId - job context for staged writes to this store. See `JobCoordinator` for more details.
|
|
426
|
+
*/
|
|
427
|
+
async finalizePendingIndexesOfAPartiallyRevertedTx(txEffect: TxEffect, jobId: string): Promise<void> {
|
|
428
|
+
const txHashStr = txEffect.txHash.toString();
|
|
418
429
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
430
|
+
// Build a set of all siloed tag values that made it onchain (first field of each private log).
|
|
431
|
+
const onChainTags = new Set<string>(txEffect.privateLogs.map(log => log.fields[0].toString()));
|
|
432
|
+
|
|
433
|
+
const secretsWithData = await this.#getSecretsWithPendingData(jobId);
|
|
434
|
+
|
|
435
|
+
for (const { secret, pendingData, lastFinalized } of secretsWithData) {
|
|
436
|
+
const matchingEntries = pendingData.filter(item => item.txHash === txHashStr);
|
|
437
|
+
if (matchingEntries.length === 0) {
|
|
438
|
+
// This is expected as a higher index might have already been finalized which would lead to pruning of
|
|
439
|
+
// pending entries.
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (matchingEntries.length > 1) {
|
|
444
|
+
// We should always just store the highest pending index for a given tx hash and secret because the lower
|
|
445
|
+
// values are irrelevant.
|
|
446
|
+
throw new Error(`Multiple pending entries found for tx hash ${txHashStr} and secret ${secret}`);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const pendingEntry = matchingEntries[0];
|
|
450
|
+
|
|
451
|
+
// Expand each matching entry's range and recompute siloed tags for each index.
|
|
452
|
+
const extendedSecret = ExtendedDirectionalAppTaggingSecret.fromString(secret);
|
|
453
|
+
let highestSurvivingIndex: number | undefined;
|
|
454
|
+
|
|
455
|
+
for (let index = pendingEntry.lowestIndex; index <= pendingEntry.highestIndex; index++) {
|
|
456
|
+
const siloedTag = await SiloedTag.compute({ extendedSecret, index });
|
|
457
|
+
if (onChainTags.has(siloedTag.value.toString())) {
|
|
458
|
+
highestSurvivingIndex = highestSurvivingIndex !== undefined ? Math.max(highestSurvivingIndex, index) : index;
|
|
425
459
|
}
|
|
426
460
|
}
|
|
427
|
-
|
|
461
|
+
|
|
462
|
+
// Remove all entries for this txHash from pending (both surviving and non-surviving).
|
|
463
|
+
let currentPending = pendingData.filter(item => item.txHash !== txHashStr);
|
|
464
|
+
|
|
465
|
+
if (highestSurvivingIndex !== undefined) {
|
|
466
|
+
const newFinalized = Math.max(lastFinalized ?? 0, highestSurvivingIndex);
|
|
467
|
+
this.#writeLastFinalizedIndex(jobId, secret, newFinalized);
|
|
468
|
+
|
|
469
|
+
// Prune pending indexes that are now <= the finalized index.
|
|
470
|
+
currentPending = currentPending.filter(item => item.highestIndex > newFinalized);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
this.#writePendingIndexes(jobId, secret, currentPending);
|
|
474
|
+
}
|
|
428
475
|
}
|
|
429
476
|
}
|
package/src/tagging/index.ts
CHANGED
|
@@ -16,4 +16,4 @@ export { getAllPrivateLogsByTags, getAllPublicLogsByTagsFromContract } from './g
|
|
|
16
16
|
|
|
17
17
|
// Re-export tagging-related types from stdlib
|
|
18
18
|
export { ExtendedDirectionalAppTaggingSecret, Tag, SiloedTag } from '@aztec/stdlib/logs';
|
|
19
|
-
export { type PreTag } from '@aztec/stdlib/logs';
|
|
19
|
+
export { type PreTag, type TaggingIndexRange } from '@aztec/stdlib/logs';
|
|
@@ -62,11 +62,29 @@ export async function syncSenderTaggingIndexes(
|
|
|
62
62
|
break;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
const { txHashesToFinalize, txHashesToDrop } = await getStatusChangeOfPending(
|
|
65
|
+
const { txHashesToFinalize, txHashesToDrop, txHashesWithExecutionReverted } = await getStatusChangeOfPending(
|
|
66
|
+
pendingTxHashes,
|
|
67
|
+
aztecNode,
|
|
68
|
+
);
|
|
66
69
|
|
|
67
70
|
await taggingStore.dropPendingIndexes(txHashesToDrop, jobId);
|
|
68
71
|
await taggingStore.finalizePendingIndexes(txHashesToFinalize, jobId);
|
|
69
72
|
|
|
73
|
+
if (txHashesWithExecutionReverted.length > 0) {
|
|
74
|
+
const indexedTxEffects = await Promise.all(
|
|
75
|
+
txHashesWithExecutionReverted.map(txHash => aztecNode.getTxEffect(txHash)),
|
|
76
|
+
);
|
|
77
|
+
for (const indexedTxEffect of indexedTxEffects) {
|
|
78
|
+
if (indexedTxEffect === undefined) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
'TxEffect not found for execution-reverted tx. This is either a bug or a reorg has occurred.',
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await taggingStore.finalizePendingIndexesOfAPartiallyRevertedTx(indexedTxEffect.data, jobId);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
70
88
|
// We check if the finalized index has been updated.
|
|
71
89
|
newFinalizedIndex = await taggingStore.getLastFinalizedIndex(secret, jobId);
|
|
72
90
|
if (previousFinalizedIndex !== newFinalizedIndex) {
|
|
@@ -2,35 +2,50 @@ import type { AztecNode } from '@aztec/stdlib/interfaces/server';
|
|
|
2
2
|
import { TxHash, TxStatus } from '@aztec/stdlib/tx';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Based on receipts obtained from `aztecNode` returns which pending transactions changed their status to finalized
|
|
6
|
-
* dropped.
|
|
5
|
+
* Based on receipts obtained from `aztecNode` returns which pending transactions changed their status to finalized,
|
|
6
|
+
* dropped, or execution-reverted (but mined).
|
|
7
7
|
*/
|
|
8
8
|
export async function getStatusChangeOfPending(
|
|
9
9
|
pending: TxHash[],
|
|
10
10
|
aztecNode: AztecNode,
|
|
11
|
-
): Promise<{
|
|
11
|
+
): Promise<{
|
|
12
|
+
txHashesToFinalize: TxHash[];
|
|
13
|
+
txHashesToDrop: TxHash[];
|
|
14
|
+
txHashesWithExecutionReverted: TxHash[];
|
|
15
|
+
}> {
|
|
12
16
|
// Get receipts for all pending tx hashes.
|
|
13
17
|
const receipts = await Promise.all(pending.map(pendingTxHash => aztecNode.getTxReceipt(pendingTxHash)));
|
|
14
18
|
|
|
15
19
|
const txHashesToFinalize: TxHash[] = [];
|
|
16
20
|
const txHashesToDrop: TxHash[] = [];
|
|
21
|
+
const txHashesWithExecutionReverted: TxHash[] = [];
|
|
17
22
|
|
|
18
23
|
for (let i = 0; i < receipts.length; i++) {
|
|
19
24
|
const receipt = receipts[i];
|
|
20
25
|
const txHash = pending[i];
|
|
21
26
|
|
|
22
|
-
if (receipt.status === TxStatus.FINALIZED
|
|
23
|
-
// Tx has been included in a block and the corresponding block is finalized
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if (receipt.status === TxStatus.FINALIZED) {
|
|
28
|
+
// Tx has been included in a block and the corresponding block is finalized
|
|
29
|
+
if (receipt.hasExecutionSucceeded()) {
|
|
30
|
+
// No part of execution reverted - we just finalize all the indexes.
|
|
31
|
+
txHashesToFinalize.push(txHash);
|
|
32
|
+
} else if (receipt.hasExecutionReverted()) {
|
|
33
|
+
// Tx was mined but execution reverted (app logic, teardown, or both). Some logs from the non-revertible
|
|
34
|
+
// phase may still be onchain. We check which tags made it onchain and finalize those; drop the rest.
|
|
35
|
+
txHashesWithExecutionReverted.push(txHash);
|
|
36
|
+
} else {
|
|
37
|
+
// Defensive check - this branch should never be triggered
|
|
38
|
+
throw new Error(
|
|
39
|
+
'Both hasExecutionSucceeded and hasExecutionReverted on the receipt returned false. This should never happen and it implies a bug. Please open an issue.',
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
} else if (receipt.isDropped()) {
|
|
43
|
+
// Tx was dropped from the mempool --> we drop the corresponding pending indexes.
|
|
29
44
|
txHashesToDrop.push(txHash);
|
|
30
45
|
} else {
|
|
31
46
|
// Tx is still pending, not yet finalized, or was mined successfully but not yet finalized --> we don't do anything.
|
|
32
47
|
}
|
|
33
48
|
}
|
|
34
49
|
|
|
35
|
-
return { txHashesToFinalize, txHashesToDrop };
|
|
50
|
+
return { txHashesToFinalize, txHashesToDrop, txHashesWithExecutionReverted };
|
|
36
51
|
}
|
|
@@ -16,6 +16,7 @@ import { getAllPrivateLogsByTags } from '../../get_all_logs_by_tags.js';
|
|
|
16
16
|
* @param end - The ending index (exclusive) of the window to process.
|
|
17
17
|
* @param aztecNode - The Aztec node instance to query for logs.
|
|
18
18
|
* @param taggingStore - The data provider to store pending indexes.
|
|
19
|
+
* @param anchorBlockHash - Hash of a block to use as reference block when querying node.
|
|
19
20
|
* @param jobId - Job identifier, used to keep writes in-memory until they can be persisted in a data integrity
|
|
20
21
|
* preserving way.
|
|
21
22
|
*/
|
|
@@ -34,12 +35,13 @@ export async function loadAndStoreNewTaggingIndexes(
|
|
|
34
35
|
);
|
|
35
36
|
|
|
36
37
|
const txsForTags = await getTxsContainingTags(siloedTagsForWindow, aztecNode, anchorBlockHash);
|
|
37
|
-
const
|
|
38
|
+
const txIndexesMap = getTxIndexesMap(txsForTags, start, siloedTagsForWindow.length);
|
|
38
39
|
|
|
39
|
-
// Now we iterate over the map,
|
|
40
|
-
for (const [txHashStr,
|
|
40
|
+
// Now we iterate over the map, construct the tagging index ranges and store them in the db.
|
|
41
|
+
for (const [txHashStr, indexes] of txIndexesMap.entries()) {
|
|
41
42
|
const txHash = TxHash.fromString(txHashStr);
|
|
42
|
-
|
|
43
|
+
const ranges = [{ extendedSecret, lowestIndex: Math.min(...indexes), highestIndex: Math.max(...indexes) }];
|
|
44
|
+
await taggingStore.storePendingIndexes(ranges, txHash, jobId);
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
|
|
@@ -56,20 +58,28 @@ async function getTxsContainingTags(
|
|
|
56
58
|
return allLogs.map(logs => logs.map(log => log.txHash));
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
// Returns a map of txHash to
|
|
60
|
-
function
|
|
61
|
+
// Returns a map of txHash to all indexes for that txHash.
|
|
62
|
+
function getTxIndexesMap(txHashesForTags: TxHash[][], start: number, count: number): Map<string, number[]> {
|
|
61
63
|
if (txHashesForTags.length !== count) {
|
|
62
64
|
throw new Error(`Number of tx hashes arrays does not match number of tags. ${txHashesForTags.length} !== ${count}`);
|
|
63
65
|
}
|
|
64
66
|
|
|
65
|
-
const
|
|
67
|
+
const indexesMap = new Map<string, number[]>();
|
|
68
|
+
// Iterate over indexes
|
|
66
69
|
for (let i = 0; i < txHashesForTags.length; i++) {
|
|
67
70
|
const taggingIndex = start + i;
|
|
68
71
|
const txHashesForTag = txHashesForTags[i];
|
|
72
|
+
// iterate over tx hashes that used that index (tag)
|
|
69
73
|
for (const txHash of txHashesForTag) {
|
|
70
74
|
const key = txHash.toString();
|
|
71
|
-
|
|
75
|
+
const existing = indexesMap.get(key);
|
|
76
|
+
// Add the index to the tx's indexes
|
|
77
|
+
if (existing) {
|
|
78
|
+
existing.push(taggingIndex);
|
|
79
|
+
} else {
|
|
80
|
+
indexesMap.set(key, [taggingIndex]);
|
|
81
|
+
}
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
|
-
return
|
|
84
|
+
return indexesMap;
|
|
75
85
|
}
|