mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 09:16:00 +00:00
Add the /beacon/heads API endpoint
This commit is contained in:
@@ -336,10 +336,17 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
|||||||
self.canonical_head.read().clone()
|
self.canonical_head.read().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the current heads of the `BeaconChain`. For the canonical head, see `Self::head`.
|
||||||
|
///
|
||||||
|
/// Returns `(block_root, block_slot)`.
|
||||||
|
pub fn heads(&self) -> Vec<(Hash256, Slot)> {
|
||||||
|
self.head_tracker.heads()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the `BeaconState` at the given slot.
|
/// Returns the `BeaconState` at the given slot.
|
||||||
///
|
///
|
||||||
/// Returns `None` when the state is not found in the database or there is an error skipping
|
/// Returns `None` when the state is not found in the database or there is an error skipping
|
||||||
/// to a future state.
|
/// to a future state.
|
||||||
pub fn state_at_slot(&self, slot: Slot) -> Result<BeaconState<T::EthSpec>, Error> {
|
pub fn state_at_slot(&self, slot: Slot) -> Result<BeaconState<T::EthSpec>, Error> {
|
||||||
let head_state = self.head().beacon_state;
|
let head_state = self.head().beacon_state;
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,29 @@ pub fn get_head<T: BeaconChainTypes>(
|
|||||||
ResponseBuilder::new(&req)?.body(&head)
|
ResponseBuilder::new(&req)?.body(&head)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Encode)]
|
||||||
|
pub struct HeadBeaconBlock {
|
||||||
|
beacon_block_root: Hash256,
|
||||||
|
beacon_block_slot: Slot,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// HTTP handler to return a list of head BeaconBlocks.
|
||||||
|
pub fn get_heads<T: BeaconChainTypes>(
|
||||||
|
req: Request<Body>,
|
||||||
|
beacon_chain: Arc<BeaconChain<T>>,
|
||||||
|
) -> ApiResult {
|
||||||
|
let heads = beacon_chain
|
||||||
|
.heads()
|
||||||
|
.into_iter()
|
||||||
|
.map(|(beacon_block_root, beacon_block_slot)| HeadBeaconBlock {
|
||||||
|
beacon_block_root,
|
||||||
|
beacon_block_slot,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
ResponseBuilder::new(&req)?.body(&heads)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Encode)]
|
#[derive(Serialize, Encode)]
|
||||||
#[serde(bound = "T: EthSpec")]
|
#[serde(bound = "T: EthSpec")]
|
||||||
pub struct BlockResponse<T: EthSpec> {
|
pub struct BlockResponse<T: EthSpec> {
|
||||||
|
|||||||
@@ -64,6 +64,9 @@ pub fn route<T: BeaconChainTypes>(
|
|||||||
|
|
||||||
// Methods for Beacon Node
|
// Methods for Beacon Node
|
||||||
(&Method::GET, "/beacon/head") => into_boxfut(beacon::get_head::<T>(req, beacon_chain)),
|
(&Method::GET, "/beacon/head") => into_boxfut(beacon::get_head::<T>(req, beacon_chain)),
|
||||||
|
(&Method::GET, "/beacon/heads") => {
|
||||||
|
into_boxfut(beacon::get_heads::<T>(req, beacon_chain))
|
||||||
|
}
|
||||||
(&Method::GET, "/beacon/block") => {
|
(&Method::GET, "/beacon/block") => {
|
||||||
into_boxfut(beacon::get_block::<T>(req, beacon_chain))
|
into_boxfut(beacon::get_block::<T>(req, beacon_chain))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user