UNPKG

lisk-framework

Version:

Lisk blockchain application platform

79 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createDefaultApplicationEnv = exports.ApplicationEnv = void 0; const lisk_api_client_1 = require("@liskhq/lisk-api-client"); const lisk_codec_1 = require("@liskhq/lisk-codec"); const lisk_utils_1 = require("@liskhq/lisk-utils"); const os_1 = require("os"); const fs_extra_1 = require("fs-extra"); const fixtures_1 = require("./fixtures"); const application_1 = require("../application"); const constants_1 = require("../constants"); class ApplicationEnv { constructor(appConfig) { this._initApplication(appConfig); } get application() { return this._application; } get ipcClient() { return this._ipcClient; } get dataPath() { return this._dataPath; } async startApplication() { 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)), ]); if (this._application.config.rpc.modes.includes(constants_1.RPC_MODES.IPC)) { this._ipcClient = await (0, lisk_api_client_1.createIPCClient)(this._dataPath); } } async stopApplication(options = { clearDB: true }) { if (options.clearDB) { (0, fs_extra_1.rmdirSync)(this._dataPath); } if (this._application.config.rpc.modes.includes(constants_1.RPC_MODES.IPC)) { await this._ipcClient.disconnect(); } await this._application.shutdown(); } _initApplication(appConfig) { var _a, _b; lisk_codec_1.codec.clearCache(); const config = lisk_utils_1.objects.mergeDeep({}, fixtures_1.defaultConfig, (_a = appConfig.config) !== null && _a !== void 0 ? _a : {}); const application = new application_1.Application(config); appConfig.modules.map(module => application.registerModule(module)); (_b = appConfig.plugins) === null || _b === void 0 ? void 0 : _b.map(plugin => application.registerPlugin(plugin)); this._dataPath = application.config.system.dataPath; this._application = application; return application; } } exports.ApplicationEnv = ApplicationEnv; const createDefaultApplicationEnv = (appEnvConfig) => { var _a, _b, _c, _d; const dataPath = ((_c = (_b = (_a = appEnvConfig.config) === null || _a === void 0 ? void 0 : _a.system) === null || _b === void 0 ? void 0 : _b.dataPath) !== null && _c !== void 0 ? _c : fixtures_1.defaultConfig.system.dataPath).replace('~', (0, os_1.homedir)()); if ((0, fs_extra_1.existsSync)(dataPath)) { (0, fs_extra_1.rmdirSync)(dataPath, { recursive: true }); } const modules = []; for (const mod of (_d = appEnvConfig.modules) !== null && _d !== void 0 ? _d : []) { modules.push(mod); } const appEnv = new ApplicationEnv({ ...appEnvConfig, modules, genesisBlockJSON: {}, }); return appEnv; }; exports.createDefaultApplicationEnv = createDefaultApplicationEnv; //# sourceMappingURL=app_env.js.map