Skip to content

Commit 84aee92

Browse files
authored
feat(core-test-framework): allow use of custom crypto configuration
1 parent daadf35 commit 84aee92

File tree

5 files changed

+173
-91
lines changed

5 files changed

+173
-91
lines changed

packages/core-test-framework/src/app/contracts.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Container } from "@arkecosystem/core-kernel";
1+
import { Application, Container, Types } from "@arkecosystem/core-kernel";
22
import { Interfaces } from "@arkecosystem/crypto";
33
import { Paths } from "env-paths";
44

@@ -19,13 +19,13 @@ export interface CoreOptions {
1919
plugins?: {
2020
options?: Record<string, Record<string, any>>;
2121
};
22-
peers?: {};
23-
delegates?: {};
24-
environment?: {};
25-
app?: {};
22+
peers?: Types.JsonObject;
23+
delegates?: Types.JsonObject;
24+
environment?: Types.JsonObject;
25+
app?: Types.JsonObject;
2626
}
2727

28-
export interface CryptoOptions {
28+
export interface CryptoFlags {
2929
network: string;
3030
premine: string;
3131
delegates: number;
@@ -42,6 +42,14 @@ export interface CryptoOptions {
4242
distribute: boolean;
4343
}
4444

45+
export interface CryptoOptions {
46+
flags: CryptoFlags;
47+
exceptions?: Types.JsonObject;
48+
genesisBlock?: Types.JsonObject;
49+
milestones?: Types.JsonObject;
50+
network?: Types.JsonObject;
51+
}
52+
4553
export interface SandboxOptions {
4654
core: CoreOptions;
4755
crypto: CryptoOptions;

packages/core-test-framework/src/app/generators/core.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ export class CoreGenerator extends Generator {
3131

3232
ensureDirSync(this.destination);
3333

34-
this.createPeers();
34+
this.writePeers();
3535

36-
this.createDelegates(this.generateCoreDelegates(this.options.crypto.delegates, this.options.crypto.pubKeyHash));
36+
this.writeDelegates(
37+
this.generateCoreDelegates(this.options.crypto.flags.delegates, this.options.crypto.flags.pubKeyHash),
38+
);
3739

38-
this.createEnvironment();
40+
this.writeEnvironment();
3941

40-
this.createApplication();
42+
this.writeApplication();
4143

4244
return {
4345
root: this.destination,
@@ -52,7 +54,7 @@ export class CoreGenerator extends Generator {
5254
* @private
5355
* @memberof CoreGenerator
5456
*/
55-
private createPeers(): void {
57+
private writePeers(): void {
5658
const filePath: string = resolve(this.destination, "peers.json");
5759

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

7375
if (this.options.core.delegates) {
@@ -81,7 +83,7 @@ export class CoreGenerator extends Generator {
8183
* @private
8284
* @memberof CoreGenerator
8385
*/
84-
private createEnvironment(): void {
86+
private writeEnvironment(): void {
8587
const filePath: string = resolve(this.destination, ".env");
8688

8789
if (this.options.core.environment) {
@@ -95,7 +97,7 @@ export class CoreGenerator extends Generator {
9597
* @private
9698
* @memberof CoreGenerator
9799
*/
98-
private createApplication(): void {
100+
private writeApplication(): void {
99101
const filePath: string = resolve(this.destination, "app.json");
100102

101103
if (this.options.core.app) {

packages/core-test-framework/src/app/generators/crypto.ts

Lines changed: 116 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,53 @@ import { dirSync } from "tmp";
66

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

1011
export class CryptoGenerator extends Generator {
12+
/**
13+
* @private
14+
* @type {string}
15+
* @memberof CoreGenerator
16+
*/
17+
private destination!: string;
18+
19+
/**
20+
* @returns {CoreConfigPaths}
21+
* @memberof CoreGenerator
22+
*/
1123
public generate(): CryptoConfigPaths {
12-
const cryptoConfigDest: string = resolve(__dirname, `${dirSync().name}/${this.options.crypto.network}`);
24+
this.destination = resolve(__dirname, `${dirSync().name}/${this.options.crypto.flags.network}`);
1325

14-
if (existsSync(cryptoConfigDest)) {
15-
throw new Error(`${cryptoConfigDest} already exists.`);
26+
if (existsSync(this.destination)) {
27+
throw new Error(`${this.destination} already exists.`);
1628
}
1729

18-
ensureDirSync(cryptoConfigDest);
30+
ensureDirSync(this.destination);
1931

20-
const delegates: any[] = this.generateCoreDelegates(
21-
this.options.crypto.delegates,
22-
this.options.crypto.pubKeyHash,
23-
);
24-
25-
const genesisBlock = this.generateGenesisBlock(
26-
this.createWallet(this.options.crypto.pubKeyHash),
27-
delegates,
28-
this.options.crypto.pubKeyHash,
29-
this.options.crypto.premine,
30-
this.options.crypto.distribute,
31-
);
32+
const genesisBlock =
33+
this.options.crypto.genesisBlock ??
34+
this.generateGenesisBlock(
35+
this.createWallet(this.options.crypto.flags.pubKeyHash),
36+
this.generateCoreDelegates(this.options.crypto.flags.delegates, this.options.crypto.flags.pubKeyHash),
37+
this.options.crypto.flags.pubKeyHash,
38+
this.options.crypto.flags.premine,
39+
this.options.crypto.flags.distribute,
40+
);
3241

33-
writeJSONSync(
34-
resolve(cryptoConfigDest, "network.json"),
35-
this.generateNetwork(
36-
this.options.crypto.network,
37-
this.options.crypto.pubKeyHash,
38-
genesisBlock.payloadHash,
39-
this.options.crypto.wif,
40-
this.options.crypto.token,
41-
this.options.crypto.symbol,
42-
this.options.crypto.explorer,
43-
),
44-
{ spaces: 4 },
45-
);
42+
this.writeExceptions();
4643

47-
writeJSONSync(
48-
resolve(cryptoConfigDest, "milestones.json"),
49-
this.generateMilestones(
50-
this.options.crypto.delegates,
51-
this.options.crypto.blocktime,
52-
this.options.crypto.maxTxPerBlock,
53-
this.options.crypto.maxBlockPayload,
54-
this.options.crypto.rewardHeight,
55-
this.options.crypto.rewardAmount,
56-
),
57-
{ spaces: 4 },
58-
);
44+
this.writeGenesisBlock(genesisBlock);
5945

60-
writeJSONSync(resolve(cryptoConfigDest, "genesisBlock.json"), genesisBlock, { spaces: 4 });
46+
this.writeMilestones(genesisBlock);
6147

62-
writeJSONSync(resolve(cryptoConfigDest, "exceptions.json"), {});
48+
this.writeNetwork(genesisBlock.payloadHash);
6349

6450
return {
65-
root: cryptoConfigDest,
66-
exceptions: resolve(cryptoConfigDest, "exceptions.json"),
67-
genesisBlock: resolve(cryptoConfigDest, "genesisBlock.json"),
68-
milestones: resolve(cryptoConfigDest, "milestones.json"),
69-
network: resolve(cryptoConfigDest, "network.json"),
51+
root: this.destination,
52+
exceptions: resolve(this.destination, "exceptions.json"),
53+
genesisBlock: resolve(this.destination, "genesisBlock.json"),
54+
milestones: resolve(this.destination, "milestones.json"),
55+
network: resolve(this.destination, "network.json"),
7056
};
7157
}
7258

@@ -355,4 +341,86 @@ export class CryptoGenerator extends Generator {
355341

356342
return byteBuffer.toBuffer();
357343
}
344+
345+
/**
346+
* @private
347+
* @memberof CryptoGenerator
348+
*/
349+
private writeExceptions(): void {
350+
const filePath: string = resolve(this.destination, "exceptions.json");
351+
352+
if (this.options.crypto.exceptions) {
353+
writeJSONSync(filePath, this.options.crypto.exceptions, { spaces: 4 });
354+
} else {
355+
writeJSONSync(resolve(this.destination, "exceptions.json"), {});
356+
}
357+
}
358+
359+
/**
360+
* @private
361+
* @param {Types.JsonObject} genesisBlock
362+
* @memberof CryptoGenerator
363+
*/
364+
private writeGenesisBlock(genesisBlock: Types.JsonObject): void {
365+
const filePath: string = resolve(this.destination, "genesisBlock.json");
366+
367+
if (this.options.crypto.genesisBlock) {
368+
writeJSONSync(filePath, this.options.crypto.genesisBlock, { spaces: 4 });
369+
} else {
370+
writeJSONSync(filePath, genesisBlock, { spaces: 4 });
371+
}
372+
}
373+
374+
/**
375+
* @private
376+
* @param {Types.JsonObject} genesisBlock
377+
* @memberof CryptoGenerator
378+
*/
379+
private writeMilestones(genesisBlock: Types.JsonObject): void {
380+
const filePath: string = resolve(this.destination, "milestones.json");
381+
382+
if (this.options.crypto.milestones) {
383+
writeJSONSync(filePath, this.options.crypto.milestones, { spaces: 4 });
384+
} else {
385+
writeJSONSync(
386+
resolve(this.destination, "milestones.json"),
387+
this.generateMilestones(
388+
this.options.crypto.flags.delegates,
389+
this.options.crypto.flags.blocktime,
390+
this.options.crypto.flags.maxTxPerBlock,
391+
this.options.crypto.flags.maxBlockPayload,
392+
this.options.crypto.flags.rewardHeight,
393+
this.options.crypto.flags.rewardAmount,
394+
),
395+
{ spaces: 4 },
396+
);
397+
}
398+
}
399+
400+
/**
401+
* @private
402+
* @param {string} payloadHash
403+
* @memberof CryptoGenerator
404+
*/
405+
private writeNetwork(payloadHash: string): void {
406+
const filePath: string = resolve(this.destination, "network.json");
407+
408+
if (this.options.crypto.network) {
409+
writeJSONSync(filePath, this.options.crypto.network, { spaces: 4 });
410+
} else {
411+
writeJSONSync(
412+
filePath,
413+
this.generateNetwork(
414+
this.options.crypto.flags.network,
415+
this.options.crypto.flags.pubKeyHash,
416+
payloadHash,
417+
this.options.crypto.flags.wif,
418+
this.options.crypto.flags.token,
419+
this.options.crypto.flags.symbol,
420+
this.options.crypto.flags.explorer,
421+
),
422+
{ spaces: 4 },
423+
);
424+
}
425+
}
358426
}

packages/core-test-framework/src/app/generators/generator.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ export abstract class Generator {
1717
protected options: SandboxOptions = {
1818
core: {},
1919
crypto: {
20-
network: "unitnet",
21-
premine: "15300000000000000",
22-
delegates: 51,
23-
blocktime: 8,
24-
maxTxPerBlock: 150,
25-
maxBlockPayload: 2097152,
26-
rewardHeight: 75600,
27-
rewardAmount: 200000000,
28-
pubKeyHash: 23,
29-
wif: 186,
30-
token: "UARK",
31-
symbol: "UѦ",
32-
explorer: "http://uexplorer.ark.io",
33-
distribute: true,
20+
flags: {
21+
network: "unitnet",
22+
premine: "15300000000000000",
23+
delegates: 51,
24+
blocktime: 8,
25+
maxTxPerBlock: 150,
26+
maxBlockPayload: 2097152,
27+
rewardHeight: 75600,
28+
rewardAmount: 200000000,
29+
pubKeyHash: 23,
30+
wif: 186,
31+
token: "UARK",
32+
symbol: "UѦ",
33+
explorer: "http://uexplorer.ark.io",
34+
distribute: true,
35+
}
3436
},
3537
};
3638

3739
/**
3840
* @param {SandboxOptions} options
3941
* @memberof Generator
4042
*/
41-
public constructor(options?: SandboxOptions) {
43+
public constructor (options?: SandboxOptions) {
4244
if (options) {
4345
this.options = { ...this.options, ...options };
4446
}

packages/core-test-framework/src/app/sandbox.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,22 @@ export class Sandbox {
4646
private readonly options: SandboxOptions = {
4747
core: {},
4848
crypto: {
49-
network: "unitnet",
50-
premine: "15300000000000000",
51-
delegates: 51,
52-
blocktime: 8,
53-
maxTxPerBlock: 150,
54-
maxBlockPayload: 2097152,
55-
rewardHeight: 75600,
56-
rewardAmount: 200000000,
57-
pubKeyHash: 23,
58-
wif: 186,
59-
token: "UARK",
60-
symbol: "UѦ",
61-
explorer: "http://uexplorer.ark.io",
62-
distribute: true,
49+
flags: {
50+
network: "unitnet",
51+
premine: "15300000000000000",
52+
delegates: 51,
53+
blocktime: 8,
54+
maxTxPerBlock: 150,
55+
maxBlockPayload: 2097152,
56+
rewardHeight: 75600,
57+
rewardAmount: 200000000,
58+
pubKeyHash: 23,
59+
wif: 186,
60+
token: "UARK",
61+
symbol: "UѦ",
62+
explorer: "http://uexplorer.ark.io",
63+
distribute: true,
64+
},
6365
},
6466
};
6567

0 commit comments

Comments
 (0)