Skip to content

Commit

Permalink
Add finalized flag to /eth/v2/beacon/blocks/{block_id}
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrachi1 committed Nov 29, 2022
1 parent bf533c8 commit ee990af
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
34 changes: 33 additions & 1 deletion beacon_node/http_api/src/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eth2::types::BlockId as CoreBlockId;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use types::{Hash256, SignedBeaconBlock, SignedBlindedBeaconBlock, Slot};
use types::{EthSpec, Hash256, SignedBeaconBlock, SignedBlindedBeaconBlock, Slot};

/// Wraps `eth2::types::BlockId` and provides a simple way to obtain a block or root for a given
/// `BlockId`.
Expand Down Expand Up @@ -211,6 +211,38 @@ impl BlockId {
}
}
}

pub fn is_finalized<T: BeaconChainTypes>(&self, chain: &BeaconChain<T>) -> bool {
match &self.0 {
CoreBlockId::Head => false,
CoreBlockId::Genesis => true,
CoreBlockId::Finalized => true,
CoreBlockId::Justified => false,
CoreBlockId::Slot(slot) => {
let finalized_epoch = chain
.canonical_head
.cached_head()
.finalized_checkpoint()
.epoch;
let block_epoch = slot.epoch(T::EthSpec::slots_per_epoch());
block_epoch <= finalized_epoch
}
CoreBlockId::Root(root) => {
let finalized_epoch = chain
.canonical_head
.cached_head()
.finalized_checkpoint()
.epoch;
let block_epoch = chain
.get_blinded_block(root)
.unwrap()
.unwrap()
.slot()
.epoch(T::EthSpec::slots_per_epoch());
block_epoch <= finalized_epoch
}
}
}
}

impl FromStr for BlockId {
Expand Down
9 changes: 6 additions & 3 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ use types::{
SyncContributionData,
};
use version::{
add_consensus_version_header, execution_optimistic_fork_versioned_response,
fork_versioned_response, inconsistent_fork_rejection, unsupported_version_rejection, V1, V2,
add_consensus_version_header, execution_optimistic_finalized_fork_versioned_response,
execution_optimistic_fork_versioned_response, fork_versioned_response,
inconsistent_fork_rejection, unsupported_version_rejection, V1, V2,
};
use warp::http::StatusCode;
use warp::sse::Event;
Expand Down Expand Up @@ -1179,6 +1180,7 @@ pub fn serve<T: BeaconChainTypes>(
accept_header: Option<api_types::Accept>| {
async move {
let (block, execution_optimistic) = block_id.full_block(&chain).await?;
let finalized = block_id.is_finalized(&chain);
let fork_name = block
.fork_name(&chain.spec)
.map_err(inconsistent_fork_rejection)?;
Expand All @@ -1194,10 +1196,11 @@ pub fn serve<T: BeaconChainTypes>(
e
))
}),
_ => execution_optimistic_fork_versioned_response(
_ => execution_optimistic_finalized_fork_versioned_response(
endpoint_version,
fork_name,
execution_optimistic,
finalized,
block,
)
.map(|res| warp::reply::json(&res).into_response()),
Expand Down
25 changes: 24 additions & 1 deletion beacon_node/http_api/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::api_types::{
EndpointVersion, ExecutionOptimisticForkVersionedResponse, ForkVersionedResponse,
EndpointVersion, ExecutionOptimisticFinalizedForkVersionedResponse,
ExecutionOptimisticForkVersionedResponse, ForkVersionedResponse,
};
use eth2::CONSENSUS_VERSION_HEADER;
use serde::Serialize;
Expand Down Expand Up @@ -47,6 +48,28 @@ pub fn execution_optimistic_fork_versioned_response<T: Serialize>(
})
}

pub fn execution_optimistic_finalized_fork_versioned_response<T: Serialize>(
endpoint_version: EndpointVersion,
fork_name: ForkName,
execution_optimistic: bool,
finalized: bool,
data: T,
) -> Result<ExecutionOptimisticFinalizedForkVersionedResponse<T>, warp::reject::Rejection> {
let fork_name = if endpoint_version == V1 {
None
} else if endpoint_version == V2 {
Some(fork_name)
} else {
return Err(unsupported_version_rejection(endpoint_version));
};
Ok(ExecutionOptimisticFinalizedForkVersionedResponse {
version: fork_name,
execution_optimistic: Some(execution_optimistic),
finalized: Some(finalized),
data,
})
}

/// Add the `Eth-Consensus-Version` header to a response.
pub fn add_consensus_version_header<T: Reply>(reply: T, fork_name: ForkName) -> WithHeader<T> {
reply::with_header(reply, CONSENSUS_VERSION_HEADER, fork_name.to_string())
Expand Down
9 changes: 9 additions & 0 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ pub struct ExecutionOptimisticForkVersionedResponse<T> {
pub data: T,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct ExecutionOptimisticFinalizedForkVersionedResponse<T> {
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<ForkName>,
pub execution_optimistic: Option<bool>,
pub finalized: Option<bool>,
pub data: T,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct ForkVersionedResponse<T> {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down

0 comments on commit ee990af

Please sign in to comment.