Factored out getting beacon_chain from request into it's own function.

This commit is contained in:
Luke Anderson 2019-09-01 15:09:01 +10:00
parent 5ee1bb20b7
commit 8ea1167563
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
3 changed files with 72 additions and 29 deletions

View File

@ -5,6 +5,7 @@ use hex;
use hyper::{Body, Request};
use store::{iter::AncestorIter, Store};
use types::{BeaconState, EthSpec, Hash256, RelativeEpoch, Slot};
use std::sync::Arc;
/// Parse a slot from a `0x` preixed string.
///
@ -169,6 +170,18 @@ 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> {
// Get beacon state
let beacon_chain = req
.extensions()
.get::<Arc<BeaconChain<T>>>()
.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)))?;
Ok(beacon_chain.clone())
}
#[cfg(test)]
mod test {
use super::*;

View File

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

View File

@ -5,6 +5,7 @@ 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};
@ -33,15 +34,7 @@ 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 {
// Get beacon state
let beacon_chain = req
.extensions()
.get::<Arc<BeaconChain<T>>>()
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".to_string()))?;
//TODO Surely this state_cache thing is not necessary?
let _ = beacon_chain
.ensure_state_caches_are_built()
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = &beacon_chain.head().beacon_state;
// Parse and check query parameters
@ -146,15 +139,8 @@ 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.
pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
// Get beacon state
let beacon_chain = req
.extensions()
.get::<Arc<BeaconChain<T>>>()
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".to_string()))?;
//TODO Surely this state_cache thing is not necessary?
let _ = beacon_chain
.ensure_state_caches_are_built()
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
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 slot = match query.first_of(&["slot"]) {
@ -201,17 +187,60 @@ pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
Ok(success_response(body))
}
/// 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 {
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 slot = match query.first_of(&["slot"]) {
Ok((_, v)) => Slot::new(v.parse::<u64>().map_err(|e| {
ApiError::InvalidQueryParams(format!("Invalid slot parameter, must be a u64. {:?}", e))
})?),
Err(e) => {
return Err(e);
}
};
let randao_reveal = match query.first_of(&["randao_reveal"]) {
Ok((_, v)) => Signature::from_bytes(
hex::decode(&v)
.map_err(|e| {
ApiError::InvalidQueryParams(format!(
"Invalid hex string for randao_reveal: {:?}",
e
))
})?
.as_slice(),
)
.map_err(|e| {
ApiError::InvalidQueryParams(format!("randao_reveal is not a valid signature: {:?}", e))
})?,
Err(e) => {
return Err(e);
}
};
let new_block = match beacon_chain.produce_block(randao_reveal, slot) {
Ok((block, _state)) => block,
Err(e) => {
return Err(ApiError::ServerError(format!(
"Beacon node is not able to produce a block: {:?}",
e
)));
}
};
let body = Body::from(
serde_json::to_string(&new_block)
.expect("We should always be able to serialize a new block that we produced."),
);
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<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
// Get beacon state
let beacon_chain = req
.extensions()
.get::<Arc<BeaconChain<T>>>()
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".to_string()))?;
//TODO Surely this state_cache thing is not necessary?
let _ = beacon_chain
.ensure_state_caches_are_built()
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
let beacon_chain = get_beacon_chain_from_request(req)?;
let head_state = &beacon_chain.head().beacon_state;
let query = UrlQuery::from_request(&req)?;
@ -340,3 +369,4 @@ pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) ->
);
Ok(success_response(body))
}
*/