Don't connect to unreachable/disabled para endpoints (#6977)
* Don't connect to unreachable/disabled para endpoints * Only check on API creation * Address TODO
This commit is contained in:
parent
2db45eb717
commit
6fe5f8996b
@ -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]);
|
||||
|
||||
|
||||
@ -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[] {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user