-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathinterpreter.test.ts
57 lines (43 loc) · 2.08 KB
/
interpreter.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Fr } from '@aztec/foundation/fields';
import { MockProxy, mock } from 'jest-mock-extended';
import { AvmMachineState } from '../avm_machine_state.js';
import { initExecutionEnvironment } from '../fixtures/index.js';
import { AvmJournal } from '../journal/journal.js';
import { Add } from '../opcodes/arithmetic.js';
import { Jump, Return } from '../opcodes/control_flow.js';
import { Instruction } from '../opcodes/instruction.js';
import { CalldataCopy } from '../opcodes/memory.js';
import { AvmInterpreter, InvalidProgramCounterError } from './interpreter.js';
describe('interpreter', () => {
let journal: MockProxy<AvmJournal>;
beforeEach(() => {
journal = mock<AvmJournal>();
});
it('Should execute a series of instructions', () => {
const calldata: Fr[] = [new Fr(1), new Fr(2)];
const instructions: Instruction[] = [
new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 2, /*destOffset=*/ 0),
new Add(/*aOffset=*/ 0, /*bOffset=*/ 1, /*destOffset=*/ 2),
new Return(/*returnOffset=*/ 2, /*copySize=*/ 1),
];
const executionEnvironment = initExecutionEnvironment();
const context = new AvmMachineState(calldata, executionEnvironment);
const interpreter = new AvmInterpreter(context, journal, instructions);
const avmReturnData = interpreter.run();
expect(avmReturnData.reverted).toBe(false);
expect(avmReturnData.revertReason).toBeUndefined();
expect(avmReturnData.output).toEqual([new Fr(3)]);
});
it('Should revert with an invalid jump', () => {
const calldata: Fr[] = [];
const invalidJumpDestination = 22;
const instructions: Instruction[] = [new Jump(invalidJumpDestination)];
const executionEnvironment = initExecutionEnvironment();
const context = new AvmMachineState(calldata, executionEnvironment);
const interpreter = new AvmInterpreter(context, journal, instructions);
const avmReturnData = interpreter.run();
expect(avmReturnData.reverted).toBe(true);
expect(avmReturnData.revertReason).toBeInstanceOf(InvalidProgramCounterError);
expect(avmReturnData.output).toHaveLength(0);
});
});