Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace goerli with sepolia #19

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kevlar --help
Options:
--help Show help [boolean]
--version Show version number [boolean]
-n, --network chain id to start the proxy on (1, 5) [choices: 1, 5]
-n, --network chain id to start the proxy on (1, 11155111) [choices: 1, 11155111]
-c, --client type of the client [choices: "light", "optimistic"]
-o, --provers comma separated prover urls
-u, --rpc rpc url to proxy
Expand Down
98 changes: 98 additions & 0 deletions gen-committee-pks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// A script to fetch sync committee public keys from beaconcha.in API.
// Used for RPC bootstrap data.
//
// Usage:
// node gen-comittee-pks.js <network> <slot>
import fs from 'fs';
import path from 'path';

const SLOTS_PER_EPOCH = 32;
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD = 256;

const networkApiHostnames = {
sepolia: 'https://sepolia.beaconcha.in/api/v1',
mainnet: 'https://beaconcha.in/api/v1',
};

const fetchValidatorIds = async (network, period) => {
const apiUrl = `${networkApiHostnames[network]}/sync_committee/${period}`;
const response = await fetch(apiUrl);
const data = await response.json();
return data.data.validators;
};

// Note: max 100 validators can be fetched at once
const fetchValidatorDetails = async (network, validatorIds) => {
const apiUrl = `${networkApiHostnames[network]}/validator/${validatorIds.join(',')}`;
const response = await fetch(apiUrl);
const data = await response.json();
const validatorMap = new Map(
data.data.map(validator => [validator.validatorindex, validator]),
);
return validatorIds.map(id => validatorMap.get(id));
};

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

const calculateSyncCommitteePeriod = slot => {
return Math.floor(
slot / (SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD),
);
};

const extractPublicKeys = async (network, slot) => {
const period = calculateSyncCommitteePeriod(slot);
const committeePk = [];
const validatorIds = await fetchValidatorIds(network, period);

console.log(`Sync committee period: ${period}`);
console.log(
`Fetching validator details for ${validatorIds.length} validators...`,
);
console.log(`Validator IDs: ${validatorIds}`);

// Split validator IDs into chunks of 100
const chunkSize = 100;
for (let i = 0; i < validatorIds.length; i += chunkSize) {
const chunk = validatorIds.slice(i, i + chunkSize);
try {
const validatorDetails = await fetchValidatorDetails(network, chunk);
validatorDetails.forEach(validator => {
if (validator.pubkey) {
committeePk.push(validator.pubkey);
}
});
} catch (err) {
console.error(
`Error fetching validator details for chunk starting at index ${i}:`,
err,
);
}
// Wait for 2 seconds between requests to avoid rate limiting
await delay(2000);
}

const outputFilePath = path.join(
process.cwd(),
`committee_pk_${network}_${slot}.json`,
);
const outputData = {
network,
slot,
committee_pk: committeePk,
};

fs.writeFileSync(outputFilePath, JSON.stringify(outputData, null, 2), 'utf8');
console.log(`Output saved to ${outputFilePath}`);
};

const args = process.argv.slice(2);
const network = args[0];
const slot = parseInt(args[1], 10);

if (!network || isNaN(slot)) {
console.error('Usage: node gen-comittee-pks.js <network> <slot>');
process.exit(1);
}

extractPublicKeys(network, slot);
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lightclients/kevlar",
"version": "0.3.2",
"version": "0.4.0",
"description": "Light client-based RPC Proxy for PoS Ethereum",
"main": "dist/index.js",
"repository": {
Expand All @@ -20,30 +20,32 @@
"prettier": "2.6.2",
"pretty-quick": "3.1.3",
"ts-essentials": "9.1.2",
"ts-node": "10.9.1",
"tsx": "^4.19.1",
"typescript": "^4.6.3"
},
"dependencies": {
"@chainsafe/as-sha256": "0.4.1",
"@chainsafe/bls": "7.1.1",
"@chainsafe/ssz": "0.11.1",
"@lightclients/patronum": "0.2.0",
"@lodestar/api": "1.7.2",
"@lodestar/config": "1.7.2",
"@lodestar/light-client": "1.7.2",
"@lodestar/types": "1.7.2",
"@ethereumjs/common": "4.3.0",
"@lightclients/patronum": "0.3.1",
"@lodestar/api": "1.22.0",
"@lodestar/config": "1.22.0",
"@lodestar/light-client": "1.22.0",
"@lodestar/types": "1.22.0",
"axios": "0.27.2",
"decimal.js": "10.4.1",
"dotenv": "16.0.2",
"eth-rpc-errors": "4.0.3",
"json-rpc-engine": "6.1.0",
"rust-verkle-wasm": "^0.0.1",
"yargs": "17.5.1"
},
"scripts": {
"build": "tsc",
"prepack": "npm run build",
"start": "node ./dist/provers/start-server.js",
"rpc-proxy": "npx ts-node ./src/rpc-bundle/start-rpc.ts"
"rpc-proxy": "tsx ./src/rpc-bundle/start-rpc.ts"
},
"bin": {
"kevlar": "dist/rpc-bundle/start-rpc.js",
Expand Down
12 changes: 5 additions & 7 deletions src/clients/base-client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { AsyncOrSync } from 'ts-essentials';
import axios from 'axios';
import * as phase0 from '@lodestar/types/phase0';
import * as bellatrix from '@lodestar/types/bellatrix';
import * as capella from '@lodestar/types/capella';
import { allForks } from '@lodestar/types';
import * as deneb from '@lodestar/types/deneb';
import { digest } from '@chainsafe/as-sha256';
import { BeaconConfig, ChainForkConfig } from '@lodestar/config';
import type { PublicKey } from '@chainsafe/bls/types';
Expand Down Expand Up @@ -207,7 +205,7 @@ export abstract class BaseClient {
}

optimisticUpdateFromJSON(update: any): OptimisticUpdate {
return capella.ssz.LightClientOptimisticUpdate.fromJson(update);
return deneb.ssz.LightClientOptimisticUpdate.fromJson(update);
}

async optimisticUpdateVerify(
Expand Down Expand Up @@ -259,15 +257,15 @@ export abstract class BaseClient {

isValidLightClientHeader(
config: ChainForkConfig,
header: allForks.LightClientHeader,
header: deneb.LightClientHeader,
): boolean {
return isValidMerkleBranch(
config
.getExecutionForkTypes(header.beacon.slot)
.ExecutionPayloadHeader.hashTreeRoot(
(header as capella.LightClientHeader).execution,
(header as deneb.LightClientHeader).execution,
),
(header as capella.LightClientHeader).executionBranch,
(header as deneb.LightClientHeader).executionBranch,
EXECUTION_PAYLOAD_DEPTH,
EXECUTION_PAYLOAD_INDEX,
header.beacon.bodyRoot,
Expand Down
2 changes: 1 addition & 1 deletion src/provers/light-optimistic/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function startServer(
const httpServer = http.createServer();

httpServer.setTimeout(1000 * 20); // 20s
httpServer.on('request', await getApp(network, beaconAPIURL));
httpServer.on('request', getApp(network, beaconAPIURL));

httpServer.listen(port, function () {
console.log(`Server listening on port ${port}`);
Expand Down
Loading
Loading