/
app_env.ts
126 lines (108 loc) · 3.78 KB
/
app_env.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import { APIClient, createIPCClient } from '@liskhq/lisk-api-client';
import { codec } from '@liskhq/lisk-codec';
import { objects } from '@liskhq/lisk-utils';
import { homedir } from 'os';
import { existsSync, rmdirSync } from 'fs-extra';
import { defaultConfig } from './fixtures';
import { PartialApplicationConfig } from '../types';
import { Application } from '../application';
import { BaseModule } from '../modules';
import { RPC_MODES } from '../constants';
import { BasePlugin } from '../plugins/base_plugin';
interface ApplicationEnvConfig {
modules: BaseModule[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
plugins?: BasePlugin<any>[];
config?: PartialApplicationConfig;
genesisBlockJSON?: Record<string, unknown>;
}
export class ApplicationEnv {
private _application!: Application;
private _dataPath!: string;
private _ipcClient!: APIClient;
public constructor(appConfig: ApplicationEnvConfig) {
this._initApplication(appConfig);
}
public get application(): Application {
return this._application;
}
public get ipcClient(): APIClient {
return this._ipcClient;
}
public get dataPath(): string {
return this._dataPath;
}
public async startApplication(): Promise<void> {
const genesisBlock = await this._application.generateGenesisBlock({
assets: [],
chainID: Buffer.from(this._application.config.genesis.chainID, 'hex'),
});
this._application.config.genesis.block.blob = genesisBlock.getBytes().toString('hex');
await Promise.race([
this._application.run(),
new Promise(resolve => setTimeout(resolve, 3000)),
]);
// Only start client when ipc is enabled
if (this._application.config.rpc.modes.includes(RPC_MODES.IPC)) {
this._ipcClient = await createIPCClient(this._dataPath);
}
}
public async stopApplication(options: { clearDB: boolean } = { clearDB: true }): Promise<void> {
if (options.clearDB) {
rmdirSync(this._dataPath);
}
if (this._application.config.rpc.modes.includes(RPC_MODES.IPC)) {
await this._ipcClient.disconnect();
}
await this._application.shutdown();
}
private _initApplication(appConfig: ApplicationEnvConfig): Application {
// As we can call this function with different configuration
// so we need to make sure existing schemas are already clear
codec.clearCache();
// In order for application to start forging, update force to true
const config = objects.mergeDeep({}, defaultConfig, appConfig.config ?? {});
const application = new Application(config as PartialApplicationConfig);
appConfig.modules.map(module => application.registerModule(module));
appConfig.plugins?.map(plugin => application.registerPlugin(plugin));
this._dataPath = application.config.system.dataPath;
this._application = application;
return application;
}
}
export const createDefaultApplicationEnv = (
appEnvConfig: Partial<ApplicationEnvConfig>,
): ApplicationEnv => {
const dataPath = (appEnvConfig.config?.system?.dataPath ?? defaultConfig.system.dataPath).replace(
'~',
homedir(),
);
// Ensure directory is cleaned for each application env
if (existsSync(dataPath)) {
rmdirSync(dataPath, { recursive: true });
}
const modules: BaseModule[] = [];
for (const mod of appEnvConfig.modules ?? []) {
modules.push(mod);
}
const appEnv = new ApplicationEnv({
...appEnvConfig,
modules,
genesisBlockJSON: {},
});
return appEnv;
};