@aztec/simulator 0.0.1-commit.3469e52 → 0.0.1-commit.59e663cd

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 (67) hide show
  1. package/dest/public/avm/avm_context.d.ts +3 -3
  2. package/dest/public/avm/avm_context.d.ts.map +1 -1
  3. package/dest/public/avm/avm_contract_call_result.d.ts +6 -6
  4. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
  5. package/dest/public/avm/avm_contract_call_result.js +3 -3
  6. package/dest/public/avm/avm_execution_environment.d.ts +6 -5
  7. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
  8. package/dest/public/avm/avm_machine_state.d.ts +6 -5
  9. package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
  10. package/dest/public/avm/avm_machine_state.js +3 -2
  11. package/dest/public/avm/avm_memory_types.d.ts +1 -1
  12. package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
  13. package/dest/public/avm/avm_memory_types.js +3 -0
  14. package/dest/public/avm/avm_simulator.d.ts +3 -2
  15. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  16. package/dest/public/avm/avm_simulator.js +5 -4
  17. package/dest/public/avm/calldata.d.ts +51 -0
  18. package/dest/public/avm/calldata.d.ts.map +1 -0
  19. package/dest/public/avm/calldata.js +63 -0
  20. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +1 -1
  21. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
  22. package/dest/public/avm/fixtures/avm_simulation_tester.js +3 -2
  23. package/dest/public/avm/fixtures/initializers.d.ts +1 -1
  24. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
  25. package/dest/public/avm/fixtures/initializers.js +2 -1
  26. package/dest/public/avm/opcodes/accrued_substate.d.ts +1 -1
  27. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  28. package/dest/public/avm/opcodes/accrued_substate.js +4 -0
  29. package/dest/public/avm/opcodes/contract.d.ts +1 -1
  30. package/dest/public/avm/opcodes/contract.d.ts.map +1 -1
  31. package/dest/public/avm/opcodes/contract.js +4 -4
  32. package/dest/public/avm/opcodes/external_calls.d.ts +1 -1
  33. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
  34. package/dest/public/avm/opcodes/external_calls.js +7 -7
  35. package/dest/public/avm/opcodes/memory.js +1 -1
  36. package/dest/public/avm/opcodes/storage.d.ts +13 -12
  37. package/dest/public/avm/opcodes/storage.d.ts.map +1 -1
  38. package/dest/public/avm/opcodes/storage.js +30 -20
  39. package/dest/public/debug_fn_name.d.ts +4 -4
  40. package/dest/public/debug_fn_name.d.ts.map +1 -1
  41. package/dest/public/debug_fn_name.js +7 -5
  42. package/dest/public/fixtures/opcode_spammer.d.ts +1 -1
  43. package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -1
  44. package/dest/public/fixtures/opcode_spammer.js +20 -8
  45. package/dest/public/fuzzing/avm_simulator_bin.js +7 -4
  46. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +1 -1
  47. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  48. package/dest/public/public_tx_simulator/public_tx_simulator.js +4 -3
  49. package/package.json +16 -16
  50. package/src/public/avm/avm_context.ts +2 -2
  51. package/src/public/avm/avm_contract_call_result.ts +8 -6
  52. package/src/public/avm/avm_execution_environment.ts +9 -4
  53. package/src/public/avm/avm_machine_state.ts +6 -5
  54. package/src/public/avm/avm_memory_types.ts +4 -0
  55. package/src/public/avm/avm_simulator.ts +8 -5
  56. package/src/public/avm/calldata.ts +100 -0
  57. package/src/public/avm/fixtures/avm_simulation_tester.ts +8 -2
  58. package/src/public/avm/fixtures/initializers.ts +2 -1
  59. package/src/public/avm/opcodes/accrued_substate.ts +6 -0
  60. package/src/public/avm/opcodes/contract.ts +1 -4
  61. package/src/public/avm/opcodes/external_calls.ts +8 -7
  62. package/src/public/avm/opcodes/memory.ts +1 -1
  63. package/src/public/avm/opcodes/storage.ts +28 -20
  64. package/src/public/debug_fn_name.ts +10 -8
  65. package/src/public/fixtures/opcode_spammer.ts +37 -8
  66. package/src/public/fuzzing/avm_simulator_bin.ts +11 -1
  67. package/src/public/public_tx_simulator/public_tx_simulator.ts +8 -3
@@ -1,20 +1,21 @@
1
- import type { Fr } from '@aztec/foundation/curves/bn254';
2
1
  import { FunctionSelector } from '@aztec/stdlib/abi';
3
2
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
3
 
4
+ import type { CallData } from './avm/calldata.js';
5
5
  import type { PublicContractsDBInterface } from './db_interfaces.js';
6
6
 
7
7
  export async function getPublicFunctionDebugName(
8
8
  db: PublicContractsDBInterface,
9
9
  contractAddress: AztecAddress,
10
- calldata: Fr[],
10
+ calldata: CallData,
11
11
  ): Promise<string> {
12
12
  // Public function is dispatched and therefore the target function is passed in the first argument.
13
- if (!calldata[0]) {
13
+ const selectorField = calldata.read(0);
14
+ if (!selectorField) {
14
15
  return `<calldata[0] undefined> (Contract Address: ${contractAddress})`;
15
16
  }
16
- const fallbackName = `<calldata[0]:${calldata[0].toString()}> (Contract Address: ${contractAddress})`;
17
- const selector = FunctionSelector.fromFieldOrUndefined(calldata[0]);
17
+ const fallbackName = `<calldata[0]:${selectorField.toString()}> (Contract Address: ${contractAddress})`;
18
+ const selector = FunctionSelector.fromFieldOrUndefined(selectorField);
18
19
  if (!selector) {
19
20
  return fallbackName;
20
21
  }
@@ -32,13 +33,14 @@ export async function getPublicFunctionDebugName(
32
33
  export async function getPublicFunctionSelectorAndName(
33
34
  db: PublicContractsDBInterface,
34
35
  contractAddress: AztecAddress,
35
- calldata: Fr[],
36
+ calldata: CallData,
36
37
  ): Promise<{ functionSelector?: FunctionSelector; functionName?: string }> {
37
38
  // Public function is dispatched and therefore the target function is passed in the first argument.
38
- if (!calldata[0]) {
39
+ const selectorField = calldata.read(0);
40
+ if (!selectorField) {
39
41
  return {};
40
42
  }
41
- const selector = FunctionSelector.fromFieldOrUndefined(calldata[0]);
43
+ const selector = FunctionSelector.fromFieldOrUndefined(selectorField);
42
44
  if (!selector) {
43
45
  return {};
44
46
  }
@@ -966,34 +966,63 @@ export const SPAM_CONFIGS: Partial<Record<Opcode, SpamConfig[]>> = {
966
966
  [Opcode.SLOAD]: [
967
967
  {
968
968
  label: 'Cold read (slot not written)',
969
- setup: [{ offset: 0, value: new Field(Fr.random()) }], // random slot
970
- targetInstructions: () => [new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*dstOffset=*/ 1)],
969
+ setup: [
970
+ { offset: 0, value: new Field(Fr.random()) }, // random slot
971
+ () => [
972
+ // Get current contract address into offset 1
973
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 1, /*varEnum=*/ 0).as(
974
+ Opcode.GETENVVAR_16,
975
+ GetEnvVar.wireFormat16,
976
+ ),
977
+ ],
978
+ ],
979
+ targetInstructions: () => [
980
+ new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*contractAddressOffset=*/ 1, /*dstOffset=*/ 2),
981
+ ],
971
982
  },
972
983
  {
973
984
  label: 'Warm read (from tree)',
974
985
  // Uses pre-inserted storage from insertWarmTreeEntries() which is called after contract deployment
975
- setup: [{ offset: 0, value: new Field(WARM_STORAGE_SLOT) }], // pre-inserted slot
976
- targetInstructions: () => [new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*dstOffset=*/ 1)],
986
+ setup: [
987
+ { offset: 0, value: new Field(WARM_STORAGE_SLOT) }, // pre-inserted slot
988
+ () => [
989
+ // Get current contract address into offset 1
990
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 1, /*varEnum=*/ 0).as(
991
+ Opcode.GETENVVAR_16,
992
+ GetEnvVar.wireFormat16,
993
+ ),
994
+ ],
995
+ ],
996
+ targetInstructions: () => [
997
+ new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*contractAddressOffset=*/ 1, /*dstOffset=*/ 2),
998
+ ],
977
999
  },
978
1000
  {
979
1001
  label: 'Warm read (SSTORE first, unique slot per SLOAD)',
980
- // Memory layout: slot (incremented), value, constant 1, revertSize, loaded value
1002
+ // Memory layout: slot (incremented), value, constant 1, contract address (from GETENVVAR), revertSize, loaded value
981
1003
  setup: [
982
1004
  { offset: 0, value: new Field(Fr.random()) }, // slot (will be incremented)
983
1005
  { offset: 1, value: new Field(Fr.random()) }, // value to store
984
1006
  { offset: 2, value: new Field(1n) }, // constant 1 for ADD
985
- { offset: 3, value: new Uint32(0n) }, // revertSize
1007
+ () => [
1008
+ // Get current contract address into offset 3
1009
+ new GetEnvVar(/*addressing_mode=*/ 0, /*dstOffset=*/ 3, /*varEnum=*/ 0).as(
1010
+ Opcode.GETENVVAR_16,
1011
+ GetEnvVar.wireFormat16,
1012
+ ),
1013
+ ],
1014
+ { offset: 4, value: new Uint32(0n) }, // revertSize
986
1015
  ],
987
1016
  targetInstructions: () => [
988
1017
  new SStore(/*addressing_mode=*/ 0, /*srcOffset=*/ 1, /*slotOffset=*/ 0),
989
- new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*dstOffset=*/ 4),
1018
+ new SLoad(/*addressing_mode=*/ 0, /*slotOffset=*/ 0, /*contractAddressOffset=*/ 3, /*dstOffset=*/ 5),
990
1019
  new Add(/*addressing_mode=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 0).as(
991
1020
  Opcode.ADD_8,
992
1021
  Add.wireFormat8,
993
1022
  ), // slot++
994
1023
  ],
995
1024
  cleanupInstructions: () => [
996
- new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 3, /*returnOffset=*/ 0).as(
1025
+ new Revert(/*addressing_mode=*/ 0, /*retSizeOffset=*/ 4, /*returnOffset=*/ 0).as(
997
1026
  Opcode.REVERT_8,
998
1027
  Revert.wireFormat8,
999
1028
  ),
@@ -3,6 +3,7 @@ import { EthAddress } from '@aztec/foundation/eth-address';
3
3
  import {
4
4
  AvmCircuitPublicInputs,
5
5
  type AvmTxHint,
6
+ PublicTxEffect,
6
7
  deserializeFromMessagePack,
7
8
  serializeWithMessagePack,
8
9
  } from '@aztec/stdlib/avm';
@@ -58,7 +59,13 @@ async function simulateWithFuzzer(
58
59
  rawPublicDataWrites: any[], // Public data tree writes to apply before simulation
59
60
  rawNoteHashes: any[], // Note hashes to apply before simulation
60
61
  protocolContracts: ProtocolContracts, // Protocol contracts mapping from C++
61
- ): Promise<{ reverted: boolean; output: Fr[]; revertReason?: string; publicInputs: AvmCircuitPublicInputs }> {
62
+ ): Promise<{
63
+ reverted: boolean;
64
+ output: Fr[];
65
+ revertReason?: string;
66
+ publicInputs: AvmCircuitPublicInputs;
67
+ publicTxEffect: PublicTxEffect;
68
+ }> {
62
69
  const worldStateService = await openExistingWorldState(dataDir, mapSizeKb);
63
70
 
64
71
  const simulator = await AvmFuzzerSimulator.create(worldStateService, globals, protocolContracts);
@@ -90,6 +97,7 @@ async function simulateWithFuzzer(
90
97
  output,
91
98
  revertReason: result.findRevertReason()?.message,
92
99
  publicInputs: result.publicInputs!,
100
+ publicTxEffect: result.publicTxEffect,
93
101
  };
94
102
  }
95
103
 
@@ -119,6 +127,7 @@ async function execute(base64Line: string): Promise<void> {
119
127
  output: result.output,
120
128
  revertReason: result.revertReason ?? '',
121
129
  endTreeSnapshots: result.publicInputs.endTreeSnapshots,
130
+ publicTxEffect: result.publicTxEffect,
122
131
  });
123
132
  const base64Response = resultBuffer.toString('base64') + '\n';
124
133
  await writeOutput(base64Response);
@@ -129,6 +138,7 @@ async function execute(base64Line: string): Promise<void> {
129
138
  output: [] as Fr[],
130
139
  revertReason: `Unexpected Error ${error.message}`,
131
140
  endTreeSnapshots: TreeSnapshots.empty(),
141
+ publicTxEffect: PublicTxEffect.empty(),
132
142
  });
133
143
  await writeOutput(errorResult.toString('base64') + '\n');
134
144
  }
@@ -19,6 +19,7 @@ import {
19
19
  import { strict as assert } from 'assert';
20
20
 
21
21
  import type { AvmFinalizedCallResult } from '../avm/avm_contract_call_result.js';
22
+ import { CallDataArray } from '../avm/calldata.js';
22
23
  import { AvmSimulator } from '../avm/index.js';
23
24
  import { getPublicFunctionDebugName } from '../debug_fn_name.js';
24
25
  import { HintingMerkleWriteOperations, HintingPublicContractsDB } from '../hinting_db_sources.js';
@@ -267,7 +268,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
267
268
 
268
269
  const enqueuedCallResult = await this.simulateEnqueuedCall(phase, context, callRequest);
269
270
 
270
- returnValues.push(new NestedProcessReturnValues(enqueuedCallResult.output));
271
+ returnValues.push(new NestedProcessReturnValues(enqueuedCallResult.output.bestEffortReadAll()));
271
272
 
272
273
  if (enqueuedCallResult.reverted) {
273
274
  reverted = true;
@@ -297,7 +298,11 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
297
298
  ): Promise<AvmFinalizedCallResult> {
298
299
  const stateManager = context.state.getActiveStateManager();
299
300
  const contractAddress = callRequest.request.contractAddress;
300
- const fnName = await getPublicFunctionDebugName(this.contractsDB, contractAddress, callRequest.calldata);
301
+ const fnName = await getPublicFunctionDebugName(
302
+ this.contractsDB,
303
+ contractAddress,
304
+ new CallDataArray(callRequest.calldata),
305
+ );
301
306
 
302
307
  const allocatedGas = context.getGasLeftAtPhase(phase);
303
308
 
@@ -357,7 +362,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
357
362
  transactionFee,
358
363
  this.globalVariables,
359
364
  request.isStaticCall,
360
- calldata,
365
+ new CallDataArray(calldata),
361
366
  allocatedGas,
362
367
  this.config,
363
368
  );