All files / src/state StartState.ts

100% Statements 37/37
80% Branches 8/10
100% Functions 3/3
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x 1x 1x   1x 1x 1x 1x 1x   1x     1x                                                                         1x 1x 1x 1x 1x 1x                   1x                   4x   4x   4x 4x   4x 4x   4x 1x 1x   4x 4x   4x 4x 2x   2x 1x   2x 2x     2x 2x      
// SPDX-License-Identifier: Apache-2.0
 
import shell from 'shelljs';
import { LocalNodeErrors } from '../Errors/LocalNodeErrors';
import { START_STATE_INIT_MESSAGE, START_STATE_STARTED_DETECTING, START_STATE_STARTED_MESSAGE, START_STATE_STARTING_MESSAGE } from '../constants';
import { IOBserver } from '../controller/IObserver';
import { CLIService } from '../services/CLIService';
import { ConnectionService } from '../services/ConnectionService';
import { DockerService } from '../services/DockerService';
import { LoggerService } from '../services/LoggerService';
import { ServiceLocator } from '../services/ServiceLocator';
import { CLIOptions } from '../types/CLIOptions';
import { EventType } from '../types/EventType';
import { IState } from './IState';
 
export class StartState implements IState{
    /**
     * The logger service used for logging messages.
     */
    private logger: LoggerService;
 
    /**
     * The connection service used for establishing and managing connections.
     */
    private connectionService: ConnectionService;
 
    /**
     * Represents the Docker service used by the StartState class.
     */
    private dockerService: DockerService;
 
    /**
     * The observer for the StartState.
     */
    private observer: IOBserver | undefined;
 
    /**
     * The CLI options for the StartState class.
     */
    private cliOptions: CLIOptions;
 
    /**
     * The name of the state.
     */
    private stateName: string;
    
    /**
     * Creates an instance of StartState.
     * 
     * @constructor
     */
    constructor() {
        this.stateName = StartState.name;
        this.logger = ServiceLocator.Current.get<LoggerService>(LoggerService.name);
        this.cliOptions = ServiceLocator.Current.get<CLIService>(CLIService.name).getCurrentArgv();
        this.dockerService = ServiceLocator.Current.get<DockerService>(DockerService.name);
        this.connectionService = ServiceLocator.Current.get<ConnectionService>(ConnectionService.name);
        this.logger.trace(START_STATE_INIT_MESSAGE, this.stateName);
    }
 
    /**
     * Subscribes an observer to the state.
     * 
     * @public
     * @param {IOBserver} observer - The observer to subscribe.
     */
    public subscribe(observer: IOBserver): void {
        this.observer = observer;
    }
 
    /**
     * Starts the Hedera Local Node.
     * 
     * @public
     * @returns {Promise<void>} A Promise that resolves when the Hedera Local Node is successfully started.
     */
    public async onStart(): Promise<void> {
        this.logger.info(START_STATE_STARTING_MESSAGE, this.stateName);
 
        const rootPath = process.cwd();
 
        shell.cd(__dirname);
        shell.cd('../../');
 
        this.dockerService.checkDockerImages();
        const output = await this.dockerService.dockerComposeUp(this.cliOptions);
        
        if (output.code === 1) {
            this.observer?.update(EventType.DockerError);
            await this.dockerService.dockerComposeUp(this.cliOptions);
        }
        shell.cd(rootPath);
        this.logger.info(START_STATE_STARTED_DETECTING, this.stateName);
 
        try {
            await this.connectionService.waitForFiringUp(5600, 'Mirror Node GRPC');
            await this.connectionService.waitForFiringUp(50211, 'Network Node');
        } catch (e: any) {
            if (e instanceof LocalNodeErrors) {
                this.logger.error(e.message, this.stateName);
            }
            this.observer!.update(EventType.UnknownError);
            return;
        }
 
        this.logger.info(START_STATE_STARTED_MESSAGE, this.stateName);
        this.observer?.update(EventType.Finish);
    }
}