Skip to content

Commit

Permalink
chore(avm): Test cleanup and update yp to allow for zero gas
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Mar 26, 2024
1 parent 0e0748c commit da6e0d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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);
});
});

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/avm/opcodes/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions yellow-paper/docs/public-vm/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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**
```
Expand Down

0 comments on commit da6e0d9

Please sign in to comment.