genlayer 0.3.1 → 0.4.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
 
2
2
 
3
- ## 0.3.1 (2024-11-19)
3
+ ## 0.4.0 (2024-11-22)
4
+
5
+
6
+ ### Features
7
+
8
+ * new reset db option ([#139](https://github.com/yeagerai/genlayer-cli/issues/139)) ([e3fed64](https://github.com/yeagerai/genlayer-cli/commit/e3fed6437e7313002685258aeced710a6fd63f4f))
4
9
 
5
10
  ## 0.3.0 (2024-11-15)
6
11
 
package/dist/index.js CHANGED
@@ -46421,7 +46421,7 @@ var {
46421
46421
  } = import_index.default;
46422
46422
 
46423
46423
  // package.json
46424
- var version = "0.3.1";
46424
+ var version = "0.4.0";
46425
46425
 
46426
46426
  // src/lib/config/text.ts
46427
46427
  var CLI_DESCRIPTION = "GenLayer CLI is a development environment for the GenLayer ecosystem. It allows developers to interact with the protocol by creating accounts, sending transactions, and working with Intelligent Contracts by testing, debugging, and deploying them.";
@@ -47022,9 +47022,12 @@ var MissingRequirementError = class extends Error {
47022
47022
  // src/lib/clients/system.ts
47023
47023
  function checkCommand(command, toolName) {
47024
47024
  return __async(this, null, function* () {
47025
- const { stderr } = yield import_node_util5.default.promisify(import_child_process.exec)(command);
47026
- if (stderr) {
47027
- throw new MissingRequirementError(toolName);
47025
+ try {
47026
+ yield import_node_util5.default.promisify(import_child_process.exec)(command);
47027
+ } catch (error) {
47028
+ if (error.stderr) {
47029
+ throw new MissingRequirementError(toolName);
47030
+ }
47028
47031
  }
47029
47032
  });
47030
47033
  }
@@ -47331,6 +47334,16 @@ var SimulatorService = class {
47331
47334
  return true;
47332
47335
  });
47333
47336
  }
47337
+ cleanDatabase() {
47338
+ return __async(this, null, function* () {
47339
+ try {
47340
+ yield rpcClient.request({ method: "sim_clearDbTables", params: [["current_state", "transactions"]] });
47341
+ } catch (error) {
47342
+ console.error(error);
47343
+ }
47344
+ return true;
47345
+ });
47346
+ }
47334
47347
  };
47335
47348
  var simulator_default = new SimulatorService();
47336
47349
 
@@ -50097,6 +50110,9 @@ function initAction(options, simulatorService) {
50097
50110
  console.error(error);
50098
50111
  return;
50099
50112
  }
50113
+ if (options.resetDb) {
50114
+ yield simulatorService.cleanDatabase();
50115
+ }
50100
50116
  let successMessage = "GenLayer simulator initialized successfully! ";
50101
50117
  successMessage += options.headless ? "" : `Go to ${simulatorService.getFrontendUrl()} in your browser to access it.`;
50102
50118
  console.log(successMessage);
@@ -50113,7 +50129,7 @@ function initAction(options, simulatorService) {
50113
50129
  // src/commands/general/start.ts
50114
50130
  function startAction(options, simulatorService) {
50115
50131
  return __async(this, null, function* () {
50116
- const { resetValidators, numValidators, branch, location, headless } = options;
50132
+ const { resetValidators, numValidators, branch, location, headless, resetDb } = options;
50117
50133
  simulatorService.setComposeOptions(headless);
50118
50134
  simulatorService.setSimulatorLocation(location);
50119
50135
  const restartValidatorsHintText = resetValidators ? `creating new ${numValidators} random validators` : "keeping the existing validators";
@@ -50150,6 +50166,9 @@ function startAction(options, simulatorService) {
50150
50166
  console.error(error);
50151
50167
  return;
50152
50168
  }
50169
+ if (resetDb) {
50170
+ yield simulatorService.cleanDatabase();
50171
+ }
50153
50172
  if (resetValidators) {
50154
50173
  console.log("Initializing validators...");
50155
50174
  try {
@@ -50195,8 +50214,8 @@ function startAction(options, simulatorService) {
50195
50214
 
50196
50215
  // src/commands/general/index.ts
50197
50216
  function initializeGeneralCommands(program2) {
50198
- program2.command("init").description("Initialize the GenLayer Environment").option("--numValidators <numValidators>", "Number of validators", "5").option("--branch <branch>", "Branch", "main").option("--location <folder>", "Location where it will be installed", process.cwd()).option("--headless", "Headless mode", false).action((options) => initAction(options, simulator_default));
50199
- program2.command("up").description("Starts GenLayer's simulator").option("--reset-validators", "Remove all current validators and create new random ones", false).option("--numValidators <numValidators>", "Number of validators", "5").option("--branch <branch>", "Branch", "main").option("--location <folder>", "Location where it will be installed", process.cwd()).option("--headless", "Headless mode", false).action((options) => startAction(options, simulator_default));
50217
+ program2.command("init").description("Initialize the GenLayer Environment").option("--numValidators <numValidators>", "Number of validators", "5").option("--branch <branch>", "Branch", "main").option("--location <folder>", "Location where it will be installed", process.cwd()).option("--headless", "Headless mode", false).option("--reset-db", "Reset Database", false).action((options) => initAction(options, simulator_default));
50218
+ program2.command("up").description("Starts GenLayer's simulator").option("--reset-validators", "Remove all current validators and create new random ones", false).option("--numValidators <numValidators>", "Number of validators", "5").option("--branch <branch>", "Branch", "main").option("--location <folder>", "Location where it will be installed", process.cwd()).option("--headless", "Headless mode", false).option("--reset-db", "Reset Database", false).action((options) => startAction(options, simulator_default));
50200
50219
  return program2;
50201
50220
  }
50202
50221
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "genlayer",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "GenLayer Command Line Tool",
5
5
  "main": "src/index.ts",
6
6
  "bin": {
package/renovate.json CHANGED
@@ -13,8 +13,7 @@
13
13
  "matchUpdateTypes": [
14
14
  "minor",
15
15
  "patch"
16
- ],
17
- "matchConfidence": ["very high", "high"]
16
+ ]
18
17
  }
19
18
  ]
20
19
  }
@@ -13,6 +13,7 @@ export function initializeGeneralCommands(program: Command) {
13
13
  .option("--branch <branch>", "Branch", "main")
14
14
  .option("--location <folder>", "Location where it will be installed", process.cwd())
15
15
  .option("--headless", "Headless mode", false)
16
+ .option("--reset-db", "Reset Database", false)
16
17
  .action((options: InitActionOptions) => initAction(options, simulatorService));
17
18
 
18
19
  program
@@ -23,6 +24,7 @@ export function initializeGeneralCommands(program: Command) {
23
24
  .option("--branch <branch>", "Branch", "main")
24
25
  .option("--location <folder>", "Location where it will be installed", process.cwd())
25
26
  .option("--headless", "Headless mode", false)
27
+ .option("--reset-db", "Reset Database", false)
26
28
  .action((options: StartActionOptions) => startAction(options, simulatorService));
27
29
 
28
30
  return program;
@@ -7,6 +7,7 @@ export interface InitActionOptions {
7
7
  branch: string;
8
8
  location: string;
9
9
  headless: boolean;
10
+ resetDb: boolean;
10
11
  }
11
12
 
12
13
  function getRequirementsErrorMessage({git, docker}: Record<string, boolean>): string {
@@ -225,6 +226,10 @@ export async function initAction(options: InitActionOptions, simulatorService: I
225
226
  return;
226
227
  }
227
228
 
229
+ if(options.resetDb){
230
+ await simulatorService.cleanDatabase()
231
+ }
232
+
228
233
  // Simulator ready
229
234
  let successMessage = "GenLayer simulator initialized successfully! "
230
235
  successMessage += options.headless ? '' : `Go to ${simulatorService.getFrontendUrl()} in your browser to access it.`;
@@ -8,10 +8,11 @@ export interface StartActionOptions {
8
8
  branch: string;
9
9
  location: string;
10
10
  headless: boolean;
11
+ resetDb: boolean
11
12
  }
12
13
 
13
14
  export async function startAction(options: StartActionOptions, simulatorService: ISimulatorService) {
14
- const {resetValidators, numValidators, branch, location, headless} = options;
15
+ const {resetValidators, numValidators, branch, location, headless, resetDb} = options;
15
16
  // Update simulator location with user input
16
17
  simulatorService.setComposeOptions(headless);
17
18
  simulatorService.setSimulatorLocation(location);
@@ -60,6 +61,10 @@ export async function startAction(options: StartActionOptions, simulatorService:
60
61
  return;
61
62
  }
62
63
 
64
+ if(resetDb){
65
+ await simulatorService.cleanDatabase()
66
+ }
67
+
63
68
  if (resetValidators) {
64
69
  // Initializing validators
65
70
  console.log("Initializing validators...");
@@ -6,9 +6,12 @@ import {RunningPlatform, AVAILABLE_PLATFORMS} from "../config/simulator";
6
6
  import {MissingRequirementError} from "../errors/missingRequirement";
7
7
 
8
8
  export async function checkCommand(command: string, toolName: string): Promise<void> {
9
- const {stderr} = await util.promisify(exec)(command);
10
- if (stderr) {
11
- throw new MissingRequirementError(toolName);
9
+ try {
10
+ await util.promisify(exec)(command);
11
+ }catch (error:any) {
12
+ if (error.stderr) {
13
+ throw new MissingRequirementError(toolName);
14
+ }
12
15
  }
13
16
  }
14
17
 
@@ -20,6 +20,7 @@ export interface ISimulatorService {
20
20
  openFrontend(): Promise<boolean>;
21
21
  resetDockerContainers(): Promise<boolean>;
22
22
  resetDockerImages(): Promise<boolean>;
23
+ cleanDatabase(): Promise<boolean>;
23
24
  }
24
25
 
25
26
  export type DownloadSimulatorResultType = {
@@ -317,6 +317,17 @@ export class SimulatorService implements ISimulatorService {
317
317
 
318
318
  return true;
319
319
  }
320
+
321
+ public async cleanDatabase(): Promise<boolean> {
322
+
323
+ try {
324
+ await rpcClient.request({method: "sim_clearDbTables", params: [['current_state', 'transactions']]});
325
+ }catch (error) {
326
+ console.error(error);
327
+ }
328
+ return true;
329
+ }
330
+
320
331
  }
321
332
 
322
333
  export default new SimulatorService();
@@ -7,7 +7,7 @@ import {mkdtempSync} from "fs";
7
7
  import {join} from "path";
8
8
 
9
9
  const tempDir = mkdtempSync(join(tmpdir(), "test-initAction-"));
10
- const defaultActionOptions = { numValidators: 5, branch: "main", location: tempDir, headless: false };
10
+ const defaultActionOptions = { numValidators: 5, branch: "main", location: tempDir, headless: false, resetDb: false };
11
11
 
12
12
  describe("init action", () => {
13
13
  let error: ReturnType<any>;
@@ -188,7 +188,7 @@ describe("init action", () => {
188
188
  );
189
189
  });
190
190
 
191
- test("should open the frontend if everything went well (headless mode)", async () => {
191
+ test("should open the frontend if everything went well (custom options)", async () => {
192
192
  inquirerPrompt.mockResolvedValue({
193
193
  confirmReset: true,
194
194
  confirmDownload: true,
@@ -211,7 +211,7 @@ describe("init action", () => {
211
211
  simServResetDockerContainers.mockResolvedValue(true);
212
212
  simServResetDockerImages.mockResolvedValue(true);
213
213
 
214
- await initAction({...defaultActionOptions, headless: true}, simulatorService);
214
+ await initAction({...defaultActionOptions, headless: true, resetDb: true}, simulatorService);
215
215
 
216
216
  expect(log).toHaveBeenCalledWith(
217
217
  `GenLayer simulator initialized successfully! `
@@ -235,8 +235,12 @@ describe("init action", () => {
235
235
  simServWaitForSimulator.mockResolvedValue({ initialized: true });
236
236
  simServPullOllamaModel.mockResolvedValue(true);
237
237
  simServDeleteAllValidators.mockResolvedValue(true);
238
+ simServResetDockerContainers.mockResolvedValue(true);
239
+ simServResetDockerImages.mockResolvedValue(true);
238
240
  simServCreateRandomValidators.mockRejectedValue();
239
241
  simServOpenFrontend.mockResolvedValue(true);
242
+ simServResetDockerContainers.mockResolvedValue(true);
243
+ simServResetDockerImages.mockResolvedValue(true);
240
244
 
241
245
  await initAction({...defaultActionOptions, headless: true}, simulatorService);
242
246
 
@@ -15,6 +15,7 @@ describe("startAction - Additional Tests", () => {
15
15
  branch: "main",
16
16
  location: '',
17
17
  headless: false,
18
+ resetDb: false
18
19
  };
19
20
 
20
21
  beforeEach(() => {
@@ -36,6 +37,7 @@ describe("startAction - Additional Tests", () => {
36
37
  { name: "Provider B", value: "providerB" },
37
38
  ]),
38
39
  getFrontendUrl: vi.fn(() => "http://localhost:8080"),
40
+ cleanDatabase: vi.fn().mockResolvedValue(undefined),
39
41
  } as unknown as ISimulatorService;
40
42
  });
41
43
 
@@ -59,8 +61,8 @@ describe("startAction - Additional Tests", () => {
59
61
  expect(simulatorService.openFrontend).toHaveBeenCalled();
60
62
  });
61
63
 
62
- test("runs successfully with default options and keeps existing validators (headless)", async () => {
63
- await startAction({...defaultOptions, headless: true}, simulatorService);
64
+ test("runs successfully with custom options and keeps existing validators", async () => {
65
+ await startAction({...defaultOptions, headless: true, resetDb: true}, simulatorService);
64
66
 
65
67
  expect(simulatorService.updateSimulator).toHaveBeenCalledWith("main");
66
68
  expect(simulatorService.runSimulator).toHaveBeenCalled();
@@ -5,6 +5,13 @@ import { getCommand, getCommandOption } from "../utils";
5
5
  import simulatorService from '../../src/lib/services/simulator'
6
6
 
7
7
  const openFrontendSpy = vi.spyOn(simulatorService, "openFrontend");
8
+ const defaultOptions = {
9
+ numValidators: "5",
10
+ branch: "main",
11
+ location: process.cwd(),
12
+ headless: false,
13
+ resetDb: false
14
+ }
8
15
 
9
16
  vi.mock("inquirer", () => ({
10
17
  prompt: vi.fn(() => {}),
@@ -74,13 +81,13 @@ describe("init command", () => {
74
81
  test("action is called", async () => {
75
82
  program.parse(["node", "test", "init"]);
76
83
  expect(action).toHaveBeenCalledTimes(1);
77
- expect(action).toHaveBeenCalledWith({ numValidators: "5", branch: "main", location: process.cwd(), headless: false });
84
+ expect(action).toHaveBeenCalledWith(defaultOptions);
78
85
  });
79
86
 
80
87
  test("option --headless is accepted", async () => {
81
88
  program.parse(["node", "test", "init", "--headless"]);
82
89
  expect(action).toHaveBeenCalledTimes(1);
83
- expect(action).toHaveBeenCalledWith({ numValidators: "5", branch: "main", location: process.cwd(), headless: true });
90
+ expect(action).toHaveBeenCalledWith({...defaultOptions, headless: true});
84
91
  expect(openFrontendSpy).not.toHaveBeenCalled();
85
92
  });
86
93
  });
@@ -7,6 +7,14 @@ import simulatorService from '../../src/lib/services/simulator'
7
7
  const openFrontendSpy = vi.spyOn(simulatorService, "openFrontend");
8
8
 
9
9
  const action = vi.fn();
10
+ const defaultOptions = {
11
+ resetValidators: false,
12
+ numValidators: "5",
13
+ branch: "main",
14
+ location: process.cwd(),
15
+ headless: false ,
16
+ resetDb: false
17
+ }
10
18
 
11
19
  describe("up command", () => {
12
20
  let upCommand: Command;
@@ -72,7 +80,7 @@ describe("up command", () => {
72
80
  test("action is called with default options", async () => {
73
81
  program.parse(["node", "test", "up"]);
74
82
  expect(action).toHaveBeenCalledTimes(1);
75
- expect(action).toHaveBeenCalledWith({ resetValidators: false, numValidators: "5", branch: "main", location: process.cwd(), headless: false });
83
+ expect(action).toHaveBeenCalledWith(defaultOptions);
76
84
  });
77
85
 
78
86
  test("action is called with custom options", async () => {
@@ -86,16 +94,12 @@ describe("up command", () => {
86
94
  "--branch",
87
95
  "development",
88
96
  "--headless",
97
+ "true",
98
+ "--reset-db",
89
99
  "true"
90
100
  ]);
91
101
  expect(action).toHaveBeenCalledTimes(1);
92
- expect(action).toHaveBeenCalledWith({
93
- resetValidators: true,
94
- numValidators: "10",
95
- branch: "development",
96
- location: process.cwd(),
97
- headless: true,
98
- });
102
+ expect(action).toHaveBeenCalledWith({...defaultOptions, headless: true, branch: 'development', numValidators: '10', resetValidators: true, resetDb: true});
99
103
  expect(openFrontendSpy).not.toHaveBeenCalled();
100
104
  });
101
105
  });
@@ -88,7 +88,7 @@ describe("System Functions - Error Paths", () => {
88
88
  });
89
89
 
90
90
  test("checkCommand returns false if the command does not exist", async () => {
91
- vi.mocked(util.promisify).mockReturnValueOnce(() => Promise.resolve({
91
+ vi.mocked(util.promisify).mockReturnValueOnce(() => Promise.reject({
92
92
  stdout: "",
93
93
  stderr: "command not found"
94
94
  }));
@@ -281,6 +281,12 @@ describe("SimulatorService - Basic Tests", () => {
281
281
  expect(simulatorService.getAiProvidersOptions(false)).toEqual(expect.any(Array));
282
282
  });
283
283
 
284
+ test("clean simulator should success", async () => {
285
+ vi.mocked(rpcClient.request).mockResolvedValueOnce('Success');
286
+ await expect(simulatorService.cleanDatabase).not.toThrow();
287
+ expect(rpcClient.request).toHaveBeenCalledWith({ method: "sim_clearDbTables", params: [['current_state', 'transactions']] });
288
+ });
289
+
284
290
  });
285
291
  describe("SimulatorService - Docker Tests", () => {
286
292
  let mockExec: Mock;