-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsimulator.ts
More file actions
296 lines (245 loc) · 8.65 KB
/
simulator.ts
File metadata and controls
296 lines (245 loc) · 8.65 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import Docker, {ContainerInfo} from "dockerode";
import * as fs from "fs";
import * as dotenv from "dotenv";
import * as path from "path";
import * as semver from "semver";
import updateCheck from "update-check";
import pkg from '../../../package.json'
import {rpcClient} from "../clients/jsonRpcClient";
import {
DEFAULT_RUN_SIMULATOR_COMMAND,
DEFAULT_RUN_DOCKER_COMMAND,
STARTING_TIMEOUT_WAIT_CYLCE,
STARTING_TIMEOUT_ATTEMPTS,
AI_PROVIDERS_CONFIG,
AiProviders,
VERSION_REQUIREMENTS,
CONTAINERS_NAME_PREFIX,
IMAGES_NAME_PREFIX
} from "../config/simulator";
import {
checkCommand,
getVersion,
executeCommand,
openUrl,
} from "../clients/system";
import {MissingRequirementError} from "../errors/missingRequirement";
import {
ISimulatorService,
WaitForSimulatorToBeReadyResultType,
} from "../interfaces/ISimulatorService";
import {VersionRequiredError} from "../errors/versionRequired";
function sleep(millliseconds: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, millliseconds));
}
export class SimulatorService implements ISimulatorService {
private composeOptions: string
private docker: Docker;
public location: string;
constructor() {
this.location = path.resolve(__dirname, '..');
this.composeOptions = "";
this.docker = new Docker();
}
private readEnvConfigValue(key: string): string {
const envFilePath = path.join(this.location, ".env");
// Transform the config string to object
const envConfig = dotenv.parse(fs.readFileSync(envFilePath, "utf8"));
return envConfig[key];
}
private async getGenlayerContainers(): Promise<ContainerInfo[]> {
const containers = await this.docker.listContainers({ all: true });
return containers.filter(container =>
container.Names.some(name =>
name.startsWith(CONTAINERS_NAME_PREFIX) || name.includes("ollama")
)
);
}
private async stopAndRemoveContainers(remove: boolean = false): Promise<void> {
const genlayerContainers = await this.getGenlayerContainers();
for (const containerInfo of genlayerContainers) {
const container = this.docker.getContainer(containerInfo.Id);
if (containerInfo.State === "running") {
await container.stop();
}
const isOllamaContainer = containerInfo.Names.some(name => name.includes("ollama"));
if (remove && !isOllamaContainer) {
await container.remove();
}
}
}
public addConfigToEnvFile(newConfig: Record<string, string>): void {
const envFilePath = path.join(this.location, ".env");
// Transform the config string to object
const envConfig = dotenv.parse(fs.readFileSync(envFilePath, "utf8"));
Object.keys(newConfig).forEach(key => {
envConfig[key] = newConfig[key];
});
// Transform the updated config object back into a string
const updatedConfig = Object.keys(envConfig)
.map(key => {
return `${key}=${envConfig[key]}`;
})
.join("\n");
// Write the new .env file
fs.writeFileSync(envFilePath, updatedConfig);
}
public setComposeOptions(headless: boolean): void {
this.composeOptions = headless ? '--scale frontend=0' : '';
}
public getComposeOptions(): string {
return this.composeOptions;
}
public async checkCliVersion(): Promise<void> {
const update = await updateCheck(pkg);
if (update && update.latest !== pkg.version) {
console.warn(`\nA new version (${update.latest}) is available! You're using version ${pkg.version}.\nRun npm install -g genlayer to update\n`);
}
}
public async checkInstallRequirements(): Promise<Record<string, boolean>> {
const requirementsInstalled = {
docker: false,
};
try {
await checkCommand("docker --version", "docker");
requirementsInstalled.docker = true;
} catch (error: any) {
if (!(error instanceof MissingRequirementError)) {
throw error;
}
}
if (requirementsInstalled.docker) {
try {
await this.docker.ping()
} catch (error: any) {
await executeCommand(DEFAULT_RUN_DOCKER_COMMAND);
}
}
return requirementsInstalled;
}
public async checkVersionRequirements(): Promise<Record<string, string>> {
const missingVersions = {
docker: "",
node: "",
};
try {
await this.checkVersion(VERSION_REQUIREMENTS.node, "node");
} catch (error: any) {
missingVersions.node = VERSION_REQUIREMENTS.node;
if (!(error instanceof VersionRequiredError)) {
throw error;
}
}
try {
await this.checkVersion(VERSION_REQUIREMENTS.docker, "docker");
} catch (error: any) {
missingVersions.docker = VERSION_REQUIREMENTS.docker;
if (!(error instanceof VersionRequiredError)) {
throw error;
}
}
return missingVersions;
}
public async checkVersion(minVersion: string, toolName: string): Promise<void> {
const version = await getVersion(toolName);
if (!semver.satisfies(version, `>=${minVersion}`)) {
throw new VersionRequiredError(toolName, minVersion);
}
}
public runSimulator(): Promise<{stdout: string; stderr: string}> {
const commandsByPlatform = DEFAULT_RUN_SIMULATOR_COMMAND(this.location, this.getComposeOptions());
return executeCommand(commandsByPlatform);
}
public async waitForSimulatorToBeReady(
retries: number = STARTING_TIMEOUT_ATTEMPTS,
): Promise<WaitForSimulatorToBeReadyResultType> {
console.log("Waiting for the simulator to start up...");
try {
const response = await rpcClient.request({method: "ping", params: []});
//Compatibility with current simulator version
if (response?.result === "OK" || response?.result?.status === "OK" || response?.result?.data?.status === "OK") {
return { initialized: true };
}
if (retries > 0) {
await sleep(STARTING_TIMEOUT_WAIT_CYLCE);
return this.waitForSimulatorToBeReady(retries - 1);
}
} catch (error: any) {
if (
(error.name === "FetchError" ||
error.message.includes("Fetch Error") ||
error.message.includes("ECONNRESET") ||
error.message.includes("ECONNREFUSED") ||
error.message.includes("socket hang up")) &&
retries > 0
) {
await sleep(STARTING_TIMEOUT_WAIT_CYLCE * 2);
return this.waitForSimulatorToBeReady(retries - 1);
}
return {initialized: false, errorCode: "ERROR", errorMessage: error.message};
}
return {initialized: false, errorCode: "TIMEOUT"};
}
public createRandomValidators(numValidators: number, llmProviders: AiProviders[]): Promise<any> {
return rpcClient.request({
method: "sim_createRandomValidators",
params: [numValidators, 1, 10, llmProviders],
});
}
public deleteAllValidators(): Promise<any> {
return rpcClient.request({method: "sim_deleteAllValidators", params: []});
}
public getAiProvidersOptions(withHint: boolean = true): Array<{name: string; value: string}> {
return Object.values(AI_PROVIDERS_CONFIG).map(providerConfig => {
return {
name: `${providerConfig.name}${withHint ? ` ${providerConfig.hint}` : ""}`,
value: providerConfig.cliOptionValue,
};
});
}
public getFrontendUrl(): string {
const frontendPort = this.readEnvConfigValue("FRONTEND_PORT");
return `http://localhost:${frontendPort}`;
}
public async openFrontend(): Promise<boolean> {
await openUrl(this.getFrontendUrl());
return true;
}
public async stopDockerContainers(): Promise<void> {
await this.stopAndRemoveContainers(false);
}
public async resetDockerContainers(): Promise<void> {
await this.stopAndRemoveContainers(true);
}
public async resetDockerImages(): Promise<void> {
const images = await this.docker.listImages();
const genlayerImages = images.filter(image =>
image.RepoTags?.some(tag => tag.startsWith(IMAGES_NAME_PREFIX))
);
for (const imageInfo of genlayerImages) {
const image = this.docker.getImage(imageInfo.Id);
await image.remove({force: true});
}
}
public async cleanDatabase(): Promise<boolean> {
try {
await rpcClient.request({method: "sim_clearDbTables", params: [['current_state', 'transactions']]});
}catch (error) {
console.error(error);
}
return true;
}
public normalizeLocalnetVersion(version: string) {
if (!version.startsWith('v')) {
version = 'v' + version;
}
const versionRegex = /^v(\d+)\.(\d+)\.(\d+)(-.+)?$/;
const match = version.match(versionRegex);
if (!match) {
console.error('Invalid version format. Expected format: v0.0.0 or v0.0.0-suffix');
process.exit(1);
}
return version
}
}
export default new SimulatorService();