Skip to content

Commit

Permalink
chore: stop calling public kernel tail
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Nov 12, 2024
1 parent e305f48 commit a5ff83d
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 89 deletions.
132 changes: 80 additions & 52 deletions yarn-project/simulator/src/avm/journal/journal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
type AztecAddress,
type Gas,
type PublicCallRequest,
SerializableContractInstance,
computePublicBytecodeCommitment,
} from '@aztec/circuits.js';
Expand Down Expand Up @@ -246,7 +245,7 @@ export class AvmPersistableStateManager {
/**
* Accept nested world state modifications
*/
public acceptForkedState(forkedState: AvmPersistableStateManager) {
public mergeForkedState(forkedState: AvmPersistableStateManager) {
this.publicStorage.acceptAndMerge(forkedState.publicStorage);
this.nullifiers.acceptAndMerge(forkedState.nullifiers);
}
Expand Down Expand Up @@ -290,28 +289,23 @@ export class AvmPersistableStateManager {
return undefined;
}
}
/**
* Accept the nested call's state and trace the nested call
*/
public async processNestedCall(

public async traceNestedCall(
forkedState: AvmPersistableStateManager,
nestedEnvironment: AvmExecutionEnvironment,
startGasLeft: Gas,
endGasLeft: Gas,
bytecode: Buffer,
avmCallResults: AvmContractCallResult,
) {
if (!avmCallResults.reverted) {
this.acceptForkedState(forkedState);
}
const functionName = await getPublicFunctionDebugName(
this.worldStateDB,
nestedEnvironment.address,
nestedEnvironment.functionSelector,
nestedEnvironment.calldata,
);

this.log.verbose(`[AVM] Calling nested function ${functionName}`);
this.log.verbose(`[AVM] Tracing nested external contract call ${functionName}`);

this.trace.traceNestedCall(
forkedState.trace,
Expand All @@ -324,46 +318,80 @@ export class AvmPersistableStateManager {
);
}

public async mergeStateForEnqueuedCall(
forkedState: AvmPersistableStateManager,
/** The call request from private that enqueued this call. */
publicCallRequest: PublicCallRequest,
/** The call's calldata */
calldata: Fr[],
/** Did the call revert? */
reverted: boolean,
) {
if (!reverted) {
this.acceptForkedState(forkedState);
}
const functionName = await getPublicFunctionDebugName(
this.worldStateDB,
publicCallRequest.contractAddress,
publicCallRequest.functionSelector,
calldata,
);

this.log.verbose(`[AVM] Encountered enqueued public call starting with function ${functionName}`);

this.trace.traceEnqueuedCall(forkedState.trace, publicCallRequest, calldata, reverted);
}

public mergeStateForPhase(
/** The forked state manager used by app logic */
forkedState: AvmPersistableStateManager,
/** The call requests for each enqueued call in app logic. */
publicCallRequests: PublicCallRequest[],
/** The calldatas for each enqueued call in app logic */
calldatas: Fr[][],
/** Did the any enqueued call in app logic revert? */
reverted: boolean,
) {
if (!reverted) {
this.acceptForkedState(forkedState);
}

this.log.verbose(`[AVM] Encountered app logic phase`);

this.trace.traceExecutionPhase(forkedState.trace, publicCallRequests, calldatas, reverted);
}
///**
// * Accept the nested call's state and trace the nested call
// */
//public async processNestedCall(
// forkedState: AvmPersistableStateManager,
// nestedEnvironment: AvmExecutionEnvironment,
// startGasLeft: Gas,
// endGasLeft: Gas,
// bytecode: Buffer,
// avmCallResults: AvmContractCallResult,
//) {
// if (!avmCallResults.reverted) {
// this.mergeForkedState(forkedState);
// }
// const functionName = await getPublicFunctionDebugName(
// this.worldStateDB,
// nestedEnvironment.address,
// nestedEnvironment.functionSelector,
// nestedEnvironment.calldata,
// );

// this.log.verbose(`[AVM] Calling nested function ${functionName}`);

// this.trace.traceNestedCall(
// forkedState.trace,
// nestedEnvironment,
// startGasLeft,
// endGasLeft,
// bytecode,
// avmCallResults,
// functionName,
// );
//}

//public async mergeStateForEnqueuedCall(
// forkedState: AvmPersistableStateManager,
// /** The call request from private that enqueued this call. */
// publicCallRequest: PublicCallRequest,
// /** The call's calldata */
// calldata: Fr[],
// /** Did the call revert? */
// reverted: boolean,
//) {
// if (!reverted) {
// this.mergeForkedState(forkedState);
// }
// const functionName = await getPublicFunctionDebugName(
// this.worldStateDB,
// publicCallRequest.contractAddress,
// publicCallRequest.functionSelector,
// calldata,
// );

// this.log.verbose(`[AVM] Encountered enqueued public call starting with function ${functionName}`);

// this.trace.traceEnqueuedCall(forkedState.trace, publicCallRequest, calldata, reverted);
//}

//public mergeStateForPhase(
// /** The forked state manager used by app logic */
// forkedState: AvmPersistableStateManager,
// /** The call requests for each enqueued call in app logic. */
// publicCallRequests: PublicCallRequest[],
// /** The calldatas for each enqueued call in app logic */
// calldatas: Fr[][],
// /** Did the any enqueued call in app logic revert? */
// reverted: boolean,
//) {
// if (!reverted) {
// this.mergeForkedState(forkedState);
// }

// this.log.verbose(`[AVM] Encountered app logic phase`);

// this.trace.traceExecutionPhase(forkedState.trace, publicCallRequests, calldatas, reverted);
//}
}
5 changes: 4 additions & 1 deletion yarn-project/simulator/src/avm/opcodes/external_calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ abstract class ExternalCall extends Instruction {
context.machineState.refundGas(gasLeftToGas(nestedContext.machineState));

// Accept the nested call's state and trace the nested call
await context.persistableState.processNestedCall(
if (success) {
context.persistableState.mergeForkedState(nestedContext.persistableState);
}
await context.persistableState.traceNestedCall(
/*nestedState=*/ nestedContext.persistableState,
/*nestedEnvironment=*/ nestedContext.environment,
/*startGasLeft=*/ Gas.from(allocatedGas),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,16 @@ describe('Enqueued-call Side Effect Trace', () => {
// parent absorbs child's side effects
const parentSideEffects = trace.getSideEffects();
const childSideEffects = nestedTrace.getSideEffects();
// TODO(dbanks12): confirm that all hints were merged from child
if (callResults.reverted) {
expect(parentSideEffects.publicDataReads).toEqual(childSideEffects.publicDataReads);
expect(parentSideEffects.publicDataWrites).toEqual(childSideEffects.publicDataWrites);
expect(parentSideEffects.noteHashReadRequests).toEqual(childSideEffects.noteHashReadRequests);
expect(parentSideEffects.publicDataReads).toEqual([]);
expect(parentSideEffects.publicDataWrites).toEqual([]);
expect(parentSideEffects.noteHashReadRequests).toEqual([]);
expect(parentSideEffects.noteHashes).toEqual([]);
expect(parentSideEffects.nullifierReadRequests).toEqual(childSideEffects.nullifierReadRequests);
expect(parentSideEffects.nullifierNonExistentReadRequests).toEqual(
childSideEffects.nullifierNonExistentReadRequests,
);
expect(parentSideEffects.nullifiers).toEqual(childSideEffects.nullifiers);
expect(parentSideEffects.l1ToL2MsgReadRequests).toEqual(childSideEffects.l1ToL2MsgReadRequests);
expect(parentSideEffects.nullifierReadRequests).toEqual([]);
expect(parentSideEffects.nullifierNonExistentReadRequests).toEqual([]);
expect(parentSideEffects.nullifiers).toEqual([]);
expect(parentSideEffects.l1ToL2MsgReadRequests).toEqual([]);
expect(parentSideEffects.l2ToL1Msgs).toEqual([]);
expect(parentSideEffects.unencryptedLogs).toEqual([]);
expect(parentSideEffects.unencryptedLogsHashes).toEqual([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { UnencryptedFunctionL2Logs, UnencryptedL2Log } from '@aztec/circuit-types';
import {
AvmAccumulatedData,
AvmCircuitPublicInputs,
AvmContractBytecodeHints,
AvmContractInstanceHint,
AvmEnqueuedCallHint,
Expand All @@ -11,6 +13,8 @@ import {
type ContractClassIdPreimage,
EthAddress,
Gas,
type GasSettings,
type GlobalVariables,
L2ToL1Message,
LogHash,
MAX_ENCRYPTED_LOGS_PER_TX,
Expand All @@ -28,11 +32,14 @@ import {
MAX_UNENCRYPTED_LOGS_PER_TX,
NoteHash,
Nullifier,
PrivateToAvmAccumulatedData,
PrivateToAvmAccumulatedDataArrayLengths,
PublicAccumulatedData,
PublicAccumulatedDataArrayLengths,
PublicCallRequest,
PublicDataRead,
PublicDataUpdateRequest,
PublicDataWrite,
PublicInnerCallRequest,
PublicValidationRequestArrayLengths,
PublicValidationRequests,
Expand All @@ -44,13 +51,15 @@ import {
ScopedReadRequest,
SerializableContractInstance,
TreeLeafReadRequest,
type TreeSnapshots,
VMCircuitPublicInputs,
} from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot, siloNullifier } from '@aztec/circuits.js/hash';
import { makeTuple } from '@aztec/foundation/array';
import { padArrayEnd } from '@aztec/foundation/collection';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { type Tuple } from '@aztec/foundation/serialize';

import { type AvmContractCallResult } from '../avm/avm_contract_call_result.js';
import { type AvmExecutionEnvironment } from '../avm/avm_execution_environment.js';
Expand Down Expand Up @@ -93,6 +102,9 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI
/** The side effect counter increments with every call to the trace. */
private sideEffectCounter: number;

//private publicSetupCallRequests: PublicCallRequest[] = [];
//private publicAppLogicCallRequests: PublicCallRequest[] = [];
//private publicTeardownCallRequest: PublicCallRequest[] = [];
private enqueuedCalls: PublicCallRequest[] = [];

private publicDataReads: PublicDataRead[] = [];
Expand Down Expand Up @@ -505,14 +517,14 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI
// TODO(dbanks12): accept & merge nested trace's hints!
// TODO(dbanks12): What should happen to side effect counter on revert?
this.sideEffectCounter = nestedTrace.sideEffectCounter;
this.publicDataReads.push(...nestedTrace.publicDataReads);
this.publicDataWrites.push(...nestedTrace.publicDataWrites);
this.noteHashReadRequests.push(...nestedTrace.noteHashReadRequests);
//this.publicDataReads.push(...nestedTrace.publicDataReads);
//this.publicDataWrites.push(...nestedTrace.publicDataWrites);
//this.noteHashReadRequests.push(...nestedTrace.noteHashReadRequests);
// new noteHashes are tossed on revert
this.nullifierReadRequests.push(...nestedTrace.nullifierReadRequests);
this.nullifierNonExistentReadRequests.push(...nestedTrace.nullifierNonExistentReadRequests);
this.nullifiers.push(...nestedTrace.nullifiers);
this.l1ToL2MsgReadRequests.push(...nestedTrace.l1ToL2MsgReadRequests);
//this.nullifierReadRequests.push(...nestedTrace.nullifierReadRequests);
//this.nullifierNonExistentReadRequests.push(...nestedTrace.nullifierNonExistentReadRequests);
//this.nullifiers.push(...nestedTrace.nullifiers);
//this.l1ToL2MsgReadRequests.push(...nestedTrace.l1ToL2MsgReadRequests);
// new l2-to-l1 messages are tossed on revert
// new unencrypted logs are tossed on revert
}
Expand Down Expand Up @@ -595,6 +607,47 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI
);
}

public toAvmCircuitPublicInputs(
/** Globals. */
globalVariables: GlobalVariables,
/** Start tree snapshots. */
startTreeSnapshots: TreeSnapshots,
/** Gas used at start of TX. */
startGasUsed: Gas,
/** How much gas was available for this public execution. */
gasLimits: GasSettings,
/** Call requests for setup phase. */
publicSetupCallRequests: Tuple<PublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_TX>,
/** Call requests for app logic phase. */
publicAppLogicCallRequests: Tuple<PublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_TX>,
/** Call request for teardown phase. */
publicTeardownCallRequest: PublicCallRequest,
/** End tree snapshots. */
endTreeSnapshots: TreeSnapshots,
/** Transaction fee. */
transactionFee: Fr,
/** The call's results */
reverted: boolean,
): AvmCircuitPublicInputs {
return new AvmCircuitPublicInputs(
globalVariables,
startTreeSnapshots,
startGasUsed,
gasLimits,
publicSetupCallRequests,
publicAppLogicCallRequests,
publicTeardownCallRequest,
/*previousNonRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(),
/*previousRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(),
/*previousNonRevertibleAccumulatedDataArray=*/ PrivateToAvmAccumulatedData.empty(),
/*previousRevertibleAccumulatedDataArray=*/ PrivateToAvmAccumulatedData.empty(),
endTreeSnapshots,
/*accumulatedData=*/ this.getAvmAccumulatedData(),
transactionFee,
reverted,
);
}

public toPublicFunctionCallResult(
/** The execution environment of the nested call. */
_avmEnvironment: AvmExecutionEnvironment,
Expand Down Expand Up @@ -635,6 +688,28 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI
);
}

private getAvmAccumulatedData() {
return new AvmAccumulatedData(
padArrayEnd(
this.noteHashes.map(n => n.value),
Fr.zero(),
MAX_NOTE_HASHES_PER_TX,
),
padArrayEnd(
this.nullifiers.map(n => n.value),
Fr.zero(),
MAX_NULLIFIERS_PER_TX,
),
padArrayEnd(this.l2ToL1Messages, ScopedL2ToL1Message.empty(), MAX_L2_TO_L1_MSGS_PER_TX),
padArrayEnd(this.unencryptedLogsHashes, ScopedLogHash.empty(), MAX_UNENCRYPTED_LOGS_PER_TX),
padArrayEnd(
this.publicDataWrites.map(w => new PublicDataWrite(w.leafSlot, w.newValue)),
PublicDataWrite.empty(),
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
),
);
}

private getAccumulatedData(gasUsed: Gas) {
return new PublicAccumulatedData(
padArrayEnd(this.noteHashes, ScopedNoteHash.empty(), MAX_NOTE_HASHES_PER_TX),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ describe('enqueued_calls_processor', () => {
// squashed
// new PublicDataWrite(computePublicDataTreeLeafSlot(contractAddress, contractSlotA), fr(0x101)),
new PublicDataWrite(computePublicDataTreeLeafSlot(contractAddress, contractSlotB), fr(0x151)),

new PublicDataWrite(computePublicDataTreeLeafSlot(contractAddress, contractSlotA), fr(0x103)),
// squashed
// new PublicDataWrite(computePublicDataTreeLeafSlot(contractAddress, contractSlotC), fr(0x201)),
Expand Down
Loading

0 comments on commit a5ff83d

Please sign in to comment.