-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: calls to non-existent contracts in the AVM simulator return failure #10051
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,8 @@ import { AvmMachineState } from './avm_machine_state.js'; | |
import { isAvmBytecode } from './bytecode_utils.js'; | ||
import { | ||
AvmExecutionError, | ||
AvmRevertReason, | ||
InvalidProgramCounterError, | ||
NoBytecodeForContractError, | ||
revertReasonFromExceptionalHalt, | ||
revertReasonFromExplicitRevert, | ||
} from './errors.js'; | ||
|
@@ -83,10 +83,24 @@ export class AvmSimulator { | |
public async execute(): Promise<AvmContractCallResult> { | ||
const bytecode = await this.context.persistableState.getBytecode(this.context.environment.address); | ||
|
||
// This assumes that we will not be able to send messages to accounts without code | ||
// Pending classes and instances impl details | ||
if (!bytecode) { | ||
throw new NoBytecodeForContractError(this.context.environment.address); | ||
Comment on lines
86
to
-89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we "throw" then we'd need to wrap calls to the AVM in try-catch. It would be fine for external calls, but breaks things for enqueued calls. |
||
// revert without consuming any gas | ||
const message = `No bytecode found at: ${this.context.environment.address}. Reverting...`; | ||
const revertReason = new AvmRevertReason( | ||
message, | ||
/*failingFunction=*/ { | ||
contractAddress: this.context.environment.address, | ||
functionSelector: this.context.environment.functionSelector, | ||
}, | ||
/*noirCallStack=*/ [], | ||
); | ||
this.log.warn(message); | ||
return new AvmContractCallResult( | ||
/*reverted=*/ true, | ||
/*output=*/ [], | ||
/*gasLeft=*/ this.context.machineState.gasLeft, | ||
revertReason, | ||
); | ||
} | ||
Comment on lines
-86
to
104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it bad to hand-craft an |
||
|
||
return await this.executeBytecode(bytecode); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,42 @@ describe('External Calls', () => { | |
expect(inst.serialize()).toEqual(buf); | ||
}); | ||
|
||
it('Call to non-existent bytecode returns failure', async () => { | ||
const gasOffset = 0; | ||
const l2Gas = 2e6; | ||
const daGas = 3e6; | ||
const addrOffset = 2; | ||
const addr = new Fr(123456n); | ||
const argsOffset = 3; | ||
const args = [new Field(1), new Field(2), new Field(3)]; | ||
const argsSize = args.length; | ||
const argsSizeOffset = 20; | ||
const successOffset = 6; | ||
|
||
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, successOffset); | ||
await instruction.execute(context); | ||
|
||
const successValue = context.machineState.memory.get(successOffset); | ||
expect(successValue).toEqual(new Uint1(0n)); // failure, contract non-existent! | ||
|
||
const retValue = context.machineState.nestedReturndata; | ||
expect(retValue).toEqual([]); | ||
|
||
// should only charge gas for the call instruction itself | ||
// (all gas allocated to the nested call should be refunded) | ||
const callGasCost = instruction.gasCost(argsSize); | ||
expect(context.machineState.l2GasLeft).toEqual(initialL2Gas - callGasCost.l2Gas); | ||
expect(context.machineState.daGasLeft).toEqual(initialDaGas - callGasCost.daGas); | ||
Comment on lines
+90
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
}); | ||
|
||
it('Should execute a call correctly', async () => { | ||
const gasOffset = 0; | ||
const l2Gas = 2e6; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing non-existent contract by not mocking
getBytecode