Getting the restructured ApiService to work.

This commit is contained in:
Luke Anderson 2019-09-10 19:30:36 +10:00
parent 4dcad27381
commit b0090df543
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
3 changed files with 19 additions and 32 deletions

View File

@ -1,7 +1,7 @@
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use std::error::Error as StdError;
#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, Clone)]
pub enum ApiError {
MethodNotAllowed(String),
ServerError(String),
@ -14,7 +14,7 @@ pub enum ApiError {
pub type ApiResult = Result<Response<Body>, ApiError>;
impl ApiError {
pub fn status_code(&self) -> (StatusCode, &String) {
pub fn status_code(self) -> (StatusCode, String) {
match self {
ApiError::MethodNotAllowed(desc) => (StatusCode::METHOD_NOT_ALLOWED, desc),
ApiError::ServerError(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
@ -30,10 +30,10 @@ impl Into<Response<Body>> for ApiError {
fn into(self) -> Response<Body> {
let status_code = self.status_code();
Response::builder()
.status(status_code.0)
.header("content-type", "text/plain")
.body(Body::from(*status_code.1))
.expect("Response should always be created.")
.status(status_code.0)
.header("content-type", "text/plain")
.body(Body::from(status_code.1))
.expect("Response should always be created.")
}
}
@ -63,7 +63,7 @@ impl StdError for ApiError {
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let status = self.status_code();
let status = self.clone().status_code();
write!(f, "{:?}: {:?}", status.0, status.1)
}
}

View File

@ -79,7 +79,6 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
let result = match (req.method(), path.as_ref()) {
// Methods for Client
(&Method::GET, "/node/version") => node::get_version(req),
/*
(&Method::GET, "/node/genesis_time") => node::get_genesis_time::<T>(req),
(&Method::GET, "/node/syncing") => helpers::implementation_pending_response(req),
@ -89,9 +88,7 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
(&Method::GET, "/network/peer_id") => network::get_peer_id::<T>(req),
(&Method::GET, "/network/peers") => network::get_peer_list::<T>(req),
(&Method::GET, "/network/listen_port") => network::get_listen_port::<T>(req),
(&Method::GET, "/network/listen_addresses") => {
network::get_listen_addresses::<T>(req)
}
(&Method::GET, "/network/listen_addresses") => network::get_listen_addresses::<T>(req),
// Methods for Beacon Node
(&Method::GET, "/beacon/head") => beacon::get_head::<T>(req),
@ -99,9 +96,7 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
(&Method::GET, "/beacon/block_root") => beacon::get_block_root::<T>(req),
(&Method::GET, "/beacon/blocks") => helpers::implementation_pending_response(req),
(&Method::GET, "/beacon/fork") => beacon::get_fork::<T>(req),
(&Method::GET, "/beacon/attestations") => {
helpers::implementation_pending_response(req)
}
(&Method::GET, "/beacon/attestations") => helpers::implementation_pending_response(req),
(&Method::GET, "/beacon/attestations/pending") => {
helpers::implementation_pending_response(req)
}
@ -115,15 +110,9 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
}
// Methods for Validator
(&Method::GET, "/beacon/validator/duties") => {
validator::get_validator_duties::<T>(req)
}
(&Method::GET, "/beacon/validator/block") => {
validator::get_new_beacon_block::<T>(req)
}
(&Method::POST, "/beacon/validator/block") => {
validator::publish_beacon_block::<T>(req)
}
(&Method::GET, "/beacon/validator/duties") => validator::get_validator_duties::<T>(req),
(&Method::GET, "/beacon/validator/block") => validator::get_new_beacon_block::<T>(req),
//(&Method::POST, "/beacon/validator/block") => validator::publish_beacon_block::<T>(req),
(&Method::GET, "/beacon/validator/attestation") => {
validator::get_new_attestation::<T>(req)
}
@ -149,7 +138,6 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
(&Method::GET, "/metrics") => metrics::get_prometheus::<T>(req),
*/
_ => Err(ApiError::NotFound(
"Request path and/or method not found.".to_owned(),
)),
@ -208,8 +196,8 @@ pub fn start_server<T: BeaconChainTypes>(
let service = move || -> futures::future::FutureResult<ApiService<T>, String> {
futures::future::ok(ApiService {
log: log.clone(),
beacon_chain: beacon_chain.clone(),
log: server_log.clone(),
beacon_chain: server_bc.clone(),
db_path: db_path.clone(),
network_service: network_service.clone(),
network_channel: Arc::new(RwLock::new(network_chan.clone())),

View File

@ -203,7 +203,9 @@ pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
Ok(success_response(body))
}
/// HTTP Handler to publish a BeaconBlock, which has been signed by a validator.
/*
HTTP Handler to publish a BeaconBlock, which has been signed by a validator.
pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let _ = check_content_type_for_json(&req)?;
let log = get_logger_from_request(&req);
@ -232,11 +234,8 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
))
});
block_result
})
.unwrap()
.unwrap();
});
/*
.map_err(|e| ApiError::ServerError(format!("Unable parse request body: {:?}", e)))
.and_then(|body| {
trace!(log, "parsing json");
@ -251,7 +250,6 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
});
tokio::run(block_future);
let block = block_future.wait()?;
*/
trace!(log, "BeaconBlock successfully parsed from JSON"; "block" => serde_json::to_string(&block).expect("We should always be able to serialize a block that we just created."));
match beacon_chain.process_block(block.clone()) {
Ok(BlockProcessingOutcome::Processed { block_root }) => {
@ -277,6 +275,7 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
Ok(success_response(Body::empty()))
}
*/
/// 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 {