Skip to content

Commit

Permalink
update network availabliity while its running (#44)
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Dowideit <[email protected]>
  • Loading branch information
SvenDowideit authored Mar 16, 2022
1 parent b7a571b commit 0a08b4b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
12 changes: 5 additions & 7 deletions src/main/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as sol from '@solana/web3.js';
import {
Net,
NetStatus,
ValidatorLogsRequest,
ValidatorState,
ValidatorStateRequest,
Expand All @@ -22,26 +22,24 @@ const validatorState = async (
msg: ValidatorStateRequest
): Promise<ValidatorState> => {
const { net } = msg;

let solConn: sol.Connection;

// Connect to cluster
const ret = {
running: false,
status: NetStatus.Unknown,
} as ValidatorState;
if (net !== Net.Localhost) {
ret.running = true;
return ret;
}
try {
solConn = new sol.Connection(netToURL(net));
await solConn.getEpochInfo();
} catch (error) {
const err = error as NodeJS.ErrnoException;
if (err.code === 'ECONNREFUSED') {
ret.status = NetStatus.Unavailable;
return ret;
}
}
ret.running = true;
ret.status = NetStatus.Running;
return ret;
};

Expand Down
42 changes: 23 additions & 19 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import {
faCircle,
} from '@fortawesome/free-solid-svg-icons';
import { useEffect, useState } from 'react';
import useInterval from 'common/hooks';
import { useSelector, useDispatch } from 'react-redux';
import { configActions, RootState, validatorActions } from './slices/mainSlice';
import { ConfigAction, ConfigKey, Net } from '../types/types';
import { ConfigAction, ConfigKey, Net, NetStatus } from '../types/types';
import analytics from 'common/analytics';
import Toast from './components/Toast';
import Accounts from './nav/Accounts';
Expand Down Expand Up @@ -113,9 +114,17 @@ export default function App() {
const { toasts } = useSelector((state: RootState) => state.toast);
const validator = useSelector((state: RootState) => state.validator);
const config = useSelector((state: RootState) => state.config);
const { net } = validator;

const [analyticsEnabled, setAnalyticsEnabled] = useState(true);
const { net } = validator;

useEffect(() => {
window.electron.ipcRenderer.validatorState({ net });
}, [validator]);

useInterval(() => {
window.electron.ipcRenderer.validatorState({ net });
}, 5000);

useEffect(() => {
const listener = (resp: any) => {
Expand All @@ -125,10 +134,7 @@ export default function App() {
}
switch (method) {
case 'validator-state':
dispatch(validatorActions.setRunning(res.running));
if (res.running) {
dispatch(validatorActions.setWaitingForRun(false));
}
dispatch(validatorActions.setState(res.status));
break;
case 'config':
dispatch(
Expand All @@ -142,9 +148,6 @@ export default function App() {
}
};
window.electron.ipcRenderer.on('main', listener);
window.electron.ipcRenderer.validatorState({
net: Net.Localhost,
});
window.electron.ipcRenderer.config({
action: ConfigAction.Get,
});
Expand All @@ -159,16 +162,9 @@ export default function App() {
if (eventKey) dispatch(validatorActions.setNet(eventKey as Net));
};

const netDropdownTitle = (
<>
<FontAwesomeIcon className="me-1" icon={faNetworkWired} />{' '}
<span>{net}</span>
</>
);

let statusDisplay = <></>;

if (validator.running) {
if (validator.status === NetStatus.Running) {
statusDisplay = (
<span className="badge bg-light text-dark p-2">
<FontAwesomeIcon className="sol-green me-1" icon={faCircle} />
Expand All @@ -179,11 +175,20 @@ export default function App() {
statusDisplay = (
<span className="badge bg-light text-dark p-2">
<FontAwesomeIcon className="text-danger me-1" icon={faCircle} />
Unavailable
{validator.status}
</span>
);
}


const netDropdownTitle = (
<>
<FontAwesomeIcon className="me-1" icon={faNetworkWired} />{' '}
<span>{net}</span>
{statusDisplay}
</>
);

let mainDisplay = <></>;

if (!config.loading && !(`${ConfigKey.AnalyticsEnabled}` in config.values)) {
Expand Down Expand Up @@ -250,7 +255,6 @@ export default function App() {
<div>
<Header />
<span className="float-end">
{statusDisplay}
<DropdownButton
size="sm"
id="dropdown-basic-button"
Expand Down
18 changes: 8 additions & 10 deletions src/renderer/nav/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ACCOUNTS_NONE_KEY,
BASE58_PUBKEY_REGEX,
Net,
NetStatus,
WBAccount,
} from 'types/types';

Expand Down Expand Up @@ -149,7 +150,7 @@ const Accounts = () => {
}

let display = <></>;
if (validator.running) {
if (validator.status === NetStatus.Running) {
display = (
<>
<div className="col-auto">
Expand All @@ -161,9 +162,8 @@ const Accounts = () => {
</span>
<button
type="button"
className={`ms-2 btn rounded btn-block btn-sm no-box-shadow ${
addBtnClicked ? 'btn-primary-darker' : 'btn-primary'
}`}
className={`ms-2 btn rounded btn-block btn-sm no-box-shadow ${addBtnClicked ? 'btn-primary-darker' : 'btn-primary'
}`}
onMouseDown={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
): void => {
Expand All @@ -190,18 +190,16 @@ const Accounts = () => {
<div>
<ul className="nav">
<li
className={`${
selectedAccount
className={`${selectedAccount
? 'border-bottom active'
: 'opacity-25 cursor-not-allowed'
} ms-3 me-3 pt-1 pb-1 border-3 nav-item text-secondary nav-link-tab`}
} ms-3 me-3 pt-1 pb-1 border-3 nav-item text-secondary nav-link-tab`}
>
<small>Account</small>
</li>
<li
className={`${
selectedAccountInfo ? '' : 'border-bottom active'
} ms-3 me-3 pt-1 pb-1 border-3 cursor-pointer nav-item text-secondary nav-link-tab`}
className={`${selectedAccountInfo ? '' : 'border-bottom active'
} ms-3 me-3 pt-1 pb-1 border-3 cursor-pointer nav-item text-secondary nav-link-tab`}
onClick={() => {
dispatch(accountsActions.setSelected(''));
}}
Expand Down
16 changes: 7 additions & 9 deletions src/renderer/nav/Validator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useEffect, useRef, useState } from 'react';
import { Button, FormControl, InputGroup } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { RootState, validatorActions } from 'renderer/slices/mainSlice';
import { Net, ValidatorState } from 'types/types';
import { Net, NetStatus, ValidatorState } from 'types/types';
import { debounce } from 'underscore';

const Validator = () => {
Expand All @@ -19,14 +19,12 @@ const Validator = () => {
const dispatch = useDispatch();

useEffect(() => {
window.electron.ipcRenderer.validatorState({
net: Net.Localhost,
});
window.electron.ipcRenderer.validatorState({ net: Net.Localhost });
}, []);

useInterval(() => {
window.electron.ipcRenderer.validatorState({ net: Net.Localhost });
if (validator.running) {
if (validator.status === NetStatus.Running) {
window.electron.ipcRenderer.validatorLogs({
filter: filterRef.current.value || '',
});
Expand Down Expand Up @@ -56,18 +54,18 @@ const Validator = () => {
// TODO(nathanleclaire): Don't nest ternary
return (
<div className="row">
{!validator.running && !validator.waitingForRun ? (
{!(validator.status === NetStatus.Running) && !(validator.status === NetStatus.Starting) ? (
<Button
onClick={() => {
dispatch(validatorActions.setWaitingForRun(true));
dispatch(validatorActions.setState(NetStatus.Starting));
window.electron.ipcRenderer.runValidator();
}}
className="mt-2"
variant="dark"
>
Run
</Button>
) : validator.waitingForRun ? (
) : validator.status === NetStatus.Starting ? (
<div>
<FontAwesomeIcon className="me-1 fa-spin" icon={faSpinner} />
<small className="text-muted">
Expand All @@ -82,7 +80,7 @@ const Validator = () => {
placeholder="Filter logs"
aria-label="Amount"
onKeyDown={debounce(() => {
if (validator.running) {
if (validator.status === NetStatus.Running) {
window.electron.ipcRenderer.validatorLogs({
filter: filterRef.current.value || '',
});
Expand Down
15 changes: 4 additions & 11 deletions src/renderer/slices/mainSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {
TOAST_BOTTOM_OFFSET,
ToastProps,
Net,
NetStatus,
ConfigState,
} from 'types/types';

const validatorState: ValidatorState = {
net: Net.Localhost,
running: false,
waitingForRun: false,
loading: false,
status: NetStatus.Unknown,
};

const toastState: ToastState = {
Expand Down Expand Up @@ -48,14 +47,8 @@ export const validatorSlice = createSlice({
setNet: (state, action: PayloadAction<Net>) => {
state.net = action.payload;
},
setRunning: (state, action: PayloadAction<boolean>) => {
state.running = action.payload;
},
setWaitingForRun: (state, action: PayloadAction<boolean>) => {
state.waitingForRun = action.payload;
},
setLoading: (state, action: PayloadAction<boolean>) => {
state.loading = action.payload;
setState: (state, action: PayloadAction<NetStatus>) => {
state.status = action.payload;
},
},
});
Expand Down
10 changes: 7 additions & 3 deletions src/types/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export enum Net {
Test = 'testnet',
MainnetBeta = 'mainnet-beta',
}
export enum NetStatus {
Unknown = 'unknown',
Running = 'running',
Unavailable = 'unavailable',
Starting = 'starting',
}

export const ACCOUNTS_NONE_KEY = 'none';
export const RANDOMART_W_CH = 17;
Expand Down Expand Up @@ -153,9 +159,7 @@ export interface ChangeBatchSize {

export interface ValidatorState {
net: Net;
running: boolean;
waitingForRun: boolean;
loading: boolean;
status: NetStatus;
}

export interface AccountsState {
Expand Down

0 comments on commit 0a08b4b

Please sign in to comment.