use super::utils::w;
use crate::chain::{Chain, SyncState, SyncStatus};
use crate::p2p;
use crate::rest::*;
use crate::router::{Handler, ResponseFuture};
use crate::types::*;
use crate::web::*;
use hyper::{Body, Request, StatusCode};
use serde_json::json;
use std::sync::Weak;
pub struct IndexHandler {
pub list: Vec<String>,
}
impl IndexHandler {}
impl Handler for IndexHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture {
json_response_pretty(&self.list)
}
}
pub struct KernelDownloadHandler {
pub peers: Weak<p2p::Peers>,
}
impl Handler for KernelDownloadHandler {
fn post(&self, _req: Request<Body>) -> ResponseFuture {
if let Some(peer) = w_fut!(&self.peers).most_work_peer() {
match peer.send_kernel_data_request() {
Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("requesting kernel data from peer failed: {:?}", e),
),
}
} else {
response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("requesting kernel data from peer failed (no peers)"),
)
}
}
}
pub struct StatusHandler {
pub chain: Weak<Chain>,
pub peers: Weak<p2p::Peers>,
pub sync_state: Weak<SyncState>,
}
impl StatusHandler {
pub fn get_status(&self) -> Result<Status, Error> {
let head = w(&self.chain)?
.head()
.map_err(|e| Error::Internal(format!("can't get head: {}", e)))?;
let sync_status = w(&self.sync_state)?.status();
let (api_sync_status, api_sync_info) = sync_status_to_api(sync_status);
Ok(Status::from_tip_and_peers(
head,
w(&self.peers)?.peer_count(),
api_sync_status,
api_sync_info,
))
}
}
impl Handler for StatusHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture {
result_to_response(self.get_status())
}
}
fn sync_status_to_api(sync_status: SyncStatus) -> (String, Option<serde_json::Value>) {
match sync_status {
SyncStatus::NoSync => ("no_sync".to_string(), None),
SyncStatus::AwaitingPeers(_) => ("awaiting_peers".to_string(), None),
SyncStatus::HeaderSync {
current_height,
highest_height,
} => (
"header_sync".to_string(),
Some(json!({ "current_height": current_height, "highest_height": highest_height })),
),
SyncStatus::TxHashsetDownload {
start_time: _,
prev_update_time: _,
update_time: _,
prev_downloaded_size: _,
downloaded_size,
total_size,
} => (
"txhashset_download".to_string(),
Some(json!({ "downloaded_size": downloaded_size, "total_size": total_size })),
),
SyncStatus::TxHashsetRangeProofsValidation {
rproofs,
rproofs_total,
} => (
"txhashset_rangeproofs_validation".to_string(),
Some(json!({ "rproofs": rproofs, "rproofs_total": rproofs_total })),
),
SyncStatus::TxHashsetKernelsValidation {
kernels,
kernels_total,
} => (
"txhashset_kernels_validation".to_string(),
Some(json!({ "kernels": kernels, "kernels_total": kernels_total })),
),
SyncStatus::BodySync {
current_height,
highest_height,
} => (
"body_sync".to_string(),
Some(json!({ "current_height": current_height, "highest_height": highest_height })),
),
SyncStatus::Shutdown => ("shutdown".to_string(), None),
_ => ("syncing".to_string(), None),
}
}