Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(avm): Test cleanup and update yp to allow for zero gas #5459

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading