Factored out getting beacon_chain from request into it's own function.

This commit is contained in:
Luke Anderson
2019-09-01 15:09:01 +10:00
parent 5ee1bb20b7
commit 8ea1167563
3 changed files with 72 additions and 29 deletions

View File

@@ -5,6 +5,7 @@ use hex;
use hyper::{Body, Request};
use store::{iter::AncestorIter, Store};
use types::{BeaconState, EthSpec, Hash256, RelativeEpoch, Slot};
use std::sync::Arc;
/// Parse a slot from a `0x` preixed string.
///
@@ -169,6 +170,18 @@ pub fn implementation_pending_response(_req: Request<Body>) -> ApiResult {
))
}
pub fn get_beacon_chain_from_request<T: BeaconChainTypes + 'static>(req: &Request<Body>) -> Result<Arc<BeaconChain<T>>, ApiError> {
// Get beacon state
let beacon_chain = req
.extensions()
.get::<Arc<BeaconChain<T>>>()
.ok_or_else(|| ApiError::ServerError("Request is missing the beacon chain extension".into()))?;
let _ = beacon_chain
.ensure_state_caches_are_built()
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
Ok(beacon_chain.clone())
}
#[cfg(test)]
mod test {
use super::*;