-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17f09b6
commit 71885e4
Showing
10 changed files
with
182 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { type BotConfig, BotRunner, createBotRunnerRpcServer, getBotConfigFromEnv } from '@aztec/bot'; | ||
import { type PXE } from '@aztec/circuit-types'; | ||
import { type ServerList } from '@aztec/foundation/json-rpc/server'; | ||
import { type LogFn } from '@aztec/foundation/log'; | ||
|
||
import { mergeEnvVarsAndCliOptions, parseModuleOptions } from '../util.js'; | ||
|
||
export async function startBot( | ||
options: any, | ||
signalHandlers: (() => Promise<void>)[], | ||
userLog: LogFn, | ||
): Promise<ServerList> { | ||
// 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( | ||
`Starting a bot with --prover-node, --prover, --archiver, --sequencer, --p2p-bootstrap, or --txe is not supported.`, | ||
); | ||
process.exit(1); | ||
} | ||
|
||
await addBot(options, services, signalHandlers); | ||
return services; | ||
} | ||
|
||
export async function addBot( | ||
options: any, | ||
services: ServerList, | ||
signalHandlers: (() => Promise<void>)[], | ||
deps: { pxe?: PXE } = {}, | ||
) { | ||
const envVars = getBotConfigFromEnv(); | ||
const cliOptions = parseModuleOptions(options.bot); | ||
const config = mergeEnvVarsAndCliOptions<BotConfig>(envVars, cliOptions); | ||
|
||
const botRunner = new BotRunner(config, { pxe: deps.pxe }); | ||
const botServer = createBotRunnerRpcServer(botRunner); | ||
await botRunner.start(); | ||
services.push({ bot: botServer }); | ||
signalHandlers.push(botRunner.stop); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
import { createAztecNodeClient } from '@aztec/circuit-types'; | ||
import { type AztecNode, createAztecNodeClient } from '@aztec/circuit-types'; | ||
import { type ServerList } from '@aztec/foundation/json-rpc/server'; | ||
import { type LogFn } from '@aztec/foundation/log'; | ||
import { type PXEServiceConfig, createPXERpcServer, createPXEService, getPXEServiceConfig } from '@aztec/pxe'; | ||
|
||
import { mergeEnvVarsAndCliOptions, parseModuleOptions } from '../util.js'; | ||
|
||
const { AZTEC_NODE_URL } = process.env; | ||
|
||
export const startPXE = async (options: any, signalHandlers: (() => Promise<void>)[], userLog: LogFn) => { | ||
// Services that will be started in a single multi-rpc server | ||
export async function startPXE(options: any, signalHandlers: (() => Promise<void>)[], userLog: LogFn) { | ||
const services: ServerList = []; | ||
// Starting a PXE with a remote node. | ||
// get env vars first | ||
const pxeConfigEnvVars = getPXEServiceConfig(); | ||
// get config from options | ||
const pxeCliOptions = parseModuleOptions(options.pxe); | ||
await addPXE(options, services, signalHandlers, userLog, {}); | ||
return services; | ||
} | ||
|
||
// Determine node url from options or env vars | ||
const nodeUrl = pxeCliOptions.nodeUrl || AZTEC_NODE_URL; | ||
// throw if no Aztec Node URL is provided | ||
if (!nodeUrl) { | ||
export async function addPXE( | ||
options: any, | ||
services: ServerList, | ||
signalHandlers: (() => Promise<void>)[], | ||
userLog: LogFn, | ||
deps: { node?: AztecNode } = {}, | ||
) { | ||
const pxeCliOptions = parseModuleOptions(options.pxe); | ||
const pxeConfig = mergeEnvVarsAndCliOptions<PXEServiceConfig>(getPXEServiceConfig(), pxeCliOptions); | ||
const nodeUrl = pxeCliOptions.nodeUrl ?? process.env.AZTEC_NODE_URL; | ||
if (!nodeUrl && !deps.node) { | ||
userLog('Aztec Node URL (nodeUrl | AZTEC_NODE_URL) option is required to start PXE without --node option'); | ||
throw new Error('Aztec Node URL (nodeUrl | AZTEC_NODE_URL) option is required to start PXE without --node option'); | ||
process.exit(1); | ||
} | ||
|
||
// merge env vars and cli options | ||
const pxeConfig = mergeEnvVarsAndCliOptions<PXEServiceConfig>(pxeConfigEnvVars, pxeCliOptions); | ||
|
||
// create a node client | ||
const node = createAztecNodeClient(nodeUrl); | ||
|
||
const node = deps.node ?? createAztecNodeClient(nodeUrl); | ||
const pxe = await createPXEService(node, pxeConfig); | ||
const pxeServer = createPXERpcServer(pxe); | ||
|
||
// Add PXE to services list | ||
services.push({ pxe: pxeServer }); | ||
|
||
// Add PXE stop function to signal handlers | ||
signalHandlers.push(pxe.stop); | ||
return services; | ||
}; | ||
|
||
return pxe; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Bot } from './bot.js'; | ||
export { BotRunner } from './runner.js'; | ||
export { BotConfig, getBotConfigFromEnv, getBotDefaultConfig } from './config.js'; | ||
export { createBotRunnerRpcServer } from './rpc.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TxHash } from '@aztec/circuit-types'; | ||
import { AztecAddress } from '@aztec/foundation/aztec-address'; | ||
import { EthAddress } from '@aztec/foundation/eth-address'; | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { JsonRpcServer } from '@aztec/foundation/json-rpc/server'; | ||
|
||
import { type BotRunner } from './runner.js'; | ||
|
||
/** | ||
* Wraps a bot runner with a JSON RPC HTTP server. | ||
* @param botRunner - The BotRunner. | ||
* @returns An JSON-RPC HTTP server | ||
*/ | ||
export function createBotRunnerRpcServer(botRunner: BotRunner) { | ||
return new JsonRpcServer(botRunner, { AztecAddress, EthAddress, Fr, TxHash }, {}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,102 @@ | ||
import { createDebugLogger } from '@aztec/aztec.js'; | ||
import { type PXE, createDebugLogger } from '@aztec/aztec.js'; | ||
|
||
import { Bot } from './bot.js'; | ||
import { type BotConfig } from './config.js'; | ||
|
||
export class BotRunner { | ||
private log = createDebugLogger('aztec:bot'); | ||
private interval?: NodeJS.Timeout; | ||
private bot: Promise<Bot>; | ||
private bot?: Promise<Bot>; | ||
private pxe?: PXE; | ||
private running: Set<Promise<void>> = new Set(); | ||
|
||
protected constructor(private config: BotConfig) { | ||
this.bot = Bot.create(this.config); | ||
public constructor(private config: BotConfig, dependencies: { pxe?: PXE } = {}) { | ||
this.pxe = dependencies.pxe; | ||
} | ||
|
||
/** Initializes the bot if needed. Blocks until the bot setup is finished. */ | ||
public async setup() { | ||
if (!this.bot) { | ||
this.log.verbose(`Setting up bot`); | ||
await this.#createBot(); | ||
this.log.info(`Bot set up completed`); | ||
} | ||
} | ||
|
||
/** | ||
* Initializes the bot if needed and starts sending txs at regular intervals. | ||
* Blocks until the bot setup is finished. | ||
*/ | ||
public async start() { | ||
await this.setup(); | ||
if (!this.interval) { | ||
await this.bot; | ||
this.log.info(`Starting bot with interval of ${this.config.txIntervalSeconds}s`); | ||
this.interval = setInterval(() => this.run(), this.config.txIntervalSeconds * 1000); | ||
} | ||
} | ||
|
||
public stop() { | ||
/** | ||
* Stops sending txs. Returns once all ongoing txs are finished. | ||
*/ | ||
public async stop() { | ||
if (this.interval) { | ||
this.log.info(`Stopping bot`); | ||
this.log.verbose(`Stopping bot`); | ||
clearInterval(this.interval); | ||
this.interval = undefined; | ||
} | ||
if (this.running.size > 0) { | ||
this.log.verbose(`Waiting for ${this.running.size} running txs to finish`); | ||
await Promise.all(this.running); | ||
} | ||
this.log.info(`Stopped bot`); | ||
} | ||
|
||
/** Returns whether the bot is running. */ | ||
public isRunning() { | ||
return !!this.interval; | ||
} | ||
|
||
public update(config: BotConfig) { | ||
/** | ||
* Updates the bot config and recreates the bot. Will stop and restart the bot automatically if it was | ||
* running when this method was called. Blocks until the new bot is set up. | ||
*/ | ||
public async update(config: BotConfig) { | ||
this.log.verbose(`Updating bot config`); | ||
const wasRunning = this.isRunning(); | ||
if (wasRunning) { | ||
this.stop(); | ||
await this.stop(); | ||
} | ||
this.config = config; | ||
this.bot = Bot.create(this.config); | ||
this.config = { ...this.config, ...config }; | ||
await this.#createBot(); | ||
this.log.info(`Bot config updated`); | ||
if (wasRunning) { | ||
void this.start(); | ||
await this.start(); | ||
} | ||
} | ||
|
||
/** | ||
* Triggers a single iteration of the bot. Requires the bot to be initialized. | ||
* Blocks until the run is finished. | ||
*/ | ||
public async run() { | ||
if (!this.bot) { | ||
throw new Error(`Bot is not initialized`); | ||
} | ||
this.log.verbose(`Manually triggered bot run`); | ||
const bot = await this.bot; | ||
await bot.run(); | ||
const promise = bot.run(); | ||
this.running.add(promise); | ||
await promise; | ||
this.running.delete(promise); | ||
} | ||
|
||
/** Returns the current configuration for the bot. */ | ||
public getConfig() { | ||
return this.config; | ||
} | ||
|
||
async #createBot() { | ||
this.bot = Bot.create(this.config, { pxe: this.pxe }); | ||
await this.bot; | ||
} | ||
} |