From cea10f8ef704a1c047edc6de61db5b6b7fe6ff91 Mon Sep 17 00:00:00 2001 From: themicp Date: Wed, 14 Feb 2024 16:19:28 +0200 Subject: [PATCH] Refactor getConfig(), pass forkName in getGindexFromQueryParams to support all the forks --- src/index.ts | 36 +++++++++++++++++++++++++----------- src/utils.ts | 24 ++++++++++++++---------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/index.ts b/src/index.ts index efa6db7..b691977 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,15 @@ -import { EthAPI } from './eth.js' +import { EthAPI, NETWORK } from './eth.js' import { toHexString } from '@chainsafe/ssz' import express from 'express' import cors from 'cors' -import { getConfig, getGindexFromQueryParams } from './utils.js' +import { getConfig, getGindexFromQueryParams, supportedNetworks } from './utils.js' const { port, beaconUrls } = getConfig(); -let ethAPIs: {[network: string]: EthAPI} = {}; -for (let [network, beaconUrl] of Object.entries(beaconUrls)) { - ethAPIs[network] = new EthAPI(beaconUrl as string) -} +const ethAPIs: {[key in NETWORK]: EthAPI} = supportedNetworks.reduce((acc, network) => { + acc[network] = new EthAPI(beaconUrls[network] as string, network); + return acc; +}, {} as {[key in NETWORK]: EthAPI}); const app = express() app.use(cors()) @@ -22,9 +22,14 @@ app.get('/state_proof', async (req: express.Request, res: express.Response) => { return res.status(400).send('Missing state_id') } + if (!supportedNetworks.includes(network as NETWORK)) { + return res.status(400).send('Invalid network') + } + try { - const gindex = getGindexFromQueryParams('state', req.query) - const proof = await ethAPIs[network].getStateProof(stateId as string, Number(gindex)) + const forkName = await ethAPIs[network as NETWORK].getForkNameByStateId(stateId as string); + const gindex = getGindexFromQueryParams('state', req.query, forkName) + const proof = await ethAPIs[network as NETWORK].getStateProof(stateId as string, Number(gindex)) const serializedProof = { ...proof, @@ -48,10 +53,14 @@ app.get('/has_state', async (req, res: express.Response) => { return res.status(400).send('Missing state_id') } + if (!supportedNetworks.includes(network as NETWORK)) { + return res.status(400).send('Invalid network') + } + const gindex = 1 try { // Could take proof - const proof = await ethAPIs[network].getStateProof(stateId as string, Number(gindex)) + const proof = await ethAPIs[network as NETWORK].getStateProof(stateId as string, Number(gindex)) return res.sendStatus(200) } catch (e) { @@ -68,9 +77,14 @@ app.get('/block_proof', async (req, res: express.Response) => { return res.status(400).send('Missing block_id') } + if (!supportedNetworks.includes(network as NETWORK)) { + return res.status(400).send('Invalid network') + } + try { - const gindex = getGindexFromQueryParams('block', req.query) - const proof = await ethAPIs[network].getBlockProof(blockId as string, Number(gindex)) + const forkName = await ethAPIs[network as NETWORK].getForkNameByBlockId(blockId as string); + const gindex = getGindexFromQueryParams('block', req.query, forkName) + const proof = await ethAPIs[network as NETWORK].getBlockProof(blockId as string, Number(gindex)) const serializedProof = { ...proof, diff --git a/src/utils.ts b/src/utils.ts index c3def30..f473d68 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,14 +1,17 @@ import "dotenv/config" -import * as deneb from '@lodestar/types/deneb' +import { NETWORK } from "./eth"; +import { ForkName } from "@lodestar/params"; +import { ssz } from "@lodestar/types" -export function getConfig(): { port: number, beaconUrls: {[network: string]: string}} { +export const supportedNetworks: NETWORK[] = ["goerli", "sepolia", "mainnet"]; + +export function getConfig(): { port: number, beaconUrls: {[key in NETWORK]: string | null}} { let port = +getEnv("PORT", "3000") - let beaconUrls: {[network: string]: string;} = {}; - for (let network of ["goerli", "sepolia", "mainnet"]) { - let beaconURL = getEnv(`${network.toUpperCase()}_BEACON_API`) - if (beaconURL) beaconUrls[network] = beaconURL - } + const beaconUrls: {[key in NETWORK]: string} = supportedNetworks.reduce((acc, network) => { + acc[network] = getEnv(`${network.toUpperCase()}_BEACON_API`); + return acc; + }, {} as {[key in NETWORK]: string}); const config = { port, beaconUrls } console.log("Loaded config", config) @@ -40,7 +43,8 @@ export const parsePath = (path: string): (string | number)[] => { export const getGindexFromQueryParams = ( pathResolution: 'block' | 'state', - queryParams: Record + queryParams: Record, + forkName: ForkName ): number | null => { const { gindex: rawGindex, path } = queryParams; console.log("PATH", path); @@ -57,8 +61,8 @@ export const getGindexFromQueryParams = ( const parsedPath = parsePath(path); try { return pathResolution === 'block' - ? Number(deneb.ssz.BeaconBlock.getPathInfo(parsedPath).gindex) - : Number(deneb.ssz.BeaconState.getPathInfo(parsedPath).gindex); + ? Number(ssz.allForks[ForkName[forkName]].BeaconBlock.getPathInfo(parsedPath).gindex) + : Number(ssz.allForks[ForkName[forkName]].BeaconState.getPathInfo(parsedPath).gindex); } catch (error) { throw new Error('Could not resolve path to gindex'); }