Skip to content

Commit

Permalink
feat: revert on static call
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 24, 2024
1 parent e16bbc9 commit 7cd4968
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
});
17 changes: 17 additions & 0 deletions yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 7cd4968

Please sign in to comment.