-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
autoStartFuelCore.ts
69 lines (57 loc) · 1.89 KB
/
autoStartFuelCore.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
import { launchNode } from '@fuel-ts/account/test-utils';
import { defaultConsensusKey } from '@fuel-ts/utils';
import type { ChildProcessWithoutNullStreams } from 'child_process';
import { getPortPromise } from 'portfinder';
import type { FuelsConfig } from '../../types';
import { getBinarySource } from '../../utils/getBinarySource';
import { log, loggingConfig } from '../../utils/logger';
export type FuelCoreNode = {
bindIp: string;
accessIp: string;
port: number;
providerUrl: string;
chainConfigPath: string;
killChildProcess: () => void;
};
export type KillNodeParams = {
core: ChildProcessWithoutNullStreams;
killFn: (pid: number) => void;
state: {
isDead: boolean;
};
};
export const autoStartFuelCore = async (config: FuelsConfig) => {
let fuelCore: FuelCoreNode | undefined;
if (config.autoStartFuelCore) {
log(`Starting ${getBinarySource(config.useBuiltinFuelCore)} 'fuel-core' node..`);
const bindIp = '0.0.0.0';
const accessIp = '127.0.0.1';
const port = config.fuelCorePort ?? (await getPortPromise({ port: 4000 }));
const providerUrl = `http://${accessIp}:${port}/graphql`;
const { cleanup, chainConfigPath } = await launchNode({
args: [
['--chain', config.chainConfig],
['--db-type', 'rocks-db'],
].flat() as string[],
ip: bindIp,
port: port.toString(),
loggingEnabled: loggingConfig.isLoggingEnabled,
debugEnabled: loggingConfig.isDebugEnabled,
basePath: config.basePath,
useSystemFuelCore: !config.useBuiltinFuelCore,
});
fuelCore = {
bindIp,
accessIp,
port,
providerUrl,
chainConfigPath,
killChildProcess: cleanup,
};
// eslint-disable-next-line no-param-reassign
config.providerUrl = fuelCore.providerUrl;
// eslint-disable-next-line no-param-reassign
config.privateKey = defaultConsensusKey;
}
return fuelCore;
};