Skip to content

Commit

Permalink
Merge 257619a into f491e66
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Dec 8, 2021
2 parents f491e66 + 257619a commit 8ee1964
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ jobs:
run: yarn coverage
- name: E2e tests
run: yarn test:e2e
env:
GOERLI_RPC_URL: ${{ secrets.GOERLI_RPC_URL }}
- name: README check
run: yarn run check-readme
6 changes: 2 additions & 4 deletions packages/beacon-state-transition/test/perf/analyzeBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {getClient} from "@chainsafe/lodestar-api";
import {config} from "@chainsafe/lodestar-config/default";
import {getInfuraBeaconUrl} from "./infura";

// Analyze how eth2 blocks are in a target network to prepare accurate performance states and blocks

Expand All @@ -15,10 +16,7 @@ import {config} from "@chainsafe/lodestar-config/default";
// aggregationBitsAvg: 87.88991645944512

const network = "mainnet";
const INFURA_CREDENTIALS = "1sla4tyOFn0bB1ohyCKaH2sLmHu:b8cdb9d881039fd04fe982a5ec57b0b8";
const baseUrl = `https://${INFURA_CREDENTIALS}@eth2-beacon-${network}.infura.io`;

const client = getClient(config, {baseUrl});
const client = getClient(config, {baseUrl: getInfuraBeaconUrl(network)});

async function run(): Promise<void> {
const {data: headBlock} = await client.beacon.getBlockHeader("head");
Expand Down
13 changes: 8 additions & 5 deletions packages/beacon-state-transition/test/perf/analyzeEpochs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import fs from "fs";
import {init} from "@chainsafe/bls";
import {getClient} from "@chainsafe/lodestar-api";
import {config} from "@chainsafe/lodestar-config/default";
import {NetworkName} from "@chainsafe/lodestar-config/networks";
import {phase0, ssz} from "@chainsafe/lodestar-types";
import {allForks, computeEpochAtSlot, computeStartSlotAtEpoch} from "../../src";
import {parseAttesterFlags} from "../../lib/allForks";
import {AttesterFlags} from "../../src/allForks";
import {Validator} from "../../lib/phase0";
import {csvAppend, readCsv} from "./csv";
import {getInfuraBeaconUrl} from "./infura";

// Understand the real network characteristics regarding epoch transitions to accurately produce performance test data.
//
Expand Down Expand Up @@ -63,16 +65,15 @@ const validatorChangesCountZero: ValidatorChangesCount = {
withdrawableEpoch: 0,
};

async function analyzeEpochs(network: string, fromEpoch?: number): Promise<void> {
async function analyzeEpochs(network: NetworkName, fromEpoch?: number): Promise<void> {
await init("blst-native");

// Persist summary of epoch data as CSV
const csvPath = `epoch_data_${network}.csv`;
const currCsv = fs.existsSync(csvPath) ? readCsv<EpochData>(csvPath) : [];
const writeToCsv = csvAppend<EpochData>(csvPath);

const INFURA_CREDENTIALS = "1sla4tyOFn0bB1ohyCKaH2sLmHu:b8cdb9d881039fd04fe982a5ec57b0b8";
const baseUrl = `https://${INFURA_CREDENTIALS}@eth2-beacon-${network}.infura.io`;
const baseUrl = getInfuraBeaconUrl(network);
// Long timeout to download states
const client = getClient(config, {baseUrl, timeoutMs: 5 * 60 * 1000});

Expand Down Expand Up @@ -173,9 +174,11 @@ function countAttBits(atts: phase0.PendingAttestation[]): number {

const fromEpoch = process.env.FROM_EPOCH !== undefined ? parseInt(process.env.FROM_EPOCH) : undefined;
const network = process.env.NETWORK;
if (!network) throw Error("Must define process.env.NETWORK");
if (!network) {
throw Error("Must define process.env.NETWORK");
}

analyzeEpochs(network, fromEpoch).catch((e: Error) => {
analyzeEpochs(network as NetworkName, fromEpoch).catch((e: Error) => {
// eslint-disable-next-line no-console
console.error(e);
process.exit(1);
Expand Down
11 changes: 8 additions & 3 deletions packages/beacon-state-transition/test/perf/infura.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const INFURA_CREDENTIALS = "1sla4tyOFn0bB1ohyCKaH2sLmHu:b8cdb9d881039fd04fe982a5ec57b0b8";
import {NetworkName} from "@chainsafe/lodestar-config/networks";

export function getInfuraUrl(network: "mainnet" | "pyrmont" | "prater"): string {
return `https://${INFURA_CREDENTIALS}@eth2-beacon-${network}.infura.io`;
export function getInfuraBeaconUrl(network: NetworkName): string {
const INFURA_ETH2_CREDENTIALS = process.env.INFURA_ETH2_CREDENTIALS;
if (!INFURA_ETH2_CREDENTIALS) {
throw Error("Must set ENV INFURA_ETH2_CREDENTIALS");
}

return `https://${INFURA_ETH2_CREDENTIALS}@eth2-beacon-${network}.infura.io`;
}
4 changes: 2 additions & 2 deletions packages/beacon-state-transition/test/perf/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import {NetworkName, networksChainConfig} from "@chainsafe/lodestar-config/networks";
import {getClient} from "@chainsafe/lodestar-api";
import {getNextSyncCommittee} from "../../src/altair/util/syncCommittee";
import {getInfuraUrl} from "./infura";
import {getInfuraBeaconUrl} from "./infura";
import {testCachePath} from "../cache";
import {MutableVector} from "@chainsafe/persistent-ts";

Expand Down Expand Up @@ -457,7 +457,7 @@ export async function getNetworkCachedState(
if (fs.existsSync(filepath)) {
stateSsz = fs.readFileSync(filepath);
} else {
const client = getClient(config, {baseUrl: getInfuraUrl(network), timeoutMs: timeout ?? 300_000});
const client = getClient(config, {baseUrl: getInfuraBeaconUrl(network), timeoutMs: timeout ?? 300_000});
stateSsz =
computeEpochAtSlot(slot) < config.ALTAIR_FORK_EPOCH
? await client.debug.getState(String(slot), "ssz")
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/cmds/beacon/initBeaconState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {getStateTypeFromBytes} from "@chainsafe/lodestar/lib/util/multifork";
import {downloadOrLoadFile} from "../../util";
import {IBeaconArgs} from "./options";
import {defaultNetwork, IGlobalArgs} from "../../options/globalOptions";
import {fetchWeakSubjectivityState, getGenesisFileUrl, getInfuraBeaconUrl, infuraNetworks} from "../../networks";
import {fetchWeakSubjectivityState, getGenesisFileUrl} from "../../networks";
import {Checkpoint} from "@chainsafe/lodestar-types/phase0";
import {SLOTS_PER_EPOCH} from "@chainsafe/lodestar-params";

Expand Down Expand Up @@ -104,11 +104,9 @@ export async function initBeaconState(
// weak subjectivity sync from a state that needs to be fetched:
// if a weak subjectivity checkpoint has been provided, it is used to inform which state to download and used for additional verification
// otherwise, the 'finalized' state is downloaded and the state itself is used for verification (all trust delegated to the remote beacon node)
const remoteBeaconUrl = args.weakSubjectivityServerUrl || getInfuraBeaconUrl(args.network);
const remoteBeaconUrl = args.weakSubjectivityServerUrl;
if (!remoteBeaconUrl) {
throw new Error(
`Missing weak subjectivity server URL. Use either a custom URL via --weakSubjectivityServerUrl or use one of these options for --network: ${infuraNetworks.toString()}`
);
throw Error(`Must set arg --weakSubjectivityServerUrl for network ${args.network}`);
}

let stateId = "finalized";
Expand Down
27 changes: 1 addition & 26 deletions packages/cli/src/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import * as prater from "./prater";

export type NetworkName = "mainnet" | "pyrmont" | "prater" | "dev";
export const networkNames: NetworkName[] = ["mainnet", "pyrmont", "prater"];
/** Networks that infura supports */
export const infuraNetworks: NetworkName[] = ["mainnet", "pyrmont", "prater"];

function getNetworkData(
network: NetworkName
Expand All @@ -36,27 +34,14 @@ function getNetworkData(
}
}

export function getEth1ProviderUrl(networkId: number): string {
switch (networkId) {
case 1:
return "https://mainnet.infura.io/v3/84842078b09946638c03157f83405213";
case 5:
return "https://goerli.infura.io/v3/84842078b09946638c03157f83405213";
default:
throw Error(`Eth1 network not supported: ${networkId}`);
}
}

export function getNetworkBeaconParams(network: NetworkName): IChainConfig {
return getNetworkData(network).chainConfig;
}

export function getNetworkBeaconNodeOptions(network: NetworkName): RecursivePartial<IBeaconNodeOptions> {
const {depositContractDeployBlock, bootEnrs, chainConfig} = getNetworkData(network);
const networkId = parseInt(String(chainConfig.DEPOSIT_NETWORK_ID || 1), 10);
const {depositContractDeployBlock, bootEnrs} = getNetworkData(network);
return {
eth1: {
providerUrls: [getEth1ProviderUrl(networkId)],
depositContractDeployBlock,
},
network: {
Expand Down Expand Up @@ -92,16 +77,6 @@ export async function fetchBootnodes(network: NetworkName): Promise<string[]> {
return enrs;
}

// TODO these URLs are from a local infura account. switch with a ChainSafe account when available
const INFURA_CREDENTIALS = "1sla4tyOFn0bB1ohyCKaH2sLmHu:b8cdb9d881039fd04fe982a5ec57b0b8";

export function getInfuraBeaconUrl(network: NetworkName): string | undefined {
if (infuraNetworks.includes(network)) {
return `https://${INFURA_CREDENTIALS}@eth2-beacon-${network}.infura.io`;
}
return undefined;
}

/**
* Fetch weak subjectivity state from a remote beacon node
*/
Expand Down
9 changes: 5 additions & 4 deletions packages/light-client/test/getGenesisData.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {getClient} from "@chainsafe/lodestar-api";
import {config} from "@chainsafe/lodestar-config/default";
import {getInfuraBeaconUrl} from "@chainsafe/lodestar-beacon-state-transition/test/perf/infura";
import {NetworkName} from "@chainsafe/lodestar-config/networks";

// To populate packages/light-client/src/networks.ts
//
// ```
// ./node_modules/.bin/ts-node test/getGenesisData.ts
// INFURA_ETH2_CREDENTIALS=<user>:<secret> ./node_modules/.bin/ts-node test/getGenesisData.ts
// ```

/* eslint-disable no-console */

const networksInInfura = ["mainnet", "prater", "pyrmont"];
const INFURA_CREDENTIALS = "1sla4tyOFn0bB1ohyCKaH2sLmHu:b8cdb9d881039fd04fe982a5ec57b0b8";
const networksInInfura: NetworkName[] = ["mainnet", "prater", "pyrmont"];

async function getGenesisData(): Promise<void> {
for (const network of networksInInfura) {
const baseUrl = `https://${INFURA_CREDENTIALS}@eth2-beacon-${network}.infura.io`;
const baseUrl = getInfuraBeaconUrl(network);
const api = getClient(config, {baseUrl});
const {data: genesis} = await api.beacon.getGenesis();
console.log(network, {
Expand Down
16 changes: 9 additions & 7 deletions packages/lodestar/test/e2e/eth1/eth1ForBlockProduction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {LevelDbController} from "@chainsafe/lodestar-db";

import {Eth1ForBlockProduction} from "../../../src/eth1";
import {Eth1Options} from "../../../src/eth1/options";
import {getTestnetConfig, testnet} from "../../utils/testnet";
import {getTestnetConfig, medallaTestnetConfig} from "../../utils/testnet";
import {testLogger} from "../../utils/logger";
import {BeaconDb} from "../../../src/db";
import {generateState} from "../../utils/state";
import {fromHexString, List, toHexString} from "@chainsafe/ssz";
import {Root, ssz} from "@chainsafe/lodestar-types";
import {createCachedBeaconState} from "@chainsafe/lodestar-beacon-state-transition";
import {Eth1Provider} from "../../../src/eth1/provider/eth1Provider";
import {getGoerliRpcUrl} from "../../testParams";

const dbLocation = "./.__testdb";

Expand All @@ -31,16 +32,10 @@ const pyrmontDepositsDataRoot = [
describe("eth1 / Eth1Provider", function () {
this.timeout("2 min");

const eth1Options: Eth1Options = {
enabled: true,
providerUrls: [testnet.providerUrl],
depositContractDeployBlock: testnet.depositBlock,
};
const controller = new AbortController();

const config = getTestnetConfig();
const logger = testLogger();
const eth1Provider = new Eth1Provider(config, eth1Options, controller.signal);

let db: BeaconDb;
let dbController: LevelDbController;
Expand All @@ -67,6 +62,13 @@ describe("eth1 / Eth1Provider", function () {
});

it("Should fetch real Pyrmont eth1 data for block proposing", async function () {
const eth1Options: Eth1Options = {
enabled: true,
providerUrls: [getGoerliRpcUrl()],
depositContractDeployBlock: medallaTestnetConfig.depositBlock,
};
const eth1Provider = new Eth1Provider(config, eth1Options, controller.signal);

const eth1ForBlockProduction = new Eth1ForBlockProduction(eth1Options, {
config,
db,
Expand Down
18 changes: 11 additions & 7 deletions packages/lodestar/test/e2e/eth1/eth1MergeBlockTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {expect} from "chai";
import {Eth1Provider, IEth1Provider} from "../../../src";
import {Eth1MergeBlockTracker, StatusCode} from "../../../src/eth1/eth1MergeBlockTracker";
import {Eth1Options} from "../../../src/eth1/options";
import {testnet} from "../../utils/testnet";
import {testLogger} from "../../utils/logger";
import {quantityToBigint} from "../../../src/eth1/provider/utils";
import {ZERO_HASH} from "../../../src/constants";
import {getGoerliRpcUrl} from "../../testParams";

/* eslint-disable @typescript-eslint/naming-convention */

Expand All @@ -18,12 +18,6 @@ describe("eth1 / Eth1MergeBlockTracker", function () {

const logger = testLogger();

const eth1Options: Eth1Options = {
enabled: true,
providerUrls: [testnet.providerUrl],
depositContractDeployBlock: 0,
};

function getConfig(ttd: bigint): IChainConfig {
return ({
// Set time units to 1s to make the test faster
Expand All @@ -36,6 +30,16 @@ describe("eth1 / Eth1MergeBlockTracker", function () {
}
const eth1Config = {DEPOSIT_CONTRACT_ADDRESS: ZERO_HASH};

// Compute lazily since getGoerliRpcUrl() throws if GOERLI_RPC_URL is not set
let eth1Options: Eth1Options;
before("Get eth1Options", () => {
eth1Options = {
enabled: true,
providerUrls: [getGoerliRpcUrl()],
depositContractDeployBlock: 0,
};
});

let controller: AbortController;
beforeEach(() => (controller = new AbortController()));
afterEach(() => controller.abort());
Expand Down
16 changes: 9 additions & 7 deletions packages/lodestar/test/e2e/eth1/eth1Provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ import "mocha";
import {expect} from "chai";
import {AbortController} from "@chainsafe/abort-controller";
import {Eth1Options} from "../../../src/eth1/options";
import {getTestnetConfig, testnet} from "../../utils/testnet";
import {getTestnetConfig} from "../../utils/testnet";
import {fromHexString} from "@chainsafe/ssz";
import {phase0} from "@chainsafe/lodestar-types";
import {goerliTestnetDepositEvents} from "../../utils/testnet";
import {Eth1Provider, parseEth1Block} from "../../../src/eth1/provider/eth1Provider";
import {getGoerliRpcUrl} from "../../testParams";

describe("eth1 / Eth1Provider", function () {
this.timeout("2 min");

const eth1Options: Eth1Options = {
enabled: true,
providerUrls: [testnet.providerUrl],
depositContractDeployBlock: 0,
};

let controller: AbortController;
beforeEach(() => (controller = new AbortController()));
afterEach(() => controller.abort());

const config = getTestnetConfig();

// Compute lazily since getGoerliRpcUrl() throws if GOERLI_RPC_URL is not set
function getEth1Provider(): Eth1Provider {
const eth1Options: Eth1Options = {
enabled: true,
providerUrls: [getGoerliRpcUrl()],
depositContractDeployBlock: 0,
};
return new Eth1Provider(config, eth1Options, controller.signal);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/lodestar/test/e2e/eth1/jsonRpcHttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chaiAsPromised from "chai-as-promised";
import http from "http";
import {AbortController} from "@chainsafe/abort-controller";
import {JsonRpcHttpClient} from "../../../src/eth1/provider/jsonRpcHttpClient";
import {goerliRpcUrl} from "../../testParams";
import {getGoerliRpcUrl} from "../../testParams";
import {IRpcPayload} from "../../../src/eth1/interface";

chai.use(chaiAsPromised);
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("eth1 / jsonRpcHttpClient", function () {
);
}

if (!url) url = goerliRpcUrl;
if (!url) url = getGoerliRpcUrl();
if (!payload) payload = {method: "no-method", params: []};

const controller = new AbortController();
Expand Down
Loading

0 comments on commit 8ee1964

Please sign in to comment.