@aztec/simulator 0.63.1 → 0.64.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 (107) hide show
  1. package/dest/avm/avm_contract_call_result.d.ts +18 -1
  2. package/dest/avm/avm_contract_call_result.d.ts.map +1 -1
  3. package/dest/avm/avm_contract_call_result.js +30 -3
  4. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  5. package/dest/avm/avm_memory_types.js +7 -11
  6. package/dest/avm/avm_simulator.d.ts +6 -1
  7. package/dest/avm/avm_simulator.d.ts.map +1 -1
  8. package/dest/avm/avm_simulator.js +29 -8
  9. package/dest/avm/avm_tree.d.ts +4 -18
  10. package/dest/avm/avm_tree.d.ts.map +1 -1
  11. package/dest/avm/avm_tree.js +71 -53
  12. package/dest/avm/errors.d.ts +7 -0
  13. package/dest/avm/errors.d.ts.map +1 -1
  14. package/dest/avm/errors.js +11 -1
  15. package/dest/avm/fixtures/index.d.ts +2 -2
  16. package/dest/avm/fixtures/index.d.ts.map +1 -1
  17. package/dest/avm/fixtures/index.js +1 -1
  18. package/dest/avm/index.d.ts +1 -0
  19. package/dest/avm/index.d.ts.map +1 -1
  20. package/dest/avm/index.js +2 -1
  21. package/dest/avm/journal/journal.d.ts +14 -13
  22. package/dest/avm/journal/journal.d.ts.map +1 -1
  23. package/dest/avm/journal/journal.js +81 -68
  24. package/dest/avm/journal/public_storage.d.ts +0 -1
  25. package/dest/avm/journal/public_storage.d.ts.map +1 -1
  26. package/dest/avm/journal/public_storage.js +6 -6
  27. package/dest/avm/opcodes/accrued_substate.js +2 -2
  28. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -1
  29. package/dest/avm/opcodes/addressing_mode.js +8 -2
  30. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  31. package/dest/avm/opcodes/external_calls.js +2 -4
  32. package/dest/avm/opcodes/instruction.d.ts +1 -1
  33. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  34. package/dest/avm/opcodes/instruction.js +1 -1
  35. package/dest/index.d.ts +0 -1
  36. package/dest/index.d.ts.map +1 -1
  37. package/dest/index.js +1 -2
  38. package/dest/public/dual_side_effect_trace.d.ts +3 -9
  39. package/dest/public/dual_side_effect_trace.d.ts.map +1 -1
  40. package/dest/public/dual_side_effect_trace.js +5 -11
  41. package/dest/public/enqueued_call_side_effect_trace.d.ts +4 -10
  42. package/dest/public/enqueued_call_side_effect_trace.d.ts.map +1 -1
  43. package/dest/public/enqueued_call_side_effect_trace.js +6 -15
  44. package/dest/public/executor_metrics.d.ts +1 -2
  45. package/dest/public/executor_metrics.d.ts.map +1 -1
  46. package/dest/public/executor_metrics.js +2 -8
  47. package/dest/public/fixtures/index.d.ts +14 -0
  48. package/dest/public/fixtures/index.d.ts.map +1 -0
  49. package/dest/public/fixtures/index.js +100 -0
  50. package/dest/public/index.d.ts +1 -2
  51. package/dest/public/index.d.ts.map +1 -1
  52. package/dest/public/index.js +1 -2
  53. package/dest/public/public_processor.d.ts +0 -2
  54. package/dest/public/public_processor.d.ts.map +1 -1
  55. package/dest/public/public_processor.js +5 -12
  56. package/dest/public/public_tx_context.d.ts +6 -4
  57. package/dest/public/public_tx_context.d.ts.map +1 -1
  58. package/dest/public/public_tx_context.js +11 -7
  59. package/dest/public/public_tx_simulator.d.ts +56 -5
  60. package/dest/public/public_tx_simulator.d.ts.map +1 -1
  61. package/dest/public/public_tx_simulator.js +106 -26
  62. package/dest/public/side_effect_trace.d.ts +4 -10
  63. package/dest/public/side_effect_trace.d.ts.map +1 -1
  64. package/dest/public/side_effect_trace.js +6 -15
  65. package/dest/public/side_effect_trace_interface.d.ts +3 -9
  66. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  67. package/dest/public/transitional_adapters.d.ts +1 -2
  68. package/dest/public/transitional_adapters.d.ts.map +1 -1
  69. package/dest/public/transitional_adapters.js +8 -10
  70. package/package.json +10 -10
  71. package/src/avm/avm_contract_call_result.ts +39 -2
  72. package/src/avm/avm_memory_types.ts +6 -10
  73. package/src/avm/avm_simulator.ts +58 -8
  74. package/src/avm/avm_tree.ts +88 -64
  75. package/src/avm/errors.ts +11 -0
  76. package/src/avm/fixtures/index.ts +4 -3
  77. package/src/avm/index.ts +1 -0
  78. package/src/avm/journal/journal.ts +118 -126
  79. package/src/avm/journal/public_storage.ts +5 -6
  80. package/src/avm/opcodes/accrued_substate.ts +1 -1
  81. package/src/avm/opcodes/addressing_mode.ts +7 -2
  82. package/src/avm/opcodes/external_calls.ts +1 -3
  83. package/src/avm/opcodes/instruction.ts +1 -1
  84. package/src/index.ts +0 -1
  85. package/src/public/dual_side_effect_trace.ts +4 -13
  86. package/src/public/enqueued_call_side_effect_trace.ts +14 -17
  87. package/src/public/executor_metrics.ts +1 -9
  88. package/src/public/fixtures/index.ts +158 -0
  89. package/src/public/index.ts +1 -5
  90. package/src/public/public_processor.ts +5 -27
  91. package/src/public/public_tx_context.ts +13 -8
  92. package/src/public/public_tx_simulator.ts +182 -50
  93. package/src/public/side_effect_trace.ts +9 -19
  94. package/src/public/side_effect_trace_interface.ts +3 -9
  95. package/src/public/transitional_adapters.ts +12 -12
  96. package/dest/mocks/fixtures.d.ts +0 -28
  97. package/dest/mocks/fixtures.d.ts.map +0 -1
  98. package/dest/mocks/fixtures.js +0 -48
  99. package/dest/mocks/index.d.ts +0 -2
  100. package/dest/mocks/index.d.ts.map +0 -1
  101. package/dest/mocks/index.js +0 -2
  102. package/dest/public/executor.d.ts +0 -47
  103. package/dest/public/executor.d.ts.map +0 -1
  104. package/dest/public/executor.js +0 -90
  105. package/src/mocks/fixtures.ts +0 -72
  106. package/src/mocks/index.ts +0 -1
  107. package/src/public/executor.ts +0 -154
@@ -1,90 +0,0 @@
1
- import { Fr, Gas } from '@aztec/circuits.js';
2
- import { createDebugLogger } from '@aztec/foundation/log';
3
- import { Timer } from '@aztec/foundation/timer';
4
- import { AvmContext } from '../avm/avm_context.js';
5
- import { AvmExecutionEnvironment } from '../avm/avm_execution_environment.js';
6
- import { AvmMachineState } from '../avm/avm_machine_state.js';
7
- import { AvmSimulator } from '../avm/avm_simulator.js';
8
- import { AvmPersistableStateManager } from '../avm/journal/index.js';
9
- import { getPublicFunctionDebugName } from '../common/debug_fn_name.js';
10
- import { DualSideEffectTrace } from './dual_side_effect_trace.js';
11
- import { PublicEnqueuedCallSideEffectTrace } from './enqueued_call_side_effect_trace.js';
12
- import { ExecutorMetrics } from './executor_metrics.js';
13
- import { PublicSideEffectTrace } from './side_effect_trace.js';
14
- /**
15
- * Handles execution of public functions.
16
- */
17
- export class PublicExecutor {
18
- constructor(worldStateDB, client) {
19
- this.worldStateDB = worldStateDB;
20
- this.metrics = new ExecutorMetrics(client, 'PublicExecutor');
21
- }
22
- /**
23
- * Simulate a public execution request.
24
- * @param executionRequest - The execution to run.
25
- * @param globalVariables - The global variables to use.
26
- * @param allocatedGas - The gas available at the start of this enqueued call.
27
- * @param transactionFee - Fee offered for this TX.
28
- * @returns The result of execution.
29
- */
30
- async simulate(stateManager, executionRequest, globalVariables, allocatedGas, transactionFee = Fr.ZERO) {
31
- const address = executionRequest.callContext.contractAddress;
32
- const selector = executionRequest.callContext.functionSelector;
33
- const fnName = await getPublicFunctionDebugName(this.worldStateDB, address, selector, executionRequest.args);
34
- PublicExecutor.log.verbose(`[AVM] Executing public external function ${fnName}@${address} with ${allocatedGas.l2Gas} allocated L2 gas.`);
35
- const timer = new Timer();
36
- const avmExecutionEnv = createAvmExecutionEnvironment(executionRequest, globalVariables, transactionFee);
37
- const avmMachineState = new AvmMachineState(allocatedGas);
38
- const avmContext = new AvmContext(stateManager, avmExecutionEnv, avmMachineState);
39
- const simulator = new AvmSimulator(avmContext);
40
- const avmCallResult = await simulator.execute();
41
- const bytecode = simulator.getBytecode();
42
- PublicExecutor.log.verbose(`[AVM] ${fnName} returned, reverted: ${avmCallResult.reverted}${avmCallResult.reverted ? ', reason: ' + avmCallResult.revertReason : ''}.`, {
43
- eventName: 'avm-simulation',
44
- appCircuitName: fnName,
45
- duration: timer.ms(),
46
- bytecodeSize: bytecode.length,
47
- });
48
- const publicExecutionResult = stateManager.trace.toPublicEnqueuedCallExecutionResult(
49
- /*endGasLeft=*/ Gas.from(avmContext.machineState.gasLeft), avmCallResult);
50
- if (avmCallResult.reverted) {
51
- this.metrics.recordFunctionSimulationFailure();
52
- }
53
- else {
54
- this.metrics.recordFunctionSimulation(bytecode.length, timer.ms());
55
- }
56
- PublicExecutor.log.verbose(`[AVM] ${fnName} simulation complete. Reverted=${avmCallResult.reverted}. Consumed ${allocatedGas.l2Gas - avmContext.machineState.gasLeft.l2Gas} L2 gas, ending with ${avmContext.machineState.gasLeft.l2Gas} L2 gas left.`);
57
- return publicExecutionResult;
58
- }
59
- /**
60
- * Simulate an enqueued call on its own, and include its side effects in its results.
61
- * @param executionRequest - The execution to run.
62
- * @param globalVariables - The global variables to use.
63
- * @param allocatedGas - The gas available at the start of this enqueued call.
64
- * @param transactionFee - Fee offered for this TX.
65
- * @param startSideEffectCounter - The start counter to initialize the side effect trace with.
66
- * @returns The result of execution including side effect vectors.
67
- * FIXME: this function is only used by the TXE. Ideally we would not support this as an external interface.
68
- * Avoid using this interface as it it shouldn't really exist in the first place.
69
- */
70
- async simulateIsolatedEnqueuedCall(executionRequest, globalVariables, allocatedGas, transactionFee = Fr.ONE, startSideEffectCounter = 0) {
71
- const innerCallTrace = new PublicSideEffectTrace(startSideEffectCounter);
72
- const enqueuedCallTrace = new PublicEnqueuedCallSideEffectTrace(startSideEffectCounter);
73
- const trace = new DualSideEffectTrace(innerCallTrace, enqueuedCallTrace);
74
- const stateManager = new AvmPersistableStateManager(this.worldStateDB, trace);
75
- return (await this.simulate(stateManager, executionRequest, globalVariables, allocatedGas, transactionFee));
76
- }
77
- }
78
- PublicExecutor.log = createDebugLogger('aztec:simulator:public_executor');
79
- /**
80
- * Convert a PublicExecutionRequest object to an AvmExecutionEnvironment
81
- *
82
- * @param executionRequest
83
- * @param globalVariables
84
- * @returns
85
- */
86
- export function createAvmExecutionEnvironment(executionRequest, globalVariables, transactionFee) {
87
- return new AvmExecutionEnvironment(executionRequest.callContext.contractAddress, executionRequest.callContext.msgSender, executionRequest.callContext.functionSelector,
88
- /*contractCallDepth=*/ Fr.zero(), transactionFee, globalVariables, executionRequest.callContext.isStaticCall, executionRequest.args);
89
- }
90
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhlY3V0b3IuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvcHVibGljL2V4ZWN1dG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVBLE9BQU8sRUFBRSxFQUFFLEVBQUUsR0FBRyxFQUF3QixNQUFNLG9CQUFvQixDQUFDO0FBQ25FLE9BQU8sRUFBRSxpQkFBaUIsRUFBRSxNQUFNLHVCQUF1QixDQUFDO0FBQzFELE9BQU8sRUFBRSxLQUFLLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUdoRCxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFDbkQsT0FBTyxFQUFFLHVCQUF1QixFQUFFLE1BQU0scUNBQXFDLENBQUM7QUFDOUUsT0FBTyxFQUFFLGVBQWUsRUFBRSxNQUFNLDZCQUE2QixDQUFDO0FBQzlELE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUN2RCxPQUFPLEVBQUUsMEJBQTBCLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUNyRSxPQUFPLEVBQUUsMEJBQTBCLEVBQUUsTUFBTSw0QkFBNEIsQ0FBQztBQUN4RSxPQUFPLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUNsRSxPQUFPLEVBQUUsaUNBQWlDLEVBQUUsTUFBTSxzQ0FBc0MsQ0FBQztBQUt6RixPQUFPLEVBQUUsZUFBZSxFQUF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jLENBQ2YsQ0FBcUQsQ0FBQztJQUN6RCxDQUFDOztBQS9GZSxrQkFBRyxHQUFHLGlCQUFpQixDQUFDLGlDQUFpQyxDQUFDLENBQUM7QUFrRzdFOzs7Ozs7R0FNRztBQUNILE1BQU0sVUFBVSw2QkFBNkIsQ0FDM0MsZ0JBQXdDLEVBQ3hDLGVBQWdDLEVBQ2hDLGNBQWtCO0lBRWxCLE9BQU8sSUFBSSx1QkFBdUIsQ0FDaEMsZ0JBQWdCLENBQUMsV0FBVyxDQUFDLGVBQWUsRUFDNUMsZ0JBQWdCLENBQUMsV0FBVyxDQUFDLFNBQVMsRUFDdEMsZ0JBQWdCLENBQUMsV0FBVyxDQUFDLGdCQUFnQjtJQUM3QyxzQkFBc0IsQ0FBQyxFQUFFLENBQUMsSUFBSSxFQUFFLEVBQ2hDLGNBQWMsRUFDZCxlQUFlLEVBQ2YsZ0JBQWdCLENBQUMsV0FBVyxDQUFDLFlBQVksRUFDekMsZ0JBQWdCLENBQUMsSUFBSSxDQUN0QixDQUFDO0FBQ0osQ0FBQyJ9
@@ -1,72 +0,0 @@
1
- import { SimulationError } from '@aztec/circuit-types';
2
- import { ARGS_LENGTH, Fr, Gas } from '@aztec/circuits.js';
3
- import { makeAztecAddress, makeSelector } from '@aztec/circuits.js/testing';
4
- import { FunctionType } from '@aztec/foundation/abi';
5
- import { padArrayEnd } from '@aztec/foundation/collection';
6
-
7
- import { type EnqueuedPublicCallExecutionResult } from '../public/execution.js';
8
-
9
- export class PublicExecutionResultBuilder {
10
- private _returnValues: Fr[] = [];
11
- private _reverted = false;
12
- private _revertReason: SimulationError | undefined = undefined;
13
-
14
- constructor() {}
15
-
16
- static empty(basicRevert = false) {
17
- const builder = new PublicExecutionResultBuilder();
18
- if (basicRevert) {
19
- builder.withReverted(new SimulationError('Simulation failed', []));
20
- }
21
- return builder;
22
- }
23
-
24
- static fromPublicExecutionRequest({
25
- returnValues = [new Fr(1n)],
26
- revertReason = undefined,
27
- }: {
28
- returnValues?: Fr[];
29
- revertReason?: SimulationError;
30
- }): PublicExecutionResultBuilder {
31
- const builder = new PublicExecutionResultBuilder();
32
-
33
- builder.withReturnValues(...returnValues);
34
- if (revertReason) {
35
- builder.withReverted(revertReason);
36
- }
37
-
38
- return builder;
39
- }
40
-
41
- withReturnValues(...values: Fr[]): PublicExecutionResultBuilder {
42
- this._returnValues.push(...values);
43
- return this;
44
- }
45
-
46
- withReverted(reason: SimulationError): PublicExecutionResultBuilder {
47
- this._reverted = true;
48
- this._revertReason = reason;
49
- return this;
50
- }
51
-
52
- build(overrides: Partial<EnqueuedPublicCallExecutionResult> = {}): EnqueuedPublicCallExecutionResult {
53
- return {
54
- endGasLeft: Gas.empty(),
55
- endSideEffectCounter: Fr.ZERO,
56
- returnValues: padArrayEnd(this._returnValues, Fr.ZERO, 4), // TODO(#5450) Need to use the proper return values here
57
- reverted: this._reverted,
58
- revertReason: this._revertReason,
59
- ...overrides,
60
- };
61
- }
62
- }
63
-
64
- export const makeFunctionCall = (
65
- name = 'function',
66
- to = makeAztecAddress(30),
67
- selector = makeSelector(5),
68
- type = FunctionType.PUBLIC,
69
- args = new Array(ARGS_LENGTH).fill(Fr.ZERO),
70
- isStatic = false,
71
- returnTypes = [],
72
- ) => ({ name, to, selector, type, args, isStatic, returnTypes });
@@ -1 +0,0 @@
1
- export * from './fixtures.js';
@@ -1,154 +0,0 @@
1
- import { type PublicExecutionRequest } from '@aztec/circuit-types';
2
- import { type AvmSimulationStats } from '@aztec/circuit-types/stats';
3
- import { Fr, Gas, type GlobalVariables } from '@aztec/circuits.js';
4
- import { createDebugLogger } from '@aztec/foundation/log';
5
- import { Timer } from '@aztec/foundation/timer';
6
- import { type TelemetryClient } from '@aztec/telemetry-client';
7
-
8
- import { AvmContext } from '../avm/avm_context.js';
9
- import { AvmExecutionEnvironment } from '../avm/avm_execution_environment.js';
10
- import { AvmMachineState } from '../avm/avm_machine_state.js';
11
- import { AvmSimulator } from '../avm/avm_simulator.js';
12
- import { AvmPersistableStateManager } from '../avm/journal/index.js';
13
- import { getPublicFunctionDebugName } from '../common/debug_fn_name.js';
14
- import { DualSideEffectTrace } from './dual_side_effect_trace.js';
15
- import { PublicEnqueuedCallSideEffectTrace } from './enqueued_call_side_effect_trace.js';
16
- import {
17
- type EnqueuedPublicCallExecutionResult,
18
- type EnqueuedPublicCallExecutionResultWithSideEffects,
19
- } from './execution.js';
20
- import { ExecutorMetrics } from './executor_metrics.js';
21
- import { type WorldStateDB } from './public_db_sources.js';
22
- import { PublicSideEffectTrace } from './side_effect_trace.js';
23
-
24
- /**
25
- * Handles execution of public functions.
26
- */
27
- export class PublicExecutor {
28
- metrics: ExecutorMetrics;
29
-
30
- constructor(private readonly worldStateDB: WorldStateDB, client: TelemetryClient) {
31
- this.metrics = new ExecutorMetrics(client, 'PublicExecutor');
32
- }
33
-
34
- static readonly log = createDebugLogger('aztec:simulator:public_executor');
35
-
36
- /**
37
- * Simulate a public execution request.
38
- * @param executionRequest - The execution to run.
39
- * @param globalVariables - The global variables to use.
40
- * @param allocatedGas - The gas available at the start of this enqueued call.
41
- * @param transactionFee - Fee offered for this TX.
42
- * @returns The result of execution.
43
- */
44
- public async simulate(
45
- stateManager: AvmPersistableStateManager,
46
- executionRequest: PublicExecutionRequest,
47
- globalVariables: GlobalVariables,
48
- allocatedGas: Gas,
49
- transactionFee: Fr = Fr.ZERO,
50
- ): Promise<EnqueuedPublicCallExecutionResult> {
51
- const address = executionRequest.callContext.contractAddress;
52
- const selector = executionRequest.callContext.functionSelector;
53
- const fnName = await getPublicFunctionDebugName(this.worldStateDB, address, selector, executionRequest.args);
54
-
55
- PublicExecutor.log.verbose(
56
- `[AVM] Executing public external function ${fnName}@${address} with ${allocatedGas.l2Gas} allocated L2 gas.`,
57
- );
58
- const timer = new Timer();
59
-
60
- const avmExecutionEnv = createAvmExecutionEnvironment(executionRequest, globalVariables, transactionFee);
61
-
62
- const avmMachineState = new AvmMachineState(allocatedGas);
63
- const avmContext = new AvmContext(stateManager, avmExecutionEnv, avmMachineState);
64
- const simulator = new AvmSimulator(avmContext);
65
- const avmCallResult = await simulator.execute();
66
- const bytecode = simulator.getBytecode()!;
67
-
68
- PublicExecutor.log.verbose(
69
- `[AVM] ${fnName} returned, reverted: ${avmCallResult.reverted}${
70
- avmCallResult.reverted ? ', reason: ' + avmCallResult.revertReason : ''
71
- }.`,
72
- {
73
- eventName: 'avm-simulation',
74
- appCircuitName: fnName,
75
- duration: timer.ms(),
76
- bytecodeSize: bytecode!.length,
77
- } satisfies AvmSimulationStats,
78
- );
79
-
80
- const publicExecutionResult = stateManager.trace.toPublicEnqueuedCallExecutionResult(
81
- /*endGasLeft=*/ Gas.from(avmContext.machineState.gasLeft),
82
- avmCallResult,
83
- );
84
-
85
- if (avmCallResult.reverted) {
86
- this.metrics.recordFunctionSimulationFailure();
87
- } else {
88
- this.metrics.recordFunctionSimulation(bytecode.length, timer.ms());
89
- }
90
-
91
- PublicExecutor.log.verbose(
92
- `[AVM] ${fnName} simulation complete. Reverted=${avmCallResult.reverted}. Consumed ${
93
- allocatedGas.l2Gas - avmContext.machineState.gasLeft.l2Gas
94
- } L2 gas, ending with ${avmContext.machineState.gasLeft.l2Gas} L2 gas left.`,
95
- );
96
-
97
- return publicExecutionResult;
98
- }
99
-
100
- /**
101
- * Simulate an enqueued call on its own, and include its side effects in its results.
102
- * @param executionRequest - The execution to run.
103
- * @param globalVariables - The global variables to use.
104
- * @param allocatedGas - The gas available at the start of this enqueued call.
105
- * @param transactionFee - Fee offered for this TX.
106
- * @param startSideEffectCounter - The start counter to initialize the side effect trace with.
107
- * @returns The result of execution including side effect vectors.
108
- * FIXME: this function is only used by the TXE. Ideally we would not support this as an external interface.
109
- * Avoid using this interface as it it shouldn't really exist in the first place.
110
- */
111
- public async simulateIsolatedEnqueuedCall(
112
- executionRequest: PublicExecutionRequest,
113
- globalVariables: GlobalVariables,
114
- allocatedGas: Gas,
115
- transactionFee: Fr = Fr.ONE,
116
- startSideEffectCounter: number = 0,
117
- ): Promise<EnqueuedPublicCallExecutionResultWithSideEffects> {
118
- const innerCallTrace = new PublicSideEffectTrace(startSideEffectCounter);
119
- const enqueuedCallTrace = new PublicEnqueuedCallSideEffectTrace(startSideEffectCounter);
120
- const trace = new DualSideEffectTrace(innerCallTrace, enqueuedCallTrace);
121
- const stateManager = new AvmPersistableStateManager(this.worldStateDB, trace);
122
- return (await this.simulate(
123
- stateManager,
124
- executionRequest,
125
- globalVariables,
126
- allocatedGas,
127
- transactionFee,
128
- )) as EnqueuedPublicCallExecutionResultWithSideEffects;
129
- }
130
- }
131
-
132
- /**
133
- * Convert a PublicExecutionRequest object to an AvmExecutionEnvironment
134
- *
135
- * @param executionRequest
136
- * @param globalVariables
137
- * @returns
138
- */
139
- export function createAvmExecutionEnvironment(
140
- executionRequest: PublicExecutionRequest,
141
- globalVariables: GlobalVariables,
142
- transactionFee: Fr,
143
- ): AvmExecutionEnvironment {
144
- return new AvmExecutionEnvironment(
145
- executionRequest.callContext.contractAddress,
146
- executionRequest.callContext.msgSender,
147
- executionRequest.callContext.functionSelector,
148
- /*contractCallDepth=*/ Fr.zero(),
149
- transactionFee,
150
- globalVariables,
151
- executionRequest.callContext.isStaticCall,
152
- executionRequest.args,
153
- );
154
- }