Skip to content

Commit

Permalink
fix(avm-simulator): fix message sender (#6331)
Browse files Browse the repository at this point in the history
It was already fixed for non-static calls, but not for static...

Fixes `e2e_state_vars.test.ts` under AVM (except for the assertion
expression-related failures).
  • Loading branch information
fcarreiro authored May 10, 2024
1 parent 3cda21a commit f7e2d26
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
15 changes: 11 additions & 4 deletions yarn-project/simulator/src/avm/avm_execution_environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { FunctionSelector } from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';

import { allSameExcept, anyAvmContextInputs, initExecutionEnvironment } from './fixtures/index.js';

describe('Execution Environment', () => {
const newAddress = new Fr(123456n);
const calldata = [new Fr(1n), new Fr(2n), new Fr(3n)];
const selector = FunctionSelector.empty();

it('New call should fork execution environment correctly', () => {
const executionEnvironment = initExecutionEnvironment();
const newExecutionEnvironment = executionEnvironment.deriveEnvironmentForNestedCall(newAddress, calldata);
const newExecutionEnvironment = executionEnvironment.deriveEnvironmentForNestedCall(newAddress, calldata, selector);

expect(newExecutionEnvironment).toEqual(
allSameExcept(executionEnvironment, {
Expand All @@ -20,9 +22,10 @@ describe('Execution Environment', () => {
);
});

it('New delegate call should fork execution environment correctly', () => {
// Delegate calls not supported.
it.skip('New delegate call should fork execution environment correctly', () => {
const executionEnvironment = initExecutionEnvironment();
const newExecutionEnvironment = executionEnvironment.newDelegateCall(newAddress, calldata);
const newExecutionEnvironment = executionEnvironment.newDelegateCall(newAddress, calldata, selector);

expect(newExecutionEnvironment).toEqual(
allSameExcept(executionEnvironment, {
Expand All @@ -36,7 +39,11 @@ describe('Execution Environment', () => {

it('New static call call should fork execution environment correctly', () => {
const executionEnvironment = initExecutionEnvironment();
const newExecutionEnvironment = executionEnvironment.deriveEnvironmentForNestedStaticCall(newAddress, calldata);
const newExecutionEnvironment = executionEnvironment.deriveEnvironmentForNestedStaticCall(
newAddress,
calldata,
selector,
);

expect(newExecutionEnvironment).toEqual(
allSameExcept(executionEnvironment, {
Expand Down
76 changes: 34 additions & 42 deletions yarn-project/simulator/src/avm/avm_execution_environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,72 +45,64 @@ export class AvmExecutionEnvironment {
this.calldata = [...inputs.toFields(), ...calldata];
}

public deriveEnvironmentForNestedCall(
private deriveEnvironmentForNestedCallInternal(
targetAddress: AztecAddress,
calldata: Fr[],
temporaryFunctionSelector: FunctionSelector = FunctionSelector.empty(),
): AvmExecutionEnvironment {
functionSelector: FunctionSelector,
isStaticCall: boolean,
isDelegateCall: boolean,
) {
return new AvmExecutionEnvironment(
targetAddress,
/*address=*/ targetAddress,
/*storageAddress=*/ targetAddress,
this.address,
/*sender=*/ this.address,
this.feePerL2Gas,
this.feePerDaGas,
this.contractCallDepth,
this.header,
this.globals,
this.isStaticCall,
this.isDelegateCall,
isStaticCall,
isDelegateCall,
calldata,
this.gasSettings,
this.transactionFee,
temporaryFunctionSelector,
functionSelector,
);
}

public deriveEnvironmentForNestedStaticCall(
address: AztecAddress,
public deriveEnvironmentForNestedCall(
targetAddress: AztecAddress,
calldata: Fr[],
temporaryFunctionSelector: FunctionSelector = FunctionSelector.empty(),
functionSelector: FunctionSelector = FunctionSelector.empty(),
): AvmExecutionEnvironment {
return new AvmExecutionEnvironment(
address,
/*storageAddress=*/ address,
this.sender,
this.feePerL2Gas,
this.feePerDaGas,
this.contractCallDepth,
this.header,
this.globals,
/*isStaticCall=*/ true,
this.isDelegateCall,
return this.deriveEnvironmentForNestedCallInternal(
targetAddress,
calldata,
this.gasSettings,
this.transactionFee,
temporaryFunctionSelector,
functionSelector,
/*isStaticCall=*/ false,
/*isDelegateCall=*/ false,
);
}

public newDelegateCall(
address: AztecAddress,
public deriveEnvironmentForNestedStaticCall(
targetAddress: AztecAddress,
calldata: Fr[],
temporaryFunctionSelector: FunctionSelector = FunctionSelector.empty(),
functionSelector: FunctionSelector,
): AvmExecutionEnvironment {
return new AvmExecutionEnvironment(
address,
this.storageAddress,
this.sender,
this.feePerL2Gas,
this.feePerDaGas,
this.contractCallDepth,
this.header,
this.globals,
this.isStaticCall,
/*isDelegateCall=*/ true,
return this.deriveEnvironmentForNestedCallInternal(
targetAddress,
calldata,
this.gasSettings,
this.transactionFee,
temporaryFunctionSelector,
functionSelector,
/*isStaticCall=*/ true,
/*isDelegateCall=*/ false,
);
}

public newDelegateCall(
_targetAddress: AztecAddress,
_calldata: Fr[],
_functionSelector: FunctionSelector,
): AvmExecutionEnvironment {
throw new Error('Delegate calls not supported!');
}
}

0 comments on commit f7e2d26

Please sign in to comment.