diff --git a/yarn-project/simulator/src/public/abstract_phase_manager.ts b/yarn-project/simulator/src/public/abstract_phase_manager.ts index 054218ae5a2..5abd9635446 100644 --- a/yarn-project/simulator/src/public/abstract_phase_manager.ts +++ b/yarn-project/simulator/src/public/abstract_phase_manager.ts @@ -64,6 +64,7 @@ import { import { type PublicExecutionResult, type PublicExecutor, + type WorldStateDB, accumulateReturnValues, isPublicExecutionResult, } from '@aztec/simulator'; @@ -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}`); } /** diff --git a/yarn-project/simulator/src/public/app_logic_phase_manager.ts b/yarn-project/simulator/src/public/app_logic_phase_manager.ts index 5d78b366766..e6c0b1e92a4 100644 --- a/yarn-project/simulator/src/public/app_logic_phase_manager.ts +++ b/yarn-project/simulator/src/public/app_logic_phase_manager.ts @@ -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( diff --git a/yarn-project/simulator/src/public/phase_manager_factory.ts b/yarn-project/simulator/src/public/phase_manager_factory.ts index 7c42be49b9b..a5dbe868a53 100644 --- a/yarn-project/simulator/src/public/phase_manager_factory.ts +++ b/yarn-project/simulator/src/public/phase_manager_factory.ts @@ -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'; @@ -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; } @@ -62,12 +36,7 @@ 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(); @@ -75,28 +44,14 @@ export class PhaseManagerFactory { 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; } diff --git a/yarn-project/simulator/src/public/public_processor.ts b/yarn-project/simulator/src/public/public_processor.ts index 0e36df968c1..67faee6463d 100644 --- a/yarn-project/simulator/src/public/public_processor.ts +++ b/yarn-project/simulator/src/public/public_processor.ts @@ -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'; @@ -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(); @@ -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) { diff --git a/yarn-project/simulator/src/public/setup_phase_manager.test.ts b/yarn-project/simulator/src/public/setup_phase_manager.test.ts index 58333097a03..d05ce394ba2 100644 --- a/yarn-project/simulator/src/public/setup_phase_manager.test.ts +++ b/yarn-project/simulator/src/public/setup_phase_manager.test.ts @@ -1,4 +1,4 @@ -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'; @@ -6,6 +6,7 @@ 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'; @@ -34,14 +35,16 @@ describe('setup_phase_manager', () => { root = Buffer.alloc(32, 5); db.getTreeInfo.mockResolvedValue({ root } as TreeInfo); publicKernel = mock(); - 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 () { diff --git a/yarn-project/simulator/src/public/setup_phase_manager.ts b/yarn-project/simulator/src/public/setup_phase_manager.ts index 4fd1d02986b..6018938467e 100644 --- a/yarn-project/simulator/src/public/setup_phase_manager.ts +++ b/yarn-project/simulator/src/public/setup_phase_manager.ts @@ -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( diff --git a/yarn-project/simulator/src/public/tail_phase_manager.ts b/yarn-project/simulator/src/public/tail_phase_manager.ts index 19de912dd19..8adf8cd5b43 100644 --- a/yarn-project/simulator/src/public/tail_phase_manager.ts +++ b/yarn-project/simulator/src/public/tail_phase_manager.ts @@ -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, @@ -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( diff --git a/yarn-project/simulator/src/public/teardown_phase_manager.ts b/yarn-project/simulator/src/public/teardown_phase_manager.ts index 85ba32ebd8d..96bb188295b 100644 --- a/yarn-project/simulator/src/public/teardown_phase_manager.ts +++ b/yarn-project/simulator/src/public/teardown_phase_manager.ts @@ -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(