-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathstart-validator.ts
107 lines (96 loc) · 3.35 KB
/
start-validator.ts
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
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable unicorn/catch-error-name */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
/* eslint-disable @typescript-eslint/no-base-to-string */
/* eslint-disable no-console */
import { exec } from "node:child_process";
import { mkdtemp } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
import { Connection } from "@solana/web3.js";
import { loadKeypair } from "./keys";
import {
INTEGRITY_POOL_PROGRAM_ADDRESS,
PUBLISHER_CAPS_PROGRAM_ADDRESS,
STAKING_PROGRAM_ADDRESS,
} from "../src/constants";
export function getConnection(): Connection {
return new Connection(
`http://127.0.0.1:8899`,
AnchorProvider.defaultOptions().commitment,
);
}
/**
* If we abort immediately, the web-sockets are still subscribed, and they give a ton of errors.
* Waiting a few seconds is enough to let the sockets close.
*/
export class CustomAbortController {
abortController: AbortController;
constructor(abortController: AbortController) {
this.abortController = abortController;
}
abort() {
return new Promise((resolve) => {
setTimeout(() => {
this.abortController.abort();
resolve(undefined);
}, 5000);
});
}
}
/* Starts a validator at port portNumber with the command line arguments specified after a few basic ones
*
* returns a `{ controller, connection }` struct. Users of this method have to terminate the
* validator by calling :
* ```controller.abort()```
*/
export async function startValidatorRaw() {
const connection: Connection = getConnection();
const ledgerDir = await mkdtemp(path.join(os.tmpdir(), "ledger-"));
const internalController: AbortController = new AbortController();
const { signal } = internalController;
const user = loadKeypair(
"integration-tests/keypairs/localnet-authority.json",
);
const command = `solana-test-validator \
--ledger ${ledgerDir} \
--reset \
--mint ${user.publicKey.toBase58()} \
--bpf-program ${STAKING_PROGRAM_ADDRESS.toBase58()} integration-tests/programs/staking.so \
--bpf-program ${INTEGRITY_POOL_PROGRAM_ADDRESS.toBase58()} integration-tests/programs/integrity_pool.so \
--bpf-program ${PUBLISHER_CAPS_PROGRAM_ADDRESS.toBase58()} integration-tests/programs/publisher_caps.so \
`;
exec(command, { signal }, (error, stdout, stderr) => {
if (error?.name.includes("AbortError")) {
// Test complete, this is expected.
return;
}
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
const controller = new CustomAbortController(internalController);
let numRetries = 0;
while (true) {
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
await connection.getSlot();
break;
} catch (e) {
// Bound the number of retries so the tests don't hang if there's some problem blocking
// the connection to the validator.
if (numRetries == 30) {
console.log(
`Failed to start validator or connect to running validator. Caught exception: ${e}`,
);
throw e;
}
numRetries += 1;
}
}
return { controller, connection, wallet: new Wallet(user) };
}