From 7cd4968145d1bcd9fb02bc4f1d55c5994c9b4224 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Wed, 24 Jan 2024 03:18:07 +0000 Subject: [PATCH] feat: revert on static call --- .../src/avm/opcodes/accrued_substate.test.ts | 17 +++++++++++++++++ .../src/avm/opcodes/accrued_substate.ts | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.test.ts index b857f4c8db9..ee918809ba9 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.test.ts @@ -7,6 +7,7 @@ import { initExecutionEnvironment } from '../fixtures/index.js'; import { HostStorage } from '../journal/host_storage.js'; import { AvmJournal } from '../journal/journal.js'; import { EmitNoteHash, EmitNullifier, EmitUnencryptedLog, SendL2ToL1Message } from './accrued_substate.js'; +import { StaticCallStorageAlterError } from './storage.js'; describe('Accrued Substate', () => { let journal: AvmJournal; @@ -63,4 +64,20 @@ describe('Accrued Substate', () => { const journalState = journal.flush(); expect(journalState.newLogs).toEqual([values]); }); + + it('All substate instructions should fail within a static call', () => { + const executionEnvironment = initExecutionEnvironment({ isStaticCall: true }); + machineState = new AvmMachineState([], executionEnvironment); + + const instructions = [ + new EmitNoteHash(0), + new EmitNullifier(0), + new EmitUnencryptedLog(0, 1), + new SendL2ToL1Message(0, 1), + ]; + + for (const instruction of instructions) { + expect(() => instruction.execute(machineState, journal)).toThrowError(StaticCallStorageAlterError); + } + }); }); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.ts b/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.ts index 32bb2f24026..d26270f6769 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.ts @@ -1,6 +1,7 @@ import { AvmMachineState } from '../avm_machine_state.js'; import { AvmJournal } from '../journal/journal.js'; import { Instruction } from './instruction.js'; +import { StaticCallStorageAlterError } from './storage.js'; export class EmitNoteHash extends Instruction { static type: string = 'EMITNOTEHASH'; @@ -11,6 +12,10 @@ export class EmitNoteHash extends Instruction { } execute(machineState: AvmMachineState, journal: AvmJournal): void { + if (machineState.executionEnvironment.isStaticCall) { + throw new StaticCallStorageAlterError(); + } + const noteHash = machineState.readMemory(this.noteHashOffset); journal.writeNoteHash(noteHash); @@ -28,6 +33,10 @@ export class EmitNullifier extends Instruction { } execute(machineState: AvmMachineState, journal: AvmJournal): void { + if (machineState.executionEnvironment.isStaticCall) { + throw new StaticCallStorageAlterError(); + } + const nullifier = machineState.readMemory(this.nullifierOffset); journal.writeNullifier(nullifier); @@ -45,6 +54,10 @@ export class EmitUnencryptedLog extends Instruction { } execute(machineState: AvmMachineState, journal: AvmJournal): void { + if (machineState.executionEnvironment.isStaticCall) { + throw new StaticCallStorageAlterError(); + } + const log = machineState.readMemoryChunk(this.logOffset, this.logSize); journal.writeLog(log); @@ -62,6 +75,10 @@ export class SendL2ToL1Message extends Instruction { } execute(machineState: AvmMachineState, journal: AvmJournal): void { + if (machineState.executionEnvironment.isStaticCall) { + throw new StaticCallStorageAlterError(); + } + const msg = machineState.readMemoryChunk(this.msgOffset, this.msgSize); journal.writeLog(msg);