Skip to content

Commit

Permalink
chore: refactor AVM simulator's side-effect tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Jun 25, 2024
1 parent dec622f commit 4cfe481
Show file tree
Hide file tree
Showing 34 changed files with 2,070 additions and 2,311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,19 @@ contract AvmTest {
// Use the standard context interface to check for a nullifier
#[aztec(public)]
fn nullifier_exists(nullifier: Field) -> bool {
context.nullifier_exists(nullifier, context.this_address())
context.nullifier_exists(nullifier, context.storage_address())
}

#[aztec(public)]
fn assert_nullifier_exists(nullifier: Field) {
assert(context.nullifier_exists(nullifier, context.this_address()), "Nullifier doesn't exist!");
assert(context.nullifier_exists(nullifier, context.storage_address()), "Nullifier doesn't exist!");
}

// Use the standard context interface to emit a new nullifier
#[aztec(public)]
fn emit_nullifier_and_check(nullifier: Field) {
context.push_new_nullifier(nullifier, 0);
let exists = context.nullifier_exists(nullifier, context.this_address());
let exists = context.nullifier_exists(nullifier, context.storage_address());
assert(exists, "Nullifier was just created, but its existence wasn't detected!");
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ export const randomContractArtifact = (): ContractArtifact => ({
notes: {},
});

export const randomContractInstanceWithAddress = (opts: { contractClassId?: Fr } = {}): ContractInstanceWithAddress =>
SerializableContractInstance.random(opts).withAddress(AztecAddress.random());
export const randomContractInstanceWithAddress = (opts: { contractClassId?: Fr } = {}, address: AztecAddress = AztecAddress.random()): ContractInstanceWithAddress =>
SerializableContractInstance.random(opts).withAddress(address);

export const randomDeployedContract = () => {
const artifact = randomContractArtifact();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ export class ContractStorageUpdateRequest {
/**
* Optional side effect counter tracking position of this event in tx execution.
*/
public readonly sideEffectCounter: number,
public readonly counter: number,
/**
* Contract address whose storage is being read.
*/
public contractAddress?: AztecAddress, // TODO: Should not be optional. This is a temporary hack to silo the storage slot with the correct address for nested executions.
) {}

toBuffer() {
return serializeToBuffer(this.storageSlot, this.newValue, this.sideEffectCounter);
return serializeToBuffer(this.storageSlot, this.newValue, this.counter);
}

static fromBuffer(buffer: Buffer | BufferReader) {
Expand All @@ -52,7 +55,7 @@ export class ContractStorageUpdateRequest {
* @returns The array.
*/
static getFields(fields: FieldsOf<ContractStorageUpdateRequest>) {
return [fields.storageSlot, fields.newValue, fields.sideEffectCounter, fields.contractAddress] as const;
return [fields.storageSlot, fields.newValue, fields.counter, fields.contractAddress] as const;
}

static empty() {
Expand All @@ -65,12 +68,12 @@ export class ContractStorageUpdateRequest {

toFriendlyJSON() {
return `Slot=${this.storageSlot.toFriendlyJSON()}: ${this.newValue.toFriendlyJSON()}, sideEffectCounter=${
this.sideEffectCounter
this.counter
}`;
}

toFields(): Fr[] {
const fields = [this.storageSlot, this.newValue, new Fr(this.sideEffectCounter)];
const fields = [this.storageSlot, this.newValue, new Fr(this.counter)];
if (fields.length !== CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH) {
throw new Error(
`Invalid number of fields for ContractStorageUpdateRequest. Expected ${CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH}, got ${fields.length}`,
Expand Down
2 changes: 0 additions & 2 deletions yarn-project/end-to-end/src/e2e_block_building.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { StatefulTestContractArtifact } from '@aztec/noir-contracts.js';
import { TestContract } from '@aztec/noir-contracts.js/Test';
import { TokenContract } from '@aztec/noir-contracts.js/Token';

import 'jest-extended';

import { TaggedLog } from '../../circuit-types/src/logs/l1_payload/tagged_log.js';
import { DUPLICATE_NULLIFIER_ERROR } from './fixtures/fixtures.js';
import { setup } from './fixtures/utils.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ export function mapStorageUpdateRequestToNoir(
return {
storage_slot: mapFieldToNoir(storageUpdateRequest.storageSlot),
new_value: mapFieldToNoir(storageUpdateRequest.newValue),
counter: mapNumberToNoir(storageUpdateRequest.sideEffectCounter),
counter: mapNumberToNoir(storageUpdateRequest.counter),
};
}
/**
Expand Down Expand Up @@ -1855,7 +1855,7 @@ export function mapStorageReadToNoir(storageRead: ContractStorageRead): StorageR
return {
storage_slot: mapFieldToNoir(storageRead.storageSlot),
current_value: mapFieldToNoir(storageRead.currentValue),
counter: mapNumberToNoir(storageRead.sideEffectCounter),
counter: mapNumberToNoir(storageRead.counter),
};
}
/**
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/simulator/src/avm/avm_context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Avm Context', () => {
allSameExcept(context.environment, {
address: newAddress,
storageAddress: newAddress,
contractCallDepth: Fr.ONE,
// Calldata also includes AvmContextInputs
calldata: anyAvmContextInputs().concat(newCalldata),
isStaticCall: false,
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('Avm Context', () => {
allSameExcept(context.environment, {
address: newAddress,
storageAddress: newAddress,
contractCallDepth: Fr.ONE,
// Calldata also includes AvmContextInputs
calldata: anyAvmContextInputs().concat(newCalldata),
isStaticCall: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Execution Environment', () => {
allSameExcept(executionEnvironment, {
address: newAddress,
storageAddress: newAddress,
contractCallDepth: Fr.ONE,
// Calldata also includes AvmContextInputs
calldata: anyAvmContextInputs().concat(calldata),
}),
Expand All @@ -30,6 +31,7 @@ describe('Execution Environment', () => {
expect(newExecutionEnvironment).toEqual(
allSameExcept(executionEnvironment, {
address: newAddress,
contractCallDepth: Fr.ONE,
isDelegateCall: true,
// Calldata also includes AvmContextInputs
calldata: anyAvmContextInputs().concat(calldata),
Expand All @@ -49,6 +51,7 @@ describe('Execution Environment', () => {
allSameExcept(executionEnvironment, {
address: newAddress,
storageAddress: newAddress,
contractCallDepth: Fr.ONE,
isStaticCall: true,
// Calldata also includes AvmContextInputs
calldata: anyAvmContextInputs().concat(calldata),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class AvmExecutionEnvironment {
/*sender=*/ this.address,
this.feePerL2Gas,
this.feePerDaGas,
this.contractCallDepth,
this.contractCallDepth.add(Fr.ONE),
this.header,
this.globals,
isStaticCall,
Expand Down
Loading

0 comments on commit 4cfe481

Please sign in to comment.