Skip to content

Commit

Permalink
Refactor getConfig(), pass forkName in getGindexFromQueryParams to su…
Browse files Browse the repository at this point in the history
…pport all the forks�
  • Loading branch information
themicp committed Feb 14, 2024
1 parent 9e693b3 commit cea10f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
36 changes: 25 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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,
Expand Down
24 changes: 14 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -40,7 +43,8 @@ export const parsePath = (path: string): (string | number)[] => {

export const getGindexFromQueryParams = (
pathResolution: 'block' | 'state',
queryParams: Record<string, any>
queryParams: Record<string, any>,
forkName: ForkName
): number | null => {
const { gindex: rawGindex, path } = queryParams;
console.log("PATH", path);
Expand All @@ -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');
}
Expand Down

0 comments on commit cea10f8

Please sign in to comment.