Skip to content

Commit

Permalink
refactor: reuse command types/utils across packages (#6441)
Browse files Browse the repository at this point in the history
* refactor: reuse command types/utils across packages

* Allow  as an alternative to setting a default

* Update prover cmd options

* Update comment

* Fix comment

* Remove unused import

* Demand option if arg must be provided

* Update handler
  • Loading branch information
nflaig authored Feb 22, 2024
1 parent eeaa7da commit 8959bda
Show file tree
Hide file tree
Showing 50 changed files with 95 additions and 194 deletions.
2 changes: 1 addition & 1 deletion packages/cli/docsgen/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliOptionDefinition, CliCommand, CliExample, CliCommandOptions} from "../src/util/index.js";
import {CliOptionDefinition, CliCommand, CliExample, CliCommandOptions} from "@lodestar/utils";
import {toKebab} from "./changeCase.js";

const DEFAULT_SEPARATOR = "\n\n";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Must not use `* as yargs`, see https://github.com/yargs/yargs/issues/1131
import yargs from "yargs";
import {hideBin} from "yargs/helpers";
import {registerCommandToYargs} from "@lodestar/utils";
import {cmds} from "./cmds/index.js";
import {globalOptions, rcConfigOption} from "./options/index.js";
import {registerCommandToYargs} from "./util/index.js";
import {getVersionData} from "./util/version.js";

const {version} = getVersionData();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/beacon/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand, CliCommandOptions} from "../../util/index.js";
import {CliCommand, CliCommandOptions} from "@lodestar/utils";
import {GlobalArgs} from "../../options/index.js";
import {beaconOptions, BeaconArgs} from "./options.js";
import {beaconHandler} from "./handler.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/beacon/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CliCommandOptions, CliOptionDefinition} from "@lodestar/utils";
import {beaconNodeOptions, paramsOptions, BeaconNodeArgs} from "../../options/index.js";
import {LogArgs, logOptions} from "../../options/logOptions.js";
import {CliCommandOptions, CliOptionDefinition} from "../../util/index.js";
import {defaultBeaconPaths, BeaconPaths} from "./paths.js";

type BeaconExtraArgs = {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/bootnode/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand, CliCommandOptions} from "../../util/index.js";
import {CliCommand, CliCommandOptions} from "@lodestar/utils";
import {GlobalArgs} from "../../options/index.js";
import {bootnodeOptions, BootnodeArgs} from "./options.js";
import {bootnodeHandler} from "./handler.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/bootnode/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CliOptionDefinition, CliCommandOptions} from "@lodestar/utils";
import {LogArgs, logOptions} from "../../options/logOptions.js";
import {CliOptionDefinition, CliCommandOptions} from "../../util/index.js";
import {MetricsArgs, options as metricsOptions} from "../../options/beaconNodeOptions/metrics.js";
import {defaultListenAddress, defaultP2pPort, defaultP2pPort6} from "../../options/beaconNodeOptions/network.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand, CliCommandOptions} from "../../util/index.js";
import {CliCommand, CliCommandOptions} from "@lodestar/utils";
import {GlobalArgs} from "../../options/index.js";
import {devOptions, IDevArgs} from "./options.js";
import {devHandler} from "./handler.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/dev/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommandOptions, CliOptionDefinition} from "../../util/index.js";
import {CliCommandOptions, CliOptionDefinition} from "@lodestar/utils";
import {beaconOptions, BeaconArgs} from "../beacon/options.js";
import {NetworkName} from "../../networks/index.js";
import {beaconNodeOptions, globalOptions} from "../../options/index.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand} from "../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {GlobalArgs} from "../options/index.js";
import {beacon} from "./beacon/index.js";
import {dev} from "./dev/index.js";
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/src/cmds/lightclient/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {getNodeLogger} from "@lodestar/logger/node";
import {getBeaconConfigFromArgs} from "../../config/beaconParams.js";
import {getGlobalPaths} from "../../paths/global.js";
import {parseLoggerArgs} from "../../util/logger.js";
import {YargsError} from "../../util/errors.js";
import {GlobalArgs} from "../../options/index.js";
import {ILightClientArgs} from "./options.js";

Expand All @@ -19,11 +18,7 @@ export async function lightclientHandler(args: ILightClientArgs & GlobalArgs): P
parseLoggerArgs(args, {defaultLogFilepath: path.join(globalPaths.dataDir, "lightclient.log")}, config)
);

const {beaconApiUrl, checkpointRoot} = args;
if (!beaconApiUrl) throw new YargsError("must provide beaconApiUrl arg");
if (!checkpointRoot) throw new YargsError("must provide checkpointRoot arg");

const api = getClient({baseUrl: beaconApiUrl}, {config});
const api = getClient({baseUrl: args.beaconApiUrl}, {config});
const res = await api.beacon.getGenesis();
ApiError.assert(res, "Can not fetch genesis data");

Expand All @@ -34,7 +29,7 @@ export async function lightclientHandler(args: ILightClientArgs & GlobalArgs): P
genesisTime: Number(res.response.data.genesisTime),
genesisValidatorsRoot: res.response.data.genesisValidatorsRoot,
},
checkpointRoot: fromHexString(checkpointRoot),
checkpointRoot: fromHexString(args.checkpointRoot),
transport: new LightClientRestTransport(api),
});

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/lightclient/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand} from "../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {GlobalArgs} from "../../options/index.js";
import {ILightClientArgs, lightclientOptions} from "./options.js";
import {lightclientHandler} from "./handler.js";
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/cmds/lightclient/options.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import {CliCommandOptions} from "@lodestar/utils";
import {LogArgs, logOptions} from "../../options/logOptions.js";
import {CliCommandOptions} from "../../util/index.js";

export type ILightClientArgs = LogArgs & {
beaconApiUrl?: string;
checkpointRoot?: string;
beaconApiUrl: string;
checkpointRoot: string;
};

export const lightclientOptions: CliCommandOptions<ILightClientArgs> = {
...logOptions,
beaconApiUrl: {
description: "Url to a beacon node that support lightclient API",
type: "string",
demandOption: true,
},
checkpointRoot: {
description: "Checkpoint root hex string to sync the lightclient from, start with 0x",
type: "string",
demandOption: true,
},
};
24 changes: 10 additions & 14 deletions packages/cli/src/cmds/validator/blsToExecutionChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {DOMAIN_BLS_TO_EXECUTION_CHANGE, ForkName} from "@lodestar/params";
import {createBeaconConfig} from "@lodestar/config";
import {ssz, capella} from "@lodestar/types";
import {ApiError, getClient} from "@lodestar/api";
import {CliCommand} from "@lodestar/utils";

import {CliCommand, YargsError} from "../../util/index.js";
import {GlobalArgs} from "../../options/index.js";
import {getBeaconConfigFromArgs} from "../../config/index.js";
import {IValidatorCliArgs} from "./options.js";

/* eslint-disable no-console */

type BlsToExecutionChangeArgs = {
publicKey?: string;
fromBlsPrivkey?: string;
toExecutionAddress?: string;
publicKey: string;
fromBlsPrivkey: string;
toExecutionAddress: string;
};

export const blsToExecutionChange: CliCommand<BlsToExecutionChangeArgs, IValidatorCliArgs & GlobalArgs> = {
Expand All @@ -39,26 +39,22 @@ like to choose for BLS To Execution Change.",
publicKey: {
description: "Validator public key for which to set withdrawal address hence enabling withdrawals",
type: "string",
string: true,
demandOption: true,
},
fromBlsPrivkey: {
description: "Bls withdrawals private key to sign the message",
type: "string",
string: true,
demandOption: true,
},
toExecutionAddress: {
description: "Address to which the validator's balances will be set to be withdrawn.",
type: "string",
string: true,
demandOption: true,
},
},

handler: async (args) => {
const {publicKey, fromBlsPrivkey, toExecutionAddress} = args;
if (!publicKey) throw new YargsError("must provide publicKey arg");
if (!fromBlsPrivkey) throw new YargsError("must provide fromBlsPrivkey arg");
if (!toExecutionAddress) throw new YargsError("must provide toExecutionAddress arg");

const {publicKey} = args;
// Fetch genesisValidatorsRoot always from beacon node as anyway beacon node is needed for
// submitting the signed message
const {config: chainForkConfig} = getBeaconConfigFromArgs(args);
Expand All @@ -76,13 +72,13 @@ like to choose for BLS To Execution Change.",
throw new Error(`Validator pubkey ${publicKey} not found in state`);
}

const blsPrivkey = bls.SecretKey.fromBytes(fromHexString(fromBlsPrivkey));
const blsPrivkey = bls.SecretKey.fromBytes(fromHexString(args.fromBlsPrivkey));
const fromBlsPubkey = blsPrivkey.toPublicKey().toBytes(PointFormat.compressed);

const blsToExecutionChange: capella.BLSToExecutionChange = {
validatorIndex: stateValidator.index,
fromBlsPubkey,
toExecutionAddress: fromHexString(toExecutionAddress),
toExecutionAddress: fromHexString(args.toExecutionAddress),
};

const signatureFork = ForkName.phase0;
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/cmds/validator/import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs";
import {Keystore} from "@chainsafe/bls-keystore";
import {YargsError, CliCommand, getPubkeyHexFromKeystore} from "../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {YargsError, getPubkeyHexFromKeystore} from "../../util/index.js";
import {getBeaconConfigFromArgs} from "../../config/beaconParams.js";
import {GlobalArgs} from "../../options/index.js";
import {validatorOptions, IValidatorCliArgs} from "./options.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/validator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand} from "../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {GlobalArgs} from "../../options/index.js";
import {getAccountPaths} from "./paths.js";
import {slashingProtection} from "./slashingProtection/index.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/validator/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand} from "../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {getBeaconConfigFromArgs} from "../../config/beaconParams.js";
import {GlobalArgs} from "../../options/index.js";
import {IValidatorCliArgs} from "./options.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/cmds/validator/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {defaultOptions} from "@lodestar/validator";
import {CliCommandOptions} from "@lodestar/utils";
import {LogArgs, logOptions} from "../../options/logOptions.js";
import {ensure0xPrefix, CliCommandOptions} from "../../util/index.js";
import {ensure0xPrefix} from "../../util/index.js";
import {keymanagerRestApiServerOptsDefault} from "./keymanager/server.js";
import {defaultAccountPaths, defaultValidatorPaths} from "./paths.js";

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/cmds/validator/slashingProtection/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path from "node:path";
import {toHexString} from "@chainsafe/ssz";
import {InterchangeFormatVersion} from "@lodestar/validator";
import {getNodeLogger} from "@lodestar/logger/node";
import {CliCommand, YargsError, ensure0xPrefix, isValidatePubkeyHex, writeFile600Perm} from "../../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {YargsError, ensure0xPrefix, isValidatePubkeyHex, writeFile600Perm} from "../../../util/index.js";
import {parseLoggerArgs} from "../../../util/logger.js";
import {GlobalArgs} from "../../../options/index.js";
import {LogArgs} from "../../../options/logOptions.js";
Expand All @@ -13,7 +14,7 @@ import {getGenesisValidatorsRoot, getSlashingProtection} from "./utils.js";
import {ISlashingProtectionArgs} from "./options.js";

type ExportArgs = {
file?: string;
file: string;
pubkeys?: string[];
};

Expand Down Expand Up @@ -51,7 +52,6 @@ export const exportCmd: CliCommand<ExportArgs, ISlashingProtectionArgs & Account

handler: async (args) => {
const {file} = args;
if (!file) throw new YargsError("must provide file arg");

const {config, network} = getBeaconConfigFromArgs(args);
const validatorPaths = getValidatorPaths(args, network);
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/cmds/validator/slashingProtection/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "node:fs";
import path from "node:path";
import {Interchange} from "@lodestar/validator";
import {getNodeLogger} from "@lodestar/logger/node";
import {CliCommand, YargsError} from "../../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {parseLoggerArgs} from "../../../util/logger.js";
import {GlobalArgs} from "../../../options/index.js";
import {LogArgs} from "../../../options/logOptions.js";
Expand All @@ -13,7 +13,7 @@ import {getGenesisValidatorsRoot, getSlashingProtection} from "./utils.js";
import {ISlashingProtectionArgs} from "./options.js";

type ImportArgs = {
file?: string;
file: string;
};

export const importCmd: CliCommand<ImportArgs, ISlashingProtectionArgs & AccountValidatorArgs & GlobalArgs & LogArgs> =
Expand All @@ -39,7 +39,6 @@ export const importCmd: CliCommand<ImportArgs, ISlashingProtectionArgs & Account

handler: async (args) => {
const {file} = args;
if (!file) throw new YargsError("must provide file arg");

const {config, network} = getBeaconConfigFromArgs(args);
const validatorPaths = getValidatorPaths(args, network);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommand} from "../../../util/index.js";
import {CliCommand} from "@lodestar/utils";
import {AccountValidatorArgs} from "../options.js";
import {ISlashingProtectionArgs, slashingProtectionOptions} from "./options.js";
import {importCmd} from "./import.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliCommandOptions} from "../../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";
import {IValidatorCliArgs, validatorOptions} from "../options.js";

export type ISlashingProtectionArgs = Pick<IValidatorCliArgs, "beaconNodes"> & {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/cmds/validator/voluntaryExit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from "@lodestar/state-transition";
import {createBeaconConfig, BeaconConfig} from "@lodestar/config";
import {phase0, ssz, ValidatorIndex, Epoch} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {CliCommand, toHex} from "@lodestar/utils";
import {externalSignerPostSignature, SignableMessageType, Signer, SignerType} from "@lodestar/validator";
import {Api, ApiError, getClient} from "@lodestar/api";
import {CliCommand, ensure0xPrefix, YargsError, wrapError} from "../../util/index.js";
import {ensure0xPrefix, YargsError, wrapError} from "../../util/index.js";
import {GlobalArgs} from "../../options/index.js";
import {getBeaconConfigFromArgs} from "../../config/index.js";
import {IValidatorCliArgs} from "./options.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/options/beaconNodeOptions/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {defaultOptions, IBeaconNodeOptions, allNamespaces} from "@lodestar/beacon-node";
import {CliCommandOptions} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";

const enabledAll = "*";

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/options/beaconNodeOptions/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {defaultExecutionBuilderHttpOpts, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions, YargsError} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";
import {YargsError} from "../../util/index.js";

export type ExecutionBuilderArgs = {
builder: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/options/beaconNodeOptions/chain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "node:path";
import {defaultOptions, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";

export type ChainArgs = {
suggestedFeeRecipient: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/options/beaconNodeOptions/eth1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs";
import {defaultOptions, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions, extractJwtHexSecret} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";
import {extractJwtHexSecret} from "../../util/index.js";
import {ExecutionEngineArgs} from "./execution.js";

export type Eth1Args = {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/options/beaconNodeOptions/execution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs";
import {defaultExecutionEngineHttpOpts, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions, extractJwtHexSecret} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";
import {extractJwtHexSecret} from "../../util/index.js";

export type ExecutionEngineArgs = {
"execution.urls": string[];
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/options/beaconNodeOptions/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {defaultOptions, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";

export type MetricsArgs = {
metrics: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/options/beaconNodeOptions/monitoring.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {defaultOptions, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";

export type MonitoringArgs = {
"monitoring.endpoint"?: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/options/beaconNodeOptions/network.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {multiaddr} from "@multiformats/multiaddr";
import {ENR} from "@chainsafe/enr";
import {defaultOptions, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions, YargsError} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";
import {YargsError} from "../../util/index.js";

export const defaultListenAddress = "0.0.0.0";
export const defaultP2pPort = 9000;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/options/beaconNodeOptions/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {defaultOptions, IBeaconNodeOptions} from "@lodestar/beacon-node";
import {CliCommandOptions} from "../../util/index.js";
import {CliCommandOptions} from "@lodestar/utils";

export type SyncArgs = {
"sync.isSingleNode"?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/options/globalOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ACTIVE_PRESET} from "@lodestar/params";
import {CliCommandOptions} from "@lodestar/utils";
import {NetworkName, networkNames} from "../networks/index.js";
import {CliCommandOptions, readFile} from "../util/index.js";
import {readFile} from "../util/index.js";
import {paramsOptions, IParamsArgs} from "./paramsOptions.js";

type GlobalSingleArgs = {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/options/logOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {LogLevels} from "@lodestar/utils";
import {LogLevels, CliCommandOptions} from "@lodestar/utils";
import {LogLevel, logFormats} from "@lodestar/logger";
import {CliCommandOptions} from "../util/command.js";
import {LOG_FILE_DISABLE_KEYWORD} from "../util/logger.js";

export type LogArgs = {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/options/paramsOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ChainConfig, chainConfigTypes} from "@lodestar/config";
import {CliCommandOptions, CliOptionDefinition} from "@lodestar/utils";
import {IBeaconParamsUnparsed} from "../config/types.js";
import {ObjectKeys, CliCommandOptions, CliOptionDefinition} from "../util/index.js";
import {ObjectKeys} from "../util/index.js";

// No options are statically declared
// If an arbitrary key notation is used, it removes type safety on most of this CLI arg parsing code.
Expand Down
Loading

0 comments on commit 8959bda

Please sign in to comment.