solforge 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ import chalk from "chalk";
2
+ import { runCommand, checkSolanaTools } from "../utils/shell.js";
3
+ import { configManager } from "../config/manager.js";
4
+ import { processRegistry } from "../services/process-registry.js";
5
+
6
+ export async function statusCommand(): Promise<void> {
7
+ console.log(chalk.blue("šŸ“Š Checking system status...\n"));
8
+
9
+ // Check Solana CLI tools
10
+ console.log(chalk.cyan("šŸ”§ Solana CLI Tools:"));
11
+ const tools = await checkSolanaTools();
12
+
13
+ console.log(
14
+ ` ${tools.solana ? "āœ…" : "āŒ"} solana CLI: ${
15
+ tools.solana ? "Available" : "Not found"
16
+ }`
17
+ );
18
+ console.log(
19
+ ` ${tools.splToken ? "āœ…" : "āŒ"} spl-token CLI: ${
20
+ tools.splToken ? "Available" : "Not found"
21
+ }`
22
+ );
23
+
24
+ if (!tools.solana || !tools.splToken) {
25
+ console.log(chalk.yellow("\nšŸ’” Install Solana CLI tools:"));
26
+ console.log(
27
+ chalk.gray(
28
+ ' sh -c "$(curl -sSfL https://release.solana.com/v1.18.4/install)"'
29
+ )
30
+ );
31
+ console.log();
32
+ }
33
+
34
+ // Check running validators
35
+ console.log(chalk.cyan("\nšŸ—ļø Validator Status:"));
36
+
37
+ // Clean up dead processes first
38
+ await processRegistry.cleanup();
39
+
40
+ const validators = processRegistry.getRunning();
41
+
42
+ if (validators.length === 0) {
43
+ console.log(` āŒ No validators running`);
44
+ console.log(` šŸ’” Run 'solforge start' to launch a validator`);
45
+ } else {
46
+ console.log(
47
+ ` āœ… ${validators.length} validator${
48
+ validators.length > 1 ? "s" : ""
49
+ } running`
50
+ );
51
+
52
+ for (const validator of validators) {
53
+ const isRunning = await processRegistry.isProcessRunning(validator.pid);
54
+ if (isRunning) {
55
+ console.log(` šŸ”¹ ${validator.name} (${validator.id}):`);
56
+ console.log(` 🌐 RPC: ${validator.rpcUrl}`);
57
+ console.log(` šŸ’° Faucet: ${validator.faucetUrl}`);
58
+ console.log(` šŸ†” PID: ${validator.pid}`);
59
+
60
+ // Get current slot for this validator
61
+ try {
62
+ const slotResult = await runCommand(
63
+ "solana",
64
+ ["slot", "--url", validator.rpcUrl, "--output", "json"],
65
+ { silent: true, jsonOutput: true }
66
+ );
67
+
68
+ if (slotResult.success && typeof slotResult.stdout === "object") {
69
+ console.log(` šŸ“Š Current slot: ${slotResult.stdout}`);
70
+ }
71
+ } catch {
72
+ // Ignore slot check errors
73
+ }
74
+ } else {
75
+ processRegistry.updateStatus(validator.id, "stopped");
76
+ console.log(
77
+ ` āš ļø ${validator.name} (${validator.id}): Process stopped`
78
+ );
79
+ }
80
+ }
81
+
82
+ console.log(` šŸ’” Run 'solforge list' for detailed validator information`);
83
+ }
84
+
85
+ // Check config file
86
+ console.log(chalk.cyan("\nšŸ“„ Configuration:"));
87
+ try {
88
+ const config = configManager.getConfig();
89
+ console.log(` āœ… Config loaded: ${configManager.getConfigPath()}`);
90
+ console.log(` šŸ“ Project: ${config.name}`);
91
+ console.log(` šŸŖ™ Tokens: ${config.tokens.length}`);
92
+ console.log(` šŸ“¦ Programs: ${config.programs.length}`);
93
+ } catch (error) {
94
+ console.log(` āŒ No valid configuration found`);
95
+ console.log(` šŸ’” Run 'solforge init' to create one`);
96
+ }
97
+
98
+ console.log();
99
+ }
@@ -0,0 +1,389 @@
1
+ import chalk from "chalk";
2
+ import { select } from "@inquirer/prompts";
3
+ import { processRegistry } from "../services/process-registry.js";
4
+ import type { RunningValidator } from "../services/process-registry.js";
5
+
6
+ export async function stopCommand(
7
+ validatorId?: string,
8
+ options: { all?: boolean; kill?: boolean } = {}
9
+ ): Promise<void> {
10
+ console.log(chalk.blue("šŸ›‘ Stopping validator(s)...\n"));
11
+
12
+ // Clean up dead processes first
13
+ await processRegistry.cleanup();
14
+
15
+ const validators = processRegistry.getRunning();
16
+
17
+ if (validators.length === 0) {
18
+ console.log(chalk.yellow("āš ļø No running validators found"));
19
+ return;
20
+ }
21
+
22
+ let validatorsToStop: RunningValidator[] = [];
23
+
24
+ if (options.all) {
25
+ // Stop all validators
26
+ validatorsToStop = validators;
27
+ console.log(
28
+ chalk.cyan(`šŸ”„ Stopping all ${validators.length} validator(s)...`)
29
+ );
30
+ } else if (validatorId) {
31
+ // Stop specific validator
32
+ const validator = processRegistry.getById(validatorId);
33
+ if (!validator) {
34
+ console.error(
35
+ chalk.red(`āŒ Validator with ID '${validatorId}' not found`)
36
+ );
37
+ console.log(
38
+ chalk.gray("šŸ’” Use `solforge list` to see running validators")
39
+ );
40
+ return;
41
+ }
42
+ validatorsToStop = [validator];
43
+ console.log(
44
+ chalk.cyan(
45
+ `šŸ”„ Stopping validator '${validator.name}' (${validatorId})...`
46
+ )
47
+ );
48
+ } else {
49
+ // No specific validator specified, show available options
50
+ console.log(chalk.yellow("āš ļø Please specify which validator to stop:"));
51
+ console.log(
52
+ chalk.gray("šŸ’” Use `solforge stop <id>` to stop a specific validator")
53
+ );
54
+ console.log(
55
+ chalk.gray("šŸ’” Use `solforge stop --all` to stop all validators")
56
+ );
57
+ console.log(
58
+ chalk.gray("šŸ’” Use `solforge list` to see running validators")
59
+ );
60
+ return;
61
+ }
62
+
63
+ // Stop each validator
64
+ let stoppedCount = 0;
65
+ let errorCount = 0;
66
+
67
+ for (const validator of validatorsToStop) {
68
+ try {
69
+ const result = await stopValidator(validator, options.kill);
70
+ if (result.success) {
71
+ console.log(
72
+ chalk.green(`āœ… Stopped ${validator.name} (${validator.id})`)
73
+ );
74
+ stoppedCount++;
75
+ } else {
76
+ console.error(
77
+ chalk.red(
78
+ `āŒ Failed to stop ${validator.name} (${validator.id}): ${result.error}`
79
+ )
80
+ );
81
+ errorCount++;
82
+ }
83
+ } catch (error) {
84
+ console.error(
85
+ chalk.red(
86
+ `āŒ Error stopping ${validator.name} (${validator.id}): ${
87
+ error instanceof Error ? error.message : String(error)
88
+ }`
89
+ )
90
+ );
91
+ errorCount++;
92
+ }
93
+ }
94
+
95
+ // Summary
96
+ console.log();
97
+ if (stoppedCount > 0) {
98
+ console.log(
99
+ chalk.green(`āœ… Successfully stopped ${stoppedCount} validator(s)`)
100
+ );
101
+ }
102
+ if (errorCount > 0) {
103
+ console.log(chalk.red(`āŒ Failed to stop ${errorCount} validator(s)`));
104
+ }
105
+ }
106
+
107
+ async function stopValidator(
108
+ validator: RunningValidator,
109
+ forceKill: boolean = false
110
+ ): Promise<{ success: boolean; error?: string }> {
111
+ try {
112
+ // Check if process is still running
113
+ const isRunning = await processRegistry.isProcessRunning(validator.pid);
114
+ if (!isRunning) {
115
+ // Process already stopped, just clean up registry
116
+ processRegistry.unregister(validator.id);
117
+ return { success: true };
118
+ }
119
+
120
+ const signal = forceKill ? "SIGKILL" : "SIGTERM";
121
+
122
+ // Send termination signal
123
+ process.kill(validator.pid, signal);
124
+
125
+ if (forceKill) {
126
+ // For SIGKILL, process should stop immediately
127
+ processRegistry.unregister(validator.id);
128
+ return { success: true };
129
+ } else {
130
+ // For SIGTERM, wait for graceful shutdown
131
+ const shutdownResult = await waitForProcessShutdown(validator.pid, 10000);
132
+
133
+ if (shutdownResult.success) {
134
+ processRegistry.unregister(validator.id);
135
+ return { success: true };
136
+ } else {
137
+ // Graceful shutdown failed, try force kill
138
+ console.log(
139
+ chalk.yellow(
140
+ `āš ļø Graceful shutdown failed for ${validator.name}, force killing...`
141
+ )
142
+ );
143
+ process.kill(validator.pid, "SIGKILL");
144
+ processRegistry.unregister(validator.id);
145
+ return { success: true };
146
+ }
147
+ }
148
+ } catch (error) {
149
+ const errorMessage = error instanceof Error ? error.message : String(error);
150
+
151
+ // If error is "ESRCH" (No such process), the process is already gone
152
+ if (errorMessage.includes("ESRCH")) {
153
+ processRegistry.unregister(validator.id);
154
+ return { success: true };
155
+ }
156
+
157
+ return { success: false, error: errorMessage };
158
+ }
159
+ }
160
+
161
+ async function waitForProcessShutdown(
162
+ pid: number,
163
+ timeoutMs: number = 10000
164
+ ): Promise<{ success: boolean; error?: string }> {
165
+ const startTime = Date.now();
166
+
167
+ while (Date.now() - startTime < timeoutMs) {
168
+ try {
169
+ // Send signal 0 to check if process exists
170
+ process.kill(pid, 0);
171
+ // If no error thrown, process is still running
172
+ await new Promise((resolve) => setTimeout(resolve, 500));
173
+ } catch (error) {
174
+ // Process is gone
175
+ return { success: true };
176
+ }
177
+ }
178
+
179
+ return { success: false, error: "Process shutdown timeout" };
180
+ }
181
+
182
+ export async function killCommand(
183
+ validatorId?: string,
184
+ options: { all?: boolean } = {}
185
+ ): Promise<void> {
186
+ console.log(chalk.red("šŸ’€ Force killing validator(s)...\n"));
187
+
188
+ // Clean up dead processes first
189
+ await processRegistry.cleanup();
190
+
191
+ const validators = processRegistry.getRunning();
192
+
193
+ if (validators.length === 0) {
194
+ console.log(chalk.yellow("āš ļø No running validators found"));
195
+ return;
196
+ }
197
+
198
+ let validatorsToKill: RunningValidator[] = [];
199
+
200
+ if (options.all) {
201
+ // Kill all validators
202
+ validatorsToKill = validators;
203
+ console.log(
204
+ chalk.cyan(`šŸ”„ Force killing all ${validators.length} validator(s)...`)
205
+ );
206
+ } else if (validatorId) {
207
+ // Kill specific validator
208
+ const validator = processRegistry.getById(validatorId);
209
+ if (!validator) {
210
+ console.error(
211
+ chalk.red(`āŒ Validator with ID '${validatorId}' not found`)
212
+ );
213
+ console.log(
214
+ chalk.gray("šŸ’” Use `solforge list` to see running validators")
215
+ );
216
+ return;
217
+ }
218
+ validatorsToKill = [validator];
219
+ console.log(
220
+ chalk.cyan(
221
+ `šŸ”„ Force killing validator '${validator.name}' (${validatorId})...`
222
+ )
223
+ );
224
+ } else {
225
+ // No specific validator specified, show interactive selection
226
+ console.log(chalk.cyan("šŸ“‹ Select validator(s) to force kill:\n"));
227
+
228
+ // Display current validators
229
+ displayValidatorsTable(validators);
230
+
231
+ const choices = [
232
+ ...validators.map((v) => ({
233
+ name: `${v.name} (${v.id}) - PID: ${v.pid}`,
234
+ value: v.id,
235
+ })),
236
+ {
237
+ name: chalk.red("Kill ALL validators"),
238
+ value: "all",
239
+ },
240
+ {
241
+ name: chalk.gray("Cancel"),
242
+ value: "cancel",
243
+ },
244
+ ];
245
+
246
+ const selectedValidator = await select({
247
+ message: "Which validator would you like to force kill?",
248
+ choices,
249
+ });
250
+
251
+ if (selectedValidator === "cancel") {
252
+ console.log(chalk.gray("Operation cancelled"));
253
+ return;
254
+ }
255
+
256
+ if (selectedValidator === "all") {
257
+ validatorsToKill = validators;
258
+ console.log(
259
+ chalk.cyan(`šŸ”„ Force killing all ${validators.length} validator(s)...`)
260
+ );
261
+ } else {
262
+ const validator = processRegistry.getById(selectedValidator);
263
+ if (!validator) {
264
+ console.error(chalk.red("āŒ Selected validator not found"));
265
+ return;
266
+ }
267
+ validatorsToKill = [validator];
268
+ console.log(
269
+ chalk.cyan(
270
+ `šŸ”„ Force killing validator '${validator.name}' (${selectedValidator})...`
271
+ )
272
+ );
273
+ }
274
+ }
275
+
276
+ // Kill each validator
277
+ let killedCount = 0;
278
+ let errorCount = 0;
279
+
280
+ for (const validator of validatorsToKill) {
281
+ try {
282
+ const result = await stopValidator(validator, true); // Force kill
283
+ if (result.success) {
284
+ console.log(
285
+ chalk.green(`āœ… Killed ${validator.name} (${validator.id})`)
286
+ );
287
+ killedCount++;
288
+ } else {
289
+ console.error(
290
+ chalk.red(
291
+ `āŒ Failed to kill ${validator.name} (${validator.id}): ${result.error}`
292
+ )
293
+ );
294
+ errorCount++;
295
+ }
296
+ } catch (error) {
297
+ console.error(
298
+ chalk.red(
299
+ `āŒ Error killing ${validator.name} (${validator.id}): ${
300
+ error instanceof Error ? error.message : String(error)
301
+ }`
302
+ )
303
+ );
304
+ errorCount++;
305
+ }
306
+ }
307
+
308
+ // Summary
309
+ console.log();
310
+ if (killedCount > 0) {
311
+ console.log(
312
+ chalk.green(`āœ… Successfully killed ${killedCount} validator(s)`)
313
+ );
314
+ }
315
+ if (errorCount > 0) {
316
+ console.log(chalk.red(`āŒ Failed to kill ${errorCount} validator(s)`));
317
+ }
318
+ }
319
+
320
+ function displayValidatorsTable(validators: RunningValidator[]): void {
321
+ // Calculate column widths
322
+ const maxIdWidth = Math.max(2, ...validators.map((v) => v.id.length));
323
+ const maxNameWidth = Math.max(4, ...validators.map((v) => v.name.length));
324
+ const maxPidWidth = Math.max(
325
+ 3,
326
+ ...validators.map((v) => v.pid.toString().length)
327
+ );
328
+ const maxPortWidth = 9; // "8899/9900" format
329
+ const maxUptimeWidth = 7;
330
+
331
+ // Header
332
+ const header =
333
+ chalk.bold("ID".padEnd(maxIdWidth)) +
334
+ " | " +
335
+ chalk.bold("Name".padEnd(maxNameWidth)) +
336
+ " | " +
337
+ chalk.bold("PID".padEnd(maxPidWidth)) +
338
+ " | " +
339
+ chalk.bold("RPC/Faucet".padEnd(maxPortWidth)) +
340
+ " | " +
341
+ chalk.bold("Uptime".padEnd(maxUptimeWidth)) +
342
+ " | " +
343
+ chalk.bold("Status");
344
+
345
+ console.log(header);
346
+ console.log("-".repeat(header.length - 20)); // Subtract ANSI codes length
347
+
348
+ // Rows
349
+ validators.forEach((validator) => {
350
+ const uptime = formatUptime(validator.startTime);
351
+ const ports = `${validator.rpcPort}/${validator.faucetPort}`;
352
+ const status =
353
+ validator.status === "running" ? chalk.green("ā—") : chalk.red("ā—");
354
+
355
+ const row =
356
+ validator.id.padEnd(maxIdWidth) +
357
+ " | " +
358
+ validator.name.padEnd(maxNameWidth) +
359
+ " | " +
360
+ validator.pid.toString().padEnd(maxPidWidth) +
361
+ " | " +
362
+ ports.padEnd(maxPortWidth) +
363
+ " | " +
364
+ uptime.padEnd(maxUptimeWidth) +
365
+ " | " +
366
+ status;
367
+
368
+ console.log(row);
369
+ });
370
+
371
+ console.log(); // Empty line
372
+ }
373
+
374
+ function formatUptime(startTime: Date): string {
375
+ const now = new Date();
376
+ const uptimeMs = now.getTime() - startTime.getTime();
377
+ const uptimeSeconds = Math.floor(uptimeMs / 1000);
378
+
379
+ if (uptimeSeconds < 60) {
380
+ return `${uptimeSeconds}s`;
381
+ } else if (uptimeSeconds < 3600) {
382
+ const minutes = Math.floor(uptimeSeconds / 60);
383
+ return `${minutes}m`;
384
+ } else {
385
+ const hours = Math.floor(uptimeSeconds / 3600);
386
+ const minutes = Math.floor((uptimeSeconds % 3600) / 60);
387
+ return `${hours}h${minutes}m`;
388
+ }
389
+ }