From 89895ae25af2f3fd16795ff42ff7bdec3f74786f Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Tue, 28 Sep 2021 00:34:10 +0200 Subject: [PATCH] Use ExecutionEngineHttp in BeaconNode --- .../src/eth1/provider/jsonRpcHttpClient.ts | 3 ++- packages/lodestar/src/executionEngine/http.ts | 25 ++++++++++++++++--- packages/lodestar/src/node/nodejs.ts | 8 +++--- packages/lodestar/src/node/options.ts | 3 +++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/lodestar/src/eth1/provider/jsonRpcHttpClient.ts b/packages/lodestar/src/eth1/provider/jsonRpcHttpClient.ts index ec1b68f6e2d0..17afe77bc463 100644 --- a/packages/lodestar/src/eth1/provider/jsonRpcHttpClient.ts +++ b/packages/lodestar/src/eth1/provider/jsonRpcHttpClient.ts @@ -30,6 +30,7 @@ export class JsonRpcHttpClient { private readonly urls: string[], private readonly opts: { signal: AbortSignal; + timeout?: number; /** If returns true, do not fallback to other urls and throw early */ shouldNotFallback?: (error: Error) => boolean; } @@ -105,7 +106,7 @@ export class JsonRpcHttpClient { const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); - }, opts?.timeout ?? REQUEST_TIMEOUT); + }, opts?.timeout ?? this.opts.timeout ?? REQUEST_TIMEOUT); const onParentSignalAbort = (): void => controller.abort(); diff --git a/packages/lodestar/src/executionEngine/http.ts b/packages/lodestar/src/executionEngine/http.ts index 2856269bbe1c..ad616b4f3840 100644 --- a/packages/lodestar/src/executionEngine/http.ts +++ b/packages/lodestar/src/executionEngine/http.ts @@ -1,10 +1,20 @@ +import {AbortSignal} from "@chainsafe/abort-controller"; import {Bytes32, merge, Root, ExecutionAddress, PayloadId, RootHex} from "@chainsafe/lodestar-types"; import {fromHexString, toHexString} from "@chainsafe/ssz"; import {JsonRpcHttpClient} from "../eth1/provider/jsonRpcHttpClient"; import {hexToNumber, numberToHex} from "../eth1/provider/utils"; import {IExecutionEngine} from "./interface"; -/** +export type ExecutionEngineHttpOpts = { + urls: string[]; + timeout?: number; +}; + +export const defaultExecutionEngineHttpOpts: ExecutionEngineHttpOpts = { + urls: ["http://localhost:8550"], +}; + +export /** * based on Ethereum JSON-RPC API and inherits the following properties of this standard: * - Supported communication protocols (HTTP and WebSocket) * - Message format and encoding notation @@ -13,8 +23,17 @@ import {IExecutionEngine} from "./interface"; * Client software MUST expose Engine API at a port independent from JSON-RPC API. The default port for the Engine API is 8550 for HTTP and 8551 for WebSocket. * https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.1/src/engine/interop/specification.md */ -export class ExecutionEngineHttp implements IExecutionEngine { - constructor(private readonly rpc: JsonRpcHttpClient) {} +class ExecutionEngineHttp implements IExecutionEngine { + private readonly rpc: JsonRpcHttpClient; + + constructor(opts: ExecutionEngineHttpOpts, signal: AbortSignal, rpc?: JsonRpcHttpClient) { + this.rpc = + rpc ?? + new JsonRpcHttpClient(opts.urls, { + signal, + timeout: opts.timeout, + }); + } /** * `engine_executePayload` diff --git a/packages/lodestar/src/node/nodejs.ts b/packages/lodestar/src/node/nodejs.ts index e27215255b32..bc20f37e027e 100644 --- a/packages/lodestar/src/node/nodejs.ts +++ b/packages/lodestar/src/node/nodejs.ts @@ -4,6 +4,7 @@ import {AbortController} from "@chainsafe/abort-controller"; import LibP2p from "libp2p"; +import {Registry} from "prom-client"; import {TreeBacked} from "@chainsafe/ssz"; import {IBeaconConfig} from "@chainsafe/lodestar-config"; @@ -17,11 +18,10 @@ import {BeaconSync, IBeaconSync} from "../sync"; import {BeaconChain, IBeaconChain, initBeaconMetrics} from "../chain"; import {createMetrics, IMetrics, HttpMetricsServer} from "../metrics"; import {getApi, RestApi} from "../api"; -import {IBeaconNodeOptions} from "./options"; +import {ExecutionEngineHttp} from "../executionEngine"; import {initializeEth1ForBlockProduction} from "../eth1"; +import {IBeaconNodeOptions} from "./options"; import {runNodeNotifier} from "./notifier"; -import {Registry} from "prom-client"; -import {ExecutionEngineDisabled} from "../executionEngine"; export * from "./options"; @@ -137,7 +137,7 @@ export class BeaconNode { {config, db, logger: logger.child(opts.logger.eth1), signal}, anchorState ), - executionEngine: new ExecutionEngineDisabled(), + executionEngine: new ExecutionEngineHttp(opts.executionEngine, signal), }); // Load persisted data from disk to in-memory caches diff --git a/packages/lodestar/src/node/options.ts b/packages/lodestar/src/node/options.ts index 90e523a6c8cd..8eb9fe60b71f 100644 --- a/packages/lodestar/src/node/options.ts +++ b/packages/lodestar/src/node/options.ts @@ -10,6 +10,7 @@ import {defaultLoggerOptions, IBeaconLoggerOptions} from "./loggerOptions"; import {defaultMetricsOptions, IMetricsOptions} from "../metrics/options"; import {defaultNetworkOptions, INetworkOptions} from "../network/options"; import {defaultSyncOptions, SyncOptions} from "../sync/options"; +import {defaultExecutionEngineHttpOpts, ExecutionEngineHttpOpts} from "../executionEngine/http"; // Re-export so the CLI doesn't need to depend on lodestar-api export {allNamespaces} from "../api/rest/index"; @@ -18,6 +19,7 @@ export interface IBeaconNodeOptions { chain: IChainOptions; db: IDatabaseOptions; eth1: Eth1Options; + executionEngine: ExecutionEngineHttpOpts; logger: IBeaconLoggerOptions; metrics: IMetricsOptions; network: INetworkOptions; @@ -29,6 +31,7 @@ export const defaultOptions: IBeaconNodeOptions = { chain: defaultChainOptions, db: defaultDbOptions, eth1: defaultEth1Options, + executionEngine: defaultExecutionEngineHttpOpts, logger: defaultLoggerOptions, metrics: defaultMetricsOptions, network: defaultNetworkOptions,