Skip to content

Commit

Permalink
Add lightclient cmd (#3937)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths authored Apr 25, 2022
1 parent 97f6012 commit 67fb374
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@chainsafe/blst": "^0.2.4",
"@chainsafe/discv5": "^0.6.7",
"@chainsafe/lodestar": "^0.36.0",
"@chainsafe/lodestar-light-client": "^0.36.0",
"@chainsafe/lodestar-api": "^0.36.0",
"@chainsafe/lodestar-beacon-state-transition": "^0.36.0",
"@chainsafe/lodestar-config": "^0.36.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/cmds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {beacon} from "./beacon";
import {dev} from "./dev";
import {init} from "./init";
import {validator} from "./validator";
import {lightclient} from "./lightclient";

export const cmds: Required<ICliCommand<IGlobalArgs, Record<never, never>>>["subcommands"] = [
beacon,
validator,
lightclient,
account,
init,
dev,
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/src/cmds/lightclient/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {getClient} from "@chainsafe/lodestar-api";
import {Lightclient} from "@chainsafe/lodestar-light-client";
import {fromHexString} from "@chainsafe/ssz";
import {getBeaconConfigFromArgs} from "../../config/beaconParams";
import {IGlobalArgs} from "../../options";
import {getCliLogger, initBLS} from "../../util";
import {getBeaconPaths} from "../beacon/paths";
import {ILightClientArgs} from "./options";

export async function lightclientHandler(args: ILightClientArgs & IGlobalArgs): Promise<void> {
await initBLS();

const config = getBeaconConfigFromArgs(args);
const beaconPaths = getBeaconPaths(args);
const logger = getCliLogger(args, beaconPaths, config);
const {beaconApiUrl, checkpointRoot} = args;
const api = getClient({baseUrl: beaconApiUrl}, {config});
const {data: genesisData} = await api.beacon.getGenesis();

const client = await Lightclient.initializeFromCheckpointRoot({
config,
logger,
beaconApiUrl,
genesisData: {
genesisTime: Number(genesisData.genesisTime),
genesisValidatorsRoot: genesisData.genesisValidatorsRoot,
},
checkpointRoot: fromHexString(checkpointRoot),
});

client.start();
}
17 changes: 17 additions & 0 deletions packages/cli/src/cmds/lightclient/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {ICliCommand} from "../../util";
import {IGlobalArgs} from "../../options";
import {ILightClientArgs, lightclientOptions} from "./options";
import {lightclientHandler} from "./handler";

export const lightclient: ICliCommand<ILightClientArgs, IGlobalArgs> = {
command: "lightclient",
describe: "Run lightclient",
examples: [
{
command: "lightclient --network prater",
description: "Run lightclient with prater network",
},
],
options: lightclientOptions,
handler: lightclientHandler,
};
24 changes: 24 additions & 0 deletions packages/cli/src/cmds/lightclient/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {ICliCommandOptions, ILogArgs} from "../../util";
import {beaconPathsOptions, logOptions} from "../beacon/options";
import {IBeaconPaths} from "../beacon/paths";

export type ILightClientArgs = ILogArgs & {
logFile: IBeaconPaths["logFile"];
beaconApiUrl: string;
checkpointRoot: string;
};

export const lightclientOptions: ICliCommandOptions<ILightClientArgs> = {
...logOptions,
logFile: beaconPathsOptions.logFile,
beaconApiUrl: {
description: "Url to a beacon node that support lightclient API",
type: "string",
require: true,
},
checkpointRoot: {
description: "Checkpoint root hex string to sync the lightclient from, start with 0x",
type: "string",
require: true,
},
};

0 comments on commit 67fb374

Please sign in to comment.