@hashgraph/hedera-local
Version:
Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).
119 lines • 6.23 kB
JavaScript
;
// 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.NetworkPrepState = void 0;
const sdk_1 = require("@hashgraph/sdk");
const shelljs_1 = __importDefault(require("shelljs"));
const ClientService_1 = require("../services/ClientService");
const LoggerService_1 = require("../services/LoggerService");
const ServiceLocator_1 = require("../services/ServiceLocator");
const EventType_1 = require("../types/EventType");
const DockerService_1 = require("../services/DockerService");
const constants_1 = require("../constants");
/**
* Represents the network preparation state of the Hedera Local Node.
* @implements {IState}
*/
class NetworkPrepState {
/**
* Represents the NetworkPrepState class.
* This class is responsible for initializing the network preparation state.
*/
constructor() {
this.stateName = NetworkPrepState.name;
this.logger = ServiceLocator_1.ServiceLocator.Current.get(LoggerService_1.LoggerService.name);
this.clientService = ServiceLocator_1.ServiceLocator.Current.get(ClientService_1.ClientService.name);
this.dockerService = ServiceLocator_1.ServiceLocator.Current.get(DockerService_1.DockerService.name);
this.logger.trace(constants_1.NETWORK_PREP_STATE_INIT_MESSAGE, this.stateName);
}
/**
* Subscribes an observer to receive updates from the network preparation state.
* @param {IOBserver} observer - The observer to subscribe.
*/
subscribe(observer) {
this.observer = observer;
}
/**
* Starts the network preparation process.
* @returns {Promise<void>} A promise that resolves when the network preparation is complete.
*/
onStart() {
return __awaiter(this, void 0, void 0, function* () {
this.logger.info(constants_1.NETWORK_PREP_STATE_STARTING_MESSAGE, this.stateName);
const client = this.clientService.getClient();
yield this.importFees(client);
yield this.waitForTopicCreation();
this.observer.update(EventType_1.EventType.Finish);
});
}
/**
* Imports fees and exchange rates into the network.
* @param {Client} client - The Hedera client.
* @returns {Promise<void>} A promise that resolves when the import is complete.
*/
importFees(client) {
return __awaiter(this, void 0, void 0, function* () {
this.logger.trace(constants_1.NETWORK_PREP_STATE_IMPORT_FEES_START, this.stateName);
const feesFileId = 111;
const exchangeRatesFileId = 112;
const timestamp = Date.now();
const nullOutput = this.dockerService.getNullOutput();
const queryFees = this.buildQueryFees(feesFileId);
const fees = Buffer.from(yield queryFees.execute(client)).toString('hex');
yield shelljs_1.default.exec(`docker exec mirror-node-db psql mirror_node -U mirror_node -c "INSERT INTO public.file_data(file_data, consensus_timestamp, entity_id, transaction_type) VALUES (decode('${fees}', 'hex'), ${timestamp + '000000'}, ${feesFileId}, 17);" >> ${nullOutput}`);
const queryExchangeRates = this.buildQueryFees(exchangeRatesFileId);
const exchangeRates = Buffer.from(yield queryExchangeRates.execute(client)).toString('hex');
yield shelljs_1.default.exec(`docker exec mirror-node-db psql mirror_node -U mirror_node -c "INSERT INTO public.file_data(file_data, consensus_timestamp, entity_id, transaction_type) VALUES (decode('${exchangeRates}', 'hex'), ${timestamp + '000001'}, ${exchangeRatesFileId}, 17);" >> ${nullOutput}`);
this.logger.info(constants_1.NETWORK_PREP_STATE_IMPORT_FEES_END, this.stateName);
});
}
/**
* Builds a query for the fees file.
* @param {number} feesFileId - The fees file ID.
* @returns {FileContentsQuery} The query for the fees file.
*/
buildQueryFees(feesFileId) {
return new sdk_1.FileContentsQuery().setFileId(`0.0.${feesFileId}`);
}
/**
* Mirror Node Monitor creates a Topic Entity.
* If that happens during the account generation step all consecutive AccountIds
* get shifted by 1 and the private keys no longer correspond to the expected AccountIds.
* @returns {Promise<void>}
*/
waitForTopicCreation() {
return __awaiter(this, void 0, void 0, function* () {
this.logger.trace(constants_1.NETWORK_PREP_STATE_WAITING_TOPIC_CREATION, this.stateName);
const LOG_SEARCH_TEXT = 'Created TOPIC entity';
return new Promise((resolve, reject) => {
const command = shelljs_1.default.exec('docker logs mirror-node-monitor -f', {
silent: true,
async: true
});
command.stdout.on('data', (data) => {
if (data.indexOf(LOG_SEARCH_TEXT) !== -1) {
command.kill('SIGINT');
command.stdout.destroy();
this.logger.info(constants_1.NETWORK_PREP_STATE_TOPIC_CREATED, this.stateName);
resolve();
}
});
});
});
}
}
exports.NetworkPrepState = NetworkPrepState;
// this state waits for topics and uploads fees
//# sourceMappingURL=NetworkPrepState.js.map