diff --git a/beacon_node/rest_api/src/beacon.rs b/beacon_node/rest_api/src/beacon.rs index d74ab2ed99..302061395f 100644 --- a/beacon_node/rest_api/src/beacon.rs +++ b/beacon_node/rest_api/src/beacon.rs @@ -128,13 +128,7 @@ pub fn get_block_root(req: Request) -> ApiR /// HTTP handler to return the `Fork` of the current head. pub fn get_fork(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = get_head_state(beacon_chain)?; - - let json: String = serde_json::to_string(&head_state.fork).map_err(|e| { - ApiError::ServerError(format!("Unable to serialize BeaconState::Fork: {:?}", e)) - })?; - - Ok(success_response_old(Body::from(json))) + ResponseBuilder::new(&req).body(&beacon_chain.head().beacon_state) } /// HTTP handler to return the set of validators for an `Epoch` @@ -185,7 +179,7 @@ pub struct StateResponse { /// the current head by skipping slots. pub fn get_state(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = get_head_state(beacon_chain.clone())?; + let head_state = beacon_chain.head().beacon_state; let (key, value) = match UrlQuery::from_request(&req) { Ok(query) => { @@ -248,7 +242,7 @@ pub fn get_current_finalized_checkpoint( req: Request, ) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = get_head_state(beacon_chain)?; + let head_state = beacon_chain.head().beacon_state; let checkpoint = head_state.finalized_checkpoint.clone(); diff --git a/beacon_node/rest_api/src/helpers.rs b/beacon_node/rest_api/src/helpers.rs index 4acb563ada..99a8f1dd47 100644 --- a/beacon_node/rest_api/src/helpers.rs +++ b/beacon_node/rest_api/src/helpers.rs @@ -238,16 +238,6 @@ pub fn get_beacon_chain_from_request( Ok(beacon_chain.clone()) } -pub fn get_head_state( - bc: Arc>, -) -> Result, ApiError> { - let mut head_state = bc.head().beacon_state; - head_state - .build_all_caches(&bc.spec) - .map_err(|e| ApiError::ServerError(format!("Unable to build state cache: {:?}", e)))?; - Ok(head_state) -} - pub fn get_logger_from_request(req: &Request) -> slog::Logger { let log = req .extensions() diff --git a/beacon_node/rest_api/src/metrics.rs b/beacon_node/rest_api/src/metrics.rs index f557f2a075..09d361b8a2 100644 --- a/beacon_node/rest_api/src/metrics.rs +++ b/beacon_node/rest_api/src/metrics.rs @@ -1,5 +1,5 @@ -use crate::helpers::*; -use crate::{ApiError, ApiResult, DBPath}; +use crate::response_builder::ResponseBuilder; +use crate::{helpers::*, success_response, ApiError, ApiResult, DBPath}; use beacon_chain::BeaconChainTypes; use http::HeaderValue; use hyper::{Body, Request}; diff --git a/beacon_node/rest_api/src/node.rs b/beacon_node/rest_api/src/node.rs index 5ef35f9a07..3eb8e5594c 100644 --- a/beacon_node/rest_api/src/node.rs +++ b/beacon_node/rest_api/src/node.rs @@ -13,6 +13,5 @@ pub fn get_version(req: Request) -> ApiResult { /// Read the genesis time from the current beacon chain state. pub fn get_genesis_time(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = get_head_state(beacon_chain)?; - ResponseBuilder::new(&req).body(&head_state.genesis_time) + ResponseBuilder::new(&req).body(&beacon_chain.head().beacon_state.genesis_time) } diff --git a/beacon_node/rest_api/src/validator.rs b/beacon_node/rest_api/src/validator.rs index 7d0fbdabdd..c665a0b1f6 100644 --- a/beacon_node/rest_api/src/validator.rs +++ b/beacon_node/rest_api/src/validator.rs @@ -44,7 +44,7 @@ pub fn get_validator_duties(req: Request) - 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 = get_head_state(beacon_chain.clone())?; + let mut head_state = beacon_chain.head().beacon_state; slog::trace!(log, "Got head state from request."); // Parse and check query parameters @@ -81,6 +81,10 @@ pub fn get_validator_duties(req: Request) - .collect::, _>>()?; let mut duties: Vec = Vec::new(); + // Build cache for the requested epoch + head_state + .build_committee_cache(relative_epoch, &beacon_chain.spec) + .map_err(|e| ApiError::ServerError(format!("Unable to build committee cache: {:?}", e)))?; // Get a list of all validators for this epoch let validator_proposers: Vec = epoch .slot_iter(T::EthSpec::slots_per_epoch()) @@ -88,7 +92,6 @@ pub fn get_validator_duties(req: Request) - head_state .get_beacon_proposer_index(slot, relative_epoch, &beacon_chain.spec) .map_err(|e| { - // TODO: why are we getting an uninitialized state error here??? ApiError::ServerError(format!( "Unable to get proposer index for validator: {:?}", e @@ -190,7 +193,7 @@ pub fn get_new_beacon_block(req: Request) - serde_json::to_string(&new_block) .expect("We should always be able to serialize a new block that we produced."), ); - Ok(success_response_old(body)) + ResponseBuilder::new(&req).body(&new_block) } /// HTTP Handler to publish a BeaconBlock, which has been signed by a validator. @@ -255,7 +258,7 @@ pub fn publish_beacon_block(req: Request) - /// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator. pub fn get_new_attestation(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = get_head_state(beacon_chain.clone())?; + let mut head_state = beacon_chain.head().beacon_state; let query = UrlQuery::from_request(&req)?; let val_pk_str = query @@ -263,6 +266,9 @@ pub fn get_new_attestation(req: Request) -> .map(|(_key, value)| value)?; let val_pk = parse_pubkey(val_pk_str.as_str())?; + head_state + .update_pubkey_cache() + .map_err(|e| ApiError::ServerError(format!("Unable to build pubkey cache: {:?}", e)))?; // Get the validator index from the supplied public key // If it does not exist in the index, we cannot continue. let val_index = head_state @@ -274,6 +280,10 @@ pub fn get_new_attestation(req: Request) -> "The provided validator public key does not correspond to a validator index.".into(), ))?; + // Build cache for the requested epoch + head_state + .build_committee_cache(RelativeEpoch::Current, &beacon_chain.spec) + .map_err(|e| ApiError::ServerError(format!("Unable to build committee cache: {:?}", e)))?; // Get the duties of the validator, to make sure they match up. // If they don't have duties this epoch, then return an error let val_duty = head_state