From 6fe5f8996bbcebe816d7bf21a2643fa80f783df3 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sun, 13 Feb 2022 14:56:53 +0100 Subject: [PATCH] Don't connect to unreachable/disabled para endpoints (#6977) * Don't connect to unreachable/disabled para endpoints * Only check on API creation * Address TODO --- packages/react-hooks/src/useParaApi.ts | 23 +++++--------------- packages/react-hooks/src/useParaEndpoints.ts | 4 +++- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/packages/react-hooks/src/useParaApi.ts b/packages/react-hooks/src/useParaApi.ts index 343df9e326..945f910ccf 100644 --- a/packages/react-hooks/src/useParaApi.ts +++ b/packages/react-hooks/src/useParaApi.ts @@ -7,6 +7,7 @@ import type { BN } from '@polkadot/util'; import { useEffect, useState } from 'react'; import { ApiPromise } from '@polkadot/api'; +import { arrayShuffle } from '@polkadot/util'; import { createNamedHook } from './createNamedHook'; import { useApiUrl } from './useApiUrl'; @@ -19,23 +20,6 @@ interface Result { urls: string[]; } -// use from @polkadot/util -function arrayShuffle (result: string[]): string[] { - let currentIndex = result.length; - - while (currentIndex !== 0) { - const randomIndex = Math.floor(Math.random() * currentIndex); - - currentIndex--; - - [result[currentIndex], result[randomIndex]] = [ - result[randomIndex], result[currentIndex] - ]; - } - - return result; -} - function useParaApiImpl (paraId: BN | number): Result { const mountedRef = useIsMountedRef(); const endpoints = useParaEndpoints(paraId); @@ -50,7 +34,10 @@ function useParaApiImpl (paraId: BN | number): Result { mountedRef.current && setState({ api: null, endpoints, - urls: arrayShuffle(endpoints.map(({ value }) => value)) + urls: arrayShuffle( + endpoints + .filter(({ isDisabled, isUnreachable }) => !isDisabled && !isUnreachable) + .map(({ value }) => value)) }); }, [endpoints, mountedRef]); diff --git a/packages/react-hooks/src/useParaEndpoints.ts b/packages/react-hooks/src/useParaEndpoints.ts index c1d424060d..480033a362 100644 --- a/packages/react-hooks/src/useParaEndpoints.ts +++ b/packages/react-hooks/src/useParaEndpoints.ts @@ -23,7 +23,9 @@ function extractRelayEndpoints (genesisHash: string): LinkOption[] { function extractParaEndpoints (allEndpoints: LinkOption[], paraId: BN | number): LinkOption[] { const numId = bnToBn(paraId).toNumber(); - return allEndpoints.filter(({ paraId }) => paraId === numId); + return allEndpoints.filter(({ paraId }) => + paraId === numId + ); } function useRelayEndpointsImpl (): LinkOption[] {