Skip to content

Commit

Permalink
Fixing comments
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov committed Jan 2, 2024
1 parent 4620718 commit 7ea281e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Errors/LocalNodeErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class LocalNodeErrors extends Error {
export const Errors = {
CONNECTION_ERROR: (port?: number) => new LocalNodeErrors("Connection Error", `Something went wrong, while trying to connect ${port ? `to port ${port}` : `to local node`}`),
CLEINT_ERROR: (msg?: string) => new LocalNodeErrors("Client Error", `Something went wrong, while trying to create SDK Client${msg ? `: ${msg}` : ``}`),
NO_RECORD_FILE_FOUND_ERROR: () => new LocalNodeErrors('No record file found Error', 'No record file found for the given timestamp.'),
NO_RECORD_FILE_FOUND_ERROR: () => new LocalNodeErrors('No record file found Error', "This record file doesn't not exist, check if timestamp is correct and local-node was started in debug mode using --enable-debug option"),
INVALID_TIMESTAMP_ERROR: () => new LocalNodeErrors('Invalid Timestamp Error', 'Invalid timestamp string. Accepted formats are: 0000000000.000000000 and 0000000000-000000000'),
DEBUG_MODE_CHECK_ERROR: () => new LocalNodeErrors('Debug Mode check Error', 'Debug mode is not enabled to use this command. Please use the --enable-debug flag to enable it.'),
}
72 changes: 46 additions & 26 deletions src/state/DebugState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { LoggerService } from '../services/LoggerService';
import { ServiceLocator } from '../services/ServiceLocator';
import { IState } from './IState';
import { CLIService } from '../services/CLIService';
import readApplicationYML from '../utils/config';
import { Errors } from '../Errors/LocalNodeErrors';
import { RELATIVE_RECORDS_DIR_PATH, RELATIVE_TMP_DIR_PATH } from '../constants';

Expand All @@ -36,6 +35,9 @@ export class DebugState implements IState{
private observer: IOBserver | undefined;

private stateName: string;

private static readonly recordExt = `.${process.env.STREAM_EXTENSION}`;
private static readonly sigExt = `.${process.env.STREAM_SIG_EXTENSION}`;

constructor() {
this.stateName = DebugState.name;
Expand Down Expand Up @@ -77,14 +79,6 @@ export class DebugState implements IState{
}
}

private static checkForDebugMode(): void {
const { application } = readApplicationYML()
const { deleteAfterProcessing } = application.hedera.mirror.importer.downloader.local
if (deleteAfterProcessing) {
throw Errors.DEBUG_MODE_CHECK_ERROR();
}
}

private static getAndValidateTimestamp(timestamp: string): number {
const timestampRegEx = /^\d{10}[.-]\d{9}$/;
if (!timestampRegEx.test(timestamp)) {
Expand All @@ -102,32 +96,58 @@ export class DebugState implements IState{
private findAndCopyRecordFileToTmpDir(jsTimestampNum: number, recordFilesDirPath: string, tmpDirPath: string): void {
// Copy the record file to a temp directory
const files = readdirSync(recordFilesDirPath);
const recordExt = `.${process.env.STREAM_EXTENSION}`;

for (let i = 1; i < files.length; i++) {
const file = files[i];
const recordFileName = file.replace(recordExt, '');
const recordFileName = file.replace(DebugState.recordExt, '');
const fileTimestamp = new Date(recordFileName.replace(/_/g, ':')).getTime();

if (fileTimestamp >= jsTimestampNum) {
const fileToShow = files[i - 2];
if (fileToShow.endsWith(recordExt)) {
this.logger.trace(`Parsing record file [${fileToShow}]\n`);
}

const sigFile = recordFileName + `.${process.env.STREAM_SIG_EXTENSION}`;
copyFileSync(
resolve(recordFilesDirPath, fileToShow),
resolve(tmpDirPath, fileToShow)
);
copyFileSync(
resolve(recordFilesDirPath, sigFile),
resolve(tmpDirPath, sigFile)
);
const fileToCopy = [
files[i - 2],
files[i]
];

this.copyFilesToTmpDir(fileToCopy, tmpDirPath, recordFilesDirPath)
return
}
}

throw Errors.NO_RECORD_FILE_FOUND_ERROR();
}

private copyFilesToTmpDir(
filesToCopy: string | Array<string>,
tmpDirPath: string,
recordFilesDirPath: string
): void {
if (Array.isArray(filesToCopy)) {
for (const file of filesToCopy) {
this.copyFileToDir(file, recordFilesDirPath, tmpDirPath)
}
return
}

this.copyFileToDir(filesToCopy, recordFilesDirPath, tmpDirPath)
}

private copyFileToDir(
fileToCopy: string,
srcPath: string,
destinationPath: string,
): void {
if (fileToCopy.endsWith(DebugState.recordExt)) {
this.logger.trace(`Parsing record file [${fileToCopy}]\n`);
}

const fileToCopyName = fileToCopy.replace(DebugState.recordExt, '');
const sigFile = fileToCopyName + DebugState.sigExt;
copyFileSync(
resolve(srcPath, fileToCopy),
resolve(destinationPath, fileToCopy)
);
copyFileSync(
resolve(srcPath, sigFile),
resolve(destinationPath, sigFile)
);
}
}

0 comments on commit 7ea281e

Please sign in to comment.