@aztec/simulator 0.48.0 → 0.49.2

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.
@@ -1,3 +1,5 @@
1
+ import * as c from '@aztec/circuits.js/constants';
2
+
1
3
  import { TypeTag } from './avm_memory_types.js';
2
4
  import { InstructionExecutionError } from './errors.js';
3
5
  import { Addressing, AddressingMode } from './opcodes/addressing_mode.js';
@@ -46,90 +48,150 @@ export const EmptyGas: Gas = {
46
48
  daGas: 0,
47
49
  };
48
50
 
51
+ function makeCost(l2Gas: number, daGas: number): Gas {
52
+ return { l2Gas, daGas };
53
+ }
54
+
49
55
  /** Dimensions of gas usage: L1, L2, and DA. */
50
56
  export const GasDimensions = ['l2Gas', 'daGas'] as const;
51
57
 
52
- /** Default gas cost for an opcode. */
53
- const DefaultBaseGasCost: Gas = { l2Gas: 10, daGas: 0 };
54
-
55
58
  /** Base gas costs for each instruction. Additional gas cost may be added on top due to memory or storage accesses, etc. */
56
59
  const BaseGasCosts: Record<Opcode, Gas> = {
57
- [Opcode.ADD]: DefaultBaseGasCost,
58
- [Opcode.SUB]: DefaultBaseGasCost,
59
- [Opcode.MUL]: DefaultBaseGasCost,
60
- [Opcode.DIV]: DefaultBaseGasCost,
61
- [Opcode.FDIV]: DefaultBaseGasCost,
62
- [Opcode.EQ]: DefaultBaseGasCost,
63
- [Opcode.LT]: DefaultBaseGasCost,
64
- [Opcode.LTE]: DefaultBaseGasCost,
65
- [Opcode.AND]: DefaultBaseGasCost,
66
- [Opcode.OR]: DefaultBaseGasCost,
67
- [Opcode.XOR]: DefaultBaseGasCost,
68
- [Opcode.NOT]: DefaultBaseGasCost,
69
- [Opcode.SHL]: DefaultBaseGasCost,
70
- [Opcode.SHR]: DefaultBaseGasCost,
71
- [Opcode.CAST]: DefaultBaseGasCost,
72
- // Execution environment
73
- [Opcode.ADDRESS]: DefaultBaseGasCost,
74
- [Opcode.STORAGEADDRESS]: DefaultBaseGasCost,
75
- [Opcode.SENDER]: DefaultBaseGasCost,
76
- [Opcode.FEEPERL2GAS]: DefaultBaseGasCost,
77
- [Opcode.FEEPERDAGAS]: DefaultBaseGasCost,
78
- [Opcode.TRANSACTIONFEE]: DefaultBaseGasCost,
79
- [Opcode.FUNCTIONSELECTOR]: DefaultBaseGasCost,
80
- [Opcode.CHAINID]: DefaultBaseGasCost,
81
- [Opcode.VERSION]: DefaultBaseGasCost,
82
- [Opcode.BLOCKNUMBER]: DefaultBaseGasCost,
83
- [Opcode.TIMESTAMP]: DefaultBaseGasCost,
84
- [Opcode.COINBASE]: DefaultBaseGasCost,
85
- [Opcode.BLOCKL2GASLIMIT]: DefaultBaseGasCost,
86
- [Opcode.BLOCKDAGASLIMIT]: DefaultBaseGasCost,
87
- [Opcode.CALLDATACOPY]: DefaultBaseGasCost,
88
- // Gas
89
- [Opcode.L2GASLEFT]: DefaultBaseGasCost,
90
- [Opcode.DAGASLEFT]: DefaultBaseGasCost,
91
- // Control flow
92
- [Opcode.JUMP]: DefaultBaseGasCost,
93
- [Opcode.JUMPI]: DefaultBaseGasCost,
94
- [Opcode.INTERNALCALL]: DefaultBaseGasCost,
95
- [Opcode.INTERNALRETURN]: DefaultBaseGasCost,
96
- // Memory
97
- [Opcode.SET]: DefaultBaseGasCost,
98
- [Opcode.MOV]: DefaultBaseGasCost,
99
- [Opcode.CMOV]: DefaultBaseGasCost,
100
- // World state
101
- [Opcode.SLOAD]: DefaultBaseGasCost,
102
- [Opcode.SSTORE]: DefaultBaseGasCost,
103
- [Opcode.NOTEHASHEXISTS]: DefaultBaseGasCost,
104
- [Opcode.EMITNOTEHASH]: DefaultBaseGasCost,
105
- [Opcode.NULLIFIEREXISTS]: DefaultBaseGasCost,
106
- [Opcode.EMITNULLIFIER]: DefaultBaseGasCost,
107
- [Opcode.L1TOL2MSGEXISTS]: DefaultBaseGasCost,
108
- [Opcode.HEADERMEMBER]: DefaultBaseGasCost,
109
- [Opcode.EMITUNENCRYPTEDLOG]: DefaultBaseGasCost,
110
- [Opcode.SENDL2TOL1MSG]: DefaultBaseGasCost,
111
- [Opcode.GETCONTRACTINSTANCE]: DefaultBaseGasCost,
112
- // External calls
113
- [Opcode.CALL]: DefaultBaseGasCost,
114
- [Opcode.STATICCALL]: DefaultBaseGasCost,
115
- [Opcode.DELEGATECALL]: DefaultBaseGasCost,
116
- [Opcode.RETURN]: DefaultBaseGasCost,
117
- [Opcode.REVERT]: DefaultBaseGasCost,
118
- // Misc
119
- [Opcode.DEBUGLOG]: DefaultBaseGasCost,
120
- // Gadgets
121
- [Opcode.KECCAK]: DefaultBaseGasCost,
122
- [Opcode.POSEIDON2]: DefaultBaseGasCost,
123
- [Opcode.SHA256]: DefaultBaseGasCost,
124
- [Opcode.PEDERSEN]: DefaultBaseGasCost,
125
- [Opcode.ECADD]: DefaultBaseGasCost,
126
- [Opcode.MSM]: DefaultBaseGasCost,
127
- [Opcode.PEDERSENCOMMITMENT]: DefaultBaseGasCost,
128
- // Conversions
129
- [Opcode.TORADIXLE]: DefaultBaseGasCost,
130
- // Other
131
- [Opcode.SHA256COMPRESSION]: DefaultBaseGasCost,
132
- [Opcode.KECCAKF1600]: DefaultBaseGasCost,
60
+ [Opcode.ADD]: makeCost(c.AVM_ADD_BASE_L2_GAS, 0),
61
+ [Opcode.SUB]: makeCost(c.AVM_SUB_BASE_L2_GAS, 0),
62
+ [Opcode.MUL]: makeCost(c.AVM_MUL_BASE_L2_GAS, 0),
63
+ [Opcode.DIV]: makeCost(c.AVM_DIV_BASE_L2_GAS, 0),
64
+ [Opcode.FDIV]: makeCost(c.AVM_FDIV_BASE_L2_GAS, 0),
65
+ [Opcode.EQ]: makeCost(c.AVM_EQ_BASE_L2_GAS, 0),
66
+ [Opcode.LT]: makeCost(c.AVM_LT_BASE_L2_GAS, 0),
67
+ [Opcode.LTE]: makeCost(c.AVM_LTE_BASE_L2_GAS, 0),
68
+ [Opcode.AND]: makeCost(c.AVM_AND_BASE_L2_GAS, 0),
69
+ [Opcode.OR]: makeCost(c.AVM_OR_BASE_L2_GAS, 0),
70
+ [Opcode.XOR]: makeCost(c.AVM_XOR_BASE_L2_GAS, 0),
71
+ [Opcode.NOT]: makeCost(c.AVM_NOT_BASE_L2_GAS, 0),
72
+ [Opcode.SHL]: makeCost(c.AVM_SHL_BASE_L2_GAS, 0),
73
+ [Opcode.SHR]: makeCost(c.AVM_SHR_BASE_L2_GAS, 0),
74
+ [Opcode.CAST]: makeCost(c.AVM_CAST_BASE_L2_GAS, 0),
75
+ [Opcode.ADDRESS]: makeCost(c.AVM_ADDRESS_BASE_L2_GAS, 0),
76
+ [Opcode.STORAGEADDRESS]: makeCost(c.AVM_STORAGEADDRESS_BASE_L2_GAS, 0),
77
+ [Opcode.SENDER]: makeCost(c.AVM_SENDER_BASE_L2_GAS, 0),
78
+ [Opcode.FEEPERL2GAS]: makeCost(c.AVM_FEEPERL2GAS_BASE_L2_GAS, 0),
79
+ [Opcode.FEEPERDAGAS]: makeCost(c.AVM_FEEPERDAGAS_BASE_L2_GAS, 0),
80
+ [Opcode.TRANSACTIONFEE]: makeCost(c.AVM_TRANSACTIONFEE_BASE_L2_GAS, 0),
81
+ [Opcode.FUNCTIONSELECTOR]: makeCost(c.AVM_FUNCTIONSELECTOR_BASE_L2_GAS, 0),
82
+ [Opcode.CHAINID]: makeCost(c.AVM_CHAINID_BASE_L2_GAS, 0),
83
+ [Opcode.VERSION]: makeCost(c.AVM_VERSION_BASE_L2_GAS, 0),
84
+ [Opcode.BLOCKNUMBER]: makeCost(c.AVM_BLOCKNUMBER_BASE_L2_GAS, 0),
85
+ [Opcode.TIMESTAMP]: makeCost(c.AVM_TIMESTAMP_BASE_L2_GAS, 0),
86
+ [Opcode.COINBASE]: makeCost(c.AVM_COINBASE_BASE_L2_GAS, 0),
87
+ [Opcode.BLOCKL2GASLIMIT]: makeCost(c.AVM_BLOCKL2GASLIMIT_BASE_L2_GAS, 0),
88
+ [Opcode.BLOCKDAGASLIMIT]: makeCost(c.AVM_BLOCKDAGASLIMIT_BASE_L2_GAS, 0),
89
+ [Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_BASE_L2_GAS, 0),
90
+ [Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_BASE_L2_GAS, 0),
91
+ [Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_BASE_L2_GAS, 0),
92
+ [Opcode.JUMP]: makeCost(c.AVM_JUMP_BASE_L2_GAS, 0),
93
+ [Opcode.JUMPI]: makeCost(c.AVM_JUMPI_BASE_L2_GAS, 0),
94
+ [Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_BASE_L2_GAS, 0),
95
+ [Opcode.INTERNALRETURN]: makeCost(c.AVM_INTERNALRETURN_BASE_L2_GAS, 0),
96
+ [Opcode.SET]: makeCost(c.AVM_SET_BASE_L2_GAS, 0),
97
+ [Opcode.MOV]: makeCost(c.AVM_MOV_BASE_L2_GAS, 0),
98
+ [Opcode.CMOV]: makeCost(c.AVM_CMOV_BASE_L2_GAS, 0),
99
+ [Opcode.SLOAD]: makeCost(c.AVM_SLOAD_BASE_L2_GAS, 0),
100
+ [Opcode.SSTORE]: makeCost(c.AVM_SSTORE_BASE_L2_GAS, 0),
101
+ [Opcode.NOTEHASHEXISTS]: makeCost(c.AVM_NOTEHASHEXISTS_BASE_L2_GAS, 0),
102
+ [Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_BASE_L2_GAS, 0),
103
+ [Opcode.NULLIFIEREXISTS]: makeCost(c.AVM_NULLIFIEREXISTS_BASE_L2_GAS, 0),
104
+ [Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_BASE_L2_GAS, 0),
105
+ [Opcode.L1TOL2MSGEXISTS]: makeCost(c.AVM_L1TOL2MSGEXISTS_BASE_L2_GAS, 0),
106
+ [Opcode.HEADERMEMBER]: makeCost(c.AVM_HEADERMEMBER_BASE_L2_GAS, 0),
107
+ [Opcode.EMITUNENCRYPTEDLOG]: makeCost(c.AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS, 0),
108
+ [Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_BASE_L2_GAS, 0),
109
+ [Opcode.GETCONTRACTINSTANCE]: makeCost(c.AVM_GETCONTRACTINSTANCE_BASE_L2_GAS, 0),
110
+ [Opcode.CALL]: makeCost(c.AVM_CALL_BASE_L2_GAS, 0),
111
+ [Opcode.STATICCALL]: makeCost(c.AVM_STATICCALL_BASE_L2_GAS, 0),
112
+ [Opcode.DELEGATECALL]: makeCost(c.AVM_DELEGATECALL_BASE_L2_GAS, 0),
113
+ [Opcode.RETURN]: makeCost(c.AVM_RETURN_BASE_L2_GAS, 0),
114
+ [Opcode.REVERT]: makeCost(c.AVM_REVERT_BASE_L2_GAS, 0),
115
+ [Opcode.DEBUGLOG]: makeCost(c.AVM_DEBUGLOG_BASE_L2_GAS, 0),
116
+ [Opcode.KECCAK]: makeCost(c.AVM_KECCAK_BASE_L2_GAS, 0),
117
+ [Opcode.POSEIDON2]: makeCost(c.AVM_POSEIDON2_BASE_L2_GAS, 0),
118
+ [Opcode.SHA256]: makeCost(c.AVM_SHA256_BASE_L2_GAS, 0),
119
+ [Opcode.PEDERSEN]: makeCost(c.AVM_PEDERSEN_BASE_L2_GAS, 0),
120
+ [Opcode.ECADD]: makeCost(c.AVM_ECADD_BASE_L2_GAS, 0),
121
+ [Opcode.MSM]: makeCost(c.AVM_MSM_BASE_L2_GAS, 0),
122
+ [Opcode.PEDERSENCOMMITMENT]: makeCost(c.AVM_PEDERSENCOMMITMENT_BASE_L2_GAS, 0),
123
+ [Opcode.TORADIXLE]: makeCost(c.AVM_TORADIXLE_BASE_L2_GAS, 0),
124
+ [Opcode.SHA256COMPRESSION]: makeCost(c.AVM_SHA256COMPRESSION_BASE_L2_GAS, 0),
125
+ [Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_BASE_L2_GAS, 0),
126
+ };
127
+
128
+ const DynamicGasCosts: Record<Opcode, Gas> = {
129
+ [Opcode.ADD]: makeCost(c.AVM_ADD_DYN_L2_GAS, 0),
130
+ [Opcode.SUB]: makeCost(c.AVM_SUB_DYN_L2_GAS, 0),
131
+ [Opcode.MUL]: makeCost(c.AVM_MUL_DYN_L2_GAS, 0),
132
+ [Opcode.DIV]: makeCost(c.AVM_DIV_DYN_L2_GAS, 0),
133
+ [Opcode.FDIV]: makeCost(c.AVM_FDIV_DYN_L2_GAS, 0),
134
+ [Opcode.EQ]: makeCost(c.AVM_EQ_DYN_L2_GAS, 0),
135
+ [Opcode.LT]: makeCost(c.AVM_LT_DYN_L2_GAS, 0),
136
+ [Opcode.LTE]: makeCost(c.AVM_LTE_DYN_L2_GAS, 0),
137
+ [Opcode.AND]: makeCost(c.AVM_AND_DYN_L2_GAS, 0),
138
+ [Opcode.OR]: makeCost(c.AVM_OR_DYN_L2_GAS, 0),
139
+ [Opcode.XOR]: makeCost(c.AVM_XOR_DYN_L2_GAS, 0),
140
+ [Opcode.NOT]: makeCost(c.AVM_NOT_DYN_L2_GAS, 0),
141
+ [Opcode.SHL]: makeCost(c.AVM_SHL_DYN_L2_GAS, 0),
142
+ [Opcode.SHR]: makeCost(c.AVM_SHR_DYN_L2_GAS, 0),
143
+ [Opcode.CAST]: makeCost(c.AVM_CAST_DYN_L2_GAS, 0),
144
+ [Opcode.ADDRESS]: makeCost(c.AVM_ADDRESS_DYN_L2_GAS, 0),
145
+ [Opcode.STORAGEADDRESS]: makeCost(c.AVM_STORAGEADDRESS_DYN_L2_GAS, 0),
146
+ [Opcode.SENDER]: makeCost(c.AVM_SENDER_DYN_L2_GAS, 0),
147
+ [Opcode.FEEPERL2GAS]: makeCost(c.AVM_FEEPERL2GAS_DYN_L2_GAS, 0),
148
+ [Opcode.FEEPERDAGAS]: makeCost(c.AVM_FEEPERDAGAS_DYN_L2_GAS, 0),
149
+ [Opcode.TRANSACTIONFEE]: makeCost(c.AVM_TRANSACTIONFEE_DYN_L2_GAS, 0),
150
+ [Opcode.FUNCTIONSELECTOR]: makeCost(c.AVM_FUNCTIONSELECTOR_DYN_L2_GAS, 0),
151
+ [Opcode.CHAINID]: makeCost(c.AVM_CHAINID_DYN_L2_GAS, 0),
152
+ [Opcode.VERSION]: makeCost(c.AVM_VERSION_DYN_L2_GAS, 0),
153
+ [Opcode.BLOCKNUMBER]: makeCost(c.AVM_BLOCKNUMBER_DYN_L2_GAS, 0),
154
+ [Opcode.TIMESTAMP]: makeCost(c.AVM_TIMESTAMP_DYN_L2_GAS, 0),
155
+ [Opcode.COINBASE]: makeCost(c.AVM_COINBASE_DYN_L2_GAS, 0),
156
+ [Opcode.BLOCKL2GASLIMIT]: makeCost(c.AVM_BLOCKL2GASLIMIT_DYN_L2_GAS, 0),
157
+ [Opcode.BLOCKDAGASLIMIT]: makeCost(c.AVM_BLOCKDAGASLIMIT_DYN_L2_GAS, 0),
158
+ [Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_DYN_L2_GAS, 0),
159
+ [Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_DYN_L2_GAS, 0),
160
+ [Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_DYN_L2_GAS, 0),
161
+ [Opcode.JUMP]: makeCost(c.AVM_JUMP_DYN_L2_GAS, 0),
162
+ [Opcode.JUMPI]: makeCost(c.AVM_JUMPI_DYN_L2_GAS, 0),
163
+ [Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_DYN_L2_GAS, 0),
164
+ [Opcode.INTERNALRETURN]: makeCost(c.AVM_INTERNALRETURN_DYN_L2_GAS, 0),
165
+ [Opcode.SET]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
166
+ [Opcode.MOV]: makeCost(c.AVM_MOV_DYN_L2_GAS, 0),
167
+ [Opcode.CMOV]: makeCost(c.AVM_CMOV_DYN_L2_GAS, 0),
168
+ [Opcode.SLOAD]: makeCost(c.AVM_SLOAD_DYN_L2_GAS, 0),
169
+ [Opcode.SSTORE]: makeCost(c.AVM_SSTORE_DYN_L2_GAS, 0),
170
+ [Opcode.NOTEHASHEXISTS]: makeCost(c.AVM_NOTEHASHEXISTS_DYN_L2_GAS, 0),
171
+ [Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_DYN_L2_GAS, 0),
172
+ [Opcode.NULLIFIEREXISTS]: makeCost(c.AVM_NULLIFIEREXISTS_DYN_L2_GAS, 0),
173
+ [Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_DYN_L2_GAS, 0),
174
+ [Opcode.L1TOL2MSGEXISTS]: makeCost(c.AVM_L1TOL2MSGEXISTS_DYN_L2_GAS, 0),
175
+ [Opcode.HEADERMEMBER]: makeCost(c.AVM_HEADERMEMBER_DYN_L2_GAS, 0),
176
+ [Opcode.EMITUNENCRYPTEDLOG]: makeCost(c.AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, 0),
177
+ [Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_DYN_L2_GAS, 0),
178
+ [Opcode.GETCONTRACTINSTANCE]: makeCost(c.AVM_GETCONTRACTINSTANCE_DYN_L2_GAS, 0),
179
+ [Opcode.CALL]: makeCost(c.AVM_CALL_DYN_L2_GAS, 0),
180
+ [Opcode.STATICCALL]: makeCost(c.AVM_STATICCALL_DYN_L2_GAS, 0),
181
+ [Opcode.DELEGATECALL]: makeCost(c.AVM_DELEGATECALL_DYN_L2_GAS, 0),
182
+ [Opcode.RETURN]: makeCost(c.AVM_RETURN_DYN_L2_GAS, 0),
183
+ [Opcode.REVERT]: makeCost(c.AVM_REVERT_DYN_L2_GAS, 0),
184
+ [Opcode.DEBUGLOG]: makeCost(c.AVM_DEBUGLOG_DYN_L2_GAS, 0),
185
+ [Opcode.KECCAK]: makeCost(c.AVM_KECCAK_DYN_L2_GAS, 0),
186
+ [Opcode.POSEIDON2]: makeCost(c.AVM_POSEIDON2_DYN_L2_GAS, 0),
187
+ [Opcode.SHA256]: makeCost(c.AVM_SHA256_DYN_L2_GAS, 0),
188
+ [Opcode.PEDERSEN]: makeCost(c.AVM_PEDERSEN_DYN_L2_GAS, 0),
189
+ [Opcode.ECADD]: makeCost(c.AVM_ECADD_DYN_L2_GAS, 0),
190
+ [Opcode.MSM]: makeCost(c.AVM_MSM_DYN_L2_GAS, 0),
191
+ [Opcode.PEDERSENCOMMITMENT]: makeCost(c.AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0),
192
+ [Opcode.TORADIXLE]: makeCost(c.AVM_TORADIXLE_DYN_L2_GAS, 0),
193
+ [Opcode.SHA256COMPRESSION]: makeCost(c.AVM_SHA256COMPRESSION_DYN_L2_GAS, 0),
194
+ [Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_DYN_L2_GAS, 0),
133
195
  };
134
196
 
135
197
  /** Returns the fixed base gas cost for a given opcode. */
@@ -137,6 +199,10 @@ export function getBaseGasCost(opcode: Opcode): Gas {
137
199
  return BaseGasCosts[opcode];
138
200
  }
139
201
 
202
+ export function getDynamicGasCost(opcode: Opcode): Gas {
203
+ return DynamicGasCosts[opcode];
204
+ }
205
+
140
206
  /** Returns the gas cost associated with the memory operations performed. */
141
207
  export function getMemoryGasCost(args: { reads?: number; writes?: number; indirect?: number }) {
142
208
  const { reads, writes, indirect } = args;
@@ -241,7 +241,7 @@ export class EmitUnencryptedLog extends Instruction {
241
241
  const contractAddress = context.environment.address;
242
242
 
243
243
  const memoryOperations = { reads: 1 + logSize, indirect: this.indirect };
244
- context.machineState.consumeGas(this.gasCost(memoryOperations));
244
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: logSize }));
245
245
  const log = memory.getSlice(logOffset, logSize).map(f => f.toFr());
246
246
  context.persistableState.writeUnencryptedLog(contractAddress, log);
247
247
 
@@ -35,7 +35,7 @@ export class ToRadixLE extends Instruction {
35
35
  const memory = context.machineState.memory.track(this.type);
36
36
  const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.srcOffset, this.dstOffset], memory);
37
37
  const memoryOperations = { reads: 1, writes: this.numLimbs, indirect: this.indirect };
38
- context.machineState.consumeGas(this.gasCost(memoryOperations));
38
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.numLimbs }));
39
39
 
40
40
  // The radix gadget only takes in a Field
41
41
  memory.checkTag(TypeTag.FIELD, srcOffset);
@@ -67,7 +67,7 @@ abstract class ExternalCall extends Instruction {
67
67
 
68
68
  // First we consume the gas for this operation.
69
69
  const memoryOperations = { reads: calldataSize + 5, writes: 1 + this.retSize, indirect: this.indirect };
70
- context.machineState.consumeGas(this.gasCost(memoryOperations));
70
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: calldataSize + this.retSize }));
71
71
  // Then we consume the gas allocated for the nested call. The excess will be refunded later.
72
72
  // Gas allocation is capped by the amount of gas left in the current context.
73
73
  // We have to do some dancing here because the gas allocation is a field,
@@ -170,7 +170,7 @@ export class Return extends Instruction {
170
170
  public async execute(context: AvmContext): Promise<void> {
171
171
  const memoryOperations = { reads: this.copySize, indirect: this.indirect };
172
172
  const memory = context.machineState.memory.track(this.type);
173
- context.machineState.consumeGas(this.gasCost(memoryOperations));
173
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.copySize }));
174
174
 
175
175
  const [returnOffset] = Addressing.fromWire(this.indirect).resolve([this.returnOffset], memory);
176
176
 
@@ -199,7 +199,7 @@ export class Revert extends Instruction {
199
199
  public async execute(context: AvmContext): Promise<void> {
200
200
  const memoryOperations = { reads: this.retSize, indirect: this.indirect };
201
201
  const memory = context.machineState.memory.track(this.type);
202
- context.machineState.consumeGas(this.gasCost(memoryOperations));
202
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.retSize }));
203
203
 
204
204
  const [returnOffset] = Addressing.fromWire(this.indirect).resolve([this.returnOffset], memory);
205
205
 
@@ -1,7 +1,7 @@
1
1
  import { strict as assert } from 'assert';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
- import { getBaseGasCost, sumGas } from '../avm_gas.js';
4
+ import { getBaseGasCost, getDynamicGasCost, mulGas, sumGas } from '../avm_gas.js';
5
5
  import { type MemoryOperations } from '../avm_memory_types.js';
6
6
  import { type BufferCursor } from '../serialization/buffer_cursor.js';
7
7
  import { Opcode, type OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
@@ -67,12 +67,15 @@ export abstract class Instruction {
67
67
  * @param memoryOps Memory operations performed by the instruction.
68
68
  * @returns Gas cost.
69
69
  */
70
- protected gasCost(_memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
70
+ protected gasCost(ops: Partial<MemoryOperations & { indirect: number; dynMultiplier: number }> = {}) {
71
71
  const baseGasCost = getBaseGasCost(this.opcode);
72
+ // TODO: We are using a simplified gas model to reduce complexity in the circuit.
73
+ // Memory accounting will probably be removed.
72
74
  // TODO(https://github.com/AztecProtocol/aztec-packages/issues/6861): reconsider.
73
75
  // const memoryGasCost = getMemoryGasCost(memoryOps);
74
- const memoryGasCost = { l2Gas: 0, daGas: 0 };
75
- return sumGas(baseGasCost, memoryGasCost);
76
+ // const memoryGasCost = { l2Gas: 0, daGas: 0 };
77
+ const dynGasCost = mulGas(getDynamicGasCost(this.opcode), ops.dynMultiplier ?? 0);
78
+ return sumGas(baseGasCost, dynGasCost);
76
79
  }
77
80
 
78
81
  /**
@@ -208,7 +208,7 @@ export class CalldataCopy extends Instruction {
208
208
  public async execute(context: AvmContext): Promise<void> {
209
209
  const memoryOperations = { writes: this.copySize, indirect: this.indirect };
210
210
  const memory = context.machineState.memory.track(this.type);
211
- context.machineState.consumeGas(this.gasCost(memoryOperations));
211
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.copySize }));
212
212
 
213
213
  // We don't need to check tags here because: (1) the calldata is NOT in memory, and (2) we are the ones writing to destination.
214
214
  const [cdOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.cdOffset, this.dstOffset], memory);
@@ -42,7 +42,7 @@ export class SStore extends BaseStorageInstruction {
42
42
 
43
43
  const memoryOperations = { reads: this.size + 1, indirect: this.indirect };
44
44
  const memory = context.machineState.memory.track(this.type);
45
- context.machineState.consumeGas(this.gasCost(memoryOperations));
45
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.size }));
46
46
 
47
47
  const [srcOffset, slotOffset] = Addressing.fromWire(this.indirect).resolve([this.aOffset, this.bOffset], memory);
48
48
  memory.checkTag(TypeTag.FIELD, slotOffset);
@@ -72,7 +72,7 @@ export class SLoad extends BaseStorageInstruction {
72
72
  public async execute(context: AvmContext): Promise<void> {
73
73
  const memoryOperations = { writes: this.size, reads: 1, indirect: this.indirect };
74
74
  const memory = context.machineState.memory.track(this.type);
75
- context.machineState.consumeGas(this.gasCost(memoryOperations));
75
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.size }));
76
76
 
77
77
  const [slotOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.aOffset, this.bOffset], memory);
78
78
  memory.checkTag(TypeTag.FIELD, slotOffset);
@@ -156,6 +156,6 @@ export class NativeACVMSimulator implements SimulationProvider {
156
156
  return result.witness;
157
157
  };
158
158
 
159
- return await runInDirectory(this.workingDirectory, operation);
159
+ return await runInDirectory(this.workingDirectory, operation, false);
160
160
  }
161
161
  }
@@ -1,8 +1,8 @@
1
1
  import {
2
- type BlockProver,
3
2
  type FailedTx,
4
3
  NestedProcessReturnValues,
5
4
  type ProcessedTx,
5
+ type ProcessedTxHandler,
6
6
  PublicKernelType,
7
7
  type PublicProvingRequest,
8
8
  type SimulationError,
@@ -116,12 +116,13 @@ export class PublicProcessor {
116
116
  /**
117
117
  * Run each tx through the public circuit and the public kernel circuit if needed.
118
118
  * @param txs - Txs to process.
119
+ * @param processedTxHandler - Handler for processed txs in the context of block building or proving.
119
120
  * @returns The list of processed txs with their circuit simulation outputs.
120
121
  */
121
122
  public async process(
122
123
  txs: Tx[],
123
124
  maxTransactions = txs.length,
124
- blockProver?: BlockProver,
125
+ processedTxHandler?: ProcessedTxHandler,
125
126
  txValidator?: TxValidator<ProcessedTx>,
126
127
  ): Promise<[ProcessedTx[], FailedTx[], NestedProcessReturnValues[]]> {
127
128
  // The processor modifies the tx objects in place, so we need to clone them.
@@ -164,9 +165,9 @@ export class PublicProcessor {
164
165
  throw new Error(`Transaction ${invalid[0].hash} invalid after processing public functions`);
165
166
  }
166
167
  }
167
- // if we were given a prover then send the transaction to it for proving
168
- if (blockProver) {
169
- await blockProver.addNewTx(processedTx);
168
+ // if we were given a handler then send the transaction to it for block building or proving
169
+ if (processedTxHandler) {
170
+ await processedTxHandler.addNewTx(processedTx);
170
171
  }
171
172
  result.push(processedTx);
172
173
  returns = returns.concat(returnValues ?? []);
@@ -85,6 +85,8 @@ export class PublicProcessorMetrics {
85
85
  totalBytecode += event.packedPublicBytecode.length;
86
86
  }
87
87
 
88
- this.bytecodeDeployed.record(totalBytecode);
88
+ if (totalBytecode > 0) {
89
+ this.bytecodeDeployed.record(totalBytecode);
90
+ }
89
91
  }
90
92
  }