Skip to content

Commit

Permalink
fix: parse ENV variable values (#7809)
Browse files Browse the repository at this point in the history
  • Loading branch information
spypsy authored Aug 7, 2024
1 parent 048a085 commit faf2ab4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
26 changes: 13 additions & 13 deletions yarn-project/aztec/src/cli/aztec_start_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
},
Expand All @@ -89,7 +89,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = {
{
flag: '--l1-chain-id <value>',
description: 'The L1 chain ID',
defaultValue: '1337',
defaultValue: 1337,
envVar: 'L1_CHAIN_ID',
parseVal: val => parseInt(val, 10),
},
Expand Down Expand Up @@ -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',
},
{
Expand All @@ -179,14 +179,14 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = {
{
flag: '--node.l2QueueSize <value>',
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 <value>',
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),
},
Expand All @@ -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),
Expand All @@ -204,7 +204,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = {
{
flag: '--telemetry',
description: 'Enable telemetry',
defaultValue: 'false',
defaultValue: false,
envVar: 'TELEMETRY',
},
...getOptions('tel', telemetryClientConfigMappings),
Expand Down Expand Up @@ -293,7 +293,7 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = {
{
flag: '--txe.port <value>',
description: 'Port to run TXE on',
defaultValue: '8081',
defaultValue: 8081,
envVar: 'TXE_PORT',
parseVal: val => parseInt(val, 10),
},
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/cli/cmds/start_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion yarn-project/aztec/src/cli/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,30 @@ 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 => {
cmd.option(
opt.flag,
`${opt.description} (default: ${opt.defaultValue}) ($${opt.envVar})`,
opt.parseVal ? opt.parseVal : val => val,
process.env[opt.envVar || ''] || opt.defaultValue,
getDefaultOrEnvValue(opt),
);
});
};
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export async function createSandbox(config: Partial<SandboxConfig> = {}) {
}

const client = createAndStartTelemetryClient(getTelemetryClientConfig());
const node = await createAztecNode(client, aztecNodeConfig);
const node = await createAztecNode(aztecNodeConfig, client);
const pxe = await createAztecPXE(node);

await deployCanonicalKeyRegistry(
Expand Down Expand Up @@ -189,7 +189,7 @@ export async function createSandbox(config: Partial<SandboxConfig> = {}) {
* Create and start a new Aztec RPC HTTP Server
* @param config - Optional Aztec node settings.
*/
export async function createAztecNode(telemetryClient?: TelemetryClient, config: Partial<AztecNodeConfig> = {}) {
export async function createAztecNode(config: Partial<AztecNodeConfig> = {}, telemetryClient?: TelemetryClient) {
const aztecNodeConfig: AztecNodeConfig = { ...getConfigEnvVars(), ...config };
const node = await AztecNodeService.createAndSync(aztecNodeConfig, telemetryClient);
return node;
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/kv-store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export async function initStoreForRollup<T extends AztecKVStore>(
rollupAddress: EthAddress,
log?: Logger,
): Promise<T> {
if (!rollupAddress) {
throw new Error('Rollup address is required');
}
const rollupAddressValue = store.openSingleton<ReturnType<EthAddress['toString']>>('rollupAddress');
const rollupAddressString = rollupAddress.toString();
const storedRollupAddressString = rollupAddressValue.get();
Expand Down

0 comments on commit faf2ab4

Please sign in to comment.