Getting the restructured ApiService to work.
This commit is contained in:
parent
4dcad27381
commit
b0090df543
@ -1,7 +1,7 @@
|
|||||||
use hyper::{Body, Method, Request, Response, Server, StatusCode};
|
use hyper::{Body, Method, Request, Response, Server, StatusCode};
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug, Clone)]
|
||||||
pub enum ApiError {
|
pub enum ApiError {
|
||||||
MethodNotAllowed(String),
|
MethodNotAllowed(String),
|
||||||
ServerError(String),
|
ServerError(String),
|
||||||
@ -14,7 +14,7 @@ pub enum ApiError {
|
|||||||
pub type ApiResult = Result<Response<Body>, ApiError>;
|
pub type ApiResult = Result<Response<Body>, ApiError>;
|
||||||
|
|
||||||
impl ApiError {
|
impl ApiError {
|
||||||
pub fn status_code(&self) -> (StatusCode, &String) {
|
pub fn status_code(self) -> (StatusCode, String) {
|
||||||
match self {
|
match self {
|
||||||
ApiError::MethodNotAllowed(desc) => (StatusCode::METHOD_NOT_ALLOWED, desc),
|
ApiError::MethodNotAllowed(desc) => (StatusCode::METHOD_NOT_ALLOWED, desc),
|
||||||
ApiError::ServerError(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
|
ApiError::ServerError(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
|
||||||
@ -30,10 +30,10 @@ impl Into<Response<Body>> for ApiError {
|
|||||||
fn into(self) -> Response<Body> {
|
fn into(self) -> Response<Body> {
|
||||||
let status_code = self.status_code();
|
let status_code = self.status_code();
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(status_code.0)
|
.status(status_code.0)
|
||||||
.header("content-type", "text/plain")
|
.header("content-type", "text/plain")
|
||||||
.body(Body::from(*status_code.1))
|
.body(Body::from(status_code.1))
|
||||||
.expect("Response should always be created.")
|
.expect("Response should always be created.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ impl StdError for ApiError {
|
|||||||
|
|
||||||
impl std::fmt::Display for ApiError {
|
impl std::fmt::Display for ApiError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
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)
|
write!(f, "{:?}: {:?}", status.0, status.1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,6 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
|
|||||||
let result = match (req.method(), path.as_ref()) {
|
let result = match (req.method(), path.as_ref()) {
|
||||||
// Methods for Client
|
// Methods for Client
|
||||||
(&Method::GET, "/node/version") => node::get_version(req),
|
(&Method::GET, "/node/version") => node::get_version(req),
|
||||||
/*
|
|
||||||
(&Method::GET, "/node/genesis_time") => node::get_genesis_time::<T>(req),
|
(&Method::GET, "/node/genesis_time") => node::get_genesis_time::<T>(req),
|
||||||
(&Method::GET, "/node/syncing") => helpers::implementation_pending_response(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/peer_id") => network::get_peer_id::<T>(req),
|
||||||
(&Method::GET, "/network/peers") => network::get_peer_list::<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_port") => network::get_listen_port::<T>(req),
|
||||||
(&Method::GET, "/network/listen_addresses") => {
|
(&Method::GET, "/network/listen_addresses") => network::get_listen_addresses::<T>(req),
|
||||||
network::get_listen_addresses::<T>(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methods for Beacon Node
|
// Methods for Beacon Node
|
||||||
(&Method::GET, "/beacon/head") => beacon::get_head::<T>(req),
|
(&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/block_root") => beacon::get_block_root::<T>(req),
|
||||||
(&Method::GET, "/beacon/blocks") => helpers::implementation_pending_response(req),
|
(&Method::GET, "/beacon/blocks") => helpers::implementation_pending_response(req),
|
||||||
(&Method::GET, "/beacon/fork") => beacon::get_fork::<T>(req),
|
(&Method::GET, "/beacon/fork") => beacon::get_fork::<T>(req),
|
||||||
(&Method::GET, "/beacon/attestations") => {
|
(&Method::GET, "/beacon/attestations") => helpers::implementation_pending_response(req),
|
||||||
helpers::implementation_pending_response(req)
|
|
||||||
}
|
|
||||||
(&Method::GET, "/beacon/attestations/pending") => {
|
(&Method::GET, "/beacon/attestations/pending") => {
|
||||||
helpers::implementation_pending_response(req)
|
helpers::implementation_pending_response(req)
|
||||||
}
|
}
|
||||||
@ -115,15 +110,9 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Methods for Validator
|
// Methods for Validator
|
||||||
(&Method::GET, "/beacon/validator/duties") => {
|
(&Method::GET, "/beacon/validator/duties") => validator::get_validator_duties::<T>(req),
|
||||||
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/block") => {
|
|
||||||
validator::get_new_beacon_block::<T>(req)
|
|
||||||
}
|
|
||||||
(&Method::POST, "/beacon/validator/block") => {
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
@ -149,7 +138,6 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
|
|||||||
|
|
||||||
(&Method::GET, "/metrics") => metrics::get_prometheus::<T>(req),
|
(&Method::GET, "/metrics") => metrics::get_prometheus::<T>(req),
|
||||||
|
|
||||||
*/
|
|
||||||
_ => Err(ApiError::NotFound(
|
_ => Err(ApiError::NotFound(
|
||||||
"Request path and/or method not found.".to_owned(),
|
"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> {
|
let service = move || -> futures::future::FutureResult<ApiService<T>, String> {
|
||||||
futures::future::ok(ApiService {
|
futures::future::ok(ApiService {
|
||||||
log: log.clone(),
|
log: server_log.clone(),
|
||||||
beacon_chain: beacon_chain.clone(),
|
beacon_chain: server_bc.clone(),
|
||||||
db_path: db_path.clone(),
|
db_path: db_path.clone(),
|
||||||
network_service: network_service.clone(),
|
network_service: network_service.clone(),
|
||||||
network_channel: Arc::new(RwLock::new(network_chan.clone())),
|
network_channel: Arc::new(RwLock::new(network_chan.clone())),
|
||||||
|
@ -203,7 +203,9 @@ pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
|||||||
Ok(success_response(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 {
|
pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let _ = check_content_type_for_json(&req)?;
|
let _ = check_content_type_for_json(&req)?;
|
||||||
let log = get_logger_from_request(&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
|
block_result
|
||||||
})
|
});
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
/*
|
|
||||||
.map_err(|e| ApiError::ServerError(format!("Unable parse request body: {:?}", e)))
|
.map_err(|e| ApiError::ServerError(format!("Unable parse request body: {:?}", e)))
|
||||||
.and_then(|body| {
|
.and_then(|body| {
|
||||||
trace!(log, "parsing json");
|
trace!(log, "parsing json");
|
||||||
@ -251,7 +250,6 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
|||||||
});
|
});
|
||||||
tokio::run(block_future);
|
tokio::run(block_future);
|
||||||
let block = block_future.wait()?;
|
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."));
|
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()) {
|
match beacon_chain.process_block(block.clone()) {
|
||||||
Ok(BlockProcessingOutcome::Processed { block_root }) => {
|
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()))
|
Ok(success_response(Body::empty()))
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/// 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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user