Added YAML types for list of validators and added some logging to duties function.

This commit is contained in:
Luke Anderson 2019-09-06 14:10:49 +10:00
parent 6cbef7b58b
commit e6a0d038e9
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
3 changed files with 38 additions and 19 deletions

View File

@ -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<T: BeaconChainTypes + 'static>(req: Request<Body>) -> 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<Validator> = 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)]

View File

@ -179,6 +179,7 @@ pub fn get_beacon_chain_from_request<T: BeaconChainTypes + 'static>(
.get::<Arc<BeaconChain<T>>>()
.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<T: BeaconChainTypes + 'static>(
))?
.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<Body>) -> slog::Logger {
let log = req
.extensions()
.get::<slog::Logger>()
.expect("Should always get the logger from the request, since we put it in there.");
log.to_owned()
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -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<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 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::<u64>().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::<u64>().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<PublicKey> = query
.all_of("validator_pubkeys")?
.iter()