Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add boolean config helper #7884

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions yarn-project/aztec-node/src/aztec-node/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ArchiverConfig, archiverConfigMappings } from '@aztec/archiver';
import { type ConfigMappingsType, getConfigFromMappings } from '@aztec/foundation/config';
import { type ConfigMappingsType, booleanConfigHelper, getConfigFromMappings } from '@aztec/foundation/config';
import { type P2PConfig, p2pConfigMappings } from '@aztec/p2p';
import { type ProverClientConfig, proverClientConfigMappings } from '@aztec/prover-client';
import { type SequencerClientConfig, sequencerClientConfigMappings } from '@aztec/sequencer-client';
Expand Down Expand Up @@ -34,15 +34,13 @@ export const aztecNodeConfigMappings: ConfigMappingsType<AztecNodeConfig> = {
...p2pConfigMappings,
disableSequencer: {
env: 'SEQ_DISABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val),
default: false,
description: 'Whether the sequencer is disabled for this node.',
...booleanConfigHelper(),
},
disableProver: {
env: 'PROVER_DISABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val),
default: false,
description: 'Whether the prover is disabled for this node.',
...booleanConfigHelper(),
},
};

Expand Down
21 changes: 11 additions & 10 deletions yarn-project/aztec/src/cli/aztec_start_options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { type ArchiverConfig, archiverConfigMappings } from '@aztec/archiver';
import { sequencerClientConfigMappings } from '@aztec/aztec-node';
import { botConfigMappings } from '@aztec/bot';
import { type ConfigMapping, filterConfigMappings, isBooleanConfigValue } from '@aztec/foundation/config';
import {
type ConfigMapping,
booleanConfigHelper,
filterConfigMappings,
isBooleanConfigValue,
} from '@aztec/foundation/config';
import { bootnodeConfigMappings, p2pConfigMappings } from '@aztec/p2p';
import { proverClientConfigMappings } from '@aztec/prover-client';
import { proverNodeConfigMappings } from '@aztec/prover-node';
Expand All @@ -22,7 +27,7 @@ export interface AztecStartOption {

export const getOptions = (namespace: string, configMappings: Record<string, ConfigMapping>) => {
const options: AztecStartOption[] = [];
for (const [key, { env, default: def, parseEnv, description, printDefault }] of Object.entries(configMappings)) {
for (const [key, { env, defaultValue: def, parseEnv, description, printDefault }] of Object.entries(configMappings)) {
if (universalOptions.includes(key)) {
continue;
}
Expand Down Expand Up @@ -54,16 +59,14 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = {
{
flag: '--sandbox.testAccounts',
description: 'Deploy test accounts on sandbox start',
defaultValue: true,
envVar: 'TEST_ACCOUNTS',
parseVal: val => ['1', true].includes(val),
...booleanConfigHelper(true),
},
{
flag: '--sandbox.enableGas',
description: 'Enable gas on sandbox start',
defaultValue: false,
envVar: 'ENABLE_GAS',
parseVal: val => ['1', true].includes(val),
...booleanConfigHelper(),
},
],
API: [
Expand Down Expand Up @@ -169,9 +172,8 @@ 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,
envVar: 'DEPLOY_AZTEC_CONTRACTS',
parseVal: val => ['1', true].includes(val),
...booleanConfigHelper(),
},
{
flag: '--node.assumeProvenUntilBlockNumber',
Expand Down Expand Up @@ -206,9 +208,8 @@ export const aztecStartOptions: { [key: string]: AztecStartOption[] } = {
{
flag: '--p2p-enabled',
description: 'Enable P2P subsystem',
defaultValue: false,
envVar: 'P2P_ENABLED',
parseVal: val => ['1', true].includes(val),
...booleanConfigHelper(),
},
...getOptions('p2p', p2pConfigMappings),
],
Expand Down
15 changes: 7 additions & 8 deletions yarn-project/bot/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Fr } from '@aztec/circuits.js';
import {
type ConfigMappingsType,
booleanConfigHelper,
getConfigFromMappings,
getDefaultConfig,
numberConfigHelper,
Expand Down Expand Up @@ -40,19 +41,19 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
env: 'BOT_PRIVATE_KEY',
description: 'Signing private key for the sender account.',
parseEnv: (val: string) => Fr.fromString(val),
default: Fr.random(),
defaultValue: Fr.random(),
},
recipientEncryptionSecret: {
env: 'BOT_RECIPIENT_ENCRYPTION_SECRET',
description: 'Encryption secret for a recipient account.',
parseEnv: (val: string) => Fr.fromString(val),
default: Fr.fromString('0xcafecafe'),
defaultValue: Fr.fromString('0xcafecafe'),
},
tokenSalt: {
env: 'BOT_TOKEN_SALT',
description: 'Salt for the token contract deployment.',
parseEnv: (val: string) => Fr.fromString(val),
default: Fr.fromString('1'),
defaultValue: Fr.fromString('1'),
},
txIntervalSeconds: {
env: 'BOT_TX_INTERVAL_SECONDS',
Expand All @@ -73,13 +74,12 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
env: 'BOT_FEE_PAYMENT_METHOD',
description: 'How to handle fee payments. (Options: fee_juice, none)',
parseEnv: val => (val as 'fee_juice' | 'none') || undefined,
default: 'none',
defaultValue: 'none',
},
noStart: {
env: 'BOT_NO_START',
description: 'True to not automatically setup or start the bot on initialization.',
parseEnv: val => ['1', 'true'].includes(val),
default: false,
...booleanConfigHelper(),
},
txMinedWaitSeconds: {
env: 'BOT_TX_MINED_WAIT_SECONDS',
Expand All @@ -89,8 +89,7 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
noWaitForTransfers: {
env: 'BOT_NO_WAIT_FOR_TRANSFERS',
description: "Don't wait for transfer transactions.",
parseEnv: val => ['1', 'true'].includes(val),
default: false,
...booleanConfigHelper(),
},
};

Expand Down
8 changes: 3 additions & 5 deletions yarn-project/circuit-types/src/interfaces/prover-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type TxHash } from '@aztec/circuit-types';
import { Fr } from '@aztec/circuits.js';
import { type ConfigMappingsType, numberConfigHelper } from '@aztec/foundation/config';
import { type ConfigMappingsType, booleanConfigHelper, numberConfigHelper } from '@aztec/foundation/config';

import { type BlockProver } from './block-prover.js';
import { type MerkleTreeOperations } from './merkle_tree_operations.js';
Expand Down Expand Up @@ -37,15 +37,13 @@ export const proverConfigMappings: ConfigMappingsType<ProverConfig> = {
},
realProofs: {
env: 'PROVER_REAL_PROOFS',
parseEnv: (val: string) => ['1', 'true'].includes(val),
default: false,
description: 'Whether to construct real proofs',
...booleanConfigHelper(),
},
proverAgentEnabled: {
env: 'PROVER_AGENT_ENABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val),
default: true,
description: 'Whether this prover has a local prover agent',
...booleanConfigHelper(true),
},
proverAgentPollInterval: {
env: 'PROVER_AGENT_POLL_INTERVAL_MS',
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/ethereum/src/l1_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export const l1ReaderConfigMappings: ConfigMappingsType<L1ReaderConfig> = {
l1ChainId: {
env: 'L1_CHAIN_ID',
parseEnv: (val: string) => +val,
default: 31337,
defaultValue: 31337,
description: 'The chain ID of the ethereum host.',
},
// NOTE: Special case for l1Contracts
l1Contracts: {
description: 'The deployed L1 contract addresses',
default: l1ContractAddressesMapping,
defaultValue: l1ContractAddressesMapping,
},
};
27 changes: 21 additions & 6 deletions yarn-project/foundation/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export { EnvVar } from './env_var.js';
export interface ConfigMapping {
env?: EnvVar;
parseEnv?: (val: string) => any;
default?: any;
defaultValue?: any;
printDefault?: (val: any) => string;
description: string;
isBoolean?: boolean;
Expand All @@ -22,7 +22,7 @@ export function getConfigFromMappings<T>(configMappings: ConfigMappingsType<T>):

for (const key in configMappings) {
if (configMappings[key]) {
const { env, parseEnv, default: def } = configMappings[key];
const { env, parseEnv, defaultValue: def } = configMappings[key];
// Special case for L1 contract addresses which is an object of config values
if (key === 'l1Contracts' && def) {
(config as any)[key] = getConfigFromMappings(def);
Expand Down Expand Up @@ -60,10 +60,25 @@ export function filterConfigMappings<T, K extends keyof T>(
* @param defaultVal - The default numerical value to use if the environment variable is not set or is invalid
* @returns Object with parseEnv and default values for a numerical config value
*/
export function numberConfigHelper(defaultVal: number): Partial<ConfigMapping> {
export function numberConfigHelper(defaultVal: number): Pick<ConfigMapping, 'parseEnv' | 'defaultValue'> {
return {
parseEnv: (val: string) => safeParseNumber(val, defaultVal),
default: defaultVal,
defaultValue: defaultVal,
};
}

/**
* Generates parseEnv and default values for a boolean config value.
* @param defaultVal - The default value to use if the environment variable is not set or is invalid
* @returns Object with parseEnv and default values for a boolean config value
*/
export function booleanConfigHelper(
defaultVal = false,
): Required<Pick<ConfigMapping, 'parseEnv' | 'defaultValue' | 'isBoolean'>> {
return {
parseEnv: (val: string) => ['1', 'true', 'TRUE'].includes(val),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe yes as well?

Copy link
Collaborator Author

@spalladino spalladino Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had added 'yes' and then removed it, since it wasn't used anywhere (except a single smoke test)

defaultValue: defaultVal,
isBoolean: true,
};
}

Expand Down Expand Up @@ -106,8 +121,8 @@ export function getDefaultConfig<T>(configMappings: ConfigMappingsType<T>): T {
const defaultConfig = {} as T;

for (const key in configMappings) {
if (configMappings[key] && configMappings[key].default !== undefined) {
(defaultConfig as any)[key] = configMappings[key].default;
if (configMappings[key] && configMappings[key].defaultValue !== undefined) {
(defaultConfig as any)[key] = configMappings[key].defaultValue;
}
}

Expand Down
11 changes: 6 additions & 5 deletions yarn-project/p2p/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
type ConfigMappingsType,
booleanConfigHelper,
getConfigFromMappings,
numberConfigHelper,
pickConfigMappings,
Expand Down Expand Up @@ -91,8 +92,8 @@ export interface P2PConfig {
export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
p2pEnabled: {
env: 'P2P_ENABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val),
description: 'A flag dictating whether the P2P subsystem should be enabled.',
...booleanConfigHelper(),
},
blockCheckIntervalMS: {
env: 'P2P_BLOCK_CHECK_INTERVAL_MS',
Expand All @@ -111,12 +112,12 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
},
tcpListenAddress: {
env: 'TCP_LISTEN_ADDR',
default: '0.0.0.0:40400',
defaultValue: '0.0.0.0:40400',
description: 'The listen address for TCP. Format: <IP_ADDRESS>:<PORT>.',
},
udpListenAddress: {
env: 'UDP_LISTEN_ADDR',
default: '0.0.0.0:40400',
defaultValue: '0.0.0.0:40400',
description: 'The listen address for UDP. Format: <IP_ADDRESS>:<PORT>.',
},
tcpAnnounceAddress: {
Expand All @@ -141,7 +142,7 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
transactionProtocol: {
env: 'P2P_TX_PROTOCOL',
description: 'Protocol identifier for transaction gossiping.',
default: '/aztec/0.1.0',
defaultValue: '/aztec/0.1.0',
},
minPeerCount: {
env: 'P2P_MIN_PEERS',
Expand All @@ -161,7 +162,7 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
env: 'P2P_QUERY_FOR_IP',
description:
'If announceUdpAddress or announceTcpAddress are not provided, query for the IP address of the machine. Default is false.',
parseEnv: (val: string) => ['1', 'true'].includes(val),
...booleanConfigHelper(),
},
keepProvenTxsInPoolFor: {
env: 'P2P_TX_POOL_KEEP_PROVEN_FOR',
Expand Down
5 changes: 2 additions & 3 deletions yarn-project/prover-client/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ProverConfig, proverConfigMappings } from '@aztec/circuit-types';
import { type ConfigMappingsType, getConfigFromMappings } from '@aztec/foundation/config';
import { type ConfigMappingsType, booleanConfigHelper, getConfigFromMappings } from '@aztec/foundation/config';

/**
* The prover configuration.
Expand Down Expand Up @@ -36,9 +36,8 @@ export const proverClientConfigMappings: ConfigMappingsType<ProverClientConfig>
},
disableProver: {
env: 'PROVER_DISABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val),
default: false,
description: 'Whether to disable proving.',
...booleanConfigHelper(),
},
...proverConfigMappings,
};
Expand Down
12 changes: 8 additions & 4 deletions yarn-project/pxe/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js/constants';
import { type ConfigMappingsType, getConfigFromMappings, numberConfigHelper } from '@aztec/foundation/config';
import {
type ConfigMappingsType,
booleanConfigHelper,
getConfigFromMappings,
numberConfigHelper,
} from '@aztec/foundation/config';
import { type Network } from '@aztec/types/network';

import { readFileSync } from 'fs';
Expand Down Expand Up @@ -69,9 +74,8 @@ export const pxeConfigMappings: ConfigMappingsType<PXEServiceConfig> = {
},
proverEnabled: {
env: 'PXE_PROVER_ENABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val),
description: 'Enable real proofs',
isBoolean: true,
...booleanConfigHelper(),
},
};

Expand Down Expand Up @@ -103,7 +107,7 @@ export const allPxeConfigMappings: ConfigMappingsType<CliPXEOptions & PXEService
...pxeCliConfigMappings,
proverEnabled: {
env: 'PXE_PROVER_ENABLED',
parseEnv: (val: string) => ['1', 'true'].includes(val) || !!process.env.NETWORK,
parseEnv: (val: string) => ['1', 'true', 'TRUE'].includes(val) || !!process.env.NETWORK,
description: 'Enable real proofs',
isBoolean: true,
},
Expand Down
15 changes: 10 additions & 5 deletions yarn-project/sequencer-client/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { type AllowedElement } from '@aztec/circuit-types';
import { AztecAddress, Fr, FunctionSelector, getContractClassFromArtifact } from '@aztec/circuits.js';
import { type L1ReaderConfig, l1ReaderConfigMappings } from '@aztec/ethereum';
import { type ConfigMappingsType, getConfigFromMappings, numberConfigHelper } from '@aztec/foundation/config';
import {
type ConfigMappingsType,
booleanConfigHelper,
getConfigFromMappings,
numberConfigHelper,
} from '@aztec/foundation/config';
import { EthAddress } from '@aztec/foundation/eth-address';
import { FPCContract } from '@aztec/noir-contracts.js/FPC';
import { TokenContractArtifact } from '@aztec/noir-contracts.js/Token';
Expand Down Expand Up @@ -77,15 +82,15 @@ export const sequencerConfigMappings: ConfigMappingsType<SequencerConfig> = {
allowedInSetup: {
env: 'SEQ_ALLOWED_SETUP_FN',
parseEnv: (val: string) => parseSequencerAllowList(val),
default: getDefaultAllowedSetupFunctions(),
defaultValue: getDefaultAllowedSetupFunctions(),
description: 'The list of functions calls allowed to run in setup',
printDefault: () =>
'AuthRegistry, FeeJuice.increase_public_balance, Token.increase_public_balance, FPC.prepare_fee',
},
allowedInTeardown: {
env: 'SEQ_ALLOWED_TEARDOWN_FN',
parseEnv: (val: string) => parseSequencerAllowList(val),
default: getDefaultAllowedTeardownFunctions(),
defaultValue: getDefaultAllowedTeardownFunctions(),
description: 'The list of functions calls allowed to run teardown',
printDefault: () => 'FPC.pay_refund, FPC.pay_refund_with_shielded_rebate',
},
Expand All @@ -96,13 +101,13 @@ export const sequencerConfigMappings: ConfigMappingsType<SequencerConfig> = {
},
enforceFees: {
env: 'ENFORCE_FEES',
parseEnv: (val: string) => ['1', 'true'].includes(val),
description: 'Whether to require every tx to have a fee payer',
...booleanConfigHelper(),
},
sequencerSkipSubmitProofs: {
env: 'SEQ_SKIP_SUBMIT_PROOFS',
parseEnv: (val: string) => ['1', 'true'].includes(val),
description: 'Temporary flag to skip submitting proofs, so a prover-node takes care of it.',
...booleanConfigHelper(),
},
};

Expand Down
Loading
Loading