Skip to content

Commit

Permalink
[WIP] chore: refactoring public executor interface (#9701)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Nov 6, 2024
1 parent 21635e6 commit 9bf1802
Show file tree
Hide file tree
Showing 25 changed files with 511 additions and 664 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ namespace bb::avm_trace {
using FF = AvmFlavorSettings::FF;
using AffinePoint = grumpkin::g1::affine_element;

struct EnqueuedCallHint {
FF contract_address;
std::vector<FF> calldata;

MSGPACK_FIELDS(contract_address, calldata);
};

struct ExternalCallHint {
FF success;
std::vector<FF> return_data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ fn propagate_public_data_writes(
let writes = public_call.contract_storage_update_requests;
for i in 0..writes.len() {
let write = writes[i];
// WARNING: What does this check accomplish?
if write.counter != 0 {
data.public_data_update_requests.push(
PublicDataUpdateRequest::from_contract_storage_update_request(
Expand Down
62 changes: 0 additions & 62 deletions noir-projects/noir-protocol-circuits/pubkern.sh

This file was deleted.

2 changes: 1 addition & 1 deletion yarn-project/bb-prover/src/avm_proving.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const proveAndVerifyAvmTestContract = async (
);
}

const pxResult = trace.toPublicExecutionResult(
const pxResult = trace.toPublicFunctionCallResult(
environment,
startGas,
/*endGasLeft=*/ Gas.from(context.machineState.gasLeft),
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/bb-prover/src/test/test_avm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import {
} from '@aztec/circuits.js';
import { computeVarArgsHash } from '@aztec/circuits.js/hash';
import { padArrayEnd } from '@aztec/foundation/collection';
import { type PublicExecutionResult } from '@aztec/simulator';
import { type PublicFunctionCallResult } from '@aztec/simulator';

// TODO: pub somewhere more usable - copied from abstract phase manager
export function getPublicInputs(result: PublicExecutionResult): PublicCircuitPublicInputs {
export function getPublicInputs(result: PublicFunctionCallResult): PublicCircuitPublicInputs {
return PublicCircuitPublicInputs.from({
callContext: result.executionRequest.callContext,
proverAddress: AztecAddress.ZERO,
Expand Down
41 changes: 7 additions & 34 deletions yarn-project/circuits.js/src/structs/avm/avm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,12 @@ import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { type FieldsOf } from '@aztec/foundation/types';

//import { Decoder, Encoder, addExtension } from 'msgpackr';
import { type ContractClassIdPreimage } from '../../contract/contract_class_id.js';
import { PublicKeys } from '../../types/public_keys.js';
import { Gas } from '../gas.js';
import { PublicCircuitPublicInputs } from '../public_circuit_public_inputs.js';
import { Vector } from '../shared.js';

//addExtension({
// Class: Fr,
// write: fr => fr.toBuffer(),
//});
//
//const encoder = new Encoder({
// // always encode JS objects as MessagePack maps
// // this makes it compatible with other MessagePack decoders
// useRecords: false,
// int64AsType: 'bigint',
//});
//
///** A long-lived msgpack decoder */
//const decoder = new Decoder({
// useRecords: false,
// int64AsType: 'bigint',
//});

export class AvmEnqueuedCallHint {
public readonly contractAddress: Fr;
public readonly calldata: Vector<Fr>;
Expand All @@ -41,7 +22,6 @@ export class AvmEnqueuedCallHint {
* @returns - The inputs serialized to a buffer.
*/
toBuffer() {
//return encoder.encode(this);
return serializeToBuffer(...AvmEnqueuedCallHint.getFields(this));
}

Expand Down Expand Up @@ -87,7 +67,6 @@ export class AvmEnqueuedCallHint {
static fromBuffer(buff: Buffer | BufferReader) {
const reader = BufferReader.asReader(buff);
return new AvmEnqueuedCallHint(Fr.fromBuffer(reader), reader.readVector(Fr));
//return decoder.decode(buff);
}

/**
Expand Down Expand Up @@ -455,7 +434,6 @@ export class AvmContractBytecodeHints {
}
}

// TODO(dbanks12): rename AvmCircuitHints
export class AvmExecutionHints {
public readonly enqueuedCalls: Vector<AvmEnqueuedCallHint>;
public readonly storageValues: Vector<AvmKeyValueHint>;
Expand Down Expand Up @@ -500,14 +478,6 @@ export class AvmExecutionHints {
*/
toBuffer() {
return serializeToBuffer(...AvmExecutionHints.getFields(this));
//this.storageValues.items,
//this.noteHashExists.items,
//this.nullifierExists.items,
//this.l1ToL2MessageExists.items,
//this.externalCalls.items,
//this.contractInstances.items,
//this.contractBytecodeHints.items,
//);
}

/**
Expand Down Expand Up @@ -542,7 +512,7 @@ export class AvmExecutionHints {
*/
static from(fields: FieldsOf<AvmExecutionHints>): AvmExecutionHints {
return new AvmExecutionHints(
//fields.enqueuedCalls.items,
// omit enqueued call hints until they're implemented in C++
new Array<AvmEnqueuedCallHint>(),
fields.storageValues.items,
fields.noteHashExists.items,
Expand All @@ -561,6 +531,7 @@ export class AvmExecutionHints {
*/
static getFields(fields: FieldsOf<AvmExecutionHints>) {
return [
// omit enqueued call hints until they're implemented in C++
//fields.enqueuedCalls,
fields.storageValues,
fields.noteHashExists,
Expand All @@ -580,8 +551,7 @@ export class AvmExecutionHints {
static fromBuffer(buff: Buffer | BufferReader): AvmExecutionHints {
const reader = BufferReader.asReader(buff);
return new AvmExecutionHints(
//encoder.decode(buff),
//reader.readVector(AvmEnqueuedCallHint),
// omit enqueued call hints until they're implemented in C++
new Array<AvmEnqueuedCallHint>(),
reader.readVector(AvmKeyValueHint),
reader.readVector(AvmKeyValueHint),
Expand Down Expand Up @@ -656,6 +626,10 @@ export class AvmCircuitInputs {
);
}

static empty(): AvmCircuitInputs {
return new AvmCircuitInputs('', [], PublicCircuitPublicInputs.empty(), AvmExecutionHints.empty());
}

/**
* Creates a new instance from fields.
* @param fields - Fields to create the instance from.
Expand Down Expand Up @@ -686,7 +660,6 @@ export class AvmCircuitInputs {
/*calldata=*/ reader.readVector(Fr),
PublicCircuitPublicInputs.fromBuffer(reader),
AvmExecutionHints.fromBuffer(reader),
//decoder.decode(reader.readBuffer()),
);
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/ivc-integration/src/avm_integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const proveAvmTestContract = async (
);
}

const pxResult = trace.toPublicExecutionResult(
const pxResult = trace.toPublicFunctionCallResult(
environment,
startGas,
/*endGasLeft=*/ Gas.from(context.machineState.gasLeft),
Expand Down
6 changes: 2 additions & 4 deletions yarn-project/prover-client/src/mocks/test_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,16 @@ export class TestContext {
execution: PublicExecutionRequest,
_globalVariables: GlobalVariables,
allocatedGas: Gas,
transactionFee?: Fr,
_transactionFee?: Fr,
) => {
for (const tx of txs) {
const allCalls = tx.publicTeardownFunctionCall.isEmpty()
? tx.enqueuedPublicFunctionCalls
: [...tx.enqueuedPublicFunctionCalls, tx.publicTeardownFunctionCall];
for (const request of allCalls) {
if (execution.callContext.equals(request.callContext)) {
const result = PublicExecutionResultBuilder.fromPublicExecutionRequest({ request }).build({
startGasLeft: allocatedGas,
const result = PublicExecutionResultBuilder.empty().build({
endGasLeft: allocatedGas,
transactionFee,
});
return Promise.resolve(result);
}
Expand Down
46 changes: 15 additions & 31 deletions yarn-project/simulator/src/avm/journal/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export class AvmPersistableStateManager {
// TODO(5818): make private once no longer accessed in executor
public readonly trace: PublicSideEffectTraceInterface,
/** Public storage, including cached writes */
// TODO(5818): make private once no longer accessed in executor
public readonly publicStorage: PublicStorage = new PublicStorage(worldStateDB),
private readonly publicStorage: PublicStorage = new PublicStorage(worldStateDB),
/** Nullifier set, including cached/recently-emitted nullifiers */
private readonly nullifiers: NullifierManager = new NullifierManager(worldStateDB),
) {}
Expand Down Expand Up @@ -72,20 +71,6 @@ export class AvmPersistableStateManager {
);
}

public forkWithPendingSiloedNullifiers(incrementSideEffectCounter: boolean, pendingSiloedNullifiers: Fr[]) {
const prePhaseNullifiers = NullifierManager.newWithPendingSiloedNullifiers(
this.worldStateDB,
pendingSiloedNullifiers,
this.nullifiers,
);
return new AvmPersistableStateManager(
this.worldStateDB,
this.trace.fork(incrementSideEffectCounter),
this.publicStorage.fork(),
prePhaseNullifiers, // TODO(dbanks12): need a fork()?
);
}

/**
* Write to public storage, journal/trace the write.
*
Expand Down Expand Up @@ -257,9 +242,9 @@ export class AvmPersistableStateManager {
/**
* Accept nested world state modifications
*/
public acceptNestedCallState(nestedState: AvmPersistableStateManager) {
this.publicStorage.acceptAndMerge(nestedState.publicStorage);
this.nullifiers.acceptAndMerge(nestedState.nullifiers);
public acceptForkedState(forkedState: AvmPersistableStateManager) {
this.publicStorage.acceptAndMerge(forkedState.publicStorage);
this.nullifiers.acceptAndMerge(forkedState.nullifiers);
}

/**
Expand Down Expand Up @@ -301,20 +286,19 @@ export class AvmPersistableStateManager {
return undefined;
}
}

/**
* Accept the nested call's state and trace the nested call
*/
public async processNestedCall(
nestedState: AvmPersistableStateManager,
forkedState: AvmPersistableStateManager,
nestedEnvironment: AvmExecutionEnvironment,
startGasLeft: Gas,
endGasLeft: Gas,
bytecode: Buffer,
avmCallResults: AvmContractCallResult,
) {
if (!avmCallResults.reverted) {
this.acceptNestedCallState(nestedState);
this.acceptForkedState(forkedState);
}
const functionName = await getPublicFunctionDebugName(
this.worldStateDB,
Expand All @@ -326,7 +310,7 @@ export class AvmPersistableStateManager {
this.log.verbose(`[AVM] Calling nested function ${functionName}`);

this.trace.traceNestedCall(
nestedState.trace,
forkedState.trace,
nestedEnvironment,
startGasLeft,
endGasLeft,
Expand All @@ -336,8 +320,8 @@ export class AvmPersistableStateManager {
);
}

public async processEnqueuedCall(
nestedState: AvmPersistableStateManager,
public async mergeStateForEnqueuedCall(
forkedState: AvmPersistableStateManager,
/** The call request from private that enqueued this call. */
publicCallRequest: PublicCallRequest,
/** The call's calldata */
Expand All @@ -346,7 +330,7 @@ export class AvmPersistableStateManager {
reverted: boolean,
) {
if (!reverted) {
this.acceptNestedCallState(nestedState);
this.acceptForkedState(forkedState);
}
const functionName = await getPublicFunctionDebugName(
this.worldStateDB,
Expand All @@ -357,12 +341,12 @@ export class AvmPersistableStateManager {

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

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

public processEntireAppLogicPhase(
public mergeStateForPhase(
/** The forked state manager used by app logic */
nestedState: AvmPersistableStateManager,
forkedState: AvmPersistableStateManager,
/** The call requests for each enqueued call in app logic. */
publicCallRequests: PublicCallRequest[],
/** The calldatas for each enqueued call in app logic */
Expand All @@ -371,11 +355,11 @@ export class AvmPersistableStateManager {
reverted: boolean,
) {
if (!reverted) {
this.acceptNestedCallState(nestedState);
this.acceptForkedState(forkedState);
}

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

this.trace.traceAppLogicPhase(nestedState.trace, publicCallRequests, calldatas, reverted);
this.trace.traceExecutionPhase(forkedState.trace, publicCallRequests, calldatas, reverted);
}
}
8 changes: 2 additions & 6 deletions yarn-project/simulator/src/avm/journal/nullifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ export class NullifierManager {
/**
* Create a new nullifiers manager with some preloaded pending siloed nullifiers
*/
public static newWithPendingSiloedNullifiers(
hostNullifiers: CommitmentsDB,
pendingSiloedNullifiers: Fr[],
parent?: NullifierManager,
) {
public static newWithPendingSiloedNullifiers(hostNullifiers: CommitmentsDB, pendingSiloedNullifiers: Fr[]) {
const cache = new NullifierCache(pendingSiloedNullifiers);
return new NullifierManager(hostNullifiers, cache, parent);
return new NullifierManager(hostNullifiers, cache);
}

/**
Expand Down
Loading

0 comments on commit 9bf1802

Please sign in to comment.