Skip to content

Commit

Permalink
fix(core): network:generate save to default location (#4138)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored Nov 3, 2020
1 parent 9c19a7a commit 3c0c4eb
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 40 deletions.
8 changes: 7 additions & 1 deletion __tests__/unit/core/commands/core-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion __tests__/unit/core/commands/forger-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 16 additions & 12 deletions __tests__/unit/core/commands/network-generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -24,7 +26,7 @@ describe("GenerateCommand", () => {

await cli
.withFlags({
network: "mynet7",
network: "testnet",
premine: "120000000000",
delegates: "47",
blocktime: "9",
Expand All @@ -37,7 +39,7 @@ describe("GenerateCommand", () => {
token: "myn",
symbol: "my",
explorer: "myex.io",
distribute: true,
distribute: "true",
})
.execute(Command);

Expand All @@ -59,7 +61,7 @@ describe("GenerateCommand", () => {
await expect(
cli
.withFlags({
network: "mynet7",
network: "testnet",
premine: "120000000000",
delegates: "47",
blocktime: "9",
Expand All @@ -84,7 +86,7 @@ describe("GenerateCommand", () => {
await expect(
cli
.withFlags({
network: "mynet7",
network: "testnet",
premine: "120000000000",
delegates: "47",
blocktime: "9",
Expand All @@ -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,
Expand All @@ -125,7 +129,7 @@ describe("GenerateCommand", () => {

it("should throw if the properties are not confirmed", async () => {
prompts.inject([
"mynet7",
"testnet",
"120000000000",
"47",
"9",
Expand All @@ -147,7 +151,7 @@ describe("GenerateCommand", () => {

it("should throw if any property is undefined", async () => {
prompts.inject([
"mynet7",
"testnet",
"120000000000",
"47",
"9",
Expand Down Expand Up @@ -175,7 +179,7 @@ describe("GenerateCommand", () => {
const copyFileSync = jest.spyOn(fs, "copyFileSync").mockImplementation();

prompts.inject([
"mynet7",
"testnet",
"120000000000",
"47",
"9",
Expand Down Expand Up @@ -214,7 +218,7 @@ describe("GenerateCommand", () => {
const copyFileSync = jest.spyOn(fs, "copyFileSync").mockImplementation();

prompts.inject([
"mynet7",
"testnet",
"120000000000",
"47",
"9",
Expand Down
9 changes: 8 additions & 1 deletion __tests__/unit/core/commands/relay-run.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from "@packages/core/src/commands/relay-run";


import { executeCommand } from "../__support__/app";

const app = {
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions packages/core/bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/commands/core-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ export class Command extends Commands.Command {
},
},
});

// Prevent resolving execute method
return new Promise(() => {});
}
}
3 changes: 3 additions & 0 deletions packages/core/src/commands/forger-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
}
}
60 changes: 35 additions & 25 deletions packages/core/src/commands/network-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand All @@ -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());
}

/**
Expand All @@ -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);
}

Expand All @@ -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<string>),
)
.concat({
Expand All @@ -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<void> {
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);

Expand Down Expand Up @@ -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"),
);
},
},
]);
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/commands/relay-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ export class Command extends Commands.Command {
},
},
});

// Prevent resolving execute method
return new Promise(() => {});
}
}

0 comments on commit 3c0c4eb

Please sign in to comment.