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 5 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);
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
"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",
"@ethereumjs/common": "4.3.0",
"@lightclients/patronum": "0.2.0",
"@lodestar/api": "1.7.2",
"@lodestar/config": "1.7.2",
Expand All @@ -43,7 +44,7 @@
"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
520 changes: 0 additions & 520 deletions src/rpc-bundle/bootstrap-data/goerli.ts

This file was deleted.

1,027 changes: 514 additions & 513 deletions src/rpc-bundle/bootstrap-data/mainnet.ts

Large diffs are not rendered by default.

521 changes: 521 additions & 0 deletions src/rpc-bundle/bootstrap-data/sepolia.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/rpc-bundle/client-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ClientManager {
client: BaseClient;

constructor(
protected chain: Chain,
protected chain: Chain.Mainnet | Chain.Sepolia,
clientType: ClientType,
beaconChainAPIURL: string,
protected providerURL: string,
Expand Down Expand Up @@ -47,7 +47,7 @@ export class ClientManager {
await this.client.sync();
const { blockhash, blockNumber } =
await this.client.getNextValidExecutionInfo();
const provider = new VerifyingProvider(
const provider = await VerifyingProvider.create(
this.providerURL,
blockNumber,
blockhash,
Expand Down
31 changes: 15 additions & 16 deletions src/rpc-bundle/constants.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { Chain } from '@ethereumjs/common';
import { ClientType } from '../constants.js';

export const NETWORK_NAMES = {
[Chain.Mainnet]: 'mainnet',
[Chain.Sepolia]: 'sepolia',
} as const;

export const defaultBeaconAPIURL: { [network: number]: string } = {
1: 'https://lodestar-mainnet.chainsafe.io',
// 1: 'http://testing.mainnet.beacon-api.nimbus.team',
// 1: 'http://nimbus-mainnet.commonprefix.com',
5: 'https://lodestar-goerli.chainsafe.io',
// 5: 'http://testing.prater.beacon-api.nimbus.team',
[Chain.Mainnet]: 'https://lodestar-mainnet.chainsafe.io',
[Chain.Sepolia]: 'https://lodestar-sepolia.chainsafe.io',
};

export const defaultProvers: {
[client: string]: { [network: number]: string[] };
} = {
[ClientType.optimistic]: {
1: [
[Chain.Mainnet]: [
'https://light-optimistic-mainnet-1.herokuapp.com',
'https://light-optimistic-mainnet-2.herokuapp.com',
// 'https://eth-rpc-proxy.herokuapp.com',
// 'https://kevlar-tzinas.herokuapp.com',
],
5: [
[Chain.Sepolia]: [
// TODO setup for sepolia
'https://light-optimistic-goerli-1.herokuapp.com',
'https://light-optimistic-goerli-2.herokuapp.com',
],
},
[ClientType.light]: {
1: [defaultBeaconAPIURL[1]],
5: [defaultBeaconAPIURL[5]],
[Chain.Mainnet]: [defaultBeaconAPIURL[Chain.Mainnet]],
[Chain.Sepolia]: [defaultBeaconAPIURL[Chain.Sepolia]],
},
};

// TODO: Add more endpoints.
// Every endpoint needs to support eth_createAccessList, eth_estimateGas
export const defaultPublicRPC: { [network: number]: string[] } = {
1: [
// 'https://eth-mainnet.gateway.pokt.network/v1/5f3453978e354ab992c4da79', (not very stable)
'https://rpc.ankr.com/eth',
],
5: ['https://eth-goerli.gateway.pokt.network/v1/5f3453978e354ab992c4da79'],
[Chain.Mainnet]: ['https://rpc.ankr.com/eth'],
[Chain.Sepolia]: ['https://rpc.ankr.com/eth_sepolia'],
};
9 changes: 6 additions & 3 deletions src/rpc-bundle/start-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dotenv.config();

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { Chain } from '@ethereumjs/common';
import { startServer } from '@lightclients/patronum';
import { ClientManager } from './client-manager.js';
import { ClientType } from '../constants.js';
Expand All @@ -24,7 +25,7 @@ async function main() {
const argv = await yargs(hideBin(process.argv))
.option('network', {
alias: 'n',
choices: [1, 5],
choices: [Chain.Mainnet, Chain.Sepolia],
description: 'chain id to start the proxy on (1, 5)',
rista404 marked this conversation as resolved.
Show resolved Hide resolved
})
.option('client', {
Expand All @@ -51,8 +52,10 @@ async function main() {
})
.parse();

const network = argv.network || parseInt(process.env.CHAIN_ID || '1');
const port = argv.port || (network === 5 ? 8547 : 8546);
const network =
argv.network ||
(process.env.CHAIN_ID ? parseInt(process.env.CHAIN_ID) : Chain.Mainnet);
const port = argv.port || (network === Chain.Sepolia ? 8547 : 8546);
const clientType =
argv.client === 'light' ? ClientType.light : ClientType.optimistic;
const proverURLs = defaultProvers[clientType][network].concat(
Expand Down
16 changes: 10 additions & 6 deletions src/rpc-bundle/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { readFileSync } from 'fs';
import { createBeaconConfig } from '@lodestar/config';
import { fromHexString } from '@chainsafe/ssz';
import { networksChainConfig } from '@lodestar/config/networks';
import { goerliConfig } from './bootstrap-data/goerli.js';
import { Chain } from '@ethereumjs/common';
import { mainnetConfig } from './bootstrap-data/mainnet.js';
import { NETWORK_NAMES } from './constants.js';
import { sepoliaConfig } from './bootstrap-data/sepolia.js';

const bootstrapDataMap: { [network: number]: any } = {
1: mainnetConfig,
5: goerliConfig,
[Chain.Mainnet]: mainnetConfig,
[Chain.Sepolia]: sepoliaConfig,
};

export function getDefaultClientConfig(chain: number, n?: number) {
export function getDefaultClientConfig(
chain: Chain.Mainnet | Chain.Sepolia,
n?: number,
) {
const bootstrapData = bootstrapDataMap[chain];
if (!bootstrapData)
throw new Error(`bootstrapData not found for chain ${chain}`);
const networkName = chain === 1 ? 'mainnet' : 'goerli';
const networkName = NETWORK_NAMES[chain];
const chainConfig = createBeaconConfig(
networksChainConfig[networkName],
fromHexString(bootstrapData.genesis_validator_root),
Expand Down
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@
"emitDecoratorMetadata": true,
"typeRoots": ["node_modules/@types"]
},
"include": ["src/**/*", "src/rpc-bundle/bootstrap-data/*.json"],
"ts-node": {
"esm": true
}
"include": ["src/**/*"]
}
Loading
Loading