|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "fs"; |
| 3 | +import { spawn, spawnSync } from "child_process"; |
| 4 | +import path from "path"; |
| 5 | +import { arch, platform } from "os"; |
| 6 | +import { version } from "./package.json"; |
| 7 | + |
| 8 | +const PACKAGE_VERSION = `ephemeral-validator ${version}`; |
| 9 | + |
| 10 | +function getBinaryVersion(location: string): [string | null, string | null] { |
| 11 | + const result = spawnSync(location, ["--version"]); |
| 12 | + const error: string | null = |
| 13 | + (result.error && result.error.toString()) || |
| 14 | + (result.stderr.length > 0 && result.stderr.toString().trim()) || |
| 15 | + null; |
| 16 | + return [error, result.stdout && result.stdout.toString().trim()]; |
| 17 | +} |
| 18 | + |
| 19 | +function getExePath(): string { |
| 20 | + let os: string = platform(); |
| 21 | + let extension = ""; |
| 22 | + if (["win32", "cygwin"].includes(os)) { |
| 23 | + os = "windows"; |
| 24 | + extension = ".exe"; |
| 25 | + } |
| 26 | + const binaryName = `@magicblock-labs/ephemeral-validator-${os}-${arch()}/bin/ephemeral-validator${extension}`; |
| 27 | + try { |
| 28 | + return require.resolve(binaryName); |
| 29 | + } catch (e) { |
| 30 | + throw new Error( |
| 31 | + `Couldn't find application binary inside node_modules for ${os}-${arch()}, expected location: ${binaryName}`, |
| 32 | + ); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function runEphemeralValidator(location: string): void { |
| 37 | + const args = process.argv.slice(2); |
| 38 | + const ephemeralValidator = spawn(location, args, { stdio: "inherit" }); |
| 39 | + ephemeralValidator.on("exit", (code: number | null, signal: NodeJS.Signals | null) => { |
| 40 | + process.on("exit", () => { |
| 41 | + if (signal) { |
| 42 | + process.kill(process.pid, signal); |
| 43 | + } else if (code !== null) { |
| 44 | + process.exit(code); |
| 45 | + } |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + process.on("SIGINT", () => { |
| 50 | + ephemeralValidator.kill("SIGINT"); |
| 51 | + ephemeralValidator.kill("SIGTERM"); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +function tryPackageEphemeralValidator(): boolean { |
| 56 | + try { |
| 57 | + const path = getExePath(); |
| 58 | + runEphemeralValidator(path); |
| 59 | + return true; |
| 60 | + } catch (e) { |
| 61 | + console.error( |
| 62 | + "Failed to run bolt from package:", |
| 63 | + e instanceof Error ? e.message : e, |
| 64 | + ); |
| 65 | + return false; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function trySystemEphemeralValidator(): void { |
| 70 | + const absolutePath = process.env.PATH?.split(path.delimiter) |
| 71 | + .filter((dir) => dir !== path.dirname(process.argv[1])) |
| 72 | + .find((dir) => { |
| 73 | + try { |
| 74 | + fs.accessSync(`${dir}/ephemeral-validator`, fs.constants.X_OK); |
| 75 | + return true; |
| 76 | + } catch { |
| 77 | + return false; |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + if (!absolutePath) { |
| 82 | + console.error( |
| 83 | + `Could not find globally installed ephemeral-validator, please install with cargo.`, |
| 84 | + ); |
| 85 | + process.exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + const absoluteBinaryPath = `${absolutePath}/ephemeral-validator`; |
| 89 | + const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath); |
| 90 | + |
| 91 | + if (error !== null) { |
| 92 | + console.error(`Failed to get version of global binary: ${error}`); |
| 93 | + return; |
| 94 | + } |
| 95 | + if (binaryVersion !== PACKAGE_VERSION) { |
| 96 | + console.error( |
| 97 | + `Globally installed ephemeral-validator version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`, |
| 98 | + ); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + runEphemeralValidator(absoluteBinaryPath); |
| 103 | +} |
| 104 | +tryPackageEphemeralValidator() || trySystemEphemeralValidator(); |
0 commit comments