fix(proposals): protocol upgrade notification block querying (#4665)
This commit is contained in:
parent
247927e939
commit
85a4981700
@ -26,22 +26,36 @@ export const ProtocolUpgradeInProgressNotification = () => {
|
|||||||
const [nextUpgrade] = useLocalStorageSnapshot(
|
const [nextUpgrade] = useLocalStorageSnapshot(
|
||||||
NEXT_PROTOCOL_UPGRADE_PROPOSAL_SNAPSHOT
|
NEXT_PROTOCOL_UPGRADE_PROPOSAL_SNAPSHOT
|
||||||
);
|
);
|
||||||
const { blocksRising, block } = useBlockRising();
|
|
||||||
const detailsLink = useProtocolUpgradeProposalLink();
|
const detailsLink = useProtocolUpgradeProposalLink();
|
||||||
|
|
||||||
let vegaReleaseTag: string | undefined;
|
let vegaReleaseTag: string | undefined;
|
||||||
let upgradeBlockHeight: 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 {
|
try {
|
||||||
const stored = JSON.parse(nextUpgrade) as StoredNextProtocolUpgradeData;
|
const stored = JSON.parse(nextUpgrade) as StoredNextProtocolUpgradeData;
|
||||||
vegaReleaseTag = stored.vegaReleaseTag;
|
vegaReleaseTag = stored.vegaReleaseTag;
|
||||||
upgradeBlockHeight = stored.upgradeBlockHeight;
|
upgradeBlockHeight = stored.upgradeBlockHeight;
|
||||||
} catch {
|
} 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,
|
* 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
|
* 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.
|
* Once the networks is back then the notification disappears.
|
||||||
*/
|
*/
|
||||||
const upgradeInProgress =
|
const upgradeInProgress =
|
||||||
vegaReleaseTag &&
|
hasUpgradeInfo && !blocksRising && block <= Number(upgradeBlockHeight);
|
||||||
upgradeBlockHeight &&
|
|
||||||
!blocksRising &&
|
|
||||||
block <= Number(upgradeBlockHeight);
|
|
||||||
|
|
||||||
if (!upgradeInProgress) return null;
|
if (!upgradeInProgress) return null;
|
||||||
|
|
||||||
|
@ -15,31 +15,33 @@ const CHECK_INTERVAL = 5000; // ms
|
|||||||
*/
|
*/
|
||||||
const ALLOW_STALE = 2; // times -> MAX(this, 1) * CHECK_INTERVAL ~> min check time
|
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 [blocksRising, setBlocksRising] = useState(true);
|
||||||
const [block, setBlock] = useState(0);
|
const [block, setBlock] = useState(0);
|
||||||
const nodes = useEnvironment((state) => state.nodes);
|
const nodes = useEnvironment((state) => state.nodes);
|
||||||
const clients = useMemo(() => {
|
const clients = useMemo(() => {
|
||||||
return nodes.map(
|
return nodes.map((n) => {
|
||||||
(n) =>
|
if (n && n.length > 0) {
|
||||||
n &&
|
const client = createClient({
|
||||||
n.length > 0 &&
|
|
||||||
createClient({
|
|
||||||
url: n,
|
url: n,
|
||||||
cacheConfig: undefined,
|
cacheConfig: undefined,
|
||||||
retry: false,
|
retry: false,
|
||||||
connectToDevTools: false,
|
connectToDevTools: false,
|
||||||
connectToHeaderStore: true,
|
connectToHeaderStore: true,
|
||||||
})
|
});
|
||||||
);
|
return client;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
}, [nodes]);
|
}, [nodes]);
|
||||||
|
|
||||||
const { refetch: fetchBlockInfo } = useBlockInfo();
|
const { refetch: fetchBlockInfo } = useBlockInfo();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (skip) return;
|
||||||
let stale = 0;
|
let stale = 0;
|
||||||
let prev = 0;
|
let prev = 0;
|
||||||
const check = async () => {
|
const check = async () => {
|
||||||
const queries = clients.map((client, index) =>
|
const queries = clients.map((client) =>
|
||||||
client
|
client
|
||||||
? client
|
? client
|
||||||
.query<BlockStatisticsQuery>({
|
.query<BlockStatisticsQuery>({
|
||||||
@ -47,18 +49,16 @@ export const useBlockRising = () => {
|
|||||||
fetchPolicy: 'network-only',
|
fetchPolicy: 'network-only',
|
||||||
errorPolicy: 'ignore',
|
errorPolicy: 'ignore',
|
||||||
})
|
})
|
||||||
.catch((err) =>
|
.catch(() => {
|
||||||
Promise.reject(
|
// NOOP - could not retrieve statistics for that node (network error)
|
||||||
`could not retrieve statistics from ${nodes[index]}`
|
})
|
||||||
)
|
|
||||||
)
|
|
||||||
: undefined
|
: undefined
|
||||||
);
|
);
|
||||||
|
|
||||||
const blockInfo = await fetchBlockInfo();
|
const blockInfo = await fetchBlockInfo();
|
||||||
const results = (await Promise.allSettled(compact(queries))).map(
|
const results = (await Promise.allSettled(compact(queries))).map(
|
||||||
(res) => {
|
(res) => {
|
||||||
if (res && res.status === 'fulfilled') {
|
if (res && res.status === 'fulfilled' && res.value) {
|
||||||
return res.value.data.statistics;
|
return res.value.data.statistics;
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -86,7 +86,7 @@ export const useBlockRising = () => {
|
|||||||
return () => {
|
return () => {
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
};
|
};
|
||||||
}, [clients, fetchBlockInfo, blocksRising, nodes]);
|
}, [clients, fetchBlockInfo, blocksRising, nodes, skip]);
|
||||||
|
|
||||||
return { blocksRising, block };
|
return { blocksRising, block };
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user