From e726c85e7382794d5976426ad90080b9cfc43ad4 Mon Sep 17 00:00:00 2001 From: cornpotage Date: Fri, 6 Sep 2024 16:24:34 -0700 Subject: [PATCH] optimism_syncStatus support --- .../NodePackageScreen/NodePackageScreen.tsx | 7 ++- .../SidebarNodeItemWrapper.tsx | 7 ++- src/renderer/state/rpcExecuteTranslation.ts | 43 +++++++++++++++---- src/renderer/state/services.ts | 29 +++++++++---- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/renderer/Presentational/NodePackageScreen/NodePackageScreen.tsx b/src/renderer/Presentational/NodePackageScreen/NodePackageScreen.tsx index 40bf0d22..ae5a341d 100644 --- a/src/renderer/Presentational/NodePackageScreen/NodePackageScreen.tsx +++ b/src/renderer/Presentational/NodePackageScreen/NodePackageScreen.tsx @@ -82,7 +82,11 @@ const NodePackageScreen = () => { { pollingInterval }, ); const qExecutionIsSyncing = useGetExecutionIsSyncingQuery( - { rpcTranslation: executionRpcTranslation, httpPort: executionHttpPort }, + { + rpcTranslation: executionRpcTranslation, + httpPort: executionHttpPort, + specId: executionNode?.spec.specId, + }, { pollingInterval }, ); const qExecutionPeers = useGetExecutionPeersQuery( @@ -97,6 +101,7 @@ const NodePackageScreen = () => { { rpcTranslation: consensusRpcTranslation, httpPort: consensusHttpPort, + specId: consensusNode?.spec.specId, }, { pollingInterval }, ); diff --git a/src/renderer/Presentational/SidebarNodeItemWrapper/SidebarNodeItemWrapper.tsx b/src/renderer/Presentational/SidebarNodeItemWrapper/SidebarNodeItemWrapper.tsx index 01eaba5a..a85b6aa7 100644 --- a/src/renderer/Presentational/SidebarNodeItemWrapper/SidebarNodeItemWrapper.tsx +++ b/src/renderer/Presentational/SidebarNodeItemWrapper/SidebarNodeItemWrapper.tsx @@ -113,7 +113,11 @@ export const SidebarNodeItemWrapper = ({ { pollingInterval }, ); const qExecutionIsSyncing = useGetExecutionIsSyncingQuery( - { rpcTranslation: executionRpcTranslation, httpPort: executionHttpPort }, + { + rpcTranslation: executionRpcTranslation, + httpPort: executionHttpPort, + specId: executionNode?.spec.specId, + }, { pollingInterval }, ); const qExecutionPeers = useGetExecutionPeersQuery( @@ -128,6 +132,7 @@ export const SidebarNodeItemWrapper = ({ { rpcTranslation: consensusRpcTranslation, httpPort: consensusHttpPort, + specId: consensusNode?.spec.specId, }, { pollingInterval }, ); diff --git a/src/renderer/state/rpcExecuteTranslation.ts b/src/renderer/state/rpcExecuteTranslation.ts index bd4ec8ec..497b41d9 100644 --- a/src/renderer/state/rpcExecuteTranslation.ts +++ b/src/renderer/state/rpcExecuteTranslation.ts @@ -51,12 +51,19 @@ type RpcCall = | 'clientVersion' | 'net_version' | 'metrics'; -export const executeTranslation = async ( - rpcCall: RpcCall, - rpcTranslation: string, - httpPort: string, - url?: string, -): Promise => { +export const executeTranslation = async ({ + rpcCall, + rpcTranslation, + httpPort, + url, + specId, +}: { + rpcCall: string; + rpcTranslation: string; + httpPort: string; + url?: string; + specId?: string; +}) => { const provider = new ethers.providers.JsonRpcProvider( url ? url : `http://localhost:${httpPort}`, ); @@ -238,8 +245,28 @@ export const executeTranslation = async ( } else if (rpcTranslation === 'eth-l2-consensus') { // use provider if (rpcCall === 'sync') { + if (specId === 'op-node') { + const resp = await provider.send('optimism_syncStatus', []); + if (!resp) return { isSyncing: true, currentBlock: 0, highestBlock: 0 }; + + const l1Behind = resp.head_l1.number - resp.current_l1.number; + const l2Behind = resp.head_l1.number - resp.unsafe_l2.number; + + const isSyncing = + l1Behind > 10 || + l2Behind > 100 || + resp.current_l1.number === 0 || + resp.unsafe_l2.number === 0; + const currentBlock = resp.unsafe_l2.number; + const highestBlock = resp.head_l1.number; + + return { + isSyncing, + currentBlock, + highestBlock, + }; + } const resp = await provider.send('eth_syncing'); - // const resp = await provider.send('optimism_syncStatus'); if (!resp) return { isSyncing: false, currentSlot: 0, highestSlot: 0 }; if (resp?.data) { @@ -280,7 +307,7 @@ export const executeTranslation = async ( } } else if (rpcTranslation === 'farcaster-l1') { // todo: use the current config httpPort value instead of hardcoding 2281 - const hubbleBaseUrl = 'http://localhost:2281'; + const hubbleBaseUrl = `http://localhost:${httpPort}`; if (rpcCall === 'sync') { const resp = await callFetch(`${hubbleBaseUrl}/v1/info`); if (resp && resp.isSyncing !== undefined) { diff --git a/src/renderer/state/services.ts b/src/renderer/state/services.ts index f3f354e5..23d11231 100644 --- a/src/renderer/state/services.ts +++ b/src/renderer/state/services.ts @@ -12,6 +12,7 @@ type QueryArg = { rpcTranslation: NiceNodeRpcTranslation; httpPort: string; url?: string; + specId?: string; }; // const provider = new ethers.providers.WebSocketProvider('ws://localhost:8546'); @@ -38,12 +39,12 @@ export const RtkqExecutionWs: any = createApi({ try { // data = await provider.send('eth_getBlockByNumber', ['latest', false]); console.log('latestBlock rpcTranslation', rpcTranslation); - data = await executeTranslation( - 'latestBlock', + data = await executeTranslation({ + rpcCall: 'latestBlock', rpcTranslation, httpPort, url, - ); + }); console.log('latestBlock data', data); } catch (e) { const error = { message: 'Unable to get latestBlock value' }; @@ -54,17 +55,23 @@ export const RtkqExecutionWs: any = createApi({ }, }), getExecutionIsSyncing: builder.query({ - queryFn: async ({ rpcTranslation, httpPort }) => { + queryFn: async ({ rpcTranslation, httpPort, specId = undefined }) => { let data; console.log('rpcTranslation!', rpcTranslation); console.log('httpPort!', httpPort); + console.log('specIdpassed', specId); try { // if (!rpcTranslation.sync) { // console.log('No rpcTranslation found for sync'); // } // data = await provider.send('eth_syncing'); console.log('sync rpcTranslation', rpcTranslation); - data = await executeTranslation('sync', rpcTranslation, httpPort); + data = await executeTranslation({ + rpcCall: 'sync', + rpcTranslation, + httpPort, + specId, + }); console.log('sync data', data); } catch (e) { const error = { message: 'Unable to get syncing value' }; @@ -85,11 +92,11 @@ export const RtkqExecutionWs: any = createApi({ let data; try { console.log('clientVersion rpcTranslation', rpcTranslation); - data = await executeTranslation( - 'clientVersion', + data = await executeTranslation({ + rpcCall: 'clientVersion', rpcTranslation, httpPort, - ); + }); console.log('clientVersion data', data); } catch (e) { const error = { message: 'Unable to get node version.' }; @@ -105,7 +112,11 @@ export const RtkqExecutionWs: any = createApi({ // let error; try { console.log('peers rpcTranslation', rpcTranslation); - data = await executeTranslation('peers', rpcTranslation, httpPort); + data = await executeTranslation({ + rpcCall: 'peers', + rpcTranslation, + httpPort, + }); console.log('peers data', data); // data = await provider.send('net_peerCount');