@aztec/pxe 0.0.1-commit.96bb3f7 → 0.0.1-commit.a072138
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/bin/check_oracle_version.js +1 -1
- package/dest/block_synchronizer/block_synchronizer.d.ts +9 -5
- package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
- package/dest/block_synchronizer/block_synchronizer.js +66 -21
- package/dest/config/index.d.ts +3 -1
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +17 -0
- package/dest/contract_function_simulator/benchmarked_node.d.ts +9 -0
- package/dest/contract_function_simulator/benchmarked_node.d.ts.map +1 -0
- package/dest/contract_function_simulator/benchmarked_node.js +77 -0
- package/dest/contract_function_simulator/contract_function_simulator.d.ts +6 -10
- package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.js +33 -25
- package/dest/contract_function_simulator/execution_note_cache.d.ts +18 -9
- package/dest/contract_function_simulator/execution_note_cache.d.ts.map +1 -1
- package/dest/contract_function_simulator/execution_note_cache.js +45 -28
- package/dest/contract_function_simulator/index.d.ts +2 -2
- package/dest/contract_function_simulator/index.d.ts.map +1 -1
- package/dest/contract_function_simulator/index.js +1 -1
- package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts +1 -1
- package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
- package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +1 -1
- package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
- package/dest/contract_function_simulator/noir-structs/utility_context.d.ts +4 -10
- package/dest/contract_function_simulator/noir-structs/utility_context.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/utility_context.js +7 -18
- package/dest/contract_function_simulator/oracle/interfaces.d.ts +15 -11
- package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/message_load_oracle_inputs.d.ts +3 -1
- package/dest/contract_function_simulator/oracle/message_load_oracle_inputs.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/note_packing_utils.d.ts +6 -6
- package/dest/contract_function_simulator/oracle/note_packing_utils.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/note_packing_utils.js +8 -8
- package/dest/contract_function_simulator/oracle/oracle.d.ts +10 -8
- package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/oracle.js +44 -33
- package/dest/contract_function_simulator/oracle/private_execution.d.ts +2 -26
- package/dest/contract_function_simulator/oracle/private_execution.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution.js +0 -35
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +11 -3
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.js +22 -10
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +33 -20
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +78 -76
- package/dest/contract_sync/index.d.ts +23 -0
- package/dest/contract_sync/index.d.ts.map +1 -0
- package/dest/contract_sync/index.js +54 -0
- package/dest/debug/pxe_debug_utils.d.ts +16 -6
- package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
- package/dest/debug/pxe_debug_utils.js +20 -10
- package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
- package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
- package/dest/entrypoints/client/bundle/utils.js +12 -6
- package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
- package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
- package/dest/entrypoints/client/lazy/utils.js +13 -7
- package/dest/entrypoints/pxe_creation_options.d.ts +3 -2
- package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
- package/dest/entrypoints/server/index.d.ts +3 -1
- package/dest/entrypoints/server/index.d.ts.map +1 -1
- package/dest/entrypoints/server/index.js +2 -0
- package/dest/entrypoints/server/utils.d.ts +1 -1
- package/dest/entrypoints/server/utils.d.ts.map +1 -1
- package/dest/entrypoints/server/utils.js +19 -8
- package/dest/events/event_service.d.ts +6 -6
- package/dest/events/event_service.d.ts.map +1 -1
- package/dest/events/event_service.js +19 -22
- package/dest/events/private_event_filter_validator.d.ts +5 -5
- package/dest/events/private_event_filter_validator.d.ts.map +1 -1
- package/dest/events/private_event_filter_validator.js +5 -6
- package/dest/job_coordinator/job_coordinator.d.ts +3 -2
- package/dest/job_coordinator/job_coordinator.d.ts.map +1 -1
- package/dest/job_coordinator/job_coordinator.js +3 -2
- package/dest/logs/log_service.d.ts +6 -4
- package/dest/logs/log_service.d.ts.map +1 -1
- package/dest/logs/log_service.js +21 -16
- package/dest/notes/note_service.d.ts +7 -7
- package/dest/notes/note_service.d.ts.map +1 -1
- package/dest/notes/note_service.js +31 -36
- package/dest/oracle_version.d.ts +3 -3
- package/dest/oracle_version.d.ts.map +1 -1
- package/dest/oracle_version.js +4 -3
- package/dest/private_kernel/hints/index.d.ts +2 -2
- package/dest/private_kernel/hints/index.d.ts.map +1 -1
- package/dest/private_kernel/hints/index.js +1 -1
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +28 -0
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -0
- package/dest/private_kernel/hints/{build_private_kernel_reset_private_inputs.js → private_kernel_reset_private_inputs_builder.js} +12 -6
- package/dest/private_kernel/private_kernel_execution_prover.d.ts +3 -2
- package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
- package/dest/private_kernel/private_kernel_execution_prover.js +3 -3
- package/dest/private_kernel/private_kernel_oracle.d.ts +23 -28
- package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
- package/dest/private_kernel/private_kernel_oracle.js +90 -2
- package/dest/pxe.d.ts +7 -36
- package/dest/pxe.d.ts.map +1 -1
- package/dest/pxe.js +43 -83
- package/dest/storage/address_store/address_store.d.ts +1 -1
- package/dest/storage/address_store/address_store.d.ts.map +1 -1
- package/dest/storage/address_store/address_store.js +12 -11
- package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
- package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
- package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
- package/dest/storage/capsule_store/capsule_store.d.ts +24 -9
- package/dest/storage/capsule_store/capsule_store.d.ts.map +1 -1
- package/dest/storage/capsule_store/capsule_store.js +130 -23
- package/dest/storage/contract_store/contract_store.d.ts +1 -2
- package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
- package/dest/storage/contract_store/contract_store.js +22 -25
- package/dest/storage/metadata.d.ts +1 -1
- package/dest/storage/metadata.js +1 -1
- package/dest/storage/note_store/note_store.d.ts +51 -52
- package/dest/storage/note_store/note_store.d.ts.map +1 -1
- package/dest/storage/note_store/note_store.js +282 -263
- package/dest/storage/note_store/stored_note.d.ts +16 -0
- package/dest/storage/note_store/stored_note.d.ts.map +1 -0
- package/dest/storage/note_store/stored_note.js +43 -0
- package/dest/storage/private_event_store/private_event_store.d.ts +43 -8
- package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
- package/dest/storage/private_event_store/private_event_store.js +226 -111
- package/dest/storage/private_event_store/stored_private_event.d.ts +23 -0
- package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -0
- package/dest/storage/private_event_store/stored_private_event.js +56 -0
- package/dest/storage/tagging_store/recipient_tagging_store.d.ts +15 -8
- package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/recipient_tagging_store.js +88 -19
- package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
- package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
- package/dest/storage/tagging_store/sender_tagging_store.d.ts +19 -9
- package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.js +252 -100
- package/dest/tagging/get_all_logs_by_tags.d.ts +24 -0
- package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -0
- package/dest/tagging/get_all_logs_by_tags.js +46 -0
- package/dest/tagging/index.d.ts +2 -1
- package/dest/tagging/index.d.ts.map +1 -1
- package/dest/tagging/index.js +1 -0
- package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +3 -2
- package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
- package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +6 -6
- package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +3 -2
- package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
- package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +5 -2
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +3 -2
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +7 -7
- package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts +1 -1
- 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 +5 -8
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +5 -2
- 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 +10 -5
- package/package.json +18 -18
- package/src/bin/check_oracle_version.ts +1 -0
- package/src/block_synchronizer/block_synchronizer.ts +85 -33
- package/src/config/index.ts +14 -0
- package/src/contract_function_simulator/benchmarked_node.ts +103 -0
- package/src/contract_function_simulator/contract_function_simulator.ts +34 -28
- package/src/contract_function_simulator/execution_note_cache.ts +44 -25
- package/src/contract_function_simulator/index.ts +1 -1
- package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
- package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
- package/src/contract_function_simulator/noir-structs/utility_context.ts +6 -25
- package/src/contract_function_simulator/oracle/interfaces.ts +20 -10
- package/src/contract_function_simulator/oracle/note_packing_utils.ts +10 -10
- package/src/contract_function_simulator/oracle/oracle.ts +65 -40
- package/src/contract_function_simulator/oracle/private_execution.ts +1 -67
- package/src/contract_function_simulator/oracle/private_execution_oracle.ts +35 -12
- package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +100 -82
- package/src/contract_sync/index.ts +98 -0
- package/src/debug/pxe_debug_utils.ts +26 -11
- package/src/entrypoints/client/bundle/utils.ts +7 -14
- package/src/entrypoints/client/lazy/utils.ts +8 -14
- package/src/entrypoints/pxe_creation_options.ts +2 -1
- package/src/entrypoints/server/index.ts +2 -0
- package/src/entrypoints/server/utils.ts +15 -19
- package/src/events/event_service.ts +17 -25
- package/src/events/private_event_filter_validator.ts +3 -5
- package/src/job_coordinator/job_coordinator.ts +4 -3
- package/src/logs/log_service.ts +32 -14
- package/src/notes/note_service.ts +38 -43
- package/src/oracle_version.ts +4 -3
- package/src/private_kernel/hints/index.ts +1 -1
- package/src/private_kernel/hints/{build_private_kernel_reset_private_inputs.ts → private_kernel_reset_private_inputs_builder.ts} +32 -20
- package/src/private_kernel/private_kernel_execution_prover.ts +7 -4
- package/src/private_kernel/private_kernel_oracle.ts +116 -37
- package/src/pxe.ts +77 -118
- package/src/storage/address_store/address_store.ts +15 -15
- package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
- package/src/storage/capsule_store/capsule_store.ts +159 -23
- package/src/storage/contract_store/contract_store.ts +22 -31
- package/src/storage/metadata.ts +1 -1
- package/src/storage/note_store/note_store.ts +317 -318
- package/src/storage/note_store/stored_note.ts +48 -0
- package/src/storage/private_event_store/private_event_store.ts +292 -124
- package/src/storage/private_event_store/stored_private_event.ts +73 -0
- package/src/storage/tagging_store/recipient_tagging_store.ts +106 -20
- package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
- package/src/storage/tagging_store/sender_tagging_store.ts +293 -108
- package/src/tagging/get_all_logs_by_tags.ts +68 -0
- package/src/tagging/index.ts +1 -0
- package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +16 -5
- package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +7 -1
- package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +9 -6
- package/src/tagging/sender_sync/utils/get_status_change_of_pending.ts +5 -17
- package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +16 -4
- package/dest/contract_function_simulator/proxied_node.d.ts +0 -9
- package/dest/contract_function_simulator/proxied_node.d.ts.map +0 -1
- package/dest/contract_function_simulator/proxied_node.js +0 -27
- package/dest/private_kernel/hints/build_private_kernel_reset_private_inputs.d.ts +0 -28
- package/dest/private_kernel/hints/build_private_kernel_reset_private_inputs.d.ts.map +0 -1
- package/dest/private_kernel/private_kernel_oracle_impl.d.ts +0 -46
- package/dest/private_kernel/private_kernel_oracle_impl.d.ts.map +0 -1
- package/dest/private_kernel/private_kernel_oracle_impl.js +0 -85
- package/dest/public_storage/public_storage_service.d.ts +0 -24
- package/dest/public_storage/public_storage_service.d.ts.map +0 -1
- package/dest/public_storage/public_storage_service.js +0 -26
- package/dest/tree_membership/tree_membership_service.d.ts +0 -52
- package/dest/tree_membership/tree_membership_service.d.ts.map +0 -1
- package/dest/tree_membership/tree_membership_service.js +0 -84
- package/src/contract_function_simulator/proxied_node.ts +0 -33
- package/src/private_kernel/private_kernel_oracle_impl.ts +0 -127
- package/src/public_storage/public_storage_service.ts +0 -33
- package/src/tree_membership/tree_membership_service.ts +0 -112
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"note_store.d.ts","sourceRoot":"","sources":["../../../src/storage/note_store/note_store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"note_store.d.ts","sourceRoot":"","sources":["../../../src/storage/note_store/note_store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,2BAA2B,CAAC;AACpD,OAAO,KAAK,EAAE,iBAAiB,EAAqC,MAAM,iBAAiB,CAAC;AAC5F,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAc,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0CAA0C,CAAC;AAG5E;;;;;IAKI;AACJ,qBAAa,SAAU,YAAW,WAAW;;IAC3C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAU;IA+BpC,YAAY,KAAK,EAAE,iBAAiB,EAQnC;IAED;;;;;;;;;OASG;IACI,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAarF;IAcD;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CA6F/D;IAED;;;;;;;;;;;;;;OAcG;IACH,eAAe,CAAC,UAAU,EAAE,WAAW,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAqChF;IAED;;;;;;;;;;;OAWG;IACU,QAAQ,CAAC,WAAW,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAMpF;IA6ED;;;;;;;;;OASG;IACG,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAUzC;IAED,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG1C;CAkCF"}
|
|
@@ -1,322 +1,341 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { toArray } from '@aztec/foundation/iterable';
|
|
3
|
-
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
1
|
+
import { Semaphore } from '@aztec/foundation/queue';
|
|
4
2
|
import { NoteStatus } from '@aztec/stdlib/note';
|
|
5
|
-
import {
|
|
3
|
+
import { StoredNote } from './stored_note.js';
|
|
6
4
|
/**
|
|
7
5
|
* NoteStore manages the storage and retrieval of notes.
|
|
8
6
|
*
|
|
9
|
-
* Notes can be active or nullified. This class processes new notes, nullifications,
|
|
10
|
-
*
|
|
7
|
+
* Notes can be active or nullified. This class processes new notes, nullifications, and performs rollback handling in
|
|
8
|
+
* the case of a reorg.
|
|
11
9
|
**/ export class NoteStore {
|
|
10
|
+
storeName = 'note';
|
|
12
11
|
#store;
|
|
12
|
+
// Note that we use the siloedNullifier as the note id in the store as it's guaranteed to be unique.
|
|
13
|
+
// Main storage for notes. Avoid performing full scans on it as it contains all notes PXE knows, use
|
|
14
|
+
// #nullifiersByContractAddress or #nullifiersByNullificationBlockNumber to find relevant note nullifiers that can be
|
|
15
|
+
// used to read into this map instead.
|
|
16
|
+
// nullifier => StoredNote (serialized)
|
|
13
17
|
#notes;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
#
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
// Indexes which notes (via their nullifiers) belong to a contract. Used in `getNotes` to reduce the amount of notes
|
|
19
|
+
// checked.
|
|
20
|
+
// contract address => nullifier
|
|
21
|
+
#nullifiersByContractAddress;
|
|
22
|
+
// Groups note nullifiers by the block number they were added to the nullifier tree. Used in `rollback` to handle
|
|
23
|
+
// re-orgs.
|
|
24
|
+
// block number => nullifier (block number in which nullifier is included)
|
|
25
|
+
#nullifiersByNullificationBlockNumber;
|
|
26
|
+
// In-memory changes performed during a not-yet committed job. When `commit` is called with said job's id, these
|
|
27
|
+
// changes are persisted in the DB maps specified above and cleared.
|
|
28
|
+
// jobId => nullifier => StoredNote (serialized)
|
|
29
|
+
#notesForJob;
|
|
30
|
+
// Per job locks to prevent multiple concurrent writes to affect each other.
|
|
31
|
+
// jobId => lock
|
|
32
|
+
#jobLocks;
|
|
25
33
|
constructor(store){
|
|
26
34
|
this.#store = store;
|
|
27
35
|
this.#notes = store.openMap('notes');
|
|
28
|
-
this.#
|
|
29
|
-
this.#
|
|
30
|
-
this.#
|
|
31
|
-
this.#
|
|
32
|
-
this.#nullifiedNotesByContract = store.openMultiMap('nullified_notes_by_contract');
|
|
33
|
-
this.#nullifiedNotesByStorageSlot = store.openMultiMap('nullified_notes_by_storage_slot');
|
|
34
|
-
this.#nullifiedNotesByNullifier = store.openMap('nullified_notes_by_nullifier');
|
|
35
|
-
this.#scopes = store.openMap('scopes');
|
|
36
|
-
this.#notesToScope = store.openMultiMap('notes_to_scope');
|
|
37
|
-
this.#notesByContractAndScope = new Map();
|
|
38
|
-
this.#notesByStorageSlotAndScope = new Map();
|
|
36
|
+
this.#nullifiersByContractAddress = store.openMultiMap('note_nullifiers_by_contract');
|
|
37
|
+
this.#nullifiersByNullificationBlockNumber = store.openMultiMap('note_block_number_to_nullifier');
|
|
38
|
+
this.#jobLocks = new Map();
|
|
39
|
+
this.#notesForJob = new Map();
|
|
39
40
|
}
|
|
40
41
|
/**
|
|
41
|
-
*
|
|
42
|
+
* Adds multiple notes to the notes store under the specified scope.
|
|
42
43
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @param store - The key-value store to use for persistence
|
|
47
|
-
* @returns Promise resolving to a fully initialized NoteStore instance
|
|
48
|
-
*/ static async create(store) {
|
|
49
|
-
const pxeDB = new NoteStore(store);
|
|
50
|
-
for await (const scope of pxeDB.#scopes.keysAsync()){
|
|
51
|
-
pxeDB.#notesByContractAndScope.set(scope, store.openMultiMap(`${scope}:notes_by_contract`));
|
|
52
|
-
pxeDB.#notesByStorageSlotAndScope.set(scope, store.openMultiMap(`${scope}:notes_by_storage_slot`));
|
|
53
|
-
}
|
|
54
|
-
return pxeDB;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Adds a new scope to the note data provider.
|
|
58
|
-
*
|
|
59
|
-
* Scopes provide privacy isolation by creating separate indexes for each user.
|
|
60
|
-
* Each scope gets its own set of indexes for efficient note retrieval by various criteria.
|
|
61
|
-
*
|
|
62
|
-
* @param scope - The AztecAddress representing the scope/user to add
|
|
63
|
-
* @returns Promise resolving to true if scope was added, false if it already existed
|
|
64
|
-
*/ async addScope(scope) {
|
|
65
|
-
const scopeString = scope.toString();
|
|
66
|
-
if (await this.#scopes.hasAsync(scopeString)) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
await this.#scopes.set(scopeString, true);
|
|
70
|
-
this.#notesByContractAndScope.set(scopeString, this.#store.openMultiMap(`${scopeString}:notes_by_contract`));
|
|
71
|
-
this.#notesByStorageSlotAndScope.set(scopeString, this.#store.openMultiMap(`${scopeString}:notes_by_storage_slot`));
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Adds multiple notes to the data provider under the specified scope.
|
|
76
|
-
*
|
|
77
|
-
* Notes are stored using their index from the notes hash tree as the key, which provides
|
|
78
|
-
* uniqueness and maintains creation order. Each note is indexed by multiple criteria
|
|
79
|
-
* for efficient retrieval.
|
|
44
|
+
* Notes are stored using their siloedNullifier as the key, which provides uniqueness. Each note is indexed by
|
|
45
|
+
* multiple criteria for efficient retrieval.
|
|
80
46
|
*
|
|
81
47
|
* @param notes - Notes to store
|
|
82
48
|
* @param scope - The scope (user/account) under which to store the notes
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
await this.#notes.set(noteIndex, dao.toBuffer());
|
|
91
|
-
await this.#notesToScope.set(noteIndex, scope.toString());
|
|
92
|
-
await this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
|
|
93
|
-
await this.#notesByContractAndScope.get(scope.toString()).set(dao.contractAddress.toString(), noteIndex);
|
|
94
|
-
await this.#notesByStorageSlotAndScope.get(scope.toString()).set(dao.storageSlot.toString(), noteIndex);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
49
|
+
* @param jobId - The job context for staged writes
|
|
50
|
+
*/ addNotes(notes, scope, jobId) {
|
|
51
|
+
return this.#withJobLock(jobId, ()=>this.#store.transactionAsync(()=>Promise.all(notes.map(async (note)=>{
|
|
52
|
+
const noteForJob = await this.#readNote(note.siloedNullifier.toString(), jobId) ?? new StoredNote(note, new Set());
|
|
53
|
+
noteForJob.addScope(scope.toString());
|
|
54
|
+
this.#writeNote(noteForJob, jobId);
|
|
55
|
+
}))));
|
|
97
56
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
*
|
|
105
|
-
* @param blockNumber - The new chain tip after a reorg
|
|
106
|
-
* @param synchedBlockNumber - The block number up to which PXE managed to sync before the reorg happened.
|
|
107
|
-
*/ async rollbackNotesAndNullifiers(blockNumber, synchedBlockNumber) {
|
|
108
|
-
await this.#rewindNullifiersAfterBlock(blockNumber, synchedBlockNumber);
|
|
109
|
-
await this.#deleteActiveNotesAfterBlock(blockNumber);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Deletes (removes) all active notes created after the specified block number.
|
|
113
|
-
*
|
|
114
|
-
* Permanently delete notes from the active notes store, e.g. during a reorg.
|
|
115
|
-
* Note: This only affects #notes (active notes), not #nullifiedNotes.
|
|
116
|
-
*
|
|
117
|
-
* @param blockNumber - Notes created after this block number will be deleted
|
|
118
|
-
*/ #deleteActiveNotesAfterBlock(blockNumber) {
|
|
119
|
-
return this.#store.transactionAsync(async ()=>{
|
|
120
|
-
const notes = await toArray(this.#notes.valuesAsync());
|
|
121
|
-
for (const note of notes){
|
|
122
|
-
const noteDao = NoteDao.fromBuffer(note);
|
|
123
|
-
if (noteDao.l2BlockNumber > blockNumber) {
|
|
124
|
-
const noteIndex = toBufferBE(noteDao.index, 32).toString('hex');
|
|
125
|
-
await this.#notes.delete(noteIndex);
|
|
126
|
-
await this.#notesToScope.delete(noteIndex);
|
|
127
|
-
await this.#nullifierToNoteId.delete(noteDao.siloedNullifier.toString());
|
|
128
|
-
const scopes = await toArray(this.#scopes.keysAsync());
|
|
129
|
-
for (const scope of scopes){
|
|
130
|
-
await this.#notesByContractAndScope.get(scope).deleteValue(noteDao.contractAddress.toString(), noteIndex);
|
|
131
|
-
await this.#notesByStorageSlotAndScope.get(scope).deleteValue(noteDao.storageSlot.toString(), noteIndex);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
});
|
|
57
|
+
async #readNote(nullifier, jobId) {
|
|
58
|
+
// Always issue DB read to keep IndexedDB transaction alive (they auto-commit when a new micro-task starts and there
|
|
59
|
+
// are no pending read requests). The staged value still takes precedence if it exists.
|
|
60
|
+
const noteBuffer = await this.#notes.getAsync(nullifier);
|
|
61
|
+
const noteForJob = this.#getNotesForJob(jobId).get(nullifier);
|
|
62
|
+
return noteForJob ?? (noteBuffer ? StoredNote.fromBuffer(noteBuffer) : undefined);
|
|
136
63
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
*
|
|
140
|
-
* This operation "unnullifies" notes, rolling back nullifications that occurred
|
|
141
|
-
* in orphaned blocks, e.g. during a reorg. The notes are restored to the
|
|
142
|
-
* active notes store and removed from the nullified store.
|
|
143
|
-
*
|
|
144
|
-
* @param blockNumber - Revert nullifications that occurred after this block
|
|
145
|
-
* @param synchedBlockNumber - Upper bound for the block range to process
|
|
146
|
-
*/ async #rewindNullifiersAfterBlock(blockNumber, synchedBlockNumber) {
|
|
147
|
-
await this.#store.transactionAsync(async ()=>{
|
|
148
|
-
const nullifiersToUndo = [];
|
|
149
|
-
const currentBlockNumber = blockNumber + 1;
|
|
150
|
-
for(let i = currentBlockNumber; i <= synchedBlockNumber; i++){
|
|
151
|
-
nullifiersToUndo.push(...await toArray(this.#nullifiersByBlockNumber.getValuesAsync(i)));
|
|
152
|
-
}
|
|
153
|
-
const notesIndexesToReinsert = await Promise.all(nullifiersToUndo.map((nullifier)=>this.#nullifiedNotesByNullifier.getAsync(nullifier)));
|
|
154
|
-
const notNullNoteIndexes = notesIndexesToReinsert.filter((noteIndex)=>noteIndex != undefined);
|
|
155
|
-
const nullifiedNoteBuffers = await Promise.all(notNullNoteIndexes.map((noteIndex)=>this.#nullifiedNotes.getAsync(noteIndex)));
|
|
156
|
-
const noteDaos = nullifiedNoteBuffers.filter((buffer)=>buffer != undefined).map((buffer)=>NoteDao.fromBuffer(buffer));
|
|
157
|
-
for (const dao of noteDaos){
|
|
158
|
-
const noteIndex = toBufferBE(dao.index, 32).toString('hex');
|
|
159
|
-
await this.#notes.set(noteIndex, dao.toBuffer());
|
|
160
|
-
await this.#nullifierToNoteId.set(dao.siloedNullifier.toString(), noteIndex);
|
|
161
|
-
const scopes = await toArray(this.#nullifiedNotesToScope.getValuesAsync(noteIndex));
|
|
162
|
-
if (scopes.length === 0) {
|
|
163
|
-
// We should never run into this error because notes always have a scope assigned to them - either on initial
|
|
164
|
-
// insertion via `addNotes` or when removing their nullifiers.
|
|
165
|
-
throw new Error(`No scopes found for nullified note with index ${noteIndex}`);
|
|
166
|
-
}
|
|
167
|
-
for (const scope of scopes){
|
|
168
|
-
await this.#notesByContractAndScope.get(scope.toString()).set(dao.contractAddress.toString(), noteIndex);
|
|
169
|
-
await this.#notesByStorageSlotAndScope.get(scope.toString()).set(dao.storageSlot.toString(), noteIndex);
|
|
170
|
-
await this.#notesToScope.set(noteIndex, scope);
|
|
171
|
-
}
|
|
172
|
-
await this.#nullifiedNotes.delete(noteIndex);
|
|
173
|
-
await this.#nullifiedNotesToScope.delete(noteIndex);
|
|
174
|
-
await this.#nullifiersByBlockNumber.deleteValue(dao.l2BlockNumber, dao.siloedNullifier.toString());
|
|
175
|
-
await this.#nullifiedNotesByContract.deleteValue(dao.contractAddress.toString(), noteIndex);
|
|
176
|
-
await this.#nullifiedNotesByStorageSlot.deleteValue(dao.storageSlot.toString(), noteIndex);
|
|
177
|
-
await this.#nullifiedNotesByNullifier.delete(dao.siloedNullifier.toString());
|
|
178
|
-
}
|
|
179
|
-
});
|
|
64
|
+
#writeNote(note, jobId) {
|
|
65
|
+
this.#getNotesForJob(jobId).set(note.noteDao.siloedNullifier.toString(), note);
|
|
180
66
|
}
|
|
181
67
|
/**
|
|
182
68
|
* Retrieves notes based on the provided filter criteria.
|
|
183
69
|
*
|
|
184
|
-
* This method queries both active and optionally nullified notes based on the filter
|
|
185
|
-
* parameters.
|
|
70
|
+
* This method queries both active and optionally nullified notes based on the filter parameters.
|
|
186
71
|
*
|
|
187
|
-
* @param filter - Filter criteria including contractAddress (required), and optional
|
|
188
|
-
*
|
|
72
|
+
* @param filter - Filter criteria including contractAddress (required), and optional owner,
|
|
73
|
+
* storageSlot, status, scopes, and siloedNullifier.
|
|
74
|
+
* @params jobId - the job context to read from.
|
|
189
75
|
* @returns Filtered and deduplicated notes (a note might be present in multiple scopes - we ensure it is only
|
|
190
76
|
* returned once if this is the case)
|
|
191
77
|
* @throws If filtering by an empty scopes array. Scopes have to be set to undefined or to a non-empty array.
|
|
192
|
-
*/
|
|
193
|
-
filter.status = filter.status ?? NoteStatus.ACTIVE;
|
|
194
|
-
// throw early if scopes is an empty array
|
|
78
|
+
*/ getNotes(filter, jobId) {
|
|
195
79
|
if (filter.scopes !== undefined && filter.scopes.length === 0) {
|
|
196
|
-
|
|
80
|
+
return Promise.reject(new Error('Trying to get notes with an empty scopes array'));
|
|
197
81
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
82
|
+
return this.#store.transactionAsync(async ()=>{
|
|
83
|
+
const targetStatus = filter.status ?? NoteStatus.ACTIVE;
|
|
84
|
+
// The code below might read a bit unnatural, the reason is that we need to be careful in how we use `await` inside
|
|
85
|
+
// `transactionAsync`, otherwise browsers might choose to auto-commit the IndexedDB transaction forcing us to
|
|
86
|
+
// explicitly handle that condition. The rule we need to honor is: do not await unless you generate a database
|
|
87
|
+
// read or write or you're done using the DB for the remainder of the transaction. The following sequence is
|
|
88
|
+
// unsafe in IndexedDB:
|
|
89
|
+
//
|
|
90
|
+
// 1. start transactionAsync()
|
|
91
|
+
// 2. await readDb() <-- OK, transaction alive because we issued DB ops
|
|
92
|
+
// 3. run a bunch of computations (no await involved) <-- OK, tx alive because we are in the same microtask
|
|
93
|
+
// 4. await doSthNotInDb() <-- no DB ops issued in this task, browser's free to decide to commit the tx
|
|
94
|
+
// 5. await readDb() <-- BOOM, TransactionInactiveError
|
|
95
|
+
//
|
|
96
|
+
// Note that the real issue is in step number 5: we try to continue using a transaction that the browser might
|
|
97
|
+
// have already committed.
|
|
98
|
+
//
|
|
99
|
+
// We need to read candidate notes which are either indexed by contract address in the DB (in
|
|
100
|
+
// #nullifiersByContractAddress), or lie in memory for the not yet committed `jobId`.
|
|
101
|
+
// So we collect promises based on both sources without awaiting for them.
|
|
102
|
+
const noteReadPromises = new Map();
|
|
103
|
+
// Awaiting the getValuesAsync iterator is fine because it's reading from the DB
|
|
104
|
+
for await (const nullifier of this.#nullifiersByContractAddress.getValuesAsync(filter.contractAddress.toString())){
|
|
105
|
+
// Each #readNote will perform a DB read
|
|
106
|
+
noteReadPromises.set(nullifier, this.#readNote(nullifier, jobId));
|
|
205
107
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
if (filter.status === NoteStatus.ACTIVE_OR_NULLIFIED) {
|
|
214
|
-
const nullifiedIds = filter.storageSlot ? await toArray(this.#nullifiedNotesByStorageSlot.getValuesAsync(filter.storageSlot.toString())) : await toArray(this.#nullifiedNotesByContract.getValuesAsync(filter.contractAddress.toString()));
|
|
215
|
-
const setOfScopes = new Set(filter.scopes.map((s)=>s.toString()));
|
|
216
|
-
const filteredNullifiedIds = new Set();
|
|
217
|
-
for (const noteId of nullifiedIds){
|
|
218
|
-
const scopeList = await toArray(this.#nullifiedNotesToScope.getValuesAsync(noteId));
|
|
219
|
-
if (scopeList.some((scope)=>setOfScopes.has(scope))) {
|
|
220
|
-
filteredNullifiedIds.add(noteId);
|
|
108
|
+
// Add staged nullifiers from job, no awaits involved, so we are fine
|
|
109
|
+
for (const storedNote of this.#getNotesForJob(jobId).values()){
|
|
110
|
+
if (storedNote.noteDao.contractAddress.equals(filter.contractAddress)) {
|
|
111
|
+
const nullifier = storedNote.noteDao.siloedNullifier.toString();
|
|
112
|
+
if (!noteReadPromises.has(nullifier)) {
|
|
113
|
+
noteReadPromises.set(nullifier, Promise.resolve(storedNote));
|
|
114
|
+
}
|
|
221
115
|
}
|
|
222
116
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (!serializedNote) {
|
|
117
|
+
// By now we have pending DB requests from all the #readNote calls. Await them all together.
|
|
118
|
+
const notes = await Promise.all(noteReadPromises.values());
|
|
119
|
+
// The rest of the function is await-free, and just deals with filtering and sorting our findings.
|
|
120
|
+
const foundNotes = new Map();
|
|
121
|
+
for (const note of notes){
|
|
122
|
+
// Defensive: hitting this case means we're mishandling contract indices or in-memory job data
|
|
123
|
+
if (!note) {
|
|
124
|
+
throw new Error('PXE note database is corrupted.');
|
|
125
|
+
}
|
|
126
|
+
// Apply filters
|
|
127
|
+
if (targetStatus === NoteStatus.ACTIVE && note.isNullified()) {
|
|
235
128
|
continue;
|
|
236
129
|
}
|
|
237
|
-
|
|
238
|
-
if (!note.contractAddress.equals(filter.contractAddress)) {
|
|
130
|
+
if (filter.owner && !note.noteDao.owner.equals(filter.owner)) {
|
|
239
131
|
continue;
|
|
240
132
|
}
|
|
241
|
-
if (filter.
|
|
133
|
+
if (filter.storageSlot && !note.noteDao.storageSlot.equals(filter.storageSlot)) {
|
|
242
134
|
continue;
|
|
243
135
|
}
|
|
244
|
-
if (filter.
|
|
136
|
+
if (filter.siloedNullifier && !note.noteDao.siloedNullifier.equals(filter.siloedNullifier)) {
|
|
245
137
|
continue;
|
|
246
138
|
}
|
|
247
|
-
if (filter.
|
|
139
|
+
if (filter.scopes && note.scopes.intersection(new Set(filter.scopes.map((s)=>s.toString()))).size === 0) {
|
|
248
140
|
continue;
|
|
249
141
|
}
|
|
250
|
-
|
|
142
|
+
foundNotes.set(note.noteDao.siloedNullifier.toString(), note.noteDao);
|
|
251
143
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
144
|
+
// Sort by block number, then by tx index within block, then by note index within tx
|
|
145
|
+
return [
|
|
146
|
+
...foundNotes.values()
|
|
147
|
+
].sort((a, b)=>{
|
|
148
|
+
if (a.l2BlockNumber !== b.l2BlockNumber) {
|
|
149
|
+
return a.l2BlockNumber - b.l2BlockNumber;
|
|
150
|
+
}
|
|
151
|
+
if (a.txIndexInBlock !== b.txIndexInBlock) {
|
|
152
|
+
return a.txIndexInBlock - b.txIndexInBlock;
|
|
153
|
+
}
|
|
154
|
+
return a.noteIndexInTx - b.noteIndexInTx;
|
|
155
|
+
});
|
|
156
|
+
});
|
|
261
157
|
}
|
|
262
158
|
/**
|
|
263
159
|
* Transitions notes from "active" to "nullified" state.
|
|
264
160
|
*
|
|
265
|
-
* This operation processes a batch of nullifiers to mark the corresponding notes
|
|
266
|
-
*
|
|
267
|
-
*
|
|
161
|
+
* This operation processes a batch of nullifiers to mark the corresponding notes as spent/nullified.
|
|
162
|
+
* The operation is atomic - if any nullifier is not found, the entire operation fails and no notes are modified.
|
|
163
|
+
*
|
|
164
|
+
* applyNullifiers is idempotent: the same nullifier can be applied multiple times without error.
|
|
165
|
+
* This relaxes constraints on usage of NoteService#validateAndStoreNote, which can then be run concurrently in a Promise.all
|
|
166
|
+
* context without risking unnecessarily defensive checks failing.
|
|
268
167
|
*
|
|
269
168
|
* @param nullifiers - Array of nullifiers with their block numbers to process
|
|
270
|
-
* @
|
|
271
|
-
* @
|
|
272
|
-
|
|
169
|
+
* @param jobId - The job context for staged writes
|
|
170
|
+
* @returns Array of NoteDao objects that were nullified
|
|
171
|
+
* @throws Error if any nullifier is not found in this notes store
|
|
172
|
+
*/ applyNullifiers(nullifiers, jobId) {
|
|
273
173
|
if (nullifiers.length === 0) {
|
|
274
174
|
return Promise.resolve([]);
|
|
275
175
|
}
|
|
276
|
-
return this.#store.transactionAsync(async ()=>{
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
176
|
+
return this.#withJobLock(jobId, ()=>this.#store.transactionAsync(async ()=>{
|
|
177
|
+
const notesToNullify = await Promise.all(nullifiers.map(async (nullifierInBlock)=>{
|
|
178
|
+
const nullifier = nullifierInBlock.data.toString();
|
|
179
|
+
const storedNote = await this.#readNote(nullifier, jobId);
|
|
180
|
+
if (!storedNote) {
|
|
181
|
+
throw new Error(`Attempted to mark a note as nullified which does not exist in PXE DB`);
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
storedNote,
|
|
185
|
+
blockNumber: nullifierInBlock.l2BlockNumber
|
|
186
|
+
};
|
|
187
|
+
}));
|
|
188
|
+
const notesNullifiedInThisCall = new Map();
|
|
189
|
+
for (const noteToNullify of notesToNullify){
|
|
190
|
+
const note = noteToNullify.storedNote;
|
|
191
|
+
// Skip already nullified notes
|
|
192
|
+
if (note.isNullified()) {
|
|
193
|
+
continue;
|
|
287
194
|
}
|
|
288
|
-
|
|
195
|
+
note.markAsNullified(noteToNullify.blockNumber);
|
|
196
|
+
this.#writeNote(note, jobId);
|
|
197
|
+
notesNullifiedInThisCall.set(note.noteDao.siloedNullifier.toString(), note.noteDao);
|
|
289
198
|
}
|
|
290
|
-
|
|
199
|
+
return [
|
|
200
|
+
...notesNullifiedInThisCall.values()
|
|
201
|
+
];
|
|
202
|
+
}));
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Synchronizes notes and nullifiers to a specific block number.
|
|
206
|
+
*
|
|
207
|
+
* This method ensures that the state of notes and nullifiers is consistent with the specified block number.
|
|
208
|
+
* It restores any notes that were nullified after the given block and deletes any active notes created after that
|
|
209
|
+
* block.
|
|
210
|
+
*
|
|
211
|
+
* IMPORTANT: This method must be called within a transaction to ensure atomicity.
|
|
212
|
+
*
|
|
213
|
+
* @param blockNumber - The new chain tip after a reorg
|
|
214
|
+
* @param synchedBlockNumber - The block number up to which PXE managed to sync before the reorg happened.
|
|
215
|
+
*/ async rollback(blockNumber, synchedBlockNumber) {
|
|
216
|
+
if (this.#notesForJob.size > 0) {
|
|
217
|
+
throw new Error('PXE note store rollback is not allowed while jobs are running');
|
|
218
|
+
}
|
|
219
|
+
await this.#rewindNullifiedNotesAfterBlock(blockNumber, synchedBlockNumber);
|
|
220
|
+
await this.#deleteActiveNotesAfterBlock(blockNumber);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Deletes (removes) all notes created after the specified block number.
|
|
224
|
+
*
|
|
225
|
+
* Permanently delete notes from the notes store, e.g. during a reorg.
|
|
226
|
+
*
|
|
227
|
+
* @param blockNumber - Notes created after this block number will be deleted
|
|
228
|
+
*/ async #deleteActiveNotesAfterBlock(blockNumber) {
|
|
229
|
+
// Collect notes to delete during iteration to keep IndexedDB transaction alive.
|
|
230
|
+
const notesToDelete = [];
|
|
231
|
+
for await (const noteBuffer of this.#notes.valuesAsync()){
|
|
232
|
+
const storedNote = StoredNote.fromBuffer(noteBuffer);
|
|
233
|
+
if (storedNote.noteDao.l2BlockNumber > blockNumber) {
|
|
234
|
+
notesToDelete.push({
|
|
235
|
+
nullifier: storedNote.noteDao.siloedNullifier.toString(),
|
|
236
|
+
contractAddress: storedNote.noteDao.contractAddress.toString()
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
// Delete all collected notes. Each delete is a DB operation that keeps the transaction alive.
|
|
241
|
+
for (const { nullifier, contractAddress } of notesToDelete){
|
|
242
|
+
await this.#notes.delete(nullifier);
|
|
243
|
+
await this.#nullifiersByContractAddress.deleteValue(contractAddress, nullifier);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Rewinds nullifications after a given block number.
|
|
248
|
+
*
|
|
249
|
+
* This operation "un-nullifies" notes, rolling back nullifications that occurred in orphaned blocks, e.g. during a
|
|
250
|
+
* reorg.
|
|
251
|
+
*
|
|
252
|
+
* @param blockNumber - Revert nullifications that occurred after this block
|
|
253
|
+
* @param anchorBlockNumber - Upper bound for the block range to process
|
|
254
|
+
*/ async #rewindNullifiedNotesAfterBlock(blockNumber, anchorBlockNumber) {
|
|
255
|
+
// First pass: collect all nullifiers for all blocks, starting reads during iteration to keep tx alive.
|
|
256
|
+
const nullifiersByBlock = new Map();
|
|
257
|
+
for(let i = blockNumber + 1; i <= anchorBlockNumber; i++){
|
|
258
|
+
const blockNullifiers = [];
|
|
259
|
+
for await (const nullifier of this.#nullifiersByNullificationBlockNumber.getValuesAsync(i)){
|
|
260
|
+
// Start read immediately during iteration to keep IndexedDB transaction alive
|
|
261
|
+
blockNullifiers.push({
|
|
262
|
+
nullifier,
|
|
263
|
+
noteReadPromise: this.#notes.getAsync(nullifier)
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (blockNullifiers.length > 0) {
|
|
267
|
+
nullifiersByBlock.set(i, blockNullifiers);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Second pass: await reads and perform writes
|
|
271
|
+
for (const [block, nullifiers] of nullifiersByBlock){
|
|
272
|
+
for (const { nullifier, noteReadPromise } of nullifiers){
|
|
273
|
+
const noteBuffer = await noteReadPromise;
|
|
291
274
|
if (!noteBuffer) {
|
|
292
|
-
throw new Error(
|
|
293
|
-
}
|
|
294
|
-
const noteScopes = await toArray(this.#notesToScope.getValuesAsync(noteIndex));
|
|
295
|
-
if (noteScopes.length === 0) {
|
|
296
|
-
// We should never run into this error because notes always have a scope assigned to them - either on initial
|
|
297
|
-
// insertion via `addNotes` or when removing their nullifiers.
|
|
298
|
-
throw new Error('Note scopes are missing in applyNullifiers');
|
|
275
|
+
throw new Error(`PXE DB integrity error: no note found with nullifier ${nullifier}`);
|
|
299
276
|
}
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
await this.#notesToScope.delete(noteIndex);
|
|
304
|
-
const scopes = await toArray(this.#scopes.keysAsync());
|
|
305
|
-
for (const scope of scopes){
|
|
306
|
-
await this.#notesByContractAndScope.get(scope).deleteValue(note.contractAddress.toString(), noteIndex);
|
|
307
|
-
await this.#notesByStorageSlotAndScope.get(scope).deleteValue(note.storageSlot.toString(), noteIndex);
|
|
277
|
+
const storedNote = StoredNote.fromBuffer(noteBuffer);
|
|
278
|
+
if (storedNote.scopes.size === 0) {
|
|
279
|
+
throw new Error(`No scopes found for nullified note with nullifier ${nullifier}`);
|
|
308
280
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
await this.#nullifiedNotesByContract.set(note.contractAddress.toString(), noteIndex);
|
|
315
|
-
await this.#nullifiedNotesByStorageSlot.set(note.storageSlot.toString(), noteIndex);
|
|
316
|
-
await this.#nullifiedNotesByNullifier.set(nullifier.toString(), noteIndex);
|
|
317
|
-
await this.#nullifierToNoteId.delete(nullifier.toString());
|
|
281
|
+
storedNote.markAsActive();
|
|
282
|
+
await Promise.all([
|
|
283
|
+
this.#notes.set(nullifier, storedNote.toBuffer()),
|
|
284
|
+
this.#nullifiersByNullificationBlockNumber.deleteValue(block, nullifier)
|
|
285
|
+
]);
|
|
318
286
|
}
|
|
319
|
-
|
|
320
|
-
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Commits in memory job data to persistent storage.
|
|
291
|
+
*
|
|
292
|
+
* Called by JobCoordinator when a job completes successfully.
|
|
293
|
+
*
|
|
294
|
+
* Note: JobCoordinator wraps all commits in a single transaction, so we don't need our own transactionAsync here
|
|
295
|
+
* (and using one would throw on IndexedDB as it does not support nested txs).
|
|
296
|
+
*
|
|
297
|
+
* @param jobId - The jobId identifying which staged data to commit
|
|
298
|
+
*/ async commit(jobId) {
|
|
299
|
+
for (const [nullifier, storedNote] of this.#getNotesForJob(jobId)){
|
|
300
|
+
await this.#notes.set(nullifier, storedNote.toBuffer());
|
|
301
|
+
await this.#nullifiersByContractAddress.set(storedNote.noteDao.contractAddress.toString(), nullifier);
|
|
302
|
+
if (storedNote.nullifiedAt !== undefined) {
|
|
303
|
+
await this.#nullifiersByNullificationBlockNumber.set(storedNote.nullifiedAt, nullifier);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
this.#clearJobData(jobId);
|
|
307
|
+
}
|
|
308
|
+
discardStaged(jobId) {
|
|
309
|
+
this.#clearJobData(jobId);
|
|
310
|
+
return Promise.resolve();
|
|
311
|
+
}
|
|
312
|
+
#clearJobData(jobId) {
|
|
313
|
+
this.#notesForJob.delete(jobId);
|
|
314
|
+
this.#jobLocks.delete(jobId);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Functions run withJobLock are forced to wait for each other, i.e. if they share a `jobId`, they run serially
|
|
318
|
+
* instead of concurrently. This is needed because staged data is stored in memory, and concurrent async operations
|
|
319
|
+
* (e.g., Promise.all in `validateAndStoreNote`) could otherwise interleave and corrupt state.
|
|
320
|
+
*/ async #withJobLock(jobId, fn) {
|
|
321
|
+
let lock = this.#jobLocks.get(jobId);
|
|
322
|
+
if (!lock) {
|
|
323
|
+
lock = new Semaphore(1);
|
|
324
|
+
this.#jobLocks.set(jobId, lock);
|
|
325
|
+
}
|
|
326
|
+
await lock.acquire();
|
|
327
|
+
try {
|
|
328
|
+
return await fn();
|
|
329
|
+
} finally{
|
|
330
|
+
lock.release();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
#getNotesForJob(jobId) {
|
|
334
|
+
let notesForJob = this.#notesForJob.get(jobId);
|
|
335
|
+
if (!notesForJob) {
|
|
336
|
+
notesForJob = new Map();
|
|
337
|
+
this.#notesForJob.set(jobId, notesForJob);
|
|
338
|
+
}
|
|
339
|
+
return notesForJob;
|
|
321
340
|
}
|
|
322
341
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { NoteDao } from '@aztec/stdlib/note';
|
|
3
|
+
export declare class StoredNote {
|
|
4
|
+
readonly noteDao: NoteDao;
|
|
5
|
+
readonly scopes: Set<string>;
|
|
6
|
+
private _nullifiedAt;
|
|
7
|
+
constructor(noteDao: NoteDao, scopes: Set<string>, _nullifiedAt?: BlockNumber | undefined);
|
|
8
|
+
static fromBuffer(buffer: Buffer): StoredNote;
|
|
9
|
+
toBuffer(): Buffer;
|
|
10
|
+
addScope(scope: string): void;
|
|
11
|
+
markAsNullified(blockNumber: BlockNumber): void;
|
|
12
|
+
markAsActive(): void;
|
|
13
|
+
isNullified(): boolean;
|
|
14
|
+
get nullifiedAt(): BlockNumber | undefined;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RvcmVkX25vdGUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9zdG9yYWdlL25vdGVfc3RvcmUvc3RvcmVkX25vdGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBRTlELE9BQU8sRUFBRSxPQUFPLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUU3QyxxQkFBYSxVQUFVO0lBRW5CLFFBQVEsQ0FBQyxPQUFPLEVBQUUsT0FBTztJQUN6QixRQUFRLENBQUMsTUFBTSxFQUFFLEdBQUcsQ0FBQyxNQUFNLENBQUM7SUFDNUIsT0FBTyxDQUFDLFlBQVk7SUFIdEIsWUFDVyxPQUFPLEVBQUUsT0FBTyxFQUNoQixNQUFNLEVBQUUsR0FBRyxDQUFDLE1BQU0sQ0FBQyxFQUNwQixZQUFZLEdBQUUsV0FBVyxHQUFHLFNBQXFCLEVBQ3ZEO0lBRUosTUFBTSxDQUFDLFVBQVUsQ0FBQyxNQUFNLEVBQUUsTUFBTSxjQVUvQjtJQUVELFFBQVEsSUFBSSxNQUFNLENBR2pCO0lBRUQsUUFBUSxDQUFDLEtBQUssRUFBRSxNQUFNLFFBRXJCO0lBRUQsZUFBZSxDQUFDLFdBQVcsRUFBRSxXQUFXLFFBRXZDO0lBRUQsWUFBWSxTQUVYO0lBRUQsV0FBVyxZQUVWO0lBRUQsSUFBSSxXQUFXLDRCQUVkO0NBQ0YifQ==
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stored_note.d.ts","sourceRoot":"","sources":["../../../src/storage/note_store/stored_note.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAE9D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,qBAAa,UAAU;IAEnB,QAAQ,CAAC,OAAO,EAAE,OAAO;IACzB,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;IAC5B,OAAO,CAAC,YAAY;IAHtB,YACW,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,EACpB,YAAY,GAAE,WAAW,GAAG,SAAqB,EACvD;IAEJ,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,cAU/B;IAED,QAAQ,IAAI,MAAM,CAGjB;IAED,QAAQ,CAAC,KAAK,EAAE,MAAM,QAErB;IAED,eAAe,CAAC,WAAW,EAAE,WAAW,QAEvC;IAED,YAAY,SAEX;IAED,WAAW,YAEV;IAED,IAAI,WAAW,4BAEd;CACF"}
|