From faf2ab42027e194760293c93a66b487e051df02c Mon Sep 17 00:00:00 2001 From: spypsy Date: Wed, 7 Aug 2024 12:42:22 +0100 Subject: [PATCH] fix: parse ENV variable values (#7809) --- .../aztec/src/cli/aztec_start_options.ts | 26 +++++++++---------- yarn-project/aztec/src/cli/cmds/start_node.ts | 2 +- yarn-project/aztec/src/cli/util.ts | 18 ++++++++++++- yarn-project/aztec/src/sandbox.ts | 4 +-- yarn-project/kv-store/src/utils.ts | 3 +++ 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/yarn-project/aztec/src/cli/aztec_start_options.ts b/yarn-project/aztec/src/cli/aztec_start_options.ts index 27ffc133a6f..bdeab695007 100644 --- a/yarn-project/aztec/src/cli/aztec_start_options.ts +++ b/yarn-project/aztec/src/cli/aztec_start_options.ts @@ -12,7 +12,7 @@ import { telemetryClientConfigMappings } from '@aztec/telemetry-client/start'; export interface AztecStartOption { flag: string; description: string; - defaultValue: string | undefined; + defaultValue: any | undefined; printDefault?: (val: any) => string; envVar: string | undefined; parseVal?: (val: string) => any; @@ -52,23 +52,23 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--sandbox.testAccounts', description: 'Deploy test accounts on sandbox start', - defaultValue: 'true', + defaultValue: true, envVar: 'TEST_ACCOUNTS', - parseVal: val => ['1', 'true'].includes(val), + parseVal: val => ['1', true].includes(val), }, { flag: '--sandbox.enableGas', description: 'Enable gas on sandbox start', - defaultValue: 'false', + defaultValue: false, envVar: 'ENABLE_GAS', - parseVal: val => ['1', 'true'].includes(val), + parseVal: val => ['1', true].includes(val), }, ], API: [ { flag: '--port', description: 'Port to run the Aztec Services on on', - defaultValue: '8080', + defaultValue: 8080, envVar: 'AZTEC_PORT', parseVal: val => parseInt(val, 10), }, @@ -89,7 +89,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--l1-chain-id ', description: 'The L1 chain ID', - defaultValue: '1337', + defaultValue: 1337, envVar: 'L1_CHAIN_ID', parseVal: val => parseInt(val, 10), }, @@ -167,7 +167,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--node.deployAztecContracts', description: 'Deploys L1 Aztec contracts before starting the node. Needs mnemonic or private key to be set', - defaultValue: 'false', + defaultValue: false, envVar: 'DEPLOY_AZTEC_CONTRACTS', }, { @@ -179,14 +179,14 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--node.l2QueueSize ', description: 'Size of queue of L2 blocks to store in world state', - defaultValue: '1000', + defaultValue: 1000, envVar: 'L2_QUEUE_SIZE', parseVal: val => parseInt(val, 10), }, { flag: '--node.worldStateBlockCheckIntervalMS ', description: 'Frequency in which to check for blocks in ms', - defaultValue: '100', + defaultValue: 100, envVar: 'WS_BLOCK_CHECK_INTERVAL_MS', parseVal: val => parseInt(val, 10), }, @@ -195,7 +195,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--p2p-enabled', description: 'Enable P2P subsystem', - defaultValue: 'false', + defaultValue: false, envVar: 'P2P_ENABLED', }, ...getOptions('p2p', p2pConfigMappings), @@ -204,7 +204,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--telemetry', description: 'Enable telemetry', - defaultValue: 'false', + defaultValue: false, envVar: 'TELEMETRY', }, ...getOptions('tel', telemetryClientConfigMappings), @@ -293,7 +293,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = { { flag: '--txe.port ', description: 'Port to run TXE on', - defaultValue: '8081', + defaultValue: 8081, envVar: 'TXE_PORT', parseVal: val => parseInt(val, 10), }, diff --git a/yarn-project/aztec/src/cli/cmds/start_node.ts b/yarn-project/aztec/src/cli/cmds/start_node.ts index cf08afdfa13..21b6332bba5 100644 --- a/yarn-project/aztec/src/cli/cmds/start_node.ts +++ b/yarn-project/aztec/src/cli/cmds/start_node.ts @@ -108,7 +108,7 @@ export const startNode = async ( telemetryClient = createAndStartTelemetryClient(telemetryConfig); } // Create and start Aztec Node. - const node = await createAztecNode(telemetryClient, nodeConfig); + const node = await createAztecNode(nodeConfig, telemetryClient); const nodeServer = createAztecNodeRpcServer(node); // Add node to services list diff --git a/yarn-project/aztec/src/cli/util.ts b/yarn-project/aztec/src/cli/util.ts index 0c35b57a8d0..ba6838fc9b8 100644 --- a/yarn-project/aztec/src/cli/util.ts +++ b/yarn-project/aztec/src/cli/util.ts @@ -105,6 +105,22 @@ export function formatHelpLine( return `${chalk.cyan(paddedOption)}${chalk.yellow(paddedDefault)}${chalk.green(envVar)}`; } +const getDefaultOrEnvValue = (opt: AztecStartOption) => { + let val; + // if the option is set in the environment, use that & parse it + if (opt.envVar && process.env[opt.envVar]) { + val = process.env[opt.envVar]; + if (val && opt.parseVal) { + return opt.parseVal(val); + } + // if no env variable, use the default value + } else if (opt.defaultValue) { + val = opt.defaultValue; + } + + return val; +}; + // Function to add options dynamically export const addOptions = (cmd: Command, options: AztecStartOption[]) => { options.forEach(opt => { @@ -112,7 +128,7 @@ export const addOptions = (cmd: Command, options: AztecStartOption[]) => { opt.flag, `${opt.description} (default: ${opt.defaultValue}) ($${opt.envVar})`, opt.parseVal ? opt.parseVal : val => val, - process.env[opt.envVar || ''] || opt.defaultValue, + getDefaultOrEnvValue(opt), ); }); }; diff --git a/yarn-project/aztec/src/sandbox.ts b/yarn-project/aztec/src/sandbox.ts index 769e384f32d..d8034b5fbbe 100644 --- a/yarn-project/aztec/src/sandbox.ts +++ b/yarn-project/aztec/src/sandbox.ts @@ -160,7 +160,7 @@ export async function createSandbox(config: Partial = {}) { } const client = createAndStartTelemetryClient(getTelemetryClientConfig()); - const node = await createAztecNode(client, aztecNodeConfig); + const node = await createAztecNode(aztecNodeConfig, client); const pxe = await createAztecPXE(node); await deployCanonicalKeyRegistry( @@ -189,7 +189,7 @@ export async function createSandbox(config: Partial = {}) { * Create and start a new Aztec RPC HTTP Server * @param config - Optional Aztec node settings. */ -export async function createAztecNode(telemetryClient?: TelemetryClient, config: Partial = {}) { +export async function createAztecNode(config: Partial = {}, telemetryClient?: TelemetryClient) { const aztecNodeConfig: AztecNodeConfig = { ...getConfigEnvVars(), ...config }; const node = await AztecNodeService.createAndSync(aztecNodeConfig, telemetryClient); return node; diff --git a/yarn-project/kv-store/src/utils.ts b/yarn-project/kv-store/src/utils.ts index 2f913789dc5..0c96bf918d9 100644 --- a/yarn-project/kv-store/src/utils.ts +++ b/yarn-project/kv-store/src/utils.ts @@ -30,6 +30,9 @@ export async function initStoreForRollup( rollupAddress: EthAddress, log?: Logger, ): Promise { + if (!rollupAddress) { + throw new Error('Rollup address is required'); + } const rollupAddressValue = store.openSingleton>('rollupAddress'); const rollupAddressString = rollupAddress.toString(); const storedRollupAddressString = rollupAddressValue.get();