Add best_slot method
This commit is contained in:
parent
bb166a2599
commit
c97b3b20cb
@ -6,6 +6,21 @@ use std::sync::Arc;
|
|||||||
use store::Store;
|
use store::Store;
|
||||||
use types::{BeaconBlock, BeaconState};
|
use types::{BeaconBlock, BeaconState};
|
||||||
|
|
||||||
|
/// HTTP handler to return a `BeaconBlock` at a given `root` or `slot`.
|
||||||
|
pub fn get_best_slot<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
|
let beacon_chain = req
|
||||||
|
.extensions()
|
||||||
|
.get::<Arc<BeaconChain<T>>>()
|
||||||
|
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".to_string()))?;
|
||||||
|
|
||||||
|
let slot = beacon_chain.head().beacon_state.slot;
|
||||||
|
|
||||||
|
let json: String = serde_json::to_string(&slot)
|
||||||
|
.map_err(|e| ApiError::ServerError(format!("Unable to serialize Slot: {:?}", e)))?;
|
||||||
|
|
||||||
|
Ok(success_response(Body::from(json)))
|
||||||
|
}
|
||||||
|
|
||||||
/// HTTP handler to return a `BeaconBlock` at a given `root` or `slot`.
|
/// HTTP handler to return a `BeaconBlock` at a given `root` or `slot`.
|
||||||
pub fn get_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = req
|
let beacon_chain = req
|
||||||
|
@ -121,6 +121,7 @@ pub fn start_server<T: BeaconChainTypes + Clone + Send + Sync + 'static>(
|
|||||||
|
|
||||||
// Route the request to the correct handler.
|
// Route the request to the correct handler.
|
||||||
let result = match (req.method(), path.as_ref()) {
|
let result = match (req.method(), path.as_ref()) {
|
||||||
|
(&Method::GET, "/beacon/best_slot") => beacon::get_best_slot::<T>(req),
|
||||||
(&Method::GET, "/beacon/block") => beacon::get_block::<T>(req),
|
(&Method::GET, "/beacon/block") => beacon::get_block::<T>(req),
|
||||||
(&Method::GET, "/beacon/block_root") => beacon::get_block_root::<T>(req),
|
(&Method::GET, "/beacon/block_root") => beacon::get_block_root::<T>(req),
|
||||||
(&Method::GET, "/beacon/latest_finalized_checkpoint") => {
|
(&Method::GET, "/beacon/latest_finalized_checkpoint") => {
|
||||||
|
Loading…
Reference in New Issue
Block a user