From 1829741598f4334f25a85871c984b2e009c893f1 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Tue, 26 Mar 2024 12:22:24 -0300 Subject: [PATCH] chore(avm): Test cleanup and update yp to allow for zero gas (#5459) Apply suggestions from @fcarreiro in https://github.com/AztecProtocol/aztec-packages/pull/5438 --- yarn-project/simulator/src/avm/avm_simulator.test.ts | 7 ++++--- .../simulator/src/avm/opcodes/instruction.ts | 2 +- yellow-paper/docs/public-vm/execution.md | 12 ++++++------ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/yarn-project/simulator/src/avm/avm_simulator.test.ts b/yarn-project/simulator/src/avm/avm_simulator.test.ts index a16ae2474a3..77294b8f27a 100644 --- a/yarn-project/simulator/src/avm/avm_simulator.test.ts +++ b/yarn-project/simulator/src/avm/avm_simulator.test.ts @@ -42,12 +42,10 @@ describe('AVM simulator: injected bytecode', () => { const context = initContext({ env: initExecutionEnvironment({ calldata }) }); const { l2GasLeft: initialL2GasLeft } = AvmMachineState.fromState(context.machineState); const results = await new AvmSimulator(context).executeBytecode(bytecode); - const expectedL2GasUsed = ops.reduce((sum, op) => sum + op.gasCost().l2Gas, 0); expect(results.reverted).toBe(false); expect(results.output).toEqual([new Fr(3)]); - expect(expectedL2GasUsed).toBeGreaterThan(0); - expect(context.machineState.l2GasLeft).toEqual(initialL2GasLeft - expectedL2GasUsed); + expect(context.machineState.l2GasLeft).toEqual(initialL2GasLeft - 30); }); it('Should halt if runs out of gas', async () => { @@ -60,6 +58,9 @@ describe('AVM simulator: injected bytecode', () => { expect(results.reverted).toBe(true); expect(results.output).toEqual([]); expect(results.revertReason?.name).toEqual('OutOfGasError'); + expect(context.machineState.l2GasLeft).toEqual(0); + expect(context.machineState.l1GasLeft).toEqual(0); + expect(context.machineState.daGasLeft).toEqual(0); }); }); diff --git a/yarn-project/simulator/src/avm/opcodes/instruction.ts b/yarn-project/simulator/src/avm/opcodes/instruction.ts index 094e4af20d8..97e9f4a961b 100644 --- a/yarn-project/simulator/src/avm/opcodes/instruction.ts +++ b/yarn-project/simulator/src/avm/opcodes/instruction.ts @@ -29,7 +29,7 @@ export abstract class Instruction { * Loads default gas cost for the instruction from the GasCosts table. * Instruction sub-classes can override this if their gas cost is not fixed. */ - public gasCost(): GasCost { + protected gasCost(): GasCost { return GasCosts[this.opcode] ?? EmptyGasCost; } diff --git a/yellow-paper/docs/public-vm/execution.md b/yellow-paper/docs/public-vm/execution.md index 7f957f041cf..377efbcbf36 100644 --- a/yellow-paper/docs/public-vm/execution.md +++ b/yellow-paper/docs/public-vm/execution.md @@ -85,9 +85,9 @@ chargeGas(context, l1GasCost, l2GasCost, daGasCost) Before an instruction is executed, the VM enforces that there is sufficient gas remaining via the following assertions: ``` -assert machineState.l1GasLeft - instr.l1GasCost > 0 -assert machineState.l2GasLeft - instr.l2GasCost > 0 -assert machineState.daGasLeft - instr.daGasCost > 0 +assert machineState.l1GasLeft - instr.l1GasCost >= 0 +assert machineState.l2GasLeft - instr.l2GasCost >= 0 +assert machineState.daGasLeft - instr.daGasCost >= 0 ``` > Many instructions (like arithmetic operations) have 0 `l1GasCost` and `daGasCost`. Instructions only incur an L1 or DA cost if they modify the [world state](./state#avm-world-state) or [accrued substate](./state#accrued-substate). @@ -163,9 +163,9 @@ The AVM's exceptional halting conditions area listed below: 1. **Insufficient gas** ``` - assert machineState.l1GasLeft - instr.l1GasCost > 0 - assert machineState.l2GasLeft - instr.l2GasCost > 0 - assert machineState.daGasLeft - instr.l2GasCost > 0 + assert machineState.l1GasLeft - instr.l1GasCost >= 0 + assert machineState.l2GasLeft - instr.l2GasCost >= 0 + assert machineState.daGasLeft - instr.l2GasCost >= 0 ``` 1. **Invalid instruction encountered** ```