import { AbortController } from "@chainsafe/abort-controller" ; import { getClient } from "@chainsafe/lodestar-api" ; import { Validator , SlashingProtection } from "@chainsafe/lodestar-validator" ; import { LevelDbController } from "@chainsafe/lodestar-db" ; import { getBeaconConfigFromArgs } from "../../config" ; import { IGlobalArgs } from "../../options" ; import { YargsError , getDefaultGraffiti , initBLS , mkdir , getCliLogger } from "../../util" ; import { onGracefulShutdown } from "../../util" ; import { getBeaconPaths } from "../beacon/paths" ; import { getValidatorPaths } from "./paths" ; import { IValidatorCliArgs } from "./options" ; import { getSecretKeys } from "./keys" ; import { getVersion } from "../../util/version" ; /**
 * Runs a validator client.
 */ export async function validatorHandler ( args ) { await initBLS ( ) ; const graffiti = args . graffiti || getDefaultGraffiti ( ) ; const validatorPaths = getValidatorPaths ( args ) ; const beaconPaths = getBeaconPaths ( args ) ; const config = getBeaconConfigFromArgs ( args ) ; const logger = getCliLogger ( args , beaconPaths , config ) ; const version = getVersion ( ) ; logger . info ( "Lodestar" , { version : version , network : args . network } ) ; const { secretKeys , unlockSecretKeys : unlockSecretKeys } = await getSecretKeys ( args ) ; if ( secretKeys . length === 0 ) { throw new YargsError ( "No validator keystores found" ) ; } logger . info ( `Decrypted ${ secretKeys . length } validator keystores` ) ; for ( const secretKey of secretKeys ) { logger . info ( secretKey . toPublicKey ( ) . toHex ( ) ) ; } const dbPath = validatorPaths . validatorsDbDir ; mkdir ( dbPath ) ; const onGracefulShutdownCbs = [ ] ; onGracefulShutdown ( async ( ) => { for ( const cb of onGracefulShutdownCbs ) await cb ( ) ; unlockSecretKeys ?. ( ) ; } , logger . info . bind ( logger ) ) ; const controller = new AbortController ( ) ; onGracefulShutdownCbs . push ( async ( ) => controller . abort ( ) ) ; const api = getClient ( config , { baseUrl : args . server } ) ; const dbOps = { config : config , controller : new LevelDbController ( { name : dbPath } , { logger } ) , } ; const slashingProtection = new SlashingProtection ( dbOps ) ; const validator = await Validator . initializeFromBeaconNode ( { dbOps , slashingProtection , api , logger , secretKeys , graffiti } , controller . signal ) ; onGracefulShutdownCbs . push ( async ( ) => await validator . stop ( ) ) ; await validator . start ( ) ; }