Moved chain/cache building into separate function, and made sure that all REST API endpoints are using this function to get the state.
This commit is contained in:
parent
a2267dc4d3
commit
99c673045c
@ -109,7 +109,7 @@ pub fn get_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
||||
|
||||
/// HTTP handler to return a `BeaconBlock` root at a given `slot`.
|
||||
pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
|
||||
let target = parse_slot(&slot_string)?;
|
||||
@ -126,10 +126,9 @@ pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
|
||||
|
||||
/// HTTP handler to return the `Fork` of the current head.
|
||||
pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let chain_head = beacon_chain.head();
|
||||
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let json: String = serde_json::to_string(&chain_head.beacon_state.fork).map_err(|e| {
|
||||
let json: String = serde_json::to_string(&head_state.fork).map_err(|e| {
|
||||
ApiError::ServerError(format!("Unable to serialize BeaconState::Fork: {:?}", e))
|
||||
})?;
|
||||
|
||||
@ -141,7 +140,7 @@ pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
||||
/// The `Epoch` parameter can be any epoch number. If it is not specified,
|
||||
/// the current epoch is assumed.
|
||||
pub fn get_validators<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let epoch = match UrlQuery::from_request(&req) {
|
||||
// We have some parameters, so make sure it's the epoch one and parse it
|
||||
@ -183,7 +182,7 @@ pub struct StateResponse<T: EthSpec> {
|
||||
/// Will not return a state if the request slot is in the future. Will return states higher than
|
||||
/// the current head by skipping slots.
|
||||
pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let (key, value) = match UrlQuery::from_request(&req) {
|
||||
Ok(query) => {
|
||||
@ -199,10 +198,7 @@ pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
||||
}
|
||||
Err(ApiError::InvalidQueryParams(_)) => {
|
||||
// No parameters provided at all, use current slot.
|
||||
(
|
||||
String::from("slot"),
|
||||
beacon_chain.head().beacon_state.slot.to_string(),
|
||||
)
|
||||
(String::from("slot"), head_state.slot.to_string())
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
@ -237,7 +233,7 @@ pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
||||
/// Will not return a state if the request slot is in the future. Will return states higher than
|
||||
/// the current head by skipping slots.
|
||||
pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
|
||||
let slot = parse_slot(&slot_string)?;
|
||||
@ -254,7 +250,7 @@ pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
|
||||
pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
|
||||
req: Request<Body>,
|
||||
) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let checkpoint = beacon_chain
|
||||
.head()
|
||||
@ -270,7 +266,7 @@ pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
|
||||
|
||||
/// HTTP handler to return a `BeaconState` at the genesis block.
|
||||
pub fn get_genesis_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let (_root, state) = state_at_slot(&beacon_chain, Slot::new(0))?;
|
||||
|
||||
|
@ -172,26 +172,21 @@ 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> {
|
||||
) -> Result<(Arc<BeaconChain<T>>, BeaconState<T::EthSpec>), ApiError> {
|
||||
// Get beacon state
|
||||
let beacon_chain = req
|
||||
.extensions()
|
||||
.get::<Arc<BeaconChain<T>>>()
|
||||
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".into()))?;
|
||||
|
||||
/*
|
||||
let _state_now = beacon_chain
|
||||
let mut head_state = beacon_chain
|
||||
.state_now()
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?
|
||||
.maybe_as_mut_ref()
|
||||
.ok_or(ApiError::ServerError(
|
||||
"Unable to get mutable BeaconState".into(),
|
||||
))?
|
||||
.build_all_caches(&beacon_chain.spec)
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
|
||||
*/
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?;
|
||||
|
||||
Ok(beacon_chain.clone())
|
||||
if let Some(s) = head_state.maybe_as_mut_ref() {
|
||||
s.build_all_caches(&beacon_chain.spec).ok();
|
||||
}
|
||||
|
||||
Ok((beacon_chain.clone(), head_state.clone()))
|
||||
}
|
||||
|
||||
pub fn get_logger_from_request(req: &Request<Body>) -> slog::Logger {
|
||||
|
@ -30,7 +30,7 @@ pub fn get_prometheus<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
|
||||
let mut buffer = vec![];
|
||||
let encoder = TextEncoder::new();
|
||||
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let db_path = req
|
||||
.extensions()
|
||||
.get::<DBPath>()
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::helpers::get_beacon_chain_from_request;
|
||||
use crate::{success_response, ApiResult};
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use beacon_chain::BeaconChainTypes;
|
||||
use hyper::{Body, Request};
|
||||
use std::sync::Arc;
|
||||
use version;
|
||||
|
||||
/// Read the version string from the current Lighthouse build.
|
||||
@ -15,8 +15,8 @@ pub fn get_version(_req: Request<Body>) -> ApiResult {
|
||||
|
||||
/// Read the genesis time from the current beacon chain state.
|
||||
pub fn get_genesis_time<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = req.extensions().get::<Arc<BeaconChain<T>>>().unwrap();
|
||||
let gen_time: u64 = beacon_chain.head().beacon_state.genesis_time;
|
||||
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let gen_time: u64 = head_state.genesis_time;
|
||||
let body = Body::from(
|
||||
serde_json::to_string(&gen_time)
|
||||
.expect("Genesis should time always have a valid JSON serialization."),
|
||||
|
@ -9,7 +9,7 @@ use types::EthSpec;
|
||||
|
||||
/// HTTP handler to return the full spec object.
|
||||
pub fn get_spec<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let json: String = serde_json::to_string(&beacon_chain.spec)
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to serialize spec: {:?}", e)))?;
|
||||
|
@ -34,10 +34,7 @@ impl ValidatorDuty {
|
||||
pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let log = get_logger_from_request(&req);
|
||||
slog::trace!(log, "Validator duties requested of API: {:?}", &req);
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let mut head_state = beacon_chain
|
||||
.state_now()
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?;
|
||||
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
slog::trace!(log, "Got head state from request.");
|
||||
// Parse and check query parameters
|
||||
@ -67,9 +64,6 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
||||
e
|
||||
))
|
||||
})?;
|
||||
if let Some(s) = head_state.maybe_as_mut_ref() {
|
||||
s.build_all_caches(&beacon_chain.spec).ok();
|
||||
}
|
||||
let validators: Vec<PublicKey> = query
|
||||
.all_of("validator_pubkeys")?
|
||||
.iter()
|
||||
@ -163,7 +157,7 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
||||
|
||||
/// HTTP Handler to produce a new BeaconBlock from the current state, ready to be signed by a validator.
|
||||
pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let query = UrlQuery::from_request(&req)?;
|
||||
let slot = query
|
||||
@ -203,8 +197,7 @@ pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
||||
|
||||
/// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator.
|
||||
pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
||||
let head_state = &beacon_chain.head().beacon_state;
|
||||
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||
|
||||
let query = UrlQuery::from_request(&req)?;
|
||||
let val_pk_str = query
|
||||
|
Loading…
Reference in New Issue
Block a user