diff --git a/beacon_node/rest_api/src/beacon.rs b/beacon_node/rest_api/src/beacon.rs index 70b3f3ee9..f9b2d0383 100644 --- a/beacon_node/rest_api/src/beacon.rs +++ b/beacon_node/rest_api/src/beacon.rs @@ -6,7 +6,7 @@ use serde::Serialize; use ssz_derive::Encode; use std::sync::Arc; use store::Store; -use types::{BeaconBlock, BeaconState, Epoch, EthSpec, Hash256, Slot}; +use types::{BeaconBlock, BeaconState, Epoch, EthSpec, Hash256, Slot, Validator}; #[derive(Serialize)] pub struct HeadResponse { @@ -162,21 +162,13 @@ pub fn get_validators(req: Request) -> ApiR }; let all_validators = &beacon_chain.head().beacon_state.validators; - let mut active_validators = Vec::with_capacity(all_validators.len()); - for (_index, validator) in all_validators.iter().enumerate() { - if validator.is_active_at(epoch) { - active_validators.push(validator) - } - } - active_validators.shrink_to_fit(); - let json: String = serde_json::to_string(&active_validators).map_err(|e| { - ApiError::ServerError(format!( - "Unable to serialize list of active validators: {:?}", - e - )) - })?; + let active_vals: Vec = all_validators + .iter() + .filter(|v| v.is_active_at(epoch)) + .cloned() + .collect(); - Ok(success_response(Body::from(json))) + ResponseBuilder::new(&req).body(&active_vals) } #[derive(Serialize, Encode)] diff --git a/beacon_node/rest_api/src/helpers.rs b/beacon_node/rest_api/src/helpers.rs index e15c27df5..08ccbb6c9 100644 --- a/beacon_node/rest_api/src/helpers.rs +++ b/beacon_node/rest_api/src/helpers.rs @@ -179,6 +179,7 @@ pub fn get_beacon_chain_from_request( .get::>>() .ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".into()))?; + /* let _state_now = beacon_chain .state_now() .map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))? @@ -188,10 +189,19 @@ pub fn get_beacon_chain_from_request( ))? .build_all_caches(&beacon_chain.spec) .map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?; + */ Ok(beacon_chain.clone()) } +pub fn get_logger_from_request(req: &Request) -> slog::Logger { + let log = req + .extensions() + .get::() + .expect("Should always get the logger from the request, since we put it in there."); + log.to_owned() +} + #[cfg(test)] mod test { use super::*; diff --git a/beacon_node/rest_api/src/validator.rs b/beacon_node/rest_api/src/validator.rs index 2374373bd..c559777c0 100644 --- a/beacon_node/rest_api/src/validator.rs +++ b/beacon_node/rest_api/src/validator.rs @@ -32,27 +32,44 @@ impl ValidatorDuty { /// HTTP Handler to retrieve a the duties for a set of validators during a particular epoch pub fn get_validator_duties(req: Request) -> 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::(&req)?; - let head_state = &beacon_chain.head().beacon_state; + let mut head_state = beacon_chain + .state_now() + .map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?; + slog::trace!(log, "Got head state from request."); // Parse and check query parameters let query = UrlQuery::from_request(&req)?; let current_epoch = head_state.current_epoch(); let epoch = match query.first_of(&["epoch"]) { - Ok((_, v)) => Epoch::new(v.parse::().map_err(|e| { - ApiError::InvalidQueryParams(format!("Invalid epoch parameter, must be a u64. {:?}", e)) - })?), + Ok((_, v)) => { + slog::trace!(log, "Requested epoch {:?}", v); + Epoch::new(v.parse::().map_err(|e| { + slog::info!(log, "Invalid epoch {:?}", e); + ApiError::InvalidQueryParams(format!( + "Invalid epoch parameter, must be a u64. {:?}", + e + )) + })?) + } Err(_) => { // epoch not supplied, use the current epoch + slog::info!(log, "Using default epoch {:?}", current_epoch); current_epoch } }; let relative_epoch = RelativeEpoch::from_epoch(current_epoch, epoch).map_err(|e| { + slog::info!(log, "Requested epoch out of range."); ApiError::InvalidQueryParams(format!( "Cannot get RelativeEpoch, epoch out of range: {:?}", e )) })?; + if let Some(s) = head_state.maybe_as_mut_ref() { + s.build_all_caches(&beacon_chain.spec).ok(); + } let validators: Vec = query .all_of("validator_pubkeys")? .iter()