Skip to content

Commit

Permalink
feat: add configurable ports and gossip-host to test-validator (#855)
Browse files Browse the repository at this point in the history
* Add configurable ports and gossip-host to test-validator

* Add customizable port option for start-prover
  • Loading branch information
sergeytimoshin authored Jun 23, 2024
1 parent 21aadca commit d5bf531
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
6 changes: 6 additions & 0 deletions cli/src/commands/start-prover/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class StartProver extends Command {
default: false,
char: "n",
}),
"prover-port": Flags.integer({
description: "Enable Light Prover server on this port.",
required: false,
default: 3001,
}),
};

async run() {
Expand All @@ -27,6 +32,7 @@ class StartProver extends Command {
loader.start();

await startProver(
flags["prover-port"],
!flags["skip-prove-compressed-accounts"],
!flags["skip-prove-new-addresses"],
);
Expand Down
29 changes: 28 additions & 1 deletion cli/src/commands/test-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,39 @@ class SetupCommand extends Command {
required: false,
exclusive: ["skip-indexer"],
}),
"rpc-port": Flags.integer({
description:
"Enable JSON RPC on this port, and the next port for the RPC websocket.",
required: false,
default: 8899,
}),
"indexer-port": Flags.integer({
description: "Enable Photon indexer on this port.",
required: false,
default: 8784,
exclusive: ["skip-indexer"],
}),
"prover-port": Flags.integer({
description: "Enable Light Prover server on this port.",
required: false,
default: 3001,
exclusive: ["skip-prover"],
}),
"limit-ledger-size": Flags.integer({
description: "Keep this amount of shreds in root slots.",
required: false,
default: 10000,
}),
"gossip-host": Flags.string({
description:
"Gossip DNS name or IP address for the validator to advertise in gossip.",
required: false,
default: "127.0.0.1",
}),
};

async run() {
const { flags } = await this.parse(SetupCommand);

const loader = new CustomLoader("Performing setup tasks...\n");
loader.start();
await initTestEnv({
Expand All @@ -69,6 +92,10 @@ class SetupCommand extends Command {
indexer: !flags["skip-indexer"],
limitLedgerSize: flags["limit-ledger-size"],
photonDatabaseUrl: flags["indexer-db-url"],
rpcPort: flags["rpc-port"],
gossipHost: flags["gossip-host"],
indexerPort: flags["indexer-port"],
proverPort: flags["prover-port"],
proveCompressedAccounts: flags["prove-compressed-accounts"],
proveNewAddresses: flags["prove-new-addresses"],
prover: !flags["skip-prover"],
Expand Down
40 changes: 33 additions & 7 deletions cli/src/utils/initTestEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
BASE_PATH,
LIGHT_ACCOUNT_COMPRESSION_TAG,
LIGHT_COMPRESSED_TOKEN_TAG,
LIGHT_MERKLE_TREE_PROGRAM_TAG,
LIGHT_PROTOCOL_PROGRAMS_DIR_ENV,
LIGHT_REGISTRY_TAG,
LIGHT_SYSTEM_PROGRAM_TAG,
Expand All @@ -28,6 +27,10 @@ export async function initTestEnv({
indexer = true,
prover = true,
forester = true,
rpcPort = 8899,
indexerPort = 8784,
proverPort = 3001,
gossipHost = "127.0.0.1",
proveCompressedAccounts = true,
proveNewAddresses = false,
checkPhotonVersion = true,
Expand All @@ -39,6 +42,10 @@ export async function initTestEnv({
indexer: boolean;
prover: boolean;
forester: boolean;
rpcPort?: number;
indexerPort?: number;
proverPort?: number;
gossipHost?: string;
proveCompressedAccounts?: boolean;
proveNewAddresses?: boolean;
checkPhotonVersion?: boolean;
Expand All @@ -61,23 +68,30 @@ export async function initTestEnv({
additionalPrograms,
skipSystemAccounts,
limitLedgerSize,
rpcPort,
gossipHost,
});
await waitForServers([{ port: 8899, path: "/health" }]);
await confirmServerStability("http://127.0.0.1:8899/health");
await waitForServers([{ port: rpcPort, path: "/health" }]);
await confirmServerStability(`http://127.0.0.1:${rpcPort}/health`);
await initAccounts();

if (indexer) {
const config = getConfig();
config.indexerUrl = "http://127.0.0.1:8784";
config.indexerUrl = `http://127.0.0.1:${indexerPort}`;
setConfig(config);
await startIndexer(checkPhotonVersion, photonDatabaseUrl);
await startIndexer(
`http://127.0.0.1:${rpcPort}`,
indexerPort,
checkPhotonVersion,
photonDatabaseUrl,
);
}

if (prover) {
const config = getConfig();
config.proverUrl = "http://127.0.0.1:3001";
config.proverUrl = `http://127.0.0.1:${proverPort}`;
setConfig(config);
await startProver(proveCompressedAccounts, proveNewAddresses);
await startProver(proverPort, proveCompressedAccounts, proveNewAddresses);
}

if (forester) {
Expand Down Expand Up @@ -155,11 +169,15 @@ export async function getSolanaArgs({
additionalPrograms,
skipSystemAccounts,
limitLedgerSize,
rpcPort,
gossipHost,
downloadBinaries = true,
}: {
additionalPrograms?: { address: string; path: string }[];
skipSystemAccounts?: boolean;
limitLedgerSize?: number;
rpcPort?: number;
gossipHost?: string;
downloadBinaries?: boolean;
}): Promise<Array<string>> {
type Program = { id: string; name?: string; tag?: string; path?: string };
Expand Down Expand Up @@ -201,6 +219,8 @@ export async function getSolanaArgs({
const solanaArgs = [
"--reset",
`--limit-ledger-size=${limitLedgerSize}`,
`--rpc-port=${rpcPort}`,
`--gossip-host=${gossipHost}`,
"--quiet",
];

Expand Down Expand Up @@ -235,16 +255,22 @@ export async function startTestValidator({
additionalPrograms,
skipSystemAccounts,
limitLedgerSize,
rpcPort,
gossipHost,
}: {
additionalPrograms?: { address: string; path: string }[];
skipSystemAccounts?: boolean;
limitLedgerSize?: number;
rpcPort?: number;
gossipHost?: string;
}) {
const command = "solana-test-validator";
const solanaArgs = await getSolanaArgs({
additionalPrograms,
skipSystemAccounts,
limitLedgerSize,
rpcPort,
gossipHost,
});

await killTestValidator();
Expand Down
4 changes: 2 additions & 2 deletions cli/src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export async function killProcess(processName: string) {
});
}

export async function killProcessByPort(port: string) {
await execute("lsof -t -i:3001 | while read line; do kill -9 $line; done");
export async function killProcessByPort(port: number) {
await execute(`lsof -t -i:${port} | while read line; do kill -9 $line; done`);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions cli/src/utils/processPhotonIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { exec } from "node:child_process";
import * as util from "node:util";

export async function startIndexer(
rpcUrl: string,
indexerPort: number,
checkPhotonVersion: boolean = true,
photonDatabaseUrl?: string,
) {
Expand All @@ -21,10 +23,17 @@ export async function startIndexer(
console.log("Starting indexer...");
let args: string[] = [];
if (photonDatabaseUrl) {
args = ["--db-url", photonDatabaseUrl];
args = [
"--db-url",
photonDatabaseUrl,
"--port",
indexerPort.toString(),
"--rpc-url",
rpcUrl,
];
}
spawnBinary(INDEXER_PROCESS_NAME, args);
await waitForServers([{ port: 8784, path: "/getIndexerHealth" }]);
await waitForServers([{ port: indexerPort, path: "/getIndexerHealth" }]);
console.log("Indexer started successfully!");
}
}
Expand Down
6 changes: 4 additions & 2 deletions cli/src/utils/processProverServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function killProver() {
}

export async function startProver(
proverPort: number,
proveCompressedAccounts: boolean,
proveNewAddresses: boolean,
) {
Expand All @@ -27,17 +28,18 @@ export async function startProver(
}
console.log("Kill existing prover process...");
await killProver();
await killProcessByPort("3001");
await killProcessByPort(proverPort);

const keysDir = path.join(__dirname, "../..", "bin", KEYS_DIR);
const args = ["start"];
args.push(`--inclusion=${proveCompressedAccounts ? "true" : "false"}`);
args.push(`--non-inclusion=${proveNewAddresses ? "true" : "false"}`);
args.push("--keys-dir", keysDir);
args.push("--prover-address", `0.0.0.0:${proverPort}`);

console.log("Starting prover...");
spawnBinary(getProverNameByArch(), args);
await waitForServers([{ port: 3001, path: "/" }]);
await waitForServers([{ port: proverPort, path: "/" }]);
console.log("Prover started successfully!");
}

Expand Down

0 comments on commit d5bf531

Please sign in to comment.