@aztec-labs/pxe 6.0.0-rc.3 → 6.0.0

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.
Files changed (101) hide show
  1. package/dest/config/index.d.ts +1 -1
  2. package/dest/config/index.d.ts.map +1 -1
  3. package/dest/config/index.js +2 -1
  4. package/dest/contract_function_simulator/index.d.ts +2 -2
  5. package/dest/contract_function_simulator/index.d.ts.map +1 -1
  6. package/dest/contract_function_simulator/index.js +1 -1
  7. package/dest/contract_function_simulator/noir-structs/fact_collection.d.ts +6 -12
  8. package/dest/contract_function_simulator/noir-structs/fact_collection.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/noir-structs/fact_collection.js +1 -15
  10. package/dest/contract_function_simulator/oracle/oracle_registry.d.ts +5 -5
  11. package/dest/contract_function_simulator/oracle/oracle_registry.d.ts.map +1 -1
  12. package/dest/contract_function_simulator/oracle/oracle_registry.js +5 -5
  13. package/dest/contract_function_simulator/oracle/oracle_type_mappings.d.ts +3 -2
  14. package/dest/contract_function_simulator/oracle/oracle_type_mappings.d.ts.map +1 -1
  15. package/dest/contract_function_simulator/oracle/oracle_type_mappings.js +16 -2
  16. package/dest/contract_function_simulator/oracle/private_execution.d.ts +1 -1
  17. package/dest/contract_function_simulator/oracle/private_execution.d.ts.map +1 -1
  18. package/dest/contract_function_simulator/oracle/private_execution.js +1 -0
  19. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +6 -6
  20. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  21. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +3 -2
  22. package/dest/contract_function_simulator/pick_notes.d.ts +1 -1
  23. package/dest/contract_function_simulator/pick_notes.d.ts.map +1 -1
  24. package/dest/contract_function_simulator/pick_notes.js +1 -0
  25. package/dest/entrypoints/server/circuit_recording/simulator_recorder_wrapper.d.ts +1 -1
  26. package/dest/entrypoints/server/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -1
  27. package/dest/entrypoints/server/circuit_recording/simulator_recorder_wrapper.js +1 -0
  28. package/dest/entrypoints/server/utils.d.ts +1 -1
  29. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  30. package/dest/entrypoints/server/utils.js +3 -1
  31. package/dest/oracle_version.d.ts +3 -3
  32. package/dest/oracle_version.js +3 -3
  33. package/dest/private_kernel/batch_planner.d.ts +1 -1
  34. package/dest/private_kernel/batch_planner.d.ts.map +1 -1
  35. package/dest/private_kernel/batch_planner.js +1 -0
  36. package/dest/private_kernel/hints/test_utils.d.ts +1 -1
  37. package/dest/private_kernel/hints/test_utils.d.ts.map +1 -1
  38. package/dest/private_kernel/hints/test_utils.js +1 -0
  39. package/dest/private_kernel/private_kernel_execution_prover.d.ts +1 -1
  40. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  41. package/dest/private_kernel/private_kernel_execution_prover.js +1 -0
  42. package/dest/pxe.d.ts +27 -2
  43. package/dest/pxe.d.ts.map +1 -1
  44. package/dest/pxe.js +201 -128
  45. package/dest/storage/address_store/address_store.d.ts +1 -1
  46. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  47. package/dest/storage/address_store/address_store.js +1 -0
  48. package/dest/storage/backwards_compatibility_tests/kv_store_snapshot.d.ts +1 -1
  49. package/dest/storage/backwards_compatibility_tests/kv_store_snapshot.d.ts.map +1 -1
  50. package/dest/storage/backwards_compatibility_tests/kv_store_snapshot.js +1 -0
  51. package/dest/storage/backwards_compatibility_tests/schema_tests.d.ts +1 -1
  52. package/dest/storage/backwards_compatibility_tests/schema_tests.d.ts.map +1 -1
  53. package/dest/storage/backwards_compatibility_tests/schema_tests.js +23 -1
  54. package/dest/storage/contract_store/contract_store.d.ts +2 -1
  55. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  56. package/dest/storage/contract_store/contract_store.js +1 -0
  57. package/dest/storage/fact_store/fact_scope.d.ts +19 -0
  58. package/dest/storage/fact_store/fact_scope.d.ts.map +1 -0
  59. package/dest/storage/fact_store/fact_scope.js +37 -0
  60. package/dest/storage/fact_store/fact_service.d.ts +2 -2
  61. package/dest/storage/fact_store/fact_service.d.ts.map +1 -1
  62. package/dest/storage/fact_store/fact_service.js +19 -5
  63. package/dest/storage/fact_store/fact_store_keys.d.ts +6 -5
  64. package/dest/storage/fact_store/fact_store_keys.d.ts.map +1 -1
  65. package/dest/storage/fact_store/fact_store_keys.js +24 -8
  66. package/dest/storage/fact_store/index.d.ts +2 -1
  67. package/dest/storage/fact_store/index.d.ts.map +1 -1
  68. package/dest/storage/fact_store/index.js +1 -0
  69. package/dest/storage/fact_store/stored_fact.d.ts +9 -3
  70. package/dest/storage/fact_store/stored_fact.d.ts.map +1 -1
  71. package/dest/storage/fact_store/stored_fact.js +45 -3
  72. package/dest/storage/store_identity.d.ts +1 -1
  73. package/dest/storage/store_identity.d.ts.map +1 -1
  74. package/dest/storage/store_identity.js +1 -0
  75. package/package.json +21 -20
  76. package/src/config/index.ts +2 -1
  77. package/src/contract_function_simulator/index.ts +1 -0
  78. package/src/contract_function_simulator/noir-structs/fact_collection.ts +6 -22
  79. package/src/contract_function_simulator/oracle/oracle_registry.ts +5 -4
  80. package/src/contract_function_simulator/oracle/oracle_type_mappings.ts +15 -1
  81. package/src/contract_function_simulator/oracle/private_execution.ts +1 -0
  82. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +8 -7
  83. package/src/contract_function_simulator/pick_notes.ts +1 -0
  84. package/src/entrypoints/server/circuit_recording/simulator_recorder_wrapper.ts +1 -0
  85. package/src/entrypoints/server/utils.ts +4 -2
  86. package/src/oracle_version.ts +3 -3
  87. package/src/private_kernel/batch_planner.ts +1 -0
  88. package/src/private_kernel/hints/test_utils.ts +1 -0
  89. package/src/private_kernel/private_kernel_execution_prover.ts +1 -0
  90. package/src/pxe.ts +291 -189
  91. package/src/storage/address_store/address_store.ts +1 -0
  92. package/src/storage/backwards_compatibility_tests/__snapshots__/FactStore.json +20 -0
  93. package/src/storage/backwards_compatibility_tests/kv_store_snapshot.ts +1 -0
  94. package/src/storage/backwards_compatibility_tests/schema_tests.ts +18 -1
  95. package/src/storage/contract_store/contract_store.ts +1 -0
  96. package/src/storage/fact_store/fact_scope.ts +39 -0
  97. package/src/storage/fact_store/fact_service.ts +20 -5
  98. package/src/storage/fact_store/fact_store_keys.ts +28 -15
  99. package/src/storage/fact_store/index.ts +1 -0
  100. package/src/storage/fact_store/stored_fact.ts +50 -14
  101. package/src/storage/store_identity.ts +1 -0
package/src/pxe.ts CHANGED
@@ -2,6 +2,7 @@ import type { PrivateEventFilter } from '@aztec-labs/aztec.js/wallet';
2
2
  import { BlockNumber } from '@aztec-labs/foundation/branded-types';
3
3
  import { Fr } from '@aztec-labs/foundation/curves/bn254';
4
4
  import { Point } from '@aztec-labs/foundation/curves/grumpkin';
5
+ import { inspect } from '@aztec-labs/foundation/inspect';
5
6
  import { type Logger, type LoggerBindings, createLogger } from '@aztec-labs/foundation/log';
6
7
  import { allToCompletion } from '@aztec-labs/foundation/promise';
7
8
  import { Timer } from '@aztec-labs/foundation/timer';
@@ -55,7 +56,6 @@ import {
55
56
  TxSimulationResult,
56
57
  UtilityExecutionResult,
57
58
  } from '@aztec-labs/stdlib/tx';
58
- import { inspect } from 'util';
59
59
 
60
60
  import { BlockSynchronizer } from './block_synchronizer/index.js';
61
61
  import type { PXEConfig } from './config/index.js';
@@ -72,7 +72,7 @@ import { PrivateEventFilterValidator } from './events/private_event_filter_valid
72
72
  import type { ExecutionHooks } from './hooks/index.js';
73
73
  import { TxResolverService } from './messages/tx_resolver_service.js';
74
74
  import { type CachingAztecNode, withCache } from './node/caching_aztec_node.js';
75
- import { OperationQueue } from './operation_queue.js';
75
+ import { OperationQueue, type SyncedOperationContext } from './operation_queue.js';
76
76
  import {
77
77
  PrivateKernelExecutionProver,
78
78
  type PrivateKernelExecutionProverConfig,
@@ -142,6 +142,18 @@ export type SimulateTxOpts = {
142
142
  senderForTags?: AztecAddress;
143
143
  };
144
144
 
145
+ /** Options for PXE.simulateAndProveTx. */
146
+ export type SimulateAndProveTxOpts = SimulateTxOpts & {
147
+ /**
148
+ * Returns the request to prove and its options, given the simulation's result. Runs inside the operation: other PXE
149
+ * operations wait for it to return, and it must not call the PXE itself, since that call would wait for this
150
+ * operation to end.
151
+ */
152
+ prepareProve: (
153
+ simulationResult: TxSimulationResult,
154
+ ) => Promise<{ proveTxRequest: TxExecutionRequest; proveOpts: ProveTxOpts }>;
155
+ };
156
+
145
157
  /** Options for PXE.executeUtility. */
146
158
  export type ExecuteUtilityOpts = {
147
159
  /** The authentication witnesses required for the function call. */
@@ -648,6 +660,212 @@ export class PXE {
648
660
  return await kernelTraceProver.proveWithKernels(txExecutionRequest.toTxRequest(), privateExecutionResult, config);
649
661
  }
650
662
 
663
+ /** Simulates `txRequest` within an already-open operation, using its anchor block and change set. */
664
+ async #simulateTx(
665
+ { changeSetId, syncTime, anchorBlockHeader, recording, totalMs }: SyncedOperationContext,
666
+ txRequest: TxExecutionRequest,
667
+ {
668
+ simulatePublic,
669
+ skipTxValidation = false,
670
+ skipFeeEnforcement = false,
671
+ skipKernels = true,
672
+ overrides,
673
+ scopes,
674
+ senderForTags,
675
+ }: SimulateTxOpts,
676
+ ): Promise<TxSimulationResult> {
677
+ const txInfo = {
678
+ origin: txRequest.origin,
679
+ functionSelector: txRequest.functionSelector,
680
+ simulatePublic,
681
+ chainId: txRequest.txContext.chainId,
682
+ version: txRequest.txContext.version,
683
+ authWitnesses: txRequest.authWitnesses.map(w => w.requestHash),
684
+ };
685
+ this.log.info(
686
+ `Simulating transaction execution request to ${txRequest.functionSelector} at ${txRequest.origin}`,
687
+ txInfo,
688
+ );
689
+
690
+ if (overrides?.contracts && Object.keys(overrides.contracts).length > 0 && !skipKernels) {
691
+ throw new Error(
692
+ 'Simulating with overridden contracts is not compatible with kernel execution. Please set skipKernels to true when simulating with overridden contracts.',
693
+ );
694
+ }
695
+ const contractFunctionSimulator = this.#getSimulatorForTx(overrides);
696
+
697
+ // Execution of private functions only; no proving, and no kernel logic.
698
+ const privateExecutionResult = await this.#executePrivate({
699
+ contractFunctionSimulator,
700
+ txRequest,
701
+ anchorBlockHeader,
702
+ scopes,
703
+ changeSetId,
704
+ senderForTags,
705
+ });
706
+
707
+ let publicInputs: PrivateKernelTailCircuitPublicInputs | undefined;
708
+ let executionSteps: PrivateExecutionStep[] = [];
709
+
710
+ if (skipKernels) {
711
+ ({ publicInputs, executionSteps } = await generateSimulatedProvingResult(
712
+ privateExecutionResult,
713
+ (addr, sel) => this.#getDebugFunctionName(addr, sel, anchorBlockHeader),
714
+ this.node,
715
+ ));
716
+ } else {
717
+ // Kernel logic, plus proving of all private functions and kernels.
718
+ ({ publicInputs, executionSteps } = await this.#prove(
719
+ txRequest,
720
+ this.proofCreator,
721
+ privateExecutionResult,
722
+ anchorBlockHeader,
723
+ {
724
+ simulate: true,
725
+ skipFeeEnforcement,
726
+ profileMode: 'none',
727
+ },
728
+ ));
729
+ }
730
+
731
+ const privateSimulationResult = new PrivateSimulationResult(privateExecutionResult, publicInputs);
732
+ const simulatedTx = await privateSimulationResult.toSimulatedTx();
733
+ let publicSimulationTime: number | undefined;
734
+ let publicOutput: PublicSimulationOutput | undefined;
735
+ if (simulatePublic && publicInputs.forPublic) {
736
+ const publicSimulationTimer = new Timer();
737
+ publicOutput = await this.#simulatePublicCalls(simulatedTx, skipFeeEnforcement, overrides);
738
+ publicSimulationTime = publicSimulationTimer.ms();
739
+ if (publicOutput?.debugLogs?.length) {
740
+ await displayDebugLogs(publicOutput.debugLogs, addr => this.#getDebugContractName(addr, anchorBlockHeader));
741
+ }
742
+ }
743
+
744
+ let validationTime: number | undefined;
745
+ if (!skipTxValidation) {
746
+ const validationTimer = new Timer();
747
+ const validationResult = await this.node.isValidTx(simulatedTx, { isSimulation: true, skipFeeEnforcement });
748
+ validationTime = validationTimer.ms();
749
+ if (validationResult.result === 'invalid') {
750
+ const reason = validationResult.reason.length > 0 ? ` Reason: ${validationResult.reason.join(', ')}` : '';
751
+ throw new Error(`The simulated transaction is unable to be added to state and is invalid.${reason}`);
752
+ }
753
+ }
754
+
755
+ const txHash = simulatedTx.getTxHash();
756
+
757
+ const totalTime = totalMs();
758
+
759
+ const perFunction = executionSteps.map(({ functionName, timings: { witgen, oracles } }) => ({
760
+ functionName,
761
+ time: witgen,
762
+ oracles,
763
+ }));
764
+
765
+ const timings: SimulationTimings = {
766
+ total: totalTime,
767
+ sync: syncTime,
768
+ publicSimulation: publicSimulationTime,
769
+ validation: validationTime,
770
+ perFunction,
771
+ unaccounted:
772
+ totalTime -
773
+ (syncTime +
774
+ (publicSimulationTime ?? 0) +
775
+ (validationTime ?? 0) +
776
+ perFunction.reduce((acc, { time }) => acc + time, 0)),
777
+ };
778
+
779
+ this.log.info(`Simulation completed for ${txHash.toString()} in ${totalTime}ms`, {
780
+ txHash,
781
+ ...txInfo,
782
+ ...(publicOutput
783
+ ? {
784
+ gasUsed: publicOutput.gasUsed,
785
+ revertCode: publicOutput.txEffect.revertCode.getCode(),
786
+ revertReason: publicOutput.revertReason,
787
+ }
788
+ : {}),
789
+ });
790
+
791
+ return TxSimulationResult.fromPrivateSimulationResultAndPublicOutput(privateSimulationResult, publicOutput, {
792
+ timings,
793
+ nodeRPCCalls: recording.stats(),
794
+ });
795
+ }
796
+
797
+ /** Describes a simulation of `txRequest` for the errors it throws. */
798
+ #simulateTxErrorContext(txRequest: TxExecutionRequest, opts: SimulateTxOpts): string[] {
799
+ return [
800
+ inspect(txRequest),
801
+ `simulatePublic=${opts.simulatePublic}`,
802
+ `skipTxValidation=${opts.skipTxValidation ?? false}`,
803
+ `scopes=${opts.scopes.map(s => s.toString()).join(', ')}`,
804
+ ];
805
+ }
806
+
807
+ /**
808
+ * Proves `privateExecutionResult`, the private execution of `txRequest`, within an already-open operation, and stages
809
+ * the sender tagging indices the tx uses.
810
+ */
811
+ async #proveTx(
812
+ { changeSetId, syncTime, anchorBlockHeader, recording, totalMs }: SyncedOperationContext,
813
+ txRequest: TxExecutionRequest,
814
+ privateExecutionResult: PrivateExecutionResult,
815
+ ): Promise<TxProvingResult> {
816
+ const {
817
+ publicInputs,
818
+ chonkProof,
819
+ executionSteps,
820
+ timings: { proving } = {},
821
+ } = await this.#prove(txRequest, this.proofCreator, privateExecutionResult, anchorBlockHeader, {
822
+ simulate: false,
823
+ skipFeeEnforcement: false,
824
+ profileMode: 'none',
825
+ });
826
+
827
+ const totalTime = totalMs();
828
+
829
+ const perFunction = executionSteps.map(({ functionName, timings: { witgen, oracles } }) => ({
830
+ functionName,
831
+ time: witgen,
832
+ oracles,
833
+ }));
834
+
835
+ const timings: ProvingTimings = {
836
+ total: totalTime,
837
+ sync: syncTime,
838
+ proving,
839
+ perFunction,
840
+ unaccounted:
841
+ totalTime - ((syncTime ?? 0) + (proving ?? 0) + perFunction.reduce((acc, { time }) => acc + time, 0)),
842
+ };
843
+
844
+ this.log.debug(`Proving completed in ${totalTime}ms`, { timings });
845
+
846
+ const txProvingResult = new TxProvingResult(privateExecutionResult, publicInputs, chonkProof!, {
847
+ timings,
848
+ nodeRPCCalls: recording.stats(),
849
+ });
850
+
851
+ // We keep track of which tagging indices we've used in this tx so that we don't repeat them in future txs
852
+ // (which would link them) without having to rely on this tx being mined (and us seeing the indices being used
853
+ // onchain).
854
+ // Note that this must happen _after_ proving as it requires the proof's public inputs, from which the kernels
855
+ // may have removed some logs due to note-nullifier squashing - this may lead to range of tagging indices we've
856
+ // actually used to being reduced.
857
+ await persistSenderTaggingIndexRangesForTx(
858
+ this.senderTaggingStore,
859
+ privateExecutionResult.entrypoint.taggingIndexRanges,
860
+ publicInputs,
861
+ () => txProvingResult.getTxHash(),
862
+ changeSetId,
863
+ this.log,
864
+ );
865
+
866
+ return txProvingResult;
867
+ }
868
+
651
869
  // Public API
652
870
 
653
871
  /**
@@ -976,68 +1194,17 @@ export class PXE {
976
1194
  // We disable proving concurrently mostly out of caution, since it accesses some of our stores. Proving is so
977
1195
  // computationally demanding that it'd be rare for someone to try to do it concurrently regardless.
978
1196
  return this.operationQueue.runSynced(
979
- async ({ changeSetId, syncTime, anchorBlockHeader, recording, totalMs }) => {
1197
+ async ctx => {
980
1198
  const contractFunctionSimulator = this.#getSimulatorForTx();
981
1199
  privateExecutionResult = await this.#executePrivate({
982
1200
  contractFunctionSimulator,
983
1201
  txRequest,
984
- anchorBlockHeader,
1202
+ anchorBlockHeader: ctx.anchorBlockHeader,
985
1203
  scopes,
986
- changeSetId,
1204
+ changeSetId: ctx.changeSetId,
987
1205
  senderForTags,
988
1206
  });
989
-
990
- const {
991
- publicInputs,
992
- chonkProof,
993
- executionSteps,
994
- timings: { proving } = {},
995
- } = await this.#prove(txRequest, this.proofCreator, privateExecutionResult, anchorBlockHeader, {
996
- simulate: false,
997
- skipFeeEnforcement: false,
998
- profileMode: 'none',
999
- });
1000
-
1001
- const totalTime = totalMs();
1002
-
1003
- const perFunction = executionSteps.map(({ functionName, timings: { witgen, oracles } }) => ({
1004
- functionName,
1005
- time: witgen,
1006
- oracles,
1007
- }));
1008
-
1009
- const timings: ProvingTimings = {
1010
- total: totalTime,
1011
- sync: syncTime,
1012
- proving,
1013
- perFunction,
1014
- unaccounted:
1015
- totalTime - ((syncTime ?? 0) + (proving ?? 0) + perFunction.reduce((acc, { time }) => acc + time, 0)),
1016
- };
1017
-
1018
- this.log.debug(`Proving completed in ${totalTime}ms`, { timings });
1019
-
1020
- const txProvingResult = new TxProvingResult(privateExecutionResult, publicInputs, chonkProof!, {
1021
- timings,
1022
- nodeRPCCalls: recording.stats(),
1023
- });
1024
-
1025
- // We keep track of which tagging indices we've used in this tx so that we don't repeat them in future txs
1026
- // (which would link them) without having to rely on this tx being mined (and us seeing the indices being used
1027
- // onchain).
1028
- // Note that this must happen _after_ proving as it requires the proof's public inputs, from which the kernels
1029
- // may have removed some logs due to note-nullifier squashing - this may lead to range of tagging indices we've
1030
- // actually used to being reduced.
1031
- await persistSenderTaggingIndexRangesForTx(
1032
- this.senderTaggingStore,
1033
- privateExecutionResult.entrypoint.taggingIndexRanges,
1034
- publicInputs,
1035
- () => txProvingResult.getTxHash(),
1036
- changeSetId,
1037
- this.log,
1038
- );
1039
-
1040
- return txProvingResult;
1207
+ return this.#proveTx(ctx, txRequest, privateExecutionResult);
1041
1208
  },
1042
1209
  { errorContext: () => [inspect(txRequest), inspect(privateExecutionResult)] },
1043
1210
  );
@@ -1140,148 +1307,83 @@ export class PXE {
1140
1307
  *
1141
1308
  * TODO(#7456) Prevent msgSender being defined here for the first call
1142
1309
  */
1143
- public simulateTx(
1144
- txRequest: TxExecutionRequest,
1145
- {
1146
- simulatePublic,
1147
- skipTxValidation = false,
1148
- skipFeeEnforcement = false,
1149
- skipKernels = true,
1150
- overrides,
1151
- scopes,
1152
- senderForTags,
1153
- }: SimulateTxOpts,
1154
- ): Promise<TxSimulationResult> {
1310
+ public simulateTx(txRequest: TxExecutionRequest, opts: SimulateTxOpts): Promise<TxSimulationResult> {
1155
1311
  // We disable concurrent simulations since those might execute oracles which read and write to the PXE stores (e.g.
1156
1312
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
1157
1313
  // delete the same read value, or reading values that another simulation is currently modifying).
1158
- return this.operationQueue.runSynced(
1159
- async ({ changeSetId, syncTime, anchorBlockHeader, recording, totalMs }) => {
1160
- const txInfo = {
1161
- origin: txRequest.origin,
1162
- functionSelector: txRequest.functionSelector,
1163
- simulatePublic,
1164
- chainId: txRequest.txContext.chainId,
1165
- version: txRequest.txContext.version,
1166
- authWitnesses: txRequest.authWitnesses.map(w => w.requestHash),
1167
- };
1168
- this.log.info(
1169
- `Simulating transaction execution request to ${txRequest.functionSelector} at ${txRequest.origin}`,
1170
- txInfo,
1171
- );
1172
-
1173
- if (overrides?.contracts && Object.keys(overrides.contracts).length > 0 && !skipKernels) {
1174
- throw new Error(
1175
- 'Simulating with overridden contracts is not compatible with kernel execution. Please set skipKernels to true when simulating with overridden contracts.',
1176
- );
1177
- }
1178
- const contractFunctionSimulator = this.#getSimulatorForTx(overrides);
1179
-
1180
- // Execution of private functions only; no proving, and no kernel logic.
1181
- const privateExecutionResult = await this.#executePrivate({
1182
- contractFunctionSimulator,
1183
- txRequest,
1184
- anchorBlockHeader,
1185
- scopes,
1186
- changeSetId,
1187
- senderForTags,
1188
- });
1314
+ return this.operationQueue.runSynced(ctx => this.#simulateTx(ctx, txRequest, opts), {
1315
+ errorContext: () => this.#simulateTxErrorContext(txRequest, opts),
1316
+ });
1317
+ }
1189
1318
 
1190
- let publicInputs: PrivateKernelTailCircuitPublicInputs | undefined;
1191
- let executionSteps: PrivateExecutionStep[] = [];
1319
+ /**
1320
+ * Combination of `simulateTx` and `proveTx`: a simulation is executed, its results inspected to produce a provable
1321
+ * transaction, followed by witness generation and proof.
1322
+ *
1323
+ * The entire sequence runs as a single atomic PXE operation, with no private state sync between simulation and
1324
+ * witness generation, resulting in them running over identical environments (e.g. the note store, tagging indices,
1325
+ * etc.).
1326
+ *
1327
+ * The simulation result is passed to a `prepareProve` callback, which then returns the input to the equivalent
1328
+ * `proveTx` call. Due to the atomic nature of this function, `prepareProve` must NOT call any PXE function, as that
1329
+ * would result in a deadlock.
1330
+ */
1331
+ public simulateAndProveTx(
1332
+ simulationTxRequest: TxExecutionRequest,
1333
+ { prepareProve, ...simulateOpts }: SimulateAndProveTxOpts,
1334
+ ): Promise<TxProvingResult> {
1335
+ let proveTxRequest: TxExecutionRequest | undefined;
1336
+ let privateExecutionResult: PrivateExecutionResult | undefined;
1337
+
1338
+ // We run the entire thing as a single operation, which blocks the queue. There are two other possible paths we
1339
+ // could take here:
1340
+ // a) decouple the queue from a changeset to commit. This would let us hold the queue and have two changesets, one
1341
+ // for the simulation and its corresponding sync, and one for the prove. In case of failure of either the
1342
+ // `prepareProve` callback (e.g. user rejection) or the proof itself, we'd still have committed the simulation
1343
+ // work, including the private state sync, which could save time in future runs.
1344
+ // b) make this be two separate jobs, releasing the queue in between, but holding a second lock which would prevent
1345
+ // PXE from updating its anchor block and therefore re-syncing contracts. In the case of a long-running
1346
+ // callback, other unrelated operations (e.g. utility call invocations) would still be able to go through.
1347
+ //
1348
+ // We're not currently taking either of these paths due to uncertainty on this even being a good primitive for PXE
1349
+ // to expose to wallets, and to not invest too heavily in this direction (as both approaches would require
1350
+ // refactoring). Additionally, the gains those approaches provide are currently not valuable: for a) we expect it to
1351
+ // be unlikely for a follow-up simulation to be executed on the same anchor block as the first one, so committing
1352
+ // the sync achieves little if anything, and for b) the callback is expected to be fairly quick, and for there to be
1353
+ // no need to execute any other PXE calls while it runs.
1192
1354
 
1193
- if (skipKernels) {
1194
- ({ publicInputs, executionSteps } = await generateSimulatedProvingResult(
1195
- privateExecutionResult,
1196
- (addr, sel) => this.#getDebugFunctionName(addr, sel, anchorBlockHeader),
1197
- this.node,
1198
- ));
1199
- } else {
1200
- // Kernel logic, plus proving of all private functions and kernels.
1201
- ({ publicInputs, executionSteps } = await this.#prove(
1202
- txRequest,
1203
- this.proofCreator,
1355
+ return this.operationQueue.runSynced(
1356
+ async ctx => {
1357
+ const simulationResult = await this.#simulateTx(ctx, simulationTxRequest, simulateOpts);
1358
+ let proveOpts: ProveTxOpts;
1359
+ ({ proveTxRequest, proveOpts } = await prepareProve(simulationResult));
1360
+
1361
+ // The proving result's timings and node stats must cover proving alone, not the simulation before it.
1362
+ const provingTimer = new Timer();
1363
+ const recording = this.node.startRecording();
1364
+ try {
1365
+ privateExecutionResult = await this.#executePrivate({
1366
+ contractFunctionSimulator: this.#getSimulatorForTx(),
1367
+ txRequest: proveTxRequest,
1368
+ anchorBlockHeader: ctx.anchorBlockHeader,
1369
+ scopes: proveOpts.scopes,
1370
+ changeSetId: ctx.changeSetId,
1371
+ senderForTags: proveOpts.senderForTags,
1372
+ });
1373
+ return await this.#proveTx(
1374
+ { ...ctx, syncTime: 0, recording, totalMs: () => provingTimer.ms() },
1375
+ proveTxRequest,
1204
1376
  privateExecutionResult,
1205
- anchorBlockHeader,
1206
- {
1207
- simulate: true,
1208
- skipFeeEnforcement,
1209
- profileMode: 'none',
1210
- },
1211
- ));
1212
- }
1213
-
1214
- const privateSimulationResult = new PrivateSimulationResult(privateExecutionResult, publicInputs);
1215
- const simulatedTx = await privateSimulationResult.toSimulatedTx();
1216
- let publicSimulationTime: number | undefined;
1217
- let publicOutput: PublicSimulationOutput | undefined;
1218
- if (simulatePublic && publicInputs.forPublic) {
1219
- const publicSimulationTimer = new Timer();
1220
- publicOutput = await this.#simulatePublicCalls(simulatedTx, skipFeeEnforcement, overrides);
1221
- publicSimulationTime = publicSimulationTimer.ms();
1222
- if (publicOutput?.debugLogs?.length) {
1223
- await displayDebugLogs(publicOutput.debugLogs, addr => this.#getDebugContractName(addr, anchorBlockHeader));
1224
- }
1225
- }
1226
-
1227
- let validationTime: number | undefined;
1228
- if (!skipTxValidation) {
1229
- const validationTimer = new Timer();
1230
- const validationResult = await this.node.isValidTx(simulatedTx, { isSimulation: true, skipFeeEnforcement });
1231
- validationTime = validationTimer.ms();
1232
- if (validationResult.result === 'invalid') {
1233
- const reason = validationResult.reason.length > 0 ? ` Reason: ${validationResult.reason.join(', ')}` : '';
1234
- throw new Error(`The simulated transaction is unable to be added to state and is invalid.${reason}`);
1235
- }
1377
+ );
1378
+ } finally {
1379
+ recording.stop();
1236
1380
  }
1237
-
1238
- const txHash = simulatedTx.getTxHash();
1239
-
1240
- const totalTime = totalMs();
1241
-
1242
- const perFunction = executionSteps.map(({ functionName, timings: { witgen, oracles } }) => ({
1243
- functionName,
1244
- time: witgen,
1245
- oracles,
1246
- }));
1247
-
1248
- const timings: SimulationTimings = {
1249
- total: totalTime,
1250
- sync: syncTime,
1251
- publicSimulation: publicSimulationTime,
1252
- validation: validationTime,
1253
- perFunction,
1254
- unaccounted:
1255
- totalTime -
1256
- (syncTime +
1257
- (publicSimulationTime ?? 0) +
1258
- (validationTime ?? 0) +
1259
- perFunction.reduce((acc, { time }) => acc + time, 0)),
1260
- };
1261
-
1262
- this.log.info(`Simulation completed for ${txHash.toString()} in ${totalTime}ms`, {
1263
- txHash,
1264
- ...txInfo,
1265
- ...(publicOutput
1266
- ? {
1267
- gasUsed: publicOutput.gasUsed,
1268
- revertCode: publicOutput.txEffect.revertCode.getCode(),
1269
- revertReason: publicOutput.revertReason,
1270
- }
1271
- : {}),
1272
- });
1273
-
1274
- return TxSimulationResult.fromPrivateSimulationResultAndPublicOutput(privateSimulationResult, publicOutput, {
1275
- timings,
1276
- nodeRPCCalls: recording.stats(),
1277
- });
1278
1381
  },
1279
1382
  {
1280
1383
  errorContext: () => [
1281
- inspect(txRequest),
1282
- `simulatePublic=${simulatePublic}`,
1283
- `skipTxValidation=${skipTxValidation}`,
1284
- `scopes=${scopes.map(s => s.toString()).join(', ')}`,
1384
+ ...this.#simulateTxErrorContext(simulationTxRequest, simulateOpts),
1385
+ inspect(proveTxRequest),
1386
+ inspect(privateExecutionResult),
1285
1387
  ],
1286
1388
  },
1287
1389
  );
@@ -3,6 +3,7 @@ import { allToCompletion } from '@aztec-labs/foundation/promise';
3
3
  import type { AztecAsyncArray, AztecAsyncKVStore, AztecAsyncMap } from '@aztec-labs/kv-store';
4
4
  import type { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
5
5
  import { CompleteAddress } from '@aztec-labs/stdlib/contract';
6
+ import { Buffer } from 'buffer';
6
7
 
7
8
  export class AddressStore {
8
9
  #store: AztecAsyncKVStore;
@@ -11,6 +11,14 @@
11
11
  {
12
12
  "key": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:0x0000000000000000000000000000000000000000000000000000000000000001:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000bb:0x0000000000000000000000000000000000000000000000000000000000000002:0x00df3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b811:5:0x0000000000000000000000000000000000000000000000000000000000000001",
13
13
  "value": "00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000bb00000000000000000000000000000000000000000000000000000000000000020000000001000000050000000000000000000000000000000000000000000000000000000000000001"
14
+ },
15
+ {
16
+ "key": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc:0x0000000000000000000000000000000000000000000000000000000000000004:0x00faf6101c52e25d1323a41aa4ccd9a90b08c2478e9385ecc452b0a4b1c310f1:none",
17
+ "value": "00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000cc00000000000000000000000000000000000000000000000000000000000000040000000100000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000001"
18
+ },
19
+ {
20
+ "key": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc:0x0000000000000000000000000000000000000000000000000000000000000005:0x0041d2064a35f0d962fdd7ecb949315876208dd803a418635fd5b4f669286bf9:7:0x0000000000000000000000000000000000000000000000000000000000000003",
21
+ "value": "00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000cc000000000000000000000000000000000000000000000000000000000000000500000001000000000000000000000000000000000000000000000000000000000000000a0100000007000000000000000000000000000000000000000000000000000000000000000301"
14
22
  }
15
23
  ],
16
24
  "facts_by_collection": [
@@ -25,6 +33,14 @@
25
33
  {
26
34
  "key": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:0x0000000000000000000000000000000000000000000000000000000000000001:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000bb",
27
35
  "value": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:0x0000000000000000000000000000000000000000000000000000000000000001:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000bb:0x0000000000000000000000000000000000000000000000000000000000000002:0x00df3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b811:5:0x0000000000000000000000000000000000000000000000000000000000000001"
36
+ },
37
+ {
38
+ "key": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc",
39
+ "value": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc:0x0000000000000000000000000000000000000000000000000000000000000004:0x00faf6101c52e25d1323a41aa4ccd9a90b08c2478e9385ecc452b0a4b1c310f1:none"
40
+ },
41
+ {
42
+ "key": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc",
43
+ "value": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc:0x0000000000000000000000000000000000000000000000000000000000000005:0x0041d2064a35f0d962fdd7ecb949315876208dd803a418635fd5b4f669286bf9:7:0x0000000000000000000000000000000000000000000000000000000000000003"
28
44
  }
29
45
  ],
30
46
  "facts_by_block": [
@@ -35,6 +51,10 @@
35
51
  {
36
52
  "key": "num:6",
37
53
  "value": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:0x0000000000000000000000000000000000000000000000000000000000000001:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000aa:0x0000000000000000000000000000000000000000000000000000000000000003:0x00eebee6df09ad503384e049aea9fbf0c0db2f5af46673e550694c6564d973f7:6:0x0000000000000000000000000000000000000000000000000000000000000002"
54
+ },
55
+ {
56
+ "key": "num:7",
57
+ "value": "utf8:0x0000000000000000000000000000000000000000000000000000000000000064:public:0x0000000000000000000000000000000000000000000000000000000000000007:0x00000000000000000000000000000000000000000000000000000000000000cc:0x0000000000000000000000000000000000000000000000000000000000000005:0x0041d2064a35f0d962fdd7ecb949315876208dd803a418635fd5b4f669286bf9:7:0x0000000000000000000000000000000000000000000000000000000000000003"
38
58
  }
39
59
  ]
40
60
  }
@@ -1,4 +1,5 @@
1
1
  import type { AztecAsyncArray, AztecAsyncMap, AztecAsyncSingleton, Key } from '@aztec-labs/kv-store';
2
+ import { Buffer } from 'buffer';
2
3
 
3
4
  /**
4
5
  * This file contains helpers that produce stable, snapshot-friendly text representations of our kv-stores'
@@ -48,11 +48,13 @@ import {
48
48
  TxEffect,
49
49
  TxHash,
50
50
  } from '@aztec-labs/stdlib/tx';
51
+ import { Buffer } from 'buffer';
51
52
 
52
53
  import { AddressStore } from '../address_store/address_store.js';
53
54
  import { AnchorBlockStore } from '../anchor_block_store/index.js';
54
55
  import { CapsuleStore } from '../capsule_store/capsule_store.js';
55
56
  import { ContractStore } from '../contract_store/contract_store.js';
57
+ import type { FactScope } from '../fact_store/fact_scope.js';
56
58
  import { FactStore } from '../fact_store/fact_store.js';
57
59
  import { FactCollectionKey } from '../fact_store/fact_store_keys.js';
58
60
  import { NoteStore } from '../note_store/note_store.js';
@@ -240,7 +242,7 @@ export const SCHEMA_TESTS: readonly SchemaTest[] = [
240
242
  const changeSetId = 'fixture-change-set';
241
243
  factStore.beginChangeSet(changeSetId);
242
244
  const contract = AztecAddress.fromBigIntUnsafe(100n);
243
- const scope = AztecAddress.fromBigIntUnsafe(1n);
245
+ const scope: FactScope = { type: 'account', account: AztecAddress.fromBigIntUnsafe(1n) };
244
246
  const factCollectionTypeId = new Fr(7n);
245
247
  const keyA = FactCollectionKey.from({
246
248
  contractAddress: contract,
@@ -265,6 +267,21 @@ export const SCHEMA_TESTS: readonly SchemaTest[] = [
265
267
  // A collection with a non-retractable and a retractable fact.
266
268
  await factStore.recordFact(keyB, new Fr(1n), [new Fr(9n)], undefined, changeSetId);
267
269
  await factStore.recordFact(keyB, new Fr(2n), [], { blockNumber: 5, blockHash: new Fr(1n) }, changeSetId);
270
+ // A collection under the public scope, with a non-retractable and a retractable fact.
271
+ const publicKey = FactCollectionKey.from({
272
+ contractAddress: contract,
273
+ scope: { type: 'public' },
274
+ factCollectionTypeId,
275
+ factCollectionId: new Fr(0xccn),
276
+ });
277
+ await factStore.recordFact(publicKey, new Fr(4n), [new Fr(8n)], undefined, changeSetId);
278
+ await factStore.recordFact(
279
+ publicKey,
280
+ new Fr(5n),
281
+ [new Fr(10n)],
282
+ { blockNumber: 7, blockHash: new Fr(3n) },
283
+ changeSetId,
284
+ );
268
285
  await kvStore.transactionAsync(() => factStore.commitChangeSet(changeSetId));
269
286
  },
270
287
  snapshotStore: async kvStore => ({
@@ -28,6 +28,7 @@ import {
28
28
  SerializableContractInstancePreimage,
29
29
  getContractClassFromArtifact,
30
30
  } from '@aztec-labs/stdlib/contract';
31
+ import { Buffer } from 'buffer';
31
32
 
32
33
  import { PrivateFunctionsTree } from './private_functions_tree.js';
33
34