Skip to content

Commit

Permalink
fix: use nodes for network ping urls (#241)
Browse files Browse the repository at this point in the history
* refactor: stagger the request durations to use less precise timings

* refactor: rename system state

* fix: remove external network ping to browser websites and use network algod nodes
  • Loading branch information
kieranroneill authored May 23, 2024
1 parent 9c96846 commit 4e7798b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/common/utils/createAlgodClient/createAlgodClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default function createAlgodClient(
network: INetwork,
{ logger }: IBaseOptions = { logger: undefined }
): Algodv2 {
const _functionName: string = 'createAlgodClient';
const algod: INode = getRandomItem<INode>(network.algods);
const _functionName = 'createAlgodClient';
const algod = getRandomItem<INode>(network.algods);

logger?.debug(
`${_functionName}: selected algod node "${algod.canonicalName}"`
Expand Down
6 changes: 3 additions & 3 deletions src/extension/constants/Durations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const ACCOUNT_INFORMATION_REFRESH_INTERVAL: number = 60000; // 1 minute in milliseconds
export const ACCOUNT_INFORMATION_ANTIQUATED_TIMEOUT: number = 30000; // 30 seconds in milliseconds
export const ACCOUNT_INFORMATION_REFRESH_INTERVAL: number = 54000; // 54 seconds in milliseconds
export const ACCOUNT_INFORMATION_ANTIQUATED_TIMEOUT: number = 28000; // 28 seconds in milliseconds
export const DEFAULT_NOTIFICATION_DURATION: number = 6000; // 6 seconds in milliseconds
export const HEARTBEAT_DURATION: number = 20000; // 20 seconds in milliseconds
export const NETWORK_TRANSACTION_PARAMS_REFRESH_INTERVAL: number = 1800000; // 30 minutes in milliseconds
export const NETWORK_TRANSACTION_PARAMS_ANTIQUATED_TIMEOUT: number = 900000; // 15 minutes in milliseconds
export const NETWORK_CONNECTIVITY_CHECK_INTERVAL: number = 30000; // 30 seconds in milliseconds
export const NETWORK_CONNECTIVITY_CHECK_INTERVAL: number = 36000; // 36 seconds in milliseconds
export const NODE_REQUEST_DELAY: number = 100; // in milliseconds
export const PASSWORD_LOCK_DURATION_LOWEST: number = 60000; // 1 minute in milliseconds
export const PASSWORD_LOCK_DURATION_LOW: number = 120000; // 2 minutes in milliseconds
Expand Down
15 changes: 6 additions & 9 deletions src/extension/features/system/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StoreNameEnum } from '@extension/enums';

// types
import type { ILogger } from '@common/types';
import type { IConfirmModal, IScanQRCodeModal, ISystemState } from './types';
import type { IConfirmModal, IScanQRCodeModal, IState } from './types';

// utils
import { getInitialState } from './utils';
Expand All @@ -15,27 +15,24 @@ const slice = createSlice({
name: StoreNameEnum.System,
reducers: {
setConfirmModal: (
state: Draft<ISystemState>,
state: Draft<IState>,
action: PayloadAction<IConfirmModal | null>
) => {
state.confirmModal = action.payload;
},
setLogger: (state: Draft<ISystemState>, action: PayloadAction<ILogger>) => {
setLogger: (state: Draft<IState>, action: PayloadAction<ILogger>) => {
state.logger = action.payload;
},
setOnline: (state: Draft<ISystemState>, action: PayloadAction<boolean>) => {
setOnline: (state: Draft<IState>, action: PayloadAction<boolean>) => {
state.online = action.payload;
},
setScanQRCodeModal: (
state: Draft<ISystemState>,
state: Draft<IState>,
action: PayloadAction<IScanQRCodeModal | null>
) => {
state.scanQRCodeModal = action.payload;
},
setSideBar: (
state: Draft<ISystemState>,
action: PayloadAction<boolean>
) => {
setSideBar: (state: Draft<IState>, action: PayloadAction<boolean>) => {
state.sidebar = action.payload;
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// types
import type { ILogger } from '@common/types';
import IConfirmModal from './IConfirmModal';
import IScanQRCodeModal from './IScanQRCodeModal';
import type IConfirmModal from './IConfirmModal';
import type IScanQRCodeModal from './IScanQRCodeModal';

interface ISystemState {
interface IState {
confirmModal: IConfirmModal | null;
logger: ILogger;
online: boolean;
scanQRCodeModal: IScanQRCodeModal | null;
sidebar: boolean;
}

export default ISystemState;
export default IState;
2 changes: 1 addition & 1 deletion src/extension/features/system/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type { default as IConfirmModal } from './IConfirmModal';
export type { default as IScanQRCodeModal } from './IScanQRCodeModal';
export type { default as ISystemState } from './ISystemState';
export type { default as IState } from './IState';
4 changes: 2 additions & 2 deletions src/extension/features/system/utils/getInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import createLogger from '@common/utils/createLogger';

// types
import type { ISystemState } from '../types';
import type { IState } from '../types';

export default function getInitialState(): ISystemState {
export default function getInitialState(): IState {
return {
confirmModal: null,
logger: createLogger(__ENV__ === 'development' ? 'debug' : 'error'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ import { NETWORK_CONNECTIVITY_CHECK_INTERVAL } from '@extension/constants';
// features
import { setOnline } from '@extension/features/system';

// selectors
import { useSelectSelectedNetwork } from '@extension/selectors';

// types
import { IAppThunkDispatch } from '@extension/types';
import type { IAppThunkDispatch, INode } from '@extension/types';

// utils
import getRandomItem from '@common/utils/getRandomItem';

export default function useOnNetworkConnectivity(): void {
const dispatch: IAppThunkDispatch = useDispatch<IAppThunkDispatch>();
const dispatch = useDispatch<IAppThunkDispatch>();
// selectors
const network = useSelectSelectedNetwork();
// states
const [checking, setChecking] = useState<boolean>(false);
const [intervalId, setIntervalId] = useState<number | undefined>();
// misc
const determineNetworkStatus: () => Promise<void> = async () => {
let result: Response;
let url: string = 'https://developer.chrome.com';

switch (__TARGET__) {
case 'edge':
url = 'https://developer.microsoft.com';
break;
case 'firefox':
url = 'https://developer.mozilla.org';
break;
case 'opera':
url = 'https://dev.opera.com';
break;
case 'safari':
url = 'https://developer.apple.com';
break;
case 'chrome':
default:
break;
if (!network) {
dispatch(setOnline(false));

return;
}

try {
setChecking(true);

result = await fetch(url);
// use a random node
result = await fetch(
`${getRandomItem<INode>(network.algods).url}/versions`
);

dispatch(setOnline(result.status >= 200 && result.status < 300));
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/types/states/IBaseRootState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// features
import type { IARC0200AssetsState } from '@extension/features/arc0200-assets';
import type { ISystemState } from '@extension/features/system';
import type { IState as ISystemState } from '@extension/features/system';

interface IBaseRootState {
arc0200Assets: IARC0200AssetsState;
Expand Down
2 changes: 1 addition & 1 deletion src/extension/types/states/IRegistrationRootState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { IState as INetworksState } from '@extension/features/networks';
import type { INotificationsState } from '@extension/features/notifications';
import type { IState as RegistrationState } from '@extension/features/registration';
import type { ISettingsState } from '@extension/features/settings';
import type { ISystemState } from '@extension/features/system';
import type { IState as ISystemState } from '@extension/features/system';

// types
import type IBaseRootState from './IBaseRootState';
Expand Down

0 comments on commit 4e7798b

Please sign in to comment.