UNPKG

@hashgraph/hedera-local

Version:

Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).

113 lines 5.68 kB
"use strict"; // SPDX-License-Identifier: Apache-2.0 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CleanUpState = void 0; const fs_1 = require("fs"); const js_yaml_1 = __importDefault(require("js-yaml")); const path_1 = require("path"); const originalNodeConfiguration_json_1 = __importDefault(require("../configuration/originalNodeConfiguration.json")); const LoggerService_1 = require("../services/LoggerService"); const ServiceLocator_1 = require("../services/ServiceLocator"); const CLIService_1 = require("../services/CLIService"); const EventType_1 = require("../types/EventType"); const constants_1 = require("../constants"); /** * The `CleanUpState` class is responsible for cleaning up and reverting unneeded changes to files. * * It implements the `IState` interface and provides methods to subscribe an observer, start the cleanup process, and revert properties of the mirror node and the consensus node. * * @class * @implements {IState} */ class CleanUpState { /** * Initializes a new instance of the CleanUpState class. */ constructor() { this.stateName = CleanUpState.name; this.cliOptions = ServiceLocator_1.ServiceLocator.Current.get(CLIService_1.CLIService.name).getCurrentArgv(); this.logger = ServiceLocator_1.ServiceLocator.Current.get(LoggerService_1.LoggerService.name); this.logger.trace('Clean Up State Initialized!', this.stateName); } /** * Subscribes an observer to the `CleanUpState`. * * @param {IObserver} observer - The observer to subscribe. */ subscribe(observer) { this.observer = observer; } /** * Starts the cleanup process. * * This method initiates the cleanup procedure, tries to revert unneeded changes to files, and updates the observer when the cleanup is finished. * * @returns {Promise<void>} */ onStart() { return __awaiter(this, void 0, void 0, function* () { this.logger.info(`${constants_1.LOADING} Initiating clean up procedure. Trying to revert unneeded changes to files...`, this.stateName); this.revertNodeProperties(); this.revertMirrorNodeProperties(); if (this.observer) { this.observer.update(EventType_1.EventType.Finish); } }); } /** * Reverts the properties of the mirror node. * * This method cleans up unneeded mirror node properties and writes the updated properties back to the file. * @private */ revertMirrorNodeProperties() { this.logger.trace('Clean up unneeded mirror node properties...', this.stateName); const propertiesFilePath = (0, path_1.join)(this.cliOptions.workDir, 'compose-network/mirror-node/application.yml'); if (!(0, fs_1.existsSync)(propertiesFilePath)) { this.logger.trace(`Mirror Node Properties File doesn't exist at path ${propertiesFilePath}`, this.stateName); return; } const application = js_yaml_1.default.load((0, fs_1.readFileSync)(propertiesFilePath).toString()); delete application.hedera.mirror.importer.dataPath; delete application.hedera.mirror.importer.downloader.sources; delete application.hedera.mirror.importer.downloader.local; application.hedera.mirror.monitor.nodes = originalNodeConfiguration_json_1.default.fullNodeProperties; (0, fs_1.writeFileSync)(propertiesFilePath, js_yaml_1.default.dump(application, { lineWidth: 256 })); this.logger.info(`${constants_1.CHECK_SUCCESS} Clean up of mirror node properties finished.`, this.stateName); } /** * Reverts the properties of the consensus node. * * This method cleans up unneeded bootstrap properties and writes the original properties back to the file. * * @private */ revertNodeProperties() { this.logger.trace('Clean up unneeded bootstrap properties.', this.stateName); const propertiesFilePath = (0, path_1.join)(this.cliOptions.workDir, 'compose-network/network-node/data/config/bootstrap.properties'); if (!(0, fs_1.existsSync)(propertiesFilePath)) { this.logger.trace(`Node Properties File doesn't exist at path ${propertiesFilePath}`, this.stateName); return; } let originalProperties = ''; originalNodeConfiguration_json_1.default.bootsrapProperties.forEach(property => { originalProperties = originalProperties.concat(`${property.key}=${property.value}\n`); }); (0, fs_1.writeFileSync)(propertiesFilePath, originalProperties, { flag: 'w' }); this.logger.info(`${constants_1.CHECK_SUCCESS} Clean up of consensus node properties finished.`, this.stateName); } } exports.CleanUpState = CleanUpState; //# sourceMappingURL=CleanUpState.js.map