@hashgraph/hedera-local
Version:
Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).
117 lines • 5.53 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateController = void 0;
const constants_1 = require("../constants");
const StateData_1 = require("../data/StateData");
const LoggerService_1 = require("../services/LoggerService");
const ServiceLocator_1 = require("../services/ServiceLocator");
const CleanUpState_1 = require("../state/CleanUpState");
const RecoveryState_1 = require("../state/RecoveryState");
const EventType_1 = require("../types/EventType");
/**
* Represents a state controller that manages the state machine.
* Implements the IOBserver interface.
* @implements {IObserver}
*/
class StateController {
/**
* Constructs a new instance of the StateController class.
* @param stateName - The name of the state.
*/
constructor(stateName) {
this.controllerName = StateController.name;
this.logger = ServiceLocator_1.ServiceLocator.Current.get(LoggerService_1.LoggerService.name);
this.stateConfiguration = new StateData_1.StateData().getSelectedStateConfiguration(stateName);
this.currStateNum = 0;
this.maxStateNum = 0;
this.logger.trace(`${constants_1.CHECK_SUCCESS} State Controller Initialized!`, this.controllerName);
this.logger.trace(`${constants_1.CHECK_SUCCESS} Initiating ${stateName} procedure!`, this.controllerName);
}
/**
* Starts the state machine.
* If the state configuration is not set, it logs an error and exits the process.
* Subscribes to the current state and calls the onStart method of the current state.
*/
startStateMachine() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.stateConfiguration) {
this.logger.error(`${constants_1.CHECK_FAIL} Something is wrong with state configuration!`, this.controllerName);
// TODO: handle error
process.exit(1);
}
else {
this.maxStateNum = this.stateConfiguration.states.length - 1;
this.stateConfiguration.states[this.currStateNum].subscribe(this);
yield this.stateConfiguration.states[this.currStateNum].onStart();
}
});
}
/**
* Updates the state based on the given event.
* If the event is EventType.Finish, transitions to the next state.
* If the event is EventType.UnknownError, performs cleanup and exits the process with code 1.
* Otherwise, starts a new RecoveryState with the given event.
* @param {EventType} event - The event type.
* @returns {Promise<void>}
*/
update(event) {
return __awaiter(this, void 0, void 0, function* () {
if (event === EventType_1.EventType.Finish) {
yield this.transitionToNextState();
}
else {
if (event === EventType_1.EventType.UnknownError || event === EventType_1.EventType.UnresolvableError) {
yield new CleanUpState_1.CleanUpState().onStart();
process.exit(1);
}
else {
yield new RecoveryState_1.RecoveryState(event).onStart();
}
}
});
}
/**
* Transitions to the next state.
* If the current state number is equal to or greater than the maximum state number,
* the process will exit with a code of 0.
* Otherwise, it increments the current state number, subscribes to the next state,
* and calls the onStart method of the next state.
* If an error occurs during the transition, it logs the error and exits the process with a code of 1,
* unless the error is a TypeError, in which case it ignores the error and continues execution.
* @returns {Promise<void>}
* @private
*/
transitionToNextState() {
return __awaiter(this, void 0, void 0, function* () {
if (this.currStateNum >= this.maxStateNum) {
process.exit(0);
}
this.currStateNum += 1;
try {
this.stateConfiguration.states[this.currStateNum].subscribe(this);
yield this.stateConfiguration.states[this.currStateNum].onStart();
}
catch (error) {
if (error instanceof TypeError) {
// Ignore this error, it finds the methods and executes the code, but still results in TypeError
}
else {
this.logger.error(`Trying to transition to next state was not possible. Error is: ${error}`, this.controllerName);
process.exit(1);
}
}
});
}
}
exports.StateController = StateController;
//# sourceMappingURL=StateController.js.map