diff --git a/libs/proposals/src/components/protocol-upgrade-in-progress-notification.tsx b/libs/proposals/src/components/protocol-upgrade-in-progress-notification.tsx index a9227289f..3330abb2c 100644 --- a/libs/proposals/src/components/protocol-upgrade-in-progress-notification.tsx +++ b/libs/proposals/src/components/protocol-upgrade-in-progress-notification.tsx @@ -26,22 +26,36 @@ export const ProtocolUpgradeInProgressNotification = () => { const [nextUpgrade] = useLocalStorageSnapshot( NEXT_PROTOCOL_UPGRADE_PROPOSAL_SNAPSHOT ); - const { blocksRising, block } = useBlockRising(); const detailsLink = useProtocolUpgradeProposalLink(); let vegaReleaseTag: string | undefined; let upgradeBlockHeight: string | undefined; - if (error && !data && nextUpgrade && ALLOW_STORED_PROPOSAL_DATA) { + const hasData = data && !error; + const hasStoredData = nextUpgrade && ALLOW_STORED_PROPOSAL_DATA; + + if (hasData) { + // gets tag and height from the data api + vegaReleaseTag = data.vegaReleaseTag; + upgradeBlockHeight = data.upgradeBlockHeight; + } else if (hasStoredData) { + // gets tag and height from stored value if data api is unavailable try { const stored = JSON.parse(nextUpgrade) as StoredNextProtocolUpgradeData; vegaReleaseTag = stored.vegaReleaseTag; upgradeBlockHeight = stored.upgradeBlockHeight; } catch { - // no op + // NOOP - could not parse stored data } } + const hasUpgradeInfo = vegaReleaseTag && upgradeBlockHeight; + + const { blocksRising, block } = useBlockRising( + // skips querying blocks if there's no upgrade information available + !hasUpgradeInfo + ); + /** * If upgrade is in progress then none of the nodes should produce blocks, * same should be with the tendermint block info otherwise it's a network @@ -50,10 +64,7 @@ export const ProtocolUpgradeInProgressNotification = () => { * Once the networks is back then the notification disappears. */ const upgradeInProgress = - vegaReleaseTag && - upgradeBlockHeight && - !blocksRising && - block <= Number(upgradeBlockHeight); + hasUpgradeInfo && !blocksRising && block <= Number(upgradeBlockHeight); if (!upgradeInProgress) return null; diff --git a/libs/proposals/src/lib/protocol-upgrade-proposals/use-block-rising.ts b/libs/proposals/src/lib/protocol-upgrade-proposals/use-block-rising.ts index f02be60aa..60d7bdc9c 100644 --- a/libs/proposals/src/lib/protocol-upgrade-proposals/use-block-rising.ts +++ b/libs/proposals/src/lib/protocol-upgrade-proposals/use-block-rising.ts @@ -15,31 +15,33 @@ const CHECK_INTERVAL = 5000; // ms */ const ALLOW_STALE = 2; // times -> MAX(this, 1) * CHECK_INTERVAL ~> min check time -export const useBlockRising = () => { +export const useBlockRising = (skip = false) => { const [blocksRising, setBlocksRising] = useState(true); const [block, setBlock] = useState(0); const nodes = useEnvironment((state) => state.nodes); const clients = useMemo(() => { - return nodes.map( - (n) => - n && - n.length > 0 && - createClient({ + return nodes.map((n) => { + if (n && n.length > 0) { + const client = createClient({ url: n, cacheConfig: undefined, retry: false, connectToDevTools: false, connectToHeaderStore: true, - }) - ); + }); + return client; + } + return undefined; + }); }, [nodes]); const { refetch: fetchBlockInfo } = useBlockInfo(); useEffect(() => { + if (skip) return; let stale = 0; let prev = 0; const check = async () => { - const queries = clients.map((client, index) => + const queries = clients.map((client) => client ? client .query({ @@ -47,18 +49,16 @@ export const useBlockRising = () => { fetchPolicy: 'network-only', errorPolicy: 'ignore', }) - .catch((err) => - Promise.reject( - `could not retrieve statistics from ${nodes[index]}` - ) - ) + .catch(() => { + // NOOP - could not retrieve statistics for that node (network error) + }) : undefined ); const blockInfo = await fetchBlockInfo(); const results = (await Promise.allSettled(compact(queries))).map( (res) => { - if (res && res.status === 'fulfilled') { + if (res && res.status === 'fulfilled' && res.value) { return res.value.data.statistics; } else { return undefined; @@ -86,7 +86,7 @@ export const useBlockRising = () => { return () => { clearInterval(interval); }; - }, [clients, fetchBlockInfo, blocksRising, nodes]); + }, [clients, fetchBlockInfo, blocksRising, nodes, skip]); return { blocksRising, block }; };