@aztec/pxe 5.0.0-rc.2 → 5.0.1
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/block_synchronizer/block_synchronizer.d.ts +5 -3
- package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
- package/dest/block_synchronizer/block_synchronizer.js +7 -1
- package/dest/contract/contract_class_service.d.ts +31 -0
- package/dest/contract/contract_class_service.d.ts.map +1 -0
- package/dest/contract/contract_class_service.js +68 -0
- package/dest/{contract_sync → contract}/contract_sync_service.d.ts +7 -6
- package/dest/contract/contract_sync_service.d.ts.map +1 -0
- package/dest/{contract_sync → contract}/contract_sync_service.js +10 -30
- package/dest/contract/helpers.d.ts +7 -0
- package/dest/contract/helpers.d.ts.map +1 -0
- package/dest/contract/helpers.js +20 -0
- package/dest/contract_function_simulator/anchored_contract_data.d.ts +36 -0
- package/dest/contract_function_simulator/anchored_contract_data.d.ts.map +1 -0
- package/dest/contract_function_simulator/anchored_contract_data.js +54 -0
- package/dest/contract_function_simulator/aztec_node_read_cache.d.ts +39 -0
- package/dest/contract_function_simulator/aztec_node_read_cache.d.ts.map +1 -0
- package/dest/contract_function_simulator/aztec_node_read_cache.js +60 -0
- 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 +31 -14
- package/dest/contract_function_simulator/execution_note_cache.d.ts +3 -9
- package/dest/contract_function_simulator/execution_note_cache.d.ts.map +1 -1
- package/dest/contract_function_simulator/execution_note_cache.js +8 -28
- package/dest/contract_function_simulator/index.d.ts +4 -2
- package/dest/contract_function_simulator/index.d.ts.map +1 -1
- package/dest/contract_function_simulator/index.js +2 -1
- package/dest/contract_function_simulator/noir-structs/fact.d.ts +5 -5
- package/dest/contract_function_simulator/noir-structs/fact.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/fact.js +2 -2
- package/dest/contract_function_simulator/noir-structs/fact_collection.d.ts +3 -3
- package/dest/contract_function_simulator/noir-structs/fact_collection.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/fact_collection.js +3 -1
- package/dest/contract_function_simulator/noir-structs/log_retrieval_response.d.ts +7 -1
- package/dest/contract_function_simulator/noir-structs/log_retrieval_response.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/pending_tagged_log.d.ts +11 -0
- package/dest/contract_function_simulator/noir-structs/pending_tagged_log.d.ts.map +1 -0
- package/dest/contract_function_simulator/noir-structs/pending_tagged_log.js +4 -0
- package/dest/contract_function_simulator/noir-structs/provided_secret.d.ts +2 -2
- package/dest/contract_function_simulator/noir-structs/provided_secret.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/provided_secret.js +1 -1
- package/dest/contract_function_simulator/noir-structs/resolved_tagging_strategy.d.ts +8 -1
- package/dest/contract_function_simulator/noir-structs/resolved_tagging_strategy.d.ts.map +1 -1
- package/dest/contract_function_simulator/noir-structs/resolved_tagging_strategy.js +17 -0
- package/dest/contract_function_simulator/oracle/acir_callback.d.ts +7 -2
- package/dest/contract_function_simulator/oracle/acir_callback.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/acir_callback.js +55 -11
- package/dest/contract_function_simulator/oracle/legacy_oracle_registry.d.ts +39 -0
- package/dest/contract_function_simulator/oracle/legacy_oracle_registry.d.ts.map +1 -0
- package/dest/contract_function_simulator/oracle/legacy_oracle_registry.js +107 -0
- package/dest/contract_function_simulator/oracle/oracle_registry.d.ts +11 -7
- package/dest/contract_function_simulator/oracle/oracle_registry.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/oracle_registry.js +50 -10
- package/dest/contract_function_simulator/oracle/oracle_type_mappings.d.ts +48 -9
- package/dest/contract_function_simulator/oracle/oracle_type_mappings.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/oracle_type_mappings.js +51 -30
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +10 -2
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.js +73 -32
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +23 -16
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +105 -60
- package/dest/debug/pxe_debug_utils.d.ts +2 -2
- package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
- package/dest/entrypoints/client/bundle/index.d.ts +2 -1
- package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
- package/dest/entrypoints/client/bundle/index.js +1 -0
- 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 +11 -5
- package/dest/entrypoints/client/lazy/index.d.ts +2 -1
- package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
- package/dest/entrypoints/client/lazy/index.js +1 -0
- package/dest/entrypoints/client/lazy/utils.d.ts +1 -1
- package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
- package/dest/entrypoints/client/lazy/utils.js +11 -5
- package/dest/entrypoints/client/store.d.ts +14 -0
- package/dest/entrypoints/client/store.d.ts.map +1 -0
- package/dest/entrypoints/client/store.js +28 -0
- package/dest/entrypoints/server/index.d.ts +6 -2
- package/dest/entrypoints/server/index.d.ts.map +1 -1
- package/dest/entrypoints/server/index.js +4 -1
- package/dest/entrypoints/server/store.d.ts +18 -0
- package/dest/entrypoints/server/store.d.ts.map +1 -0
- package/dest/entrypoints/server/store.js +28 -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 +8 -2
- package/dest/error_enriching.d.ts +5 -3
- package/dest/error_enriching.d.ts.map +1 -1
- package/dest/error_enriching.js +12 -10
- package/dest/hooks/execution_hooks.d.ts +15 -4
- package/dest/hooks/execution_hooks.d.ts.map +1 -1
- package/dest/hooks/index.d.ts +3 -2
- package/dest/hooks/index.d.ts.map +1 -1
- package/dest/hooks/index.js +1 -0
- package/dest/hooks/resolve_custom_request.d.ts +23 -0
- package/dest/hooks/resolve_custom_request.d.ts.map +1 -0
- package/dest/hooks/resolve_custom_request.js +3 -0
- package/dest/hooks/resolve_tagging_secret_strategy.d.ts +10 -1
- package/dest/hooks/resolve_tagging_secret_strategy.d.ts.map +1 -1
- package/dest/hooks/resolve_tagging_secret_strategy.js +3 -3
- package/dest/logs/log_service.d.ts +5 -3
- package/dest/logs/log_service.d.ts.map +1 -1
- package/dest/logs/log_service.js +38 -17
- package/dest/notes/note_service.d.ts +1 -1
- package/dest/notes/note_service.js +1 -1
- package/dest/oracle_version.d.ts +2 -2
- package/dest/oracle_version.js +2 -2
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +1 -1
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -1
- package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.js +6 -2
- package/dest/private_kernel/private_kernel_execution_prover.d.ts +1 -1
- package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
- package/dest/private_kernel/private_kernel_execution_prover.js +17 -36
- package/dest/private_kernel/private_kernel_oracle.d.ts +4 -2
- package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
- package/dest/private_kernel/private_kernel_oracle.js +17 -5
- package/dest/pxe.d.ts +81 -46
- package/dest/pxe.d.ts.map +1 -1
- package/dest/pxe.js +138 -104
- package/dest/storage/backwards_compatibility_tests/schema_tests.d.ts +1 -1
- package/dest/storage/backwards_compatibility_tests/schema_tests.d.ts.map +1 -1
- package/dest/storage/backwards_compatibility_tests/schema_tests.js +31 -7
- package/dest/storage/contract_store/contract_store.d.ts +27 -24
- package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
- package/dest/storage/contract_store/contract_store.js +63 -74
- package/dest/storage/fact_store/fact_service.d.ts +6 -4
- package/dest/storage/fact_store/fact_service.d.ts.map +1 -1
- package/dest/storage/fact_store/fact_service.js +19 -4
- package/dest/storage/fact_store/fact_store.d.ts +1 -1
- package/dest/storage/fact_store/fact_store.js +1 -1
- package/dest/storage/fact_store/index.d.ts +2 -1
- package/dest/storage/fact_store/index.d.ts.map +1 -1
- package/dest/storage/fact_store/index.js +1 -0
- package/dest/storage/fact_store/origin_state.d.ts +57 -0
- package/dest/storage/fact_store/origin_state.d.ts.map +1 -0
- package/dest/storage/fact_store/origin_state.js +53 -0
- package/dest/storage/metadata.d.ts +2 -2
- package/dest/storage/metadata.d.ts.map +1 -1
- package/dest/storage/metadata.js +1 -1
- package/dest/storage/store_identity.d.ts +35 -0
- package/dest/storage/store_identity.d.ts.map +1 -0
- package/dest/storage/store_identity.js +44 -0
- package/dest/storage/tagging_store/sender_tagging_store.d.ts +19 -3
- package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.js +48 -11
- package/dest/storage/tagging_store/tagging_secret_sources_store.d.ts +17 -6
- package/dest/storage/tagging_store/tagging_secret_sources_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/tagging_secret_sources_store.js +24 -11
- package/dest/tagging/constants.d.ts +2 -2
- package/dest/tagging/constants.d.ts.map +1 -1
- package/dest/tagging/constants.js +18 -9
- package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +1 -1
- package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +1 -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 +6 -2
- package/package.json +17 -17
- package/src/block_synchronizer/block_synchronizer.ts +9 -1
- package/src/contract/contract_class_service.ts +83 -0
- package/src/{contract_sync → contract}/contract_sync_service.ts +18 -37
- package/src/contract/helpers.ts +35 -0
- package/src/contract_function_simulator/anchored_contract_data.ts +74 -0
- package/src/contract_function_simulator/aztec_node_read_cache.ts +89 -0
- package/src/contract_function_simulator/contract_function_simulator.ts +56 -14
- package/src/contract_function_simulator/execution_note_cache.ts +7 -28
- package/src/contract_function_simulator/index.ts +7 -1
- package/src/contract_function_simulator/noir-structs/fact.ts +4 -4
- package/src/contract_function_simulator/noir-structs/fact_collection.ts +9 -4
- package/src/contract_function_simulator/noir-structs/log_retrieval_response.ts +6 -0
- package/src/contract_function_simulator/noir-structs/pending_tagged_log.ts +12 -0
- package/src/contract_function_simulator/noir-structs/provided_secret.ts +1 -1
- package/src/contract_function_simulator/noir-structs/resolved_tagging_strategy.ts +21 -0
- package/src/contract_function_simulator/oracle/acir_callback.ts +58 -12
- package/src/contract_function_simulator/oracle/legacy_oracle_registry.ts +147 -0
- package/src/contract_function_simulator/oracle/oracle_registry.ts +37 -7
- package/src/contract_function_simulator/oracle/oracle_type_mappings.ts +49 -32
- package/src/contract_function_simulator/oracle/private_execution_oracle.ts +92 -41
- package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +149 -71
- package/src/debug/pxe_debug_utils.ts +1 -1
- package/src/entrypoints/client/bundle/index.ts +1 -0
- package/src/entrypoints/client/bundle/utils.ts +16 -4
- package/src/entrypoints/client/lazy/index.ts +1 -0
- package/src/entrypoints/client/lazy/utils.ts +16 -4
- package/src/entrypoints/client/store.ts +38 -0
- package/src/entrypoints/server/index.ts +6 -1
- package/src/entrypoints/server/store.ts +47 -0
- package/src/entrypoints/server/utils.ts +21 -7
- package/src/error_enriching.ts +26 -10
- package/src/hooks/execution_hooks.ts +14 -3
- package/src/hooks/index.ts +2 -0
- package/src/hooks/resolve_custom_request.ts +24 -0
- package/src/hooks/resolve_tagging_secret_strategy.ts +14 -0
- package/src/logs/log_service.ts +35 -11
- package/src/notes/note_service.ts +1 -1
- package/src/oracle_version.ts +2 -2
- package/src/private_kernel/hints/private_kernel_reset_private_inputs_builder.ts +11 -1
- package/src/private_kernel/private_kernel_execution_prover.ts +22 -52
- package/src/private_kernel/private_kernel_oracle.ts +23 -4
- package/src/pxe.ts +212 -143
- package/src/storage/backwards_compatibility_tests/__snapshots__/AddressStore.json +6 -6
- package/src/storage/backwards_compatibility_tests/__snapshots__/ContractStore.json +1 -1
- package/src/storage/backwards_compatibility_tests/__snapshots__/KeyStore.json +12 -12
- package/src/storage/backwards_compatibility_tests/__snapshots__/RecipientTaggingStore.json +11 -3
- package/src/storage/backwards_compatibility_tests/__snapshots__/SenderTaggingStore.json +12 -4
- package/src/storage/backwards_compatibility_tests/__snapshots__/TaggingSecretSourcesStore.json +3 -3
- package/src/storage/backwards_compatibility_tests/__snapshots__/opened_stores.json +1 -1
- package/src/storage/backwards_compatibility_tests/schema_tests.ts +38 -4
- package/src/storage/contract_store/contract_store.ts +85 -92
- package/src/storage/fact_store/fact_service.ts +29 -5
- package/src/storage/fact_store/fact_store.ts +1 -1
- package/src/storage/fact_store/index.ts +10 -0
- package/src/storage/fact_store/origin_state.ts +75 -0
- package/src/storage/metadata.ts +1 -1
- package/src/storage/store_identity.ts +72 -0
- package/src/storage/tagging_store/sender_tagging_store.ts +62 -15
- package/src/storage/tagging_store/tagging_secret_sources_store.ts +36 -15
- package/src/tagging/constants.ts +19 -9
- package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +1 -1
- package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +6 -2
- package/dest/contract_function_simulator/proxied_contract_data_source.d.ts +0 -6
- package/dest/contract_function_simulator/proxied_contract_data_source.d.ts.map +0 -1
- package/dest/contract_function_simulator/proxied_contract_data_source.js +0 -51
- package/dest/contract_sync/contract_sync_service.d.ts.map +0 -1
- package/dest/contract_sync/helpers.d.ts +0 -28
- package/dest/contract_sync/helpers.d.ts.map +0 -1
- package/dest/contract_sync/helpers.js +0 -48
- package/src/contract_function_simulator/proxied_contract_data_source.ts +0 -60
- package/src/contract_sync/helpers.ts +0 -79
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
|
+
import { isProtocolContract } from '@aztec/protocol-contracts';
|
|
3
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
+
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
5
|
+
import type { BlockHeader } from '@aztec/stdlib/tx';
|
|
6
|
+
|
|
7
|
+
import type { ContractStore } from '../storage/contract_store/contract_store.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the contract class id that an address runs at a given anchor block, as tracked by the chain.
|
|
11
|
+
*
|
|
12
|
+
* PXE does not store a contract's current class id: it is mutable, chain-derived state that changes when a contract is
|
|
13
|
+
* upgraded and can be undone by a reorg. Instead this service asks the node for the current class at an anchor block,
|
|
14
|
+
* falling back to a local instance if no upgrades were scheduled.
|
|
15
|
+
*/
|
|
16
|
+
export class ContractClassService {
|
|
17
|
+
/**
|
|
18
|
+
* Class ids are cached per `(address, anchorHash)`. This avoid unnecessary network roundtrips in scenarios where
|
|
19
|
+
* multiple executions are done on the same anchor block (e.g. simulation followed by witgen), or when the same
|
|
20
|
+
* contract is invoked multiple times in an execution (e.g. authwit checks).
|
|
21
|
+
* It also means the callers don't need to worry about caching this service's return values, simplifying callsites.
|
|
22
|
+
*/
|
|
23
|
+
#cache: Map<string, Promise<Fr | undefined>> = new Map();
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
private node: AztecNode,
|
|
27
|
+
private contractStore: ContractStore,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the class id that corresponds to `address` as of `anchorBlockHeader`, or `undefined` if no instance is
|
|
32
|
+
* registered for `address`. A missing instance is an absence the caller decides how to handle, not an error; genuine
|
|
33
|
+
* failures (e.g. the node being unreachable) still throw.
|
|
34
|
+
*/
|
|
35
|
+
async getCurrentClassId(address: AztecAddress, anchorBlockHeader: BlockHeader): Promise<Fr | undefined> {
|
|
36
|
+
// Protocol contracts are not in the registry and cannot be upgraded, so their original class is always current.
|
|
37
|
+
// Short-circuiting avoids a node round-trip.
|
|
38
|
+
if (isProtocolContract(address)) {
|
|
39
|
+
const instance = await this.contractStore.getContractInstance(address);
|
|
40
|
+
return instance?.originalContractClassId;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const key = `${address.toString()}:${(await anchorBlockHeader.hash()).toString()}`;
|
|
44
|
+
let promise = this.#cache.get(key);
|
|
45
|
+
if (!promise) {
|
|
46
|
+
promise = (async () => {
|
|
47
|
+
// The node resolves the current class from the same scheduled value change the AVM enforces against the public
|
|
48
|
+
// data tree. If the contract was upgraded the node returns a non-undefined instance; an undefined result means
|
|
49
|
+
// no upgrade happened (or the node has no record of it, e.g. it was never publicly deployed), so the original
|
|
50
|
+
// class is current.
|
|
51
|
+
|
|
52
|
+
const nodeInstance = await this.node.getContract(address, await anchorBlockHeader.hash());
|
|
53
|
+
|
|
54
|
+
if (nodeInstance) {
|
|
55
|
+
return nodeInstance.currentContractClassId;
|
|
56
|
+
} else {
|
|
57
|
+
return (await this.contractStore.getContractInstance(address))?.originalContractClassId;
|
|
58
|
+
}
|
|
59
|
+
})().catch(err => {
|
|
60
|
+
this.#cache.delete(key);
|
|
61
|
+
throw err;
|
|
62
|
+
});
|
|
63
|
+
this.#cache.set(key, promise);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const classId = await promise;
|
|
67
|
+
if (classId === undefined) {
|
|
68
|
+
// Don't memoize a missing instance: it may be registered later in this PXE (e.g. via contract sync
|
|
69
|
+
// mid-execution), and a cached miss would then hide it. Only successful resolutions stay cached.
|
|
70
|
+
this.#cache.delete(key);
|
|
71
|
+
}
|
|
72
|
+
return classId;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Clears the cache.
|
|
77
|
+
*
|
|
78
|
+
* This is not required for correctness, only to limit how much memory the cache uses. The cache is resilient against
|
|
79
|
+
* reorgs etc. as it is based on block hashes, not block numbers. */
|
|
80
|
+
wipe(): void {
|
|
81
|
+
this.#cache.clear();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -10,7 +10,8 @@ import type { StagedStore } from '../job_coordinator/job_coordinator.js';
|
|
|
10
10
|
import { NoteService } from '../notes/note_service.js';
|
|
11
11
|
import type { ContractStore } from '../storage/contract_store/contract_store.js';
|
|
12
12
|
import type { NoteStore } from '../storage/note_store/note_store.js';
|
|
13
|
-
import {
|
|
13
|
+
import type { ContractClassService } from './contract_class_service.js';
|
|
14
|
+
import { syncScope } from './helpers.js';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Maximum number of scope syncs running concurrently within a single sync call. Sized to trade off parallelism
|
|
@@ -19,8 +20,8 @@ import { syncScope, verifyCurrentClassId } from './helpers.js';
|
|
|
19
20
|
export const MAX_CONCURRENT_SCOPE_SYNCS = 5;
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
|
-
* Service for syncing the private state of contracts
|
|
23
|
-
*
|
|
23
|
+
* Service for syncing the private state of contracts. It uses a cache to avoid redundant sync operations - the cache
|
|
24
|
+
* is wiped when the anchor block changes.
|
|
24
25
|
*
|
|
25
26
|
* TODO: The StagedStore naming is broken here. Figure out a better name.
|
|
26
27
|
*/
|
|
@@ -32,19 +33,16 @@ export class ContractSyncService implements StagedStore {
|
|
|
32
33
|
// The value is a promise that resolves when the contract is synced.
|
|
33
34
|
private syncedContracts: Map<string, Promise<void>> = new Map();
|
|
34
35
|
|
|
35
|
-
// Tracks class ID verification per contract. Keyed by contract address only (no scope), since
|
|
36
|
-
// class ID verification is scope-independent. Cleared on wipe/discard.
|
|
37
|
-
private classIdVerificationCache: Map<string, Promise<void>> = new Map();
|
|
38
|
-
|
|
39
36
|
constructor(
|
|
40
37
|
private aztecNode: AztecNode,
|
|
41
38
|
private contractStore: ContractStore,
|
|
39
|
+
private contractClassService: ContractClassService,
|
|
42
40
|
private noteStore: NoteStore,
|
|
43
41
|
private log: Logger,
|
|
44
42
|
) {}
|
|
45
43
|
|
|
46
44
|
/**
|
|
47
|
-
* Ensures a contract's private state is synchronized
|
|
45
|
+
* Ensures a contract's private state is synchronized.
|
|
48
46
|
* Uses a cache to avoid redundant sync operations - the cache is wiped when the anchor block changes.
|
|
49
47
|
* @param contractAddress - The address of the contract to sync.
|
|
50
48
|
* @param functionToInvokeAfterSync - The function selector that will be called after sync (used to validate it's
|
|
@@ -61,7 +59,15 @@ export class ContractSyncService implements StagedStore {
|
|
|
61
59
|
scopes: AztecAddress[],
|
|
62
60
|
): Promise<void> {
|
|
63
61
|
this.#startSyncIfNeeded(contractAddress, scopes, anchorBlockHeader, jobId, scope =>
|
|
64
|
-
syncScope(
|
|
62
|
+
syncScope(
|
|
63
|
+
contractAddress,
|
|
64
|
+
this.contractStore,
|
|
65
|
+
this.contractClassService,
|
|
66
|
+
anchorBlockHeader,
|
|
67
|
+
functionToInvokeAfterSync,
|
|
68
|
+
utilityExecutor,
|
|
69
|
+
scope,
|
|
70
|
+
),
|
|
65
71
|
);
|
|
66
72
|
|
|
67
73
|
await this.#awaitSync(contractAddress, scopes);
|
|
@@ -79,7 +85,6 @@ export class ContractSyncService implements StagedStore {
|
|
|
79
85
|
wipe(): void {
|
|
80
86
|
this.log.debug(`Wiping contract sync cache (${this.syncedContracts.size} entries)`);
|
|
81
87
|
this.syncedContracts.clear();
|
|
82
|
-
this.classIdVerificationCache.clear();
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
commit(_jobId: string): Promise<void> {
|
|
@@ -90,15 +95,13 @@ export class ContractSyncService implements StagedStore {
|
|
|
90
95
|
// We clear the synced contracts cache here because, when the job is discarded, any associated database writes from
|
|
91
96
|
// the sync are also undone.
|
|
92
97
|
this.syncedContracts.clear();
|
|
93
|
-
this.classIdVerificationCache.clear();
|
|
94
98
|
return Promise.resolve();
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
/**
|
|
98
102
|
* For each unsynced scope, creates a promise that waits on:
|
|
99
|
-
* 1.
|
|
100
|
-
* 2.
|
|
101
|
-
* 3. Per-scope sync (individual, semaphore-bounded).
|
|
103
|
+
* 1. Note nullifier sync (shared, batched across all unsynced scopes).
|
|
104
|
+
* 2. Per-scope sync (individual, semaphore-bounded).
|
|
102
105
|
*/
|
|
103
106
|
#startSyncIfNeeded(
|
|
104
107
|
contractAddress: AztecAddress,
|
|
@@ -114,7 +117,6 @@ export class ContractSyncService implements StagedStore {
|
|
|
114
117
|
|
|
115
118
|
this.log.debug(`Syncing contract ${contractAddress} for ${scopesToSync.length} scope(s)`);
|
|
116
119
|
|
|
117
|
-
const verifyPromise = this.#verifyClassId(contractAddress, anchorBlockHeader);
|
|
118
120
|
const syncNullifiersPromise = this.#syncNoteNullifiers(contractAddress, anchorBlockHeader, jobId, scopesToSync);
|
|
119
121
|
|
|
120
122
|
// We build a new semaphore for each sync call, so it rate-limits the scopes within that single call. We do
|
|
@@ -123,11 +125,7 @@ export class ContractSyncService implements StagedStore {
|
|
|
123
125
|
|
|
124
126
|
for (const scope of scopesToSync) {
|
|
125
127
|
const key = toKey(contractAddress, scope);
|
|
126
|
-
const promise = Promise.all([
|
|
127
|
-
verifyPromise,
|
|
128
|
-
syncNullifiersPromise,
|
|
129
|
-
runBounded(syncSlot, () => syncScopeFn(scope)),
|
|
130
|
-
])
|
|
128
|
+
const promise = Promise.all([syncNullifiersPromise, runBounded(syncSlot, () => syncScopeFn(scope))])
|
|
131
129
|
.then(() => {})
|
|
132
130
|
.catch(err => {
|
|
133
131
|
this.syncedContracts.delete(key);
|
|
@@ -137,23 +135,6 @@ export class ContractSyncService implements StagedStore {
|
|
|
137
135
|
}
|
|
138
136
|
}
|
|
139
137
|
|
|
140
|
-
/** Verifies the local class ID matches the on-chain value (cached, evicts on failure so retries re-verify). */
|
|
141
|
-
#verifyClassId(contractAddress: AztecAddress, anchorBlockHeader: BlockHeader): Promise<void> {
|
|
142
|
-
const contractKey = contractAddress.toString();
|
|
143
|
-
const cached = this.classIdVerificationCache.get(contractKey);
|
|
144
|
-
if (cached) {
|
|
145
|
-
return cached;
|
|
146
|
-
}
|
|
147
|
-
const promise = verifyCurrentClassId(contractAddress, this.aztecNode, this.contractStore, anchorBlockHeader).catch(
|
|
148
|
-
err => {
|
|
149
|
-
this.classIdVerificationCache.delete(contractKey);
|
|
150
|
-
throw err;
|
|
151
|
-
},
|
|
152
|
-
);
|
|
153
|
-
this.classIdVerificationCache.set(contractKey, promise);
|
|
154
|
-
return promise;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
138
|
/** Syncs note nullifiers across all unsynced scopes in a single batched call. */
|
|
158
139
|
async #syncNoteNullifiers(
|
|
159
140
|
contractAddress: AztecAddress,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isProtocolContract } from '@aztec/protocol-contracts';
|
|
2
|
+
import type { FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
|
|
3
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
+
import type { BlockHeader } from '@aztec/stdlib/tx';
|
|
5
|
+
|
|
6
|
+
import type { ContractStore } from '../storage/contract_store/contract_store.js';
|
|
7
|
+
import type { ContractClassService } from './contract_class_service.js';
|
|
8
|
+
|
|
9
|
+
export async function syncScope(
|
|
10
|
+
contractAddress: AztecAddress,
|
|
11
|
+
contractStore: ContractStore,
|
|
12
|
+
contractClassService: ContractClassService,
|
|
13
|
+
anchorBlockHeader: BlockHeader,
|
|
14
|
+
functionToInvokeAfterSync: FunctionSelector | null,
|
|
15
|
+
utilityExecutor: (privateSyncCall: FunctionCall, scopes: AztecAddress[]) => Promise<any>,
|
|
16
|
+
scope: AztecAddress,
|
|
17
|
+
) {
|
|
18
|
+
// Protocol contracts don't have private state to sync
|
|
19
|
+
if (isProtocolContract(contractAddress)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const classId = await contractClassService.getCurrentClassId(contractAddress, anchorBlockHeader);
|
|
24
|
+
if (!classId) {
|
|
25
|
+
throw new Error(`Cannot sync contract ${contractAddress}: its instance is not registered nor published.`);
|
|
26
|
+
}
|
|
27
|
+
const syncStateFunctionCall = await contractStore.getFunctionCall('sync_state', [scope], contractAddress, classId);
|
|
28
|
+
if (functionToInvokeAfterSync && functionToInvokeAfterSync.equals(syncStateFunctionCall.selector)) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
'Forbidden `sync_state` invocation. `sync_state` can only be invoked by PXE, manual execution can lead to inconsistencies.',
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await utilityExecutor(syncStateFunctionCall, [scope]);
|
|
35
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
|
+
import type { FunctionArtifactWithContractName, FunctionSelector } from '@aztec/stdlib/abi';
|
|
3
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
+
import type { ContractInstancePreimageWithAddress } from '@aztec/stdlib/contract';
|
|
5
|
+
import type { BlockHeader, ContractOverrides } from '@aztec/stdlib/tx';
|
|
6
|
+
|
|
7
|
+
import type { ContractClassService } from '../contract/contract_class_service.js';
|
|
8
|
+
import type { ContractStore } from '../storage/contract_store/contract_store.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Per-run view of contract data for a single simulation, bound to its anchor block.
|
|
12
|
+
*
|
|
13
|
+
* The {@link ContractStore} is pure class-id-keyed storage and has no notion of which class an address runs. This
|
|
14
|
+
* class bridges that gap: it resolves an address to its current class id (via the {@link ContractClassService}
|
|
15
|
+
* at the run's anchor block) and then serves artifacts from the store. It is also the single place contract
|
|
16
|
+
* overrides are applied — when an address is overridden, both its instance and its class id come from the override
|
|
17
|
+
* rather than the chain, so a simulation can execute different bytecode at that address. Overrides are only set for
|
|
18
|
+
* `simulateTx` (which skips the kernels), so the override path never reaches proving.
|
|
19
|
+
*/
|
|
20
|
+
export class AnchoredContractData {
|
|
21
|
+
constructor(
|
|
22
|
+
private readonly store: ContractStore,
|
|
23
|
+
private readonly contractClassService: ContractClassService,
|
|
24
|
+
private readonly anchorBlockHeader: BlockHeader,
|
|
25
|
+
private readonly overrides?: ContractOverrides,
|
|
26
|
+
) {}
|
|
27
|
+
|
|
28
|
+
/** Returns the address preimage of the instance at `address`, from the override if any, else from storage. */
|
|
29
|
+
getContractInstance(address: AztecAddress): Promise<ContractInstancePreimageWithAddress | undefined> {
|
|
30
|
+
const override = this.overrides?.[address.toString()];
|
|
31
|
+
if (override) {
|
|
32
|
+
return Promise.resolve(override.instance);
|
|
33
|
+
}
|
|
34
|
+
return this.store.getContractInstance(address);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolves the class id `address` runs in this simulation: the override's class if overridden, else resolved against
|
|
39
|
+
* the chain at the anchor block.
|
|
40
|
+
*/
|
|
41
|
+
getCurrentClassId(address: AztecAddress): Promise<Fr | undefined> {
|
|
42
|
+
const override = this.overrides?.[address.toString()];
|
|
43
|
+
if (override) {
|
|
44
|
+
return Promise.resolve(override.instance.currentContractClassId);
|
|
45
|
+
}
|
|
46
|
+
return this.contractClassService.getCurrentClassId(address, this.anchorBlockHeader);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async getFunctionArtifact(
|
|
50
|
+
address: AztecAddress,
|
|
51
|
+
selector: FunctionSelector,
|
|
52
|
+
): Promise<FunctionArtifactWithContractName | undefined> {
|
|
53
|
+
const classId = await this.getCurrentClassId(address);
|
|
54
|
+
return classId ? this.store.getFunctionArtifact(classId, selector) : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async getFunctionArtifactWithDebugMetadata(
|
|
58
|
+
address: AztecAddress,
|
|
59
|
+
selector: FunctionSelector,
|
|
60
|
+
): Promise<FunctionArtifactWithContractName | undefined> {
|
|
61
|
+
const classId = await this.getCurrentClassId(address);
|
|
62
|
+
return classId ? this.store.getFunctionArtifactWithDebugMetadata(classId, selector) : undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async getDebugContractName(address: AztecAddress): Promise<string | undefined> {
|
|
66
|
+
const classId = await this.getCurrentClassId(address);
|
|
67
|
+
return classId ? this.store.getDebugContractName(classId) : undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string> {
|
|
71
|
+
const classId = await this.getCurrentClassId(address);
|
|
72
|
+
return classId ? this.store.getDebugFunctionName(classId, selector) : `${address}:${selector}`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
3
|
+
import type { BlockHash, BlockParameter } from '@aztec/stdlib/block';
|
|
4
|
+
import type { AztecNode } from '@aztec/stdlib/interfaces/server';
|
|
5
|
+
import type { TxHash } from '@aztec/stdlib/tx';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Per-execution cache for immutable Aztec node reads.
|
|
9
|
+
*/
|
|
10
|
+
export class AztecNodeReadCache {
|
|
11
|
+
private readonly cache = new Map<string, Promise<unknown>>();
|
|
12
|
+
|
|
13
|
+
constructor(private readonly aztecNode: AztecNode) {}
|
|
14
|
+
|
|
15
|
+
/** Fetches a block without reissuing the same node request. */
|
|
16
|
+
public getBlock(block: BlockParameter) {
|
|
17
|
+
return this.#cachedRead(`block:${this.#keyPart(block)}`, () => this.aztecNode.getBlock(block));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Fetches a transaction receipt with its effect attached. */
|
|
21
|
+
public getTxReceiptWithEffect(txHash: TxHash) {
|
|
22
|
+
return this.#cachedRead(`tx-receipt-with-effect:${txHash.toString()}`, () =>
|
|
23
|
+
this.aztecNode.getTxReceipt(txHash, { includeTxEffect: true } as const),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Fetches an archive-tree witness for a block hash. */
|
|
28
|
+
public getBlockHashMembershipWitness(referenceBlock: BlockParameter, blockHash: BlockHash) {
|
|
29
|
+
return this.#cachedRead(
|
|
30
|
+
`block-hash-membership-witness:${this.#keyPart(referenceBlock)}:${blockHash.toString()}`,
|
|
31
|
+
() => this.aztecNode.getBlockHashMembershipWitness(referenceBlock, blockHash),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Fetches a public-data-tree witness for a leaf slot. */
|
|
36
|
+
public getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr) {
|
|
37
|
+
return this.#cachedRead(`public-data-witness:${this.#keyPart(referenceBlock)}:${leafSlot.toString()}`, () =>
|
|
38
|
+
this.aztecNode.getPublicDataWitness(referenceBlock, leafSlot),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Fetches public storage for a single slot. */
|
|
43
|
+
public getPublicStorageAt(referenceBlock: BlockParameter, contractAddress: AztecAddress, storageSlot: Fr) {
|
|
44
|
+
return this.#cachedRead(
|
|
45
|
+
`public-storage:${this.#keyPart(referenceBlock)}:${contractAddress.toString()}:${storageSlot.toString()}`,
|
|
46
|
+
() => this.aztecNode.getPublicStorageAt(referenceBlock, contractAddress, storageSlot),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Fetches a contiguous public storage range, reusing cached reads for overlapping slots. */
|
|
51
|
+
public getPublicStorageRange(
|
|
52
|
+
referenceBlock: BlockParameter,
|
|
53
|
+
contractAddress: AztecAddress,
|
|
54
|
+
startStorageSlot: Fr,
|
|
55
|
+
numberOfElements: number,
|
|
56
|
+
) {
|
|
57
|
+
const slots = Array(numberOfElements)
|
|
58
|
+
.fill(0)
|
|
59
|
+
.map((_, i) => new Fr(startStorageSlot.value + BigInt(i)));
|
|
60
|
+
|
|
61
|
+
return Promise.all(slots.map(storageSlot => this.getPublicStorageAt(referenceBlock, contractAddress, storageSlot)));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#cachedRead<T>(key: string, fetch: () => Promise<T>): Promise<T> {
|
|
65
|
+
const cached = this.cache.get(key);
|
|
66
|
+
if (cached) {
|
|
67
|
+
return cached as Promise<T>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const promise = fetch();
|
|
71
|
+
promise.catch(() => this.cache.delete(key));
|
|
72
|
+
this.cache.set(key, promise);
|
|
73
|
+
return promise;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#keyPart(value: unknown): string {
|
|
77
|
+
if (['string', 'number', 'bigint', 'boolean'].includes(typeof value)) {
|
|
78
|
+
return String(value);
|
|
79
|
+
}
|
|
80
|
+
if (value && typeof value === 'object') {
|
|
81
|
+
const toString = (value as { toString?: () => string }).toString;
|
|
82
|
+
if (toString && toString !== Object.prototype.toString) {
|
|
83
|
+
return toString.call(value);
|
|
84
|
+
}
|
|
85
|
+
return JSON.stringify(value, (_key, nested) => (typeof nested === 'bigint' ? nested.toString() : nested));
|
|
86
|
+
}
|
|
87
|
+
return String(value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
MAX_PRIVATE_LOGS_PER_TX,
|
|
20
20
|
MAX_TX_LIFETIME,
|
|
21
21
|
PRIVATE_TX_L2_GAS_OVERHEAD,
|
|
22
|
+
PUBLIC_DATA_WRITE_LENGTH,
|
|
22
23
|
PUBLIC_TX_L2_GAS_OVERHEAD,
|
|
23
24
|
TX_DA_GAS_OVERHEAD,
|
|
24
25
|
} from '@aztec/constants';
|
|
@@ -80,6 +81,7 @@ import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
|
80
81
|
import {
|
|
81
82
|
BlockHeader,
|
|
82
83
|
CallContext,
|
|
84
|
+
type ContractOverrides,
|
|
83
85
|
HashedValues,
|
|
84
86
|
type OffchainEffect,
|
|
85
87
|
PrivateExecutionResult,
|
|
@@ -90,7 +92,8 @@ import {
|
|
|
90
92
|
getFinalMinRevertibleSideEffectCounter,
|
|
91
93
|
} from '@aztec/stdlib/tx';
|
|
92
94
|
|
|
93
|
-
import type {
|
|
95
|
+
import type { ContractClassService } from '../contract/contract_class_service.js';
|
|
96
|
+
import type { ContractSyncService } from '../contract/contract_sync_service.js';
|
|
94
97
|
import type { ExecutionHooks } from '../hooks/index.js';
|
|
95
98
|
import type { TxResolverService } from '../messages/tx_resolver_service.js';
|
|
96
99
|
import type { AddressStore } from '../storage/address_store/address_store.js';
|
|
@@ -104,6 +107,7 @@ import type { PrivateEventStore } from '../storage/private_event_store/private_e
|
|
|
104
107
|
import type { RecipientTaggingStore } from '../storage/tagging_store/recipient_tagging_store.js';
|
|
105
108
|
import type { SenderTaggingStore } from '../storage/tagging_store/sender_tagging_store.js';
|
|
106
109
|
import type { TaggingSecretSourcesStore } from '../storage/tagging_store/tagging_secret_sources_store.js';
|
|
110
|
+
import { AnchoredContractData } from './anchored_contract_data.js';
|
|
107
111
|
import type { BenchmarkedNode } from './benchmarked_node.js';
|
|
108
112
|
import { ExecutionNoteCache } from './execution_note_cache.js';
|
|
109
113
|
import { ExecutionTaggingIndexCache } from './execution_tagging_index_cache.js';
|
|
@@ -131,6 +135,9 @@ export type ContractSimulatorRunOpts = {
|
|
|
131
135
|
/** Args for ContractFunctionSimulator constructor. */
|
|
132
136
|
export type ContractFunctionSimulatorArgs = {
|
|
133
137
|
contractStore: ContractStore;
|
|
138
|
+
contractClassService: ContractClassService;
|
|
139
|
+
/** Per-simulation contract overrides. */
|
|
140
|
+
overrides?: ContractOverrides;
|
|
134
141
|
noteStore: NoteStore;
|
|
135
142
|
keyStore: KeyStore;
|
|
136
143
|
addressStore: AddressStore;
|
|
@@ -154,6 +161,8 @@ export type ContractFunctionSimulatorArgs = {
|
|
|
154
161
|
export class ContractFunctionSimulator {
|
|
155
162
|
private readonly log: Logger;
|
|
156
163
|
private readonly contractStore: ContractStore;
|
|
164
|
+
private readonly contractClassService: ContractClassService;
|
|
165
|
+
private readonly overrides: ContractOverrides | undefined;
|
|
157
166
|
private readonly noteStore: NoteStore;
|
|
158
167
|
private readonly keyStore: KeyStore;
|
|
159
168
|
private readonly addressStore: AddressStore;
|
|
@@ -172,6 +181,8 @@ export class ContractFunctionSimulator {
|
|
|
172
181
|
|
|
173
182
|
constructor(args: ContractFunctionSimulatorArgs) {
|
|
174
183
|
this.contractStore = args.contractStore;
|
|
184
|
+
this.contractClassService = args.contractClassService;
|
|
185
|
+
this.overrides = args.overrides;
|
|
175
186
|
this.noteStore = args.noteStore;
|
|
176
187
|
this.keyStore = args.keyStore;
|
|
177
188
|
this.addressStore = args.addressStore;
|
|
@@ -208,10 +219,23 @@ export class ContractFunctionSimulator {
|
|
|
208
219
|
|
|
209
220
|
const contractAddress = request.origin;
|
|
210
221
|
|
|
211
|
-
const
|
|
222
|
+
const anchoredContractData = new AnchoredContractData(
|
|
223
|
+
this.contractStore,
|
|
224
|
+
this.contractClassService,
|
|
225
|
+
anchorBlockHeader,
|
|
226
|
+
this.overrides,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
const entryPointArtifact = await anchoredContractData.getFunctionArtifactWithDebugMetadata(
|
|
212
230
|
contractAddress,
|
|
213
231
|
request.functionSelector,
|
|
214
232
|
);
|
|
233
|
+
if (!entryPointArtifact) {
|
|
234
|
+
throw new Error(
|
|
235
|
+
`Cannot run function ${request.functionSelector} on ${contractAddress}: the contract is not registered. ` +
|
|
236
|
+
`Register it via wallet.registerContract(...).`,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
215
239
|
|
|
216
240
|
if (entryPointArtifact.functionType !== FunctionType.PRIVATE) {
|
|
217
241
|
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as private`);
|
|
@@ -239,6 +263,7 @@ export class ContractFunctionSimulator {
|
|
|
239
263
|
const privateExecutionOracle = new PrivateExecutionOracle({
|
|
240
264
|
argsHash: request.firstCallArgsHash,
|
|
241
265
|
txContext: request.txContext,
|
|
266
|
+
txRequestSalt: request.salt,
|
|
242
267
|
callContext,
|
|
243
268
|
anchorBlockHeader,
|
|
244
269
|
utilityExecutor: async (call, execScopes) => {
|
|
@@ -249,7 +274,7 @@ export class ContractFunctionSimulator {
|
|
|
249
274
|
executionCache: HashedValuesCache.create(request.argsOfCalls),
|
|
250
275
|
noteCache,
|
|
251
276
|
taggingIndexCache,
|
|
252
|
-
|
|
277
|
+
anchoredContractData,
|
|
253
278
|
noteStore: this.noteStore,
|
|
254
279
|
keyStore: this.keyStore,
|
|
255
280
|
addressStore: this.addressStore,
|
|
@@ -289,8 +314,7 @@ export class ContractFunctionSimulator {
|
|
|
289
314
|
);
|
|
290
315
|
const simulatorTeardownTimer = new Timer();
|
|
291
316
|
|
|
292
|
-
noteCache.
|
|
293
|
-
const firstNullifierHint = noteCache.getNonceGenerator();
|
|
317
|
+
const firstNullifier = noteCache.getNonceGenerator();
|
|
294
318
|
|
|
295
319
|
const publicCallRequests = collectNested([executionResult], r =>
|
|
296
320
|
r.publicInputs.publicCallRequests
|
|
@@ -313,9 +337,9 @@ export class ContractFunctionSimulator {
|
|
|
313
337
|
}
|
|
314
338
|
|
|
315
339
|
// Not to be confused with a PrivateCallExecutionResult. This is a superset
|
|
316
|
-
// of the PrivateCallExecutionResult, containing also
|
|
340
|
+
// of the PrivateCallExecutionResult, containing also firstNullifier
|
|
317
341
|
// and publicFunctionsCalldata.
|
|
318
|
-
return new PrivateExecutionResult(executionResult,
|
|
342
|
+
return new PrivateExecutionResult(executionResult, firstNullifier, publicFunctionsCalldata);
|
|
319
343
|
} catch (err) {
|
|
320
344
|
throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
|
|
321
345
|
}
|
|
@@ -333,7 +357,20 @@ export class ContractFunctionSimulator {
|
|
|
333
357
|
scopes: AztecAddress[],
|
|
334
358
|
jobId: string,
|
|
335
359
|
): Promise<{ result: Fr[]; offchainEffects: OffchainEffect[] }> {
|
|
336
|
-
const
|
|
360
|
+
const anchoredContractData = new AnchoredContractData(
|
|
361
|
+
this.contractStore,
|
|
362
|
+
this.contractClassService,
|
|
363
|
+
anchorBlockHeader,
|
|
364
|
+
this.overrides,
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const entryPointArtifact = await anchoredContractData.getFunctionArtifactWithDebugMetadata(call.to, call.selector);
|
|
368
|
+
if (!entryPointArtifact) {
|
|
369
|
+
throw new Error(
|
|
370
|
+
`Cannot run function ${call.selector} on ${call.to}: the contract is not registered. ` +
|
|
371
|
+
`Register it via wallet.registerContract(...).`,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
337
374
|
|
|
338
375
|
if (entryPointArtifact.functionType !== FunctionType.UTILITY) {
|
|
339
376
|
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as utility`);
|
|
@@ -353,7 +390,7 @@ export class ContractFunctionSimulator {
|
|
|
353
390
|
authWitnesses: authwits,
|
|
354
391
|
capsules: [],
|
|
355
392
|
anchorBlockHeader,
|
|
356
|
-
|
|
393
|
+
anchoredContractData,
|
|
357
394
|
noteStore: this.noteStore,
|
|
358
395
|
keyStore: this.keyStore,
|
|
359
396
|
addressStore: this.addressStore,
|
|
@@ -618,12 +655,12 @@ export async function generateSimulatedProvingResult(
|
|
|
618
655
|
siloedNullifiers,
|
|
619
656
|
minRevertibleSideEffectCounter,
|
|
620
657
|
);
|
|
658
|
+
// The protocol nullifier is the nonce generator and the tx's first nullifier. It is injected by the
|
|
659
|
+
// init kernel (at counter 1, index 0) rather than emitted by any private call, so it is absent from
|
|
660
|
+
// the executed nullifiers above. Mirror the kernel here by prepending it as the first non-revertible
|
|
661
|
+
// nullifier.
|
|
621
662
|
const nonceGenerator = privateExecutionResult.firstNullifier;
|
|
622
|
-
|
|
623
|
-
nonRevertibleNullifiers.push(nonceGenerator);
|
|
624
|
-
} else if (!nonRevertibleNullifiers[0].equals(nonceGenerator)) {
|
|
625
|
-
throw new Error('The first non revertible nullifier should be equal to the nonce generator. This is a bug!');
|
|
626
|
-
}
|
|
663
|
+
nonRevertibleNullifiers.unshift(nonceGenerator);
|
|
627
664
|
|
|
628
665
|
if (isPrivateOnlyTx) {
|
|
629
666
|
// We must make the note hashes unique by using the
|
|
@@ -886,6 +923,11 @@ function meterGasUsed(data: PrivateToRollupAccumulatedData | PrivateToPublicAccu
|
|
|
886
923
|
);
|
|
887
924
|
meteredL2Gas += numContractClassLogs * L2_GAS_PER_CONTRACT_CLASS_LOG;
|
|
888
925
|
|
|
926
|
+
if (isPrivateOnlyTx) {
|
|
927
|
+
// The public data write that the private tx base rollup always performs to update the fee payer's balance.
|
|
928
|
+
meteredDAFields += PUBLIC_DATA_WRITE_LENGTH;
|
|
929
|
+
}
|
|
930
|
+
|
|
889
931
|
const meteredDAGas = meteredDAFields * DA_GAS_PER_FIELD;
|
|
890
932
|
|
|
891
933
|
if ((data as PrivateToPublicAccumulatedData).publicCallRequests) {
|
|
@@ -43,12 +43,6 @@ export class ExecutionNoteCache {
|
|
|
43
43
|
|
|
44
44
|
private inRevertiblePhase = false;
|
|
45
45
|
|
|
46
|
-
/**
|
|
47
|
-
* Whether the protocol nullifier was used for nonce generation.
|
|
48
|
-
* We don't need to use the protocol nullifier if a non-revertible nullifier is emitted.
|
|
49
|
-
*/
|
|
50
|
-
private usedProtocolNullifierForNonces: boolean | undefined;
|
|
51
|
-
|
|
52
46
|
constructor(private readonly protocolNullifier: Fr) {}
|
|
53
47
|
|
|
54
48
|
/**
|
|
@@ -64,12 +58,9 @@ export class ExecutionNoteCache {
|
|
|
64
58
|
this.inRevertiblePhase = true;
|
|
65
59
|
this.minRevertibleSideEffectCounter = minRevertibleSideEffectCounter;
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
// we don't want to use them as the nonce generator.
|
|
71
|
-
this.usedProtocolNullifierForNonces = nullifiers.length === 0;
|
|
72
|
-
const nonceGenerator = this.usedProtocolNullifierForNonces ? this.protocolNullifier : new Fr(nullifiers[0]);
|
|
61
|
+
// The protocol nullifier is always the tx's first nullifier (injected by the kernel init), so it is the
|
|
62
|
+
// nonce generator for every note hash.
|
|
63
|
+
const nonceGenerator = this.protocolNullifier;
|
|
73
64
|
|
|
74
65
|
// The existing pending notes are all non-revertible.
|
|
75
66
|
// They cannot be squashed by nullifiers emitted after minRevertibleSideEffectCounter is set.
|
|
@@ -102,14 +93,6 @@ export class ExecutionNoteCache {
|
|
|
102
93
|
return sideEffectCounter >= this.minRevertibleSideEffectCounter;
|
|
103
94
|
}
|
|
104
95
|
|
|
105
|
-
public finish() {
|
|
106
|
-
// If we never entered the revertible phase, and there are no nullifiers emitted, we need to use the protocol
|
|
107
|
-
// nullifier as the nonce generator.
|
|
108
|
-
if (!this.inRevertiblePhase) {
|
|
109
|
-
this.usedProtocolNullifierForNonces = this.getEmittedNullifiers().length === 0;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
96
|
/**
|
|
114
97
|
* Add a new note to cache.
|
|
115
98
|
* @param note - New note created during execution.
|
|
@@ -223,19 +206,15 @@ export class ExecutionNoteCache {
|
|
|
223
206
|
}
|
|
224
207
|
|
|
225
208
|
/**
|
|
226
|
-
* @returns All nullifiers
|
|
227
|
-
*
|
|
209
|
+
* @returns All nullifiers of the transaction: the protocol nullifier (the kernel-injected first nullifier),
|
|
210
|
+
* followed by those emitted by private calls.
|
|
228
211
|
*/
|
|
229
212
|
getAllNullifiers(): Fr[] {
|
|
230
|
-
|
|
231
|
-
throw new Error('usedProtocolNullifierForNonces is not set yet. Call finish() to complete the transaction.');
|
|
232
|
-
}
|
|
233
|
-
const allNullifiers = this.getEmittedNullifiers();
|
|
234
|
-
return [...(this.usedProtocolNullifierForNonces ? [this.protocolNullifier] : []), ...allNullifiers];
|
|
213
|
+
return [this.protocolNullifier, ...this.getEmittedNullifiers()];
|
|
235
214
|
}
|
|
236
215
|
|
|
237
216
|
getNonceGenerator(): Fr {
|
|
238
|
-
return this.
|
|
217
|
+
return this.protocolNullifier;
|
|
239
218
|
}
|
|
240
219
|
|
|
241
220
|
#recordNullifier(contractAddress: AztecAddress, siloedNullifier: bigint) {
|