From bf69c9b110356a33dab29c05710a4206c42b4759 Mon Sep 17 00:00:00 2001 From: Tal Derei Date: Wed, 4 Sep 2024 21:46:25 -0700 Subject: [PATCH] add request level timeout handling --- .../src/hooks/latest-block-height.ts | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/apps/extension/src/hooks/latest-block-height.ts b/apps/extension/src/hooks/latest-block-height.ts index 6dd35982..b78de00d 100644 --- a/apps/extension/src/hooks/latest-block-height.ts +++ b/apps/extension/src/hooks/latest-block-height.ts @@ -23,7 +23,7 @@ const fetchBlockHeightWithFallback = async (endpoints: string[]): Promise endpoint !== randomGrpcEndpoint); @@ -31,11 +31,43 @@ const fetchBlockHeightWithFallback = async (endpoints: string[]): Promise => { + return new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + reject(new Error('Request timed out')); + }, timeoutMs); + + const tendermintClient = createPromiseClient( + TendermintProxyService, + createGrpcWebTransport({ baseUrl: grpcEndpoint }), + ); + + tendermintClient + .getStatus({}) + .then(result => { + if (!result.syncInfo) { + reject(new Error('No syncInfo in getStatus result')); + } + clearTimeout(timeout); + resolve(Number(result.syncInfo?.latestBlockHeight)); + }) + .catch(() => { + clearTimeout(timeout); + reject(new Error('RPC request timed out while fetching block height')); + }); + }); +}; + +// Fetch the block height from a specific RPC endpoint. export const fetchBlockHeight = async (grpcEndpoint: string): Promise => { const tendermintClient = createPromiseClient( TendermintProxyService, - createGrpcWebTransport({ baseUrl: grpcEndpoint, defaultTimeoutMs: 2000 }), + createGrpcWebTransport({ baseUrl: grpcEndpoint }), ); const result = await tendermintClient.getStatus({});