Permalink
Browse files

[DDW-518] Prevent forced NTP checks if system is going into sleep or …

…has just woken up from sleep
  • Loading branch information...
nikolaglumac committed Dec 6, 2018
1 parent 1f615ed commit a4fbe08c3318509619e9d690f35e0f6867334edd
@@ -17,7 +17,7 @@ Changelog
- Implemented support for Cardano node "structured logging" ([PR 1092](https://github.com/input-output-hk/daedalus/pull/1092), [PR 1122](https://github.com/input-output-hk/daedalus/pull/1122))
- Implemented the IPC driven Cardano node / Daedalus communication ([PR 1075](https://github.com/input-output-hk/daedalus/pull/1075), [PR 1107](https://github.com/input-output-hk/daedalus/pull/1107), [PR 1109](https://github.com/input-output-hk/daedalus/pull/1109), [PR 1115](https://github.com/input-output-hk/daedalus/pull/1115), [PR 1118](https://github.com/input-output-hk/daedalus/pull/1118), [PR 1119](https://github.com/input-output-hk/daedalus/pull/1119), [PR 1162](https://github.com/input-output-hk/daedalus/pull/1162))
- Improved the loading UX ([PR 723](https://github.com/input-output-hk/daedalus/pull/723))
- Improved the NTP check handling ([PR 1086](https://github.com/input-output-hk/daedalus/pull/1086), [PR 1149](https://github.com/input-output-hk/daedalus/pull/1149), [PR 1158](https://github.com/input-output-hk/daedalus/pull/1158), [PR 1194](https://github.com/input-output-hk/daedalus/pull/1194))
- Improved the NTP check handling ([PR 1086](https://github.com/input-output-hk/daedalus/pull/1086), [PR 1149](https://github.com/input-output-hk/daedalus/pull/1149), [PR 1158](https://github.com/input-output-hk/daedalus/pull/1158), [PR 1194](https://github.com/input-output-hk/daedalus/pull/1194), [PR 1212](https://github.com/input-output-hk/daedalus/pull/1212))
- Improved the transaction details text selection ([PR 1073](https://github.com/input-output-hk/daedalus/pull/1073), [PR 1095](https://github.com/input-output-hk/daedalus/pull/1095))
- Integrated Cardano V1 API endpoints ([PR 1018](https://github.com/input-output-hk/daedalus/pull/1018), [PR 1031](https://github.com/input-output-hk/daedalus/pull/1031), [PR 1037](https://github.com/input-output-hk/daedalus/pull/1037), [PR 1042](https://github.com/input-output-hk/daedalus/pull/1042), [PR 1045](https://github.com/input-output-hk/daedalus/pull/1045), [PR 1070](https://github.com/input-output-hk/daedalus/pull/1070), [PR 1078](https://github.com/input-output-hk/daedalus/pull/1078), [PR 1079](https://github.com/input-output-hk/daedalus/pull/1079), [PR 1080](https://github.com/input-output-hk/daedalus/pull/1080), [PR 1088](https://github.com/input-output-hk/daedalus/pull/1088))
- Refactored and improved `NetworkStatus` store to use V1 API data ([PR 1081](https://github.com/input-output-hk/daedalus/pull/1081))
@@ -0,0 +1,2 @@
export const SYSTEM_SUSPEND_CHANNEL = 'SYSTEM_SUSPEND';
export const SYSTEM_WAKE_UP_CHANNEL = 'SYSTEM_WAKE_UP';
@@ -1,6 +1,6 @@
// @flow
import os from 'os';
import { app, BrowserWindow, globalShortcut, Menu, dialog } from 'electron';
import { app, BrowserWindow, globalShortcut, Menu, dialog, powerMonitor } from 'electron';
import { client } from 'electron-connect';
import { includes } from 'lodash';
import { Logger } from '../common/logging';
@@ -14,6 +14,10 @@ import environment from '../common/environment';
import { OPEN_ABOUT_DIALOG_CHANNEL } from '../common/ipc/open-about-dialog';
import { GO_TO_ADA_REDEMPTION_SCREEN_CHANNEL } from '../common/ipc/go-to-ada-redemption-screen';
import { GO_TO_NETWORK_STATUS_SCREEN_CHANNEL } from '../common/ipc/go-to-network-status-screen';
import {
SYSTEM_SUSPEND_CHANNEL,
SYSTEM_WAKE_UP_CHANNEL
} from '../common/ipc/system-power-monitor';
import mainErrorHandler from './utils/mainErrorHandler';
import { launcherConfig } from './config';
import { setupCardano } from './cardano/setup';
@@ -134,6 +138,16 @@ app.on('ready', async () => {
});
}
// Detect system suspend event
powerMonitor.on('suspend', () => {
mainWindow.webContents.send(SYSTEM_SUSPEND_CHANNEL);
});
// Detect system wake-up event
powerMonitor.on('resume', () => {
mainWindow.webContents.send(SYSTEM_WAKE_UP_CHANNEL);
});
mainWindow.on('close', async (event) => {
Logger.info('mainWindow received <close> event. Safe exiting Daedalus now.');
event.preventDefault();
@@ -1,5 +1,6 @@
// @flow
import { observable, action, computed, runInAction } from 'mobx';
import { ipcRenderer } from 'electron';
import moment from 'moment';
import { isEqual } from 'lodash';
import Store from './lib/Store';
@@ -18,6 +19,10 @@ import {
tlsConfigChannel,
restartCardanoNodeChannel
} from '../ipc/cardano.ipc';
import {
SYSTEM_SUSPEND_CHANNEL,
SYSTEM_WAKE_UP_CHANNEL
} from '../../../common/ipc/system-power-monitor';
import { CardanoNodeStates } from '../../../common/types/cardanoNode.types';
import type { GetNetworkStatusResponse } from '../api/nodes/types';
import type { CardanoNodeState, TlsConfig } from '../../../common/types/cardanoNode.types';
@@ -65,6 +70,8 @@ export default class NetworkStatusStore extends Store {
@observable localTimeDifference: ?number = 0; // microseconds
@observable isSystemTimeIgnored = false; // Tracks if NTP time checks are ignored
@observable isSystemTimeChanged = false; // Tracks system time change event
@observable isSystemGoingToSleep = false; // Tracks if system is going to sleep
@observable isSystemFreshlyWokenUp = false; // Tracks if system has just been woken up
@observable getNetworkStatusRequest: Request<GetNetworkStatusResponse> = new Request(
this.api.ada.getNetworkStatus
);
@@ -81,6 +88,9 @@ export default class NetworkStatusStore extends Store {
// Passively receive state changes of the cardano-node
cardanoStateChangeChannel.onReceive(this._handleCardanoNodeStateChange);
this._requestCardanoNodeState();
// Receive system suspend and wake-up events
ipcRenderer.on(SYSTEM_SUSPEND_CHANNEL, this._handleSystemSuspend);
ipcRenderer.on(SYSTEM_WAKE_UP_CHANNEL, this._handleSystemWakeUp);
// ========== MOBX REACTIONS =========== //
@@ -220,9 +230,22 @@ export default class NetworkStatusStore extends Store {
}
if (isForcedTimeDifferenceCheck) {
// Prevent NTP forced time checks in case system is going into sleep
if (this.isSystemGoingToSleep) return;
// Prevent NTP forced time checks in case system has just woken up from sleep
if (this.isSystemFreshlyWokenUp) {
runInAction('reset isSystemFreshlyWokenUp', () => {
this.isSystemFreshlyWokenUp = false;
});
return;
}
// Set most recent block timestamp into the future as a guard
// against system time changes - e.g. if system time was set into the past
this.mostRecentBlockTimestamp = Date.now() + NETWORK_STATUS_REQUEST_TIMEOUT;
runInAction('update mostRecentBlockTimestamp', () => {
this.mostRecentBlockTimestamp = Date.now() + NETWORK_STATUS_REQUEST_TIMEOUT;
});
}
// Record connection status before running network status call
@@ -385,6 +408,16 @@ export default class NetworkStatusStore extends Store {
await this._updateNetworkStatus({ force_ntp_check: true });
};
@action _handleSystemSuspend = () => {
this.isSystemGoingToSleep = true;
this.isSystemFreshlyWokenUp = false;
};
@action _handleSystemWakeUp = () => {
this.isSystemGoingToSleep = false;
this.isSystemFreshlyWokenUp = true;
};
// DEFINE COMPUTED VALUES
@computed get isConnected(): boolean {
return this.isNodeResponding && this.isNodeSubscribed && this.isNodeSyncing;

0 comments on commit a4fbe08

Please sign in to comment.