diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 49971445bca..99a9e15ae86 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -3053,7 +3053,8 @@ pub fn serve( task_spawner: TaskSpawner, chain: Arc>| { task_spawner.spawn_async_with_rejection(Priority::P0, async move { - produce_blinded_block_v2(EndpointVersion(2), accept_header, chain, slot, query).await + produce_blinded_block_v2(EndpointVersion(2), accept_header, chain, slot, query) + .await }) }, ); diff --git a/beacon_node/http_api/src/validator.rs b/beacon_node/http_api/src/validator.rs index 48549f65583..3db18c7f020 100644 --- a/beacon_node/http_api/src/validator.rs +++ b/beacon_node/http_api/src/validator.rs @@ -1,6 +1,6 @@ -use types::{payload::BlockProductionVersion, *}; use bytes::Bytes; use std::sync::Arc; +use types::{payload::BlockProductionVersion, *}; use beacon_chain::{ BeaconBlockAndStateResponse, BeaconChain, BeaconChainError, BeaconChainTypes, @@ -82,111 +82,10 @@ pub async fn produce_blinded_block_v2( match block_response { BeaconBlockAndStateResponse::Full((block, _, _)) => { - let fork_name = block - .to_ref() - .fork_name(&chain.spec) - .map_err(inconsistent_fork_rejection)?; - - match accept_header { - Some(api_types::Accept::Ssz) => Response::builder() - .status(200) - .header("Content-Type", "application/octet-stream") - .body(block.as_ssz_bytes().into()) - .map(|res: Response| add_consensus_version_header(res, fork_name)) - .map_err(|e| { - warp_utils::reject::custom_server_error(format!( - "failed to create response: {}", - e - )) - }), - _ => fork_versioned_response(endpoint_version, fork_name, block) - .map(|response| warp::reply::json(&response).into_response()) - .map(|res| add_consensus_version_header(res, fork_name)), - } + build_response_v2(chain, block, endpoint_version) } BeaconBlockAndStateResponse::Blinded((block, _, _)) => { - let fork_name = block - .to_ref() - .fork_name(&chain.spec) - .map_err(inconsistent_fork_rejection)?; - - match accept_header { - Some(api_types::Accept::Ssz) => Response::builder() - .status(200) - .header("Content-Type", "application/octet-stream") - .body(block.as_ssz_bytes().into()) - .map(|res: Response| add_consensus_version_header(res, fork_name)) - .map_err(|e| { - warp_utils::reject::custom_server_error(format!( - "failed to create response: {}", - e - )) - }), - _ => fork_versioned_response(endpoint_version, fork_name, block) - .map(|response| warp::reply::json(&response).into_response()) - .map(|res| add_consensus_version_header(res, fork_name)), - } - } - } -} - -pub async fn produce_block_v2( - endpoint_version: EndpointVersion, - accept_header: Option, - chain: Arc>, - slot: Slot, - query: api_types::ValidatorBlocksQuery, -) -> Result, warp::Rejection> { - let randao_reveal = query.randao_reveal.decompress().map_err(|e| { - warp_utils::reject::custom_bad_request(format!( - "randao reveal is not a valid BLS signature: {:?}", - e - )) - })?; - - let randao_verification = get_randao_verification(&query, randao_reveal.is_infinity())?; - - let block_response = chain - .produce_block_with_verification( - randao_reveal, - slot, - query.graffiti.map(Into::into), - randao_verification, - BlockProductionVersion::FullV2, - ) - .await - .map_err(warp_utils::reject::block_production_error)?; - - match block_response { - BeaconBlockAndStateResponse::Full((block, _, _)) => { - let fork_name = block - .to_ref() - .fork_name(&chain.spec) - .map_err(inconsistent_fork_rejection)?; - - match accept_header { - Some(api_types::Accept::Ssz) => Response::builder() - .status(200) - .header("Content-Type", "application/octet-stream") - .body(block.as_ssz_bytes().into()) - .map(|res: Response| { - add_consensus_version_header(res, fork_name) - }) - .map_err(|e| { - warp_utils::reject::custom_server_error(format!( - "failed to create response: {}", - e - )) - }), - _ => fork_versioned_response(endpoint_version, fork_name, block) - .map(|response| warp::reply::json(&response).into_response()) - .map(|res| add_consensus_version_header(res, fork_name)), - } - } - BeaconBlockAndStateResponse::Blinded((_, _, _)) => { - Err(warp_utils::reject::custom_server_error( - "Returned a blinded block. It should be impossible to return a blinded block via the Full Payload V2 block fetching flow.".to_string() - )) + build_response_v2(chain, block, endpoint_version) } } } @@ -326,3 +225,69 @@ pub fn generate_json_response_v3< .map(|res| add_execution_payload_blinded_header(res, blinded_payload_flag)) .map(|res: Response| add_execution_payload_value_header(res, block_value)) } + + + +pub async fn produce_block_v2( + endpoint_version: EndpointVersion, + accept_header: Option, + chain: Arc>, + slot: Slot, + query: api_types::ValidatorBlocksQuery, +) -> Result, warp::Rejection> { + let randao_reveal = query.randao_reveal.decompress().map_err(|e| { + warp_utils::reject::custom_bad_request(format!( + "randao reveal is not a valid BLS signature: {:?}", + e + )) + })?; + + let randao_verification = get_randao_verification(&query, randao_reveal.is_infinity())?; + + let block_response = chain + .produce_block_with_verification( + randao_reveal, + slot, + query.graffiti.map(Into::into), + randao_verification, + BlockProductionVersion::FullV2, + ) + .await + .map_err(warp_utils::reject::block_production_error)?; + + match block_response { + BeaconBlockAndStateResponse::Full((block, _, _)) => { + build_response_v2(chain, block, endpoint_version) + } + BeaconBlockAndStateResponse::Blinded((_, _, _)) => { + Err(warp_utils::reject::custom_server_error( + "Returned a blinded block. It should be impossible to return a blinded block via the Full Payload V2 block fetching flow.".to_string() + )) + } + } +} + +pub fn build_response_v2>( + chain: Arc>, + block: BeaconBlock, + endpoint_version: EndpointVersion, +) -> Result, warp::Rejection> { + let fork_name = block + .to_ref() + .fork_name(&chain.spec) + .map_err(inconsistent_fork_rejection)?; + + match accept_header { + Some(api_types::Accept::Ssz) => Response::builder() + .status(200) + .header("Content-Type", "application/octet-stream") + .body(block.as_ssz_bytes().into()) + .map(|res: Response| add_consensus_version_header(res, fork_name)) + .map_err(|e| { + warp_utils::reject::custom_server_error(format!("failed to create response: {}", e)) + }), + _ => fork_versioned_response(endpoint_version, fork_name, block) + .map(|response| warp::reply::json(&response).into_response()) + .map(|res| add_consensus_version_header(res, fork_name)), + } +} diff --git a/common/eth2/src/lib.rs b/common/eth2/src/lib.rs index 033087c1bf9..1071ec14d03 100644 --- a/common/eth2/src/lib.rs +++ b/common/eth2/src/lib.rs @@ -1671,8 +1671,8 @@ impl BeaconNodeHttpClient { .await } - /// `GET v3/validator/blocks/{slot}` - pub async fn get_validator_blocks_v3_modular( + /// `GET v3/validator/blocks/{slot}` + pub async fn get_validator_blocks_v3_modular( &self, slot: Slot, randao_reveal: &SignatureBytes,