forked from BlackBeard085/x1console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activatestake.js
138 lines (126 loc) · 6.85 KB
/
activatestake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const { exec } = require('child_process');
const os = require('os');
// Get the current user's username
const username = os.userInfo().username;
// Define the validator directory
const validatorDirectory = `/home/${username}/x1/solanalabs`;
// Command to start the validator
const startCommand = `target/release/solana-validator --identity $HOME/.config/solana/identity.json --limit-ledger-size 50000000 --rpc-port 8899 --entrypoint xolana.xen.network:8001 --full-rpc-api --log - --vote-account $HOME/.config/solana/vote.json --max-genesis-archive-unpacked-size 1073741824 --require-tower --enable-rpc-transaction-history --enable-extended-tx-metadata-storage --rpc-pubsub-enable-block-subscription --only-known-rpc --known-validator C58LhVv822GiE3s84pwb58yiaezWLaFFdUtTWDGFySsU --known-validator Abt4r6uhFs7yPwR3jT5qbnLjBtasgHkRVAd1W6H5yonT --expected-shred-version 19582 --minimal-snapshot-download-speed 5000000 --full-snapshot-interval-slots 300 --maximum-incremental-snapshots-to-retain 100 --maximum-full-snapshots-to-retain 50`;
// Check if the validator is running by checking port 8899
function isValidatorRunning() {
return new Promise((resolve) => {
exec("lsof -i :8899", (error, stdout) => {
resolve(stdout.trim() !== ''); // Resolve true if output is not empty
});
});
}
// Function to run Solana catchup
async function runCatchup() {
while (true) {
try {
console.log("Executing catchup command...");
const output = await new Promise((resolve, reject) => {
exec('solana catchup --our-localhost', (error, stdout, stderr) => {
if (error) {
reject(`Error running catchup: ${stderr.trim()}`);
} else {
resolve(stdout.trim()); // Output from the catchup command
}
});
});
console.log('Catchup command output:\n', output);
// Check for outputs
if (output.includes('has caught up')) {
console.log('Catchup successful: Validator has caught up.');
return output; // Return the output if caught up
} else if (output.includes('Connection refused')) {
console.log('Connection refused. Checking if the validator is still running...');
// Check if the validator is running and wait if it's still running
for (let attempt = 0; attempt < 5; attempt++) { // Try 5 times
const running = await isValidatorRunning();
if (running) {
console.log('Validator is still running. Waiting for 10 seconds before trying catchup again...');
await new Promise(res => setTimeout(res, 10000)); // Wait for 10 seconds
console.log('Retrying catchup command...');
break; // Exit retry loop
} else {
console.log('Validator is not running. Exiting catchup.');
throw new Error('Validator is not running.');
}
}
} else {
console.log('Unexpected output from catchup command. Checking again...');
await new Promise(res => setTimeout(res, 10000)); // Wait for 10 seconds
}
} catch (error) {
console.error('Error during catchup:', error);
await new Promise(res => setTimeout(res, 10000)); // Wait for 10 seconds before retrying
}
}
}
// Function to delegate stake
function delegateStake() {
return new Promise((resolve, reject) => {
exec(`cd ${validatorDirectory} && solana delegate-stake stake.json vote.json`, (error, stdout, stderr) => {
if (error) {
reject(`Error delegating stake: ${stderr.trim()}`);
} else {
resolve(stdout.trim()); // Output from the delegate stake command
}
});
});
}
// Execute the script
(async () => {
try {
const running = await isValidatorRunning();
if (running) {
console.log('Validator is already running on port 8899.');
// Run catchup command before delegating stake
console.log('Running catchup command...');
const catchupOutput = await runCatchup();
// Proceed to delegate stake
const delegateOutput = await delegateStake();
console.log('Delegate stake command output:\n', delegateOutput);
console.log('Delegation successful, a restart is required.'); // Message to show after delegation
return; // Exit the script after handling catchup and delegation
} else {
console.log('Validator is not currently running. Proceeding to start it.');
console.log('Starting the validator now...');
exec(`cd ${validatorDirectory} && ${startCommand}`, { stdio: 'pipe' }, (error, stdout, stderr) => {
if (error) {
console.error(`Error starting validator: ${stderr.trim()}`);
return; // Exit if there was an error starting the validator
}
console.log('Validator start command issued.');
});
// Check if the validator has started successfully
for (let attempt = 0; attempt < 10; attempt++) {
await new Promise(res => setTimeout(res, 5000)); // Wait for 5 seconds before checking
const isRunning = await isValidatorRunning();
if (isRunning) {
console.log('Validator started successfully and is running on port 8899.');
// Countdown for 10 seconds before running the catchup command
for (let i = 10; i > 0; i--) {
console.log(`Waiting for ${i} seconds for the validator to stabilize...`);
await new Promise(res => setTimeout(res, 1000));
}
// Run catchup command
console.log('Running catchup command...');
const catchupOutput = await runCatchup();
// Proceed to delegate stake
const delegateOutput = await delegateStake();
console.log('Delegate stake command output:\n', delegateOutput);
console.log('Delegation successful, a restart is required.'); // Message to show after successful delegation
return; // Exit the script after handling catchup and delegation
}
}
console.log('Failed to start the validator. Port 8899 is still not in use. Check logs for fatal error');
}
} catch (error) {
console.error('Failed to manage validator:', error);
} finally {
// Ensure the script exits when done
process.exit();
}
})();