Skip to content
Closed
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
20 changes: 18 additions & 2 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
SStore,
SendL2ToL1Message,
Set,
Div,
} from './opcodes/index.js';
import { encodeToBytecode } from './serialization/bytecode_serialization.js';
import { Opcode } from './serialization/instruction_serialization.js';
Expand Down Expand Up @@ -111,8 +112,23 @@ describe('AVM simulator: injected bytecode', () => {
expect(results.reverted).toBe(true);
expect(results.output).toEqual([]);
expect(results.revertReason?.message).toEqual('Not enough L2GAS gas left');
expect(context.machineState.l2GasLeft).toEqual(0);
expect(context.machineState.daGasLeft).toEqual(0);
expect(results.gasLeft.l2Gas).toEqual(0);
expect(results.gasLeft.daGas).toEqual(0);
});

it('An exceptional halt should consume all allocated gas', async () => {
const context = initContext();

// should halt with tag mismatch
const badBytecode = encodeToBytecode([
new Div(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 0, /*dstOffset=*/ 0).as(Opcode.DIV_8, Div.wireFormat8),
]);
const results = await new AvmSimulator(context).executeBytecode(markBytecodeAsAvm(badBytecode));
expect(results.reverted).toBe(true);
expect(results.output).toEqual([]);
expect(results.revertReason?.message).toMatch(/Tag mismatch/);
expect(results.gasLeft.l2Gas).toEqual(0);
expect(results.gasLeft.daGas).toEqual(0);
});
});

Expand Down
6 changes: 4 additions & 2 deletions yarn-project/simulator/src/avm/avm_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class AvmSimulator {
return new AvmContractCallResult(
/*reverted=*/ true,
/*output=*/ [],
/*gasLeft=*/ { l2Gas: 0, daGas: 0 },
/*gasLeft=*/ { l2Gas: 0, daGas: 0 }, // consumes all allocated gas
revertReason,
);
}
Expand Down Expand Up @@ -190,8 +190,10 @@ export class AvmSimulator {
}

const revertReason = revertReasonFromExceptionalHalt(err, this.context);
// Exceptional halts consume all allocated gas
const noGasLeft = { l2Gas: 0, daGas: 0, };
// Note: "exceptional halts" cannot return data, hence [].
const results = new AvmContractCallResult(/*reverted=*/ true, /*output=*/ [], machineState.gasLeft, revertReason);
const results = new AvmContractCallResult(/*reverted=*/ true, /*output=*/ [], noGasLeft, revertReason);
this.log.debug(`Context execution results: ${results.toString()}`);

this.tallyPrintFunction();
Expand Down