-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathexternal_calls.test.ts
318 lines (275 loc) · 12.1 KB
/
external_calls.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { Fr } from '@aztec/foundation/fields';
import { mock } from 'jest-mock-extended';
import { type PublicSideEffectTraceInterface } from '../../public/side_effect_trace_interface.js';
import { type AvmContext } from '../avm_context.js';
import { Field, TypeTag, Uint8, Uint32 } from '../avm_memory_types.js';
import { markBytecodeAsAvm } from '../bytecode_utils.js';
import { adjustCalldataIndex, initContext, initHostStorage, initPersistableStateManager } from '../fixtures/index.js';
import { type HostStorage } from '../journal/host_storage.js';
import { type AvmPersistableStateManager } from '../journal/journal.js';
import { encodeToBytecode } from '../serialization/bytecode_serialization.js';
import { Opcode } from '../serialization/instruction_serialization.js';
import { mockGetBytecode, mockTraceFork } from '../test_utils.js';
import { L2GasLeft } from './context_getters.js';
import { Call, Return, Revert, StaticCall } from './external_calls.js';
import { type Instruction } from './instruction.js';
import { CalldataCopy, Set } from './memory.js';
import { SStore } from './storage.js';
describe('External Calls', () => {
let context: AvmContext;
let hostStorage: HostStorage;
let trace: PublicSideEffectTraceInterface;
let persistableState: AvmPersistableStateManager;
beforeEach(() => {
hostStorage = initHostStorage();
trace = mock<PublicSideEffectTraceInterface>();
persistableState = initPersistableStateManager({ hostStorage, trace });
context = initContext({ persistableState: persistableState });
mockTraceFork(trace); // make sure trace.fork() works on nested call
});
describe('Call', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
Call.opcode, // opcode
0x01, // indirect
...Buffer.from('12345678', 'hex'), // gasOffset
...Buffer.from('a2345678', 'hex'), // addrOffset
...Buffer.from('b2345678', 'hex'), // argsOffset
...Buffer.from('c2345678', 'hex'), // argsSizeOffset
...Buffer.from('d2345678', 'hex'), // retOffset
...Buffer.from('e2345678', 'hex'), // retSize
...Buffer.from('f2345678', 'hex'), // successOffset
...Buffer.from('f3345678', 'hex'), // functionSelectorOffset
]);
const inst = new Call(
/*indirect=*/ 0x01,
/*gasOffset=*/ 0x12345678,
/*addrOffset=*/ 0xa2345678,
/*argsOffset=*/ 0xb2345678,
/*argsSizeOffset=*/ 0xc2345678,
/*retOffset=*/ 0xd2345678,
/*retSize=*/ 0xe2345678,
/*successOffset=*/ 0xf2345678,
/*functionSelectorOffset=*/ 0xf3345678,
);
expect(Call.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
});
it('Should execute a call correctly', async () => {
const gasOffset = 0;
const l2Gas = 2e6;
const daGas = 3e6;
const addrOffset = 2;
const addr = new Fr(123456n);
const argsOffset = 3;
const valueToStore = new Fr(42);
const valueOffset = 0; // 0th entry in calldata to nested call
const slot = new Fr(100);
const slotOffset = 1; // 1st entry in calldata to nested call
const args = [new Field(valueToStore), new Field(slot), new Field(3n)];
const argsSize = args.length;
const argsSizeOffset = 20;
const retOffset = 7;
const retSize = 2;
const expectedRetValue = args.slice(0, retSize);
const successOffset = 6;
// const otherContextInstructionsL2GasCost = 780; // Includes the cost of the call itself
const otherContextInstructionsBytecode = markBytecodeAsAvm(
encodeToBytecode([
new Set(/*indirect=*/ 0, TypeTag.UINT32, adjustCalldataIndex(0), /*dstOffset=*/ 0).as(
Opcode.SET_8,
Set.wireFormat8,
),
new Set(/*indirect=*/ 0, TypeTag.UINT32, argsSize, /*dstOffset=*/ 1).as(Opcode.SET_8, Set.wireFormat8),
new CalldataCopy(/*indirect=*/ 0, /*csOffsetAddress=*/ 0, /*copySizeOffset=*/ 1, /*dstOffset=*/ 0),
new SStore(/*indirect=*/ 0, /*srcOffset=*/ valueOffset, /*slotOffset=*/ slotOffset),
new Return(/*indirect=*/ 0, /*retOffset=*/ 0, /*size=*/ 2),
]),
);
mockGetBytecode(hostStorage, otherContextInstructionsBytecode);
const { l2GasLeft: initialL2Gas, daGasLeft: initialDaGas } = context.machineState;
context.machineState.memory.set(0, new Field(l2Gas));
context.machineState.memory.set(1, new Field(daGas));
context.machineState.memory.set(2, new Field(addr));
context.machineState.memory.set(argsSizeOffset, new Uint32(argsSize));
context.machineState.memory.setSlice(3, args);
const instruction = new Call(
/*indirect=*/ 0,
gasOffset,
addrOffset,
argsOffset,
argsSizeOffset,
retOffset,
retSize,
successOffset,
/*functionSelectorOffset=*/ 0,
);
await instruction.execute(context);
const successValue = context.machineState.memory.get(successOffset);
expect(successValue).toEqual(new Uint8(1n));
const retValue = context.machineState.memory.getSlice(retOffset, retSize);
expect(retValue).toEqual(expectedRetValue);
// Check that the storage call has been merged into the parent journal
expect(await context.persistableState.peekStorage(addr, slot)).toEqual(valueToStore);
expect(context.machineState.l2GasLeft).toBeLessThan(initialL2Gas);
expect(context.machineState.daGasLeft).toEqual(initialDaGas);
});
it('Should cap to available gas if allocated is bigger', async () => {
const gasOffset = 0;
const l2Gas = 1e9;
const daGas = 1e9;
const addrOffset = 2;
const addr = new Fr(123456n);
const argsSize = 0;
const argsSizeOffset = 20;
const retOffset = 7;
const retSize = 1;
const successOffset = 6;
const otherContextInstructionsBytecode = markBytecodeAsAvm(
encodeToBytecode([
new L2GasLeft(/*indirect=*/ 0, /*dstOffset=*/ 0),
new Return(/*indirect=*/ 0, /*retOffset=*/ 0, /*size=*/ 1),
]),
);
mockGetBytecode(hostStorage, otherContextInstructionsBytecode);
const { l2GasLeft: initialL2Gas, daGasLeft: initialDaGas } = context.machineState;
context.machineState.memory.set(0, new Field(l2Gas));
context.machineState.memory.set(1, new Field(daGas));
context.machineState.memory.set(2, new Field(addr));
context.machineState.memory.set(argsSizeOffset, new Uint32(argsSize));
const instruction = new Call(
/*indirect=*/ 0,
gasOffset,
addrOffset,
/*argsOffset=*/ 0,
argsSizeOffset,
retOffset,
retSize,
successOffset,
/*functionSelectorOffset=*/ 0,
);
await instruction.execute(context);
const successValue = context.machineState.memory.get(successOffset);
expect(successValue).toEqual(new Uint8(1n));
const retValue = context.machineState.memory.get(retOffset).toBigInt();
expect(retValue).toBeLessThan(initialL2Gas);
expect(context.machineState.l2GasLeft).toBeLessThan(initialL2Gas);
expect(context.machineState.daGasLeft).toEqual(initialDaGas);
});
});
describe('Static Call', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
StaticCall.opcode, // opcode
0x01, // indirect
...Buffer.from('12345678', 'hex'), // gasOffset
...Buffer.from('a2345678', 'hex'), // addrOffset
...Buffer.from('b2345678', 'hex'), // argsOffset
...Buffer.from('c2345678', 'hex'), // argsSizeOffset
...Buffer.from('d2345678', 'hex'), // retOffset
...Buffer.from('e2345678', 'hex'), // retSize
...Buffer.from('f2345678', 'hex'), // successOffset
...Buffer.from('f3345678', 'hex'), // functionSelectorOffset
]);
const inst = new StaticCall(
/*indirect=*/ 0x01,
/*gasOffset=*/ 0x12345678,
/*addrOffset=*/ 0xa2345678,
/*argsOffset=*/ 0xb2345678,
/*argsSizeOffset=*/ 0xc2345678,
/*retOffset=*/ 0xd2345678,
/*retSize=*/ 0xe2345678,
/*successOffset=*/ 0xf2345678,
/*functionSelectorOffset=*/ 0xf3345678,
);
expect(StaticCall.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
});
it('Should fail if a static call attempts to touch storage', async () => {
const gasOffset = 0;
const gas = [new Field(0n), new Field(0n), new Field(0n)];
const addrOffset = 10;
const addr = new Field(123456n);
const argsOffset = 20;
const args = [new Field(1n), new Field(2n), new Field(3n)];
const argsSize = args.length;
const argsSizeOffset = 40;
const retOffset = 80;
const retSize = 2;
const successOffset = 70;
context.machineState.memory.setSlice(gasOffset, gas);
context.machineState.memory.set(addrOffset, addr);
context.machineState.memory.set(argsSizeOffset, new Uint32(argsSize));
context.machineState.memory.setSlice(argsOffset, args);
const otherContextInstructions: Instruction[] = [
new SStore(/*indirect=*/ 0, /*srcOffset=*/ 0, /*slotOffset=*/ 0),
];
const otherContextInstructionsBytecode = markBytecodeAsAvm(encodeToBytecode(otherContextInstructions));
mockGetBytecode(hostStorage, otherContextInstructionsBytecode);
const instruction = new StaticCall(
/*indirect=*/ 0,
gasOffset,
addrOffset,
argsOffset,
argsSizeOffset,
retOffset,
retSize,
successOffset,
/*functionSelectorOffset=*/ 0,
);
await expect(() => instruction.execute(context)).rejects.toThrow(
'Static call cannot update the state, emit L2->L1 messages or generate logs',
);
});
});
describe('RETURN', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
Return.opcode, // opcode
0x01, // indirect
...Buffer.from('12345678', 'hex'), // returnOffset
...Buffer.from('a2345678', 'hex'), // copySize
]);
const inst = new Return(/*indirect=*/ 0x01, /*returnOffset=*/ 0x12345678, /*copySize=*/ 0xa2345678);
expect(Return.deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
});
it('Should return data from the return opcode', async () => {
const returnData = [new Fr(1n), new Fr(2n), new Fr(3n)];
context.machineState.memory.set(0, new Field(1n));
context.machineState.memory.set(1, new Field(2n));
context.machineState.memory.set(2, new Field(3n));
const instruction = new Return(/*indirect=*/ 0, /*returnOffset=*/ 0, returnData.length);
await instruction.execute(context);
expect(context.machineState.getHalted()).toBe(true);
expect(context.machineState.getReverted()).toBe(false);
expect(context.machineState.getOutput()).toEqual(returnData);
});
});
describe('REVERT', () => {
it('Should (de)serialize correctly', () => {
const buf = Buffer.from([
Opcode.REVERT_16, // opcode
0x01, // indirect
...Buffer.from('1234', 'hex'), // returnOffset
...Buffer.from('a234', 'hex'), // retSize
]);
const inst = new Revert(/*indirect=*/ 0x01, /*returnOffset=*/ 0x1234, /*retSize=*/ 0xa234).as(
Opcode.REVERT_16,
Revert.wireFormat16,
);
expect(Revert.as(Revert.wireFormat16).deserialize(buf)).toEqual(inst);
expect(inst.serialize()).toEqual(buf);
});
it('Should return data and revert from the revert opcode', async () => {
const returnData = [...'assert message'].flatMap(c => new Field(c.charCodeAt(0)));
returnData.unshift(new Field(0n)); // Prepend an error selector
context.machineState.memory.setSlice(0, returnData);
const instruction = new Revert(/*indirect=*/ 0, /*returnOffset=*/ 0, returnData.length);
await instruction.execute(context);
expect(context.machineState.getHalted()).toBe(true);
expect(context.machineState.getReverted()).toBe(true);
expect(context.machineState.getOutput()).toEqual(returnData.map(f => f.toFr()));
});
});
});