Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eserilev committed Sep 27, 2023
1 parent c68e7af commit df25083
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 107 deletions.
3 changes: 2 additions & 1 deletion beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3053,7 +3053,8 @@ pub fn serve<T: BeaconChainTypes>(
task_spawner: TaskSpawner<T::EthSpec>,
chain: Arc<BeaconChain<T>>| {
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
})
},
);
Expand Down
173 changes: 69 additions & 104 deletions beacon_node/http_api/src/validator.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -82,111 +82,10 @@ pub async fn produce_blinded_block_v2<T: BeaconChainTypes>(

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<Bytes>| 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<Bytes>| 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<T: BeaconChainTypes>(
endpoint_version: EndpointVersion,
accept_header: Option<api_types::Accept>,
chain: Arc<BeaconChain<T>>,
slot: Slot,
query: api_types::ValidatorBlocksQuery,
) -> Result<Response<Body>, 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<Bytes>| {
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)
}
}
}
Expand Down Expand Up @@ -326,3 +225,69 @@ pub fn generate_json_response_v3<
.map(|res| add_execution_payload_blinded_header(res, blinded_payload_flag))
.map(|res: Response<Body>| add_execution_payload_value_header(res, block_value))
}



pub async fn produce_block_v2<T: BeaconChainTypes>(
endpoint_version: EndpointVersion,
accept_header: Option<api_types::Accept>,
chain: Arc<BeaconChain<T>>,
slot: Slot,
query: api_types::ValidatorBlocksQuery,
) -> Result<Response<Body>, 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<E: EthSpec, Payload: AbstractExecPayload<E>>(
chain: Arc<BeaconChain<T>>,
block: BeaconBlock<E, Payload>,
endpoint_version: EndpointVersion,
) -> Result<Response<Body>, 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<Bytes>| 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)),
}
}
4 changes: 2 additions & 2 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,8 +1671,8 @@ impl BeaconNodeHttpClient {
.await
}

/// `GET v3/validator/blocks/{slot}`
pub async fn get_validator_blocks_v3_modular<T: EthSpec>(
/// `GET v3/validator/blocks/{slot}`
pub async fn get_validator_blocks_v3_modular<T: EthSpec>(
&self,
slot: Slot,
randao_reveal: &SignatureBytes,
Expand Down

0 comments on commit df25083

Please sign in to comment.