Fixing some API bits

- Adding the validator routes into the main function.
 - Fixing the setting of the aggregation bits, and handling errors correctly.
 - Rust format fixes, and addressing compiler warnings.
This commit is contained in:
Luke Anderson 2019-09-01 15:41:03 +10:00
parent 8ea1167563
commit 632c13a9ec
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
3 changed files with 26 additions and 23 deletions

View File

@ -3,9 +3,9 @@ use beacon_chain::{BeaconChain, BeaconChainTypes};
use bls::PublicKey; use bls::PublicKey;
use hex; use hex;
use hyper::{Body, Request}; use hyper::{Body, Request};
use std::sync::Arc;
use store::{iter::AncestorIter, Store}; use store::{iter::AncestorIter, Store};
use types::{BeaconState, EthSpec, Hash256, RelativeEpoch, Slot}; use types::{BeaconState, EthSpec, Hash256, RelativeEpoch, Slot};
use std::sync::Arc;
/// Parse a slot from a `0x` preixed string. /// Parse a slot from a `0x` preixed string.
/// ///
@ -170,12 +170,16 @@ 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> { pub fn get_beacon_chain_from_request<T: BeaconChainTypes + 'static>(
req: &Request<Body>,
) -> Result<Arc<BeaconChain<T>>, ApiError> {
// Get beacon state // Get beacon state
let beacon_chain = req let beacon_chain = req
.extensions() .extensions()
.get::<Arc<BeaconChain<T>>>() .get::<Arc<BeaconChain<T>>>()
.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 let _ = beacon_chain
.ensure_state_caches_are_built() .ensure_state_caches_are_built()
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?; .map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;

View File

@ -170,11 +170,11 @@ pub fn start_server<T: BeaconChainTypes>(
validator::get_new_beacon_block::<T>(req) validator::get_new_beacon_block::<T>(req)
} }
(&Method::POST, "/beacon/validator/block") => { (&Method::POST, "/beacon/validator/block") => {
helpers::implementation_pending_response(req) validator::publish_beacon_block::<T>(req)
} }
/*(&Method::GET, "/beacon/validator/attestation") => { (&Method::GET, "/beacon/validator/attestation") => {
validator::get_new_attestation::<T>(req) validator::get_new_attestation::<T>(req)
}*/ }
(&Method::POST, "/beacon/validator/attestation") => { (&Method::POST, "/beacon/validator/attestation") => {
helpers::implementation_pending_response(req) helpers::implementation_pending_response(req)
} }

View File

@ -1,11 +1,9 @@
use super::{success_response, ApiResult}; use super::{success_response, ApiResult};
use crate::{helpers::*, ApiError, UrlQuery}; use crate::{helpers::*, ApiError, UrlQuery};
use beacon_chain::{BeaconChain, BeaconChainTypes}; use beacon_chain::BeaconChainTypes;
use bls::{AggregateSignature, PublicKey, Signature}; use bls::{AggregateSignature, PublicKey, Signature};
use hyper::{Body, Request}; use hyper::{Body, Request};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::borrow::Borrow;
use types::beacon_state::EthSpec; use types::beacon_state::EthSpec;
use types::{Attestation, BitList, Epoch, RelativeEpoch, Shard, Slot}; use types::{Attestation, BitList, Epoch, RelativeEpoch, Shard, Slot};
@ -140,7 +138,6 @@ 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. /// 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 { 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 = get_beacon_chain_from_request::<T>(&req)?;
let head_state = &beacon_chain.head().beacon_state;
let query = UrlQuery::from_request(&req)?; let query = UrlQuery::from_request(&req)?;
let slot = match query.first_of(&["slot"]) { let slot = match query.first_of(&["slot"]) {
@ -190,7 +187,6 @@ pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
/// HTTP Handler to accept a validator-signed BeaconBlock, and publish it to the network. /// HTTP Handler to accept a validator-signed BeaconBlock, and publish it to the network.
pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult { pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?; let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = &beacon_chain.head().beacon_state;
let query = UrlQuery::from_request(&req)?; let query = UrlQuery::from_request(&req)?;
let slot = match query.first_of(&["slot"]) { let slot = match query.first_of(&["slot"]) {
@ -212,9 +208,9 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
})? })?
.as_slice(), .as_slice(),
) )
.map_err(|e| { .map_err(|e| {
ApiError::InvalidQueryParams(format!("randao_reveal is not a valid signature: {:?}", e)) ApiError::InvalidQueryParams(format!("randao_reveal is not a valid signature: {:?}", e))
})?, })?,
Err(e) => { Err(e) => {
return Err(e); return Err(e);
} }
@ -237,10 +233,9 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
Ok(success_response(body)) Ok(success_response(body))
} }
/*
/// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator. /// 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 { pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let beacon_chain = get_beacon_chain_from_request(req)?; let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = &beacon_chain.head().beacon_state; let head_state = &beacon_chain.head().beacon_state;
let query = UrlQuery::from_request(&req)?; let query = UrlQuery::from_request(&req)?;
@ -304,10 +299,16 @@ pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) ->
return Err(e); return Err(e);
} }
}; };
let mut aggregation_bits: BitList<bool> = BitList::with_capacity(val_duty.committee_len) let mut aggregation_bits = BitList::with_capacity(val_duty.committee_len)
.expect("An empty BitList should always be created, or we have bigger problems.") .expect("An empty BitList should always be created, or we have bigger problems.");
.into(); aggregation_bits
aggregation_bits.set(val_duty.committee_index, poc_bit); .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. // 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. // Presently, we don't support attestations at future or past slots.
@ -354,7 +355,7 @@ pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) ->
} }
}; };
let attestation = Attestation { let attestation: Attestation<T::EthSpec> = Attestation {
aggregation_bits, aggregation_bits,
data: attestation_data, data: attestation_data,
custody_bits: BitList::with_capacity(val_duty.committee_len) custody_bits: BitList::with_capacity(val_duty.committee_len)
@ -362,11 +363,9 @@ pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) ->
signature: AggregateSignature::new(), signature: AggregateSignature::new(),
}; };
//TODO: This is currently AttestationData, but should be IndexedAttestation?
let body = Body::from( let body = Body::from(
serde_json::to_string(&attestation) serde_json::to_string(&attestation)
.expect("We should always be able to serialize a new attestation that we produced."), .expect("We should always be able to serialize a new attestation that we produced."),
); );
Ok(success_response(body)) Ok(success_response(body))
} }
*/