Skip to content

Commit

Permalink
refactor: stop calling public kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Nov 15, 2024
1 parent 9a4706b commit 0cb272b
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 255 deletions.
1 change: 0 additions & 1 deletion yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,6 @@ export class AztecNodeService implements AztecNode {
const prevHeader = (await this.blockSource.getBlock(-1))?.header;
const publicProcessorFactory = new PublicProcessorFactory(
this.contractDataSource,
new WASMSimulator(),
this.telemetry,
);

Expand Down
3 changes: 1 addition & 2 deletions yarn-project/prover-node/src/prover-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ProverNode implements ClaimsMonitorHandler, EpochMonitorHandler, Pr
private readonly contractDataSource: ContractDataSource,
private readonly worldState: WorldStateSynchronizer,
private readonly coordination: ProverCoordination & Maybe<Service>,
private readonly simulator: SimulationProvider,
private readonly _simulator: SimulationProvider,
private readonly quoteProvider: QuoteProvider,
private readonly quoteSigner: QuoteSigner,
private readonly claimsMonitor: ClaimsMonitor,
Expand Down Expand Up @@ -245,7 +245,6 @@ export class ProverNode implements ClaimsMonitorHandler, EpochMonitorHandler, Pr
// Create a processor using the forked world state
const publicProcessorFactory = new PublicProcessorFactory(
this.contractDataSource,
this.simulator,
this.telemetryClient,
);

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/sequencer-client/src/client/sequencer-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export class SequencerClient {
contractDataSource: ContractDataSource,
l2BlockSource: L2BlockSource,
l1ToL2MessageSource: L1ToL2MessageSource,
simulationProvider: SimulationProvider,
_simulationProvider: SimulationProvider,
telemetryClient: TelemetryClient,
) {
const publisher = new L1Publisher(config, telemetryClient);
const globalsBuilder = new GlobalVariableBuilder(config);

const publicProcessorFactory = new PublicProcessorFactory(contractDataSource, simulationProvider, telemetryClient);
const publicProcessorFactory = new PublicProcessorFactory(contractDataSource, telemetryClient);

const rollup = publisher.getRollupContract();
const [l1GenesisTime, slotDuration] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ 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 { assert } from 'console';

Expand Down Expand Up @@ -631,9 +630,9 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI
/** 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>,
publicSetupCallRequests: PublicCallRequest[],
/** Call requests for app logic phase. */
publicAppLogicCallRequests: Tuple<PublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_TX>,
publicAppLogicCallRequests: PublicCallRequest[],
/** Call request for teardown phase. */
publicTeardownCallRequest: PublicCallRequest,
/** End tree snapshots. */
Expand All @@ -653,8 +652,8 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI
startTreeSnapshots,
startGasUsed,
gasLimits,
publicSetupCallRequests,
publicAppLogicCallRequests,
padArrayEnd(publicSetupCallRequests, PublicCallRequest.empty(), MAX_ENQUEUED_CALLS_PER_TX),
padArrayEnd(publicAppLogicCallRequests, PublicCallRequest.empty(), MAX_ENQUEUED_CALLS_PER_TX),
publicTeardownCallRequest,
/*previousNonRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(),
/*previousRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(),
Expand Down
13 changes: 1 addition & 12 deletions yarn-project/simulator/src/public/public_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,17 @@ import { Timer } from '@aztec/foundation/timer';
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
import { Attributes, type TelemetryClient, type Tracer, trackSpan } from '@aztec/telemetry-client';

import { type SimulationProvider } from '../providers/index.js';
import { PublicExecutor } from './executor.js';
import { computeFeePayerBalanceLeafSlot, computeFeePayerBalanceStorageSlot } from './fee_payment.js';
import { WorldStateDB } from './public_db_sources.js';
import { RealPublicKernelCircuitSimulator } from './public_kernel.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { PublicProcessorMetrics } from './public_processor_metrics.js';
import { PublicTxSimulator } from './public_tx_simulator.js';

/**
* Creates new instances of PublicProcessor given the provided merkle tree db and contract data source.
*/
export class PublicProcessorFactory {
constructor(
private contractDataSource: ContractDataSource,
private simulator: SimulationProvider,
private telemetryClient: TelemetryClient,
) {}
constructor(private contractDataSource: ContractDataSource, private telemetryClient: TelemetryClient) {}

/**
* Creates a new instance of a PublicProcessor.
Expand All @@ -66,12 +59,10 @@ export class PublicProcessorFactory {

const worldStateDB = new WorldStateDB(merkleTree, this.contractDataSource);
const publicExecutor = new PublicExecutor(worldStateDB, telemetryClient);
const publicKernelSimulator = new RealPublicKernelCircuitSimulator(this.simulator);

return PublicProcessor.create(
merkleTree,
publicExecutor,
publicKernelSimulator,
globalVariables,
historicalHeader,
worldStateDB,
Expand Down Expand Up @@ -101,7 +92,6 @@ export class PublicProcessor {
static create(
db: MerkleTreeWriteOperations,
publicExecutor: PublicExecutor,
publicKernelSimulator: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
worldStateDB: WorldStateDB,
Expand All @@ -110,7 +100,6 @@ export class PublicProcessor {
const enqueuedCallsProcessor = PublicTxSimulator.create(
db,
publicExecutor,
publicKernelSimulator,
globalVariables,
historicalHeader,
worldStateDB,
Expand Down
53 changes: 27 additions & 26 deletions yarn-project/simulator/src/public/public_tx_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
Gas,
type GasSettings,
type GlobalVariables,
type PrivateToPublicAccumulatedData,
PublicAccumulatedDataArrayLengths,
type PublicCallRequest,
type PublicKernelCircuitPublicInputs,
PublicValidationRequestArrayLengths,
RevertCode,
type StateReference,
Expand All @@ -31,10 +31,10 @@ import { PublicEnqueuedCallSideEffectTrace } from './enqueued_call_side_effect_t
import { type WorldStateDB } from './public_db_sources.js';
import { PublicSideEffectTrace } from './side_effect_trace.js';
import {
convertPrivateToPublicAccumulatedData,
generateAvmCircuitPublicInputs,
getCallRequestsByPhase,
getExecutionRequestsByPhase,
getPublicKernelCircuitPublicInputs,
} from './utils.js';

export class PublicTxContext {
Expand All @@ -54,20 +54,20 @@ export class PublicTxContext {

constructor(
public readonly state: PhaseStateManager,
public readonly tx: Tx, // tmp hack
public readonly tx: Tx, // TODO(dbanks12): remove
public readonly globalVariables: GlobalVariables,
public readonly constants: CombinedConstantData, // tmp hack
public readonly constants: CombinedConstantData, // TODO(dbanks12): remove
public readonly startStateReference: StateReference,
startGasUsed: Gas,
private readonly startGasUsed: Gas,
private readonly gasSettings: GasSettings,
private readonly setupCallRequests: PublicCallRequest[],
private readonly appLogicCallRequests: PublicCallRequest[],
private readonly teardownCallRequests: PublicCallRequest[],
private readonly setupExecutionRequests: PublicExecutionRequest[],
private readonly appLogicExecutionRequests: PublicExecutionRequest[],
private readonly teardownExecutionRequests: PublicExecutionRequest[],
private firstPublicKernelOutput: PublicKernelCircuitPublicInputs,
public latestPublicKernelOutput: PublicKernelCircuitPublicInputs,
private readonly nonRevertibleAccumulatedDataFromPrivate: PrivateToPublicAccumulatedData,
private readonly revertibleAccumulatedDataFromPrivate: PrivateToPublicAccumulatedData,
public trace: PublicEnqueuedCallSideEffectTrace,
) {
this.log = createDebugLogger(`aztec:public_tx_context`);
Expand All @@ -80,29 +80,25 @@ export class PublicTxContext {
tx: Tx,
globalVariables: GlobalVariables,
) {
const privateKernelOutput = tx.data;
const firstPublicKernelOutput = getPublicKernelCircuitPublicInputs(privateKernelOutput, globalVariables);
const nonRevertibleAccumulatedDataFromPrivate = convertPrivateToPublicAccumulatedData(
tx.data.forPublic!.nonRevertibleAccumulatedData,
);
const revertibleAccumulatedDataFromPrivate = convertPrivateToPublicAccumulatedData(
tx.data.forPublic!.revertibleAccumulatedData,
);

const nonRevertibleNullifiersFromPrivate = firstPublicKernelOutput.endNonRevertibleData.nullifiers
const nonRevertibleNullifiersFromPrivate = nonRevertibleAccumulatedDataFromPrivate.nullifiers
.filter(n => !n.isEmpty())
.map(n => n.value);
const _revertibleNullifiersFromPrivate = firstPublicKernelOutput.end.nullifiers
const _revertibleNullifiersFromPrivate = revertibleAccumulatedDataFromPrivate.nullifiers
.filter(n => !n.isEmpty())
.map(n => n.value);

// During SETUP, non revertible side effects from private are our "previous data"
const prevAccumulatedData = firstPublicKernelOutput.endNonRevertibleData;
const previousValidationRequestArrayLengths = PublicValidationRequestArrayLengths.new(
firstPublicKernelOutput.validationRequests,
);

const previousAccumulatedDataArrayLengths = PublicAccumulatedDataArrayLengths.new(prevAccumulatedData);

const innerCallTrace = new PublicSideEffectTrace();
const enqueuedCallTrace = new PublicEnqueuedCallSideEffectTrace(
/*startSideEffectCounter=*/ 0,
previousValidationRequestArrayLengths,
previousAccumulatedDataArrayLengths,
PublicValidationRequestArrayLengths.empty(),
PublicAccumulatedDataArrayLengths.new(nonRevertibleAccumulatedDataFromPrivate),
);
const trace = new DualSideEffectTrace(innerCallTrace, enqueuedCallTrace);

Expand All @@ -127,8 +123,8 @@ export class PublicTxContext {
getExecutionRequestsByPhase(tx, TxExecutionPhase.SETUP),
getExecutionRequestsByPhase(tx, TxExecutionPhase.APP_LOGIC),
getExecutionRequestsByPhase(tx, TxExecutionPhase.TEARDOWN),
firstPublicKernelOutput,
firstPublicKernelOutput,
tx.data.forPublic!.nonRevertibleAccumulatedData,
tx.data.forPublic!.revertibleAccumulatedData,
enqueuedCallTrace,
);
}
Expand Down Expand Up @@ -269,15 +265,20 @@ export class PublicTxContext {
'Can only get AvmCircuitPublicInputs after teardown (tx done)',
);
return generateAvmCircuitPublicInputs(
this.tx,
this.trace,
this.globalVariables,
this.startStateReference,
this.startGasUsed,
this.gasSettings,
this.setupCallRequests,
this.appLogicCallRequests,
this.teardownCallRequests,
this.nonRevertibleAccumulatedDataFromPrivate,
this.revertibleAccumulatedDataFromPrivate,
endStateReference,
this.gasUsed,
/*endGasUsed=*/ this.gasUsed,
this.getFinalTransactionFee(),
this.revertCode,
this.firstPublicKernelOutput,
);
}

Expand Down
7 changes: 0 additions & 7 deletions yarn-project/simulator/src/public/public_tx_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ import { type MockProxy, mock } from 'jest-mock-extended';

import { type AvmPersistableStateManager } from '../avm/journal/journal.js';
import { PublicExecutionResultBuilder } from '../mocks/fixtures.js';
import { WASMSimulator } from '../providers/acvm_wasm.js';
import { type PublicExecutor } from './executor.js';
import { type WorldStateDB } from './public_db_sources.js';
import { RealPublicKernelCircuitSimulator } from './public_kernel.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { PublicTxSimulator } from './public_tx_simulator.js';

describe('public_tx_simulator', () => {
Expand All @@ -53,7 +50,6 @@ describe('public_tx_simulator', () => {

let db: MockProxy<MerkleTreeWriteOperations>;
let publicExecutor: MockProxy<PublicExecutor>;
let publicKernel: PublicKernelCircuitSimulator;
let worldStateDB: MockProxy<WorldStateDB>;

let root: Buffer;
Expand Down Expand Up @@ -163,12 +159,9 @@ describe('public_tx_simulator', () => {
db.getPreviousValueIndex.mockResolvedValue({ index: 0n, alreadyPresent: true });
db.getLeafPreimage.mockResolvedValue(new PublicDataTreeLeafPreimage(new Fr(0), new Fr(0), new Fr(0), 0n));

publicKernel = new RealPublicKernelCircuitSimulator(new WASMSimulator());

processor = PublicTxSimulator.create(
db,
publicExecutor,
publicKernel,
GlobalVariables.from({ ...GlobalVariables.empty(), gasFees }),
Header.empty(),
worldStateDB,
Expand Down
50 changes: 9 additions & 41 deletions yarn-project/simulator/src/public/public_tx_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import { Timer } from '@aztec/foundation/timer';
import { EnqueuedCallSimulator } from './enqueued_call_simulator.js';
import { type PublicExecutor } from './executor.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { PublicKernelTailSimulator } from './public_kernel_tail_simulator.js';
import { PublicTxContext } from './public_tx_context.js';
import { generateAvmCircuitPublicInputsDeprecated, runMergeKernelCircuit } from './utils.js';

export type ProcessedPhase = {
phase: TxExecutionPhase;
Expand All @@ -42,19 +39,16 @@ export class PublicTxSimulator {

constructor(
private db: MerkleTreeReadOperations,
private publicKernelSimulator: PublicKernelCircuitSimulator,
private globalVariables: GlobalVariables,
private worldStateDB: WorldStateDB,
private enqueuedCallSimulator: EnqueuedCallSimulator,
private publicKernelTailSimulator: PublicKernelTailSimulator,
) {
this.log = createDebugLogger(`aztec:sequencer`);
this.log = createDebugLogger(`aztec:public_tx_simulator`);
}

static create(
db: MerkleTreeReadOperations,
publicExecutor: PublicExecutor,
publicKernelSimulator: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
worldStateDB: WorldStateDB,
Expand All @@ -69,16 +63,7 @@ export class PublicTxSimulator {
realAvmProvingRequests,
);

const publicKernelTailSimulator = PublicKernelTailSimulator.create(db, publicKernelSimulator);

return new PublicTxSimulator(
db,
publicKernelSimulator,
globalVariables,
worldStateDB,
enqueuedCallSimulator,
publicKernelTailSimulator,
);
return new PublicTxSimulator(db, globalVariables, worldStateDB, enqueuedCallSimulator);
}

async process(tx: Tx): Promise<PublicTxResult> {
Expand All @@ -96,22 +81,9 @@ export class PublicTxSimulator {

const endStateReference = await this.db.getStateReference();

const tailKernelOutput = await this.publicKernelTailSimulator.simulate(context.latestPublicKernelOutput);

generateAvmCircuitPublicInputsDeprecated(
tx,
tailKernelOutput,
context.getGasUsedForFee(),
context.getFinalTransactionFee(),
);

const gasUsed = {
totalGas: context.getActualGasUsed(),
teardownGas: context.teardownGasUsed,
};
return {
avmProvingRequest: context.generateProvingRequest(endStateReference),
gasUsed,
gasUsed: { totalGas: context.getActualGasUsed(), teardownGas: context.teardownGasUsed },
revertCode: context.revertCode,
revertReason: context.revertReason,
processedPhases: processedPhases,
Expand Down Expand Up @@ -214,14 +186,16 @@ export class PublicTxSimulator {
/*transactionFee=*/ context.getTransactionFee(),
enqueuedCallStateManager,
);
if (context.avmProvingRequest === undefined) {
// Propagate the very first avmProvingRequest of the tx for now.
// Eventually this will be the proof for the entire public portion of the transaction.
context.avmProvingRequest = enqueuedCallResult.avmProvingRequest;
}

txStateManager.traceEnqueuedCall(callRequest, executionRequest.args, enqueuedCallResult.reverted!);

context.consumeGas(enqueuedCallResult.gasUsed);
returnValues.push(enqueuedCallResult.returnValues);
// Propagate only one avmProvingRequest of a function call for now, so that we know it's still provable.
// Eventually this will be the proof for the entire public portion of the transaction.
context.avmProvingRequest = enqueuedCallResult.avmProvingRequest;
if (enqueuedCallResult.reverted) {
reverted = true;
const culprit = `${executionRequest.callContext.contractAddress}:${executionRequest.callContext.functionSelector}`;
Expand All @@ -234,7 +208,7 @@ export class PublicTxSimulator {
// FIXME: we shouldn't need to directly modify worldStateDb here!
await this.worldStateDB.removeNewContracts(tx);
// FIXME: we shouldn't be modifying the transaction here!
tx.filterRevertedLogs(context.latestPublicKernelOutput);
//tx.filterRevertedLogs(context.latestPublicKernelOutput);
// Enqueeud call reverted. Discard state updates and accumulated side effects, but keep hints traced for the circuit.
txStateManager.rejectForkedState(enqueuedCallStateManager);
} else {
Expand All @@ -243,12 +217,6 @@ export class PublicTxSimulator {
// Enqueued call succeeded! Merge in any state updates made in the forked state manager.
txStateManager.mergeForkedState(enqueuedCallStateManager);
}

context.latestPublicKernelOutput = await runMergeKernelCircuit(
context.latestPublicKernelOutput,
enqueuedCallResult.kernelOutput,
this.publicKernelSimulator,
);
}

return {
Expand Down
Loading

0 comments on commit 0cb272b

Please sign in to comment.