diff --git a/beacon_node/rest_api/src/helpers.rs b/beacon_node/rest_api/src/helpers.rs index 98293e75c..d47afc02c 100644 --- a/beacon_node/rest_api/src/helpers.rs +++ b/beacon_node/rest_api/src/helpers.rs @@ -3,9 +3,9 @@ use beacon_chain::{BeaconChain, BeaconChainTypes}; use bls::PublicKey; use hex; use hyper::{Body, Request}; +use std::sync::Arc; use store::{iter::AncestorIter, Store}; use types::{BeaconState, EthSpec, Hash256, RelativeEpoch, Slot}; -use std::sync::Arc; /// Parse a slot from a `0x` preixed string. /// @@ -170,12 +170,16 @@ pub fn implementation_pending_response(_req: Request) -> ApiResult { )) } -pub fn get_beacon_chain_from_request(req: &Request) -> Result>, ApiError> { +pub fn get_beacon_chain_from_request( + req: &Request, +) -> Result>, ApiError> { // Get beacon state let beacon_chain = req .extensions() .get::>>() - .ok_or_else(|| ApiError::ServerError("Request is missing the beacon chain extension".into()))?; + .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)))?; diff --git a/beacon_node/rest_api/src/lib.rs b/beacon_node/rest_api/src/lib.rs index 2c9c4011a..b269bd476 100644 --- a/beacon_node/rest_api/src/lib.rs +++ b/beacon_node/rest_api/src/lib.rs @@ -170,11 +170,11 @@ pub fn start_server( validator::get_new_beacon_block::(req) } (&Method::POST, "/beacon/validator/block") => { - helpers::implementation_pending_response(req) + validator::publish_beacon_block::(req) } - /*(&Method::GET, "/beacon/validator/attestation") => { + (&Method::GET, "/beacon/validator/attestation") => { validator::get_new_attestation::(req) - }*/ + } (&Method::POST, "/beacon/validator/attestation") => { helpers::implementation_pending_response(req) } diff --git a/beacon_node/rest_api/src/validator.rs b/beacon_node/rest_api/src/validator.rs index f60acbad8..427f6a514 100644 --- a/beacon_node/rest_api/src/validator.rs +++ b/beacon_node/rest_api/src/validator.rs @@ -1,11 +1,9 @@ use super::{success_response, ApiResult}; use crate::{helpers::*, ApiError, UrlQuery}; -use beacon_chain::{BeaconChain, BeaconChainTypes}; +use beacon_chain::BeaconChainTypes; use bls::{AggregateSignature, PublicKey, Signature}; use hyper::{Body, Request}; use serde::{Deserialize, Serialize}; -use std::sync::Arc; -use std::borrow::Borrow; use types::beacon_state::EthSpec; use types::{Attestation, BitList, Epoch, RelativeEpoch, Shard, Slot}; @@ -140,7 +138,6 @@ pub fn get_validator_duties(req: Request) - /// HTTP Handler to produce a new BeaconBlock from the current state, ready to be signed by a validator. pub fn get_new_beacon_block(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = &beacon_chain.head().beacon_state; let query = UrlQuery::from_request(&req)?; let slot = match query.first_of(&["slot"]) { @@ -190,7 +187,6 @@ pub fn get_new_beacon_block(req: Request) - /// HTTP Handler to accept a validator-signed BeaconBlock, and publish it to the network. pub fn publish_beacon_block(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; - let head_state = &beacon_chain.head().beacon_state; let query = UrlQuery::from_request(&req)?; let slot = match query.first_of(&["slot"]) { @@ -212,9 +208,9 @@ pub fn publish_beacon_block(req: Request) - })? .as_slice(), ) - .map_err(|e| { - ApiError::InvalidQueryParams(format!("randao_reveal is not a valid signature: {:?}", e)) - })?, + .map_err(|e| { + ApiError::InvalidQueryParams(format!("randao_reveal is not a valid signature: {:?}", e)) + })?, Err(e) => { return Err(e); } @@ -237,10 +233,9 @@ pub fn publish_beacon_block(req: Request) - Ok(success_response(body)) } -/* /// 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 beacon_chain = get_beacon_chain_from_request::(&req)?; let head_state = &beacon_chain.head().beacon_state; let query = UrlQuery::from_request(&req)?; @@ -304,10 +299,16 @@ pub fn get_new_attestation(req: Request) -> return Err(e); } }; - let mut aggregation_bits: BitList = BitList::with_capacity(val_duty.committee_len) - .expect("An empty BitList should always be created, or we have bigger problems.") - .into(); - aggregation_bits.set(val_duty.committee_index, poc_bit); + let mut aggregation_bits = BitList::with_capacity(val_duty.committee_len) + .expect("An empty BitList should always be created, or we have bigger problems."); + aggregation_bits + .set(val_duty.committee_index, poc_bit) + .map_err(|e| { + ApiError::ServerError(format!( + "Unable to set aggregation bits for the attestation: {:?}", + e + )) + })?; // Allow a provided slot parameter to check against the expected slot as a sanity check. // Presently, we don't support attestations at future or past slots. @@ -354,7 +355,7 @@ pub fn get_new_attestation(req: Request) -> } }; - let attestation = Attestation { + let attestation: Attestation = Attestation { aggregation_bits, data: attestation_data, custody_bits: BitList::with_capacity(val_duty.committee_len) @@ -362,11 +363,9 @@ pub fn get_new_attestation(req: Request) -> signature: AggregateSignature::new(), }; - //TODO: This is currently AttestationData, but should be IndexedAttestation? let body = Body::from( serde_json::to_string(&attestation) .expect("We should always be able to serialize a new attestation that we produced."), ); Ok(success_response(body)) } -*/