Newer
Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import BigNumber from 'bignumber.js';
import { combineLatest, Observable, fromEvent, merge } from 'rxjs';
import { compose, mapPropsStream } from 'recompose';
import {
distinctUntilChanged,
filter,
map,
publishReplay,
startWith,
switchMap,
take
} from 'rxjs/operators';
import isElectron from 'is-electron';
import isEqual from 'lodash/isEqual';
import { peerCount$, syncStatus$, withoutLoading } from '@parity/light.js';
import parityStore from '../stores/parityStore';
const electron = isElectron() ? window.require('electron') : null;
// List here all possible states of our health store. Each state can have a
// payload.
export const STATUS = {
CANTCONNECT: Symbol('CANTCONNECT'), // Can't connect to Parity's api
CLOCKNOTSYNC: Symbol('CLOCKNOTSYNC'), // Local clock is not sync
DOWNLOADING: Symbol('DOWNLOADING'), // Currently downloading Parity
GOOD: Symbol('GOOD'), // Everything's fine
NOINTERNET: Symbol('NOINTERNET'), // No Internet connection
NOPEERS: Symbol('NOPEERS'), // Not connected to any peers
RUNNING: Symbol('RUNNING'), // Parity is running (only checked at startup)
SYNCING: Symbol('SYNCING') // Obvious
};
const isApiConnected$ = parityStore.isApiConnected$;
const isParityRunning$ = Observable.create(observer => {
if (electron) {
electron.ipcRenderer.on('parity-running', (_, isParityRunning) => {
observer.next(isParityRunning);
});
}
}).pipe(
startWith(electron ? !!electron.remote.getGlobal('isParityRunning') : false)
);
const downloadProgress$ = Observable.create(observer => {
if (electron) {
electron.ipcRenderer.on('parity-download-progress', (_, progress) => {
observer.next(progress);
});
}
const isClockSync$ = Observable.create(observer => {
if (electron) {
electron.ipcRenderer.send('asynchronous-message', 'check-clock-sync');
electron.ipcRenderer.once('check-clock-sync-reply', (_, clockSync) => {
observer.next(clockSync.isClockSync);
});
}
const online$ = merge(
fromEvent(window, 'online').pipe(map(() => true)),
fromEvent(window, 'offline').pipe(map(() => false))
// Subscribe to the RPCs only once we set a provider
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
111
112
113
114
115
116
117
118
119
const rpcs$ = isApiConnected$.pipe(
filter(isApiConnected => isApiConnected),
take(1),
switchMap(() =>
combineLatest(
syncStatus$().pipe(
map(syncStatus => {
if (!syncStatus) {
return {
isSync: true
};
}
const { currentBlock, highestBlock, startingBlock } = syncStatus;
const percentage = currentBlock
.minus(startingBlock)
.mul(100)
.div(highestBlock.minus(startingBlock));
return {
isSync: false,
syncPayload: {
currentBlock,
highestBlock,
percentage,
startingBlock
}
};
})
),
peerCount$().pipe(withoutLoading())
)
),
startWith([{ isSync: true }, null]), // Don't stall the HOC's combineLatest; emit immediately
publishReplay(1)
);
rpcs$.connect();
// Inject node health information as health.{status, payload} props
export default compose(
mapPropsStream(props$ =>
combineLatest(props$, combined$, rpcs$).pipe(
[
isParityRunning,
isApiConnected,
downloadProgress,
online,
isClockSync
],
[{ isSync, syncPayload }, peerCount]
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
]) => {
// Parity is being downloaded
if (downloadProgress > 0 && !isParityRunning) {
return {
...props,
health: {
status: STATUS.DOWNLOADING,
payload: {
percentage: new BigNumber(
Math.round(parityStore.downloadProgress * 100)
)
}
}
};
}
// Parity is being launched
if (isParityRunning && !isApiConnected) {
return {
...props,
health: {
status: STATUS.RUNNING
}
};
}
// Not connected to the WS server
if (!isApiConnected) {
return {
...props,
health: {
status: STATUS.CANTCONNECT
}
};
}
// At this point we have a successful connection to parity
// Syncing blocks
if (!isSync) {
return {
...props,
health: {
status: STATUS.SYNCING,
payload: syncPayload
}
};
}
// Clock is not synchronized
if (!isClockSync) {
return {
...props,
health: {
status: STATUS.CLOCKNOTSYNC
}
};
}
// No connexion to the internet
if (!online) {
return {
...props,
health: {
status: STATUS.NOINTERNET
}
};
}
// Not enough peers
if (peerCount && peerCount.lte(1)) {
return {
...props,
health: {
status: STATUS.NOPEERS
}
};
}
// Everything's OK
return {
...props,
health: {
status: STATUS.GOOD
}
};
}
),
distinctUntilChanged(isEqual) // Perform deep comparison