From 3c0c4eb3e6bc5bd6f1dc1687eb82301aaeca4df6 Mon Sep 17 00:00:00 2001 From: Sebastijan K <58827427+sebastijankuzner@users.noreply.github.com> Date: Tue, 3 Nov 2020 09:50:48 +0100 Subject: [PATCH] fix(core): network:generate save to default location (#4138) --- __tests__/unit/core/commands/core-run.test.ts | 8 ++- .../unit/core/commands/forger-run.test.ts | 8 ++- .../core/commands/network-generate.test.ts | 28 +++++---- .../unit/core/commands/relay-run.test.ts | 9 ++- packages/core/bin/run | 2 + packages/core/src/commands/core-run.ts | 3 + packages/core/src/commands/forger-run.ts | 3 + .../core/src/commands/network-generate.ts | 60 +++++++++++-------- packages/core/src/commands/relay-run.ts | 3 + 9 files changed, 84 insertions(+), 40 deletions(-) diff --git a/__tests__/unit/core/commands/core-run.test.ts b/__tests__/unit/core/commands/core-run.test.ts index 90c6909b1a..7c3f64d21f 100644 --- a/__tests__/unit/core/commands/core-run.test.ts +++ b/__tests__/unit/core/commands/core-run.test.ts @@ -32,7 +32,13 @@ describe("RunCommand", () => { const spyBootstrap = jest.spyOn(app, "bootstrap").mockImplementation(undefined); const spyBoot = jest.spyOn(app, "boot").mockImplementation(undefined); - await executeCommand(Command); + executeCommand(Command); + + await new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 50); + }); expect(spyBootstrap).toHaveBeenCalled(); expect(spyBoot).toHaveBeenCalled(); diff --git a/__tests__/unit/core/commands/forger-run.test.ts b/__tests__/unit/core/commands/forger-run.test.ts index deb263d55e..e0fa8b6e3f 100644 --- a/__tests__/unit/core/commands/forger-run.test.ts +++ b/__tests__/unit/core/commands/forger-run.test.ts @@ -32,7 +32,13 @@ describe("RunCommand", () => { const spyBootstrap = jest.spyOn(app, "bootstrap").mockImplementation(undefined); const spyBoot = jest.spyOn(app, "boot").mockImplementation(undefined); - await executeCommand(Command); + executeCommand(Command); + + await new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 50); + }); expect(spyBootstrap).toHaveBeenCalled(); expect(spyBoot).toHaveBeenCalled(); diff --git a/__tests__/unit/core/commands/network-generate.test.ts b/__tests__/unit/core/commands/network-generate.test.ts index 0691af0421..fb135d08ac 100644 --- a/__tests__/unit/core/commands/network-generate.test.ts +++ b/__tests__/unit/core/commands/network-generate.test.ts @@ -3,11 +3,13 @@ import "jest-extended"; import { Console } from "@arkecosystem/core-test-framework"; import { Command } from "@packages/core/src/commands/network-generate"; import fs from "fs-extra"; -import { resolve } from "path"; +import { join } from "path"; import prompts from "prompts"; +import envPaths from "env-paths"; -const configCore: string = resolve(__dirname, "../../../../packages/core/bin/config/mynet7"); -const configCrypto: string = resolve(__dirname, "../../../../packages/crypto/src/networks/mynet7"); +const paths = envPaths("myn", { suffix: "core" }); +const configCore = join(paths.config, "testnet"); +const configCrypto = join(configCore, "crypto"); let cli; beforeEach(() => (cli = new Console())); @@ -24,7 +26,7 @@ describe("GenerateCommand", () => { await cli .withFlags({ - network: "mynet7", + network: "testnet", premine: "120000000000", delegates: "47", blocktime: "9", @@ -37,7 +39,7 @@ describe("GenerateCommand", () => { token: "myn", symbol: "my", explorer: "myex.io", - distribute: true, + distribute: "true", }) .execute(Command); @@ -59,7 +61,7 @@ describe("GenerateCommand", () => { await expect( cli .withFlags({ - network: "mynet7", + network: "testnet", premine: "120000000000", delegates: "47", blocktime: "9", @@ -84,7 +86,7 @@ describe("GenerateCommand", () => { await expect( cli .withFlags({ - network: "mynet7", + network: "testnet", premine: "120000000000", delegates: "47", blocktime: "9", @@ -103,7 +105,9 @@ describe("GenerateCommand", () => { ).rejects.toThrow(`${configCrypto} already exists.`); }); - it("should throw if some properties are not provided", async () => { + + + it("should throw if some prompt properties are not provided", async () => { prompts.inject([ undefined, undefined, @@ -125,7 +129,7 @@ describe("GenerateCommand", () => { it("should throw if the properties are not confirmed", async () => { prompts.inject([ - "mynet7", + "testnet", "120000000000", "47", "9", @@ -147,7 +151,7 @@ describe("GenerateCommand", () => { it("should throw if any property is undefined", async () => { prompts.inject([ - "mynet7", + "testnet", "120000000000", "47", "9", @@ -175,7 +179,7 @@ describe("GenerateCommand", () => { const copyFileSync = jest.spyOn(fs, "copyFileSync").mockImplementation(); prompts.inject([ - "mynet7", + "testnet", "120000000000", "47", "9", @@ -214,7 +218,7 @@ describe("GenerateCommand", () => { const copyFileSync = jest.spyOn(fs, "copyFileSync").mockImplementation(); prompts.inject([ - "mynet7", + "testnet", "120000000000", "47", "9", diff --git a/__tests__/unit/core/commands/relay-run.test.ts b/__tests__/unit/core/commands/relay-run.test.ts index 3f9d7a2afb..56386674b2 100644 --- a/__tests__/unit/core/commands/relay-run.test.ts +++ b/__tests__/unit/core/commands/relay-run.test.ts @@ -1,5 +1,6 @@ import { Command } from "@packages/core/src/commands/relay-run"; + import { executeCommand } from "../__support__/app"; const app = { @@ -22,7 +23,13 @@ describe("RunCommand", () => { const spyBootstrap = jest.spyOn(app, "bootstrap").mockImplementation(undefined); const spyBoot = jest.spyOn(app, "boot").mockImplementation(undefined); - await executeCommand(Command); + executeCommand(Command); + + await new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 50); + }); expect(spyBootstrap).toHaveBeenCalled(); expect(spyBoot).toHaveBeenCalled(); diff --git a/packages/core/bin/run b/packages/core/bin/run index eed0cba247..56d34fb14d 100755 --- a/packages/core/bin/run +++ b/packages/core/bin/run @@ -28,6 +28,8 @@ const main = async () => { const CommandLineInterface = new (require("../dist").CommandLineInterface)(process.argv.slice(2)); await CommandLineInterface.execute(); + + process.exit(0) } catch (error) { if (error.name !== "FatalException") { console.error(error) diff --git a/packages/core/src/commands/core-run.ts b/packages/core/src/commands/core-run.ts index 8ae4fda705..f588d3d9c5 100644 --- a/packages/core/src/commands/core-run.ts +++ b/packages/core/src/commands/core-run.ts @@ -78,5 +78,8 @@ export class Command extends Commands.Command { }, }, }); + + // Prevent resolving execute method + return new Promise(() => {}); } } diff --git a/packages/core/src/commands/forger-run.ts b/packages/core/src/commands/forger-run.ts index 1a83b884e6..817848db0c 100644 --- a/packages/core/src/commands/forger-run.ts +++ b/packages/core/src/commands/forger-run.ts @@ -59,5 +59,8 @@ export class Command extends Commands.Command { "@arkecosystem/core-forger": await buildBIP38(flags, this.app.getCorePath("config")), }, }); + + // Prevent resolving execute method + return new Promise(() => {}); } } diff --git a/packages/core/src/commands/network-generate.ts b/packages/core/src/commands/network-generate.ts index 806f797da4..b4dac39bec 100644 --- a/packages/core/src/commands/network-generate.ts +++ b/packages/core/src/commands/network-generate.ts @@ -3,8 +3,9 @@ import { Crypto, Identities, Interfaces, Transactions, Utils } from "@arkecosyst import Joi from "@hapi/joi"; import { generateMnemonic } from "bip39"; import ByteBuffer from "bytebuffer"; +import envPaths from "env-paths"; import { copyFileSync, ensureDirSync, existsSync, writeFileSync, writeJSONSync } from "fs-extra"; -import { resolve } from "path"; +import { join, resolve } from "path"; import prompts from "prompts"; interface Wallet { @@ -45,6 +46,17 @@ export class Command extends Commands.Command { */ public requiresNetwork: boolean = false; + private defaults = { + premine: "12500000000000000", + delegates: 51, + blocktime: 8, + maxTxPerBlock: 150, + maxBlockPayload: 2097152, + rewardHeight: 75600, + rewardAmount: "200000000", + distribute: false, + }; + /** * Configure the console command. * @@ -54,31 +66,23 @@ export class Command extends Commands.Command { public configure(): void { this.definition .setFlag("network", "The name of the network.", Joi.string()) - .setFlag( - "premine", - "The number of pre-mined tokens.", - Joi.alternatives().try(Joi.string(), Joi.number()).default("12500000000000000"), - ) - .setFlag("delegates", "The number of delegates to generate.", Joi.number().default(51)) - .setFlag("blocktime", "The network blocktime.", Joi.number().default(8)) - .setFlag("maxTxPerBlock", "The maximum number of transactions per block.", Joi.number().default(150)) - .setFlag("maxBlockPayload", "The maximum payload length by block.", Joi.number().default(2097152)) - .setFlag("rewardHeight", "The height at which delegate block reward starts.", Joi.number().default(75600)) + .setFlag("premine", "The number of pre-mined tokens.", Joi.alternatives().try(Joi.string(), Joi.number())) + .setFlag("delegates", "The number of delegates to generate.", Joi.number()) + .setFlag("blocktime", "The network blocktime.", Joi.number()) + .setFlag("maxTxPerBlock", "The maximum number of transactions per block.", Joi.number()) + .setFlag("maxBlockPayload", "The maximum payload length by block.", Joi.number()) + .setFlag("rewardHeight", "The height at which delegate block reward starts.", Joi.number()) .setFlag( "rewardAmount", "The number of the block reward per forged block.", - Joi.alternatives().try(Joi.string(), Joi.number()).default("200000000"), + Joi.alternatives().try(Joi.string(), Joi.number()), ) .setFlag("pubKeyHash", "The public key hash.", Joi.number()) .setFlag("wif", "The WIF (Wallet Import Format) that should be used.", Joi.number()) .setFlag("token", "The name that is attributed to the token on the network.", Joi.string()) .setFlag("symbol", "The character that is attributed to the token on the network.", Joi.string()) .setFlag("explorer", "The URL that hosts the network explorer.", Joi.string()) - .setFlag( - "distribute", - "Distribute the premine evenly between all delegates?", - Joi.boolean().default(false), - ); + .setFlag("distribute", "Distribute the premine evenly between all delegates?", Joi.boolean()); } /** @@ -92,7 +96,9 @@ export class Command extends Commands.Command { const flags: Contracts.AnyObject = this.getFlags(); - if (!Object.keys(flagsDefinition).find((flagName) => !flags[flagName])) { + const allFlagsSet = !Object.keys(flagsDefinition).find((flagName) => flags[flagName] === undefined); + + if (allFlagsSet) { return this.generateNetwork(flags); } @@ -105,7 +111,7 @@ export class Command extends Commands.Command { type: stringFlags.includes(flagName) ? "text" : "number", name: flagName, message: flagsDefinition[flagName].description, - initial: `${flags[flagName]}`, + initial: `${this.defaults[flagName]}`, } as prompts.PromptObject), ) .concat({ @@ -120,19 +126,20 @@ export class Command extends Commands.Command { // and it is defined as a number in this.generateCryptoGenesisBlock() // If false or 0 are passed intentionally, this would fail (despite all flags being provided). if (Object.keys(flagsDefinition).find((flagName) => response[flagName] === undefined)) { - this.components.fatal("Please provide all flags and try again!"); + throw new Error("Please provide all flags and try again!"); } if (!response.confirm) { - this.components.fatal("You'll need to confirm the input to continue."); + throw new Error("You'll need to confirm the input to continue."); } await this.generateNetwork({ ...flags, ...response }); } private async generateNetwork(flags: Contracts.AnyObject): Promise { - const coreConfigDest: string = resolve(__dirname, `../../bin/config/${flags.network}`); - const cryptoConfigDest: string = resolve(__dirname, `../../../crypto/src/networks/${flags.network}`); + const paths = envPaths(flags.token, { suffix: "core" }); + const coreConfigDest = join(paths.config, flags.network); + const cryptoConfigDest = join(coreConfigDest, "crypto"); const delegates: any[] = this.generateCoreDelegates(flags.delegates, flags.pubKeyHash); @@ -227,9 +234,12 @@ export class Command extends Commands.Command { { spaces: 4 }, ); - copyFileSync(resolve(coreConfigDest, "../testnet/.env"), resolve(coreConfigDest, ".env")); + copyFileSync(resolve(__dirname, "../../bin/config/testnet/.env"), resolve(coreConfigDest, ".env")); - copyFileSync(resolve(coreConfigDest, "../testnet/app.json"), resolve(coreConfigDest, "app.json")); + copyFileSync( + resolve(__dirname, "../../bin/config/testnet/app.json"), + resolve(coreConfigDest, "app.json"), + ); }, }, ]); diff --git a/packages/core/src/commands/relay-run.ts b/packages/core/src/commands/relay-run.ts index 531b81eb2c..b0c4a7cc91 100644 --- a/packages/core/src/commands/relay-run.ts +++ b/packages/core/src/commands/relay-run.ts @@ -62,5 +62,8 @@ export class Command extends Commands.Command { }, }, }); + + // Prevent resolving execute method + return new Promise(() => {}); } }