Skip to content

Commit 4894005

Browse files
author
Marcin Mazurek
committed
[DDW-1088] Fix race condition - send full proress reports so no messages are lost if the IPC subscription is registered late
1 parent 276eaa5 commit 4894005

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

source/common/ipc/api.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { GenerateVotingPDFParams } from '../types/voting-pdf-request.types'
1414
import type { GenerateCsvParams } from '../types/csv-request.types';
1515
import type { GenerateQRCodeParams } from '../types/save-qrCode.types';
1616
import type {
17+
BlockSyncProgress,
1718
BlockSyncType,
1819
CardanoNodeState,
1920
CardanoStatus,
@@ -455,10 +456,7 @@ export type IntrospectAddressMainResponse = IntrospectAddressResponse;
455456
export const GET_BLOCK_SYNC_PROGRESS_CHANNEL = 'GetBlockSyncProgressChannel';
456457
export type GetBlockSyncProgressType = BlockSyncType;
457458
export type GetBlockSyncProgressRendererRequest = void;
458-
export type GetBlockSyncProgressMainResponse = {
459-
progress: number;
460-
type: GetBlockSyncProgressType;
461-
};
459+
export type GetBlockSyncProgressMainResponse = BlockSyncProgress;
462460

463461
/**
464462
* Channels for connecting / interacting with Hardware Wallet devices

source/common/types/cardano-node.types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ALONZO_PURPLE,
77
SELFNODE,
88
} from './environment.types';
9+
import { GetBlockSyncProgressType } from '../ipc/api';
910

1011
export type TlsConfig = {
1112
hostname: string;
@@ -159,8 +160,11 @@ export const NetworkMagics: {
159160
// Cardano Selfnode network magic
160161
[SELFNODE]: [1, null],
161162
};
163+
162164
export enum BlockSyncType {
163165
pushingLedger = 'pushingLedger',
164166
replayedBlock = 'replayedBlock',
165167
validatingChunk = 'validatingChunk',
166168
}
169+
170+
export type BlockSyncProgress = Record<GetBlockSyncProgressType, number>;

source/main/utils/handleCheckBlockReplayProgress.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import moment from 'moment';
44
import path from 'path';
55
import { Tail } from 'tail';
66
import { 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';
911
import { isItFreshLog } from './blockSyncProgressHelpers';
1012
import { environment } from '../environment';
13+
import { logger } from './logging';
1114

1215
const blockKeyword = 'Replayed block';
1316
const 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 {
4548
const applicationStartDate = moment.utc();
4649

4750
const 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,

source/renderer/app/stores/NetworkStatusStore.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -805,14 +805,10 @@ export default class NetworkStatusStore extends Store {
805805
return Promise.resolve();
806806
};
807807

808-
@action _onBlockSyncProgressUpdate = async ({
809-
progress,
810-
type,
811-
}: GetBlockSyncProgressMainResponse) => {
812-
this.blockSyncProgress = {
813-
...this.blockSyncProgress,
814-
[type]: progress,
815-
};
808+
@action _onBlockSyncProgressUpdate = async (
809+
blockSyncProgress: GetBlockSyncProgressMainResponse
810+
) => {
811+
this.blockSyncProgress = blockSyncProgress;
816812
};
817813

818814
@action

0 commit comments

Comments
 (0)