From 354955ccff767635d1cdbecc00a13cf6c9d4ba47 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 28 Sep 2021 15:06:31 +1000 Subject: [PATCH] Add bones for handling RPC methods on test server --- .../src/test_utils/handle_rpc.rs | 10 ++++++ .../execution_layer/src/test_utils/mod.rs | 32 ++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 beacon_node/execution_layer/src/test_utils/handle_rpc.rs diff --git a/beacon_node/execution_layer/src/test_utils/handle_rpc.rs b/beacon_node/execution_layer/src/test_utils/handle_rpc.rs new file mode 100644 index 0000000000..19c968f40b --- /dev/null +++ b/beacon_node/execution_layer/src/test_utils/handle_rpc.rs @@ -0,0 +1,10 @@ +use super::Context; +use std::sync::Arc; +use types::EthSpec; + +pub async fn handle_rpc( + body: serde_json::Value, + ctx: Arc>, +) -> Result { + todo!("handle_rpc") +} diff --git a/beacon_node/execution_layer/src/test_utils/mod.rs b/beacon_node/execution_layer/src/test_utils/mod.rs index 670eb54513..c7383e2bc8 100644 --- a/beacon_node/execution_layer/src/test_utils/mod.rs +++ b/beacon_node/execution_layer/src/test_utils/mod.rs @@ -1,6 +1,7 @@ use bytes::Bytes; use environment::null_logger; use execution_block_generator::ExecutionBlockGenerator; +use handle_rpc::handle_rpc; use serde::{Deserialize, Serialize}; use slog::{info, Logger}; use std::future::Future; @@ -15,6 +16,7 @@ const DEFAULT_TERMINAL_DIFFICULTY: u64 = 6400; const DEFAULT_TERMINAL_BLOCK: u64 = 64; mod execution_block_generator; +mod handle_rpc; pub struct MockServer { _shutdown_tx: oneshot::Sender<()>, @@ -139,16 +141,38 @@ pub fn serve( let log = ctx.log.clone(); let inner_ctx = ctx.clone(); - let routes = warp::post() - .and(warp::path("echo")) + let ctx_filter = warp::any().map(move || inner_ctx.clone()); + + // `/` + // + // Handles actual JSON-RPC requests. + let root = warp::path::end() + .and(warp::body::json()) + .and(ctx_filter.clone()) + .and_then(|body: serde_json::Value, ctx: Arc>| async move { + let response = handle_rpc(body, ctx).await; + Ok::<_, warp::reject::Rejection>( + warp::http::Response::builder() + .status(200) + .body(serde_json::to_string(&response).expect("response must be valid JSON")), + ) + }); + + // `/echo` + // + // Sends the body of the request to `ctx.last_echo_request` so we can inspect requests. + let echo = warp::path("echo") .and(warp::body::bytes()) - .and(warp::any().map(move || inner_ctx.clone())) + .and(ctx_filter.clone()) .and_then(|bytes: Bytes, ctx: Arc>| async move { *ctx.last_echo_request.write().await = Some(bytes.clone()); Ok::<_, warp::reject::Rejection>( warp::http::Response::builder().status(200).body(bytes), ) - }) + }); + + let routes = warp::post() + .and(root.or(echo)) // Add a `Server` header. .map(|reply| warp::reply::with_header(reply, "Server", "lighthouse-mock-execution-client"));