diff --git a/yarn-project/.earthlyignore b/yarn-project/.earthlyignore index bd9d331e9a6..0fc5487b6b8 100644 --- a/yarn-project/.earthlyignore +++ b/yarn-project/.earthlyignore @@ -44,6 +44,7 @@ aztec-faucet/data* aztec-node/data* aztec/log circuits.js/fixtures/*.json +circuit-types/src/test/artifacts docs/dist end-to-end/addresses.json end-to-end/log diff --git a/yarn-project/.gitignore b/yarn-project/.gitignore index a6182305e1e..5969baa4dc3 100644 --- a/yarn-project/.gitignore +++ b/yarn-project/.gitignore @@ -17,6 +17,7 @@ aztec-faucet/data* aztec-node/data* aztec/log circuits.js/fixtures/*.json +circuit-types/src/test/artifacts docs/dist end-to-end/addresses.json end-to-end/flame_graph diff --git a/yarn-project/archiver/src/factory.ts b/yarn-project/archiver/src/factory.ts index b7f5d8d8ab8..ac5b2786757 100644 --- a/yarn-project/archiver/src/factory.ts +++ b/yarn-project/archiver/src/factory.ts @@ -1,5 +1,7 @@ +import { type ArchiverApi, type Service } from '@aztec/circuit-types'; import { type ContractClassPublic } from '@aztec/circuits.js'; import { createDebugLogger } from '@aztec/foundation/log'; +import { type Maybe } from '@aztec/foundation/types'; import { createStore } from '@aztec/kv-store/utils'; import { getCanonicalProtocolContract, protocolContractNames } from '@aztec/protocol-contracts'; import { type TelemetryClient } from '@aztec/telemetry-client'; @@ -8,13 +10,13 @@ import { NoopTelemetryClient } from '@aztec/telemetry-client/noop'; import { Archiver } from './archiver/archiver.js'; import { type ArchiverConfig } from './archiver/config.js'; import { KVArchiverDataStore } from './archiver/index.js'; -import { createArchiverClient } from './rpc/archiver_client.js'; +import { createArchiverClient } from './rpc/index.js'; export async function createArchiver( config: ArchiverConfig, telemetry: TelemetryClient = new NoopTelemetryClient(), opts: { blockUntilSync: boolean } = { blockUntilSync: true }, -) { +): Promise> { if (!config.archiverUrl) { const store = await createStore('archiver', config, createDebugLogger('aztec:archiver:lmdb')); const archiverStore = new KVArchiverDataStore(store, config.maxLogs); diff --git a/yarn-project/archiver/src/rpc/archiver_client.ts b/yarn-project/archiver/src/rpc/archiver_client.ts deleted file mode 100644 index 562b89148c2..00000000000 --- a/yarn-project/archiver/src/rpc/archiver_client.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { - EncryptedNoteL2BlockL2Logs, - ExtendedUnencryptedL2Log, - L2Block, - NullifierMembershipWitness, - TxReceipt, - UnencryptedL2BlockL2Logs, -} from '@aztec/circuit-types'; -import { EthAddress, Fr } from '@aztec/circuits.js'; -import { createJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/client'; - -import { type ArchiveSource } from '../archiver/archiver.js'; - -export const createArchiverClient = (url: string, fetch = makeFetch([1, 2, 3], true)): ArchiveSource => - createJsonRpcClient( - url, - { - EthAddress, - ExtendedUnencryptedL2Log, - Fr, - L2Block, - EncryptedNoteL2BlockL2Logs, - UnencryptedL2BlockL2Logs, - }, - { TxReceipt, NullifierMembershipWitness }, - false, - 'archiver', - fetch, - ) as ArchiveSource; diff --git a/yarn-project/archiver/src/rpc/archiver_server.ts b/yarn-project/archiver/src/rpc/archiver_server.ts deleted file mode 100644 index be18f647e79..00000000000 --- a/yarn-project/archiver/src/rpc/archiver_server.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { - EncryptedNoteL2BlockL2Logs, - ExtendedUnencryptedL2Log, - L2Block, - NullifierMembershipWitness, - TxEffect, - TxReceipt, - UnencryptedL2BlockL2Logs, -} from '@aztec/circuit-types'; -import { EthAddress, Fr } from '@aztec/circuits.js'; -import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; - -import { type Archiver } from '../archiver/archiver.js'; - -/** - * Wrap an Archiver instance with a JSON RPC HTTP server. - * @param archiverService - The Archiver instance - * @returns An JSON-RPC HTTP server - */ -export function createArchiverRpcServer(archiverService: Archiver): JsonRpcServer { - return new JsonRpcServer( - archiverService, - { - EthAddress, - ExtendedUnencryptedL2Log, - Fr, - L2Block, - EncryptedNoteL2BlockL2Logs, - UnencryptedL2BlockL2Logs, - TxEffect, - }, - { TxReceipt, NullifierMembershipWitness }, - ['start', 'stop'], - ); -} diff --git a/yarn-project/archiver/src/rpc/index.ts b/yarn-project/archiver/src/rpc/index.ts index 726d9120af8..5845eacb4b8 100644 --- a/yarn-project/archiver/src/rpc/index.ts +++ b/yarn-project/archiver/src/rpc/index.ts @@ -1,2 +1,11 @@ -export * from './archiver_client.js'; -export * from './archiver_server.js'; +import { type ArchiverApi, ArchiverApiSchema } from '@aztec/circuit-types'; +import { createSafeJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/client'; +import { createSafeJsonRpcServer } from '@aztec/foundation/json-rpc/server'; + +export function createArchiverClient(url: string, fetch = makeFetch([1, 2, 3], true)): ArchiverApi { + return createSafeJsonRpcClient(url, ArchiverApiSchema, false, 'archiver', fetch); +} + +export function createArchiverRpcServer(handler: ArchiverApi) { + return createSafeJsonRpcServer(handler, ArchiverApiSchema); +} diff --git a/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts b/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts index 00553b4d004..3ceaf4c6c69 100644 --- a/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts +++ b/yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts @@ -1,28 +1,5 @@ -import { - type AztecNode, - EncryptedL2NoteLog, - EncryptedNoteL2BlockL2Logs, - EpochProofQuote, - ExtendedUnencryptedL2Log, - L2Block, - LogId, - NullifierMembershipWitness, - PublicDataWitness, - PublicSimulationOutput, - SiblingPath, - Tx, - TxEffect, - TxHash, - TxReceipt, - UnencryptedL2BlockL2Logs, -} from '@aztec/circuit-types'; -import { FunctionSelector, Header, PublicKeys } from '@aztec/circuits.js'; -import { NoteSelector } from '@aztec/foundation/abi'; -import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Buffer32 } from '@aztec/foundation/buffer'; -import { EthAddress } from '@aztec/foundation/eth-address'; -import { Fr } from '@aztec/foundation/fields'; -import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; +import { type AztecNode, AztecNodeApiSchema } from '@aztec/circuit-types'; +import { createSafeJsonRpcServer } from '@aztec/foundation/json-rpc/server'; /** * Wrap an AztecNode instance with a JSON RPC HTTP server. @@ -30,37 +7,5 @@ import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; * @returns An JSON-RPC HTTP server */ export function createAztecNodeRpcServer(node: AztecNode) { - const rpc = new JsonRpcServer( - node, - { - AztecAddress, - EthAddress, - ExtendedUnencryptedL2Log, - Fr, - FunctionSelector, - Header, - L2Block, - TxEffect, - LogId, - TxHash, - Buffer32, - PublicDataWitness, - PublicKeys, - SiblingPath, - }, - { - EncryptedNoteL2BlockL2Logs, - EncryptedL2NoteLog, - NoteSelector, - NullifierMembershipWitness, - PublicSimulationOutput, - Tx, - TxReceipt, - UnencryptedL2BlockL2Logs, - EpochProofQuote, - }, - // disable methods not part of the AztecNode interface - ['start', 'stop'], - ); - return rpc; + return createSafeJsonRpcServer(node, AztecNodeApiSchema); } diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index 2340fd6f2d4..0f8e30f584d 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -21,6 +21,7 @@ import { PublicDataWitness, PublicSimulationOutput, type SequencerConfig, + type Service, SiblingPath, type Tx, type TxEffect, @@ -30,6 +31,7 @@ import { TxStatus, type TxValidator, type WorldStateSynchronizer, + tryStop, } from '@aztec/circuit-types'; import { type ARCHIVE_HEIGHT, @@ -89,7 +91,7 @@ export class AztecNodeService implements AztecNode { constructor( protected config: AztecNodeConfig, protected readonly p2pClient: P2P, - protected readonly blockSource: L2BlockSource, + protected readonly blockSource: L2BlockSource & Partial, protected readonly encryptedLogsSource: L2LogsSource, protected readonly unencryptedLogsSource: L2LogsSource, protected readonly contractDataSource: ContractDataSource, @@ -373,7 +375,7 @@ export class AztecNodeService implements AztecNode { await this.sequencer?.stop(); await this.p2pClient.stop(); await this.worldStateSynchronizer.stop(); - await this.blockSource.stop(); + await tryStop(this.blockSource); await this.telemetry.stop(); this.log.info(`Stopped`); } diff --git a/yarn-project/aztec.js/src/contract/batch_call.ts b/yarn-project/aztec.js/src/contract/batch_call.ts index 89cc0beb8ae..31f6ce37df1 100644 --- a/yarn-project/aztec.js/src/contract/batch_call.ts +++ b/yarn-project/aztec.js/src/contract/batch_call.ts @@ -70,7 +70,10 @@ export class BatchCall extends BaseContractInteraction { const unconstrainedCalls = unconstrained.map(async indexedCall => { const call = indexedCall[0]; - return [await this.wallet.simulateUnconstrained(call.name, call.args, call.to, options?.from), indexedCall[1]]; + return [ + await this.wallet.simulateUnconstrained(call.name, call.args, call.to, options?.from), + indexedCall[1], + ] as const; }); const [unconstrainedResults, simulatedTx] = await Promise.all([ diff --git a/yarn-project/aztec.js/src/fee/fee_juice_payment_method_with_claim.ts b/yarn-project/aztec.js/src/fee/fee_juice_payment_method_with_claim.ts index 2c3fa87bb59..f69f515388e 100644 --- a/yarn-project/aztec.js/src/fee/fee_juice_payment_method_with_claim.ts +++ b/yarn-project/aztec.js/src/fee/fee_juice_payment_method_with_claim.ts @@ -32,7 +32,12 @@ export class FeeJuicePaymentMethodWithClaim extends FeeJuicePaymentMethod { name: 'claim', selector, isStatic: false, - args: [this.sender, this.claim.claimAmount, this.claim.claimSecret, new Fr(this.claim.messageLeafIndex)], + args: [ + this.sender.toField(), + this.claim.claimAmount, + this.claim.claimSecret, + new Fr(this.claim.messageLeafIndex), + ], returnTypes: [], type: FunctionType.PRIVATE, }, diff --git a/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts b/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts index 0578179ef1c..e505901da36 100644 --- a/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts @@ -65,7 +65,7 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod { caller: this.paymentContract, action: { name: 'setup_refund', - args: [this.feeRecipient, this.wallet.getAddress(), maxFee, nonce], + args: [this.feeRecipient.toField(), this.wallet.getAddress().toField(), maxFee, nonce], selector: FunctionSelector.fromSignature('setup_refund((Field),(Field),Field,Field)'), type: FunctionType.PRIVATE, isStatic: false, @@ -81,7 +81,7 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod { selector: FunctionSelector.fromSignature('fee_entrypoint_private(Field,(Field),Field)'), type: FunctionType.PRIVATE, isStatic: false, - args: [maxFee, this.asset, nonce], + args: [maxFee, this.asset.toField(), nonce], returnTypes: [], }, ]; diff --git a/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts b/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts index dae20d5fe8a..13842e36131 100644 --- a/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts @@ -54,7 +54,7 @@ export class PublicFeePaymentMethod implements FeePaymentMethod { caller: this.paymentContract, action: { name: 'transfer_public', - args: [this.wallet.getAddress(), this.paymentContract, maxFee, nonce], + args: [this.wallet.getAddress().toField(), this.paymentContract.toField(), maxFee, nonce], selector: FunctionSelector.fromSignature('transfer_public((Field),(Field),Field,Field)'), type: FunctionType.PUBLIC, isStatic: false, @@ -71,7 +71,7 @@ export class PublicFeePaymentMethod implements FeePaymentMethod { selector: FunctionSelector.fromSignature('fee_entrypoint_public(Field,(Field),Field)'), type: FunctionType.PRIVATE, isStatic: false, - args: [maxFee, this.asset, nonce], + args: [maxFee, this.asset.toField(), nonce], returnTypes: [], }, ]); diff --git a/yarn-project/aztec.js/src/rpc_clients/pxe_client.ts b/yarn-project/aztec.js/src/rpc_clients/pxe_client.ts index 0e7a81a0714..b95c3802fff 100644 --- a/yarn-project/aztec.js/src/rpc_clients/pxe_client.ts +++ b/yarn-project/aztec.js/src/rpc_clients/pxe_client.ts @@ -1,45 +1,5 @@ -import { - AuthWitness, - CountedNoteLog, - CountedPublicExecutionRequest, - EncryptedL2Log, - EncryptedL2NoteLog, - EncryptedNoteL2BlockL2Logs, - EventMetadata, - ExtendedNote, - ExtendedUnencryptedL2Log, - L2Block, - LogId, - Note, - NullifierMembershipWitness, - type PXE, - PrivateExecutionResult, - SiblingPath, - Tx, - TxEffect, - TxExecutionRequest, - TxHash, - TxProvingResult, - TxReceipt, - TxSimulationResult, - UnencryptedL2BlockL2Logs, - UnencryptedL2Log, - UniqueNote, -} from '@aztec/circuit-types'; -import { - AztecAddress, - CompleteAddress, - EthAddress, - Fr, - FunctionSelector, - GrumpkinScalar, - Point, - PrivateCircuitPublicInputs, - PublicKeys, -} from '@aztec/circuits.js'; -import { EventSelector, NoteSelector } from '@aztec/foundation/abi'; -import { Buffer32 } from '@aztec/foundation/buffer'; -import { createJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/client'; +import { type PXE, PXESchema } from '@aztec/circuit-types'; +import { createSafeJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/client'; /** * Creates a JSON-RPC client to remotely talk to PXE. @@ -47,51 +7,6 @@ import { createJsonRpcClient, makeFetch } from '@aztec/foundation/json-rpc/clien * @param fetch - The fetch implementation to use. * @returns A JSON-RPC client of PXE. */ -export const createPXEClient = (url: string, fetch = makeFetch([1, 2, 3], false)): PXE => - createJsonRpcClient( - url, - { - AuthWitness, - AztecAddress, - CompleteAddress, - FunctionSelector, - EthAddress, - EventSelector, - ExtendedNote, - UniqueNote, - ExtendedUnencryptedL2Log, - Fr, - GrumpkinScalar, - L2Block, - TxEffect, - LogId, - Note, - Point, - PublicKeys, - TxExecutionRequest, - TxHash, - Buffer32, - SiblingPath, - }, - { - EncryptedNoteL2BlockL2Logs, - EncryptedL2NoteLog, - EncryptedL2Log, - EventMetadata, - UnencryptedL2Log, - NoteSelector, - NullifierMembershipWitness, - TxSimulationResult, - TxProvingResult, - PrivateCircuitPublicInputs, - PrivateExecutionResult, - CountedPublicExecutionRequest, - CountedNoteLog, - Tx, - TxReceipt, - UnencryptedL2BlockL2Logs, - }, - false, - 'pxe', - fetch, - ) as PXE; +export function createPXEClient(url: string, fetch = makeFetch([1, 2, 3], false)): PXE { + return createSafeJsonRpcClient(url, PXESchema, false, 'pxe', fetch); +} diff --git a/yarn-project/aztec.js/src/utils/abi_types.ts b/yarn-project/aztec.js/src/utils/abi_types.ts index 304c109899b..96f43ac5de3 100644 --- a/yarn-project/aztec.js/src/utils/abi_types.ts +++ b/yarn-project/aztec.js/src/utils/abi_types.ts @@ -13,7 +13,7 @@ export type FieldLike = Fr | Buffer | bigint | number | { /** Converts to field export type EthAddressLike = { /** Wrapped address */ address: FieldLike } | EthAddress; /** Any type that can be converted into an AztecAddress Aztec.nr struct. */ -export type AztecAddressLike = { /** Wrapped address */ address: FieldLike } | AztecAddress; +export type AztecAddressLike = { /** Wrapped address */ address: FieldLike } | AztecAddress | Fr; /** Any type that can be converted into a FunctionSelector Aztec.nr struct. */ export type FunctionSelectorLike = FieldLike | FunctionSelector; diff --git a/yarn-project/aztec.js/src/utils/authwit.ts b/yarn-project/aztec.js/src/utils/authwit.ts index d59ef37e3d0..51c62370891 100644 --- a/yarn-project/aztec.js/src/utils/authwit.ts +++ b/yarn-project/aztec.js/src/utils/authwit.ts @@ -57,7 +57,7 @@ export const computeAuthWitMessageHash = (intent: IntentInnerHash | IntentAction if ('caller' in intent) { const action = intent.action instanceof ContractFunctionInteraction ? intent.action.request() : intent.action; return computeOuterAuthWitHash( - action.to.toField(), + action.to, chainId, version, computeInnerAuthWitHashFromAction(intent.caller, action), diff --git a/yarn-project/aztec.js/src/utils/cheat_codes.ts b/yarn-project/aztec.js/src/utils/cheat_codes.ts index 7bbc95044cc..f35ae53c25f 100644 --- a/yarn-project/aztec.js/src/utils/cheat_codes.ts +++ b/yarn-project/aztec.js/src/utils/cheat_codes.ts @@ -444,7 +444,8 @@ export class AztecCheatCodes { * @returns The storage slot of the value in the map */ public computeSlotInMap(mapSlot: Fr | bigint, key: Fr | bigint | AztecAddress): Fr { - return deriveStorageSlotInMap(mapSlot, new Fr(key)); + const keyFr = typeof key === 'bigint' ? new Fr(key) : key.toField(); + return deriveStorageSlotInMap(mapSlot, keyFr); } /** diff --git a/yarn-project/aztec.js/src/utils/pxe.ts b/yarn-project/aztec.js/src/utils/pxe.ts index 700a003d217..57d79ce15ae 100644 --- a/yarn-project/aztec.js/src/utils/pxe.ts +++ b/yarn-project/aztec.js/src/utils/pxe.ts @@ -5,8 +5,9 @@ import { retryUntil } from '@aztec/foundation/retry'; export const waitForPXE = async (pxe: PXE, logger?: DebugLogger) => { await retryUntil(async () => { try { - logger?.debug('Attempting to contact PXE...'); + logger?.verbose('Attempting to contact PXE...'); await pxe.getNodeInfo(); + logger?.verbose('Contacted PXE'); return true; } catch (error) { logger?.verbose('Failed to contact PXE'); diff --git a/yarn-project/aztec.js/src/wallet/base_wallet.ts b/yarn-project/aztec.js/src/wallet/base_wallet.ts index bc896799004..9e5156f01d2 100644 --- a/yarn-project/aztec.js/src/wallet/base_wallet.ts +++ b/yarn-project/aztec.js/src/wallet/base_wallet.ts @@ -1,6 +1,6 @@ import { type AuthWitness, - type EventType, + type EventMetadataDefinition, type ExtendedNote, type GetUnencryptedLogsResponse, type IncomingNotesFilter, @@ -33,7 +33,7 @@ import { type PartialAddress, type Point, } from '@aztec/circuits.js'; -import type { AbiType, ContractArtifact, EventSelector } from '@aztec/foundation/abi'; +import type { AbiDecoded, ContractArtifact } from '@aztec/foundation/abi'; import { type Wallet } from '../account/wallet.js'; import { type ExecutionRequestInit } from '../entrypoint/entrypoint.js'; @@ -152,7 +152,7 @@ export abstract class BaseWallet implements Wallet { args: any[], to: AztecAddress, from?: AztecAddress | undefined, - ): Promise { + ): Promise { return this.pxe.simulateUnconstrained(functionName, args, to, from); } getUnencryptedLogs(filter: LogFilter): Promise { @@ -197,16 +197,8 @@ export abstract class BaseWallet implements Wallet { getPXEInfo(): Promise { return this.pxe.getPXEInfo(); } - getEvents( - type: EventType, - event: { - /** The event selector */ - eventSelector: EventSelector; - /** The event's abi type */ - abiType: AbiType; - /** The field names */ - fieldNames: string[]; - }, + getEncryptedEvents( + event: EventMetadataDefinition, from: number, limit: number, vpks: Point[] = [ @@ -214,7 +206,10 @@ export abstract class BaseWallet implements Wallet { this.getCompleteAddress().publicKeys.masterOutgoingViewingPublicKey, ], ): Promise { - return this.pxe.getEvents(type, event, from, limit, vpks); + return this.pxe.getEncryptedEvents(event, from, limit, vpks); + } + getUnencryptedEvents(event: EventMetadataDefinition, from: number, limit: number): Promise { + return this.pxe.getUnencryptedEvents(event, from, limit); } public getL1ToL2MembershipWitness( contractAddress: AztecAddress, diff --git a/yarn-project/aztec/src/cli/cli.ts b/yarn-project/aztec/src/cli/cli.ts index 24251b0165e..57650fede28 100644 --- a/yarn-project/aztec/src/cli/cli.ts +++ b/yarn-project/aztec/src/cli/cli.ts @@ -1,11 +1,13 @@ import { deployInitialTestAccounts } from '@aztec/accounts/testing'; -import { createAztecNodeRpcServer } from '@aztec/aztec-node'; -import { type ServerList, createNamespacedJsonRpcServer, createStatusRouter } from '@aztec/foundation/json-rpc/server'; +import { AztecNodeApiSchema, PXESchema } from '@aztec/circuit-types'; +import { + type NamespacedApiHandlers, + createNamespacedSafeJsonRpcServer, + startHttpRpcServer, +} from '@aztec/foundation/json-rpc/server'; import { type DebugLogger, type LogFn } from '@aztec/foundation/log'; -import { createPXERpcServer } from '@aztec/pxe'; import { Command } from 'commander'; -import http from 'http'; import { setupConsoleJsonLog } from '../logging.js'; import { createSandbox } from '../sandbox.js'; @@ -44,7 +46,7 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge // list of 'stop' functions to call when process ends const signalHandlers: Array<() => Promise> = []; - let services: ServerList = []; + const services: NamespacedApiHandlers = {}; if (options.sandbox) { const sandboxOptions = extractNamespacedOptions(options, 'sandbox'); @@ -69,38 +71,37 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge } // Start Node and PXE JSON-RPC server - const nodeServer = createAztecNodeRpcServer(node); - const pxeServer = createPXERpcServer(pxe); signalHandlers.push(stop); - services = [{ node: nodeServer }, { pxe: pxeServer }]; + services.node = [node, AztecNodeApiSchema]; + services.pxe = [pxe, PXESchema]; } else { if (options.node) { const { startNode } = await import('./cmds/start_node.js'); - services = await startNode(options, signalHandlers, userLog); + await startNode(options, signalHandlers, services, userLog); } else if (options.proofVerifier) { const { startProofVerifier } = await import('./cmds/start_proof_verifier.js'); - services = await startProofVerifier(options, signalHandlers, userLog); + await startProofVerifier(options, signalHandlers, userLog); } else if (options.bot) { const { startBot } = await import('./cmds/start_bot.js'); - services = await startBot(options, signalHandlers, userLog); + await startBot(options, signalHandlers, services, userLog); } else if (options.proverNode) { const { startProverNode } = await import('./cmds/start_prover_node.js'); - services = await startProverNode(options, signalHandlers, userLog); + await startProverNode(options, signalHandlers, services, userLog); } else if (options.pxe) { const { startPXE } = await import('./cmds/start_pxe.js'); - services = await startPXE(options, signalHandlers, userLog); + await startPXE(options, signalHandlers, services, userLog); } else if (options.archiver) { const { startArchiver } = await import('./cmds/start_archiver.js'); - services = await startArchiver(options, signalHandlers); + await startArchiver(options, signalHandlers, services); } else if (options.p2pBootstrap) { const { startP2PBootstrap } = await import('./cmds/start_p2p_bootstrap.js'); await startP2PBootstrap(options, userLog, debugLogger); } else if (options.prover) { const { startProverAgent } = await import('./cmds/start_prover_agent.js'); - services = await startProverAgent(options, signalHandlers, userLog); + await startProverAgent(options, signalHandlers, services, userLog); } else if (options.txe) { const { startTXE } = await import('./cmds/start_txe.js'); - startTXE(options, debugLogger); + await startTXE(options, debugLogger); } else if (options.sequencer) { userLog(`Cannot run a standalone sequencer without a node`); process.exit(1); @@ -112,17 +113,10 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge installSignalHandlers(debugLogger.info, signalHandlers); - if (services.length) { - const rpcServer = createNamespacedJsonRpcServer(services, debugLogger); - - const app = rpcServer.getApp(options.apiPrefix); - // add status route - const statusRouter = createStatusRouter(() => rpcServer.isHealthy(), options.apiPrefix); - app.use(statusRouter.routes()).use(statusRouter.allowedMethods()); - - const httpServer = http.createServer(app.callback()); - httpServer.listen(options.port); - debugLogger.info(`Aztec Server listening on port ${options.port}`); + if (Object.entries(services).length > 0) { + const rpcServer = createNamespacedSafeJsonRpcServer(services, debugLogger); + const { port } = await startHttpRpcServer(rpcServer, { port: options.port }); + debugLogger.info(`Aztec Server listening on port ${port}`); } }); diff --git a/yarn-project/aztec/src/cli/cmds/start_archiver.ts b/yarn-project/aztec/src/cli/cmds/start_archiver.ts index d9e0c092aa7..cdee55cc214 100644 --- a/yarn-project/aztec/src/cli/cmds/start_archiver.ts +++ b/yarn-project/aztec/src/cli/cmds/start_archiver.ts @@ -1,12 +1,7 @@ -import { - Archiver, - type ArchiverConfig, - KVArchiverDataStore, - archiverConfigMappings, - createArchiverRpcServer, -} from '@aztec/archiver'; +import { Archiver, type ArchiverConfig, KVArchiverDataStore, archiverConfigMappings } from '@aztec/archiver'; import { createDebugLogger } from '@aztec/aztec.js'; -import { type ServerList } from '@aztec/foundation/json-rpc/server'; +import { ArchiverApiSchema } from '@aztec/circuit-types'; +import { type NamespacedApiHandlers } from '@aztec/foundation/json-rpc/server'; import { createStore } from '@aztec/kv-store/utils'; import { createAndStartTelemetryClient, @@ -15,9 +10,12 @@ import { import { extractRelevantOptions } from '../util.js'; -export const startArchiver = async (options: any, signalHandlers: (() => Promise)[]) => { - const services: ServerList = []; - // Start a standalone archiver. +/** Starts a standalone archiver. */ +export async function startArchiver( + options: any, + signalHandlers: (() => Promise)[], + services: NamespacedApiHandlers, +) { const archiverConfig = extractRelevantOptions(options, archiverConfigMappings, 'archiver'); const storeLog = createDebugLogger('aztec:archiver:lmdb'); @@ -26,8 +24,7 @@ export const startArchiver = async (options: any, signalHandlers: (() => Promise const telemetry = await createAndStartTelemetryClient(getTelemetryClientConfig()); const archiver = await Archiver.createAndSync(archiverConfig, archiverStore, telemetry, true); - const archiverServer = createArchiverRpcServer(archiver); - services.push({ archiver: archiverServer }); + services.archiver = [archiver, ArchiverApiSchema]; signalHandlers.push(archiver.stop); return services; -}; +} diff --git a/yarn-project/aztec/src/cli/cmds/start_bot.ts b/yarn-project/aztec/src/cli/cmds/start_bot.ts index cf61ed0aa95..1d3e110db33 100644 --- a/yarn-project/aztec/src/cli/cmds/start_bot.ts +++ b/yarn-project/aztec/src/cli/cmds/start_bot.ts @@ -1,6 +1,6 @@ -import { type BotConfig, BotRunner, botConfigMappings, createBotRunnerRpcServer } from '@aztec/bot'; +import { type BotConfig, BotRunner, botConfigMappings, getBotRunnerApiHandler } from '@aztec/bot'; import { type AztecNode, type PXE } from '@aztec/circuit-types'; -import { type ServerList } from '@aztec/foundation/json-rpc/server'; +import { type NamespacedApiHandlers } from '@aztec/foundation/json-rpc/server'; import { type LogFn } from '@aztec/foundation/log'; import { extractRelevantOptions } from '../util.js'; @@ -8,11 +8,9 @@ import { extractRelevantOptions } from '../util.js'; export async function startBot( options: any, signalHandlers: (() => Promise)[], + services: NamespacedApiHandlers, userLog: LogFn, -): Promise { - // Services that will be started in a single multi-rpc server - const services: ServerList = []; - +) { const { proverNode, archiver, sequencer, p2pBootstrap, txe, prover } = options; if (proverNode || archiver || sequencer || p2pBootstrap || txe || prover) { userLog( @@ -24,27 +22,25 @@ export async function startBot( let pxe: PXE | undefined; if (options.pxe) { const { addPXE } = await import('./start_pxe.js'); - pxe = await addPXE(options, services, signalHandlers, userLog); + pxe = await addPXE(options, signalHandlers, services, userLog); } - await addBot(options, services, signalHandlers, { pxe }); - return services; + await addBot(options, signalHandlers, services, { pxe }); } export function addBot( options: any, - services: ServerList, signalHandlers: (() => Promise)[], + services: NamespacedApiHandlers, deps: { pxe?: PXE; node?: AztecNode } = {}, ) { const config = extractRelevantOptions(options, botConfigMappings, 'bot'); const botRunner = new BotRunner(config, deps); - const botServer = createBotRunnerRpcServer(botRunner); if (!config.noStart) { void botRunner.start(); // Do not block since bot setup takes time } - services.push({ bot: botServer }); + services.bot = getBotRunnerApiHandler(botRunner); signalHandlers.push(botRunner.stop); return Promise.resolve(); } diff --git a/yarn-project/aztec/src/cli/cmds/start_node.ts b/yarn-project/aztec/src/cli/cmds/start_node.ts index ad0cb056257..939776c345b 100644 --- a/yarn-project/aztec/src/cli/cmds/start_node.ts +++ b/yarn-project/aztec/src/cli/cmds/start_node.ts @@ -1,7 +1,7 @@ -import { aztecNodeConfigMappings, createAztecNodeRpcServer } from '@aztec/aztec-node'; -import { type PXE } from '@aztec/circuit-types'; +import { aztecNodeConfigMappings } from '@aztec/aztec-node'; +import { AztecNodeApiSchema, type PXE } from '@aztec/circuit-types'; import { NULL_KEY } from '@aztec/ethereum'; -import { type ServerList } from '@aztec/foundation/json-rpc/server'; +import { type NamespacedApiHandlers } from '@aztec/foundation/json-rpc/server'; import { type LogFn } from '@aztec/foundation/log'; import { type TelemetryClientConfig, @@ -14,14 +14,12 @@ import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts'; import { createAztecNode, deployContractsToL1 } from '../../sandbox.js'; import { extractNamespacedOptions, extractRelevantOptions } from '../util.js'; -export const startNode = async ( +export async function startNode( options: any, signalHandlers: (() => Promise)[], + services: NamespacedApiHandlers, userLog: LogFn, -): Promise => { - // Services that will be started in a single multi-rpc server - const services: ServerList = []; - +) { // options specifically namespaced with --node.