Skip to content

Commit

Permalink
refactor: use config object to make phase manager less noisy (#8586)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Sep 20, 2024
1 parent bc6d7ee commit 5a5f2b2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 161 deletions.
41 changes: 31 additions & 10 deletions yarn-project/simulator/src/public/abstract_phase_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
import {
type PublicExecutionResult,
type PublicExecutor,
type WorldStateDB,
accumulateReturnValues,
isPublicExecutionResult,
} from '@aztec/simulator';
Expand Down Expand Up @@ -139,19 +140,39 @@ export type PhaseResult = {
gasUsed?: Gas;
};

export interface PhaseConfig {
db: MerkleTreeOperations;
publicExecutor: PublicExecutor;
publicKernel: PublicKernelCircuitSimulator;
globalVariables: GlobalVariables;
historicalHeader: Header;
worldStateDB: WorldStateDB;
phase?: PublicKernelType;
}

export abstract class AbstractPhaseManager {
protected hintsBuilder: HintsBuilder;
protected log: DebugLogger;
constructor(
protected db: MerkleTreeOperations,
protected publicExecutor: PublicExecutor,
protected publicKernel: PublicKernelCircuitSimulator,
protected globalVariables: GlobalVariables,
protected historicalHeader: Header,
public phase: PublicKernelType,
) {
this.hintsBuilder = new HintsBuilder(db);
this.log = createDebugLogger(`aztec:sequencer:${phase}`);

protected db: MerkleTreeOperations;
protected publicExecutor: PublicExecutor;
protected publicKernel: PublicKernelCircuitSimulator;
protected globalVariables: GlobalVariables;
protected historicalHeader: Header;
protected worldStateDB: WorldStateDB;
public phase: PublicKernelType;

constructor(config: PhaseConfig) {
this.db = config.db;
this.publicExecutor = config.publicExecutor;
this.publicKernel = config.publicKernel;
this.globalVariables = config.globalVariables;
this.historicalHeader = config.historicalHeader;
this.worldStateDB = config.worldStateDB;
this.phase = config.phase ?? PublicKernelType.SETUP;

this.hintsBuilder = new HintsBuilder(this.db);
this.log = createDebugLogger(`aztec:sequencer:${this.phase}`);
}

/**
Expand Down
20 changes: 4 additions & 16 deletions yarn-project/simulator/src/public/app_logic_phase_manager.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { PublicKernelType, type PublicProvingRequest, type Tx } from '@aztec/circuit-types';
import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
import { type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
import { type PublicExecutor } from '@aztec/simulator';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { AbstractPhaseManager, makeAvmProvingRequest } from './abstract_phase_manager.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { AbstractPhaseManager, type PhaseConfig, makeAvmProvingRequest } from './abstract_phase_manager.js';

/**
* The phase manager responsible for performing the fee preparation phase.
*/
export class AppLogicPhaseManager extends AbstractPhaseManager {
constructor(
db: MerkleTreeOperations,
publicExecutor: PublicExecutor,
publicKernel: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
protected worldStateDB: WorldStateDB,
phase: PublicKernelType = PublicKernelType.APP_LOGIC,
) {
super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
constructor(config: PhaseConfig, public override phase: PublicKernelType = PublicKernelType.APP_LOGIC) {
super(config);
}

override async handle(
Expand Down
65 changes: 10 additions & 55 deletions yarn-project/simulator/src/public/phase_manager_factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { PublicKernelType, type Tx } from '@aztec/circuit-types';
import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
import { type PublicExecutor } from '@aztec/simulator';
import { type MerkleTreeOperations } from '@aztec/world-state';
import { type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';

import { type AbstractPhaseManager } from './abstract_phase_manager.js';
import { type AbstractPhaseManager, type PhaseConfig } from './abstract_phase_manager.js';
import { AppLogicPhaseManager } from './app_logic_phase_manager.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { SetupPhaseManager } from './setup_phase_manager.js';
import { TailPhaseManager } from './tail_phase_manager.js';
import { TeardownPhaseManager } from './teardown_phase_manager.js';
Expand All @@ -24,36 +20,14 @@ export class CannotTransitionToSetupError extends Error {
}

export class PhaseManagerFactory {
public static phaseFromTx(
tx: Tx,
db: MerkleTreeOperations,
publicExecutor: PublicExecutor,
publicKernel: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
worldStateDB: WorldStateDB,
): AbstractPhaseManager | undefined {
public static phaseFromTx(tx: Tx, config: PhaseConfig): AbstractPhaseManager | undefined {
const data = tx.data.forPublic!;
if (data.needsSetup) {
return new SetupPhaseManager(db, publicExecutor, publicKernel, globalVariables, historicalHeader, worldStateDB);
return new SetupPhaseManager(config);
} else if (data.needsAppLogic) {
return new AppLogicPhaseManager(
db,
publicExecutor,
publicKernel,
globalVariables,
historicalHeader,
worldStateDB,
);
return new AppLogicPhaseManager(config);
} else if (data.needsTeardown) {
return new TeardownPhaseManager(
db,
publicExecutor,
publicKernel,
globalVariables,
historicalHeader,
worldStateDB,
);
return new TeardownPhaseManager(config);
} else {
return undefined;
}
Expand All @@ -62,41 +36,22 @@ export class PhaseManagerFactory {
public static phaseFromOutput(
output: PublicKernelCircuitPublicInputs,
currentPhaseManager: AbstractPhaseManager,
db: MerkleTreeOperations,
publicExecutor: PublicExecutor,
publicKernel: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
worldStateDB: WorldStateDB,
config: PhaseConfig,
): AbstractPhaseManager | undefined {
if (output.needsSetup) {
throw new CannotTransitionToSetupError();
} else if (output.needsAppLogic) {
if (currentPhaseManager.phase === PublicKernelType.APP_LOGIC) {
throw new PhaseDidNotChangeError(currentPhaseManager.phase);
}
return new AppLogicPhaseManager(
db,
publicExecutor,
publicKernel,
globalVariables,
historicalHeader,
worldStateDB,
);
return new AppLogicPhaseManager(config);
} else if (output.needsTeardown) {
if (currentPhaseManager.phase === PublicKernelType.TEARDOWN) {
throw new PhaseDidNotChangeError(currentPhaseManager.phase);
}
return new TeardownPhaseManager(
db,
publicExecutor,
publicKernel,
globalVariables,
historicalHeader,
worldStateDB,
);
return new TeardownPhaseManager(config);
} else if (currentPhaseManager.phase !== PublicKernelType.TAIL) {
return new TailPhaseManager(db, publicExecutor, publicKernel, globalVariables, historicalHeader, worldStateDB);
return new TailPhaseManager(config);
} else {
return undefined;
}
Expand Down
38 changes: 18 additions & 20 deletions yarn-project/simulator/src/public/public_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { Attributes, type TelemetryClient, type Tracer, trackSpan } from '@aztec
import { type ContractDataSource } from '@aztec/types/contracts';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { type AbstractPhaseManager } from './abstract_phase_manager.js';
import { type AbstractPhaseManager, type PhaseConfig } from './abstract_phase_manager.js';
import { PhaseManagerFactory } from './phase_manager_factory.js';
import { WorldStateDB } from './public_db_sources.js';
import { RealPublicKernelCircuitSimulator } from './public_kernel.js';
Expand Down Expand Up @@ -229,15 +229,16 @@ export class PublicProcessor {
const timer = new Timer();
let returnValues: NestedProcessReturnValues[] = [];
const publicProvingRequests: PublicProvingRequest[] = [];
let phase: AbstractPhaseManager | undefined = PhaseManagerFactory.phaseFromTx(
tx,
this.db,
this.publicExecutor,
this.publicKernel,
this.globalVariables,
this.historicalHeader,
this.worldStateDB,
);

const phaseManagerConfig: PhaseConfig = {
db: this.db,
publicExecutor: this.publicExecutor,
publicKernel: this.publicKernel,
globalVariables: this.globalVariables,
historicalHeader: this.historicalHeader,
worldStateDB: this.worldStateDB,
};
let phase: AbstractPhaseManager | undefined = PhaseManagerFactory.phaseFromTx(tx, phaseManagerConfig);
this.log.debug(`Beginning processing in phase ${phase?.phase} for tx ${tx.getTxHash()}`);

let publicKernelPublicInput = tx.data.toPublicKernelCircuitPublicInputs();
Expand Down Expand Up @@ -266,16 +267,13 @@ export class PublicProcessor {
lastKernelArtifact = output.lastKernelArtifact;
finalKernelOutput = output.finalKernelOutput;
revertReason ??= output.revertReason;
phase = PhaseManagerFactory.phaseFromOutput(
publicKernelPublicInput,
phase,
this.db,
this.publicExecutor,
this.publicKernel,
this.globalVariables,
this.historicalHeader,
this.worldStateDB,
);

// Update the phase manager config with the current phase
const phaseConfig = {
...phaseManagerConfig,
phase: phase.phase,
};
phase = PhaseManagerFactory.phaseFromOutput(publicKernelPublicInput, phase, phaseConfig);
}

if (!finalKernelOutput) {
Expand Down
13 changes: 8 additions & 5 deletions yarn-project/simulator/src/public/setup_phase_manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type TreeInfo, mockTx } from '@aztec/circuit-types';
import { PublicKernelType, type TreeInfo, mockTx } from '@aztec/circuit-types';
import { GlobalVariables, Header } from '@aztec/circuits.js';
import { type PublicExecutor } from '@aztec/simulator';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { it } from '@jest/globals';
import { type MockProxy, mock } from 'jest-mock-extended';

import { type PhaseConfig } from './abstract_phase_manager.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { SetupPhaseManager } from './setup_phase_manager.js';
Expand Down Expand Up @@ -34,14 +35,16 @@ describe('setup_phase_manager', () => {
root = Buffer.alloc(32, 5);
db.getTreeInfo.mockResolvedValue({ root } as TreeInfo);
publicKernel = mock<PublicKernelCircuitSimulator>();
phaseManager = new TestSetupPhaseManager(
const config: PhaseConfig = {
db,
publicExecutor,
publicKernel,
GlobalVariables.empty(),
Header.empty(),
globalVariables: GlobalVariables.empty(),
historicalHeader: Header.empty(),
phase: PublicKernelType.SETUP,
worldStateDB,
);
};
phaseManager = new TestSetupPhaseManager(config);
});

it('does not extract non-revertible calls when none exist', function () {
Expand Down
20 changes: 4 additions & 16 deletions yarn-project/simulator/src/public/setup_phase_manager.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { PublicKernelType, type PublicProvingRequest, type Tx } from '@aztec/circuit-types';
import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
import { type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
import { type PublicExecutor } from '@aztec/simulator';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { AbstractPhaseManager, makeAvmProvingRequest } from './abstract_phase_manager.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { AbstractPhaseManager, type PhaseConfig, makeAvmProvingRequest } from './abstract_phase_manager.js';

/**
* The phase manager responsible for performing the fee preparation phase.
*/
export class SetupPhaseManager extends AbstractPhaseManager {
constructor(
db: MerkleTreeOperations,
publicExecutor: PublicExecutor,
publicKernel: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
protected worldStateDB: WorldStateDB,
phase: PublicKernelType = PublicKernelType.SETUP,
) {
super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
constructor(config: PhaseConfig, public override phase: PublicKernelType = PublicKernelType.SETUP) {
super(config);
}

override async handle(
Expand Down
20 changes: 3 additions & 17 deletions yarn-project/simulator/src/public/tail_phase_manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { type PublicKernelRequest, PublicKernelType, type Tx } from '@aztec/circuit-types';
import {
type GlobalVariables,
type Header,
type KernelCircuitPublicInputs,
MAX_NULLIFIERS_PER_TX,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
Expand All @@ -10,24 +8,12 @@ import {
mergeAccumulatedData,
} from '@aztec/circuits.js';
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
import { type PublicExecutor } from '@aztec/simulator';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { AbstractPhaseManager } from './abstract_phase_manager.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { AbstractPhaseManager, type PhaseConfig } from './abstract_phase_manager.js';

export class TailPhaseManager extends AbstractPhaseManager {
constructor(
db: MerkleTreeOperations,
publicExecutor: PublicExecutor,
publicKernel: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
protected worldStateDB: WorldStateDB,
phase: PublicKernelType = PublicKernelType.TAIL,
) {
super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
constructor(config: PhaseConfig, public override phase: PublicKernelType = PublicKernelType.TAIL) {
super(config);
}

override async handle(
Expand Down
26 changes: 4 additions & 22 deletions yarn-project/simulator/src/public/teardown_phase_manager.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
import { PublicKernelType, type PublicProvingRequest, type Tx } from '@aztec/circuit-types';
import {
type Fr,
type Gas,
type GlobalVariables,
type Header,
type PublicKernelCircuitPublicInputs,
} from '@aztec/circuits.js';
import { type Fr, type Gas, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
import { type ProtocolArtifact } from '@aztec/noir-protocol-circuits-types';
import { type PublicExecutor } from '@aztec/simulator';
import { type MerkleTreeOperations } from '@aztec/world-state';

import { inspect } from 'util';

import { AbstractPhaseManager, makeAvmProvingRequest } from './abstract_phase_manager.js';
import { type WorldStateDB } from './public_db_sources.js';
import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
import { AbstractPhaseManager, type PhaseConfig, makeAvmProvingRequest } from './abstract_phase_manager.js';

/**
* The phase manager responsible for performing the fee preparation phase.
*/
export class TeardownPhaseManager extends AbstractPhaseManager {
constructor(
db: MerkleTreeOperations,
publicExecutor: PublicExecutor,
publicKernel: PublicKernelCircuitSimulator,
globalVariables: GlobalVariables,
historicalHeader: Header,
protected worldStateDB: WorldStateDB,
phase: PublicKernelType = PublicKernelType.TEARDOWN,
) {
super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
constructor(config: PhaseConfig, public override phase: PublicKernelType = PublicKernelType.TEARDOWN) {
super(config);
}

override async handle(
Expand Down

0 comments on commit 5a5f2b2

Please sign in to comment.