Skip to content

Commit

Permalink
feat(core-test-framework): allow use of custom crypto configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Dec 4, 2019
1 parent daadf35 commit 84aee92
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 91 deletions.
20 changes: 14 additions & 6 deletions packages/core-test-framework/src/app/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Application, Container } from "@arkecosystem/core-kernel";
import { Application, Container, Types } from "@arkecosystem/core-kernel";
import { Interfaces } from "@arkecosystem/crypto";
import { Paths } from "env-paths";

Expand All @@ -19,13 +19,13 @@ export interface CoreOptions {
plugins?: {
options?: Record<string, Record<string, any>>;
};
peers?: {};
delegates?: {};
environment?: {};
app?: {};
peers?: Types.JsonObject;
delegates?: Types.JsonObject;
environment?: Types.JsonObject;
app?: Types.JsonObject;
}

export interface CryptoOptions {
export interface CryptoFlags {
network: string;
premine: string;
delegates: number;
Expand All @@ -42,6 +42,14 @@ export interface CryptoOptions {
distribute: boolean;
}

export interface CryptoOptions {
flags: CryptoFlags;
exceptions?: Types.JsonObject;
genesisBlock?: Types.JsonObject;
milestones?: Types.JsonObject;
network?: Types.JsonObject;
}

export interface SandboxOptions {
core: CoreOptions;
crypto: CryptoOptions;
Expand Down
18 changes: 10 additions & 8 deletions packages/core-test-framework/src/app/generators/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export class CoreGenerator extends Generator {

ensureDirSync(this.destination);

this.createPeers();
this.writePeers();

this.createDelegates(this.generateCoreDelegates(this.options.crypto.delegates, this.options.crypto.pubKeyHash));
this.writeDelegates(
this.generateCoreDelegates(this.options.crypto.flags.delegates, this.options.crypto.flags.pubKeyHash),
);

this.createEnvironment();
this.writeEnvironment();

this.createApplication();
this.writeApplication();

return {
root: this.destination,
Expand All @@ -52,7 +54,7 @@ export class CoreGenerator extends Generator {
* @private
* @memberof CoreGenerator
*/
private createPeers(): void {
private writePeers(): void {
const filePath: string = resolve(this.destination, "peers.json");

if (this.options.core.peers) {
Expand All @@ -67,7 +69,7 @@ export class CoreGenerator extends Generator {
* @param {*} delegates
* @memberof CoreGenerator
*/
private createDelegates(delegates): void {
private writeDelegates(delegates): void {
const filePath: string = resolve(this.destination, "delegates.json");

if (this.options.core.delegates) {
Expand All @@ -81,7 +83,7 @@ export class CoreGenerator extends Generator {
* @private
* @memberof CoreGenerator
*/
private createEnvironment(): void {
private writeEnvironment(): void {
const filePath: string = resolve(this.destination, ".env");

if (this.options.core.environment) {
Expand All @@ -95,7 +97,7 @@ export class CoreGenerator extends Generator {
* @private
* @memberof CoreGenerator
*/
private createApplication(): void {
private writeApplication(): void {
const filePath: string = resolve(this.destination, "app.json");

if (this.options.core.app) {
Expand Down
164 changes: 116 additions & 48 deletions packages/core-test-framework/src/app/generators/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,53 @@ import { dirSync } from "tmp";

import { CryptoConfigPaths, Wallet } from "../contracts";
import { Generator } from "./generator";
import { Types } from "@arkecosystem/core-kernel";

export class CryptoGenerator extends Generator {
/**
* @private
* @type {string}
* @memberof CoreGenerator
*/
private destination!: string;

/**
* @returns {CoreConfigPaths}
* @memberof CoreGenerator
*/
public generate(): CryptoConfigPaths {
const cryptoConfigDest: string = resolve(__dirname, `${dirSync().name}/${this.options.crypto.network}`);
this.destination = resolve(__dirname, `${dirSync().name}/${this.options.crypto.flags.network}`);

if (existsSync(cryptoConfigDest)) {
throw new Error(`${cryptoConfigDest} already exists.`);
if (existsSync(this.destination)) {
throw new Error(`${this.destination} already exists.`);
}

ensureDirSync(cryptoConfigDest);
ensureDirSync(this.destination);

const delegates: any[] = this.generateCoreDelegates(
this.options.crypto.delegates,
this.options.crypto.pubKeyHash,
);

const genesisBlock = this.generateGenesisBlock(
this.createWallet(this.options.crypto.pubKeyHash),
delegates,
this.options.crypto.pubKeyHash,
this.options.crypto.premine,
this.options.crypto.distribute,
);
const genesisBlock =
this.options.crypto.genesisBlock ??
this.generateGenesisBlock(
this.createWallet(this.options.crypto.flags.pubKeyHash),
this.generateCoreDelegates(this.options.crypto.flags.delegates, this.options.crypto.flags.pubKeyHash),
this.options.crypto.flags.pubKeyHash,
this.options.crypto.flags.premine,
this.options.crypto.flags.distribute,
);

writeJSONSync(
resolve(cryptoConfigDest, "network.json"),
this.generateNetwork(
this.options.crypto.network,
this.options.crypto.pubKeyHash,
genesisBlock.payloadHash,
this.options.crypto.wif,
this.options.crypto.token,
this.options.crypto.symbol,
this.options.crypto.explorer,
),
{ spaces: 4 },
);
this.writeExceptions();

writeJSONSync(
resolve(cryptoConfigDest, "milestones.json"),
this.generateMilestones(
this.options.crypto.delegates,
this.options.crypto.blocktime,
this.options.crypto.maxTxPerBlock,
this.options.crypto.maxBlockPayload,
this.options.crypto.rewardHeight,
this.options.crypto.rewardAmount,
),
{ spaces: 4 },
);
this.writeGenesisBlock(genesisBlock);

writeJSONSync(resolve(cryptoConfigDest, "genesisBlock.json"), genesisBlock, { spaces: 4 });
this.writeMilestones(genesisBlock);

writeJSONSync(resolve(cryptoConfigDest, "exceptions.json"), {});
this.writeNetwork(genesisBlock.payloadHash);

return {
root: cryptoConfigDest,
exceptions: resolve(cryptoConfigDest, "exceptions.json"),
genesisBlock: resolve(cryptoConfigDest, "genesisBlock.json"),
milestones: resolve(cryptoConfigDest, "milestones.json"),
network: resolve(cryptoConfigDest, "network.json"),
root: this.destination,
exceptions: resolve(this.destination, "exceptions.json"),
genesisBlock: resolve(this.destination, "genesisBlock.json"),
milestones: resolve(this.destination, "milestones.json"),
network: resolve(this.destination, "network.json"),
};
}

Expand Down Expand Up @@ -355,4 +341,86 @@ export class CryptoGenerator extends Generator {

return byteBuffer.toBuffer();
}

/**
* @private
* @memberof CryptoGenerator
*/
private writeExceptions(): void {
const filePath: string = resolve(this.destination, "exceptions.json");

if (this.options.crypto.exceptions) {
writeJSONSync(filePath, this.options.crypto.exceptions, { spaces: 4 });
} else {
writeJSONSync(resolve(this.destination, "exceptions.json"), {});
}
}

/**
* @private
* @param {Types.JsonObject} genesisBlock
* @memberof CryptoGenerator
*/
private writeGenesisBlock(genesisBlock: Types.JsonObject): void {
const filePath: string = resolve(this.destination, "genesisBlock.json");

if (this.options.crypto.genesisBlock) {
writeJSONSync(filePath, this.options.crypto.genesisBlock, { spaces: 4 });
} else {
writeJSONSync(filePath, genesisBlock, { spaces: 4 });
}
}

/**
* @private
* @param {Types.JsonObject} genesisBlock
* @memberof CryptoGenerator
*/
private writeMilestones(genesisBlock: Types.JsonObject): void {
const filePath: string = resolve(this.destination, "milestones.json");

if (this.options.crypto.milestones) {
writeJSONSync(filePath, this.options.crypto.milestones, { spaces: 4 });
} else {
writeJSONSync(
resolve(this.destination, "milestones.json"),
this.generateMilestones(
this.options.crypto.flags.delegates,
this.options.crypto.flags.blocktime,
this.options.crypto.flags.maxTxPerBlock,
this.options.crypto.flags.maxBlockPayload,
this.options.crypto.flags.rewardHeight,
this.options.crypto.flags.rewardAmount,
),
{ spaces: 4 },
);
}
}

/**
* @private
* @param {string} payloadHash
* @memberof CryptoGenerator
*/
private writeNetwork(payloadHash: string): void {
const filePath: string = resolve(this.destination, "network.json");

if (this.options.crypto.network) {
writeJSONSync(filePath, this.options.crypto.network, { spaces: 4 });
} else {
writeJSONSync(
filePath,
this.generateNetwork(
this.options.crypto.flags.network,
this.options.crypto.flags.pubKeyHash,
payloadHash,
this.options.crypto.flags.wif,
this.options.crypto.flags.token,
this.options.crypto.flags.symbol,
this.options.crypto.flags.explorer,
),
{ spaces: 4 },
);
}
}
}
32 changes: 17 additions & 15 deletions packages/core-test-framework/src/app/generators/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@ export abstract class Generator {
protected options: SandboxOptions = {
core: {},
crypto: {
network: "unitnet",
premine: "15300000000000000",
delegates: 51,
blocktime: 8,
maxTxPerBlock: 150,
maxBlockPayload: 2097152,
rewardHeight: 75600,
rewardAmount: 200000000,
pubKeyHash: 23,
wif: 186,
token: "UARK",
symbol: "UѦ",
explorer: "http://uexplorer.ark.io",
distribute: true,
flags: {
network: "unitnet",
premine: "15300000000000000",
delegates: 51,
blocktime: 8,
maxTxPerBlock: 150,
maxBlockPayload: 2097152,
rewardHeight: 75600,
rewardAmount: 200000000,
pubKeyHash: 23,
wif: 186,
token: "UARK",
symbol: "UѦ",
explorer: "http://uexplorer.ark.io",
distribute: true,
}
},
};

/**
* @param {SandboxOptions} options
* @memberof Generator
*/
public constructor(options?: SandboxOptions) {
public constructor (options?: SandboxOptions) {
if (options) {
this.options = { ...this.options, ...options };
}
Expand Down
30 changes: 16 additions & 14 deletions packages/core-test-framework/src/app/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ export class Sandbox {
private readonly options: SandboxOptions = {
core: {},
crypto: {
network: "unitnet",
premine: "15300000000000000",
delegates: 51,
blocktime: 8,
maxTxPerBlock: 150,
maxBlockPayload: 2097152,
rewardHeight: 75600,
rewardAmount: 200000000,
pubKeyHash: 23,
wif: 186,
token: "UARK",
symbol: "UѦ",
explorer: "http://uexplorer.ark.io",
distribute: true,
flags: {
network: "unitnet",
premine: "15300000000000000",
delegates: 51,
blocktime: 8,
maxTxPerBlock: 150,
maxBlockPayload: 2097152,
rewardHeight: 75600,
rewardAmount: 200000000,
pubKeyHash: 23,
wif: 186,
token: "UARK",
symbol: "UѦ",
explorer: "http://uexplorer.ark.io",
distribute: true,
},
},
};

Expand Down

0 comments on commit 84aee92

Please sign in to comment.