@@ -4,10 +4,13 @@ import moment from 'moment';
44import path from 'path' ;
55import { Tail } from 'tail' ;
66import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress' ;
7- import type { GetBlockSyncProgressType } from '../../common/ipc/api' ;
8- import { BlockSyncType } from '../../common/types/cardano-node.types' ;
7+ import {
8+ BlockSyncProgress ,
9+ BlockSyncType ,
10+ } from '../../common/types/cardano-node.types' ;
911import { isItFreshLog } from './blockSyncProgressHelpers' ;
1012import { environment } from '../environment' ;
13+ import { logger } from './logging' ;
1114
1215const blockKeyword = 'Replayed block' ;
1316const validatingChunkKeyword = 'Validating chunk' ;
@@ -21,7 +24,7 @@ const progressKeywords = [
2124 ledgerKeyword ,
2225] ;
2326
24- const keywordTypeMap : Record < string , GetBlockSyncProgressType > = {
27+ const keywordTypeMap : Record < string , BlockSyncType > = {
2528 [ blockKeyword ] : BlockSyncType . replayedBlock ,
2629 [ validatingChunkKeyword ] : BlockSyncType . validatingChunk ,
2730 [ validatedChunkKeyword ] : BlockSyncType . validatingChunk ,
@@ -32,7 +35,7 @@ function containProgressKeywords(line: string) {
3235 return progressKeywords . some ( ( keyword ) => line . includes ( keyword ) ) ;
3336}
3437
35- function getProgressType ( line : string ) : GetBlockSyncProgressType | null {
38+ function getProgressType ( line : string ) : BlockSyncType | null {
3639 const key = progressKeywords . find ( ( k ) => line . includes ( k ) ) ;
3740
3841 if ( ! key ) {
@@ -45,10 +48,10 @@ function getProgressType(line: string): GetBlockSyncProgressType | null {
4548const applicationStartDate = moment . utc ( ) ;
4649
4750const createHandleNewLogLine = ( mainWindow : BrowserWindow ) => {
48- const lastReportedProgressByType : Record < BlockSyncType , number > = {
49- [ BlockSyncType . pushingLedger ] : 0 ,
50- [ BlockSyncType . replayedBlock ] : 0 ,
51+ const progressReport : BlockSyncProgress = {
5152 [ BlockSyncType . validatingChunk ] : 0 ,
53+ [ BlockSyncType . replayedBlock ] : 0 ,
54+ [ BlockSyncType . pushingLedger ] : 0 ,
5255 } ;
5356
5457 return ( line : string ) => {
@@ -65,15 +68,26 @@ const createHandleNewLogLine = (mainWindow: BrowserWindow) => {
6568 return ;
6669 }
6770
68- const progress = Math . floor ( parseFloat ( unparsedProgress ) ) ;
71+ // In rare cases cardano-node does not log 100%, therefore we need to manually mark the previous step as complete.
72+ if (
73+ type === BlockSyncType . replayedBlock &&
74+ progressReport [ BlockSyncType . validatingChunk ] !== 100
75+ ) {
76+ progressReport [ BlockSyncType . validatingChunk ] = 100 ;
77+ }
6978
70- if ( lastReportedProgressByType [ type ] !== progress ) {
71- lastReportedProgressByType [ type ] = progress ;
79+ if (
80+ type === BlockSyncType . pushingLedger &&
81+ progressReport [ BlockSyncType . replayedBlock ] !== 100
82+ ) {
83+ progressReport [ BlockSyncType . replayedBlock ] = 100 ;
84+ }
7285
73- getBlockSyncProgressChannel . send (
74- { progress, type } ,
75- mainWindow . webContents
76- ) ;
86+ const progress = Math . floor ( parseFloat ( unparsedProgress ) ) ;
87+
88+ if ( progressReport [ type ] !== progress ) {
89+ progressReport [ type ] = progress ;
90+ getBlockSyncProgressChannel . send ( progressReport , mainWindow . webContents ) ;
7791 }
7892 } ;
7993} ;
@@ -97,7 +111,7 @@ const watchLogFile = ({
97111 tail . on ( 'line' , handleNewLogLine ) ;
98112} ;
99113
100- const watchLogFileDir = ( {
114+ const waitForLogFileToBeCreatedAndWatchLogFile = ( {
101115 logFileName,
102116 logFileDirPath,
103117 mainWindow,
@@ -106,8 +120,8 @@ const watchLogFileDir = ({
106120 logFileDirPath : string ;
107121 mainWindow : BrowserWindow ;
108122} ) => {
109- const watcher = fs . watch ( logFileDirPath , { } , ( eventname , file ) => {
110- if ( eventname === 'rename' && logFileName === file ) {
123+ const watcher = fs . watch ( logFileDirPath , { } , ( eventName , file ) => {
124+ if ( eventName === 'rename' && logFileName === file ) {
111125 watchLogFile ( {
112126 logFilePath : path . join ( logFileDirPath , logFileName ) ,
113127 mainWindow,
@@ -126,7 +140,11 @@ export const handleCheckBlockReplayProgress = (
126140 const logFilePath = path . join ( logFileDirPath , logFileName ) ;
127141
128142 if ( ! fs . existsSync ( logFilePath ) ) {
129- watchLogFileDir ( { logFileDirPath, logFileName, mainWindow } ) ;
143+ waitForLogFileToBeCreatedAndWatchLogFile ( {
144+ logFileDirPath,
145+ logFileName,
146+ mainWindow,
147+ } ) ;
130148 } else {
131149 watchLogFile ( {
132150 logFilePath,
0 commit comments