mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 19:51:47 +00:00
Block v3 endpoint (#4629)
## Issue Addressed #4582 ## Proposed Changes Add a new v3 block fetching flow that can decide to return a Full OR Blinded payload ## Additional Info Co-authored-by: Michael Sproul <micsproul@gmail.com>
This commit is contained in:
@@ -1,62 +1,51 @@
|
||||
use beacon_chain::BlockProductionError;
|
||||
use eth2::types::{BeaconBlockAndBlobSidecars, BlindedBeaconBlockAndBlobSidecars, BlockContents};
|
||||
use types::{
|
||||
BeaconBlock, BlindedBlobSidecarList, BlindedPayload, BlobSidecarList, EthSpec, ForkName,
|
||||
FullPayload,
|
||||
};
|
||||
|
||||
use types::{AbstractExecPayload, BeaconBlock, EthSpec, ForkName, SidecarList};
|
||||
type Error = warp::reject::Rejection;
|
||||
type FullBlockContents<E> = BlockContents<E, FullPayload<E>>;
|
||||
type BlindedBlockContents<E> = BlockContents<E, BlindedPayload<E>>;
|
||||
|
||||
pub fn build_block_contents<E: EthSpec>(
|
||||
pub fn build_block_contents<E: EthSpec, Payload: AbstractExecPayload<E>>(
|
||||
fork_name: ForkName,
|
||||
block: BeaconBlock<E, FullPayload<E>>,
|
||||
maybe_blobs: Option<BlobSidecarList<E>>,
|
||||
) -> Result<FullBlockContents<E>, Error> {
|
||||
match fork_name {
|
||||
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => {
|
||||
Ok(BlockContents::Block(block))
|
||||
}
|
||||
ForkName::Deneb => {
|
||||
if let Some(blob_sidecars) = maybe_blobs {
|
||||
let block_and_blobs = BeaconBlockAndBlobSidecars {
|
||||
block,
|
||||
blob_sidecars,
|
||||
};
|
||||
|
||||
Ok(BlockContents::BlockAndBlobSidecars(block_and_blobs))
|
||||
} else {
|
||||
Err(warp_utils::reject::block_production_error(
|
||||
BlockProductionError::MissingBlobs,
|
||||
))
|
||||
block: BeaconBlock<E, Payload>,
|
||||
maybe_blobs: Option<SidecarList<E, <Payload as AbstractExecPayload<E>>::Sidecar>>,
|
||||
) -> Result<BlockContents<E, Payload>, Error> {
|
||||
match Payload::block_type() {
|
||||
types::BlockType::Blinded => match fork_name {
|
||||
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => {
|
||||
Ok(BlockContents::Block(block))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_blinded_block_contents<E: EthSpec>(
|
||||
fork_name: ForkName,
|
||||
block: BeaconBlock<E, BlindedPayload<E>>,
|
||||
maybe_blobs: Option<BlindedBlobSidecarList<E>>,
|
||||
) -> Result<BlindedBlockContents<E>, Error> {
|
||||
match fork_name {
|
||||
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => {
|
||||
Ok(BlockContents::Block(block))
|
||||
}
|
||||
ForkName::Deneb => {
|
||||
if let Some(blinded_blob_sidecars) = maybe_blobs {
|
||||
let block_and_blobs = BlindedBeaconBlockAndBlobSidecars {
|
||||
blinded_block: block,
|
||||
blinded_blob_sidecars,
|
||||
};
|
||||
|
||||
Ok(BlockContents::BlindedBlockAndBlobSidecars(block_and_blobs))
|
||||
} else {
|
||||
Err(warp_utils::reject::block_production_error(
|
||||
BlockProductionError::MissingBlobs,
|
||||
))
|
||||
}
|
||||
}
|
||||
ForkName::Deneb => {
|
||||
if let Some(blinded_blob_sidecars) = maybe_blobs {
|
||||
let block_and_blobs = BlindedBeaconBlockAndBlobSidecars {
|
||||
blinded_block: block,
|
||||
blinded_blob_sidecars,
|
||||
};
|
||||
|
||||
Ok(BlockContents::BlindedBlockAndBlobSidecars(block_and_blobs))
|
||||
} else {
|
||||
Err(warp_utils::reject::block_production_error(
|
||||
BlockProductionError::MissingBlobs,
|
||||
))
|
||||
}
|
||||
}
|
||||
},
|
||||
types::BlockType::Full => match fork_name {
|
||||
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => {
|
||||
Ok(BlockContents::Block(block))
|
||||
}
|
||||
ForkName::Deneb => {
|
||||
if let Some(blob_sidecars) = maybe_blobs {
|
||||
let block_and_blobs = BeaconBlockAndBlobSidecars {
|
||||
block,
|
||||
blob_sidecars,
|
||||
};
|
||||
|
||||
Ok(BlockContents::BlockAndBlobSidecars(block_and_blobs))
|
||||
} else {
|
||||
Err(warp_utils::reject::block_production_error(
|
||||
BlockProductionError::MissingBlobs,
|
||||
))
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ mod build_block_contents;
|
||||
mod builder_states;
|
||||
mod database;
|
||||
mod metrics;
|
||||
mod produce_block;
|
||||
mod proposer_duties;
|
||||
mod publish_blocks;
|
||||
mod standard_block_rewards;
|
||||
@@ -27,10 +28,11 @@ mod validator;
|
||||
mod validator_inclusion;
|
||||
mod version;
|
||||
|
||||
use crate::produce_block::{produce_blinded_block_v2, produce_block_v2, produce_block_v3};
|
||||
use beacon_chain::{
|
||||
attestation_verification::VerifiedAttestation, observed_operations::ObservationOutcome,
|
||||
validator_monitor::timestamp_now, AttestationError as AttnError, BeaconChain, BeaconChainError,
|
||||
BeaconChainTypes, ProduceBlockVerification, WhenSlotSkipped,
|
||||
BeaconChainTypes, WhenSlotSkipped,
|
||||
};
|
||||
use beacon_processor::BeaconProcessorSend;
|
||||
pub use block_id::BlockId;
|
||||
@@ -39,8 +41,7 @@ use bytes::Bytes;
|
||||
use directory::DEFAULT_ROOT_DIR;
|
||||
use eth2::types::{
|
||||
self as api_types, BroadcastValidation, EndpointVersion, ForkChoice, ForkChoiceNode,
|
||||
SignedBlindedBlockContents, SignedBlockContents, SkipRandaoVerification, ValidatorId,
|
||||
ValidatorStatus,
|
||||
SignedBlindedBlockContents, SignedBlockContents, ValidatorId, ValidatorStatus,
|
||||
};
|
||||
use lighthouse_network::{types::SyncState, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
|
||||
use lighthouse_version::version_with_platform;
|
||||
@@ -75,7 +76,7 @@ use tokio_stream::{
|
||||
};
|
||||
use types::{
|
||||
Attestation, AttestationData, AttestationShufflingId, AttesterSlashing, BeaconStateError,
|
||||
BlindedPayload, CommitteeCache, ConfigAndPreset, Epoch, EthSpec, ForkName, FullPayload,
|
||||
BlindedPayload, CommitteeCache, ConfigAndPreset, Epoch, EthSpec, ForkName,
|
||||
ProposerPreparationData, ProposerSlashing, RelativeEpoch, SignedAggregateAndProof,
|
||||
SignedBlsToExecutionChange, SignedContributionAndProof, SignedValidatorRegistrationData,
|
||||
SignedVoluntaryExit, Slot, SyncCommitteeMessage, SyncContributionData,
|
||||
@@ -83,7 +84,7 @@ use types::{
|
||||
use validator::pubkey_to_validator_index;
|
||||
use version::{
|
||||
add_consensus_version_header, execution_optimistic_finalized_fork_versioned_response,
|
||||
fork_versioned_response, inconsistent_fork_rejection, unsupported_version_rejection, V1, V2,
|
||||
inconsistent_fork_rejection, unsupported_version_rejection, V1, V2, V3,
|
||||
};
|
||||
use warp::http::StatusCode;
|
||||
use warp::sse::Event;
|
||||
@@ -3052,17 +3053,17 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
))
|
||||
}))
|
||||
.and(warp::path::end())
|
||||
.and(warp::header::optional::<api_types::Accept>("accept"))
|
||||
.and(not_while_syncing_filter.clone())
|
||||
.and(warp::query::<api_types::ValidatorBlocksQuery>())
|
||||
.and(warp::header::optional::<api_types::Accept>("accept"))
|
||||
.and(task_spawner_filter.clone())
|
||||
.and(chain_filter.clone())
|
||||
.and(log_filter.clone())
|
||||
.then(
|
||||
|endpoint_version: EndpointVersion,
|
||||
slot: Slot,
|
||||
query: api_types::ValidatorBlocksQuery,
|
||||
accept_header: Option<api_types::Accept>,
|
||||
query: api_types::ValidatorBlocksQuery,
|
||||
task_spawner: TaskSpawner<T::EthSpec>,
|
||||
chain: Arc<BeaconChain<T>>,
|
||||
log: Logger| {
|
||||
@@ -3073,60 +3074,10 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
"slot" => slot
|
||||
);
|
||||
|
||||
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 =
|
||||
if query.skip_randao_verification == SkipRandaoVerification::Yes {
|
||||
if !randao_reveal.is_infinity() {
|
||||
return Err(warp_utils::reject::custom_bad_request(
|
||||
"randao_reveal must be point-at-infinity if verification is skipped"
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
ProduceBlockVerification::NoVerification
|
||||
} else {
|
||||
ProduceBlockVerification::VerifyRandao
|
||||
};
|
||||
|
||||
let (block, _, maybe_blobs) = chain
|
||||
.produce_block_with_verification::<FullPayload<T::EthSpec>>(
|
||||
randao_reveal,
|
||||
slot,
|
||||
query.graffiti.map(Into::into),
|
||||
randao_verification,
|
||||
)
|
||||
.await
|
||||
.map_err(warp_utils::reject::block_production_error)?;
|
||||
let fork_name = block
|
||||
.to_ref()
|
||||
.fork_name(&chain.spec)
|
||||
.map_err(inconsistent_fork_rejection)?;
|
||||
|
||||
let block_contents =
|
||||
build_block_contents::build_block_contents(fork_name, block, maybe_blobs)?;
|
||||
|
||||
match accept_header {
|
||||
Some(api_types::Accept::Ssz) => Response::builder()
|
||||
.status(200)
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.body(block_contents.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_contents)
|
||||
.map(|response| warp::reply::json(&response).into_response())
|
||||
.map(|res| add_consensus_version_header(res, fork_name)),
|
||||
if endpoint_version == V3 {
|
||||
produce_block_v3(endpoint_version, accept_header, chain, slot, query).await
|
||||
} else {
|
||||
produce_block_v2(endpoint_version, accept_header, chain, slot, query).await
|
||||
}
|
||||
})
|
||||
},
|
||||
@@ -3154,65 +3105,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 {
|
||||
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 =
|
||||
if query.skip_randao_verification == SkipRandaoVerification::Yes {
|
||||
if !randao_reveal.is_infinity() {
|
||||
return Err(warp_utils::reject::custom_bad_request(
|
||||
"randao_reveal must be point-at-infinity if verification is skipped"
|
||||
.into()
|
||||
));
|
||||
}
|
||||
ProduceBlockVerification::NoVerification
|
||||
} else {
|
||||
ProduceBlockVerification::VerifyRandao
|
||||
};
|
||||
|
||||
let (block, _, maybe_blobs) = chain
|
||||
.produce_block_with_verification::<BlindedPayload<T::EthSpec>>(
|
||||
randao_reveal,
|
||||
slot,
|
||||
query.graffiti.map(Into::into),
|
||||
randao_verification,
|
||||
)
|
||||
produce_blinded_block_v2(EndpointVersion(2), accept_header, chain, slot, query)
|
||||
.await
|
||||
.map_err(warp_utils::reject::block_production_error)?;
|
||||
let fork_name = block
|
||||
.to_ref()
|
||||
.fork_name(&chain.spec)
|
||||
.map_err(inconsistent_fork_rejection)?;
|
||||
|
||||
let block_contents = build_block_contents::build_blinded_block_contents(
|
||||
fork_name,
|
||||
block,
|
||||
maybe_blobs,
|
||||
)?;
|
||||
|
||||
match accept_header {
|
||||
Some(api_types::Accept::Ssz) => Response::builder()
|
||||
.status(200)
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.body(block_contents.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
|
||||
))
|
||||
}),
|
||||
// Pose as a V2 endpoint so we return the fork `version`.
|
||||
_ => fork_versioned_response(V2, fork_name, block_contents)
|
||||
.map(|response| warp::reply::json(&response).into_response())
|
||||
.map(|res| add_consensus_version_header(res, fork_name)),
|
||||
}
|
||||
})
|
||||
},
|
||||
);
|
||||
@@ -3740,7 +3634,6 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
.as_ref()
|
||||
.ok_or(BeaconChainError::BuilderMissing)
|
||||
.map_err(warp_utils::reject::beacon_chain_error)?;
|
||||
|
||||
builder
|
||||
.post_builder_validators(&filtered_registration_data)
|
||||
.await
|
||||
|
||||
231
beacon_node/http_api/src/produce_block.rs
Normal file
231
beacon_node/http_api/src/produce_block.rs
Normal file
@@ -0,0 +1,231 @@
|
||||
use bytes::Bytes;
|
||||
use std::sync::Arc;
|
||||
use types::{payload::BlockProductionVersion, *};
|
||||
|
||||
use beacon_chain::{
|
||||
BeaconBlockResponse, BeaconBlockResponseType, BeaconChain, BeaconChainTypes,
|
||||
ProduceBlockVerification,
|
||||
};
|
||||
use eth2::types::{self as api_types, EndpointVersion, SkipRandaoVerification};
|
||||
use ssz::Encode;
|
||||
use warp::{
|
||||
hyper::{Body, Response},
|
||||
Reply,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
build_block_contents,
|
||||
version::{
|
||||
add_consensus_block_value_header, add_consensus_version_header,
|
||||
add_execution_payload_blinded_header, add_execution_payload_value_header,
|
||||
fork_versioned_response, inconsistent_fork_rejection,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn get_randao_verification(
|
||||
query: &api_types::ValidatorBlocksQuery,
|
||||
randao_reveal_infinity: bool,
|
||||
) -> Result<ProduceBlockVerification, warp::Rejection> {
|
||||
let randao_verification = if query.skip_randao_verification == SkipRandaoVerification::Yes {
|
||||
if !randao_reveal_infinity {
|
||||
return Err(warp_utils::reject::custom_bad_request(
|
||||
"randao_reveal must be point-at-infinity if verification is skipped".into(),
|
||||
));
|
||||
}
|
||||
ProduceBlockVerification::NoVerification
|
||||
} else {
|
||||
ProduceBlockVerification::VerifyRandao
|
||||
};
|
||||
|
||||
Ok(randao_verification)
|
||||
}
|
||||
|
||||
pub async fn produce_block_v3<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_type = chain
|
||||
.produce_block_with_verification(
|
||||
randao_reveal,
|
||||
slot,
|
||||
query.graffiti.map(Into::into),
|
||||
randao_verification,
|
||||
BlockProductionVersion::V3,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
warp_utils::reject::custom_bad_request(format!("failed to fetch a block: {:?}", e))
|
||||
})?;
|
||||
|
||||
match block_response_type {
|
||||
BeaconBlockResponseType::Full(block_response) => {
|
||||
build_response_v3(chain, block_response, endpoint_version, accept_header)
|
||||
}
|
||||
BeaconBlockResponseType::Blinded(block_response) => {
|
||||
build_response_v3(chain, block_response, endpoint_version, accept_header)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_response_v3<T: BeaconChainTypes, E: EthSpec, Payload: AbstractExecPayload<E>>(
|
||||
chain: Arc<BeaconChain<T>>,
|
||||
block_response: BeaconBlockResponse<E, Payload>,
|
||||
endpoint_version: EndpointVersion,
|
||||
accept_header: Option<api_types::Accept>,
|
||||
) -> Result<Response<Body>, warp::Rejection> {
|
||||
let fork_name = block_response
|
||||
.block
|
||||
.to_ref()
|
||||
.fork_name(&chain.spec)
|
||||
.map_err(inconsistent_fork_rejection)?;
|
||||
|
||||
let block_contents = build_block_contents::build_block_contents(
|
||||
fork_name,
|
||||
block_response.block,
|
||||
block_response.maybe_side_car,
|
||||
)?;
|
||||
|
||||
let execution_payload_blinded = Payload::block_type() == BlockType::Blinded;
|
||||
|
||||
match accept_header {
|
||||
Some(api_types::Accept::Ssz) => Response::builder()
|
||||
.status(200)
|
||||
.header("Content-Type", "application/ssz")
|
||||
.body(block_contents.as_ssz_bytes().into())
|
||||
.map(|res: Response<Body>| add_consensus_version_header(res, fork_name))
|
||||
.map(|res| add_execution_payload_blinded_header(res, execution_payload_blinded))
|
||||
.map(|res: Response<Body>| {
|
||||
add_execution_payload_value_header(res, block_response.execution_payload_value)
|
||||
})
|
||||
.map(|res| add_consensus_block_value_header(res, block_response.consensus_block_value))
|
||||
.map_err(|e| -> warp::Rejection {
|
||||
warp_utils::reject::custom_server_error(format!("failed to create response: {}", e))
|
||||
}),
|
||||
_ => fork_versioned_response(endpoint_version, fork_name, block_contents)
|
||||
.map(|response| warp::reply::json(&response).into_response())
|
||||
.map(|res| add_consensus_version_header(res, fork_name))
|
||||
.map(|res| add_execution_payload_blinded_header(res, execution_payload_blinded))
|
||||
.map(|res| {
|
||||
add_execution_payload_value_header(res, block_response.execution_payload_value)
|
||||
})
|
||||
.map(|res| add_consensus_block_value_header(res, block_response.consensus_block_value)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn produce_blinded_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_type = chain
|
||||
.produce_block_with_verification(
|
||||
randao_reveal,
|
||||
slot,
|
||||
query.graffiti.map(Into::into),
|
||||
randao_verification,
|
||||
BlockProductionVersion::BlindedV2,
|
||||
)
|
||||
.await
|
||||
.map_err(warp_utils::reject::block_production_error)?;
|
||||
|
||||
match block_response_type {
|
||||
BeaconBlockResponseType::Full(block_response) => {
|
||||
build_response_v2(chain, block_response, endpoint_version, accept_header)
|
||||
}
|
||||
BeaconBlockResponseType::Blinded(block_response) => {
|
||||
build_response_v2(chain, block_response, endpoint_version, accept_header)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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_type = 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_type {
|
||||
BeaconBlockResponseType::Full(block_response) => {
|
||||
build_response_v2(chain, block_response, endpoint_version, accept_header)
|
||||
}
|
||||
BeaconBlockResponseType::Blinded(block_response) => {
|
||||
build_response_v2(chain, block_response, endpoint_version, accept_header)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_response_v2<T: BeaconChainTypes, E: EthSpec, Payload: AbstractExecPayload<E>>(
|
||||
chain: Arc<BeaconChain<T>>,
|
||||
block_response: BeaconBlockResponse<E, Payload>,
|
||||
endpoint_version: EndpointVersion,
|
||||
accept_header: Option<api_types::Accept>,
|
||||
) -> Result<Response<Body>, warp::Rejection> {
|
||||
let fork_name = block_response
|
||||
.block
|
||||
.to_ref()
|
||||
.fork_name(&chain.spec)
|
||||
.map_err(inconsistent_fork_rejection)?;
|
||||
|
||||
let block_contents = build_block_contents::build_block_contents(
|
||||
fork_name,
|
||||
block_response.block,
|
||||
block_response.maybe_side_car,
|
||||
)?;
|
||||
|
||||
match accept_header {
|
||||
Some(api_types::Accept::Ssz) => Response::builder()
|
||||
.status(200)
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.body(block_contents.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_contents)
|
||||
.map(|response| warp::reply::json(&response).into_response())
|
||||
.map(|res| add_consensus_version_header(res, fork_name)),
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use beacon_chain::{BeaconChain, BeaconChainError, BeaconChainTypes};
|
||||
use types::*;
|
||||
use types::{BeaconState, PublicKeyBytes};
|
||||
|
||||
/// Uses the `chain.validator_pubkey_cache` to resolve a pubkey to a validator
|
||||
/// index and then ensures that the validator exists in the given `state`.
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
use crate::api_types::fork_versioned_response::ExecutionOptimisticFinalizedForkVersionedResponse;
|
||||
use crate::api_types::EndpointVersion;
|
||||
use eth2::CONSENSUS_VERSION_HEADER;
|
||||
use eth2::{
|
||||
CONSENSUS_BLOCK_VALUE_HEADER, CONSENSUS_VERSION_HEADER, EXECUTION_PAYLOAD_BLINDED_HEADER,
|
||||
EXECUTION_PAYLOAD_VALUE_HEADER,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use types::{ForkName, ForkVersionedResponse, InconsistentFork};
|
||||
use types::{ForkName, ForkVersionedResponse, InconsistentFork, Uint256};
|
||||
use warp::reply::{self, Reply, Response};
|
||||
|
||||
pub const V1: EndpointVersion = EndpointVersion(1);
|
||||
pub const V2: EndpointVersion = EndpointVersion(2);
|
||||
pub const V3: EndpointVersion = EndpointVersion(3);
|
||||
|
||||
pub fn fork_versioned_response<T: Serialize>(
|
||||
endpoint_version: EndpointVersion,
|
||||
@@ -15,7 +19,7 @@ pub fn fork_versioned_response<T: Serialize>(
|
||||
) -> Result<ForkVersionedResponse<T>, warp::reject::Rejection> {
|
||||
let fork_name = if endpoint_version == V1 {
|
||||
None
|
||||
} else if endpoint_version == V2 {
|
||||
} else if endpoint_version == V2 || endpoint_version == V3 {
|
||||
Some(fork_name)
|
||||
} else {
|
||||
return Err(unsupported_version_rejection(endpoint_version));
|
||||
@@ -53,6 +57,45 @@ pub fn add_consensus_version_header<T: Reply>(reply: T, fork_name: ForkName) ->
|
||||
reply::with_header(reply, CONSENSUS_VERSION_HEADER, fork_name.to_string()).into_response()
|
||||
}
|
||||
|
||||
/// Add the `Eth-Execution-Payload-Blinded` header to a response.
|
||||
pub fn add_execution_payload_blinded_header<T: Reply>(
|
||||
reply: T,
|
||||
execution_payload_blinded: bool,
|
||||
) -> Response {
|
||||
reply::with_header(
|
||||
reply,
|
||||
EXECUTION_PAYLOAD_BLINDED_HEADER,
|
||||
execution_payload_blinded.to_string(),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
/// Add the `Eth-Execution-Payload-Value` header to a response.
|
||||
pub fn add_execution_payload_value_header<T: Reply>(
|
||||
reply: T,
|
||||
execution_payload_value: Option<Uint256>,
|
||||
) -> Response {
|
||||
reply::with_header(
|
||||
reply,
|
||||
EXECUTION_PAYLOAD_VALUE_HEADER,
|
||||
execution_payload_value.unwrap_or_default().to_string(),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
/// Add the `Eth-Consensus-Block-Value` header to a response.
|
||||
pub fn add_consensus_block_value_header<T: Reply>(
|
||||
reply: T,
|
||||
consensus_payload_value: Option<u64>,
|
||||
) -> Response {
|
||||
reply::with_header(
|
||||
reply,
|
||||
CONSENSUS_BLOCK_VALUE_HEADER,
|
||||
consensus_payload_value.unwrap_or_default().to_string(),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
pub fn inconsistent_fork_rejection(error: InconsistentFork) -> warp::reject::Rejection {
|
||||
warp_utils::reject::custom_server_error(format!("wrong fork: {:?}", error))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user