Skip to content

Commit

Permalink
optimism_syncStatus support
Browse files Browse the repository at this point in the history
  • Loading branch information
corn-potage committed Sep 6, 2024
1 parent 235d183 commit e726c85
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -97,6 +101,7 @@ const NodePackageScreen = () => {
{
rpcTranslation: consensusRpcTranslation,
httpPort: consensusHttpPort,
specId: consensusNode?.spec.specId,
},
{ pollingInterval },
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -128,6 +132,7 @@ export const SidebarNodeItemWrapper = ({
{
rpcTranslation: consensusRpcTranslation,
httpPort: consensusHttpPort,
specId: consensusNode?.spec.specId,
},
{ pollingInterval },
);
Expand Down
43 changes: 35 additions & 8 deletions src/renderer/state/rpcExecuteTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ type RpcCall =
| 'clientVersion'
| 'net_version'
| 'metrics';
export const executeTranslation = async (
rpcCall: RpcCall,
rpcTranslation: string,
httpPort: string,
url?: string,
): Promise<any> => {
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}`,
);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 20 additions & 9 deletions src/renderer/state/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type QueryArg = {
rpcTranslation: NiceNodeRpcTranslation;
httpPort: string;
url?: string;
specId?: string;
};

// const provider = new ethers.providers.WebSocketProvider('ws://localhost:8546');
Expand All @@ -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' };
Expand All @@ -54,17 +55,23 @@ export const RtkqExecutionWs: any = createApi({
},
}),
getExecutionIsSyncing: builder.query<ProviderResponse, QueryArg>({
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' };
Expand All @@ -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.' };
Expand All @@ -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');
Expand Down

0 comments on commit e726c85

Please sign in to comment.