From 1e3e3f050dc45352d39ec23e01d59efb1ab2cada Mon Sep 17 00:00:00 2001 From: POPPIN-FUMI Date: Tue, 5 Nov 2024 08:58:25 +0100 Subject: [PATCH] add getSolanaCLI for agave/solana CLI --- packages/solv/src/cli/get/index.ts | 15 +++--- packages/solv/src/cli/get/monitorSolana.ts | 15 ++---- packages/solv/src/cli/relayer/index.ts | 3 +- packages/solv/src/cli/restart/index.ts | 15 ++---- .../cli/setup/genStartupValidatorScript.ts | 26 ---------- .../src/cli/switch/changeIdentityIncoming.ts | 8 +-- .../src/cli/switch/changeIdentityOutgoing.ts | 8 +-- packages/solv/src/cli/update/update.ts | 21 ++------ packages/solv/src/config/getSolanaCLI.ts | 24 +++++++++ packages/solv/src/config/versionConfig.ts | 6 +-- packages/solv/src/lib/migrate/migrateSetup.ts | 51 ------------------ .../solv/src/template/getStartupScript.ts | 52 ------------------- .../startJitoValidatorScript.ts | 3 +- .../startMainnetValidatorScript.ts | 4 +- 14 files changed, 56 insertions(+), 195 deletions(-) delete mode 100644 packages/solv/src/cli/setup/genStartupValidatorScript.ts create mode 100644 packages/solv/src/config/getSolanaCLI.ts delete mode 100644 packages/solv/src/lib/migrate/migrateSetup.ts delete mode 100644 packages/solv/src/template/getStartupScript.ts diff --git a/packages/solv/src/cli/get/index.ts b/packages/solv/src/cli/get/index.ts index 6afbff47..30a61c83 100644 --- a/packages/solv/src/cli/get/index.ts +++ b/packages/solv/src/cli/get/index.ts @@ -9,7 +9,12 @@ import { AGAVE_VALIDATOR, SOLANA_VALIDATOR } from '@/config/constants' import chalk from 'chalk' import { DefaultConfigType } from '@/config/types' import { Network, NodeType } from '@/config/enums' -import { VERSION_MAINNET, VERSION_SOLANA_RPC, VERSION_TESTNET } from '@/config/versionConfig' +import { + VERSION_MAINNET, + VERSION_SOLANA_RPC, + VERSION_TESTNET, +} from '@/config/versionConfig' +import getSolanaCLI from '@/config/getSolanaCLI' export const getCommands = (config: DefaultConfigType) => { const isTest = config.NETWORK === Network.TESTNET @@ -64,11 +69,7 @@ export const getCommands = (config: DefaultConfigType) => { 'Snapshot Path', '/mnt/ledger/snapshot', ) - .option( - '-v, --version ', - 'Specific Version Node', - version, - ) + .option('-v, --version ', 'Specific Version Node', version) .description(`Download the latest snapshot`) .action( (options: { @@ -89,7 +90,7 @@ export const getCommands = (config: DefaultConfigType) => { .command('contact') .description('Show Validator Contact Information') .action(() => { - const solanaValidatorClient = isTest ? AGAVE_VALIDATOR : SOLANA_VALIDATOR + const solanaValidatorClient = getSolanaCLI() const cmd = `${solanaValidatorClient} --ledger /mnt/ledger/ contact-info` spawnSync(cmd, { shell: true, stdio: 'inherit' }) }) diff --git a/packages/solv/src/cli/get/monitorSolana.ts b/packages/solv/src/cli/get/monitorSolana.ts index 27d09522..349c6407 100644 --- a/packages/solv/src/cli/get/monitorSolana.ts +++ b/packages/solv/src/cli/get/monitorSolana.ts @@ -1,19 +1,10 @@ -import { - AGAVE_VALIDATOR, - LEDGER_PATH, - SOLANA_VALIDATOR, -} from '@/config/constants' -import { Network, NodeType } from '@/config/enums' +import { LEDGER_PATH } from '@/config/constants' +import getSolanaCLI from '@/config/getSolanaCLI' import { DefaultConfigType } from '@/config/types' import { spawnSync } from 'node:child_process' export const monitorSolana = (config: DefaultConfigType) => { - const isTestnet = config.NETWORK === Network.TESTNET - const isRPC = config.NODE_TYPE === NodeType.RPC - let solanaValidatorClient = isTestnet ? AGAVE_VALIDATOR : SOLANA_VALIDATOR - if (isRPC) { - solanaValidatorClient = AGAVE_VALIDATOR - } + const solanaValidatorClient = getSolanaCLI() const cmd = `${solanaValidatorClient} --ledger ${LEDGER_PATH} monitor` spawnSync(cmd, { shell: true, stdio: 'inherit' }) } diff --git a/packages/solv/src/cli/relayer/index.ts b/packages/solv/src/cli/relayer/index.ts index 0271cbc6..876e7360 100644 --- a/packages/solv/src/cli/relayer/index.ts +++ b/packages/solv/src/cli/relayer/index.ts @@ -8,6 +8,7 @@ import chalk from 'chalk' import { LEDGER_PATH } from '@/config/constants' import { spawnSync } from 'child_process' import { relayerEnable } from './relayerEnable' +import getSolanaCLI from '@/config/getSolanaCLI' export const relayerCommands = () => { const relayer = program @@ -66,7 +67,7 @@ export const relayerCommands = () => { console.log(chalk.red('Please provide a URL')) return } - const cmd = `solana-validator --ledger ${LEDGER_PATH} set-relayer-config --relayer-url ${options.url}` + const cmd = `${getSolanaCLI()} --ledger ${LEDGER_PATH} set-relayer-config --relayer-url ${options.url}` console.log(chalk.white('Setting Relayer URL ...')) spawnSync(cmd, { shell: true, stdio: 'inherit' }) console.log(chalk.green('🟢 Relayer URL Set')) diff --git a/packages/solv/src/cli/restart/index.ts b/packages/solv/src/cli/restart/index.ts index edd589a8..3a4d8d64 100644 --- a/packages/solv/src/cli/restart/index.ts +++ b/packages/solv/src/cli/restart/index.ts @@ -1,21 +1,12 @@ import { program } from '@/index' import { spawnSync } from 'node:child_process' import chalk from 'chalk' -import { - AGAVE_VALIDATOR, - LEDGER_PATH, - SOLANA_VALIDATOR, -} from '@/config/constants' +import { LEDGER_PATH } from '@/config/constants' import { DefaultConfigType } from '@/config/types' -import { Network, NodeType } from '@/config/enums' +import getSolanaCLI from '@/config/getSolanaCLI' export const restartCommand = (config: DefaultConfigType) => { - const isTestnet = config.NETWORK === Network.TESTNET - const isRPC = config.NODE_TYPE === NodeType.RPC - let solanaValidatorClient = isTestnet ? AGAVE_VALIDATOR : SOLANA_VALIDATOR - if (isRPC) { - solanaValidatorClient = AGAVE_VALIDATOR - } + let solanaValidatorClient = getSolanaCLI() program .command('restart') .description('Restart Solana Validator') diff --git a/packages/solv/src/cli/setup/genStartupValidatorScript.ts b/packages/solv/src/cli/setup/genStartupValidatorScript.ts deleted file mode 100644 index 1b3832f9..00000000 --- a/packages/solv/src/cli/setup/genStartupValidatorScript.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { SOLV_TYPES } from '@/config/config' -import { STARTUP_SCRIPT } from '@/config/constants' -import { getStartupScript } from '@/template/getStartupScript' -import { spawnSync } from 'node:child_process' -import { writeFile } from 'fs/promises' - -export const genStartupValidatorScript = async ( - fetchSnapshot = false, - solvType = SOLV_TYPES.TESTNET_VALIDATOR, - isJitoMev = false, - isJitoRPC = false, -) => { - try { - const body = await getStartupScript( - fetchSnapshot, - solvType, - isJitoMev, - isJitoRPC, - ) - await writeFile(STARTUP_SCRIPT, body, 'utf-8') - const cmd = `sudo chmod +x ${STARTUP_SCRIPT}` - spawnSync(cmd, { shell: true, stdio: 'inherit' }) - } catch (error) { - throw new Error(`genStartupValidatorScript: ${error}`) - } -} diff --git a/packages/solv/src/cli/switch/changeIdentityIncoming.ts b/packages/solv/src/cli/switch/changeIdentityIncoming.ts index 629ba378..3ca62fc8 100644 --- a/packages/solv/src/cli/switch/changeIdentityIncoming.ts +++ b/packages/solv/src/cli/switch/changeIdentityIncoming.ts @@ -1,10 +1,8 @@ import { - AGAVE_VALIDATOR, IDENTITY_KEY, IDENTITY_KEY_PATH, LEDGER_PATH, MAINNET_VALIDATOR_KEY_PATH, - SOLANA_VALIDATOR, SOLV_HOME, TESTNET_VALIDATOR_KEY_PATH, UNSTAKED_KEY, @@ -16,6 +14,7 @@ import checkValidatorKey from './checkValidatorKey' import { updateDefaultConfig } from '@/config/updateDefaultConfig' import { DefaultConfigType } from '@/config/types' import { Network, NodeType } from '@/config/enums' +import getSolanaCLI from '@/config/getSolanaCLI' const unstakedKeyPath = join(SOLV_HOME, UNSTAKED_KEY) const identityKeyPath = join(SOLV_HOME, IDENTITY_KEY) @@ -33,10 +32,7 @@ export const changeIdentityIncoming = async ( if (isRPC) { validatorKeyPath = TESTNET_VALIDATOR_KEY_PATH } - let solanaClient = isTestnet ? AGAVE_VALIDATOR : SOLANA_VALIDATOR - if (isRPC) { - solanaClient = AGAVE_VALIDATOR - } + const solanaClient = getSolanaCLI() const isKeyOkay = checkValidatorKey(validatorKeyPath, ip) if (!isKeyOkay) { diff --git a/packages/solv/src/cli/switch/changeIdentityOutgoing.ts b/packages/solv/src/cli/switch/changeIdentityOutgoing.ts index cda96a8b..904ae84c 100644 --- a/packages/solv/src/cli/switch/changeIdentityOutgoing.ts +++ b/packages/solv/src/cli/switch/changeIdentityOutgoing.ts @@ -1,10 +1,8 @@ import { - AGAVE_VALIDATOR, IDENTITY_KEY, IDENTITY_KEY_PATH, LEDGER_PATH, MAINNET_VALIDATOR_KEY_PATH, - SOLANA_VALIDATOR, SOLV_HOME, TESTNET_VALIDATOR_KEY_PATH, UNSTAKED_KEY, @@ -16,6 +14,7 @@ import checkValidatorKey from './checkValidatorKey' import { updateDefaultConfig } from '@/config/updateDefaultConfig' import { DefaultConfigType } from '@/config/types' import { Network, NodeType } from '@/config/enums' +import getSolanaCLI from '@/config/getSolanaCLI' const unstakedKeyPath = join(SOLV_HOME, UNSTAKED_KEY) const identityKeyPath = join(SOLV_HOME, IDENTITY_KEY) @@ -34,10 +33,7 @@ export const changeIdentityOutgoing = async ( if (isRPC) { validatorKeyPath = TESTNET_VALIDATOR_KEY_PATH } - let solanaClient = isTestnet ? AGAVE_VALIDATOR : SOLANA_VALIDATOR - if (isRPC) { - solanaClient = AGAVE_VALIDATOR - } + let solanaClient = getSolanaCLI() const isKeyOkay = checkValidatorKey(validatorKeyPath, ip) if (!isKeyOkay) { diff --git a/packages/solv/src/cli/update/update.ts b/packages/solv/src/cli/update/update.ts index c27ca121..f29e9669 100644 --- a/packages/solv/src/cli/update/update.ts +++ b/packages/solv/src/cli/update/update.ts @@ -1,18 +1,11 @@ -import { - AGAVE_VALIDATOR, - LEDGER_PATH, - SOLANA_VALIDATOR, -} from '@/config/constants' +import { AGAVE_VALIDATOR, LEDGER_PATH } from '@/config/constants' import { spawnSync } from 'child_process' import installAgave from '../install/installAgave' -import readConfig from '@/config/readConfig' -import { Network, NodeType } from '@/config/enums' +import getSolanaCLI from '@/config/getSolanaCLI' export const updateVersion = async (version: string) => { - const config = await readConfig() - const isTestnet = config.NETWORK === Network.TESTNET - const isRPC = config.NODE_TYPE === NodeType.RPC - if (isTestnet || isRPC) { + const solanaCLI = getSolanaCLI() + if (solanaCLI === AGAVE_VALIDATOR) { installAgave(version) return } @@ -26,11 +19,7 @@ export const monitorUpdate = async ( maxDelinquentStake: number, noMonitor = false, ) => { - const config = await readConfig() - const isTestnet = config.NETWORK === Network.TESTNET - const isRPC = config.NODE_TYPE === NodeType.RPC - let solanaValidatorClient = - isRPC || isTestnet ? AGAVE_VALIDATOR : SOLANA_VALIDATOR + const solanaValidatorClient = getSolanaCLI() let cmd = `${solanaValidatorClient} --ledger ${LEDGER_PATH} exit --max-delinquent-stake ${maxDelinquentStake} --monitor` if (noMonitor) { cmd = `${solanaValidatorClient} --ledger ${LEDGER_PATH} exit --max-delinquent-stake ${maxDelinquentStake}` diff --git a/packages/solv/src/config/getSolanaCLI.ts b/packages/solv/src/config/getSolanaCLI.ts new file mode 100644 index 00000000..716d6d95 --- /dev/null +++ b/packages/solv/src/config/getSolanaCLI.ts @@ -0,0 +1,24 @@ +import getSolanaVersion from '@/cli/epochTimer/getSolanaVersion' + +const SOLANA_CLI = 'solana-validator' +const AGAVE_CLI = 'agave-validator' + +export type SolanaCLI = typeof SOLANA_CLI | typeof AGAVE_CLI + +const getSolanaCLI = (): SolanaCLI => { + try { + const solanaVersion = getSolanaVersion() + console.log(solanaVersion) + // convert solanaVersion to number. e.g. 1.2.0 to 120 + const hasVersion20 = solanaVersion.includes('2.0') + if (hasVersion20) { + return AGAVE_CLI + } + return SOLANA_CLI + } catch (error) { + console.error(error) + return SOLANA_CLI + } +} + +export default getSolanaCLI diff --git a/packages/solv/src/config/versionConfig.ts b/packages/solv/src/config/versionConfig.ts index 08c6b7d1..242e2f11 100644 --- a/packages/solv/src/config/versionConfig.ts +++ b/packages/solv/src/config/versionConfig.ts @@ -1,9 +1,9 @@ export const VERSION_TESTNET = '2.0.14' -export const VERSION_MAINNET = '1.18.25' +export const VERSION_MAINNET = '2.0.14' export const VERSION_JITO_TESTNET = '2.0.14' -export const VERSION_JITO_MAINNET = '1.18.26' +export const VERSION_JITO_MAINNET = '2.0.14' export const VERSION_JITO_RPC = '2.0.14' -export const VERSION_SOLANA_RPC = '1.18.26' +export const VERSION_SOLANA_RPC = '2.0.14' export const DELINQUENT_STAKE_TESTNET = 10 export const DELINQUENT_STAKE_MAINNET = 5 export const COMMISSION = 5 diff --git a/packages/solv/src/lib/migrate/migrateSetup.ts b/packages/solv/src/lib/migrate/migrateSetup.ts deleted file mode 100644 index 43b1a69a..00000000 --- a/packages/solv/src/lib/migrate/migrateSetup.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { spawnSync } from 'child_process' -import { umount } from '@/cli/check/mt/umount' -import getPreferredDisk, { - GetPreferredDisksResult, -} from '@/cli/check/mt/getLargestDisk' -import { startSolana } from '@/cli/start/startSolana' -import { DISK_TYPES, SOLV_TYPES } from '@/config/config' -import { ensureFstabEntries } from '@/cli/check/ensureMountAndFiles' -import { updateSolvConfig } from '@/lib/updateSolvConfig' -import { setupDirs } from '@/cli/setup/mkdirs' -import { formatDisk } from '@/cli/setup/formatDisk' -import { setupPermissions } from '@/cli/setup/userPermissions' -import { genStartupValidatorScript } from '@/cli/setup/genStartupValidatorScript' -import { makeServices } from '@/cli/setup/makeServices' - -export const migrateSetup = async () => { - try { - const sType = SOLV_TYPES.TESTNET_VALIDATOR - - console.log('Migrating to Solv3...') - - const disks: GetPreferredDisksResult = getPreferredDisk() - const mountPoint = disks.disks[0].mountpoint - setupDirs() - console.log('Setting up SINGLE DISK...') - updateSolvConfig({ - DISK_TYPES: DISK_TYPES.SINGLE, - SOLV_TYPE: sType, - }) - const isUmounted = umount(mountPoint) - const fileSystem = '/dev/' + disks.disks[0].name - formatDisk(fileSystem) - ensureFstabEntries(fileSystem) - setupPermissions() - await genStartupValidatorScript(true) - makeServices() - const cmds = [ - 'sudo systemctl daemon-reload', - 'sudo systemctl enable solv', - 'sudo systemctl restart logrotate', - ] - for (const line of cmds) { - spawnSync(line, { shell: true, stdio: 'inherit' }) - } - startSolana() - updateSolvConfig({ IS_SETUP: true }) - return true - } catch (error) { - throw new Error(`setup Error: ${error}`) - } -} diff --git a/packages/solv/src/template/getStartupScript.ts b/packages/solv/src/template/getStartupScript.ts deleted file mode 100644 index 04699fb0..00000000 --- a/packages/solv/src/template/getStartupScript.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { SOLV_TYPES } from '@/config/config' -import { startTestnetAgaveValidatorScript } from '@/template/startupScripts/startTestnetAgaveValidatorScript' -import { startMainnetValidatorScript } from './startupScripts/startMainnetValidatorScript' -import { startRPCNodeScript } from './startupScripts/startRPCNodeScript' -import { startJitoValidatorScript } from './startupScripts/startJitoValidatorScript' -import { readOrCreateJitoConfig } from '@/lib/readOrCreateJitoConfig' -import { startJitoRPCScript } from './startupScripts/startJitoRPCScript' - -export const getStartupScript = async ( - fetchSnapshot = false, - solvTypes = SOLV_TYPES.TESTNET_VALIDATOR, - isJitoMev = false, - isJitoRPC = false, -) => { - let script = '' - if (isJitoMev) { - const jitoConfig = await readOrCreateJitoConfig() - if (isJitoRPC) { - return startJitoRPCScript( - jitoConfig.commissionBps, - jitoConfig.relayerUrl, - jitoConfig.blockEngineUrl, - jitoConfig.shredReceiverAddr, - ) - } - - return startJitoValidatorScript( - jitoConfig.commissionBps, - jitoConfig.relayerUrl, - jitoConfig.blockEngineUrl, - jitoConfig.shredReceiverAddr, - ) - } - switch (solvTypes) { - case SOLV_TYPES.TESTNET_VALIDATOR: - script = startTestnetAgaveValidatorScript() - break - case SOLV_TYPES.MAINNET_VALIDATOR: - script = startMainnetValidatorScript() - break - case SOLV_TYPES.RPC_NODE: - script = startRPCNodeScript() - break - default: - script = startTestnetAgaveValidatorScript() - break - } - if (!fetchSnapshot) { - return `${script}--use-snapshot-archives-at-startup when-newest \\\n--no-genesis-fetch` - } - return script -} diff --git a/packages/solv/src/template/startupScripts/startJitoValidatorScript.ts b/packages/solv/src/template/startupScripts/startJitoValidatorScript.ts index de43636c..51130b77 100644 --- a/packages/solv/src/template/startupScripts/startJitoValidatorScript.ts +++ b/packages/solv/src/template/startupScripts/startJitoValidatorScript.ts @@ -12,9 +12,10 @@ export const startJitoValidatorScript = ( relayerUrl: string, blockEngineUrl: string, shredReceiverAddr: string, + solanaCLI = 'agave-validator', ) => { const script = `#!/bin/bash -exec solana-validator \\ +exec ${solanaCLI} \\ --identity ${IDENTITY_KEY_PATH} \\ --vote-account ${MAINNET_VALIDATOR_VOTE_KEY_PATH} \\ --authorized-voter ${MAINNET_VALIDATOR_KEY_PATH} \\ diff --git a/packages/solv/src/template/startupScripts/startMainnetValidatorScript.ts b/packages/solv/src/template/startupScripts/startMainnetValidatorScript.ts index e212a5da..327ab77c 100644 --- a/packages/solv/src/template/startupScripts/startMainnetValidatorScript.ts +++ b/packages/solv/src/template/startupScripts/startMainnetValidatorScript.ts @@ -7,9 +7,9 @@ import { MAINNET_VALIDATOR_VOTE_KEY_PATH, } from '@/config/constants' -export const startMainnetValidatorScript = () => { +export const startMainnetValidatorScript = (solanaCLI = 'agave-validator') => { const script = `#!/bin/bash -exec solana-validator \\ +exec ${solanaCLI} \\ --identity ${IDENTITY_KEY_PATH} \\ --vote-account ${MAINNET_VALIDATOR_VOTE_KEY_PATH} \\ --authorized-voter ${MAINNET_VALIDATOR_KEY_PATH} \\