use redgold_common::client::http::RgHttpClient;
use crate::api::rosetta;
use crate::api::rosetta::handlers::*;
use crate::api::rosetta::models;
use crate::api::rosetta::models::{AccountBalanceRequest, AccountBalanceResponse, AccountCoinsRequest, AccountIdentifier, Error};
use crate::api::rosetta::spec::Rosetta;
use crate::api::warp_helpers::{easy_post, with_response_logger, with_response_logger_error};
use crate::core::relay::Relay;
use crate::util::random_port;
use bytes::Bytes;
use futures::TryFutureExt;
use redgold_schema::structs::ErrorInfo;
use redgold_schema::util::lang_util::SameResult;
use redgold_schema::{error_message, error_msg, structs, ErrorInfoContext, RgResult};
use reqwest::StatusCode;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::time::Duration;
use tracing::trace;
use tracing::{error, info};
use warp::path::FullPath;
use warp::reply::{Json, WithStatus};
use warp::{Filter, Rejection};
fn format_error<T>(
result: Result<T, ErrorInfo>
) -> Result<T, Error> {
result.map_err(|e| {
let e2 = e.clone();
Error {
code: e.code as u32,
message: e.description,
description: Some(e.description_extended),
retriable: e.retriable,
details: Some(e2),
}
})
}
fn format_response<T>(
result: Result<T, Error>,
) -> WithStatus<Json>
where
T: Serialize,
{
let response = result
.map_err(|e| {
warp::reply::with_status(
warp::reply::json(&e),
StatusCode::from_u16(500).expect("Status code"),
)
})
.map(|j| {
warp::reply::with_status(
warp::reply::json(&j),
StatusCode::from_u16(200).expect("Status code"),
)
})
.combine();
response
}
async fn deser<'a, T : Deserialize<'a>>(s: &'a str) -> Result<T, ErrorInfo> {
serde_json::from_str::<T>(s)
.error_msg(structs::ErrorCode::DeserializationFailure, "error deserializing string input as type")
}
fn ser<T>(t: Result<T, ErrorInfo>, e: String) -> WithStatus<Json>
where
T: Serialize + Clone,{
with_response_logger_error(t.clone(), e).ok();
let result = format_error(t);
format_response(result)
}
pub async fn handle_warp_request(path: String, r: Rosetta, b: Bytes) -> Result<WithStatus<Json>, Rejection> {
let e = path.clone();
let p = &*path;
let vec = b.to_vec();
let so = match std::str::from_utf8(&*vec) {
Ok(v) => v,
Err(er) => {
return Ok(
ser(Err::<AccountBalanceResponse, ErrorInfo>(
error_msg(structs::ErrorCode::UnknownError, "Error deserializing bytes to utf8 string", er.to_string())
), e.clone())
);
},
};
let so2 = so;
let s = so2.to_owned();
log::debug!("Request ENDPOINT={} BODY_TEXT={}", e.clone(), s.clone());
let res = match p {
"/account/balance" => {
ser(deser(so).and_then(|req| account_balance(r, req)).await, e)
},
"/account/coins" => {
ser(deser(so).and_then(|req| account_coins(r, req)).await, e)
},
"/block" => {
ser(deser(so).and_then(|req| block(r, req)).await, e)
},
"/block/transaction" => {
ser(deser(so).and_then(|req| block_transaction(r, req)).await, e)
},
"/call" => {
ser(deser(so).and_then(|req| call(r, req)).await, e)
},
"/construction/combine" => {
ser(deser(so).and_then(|req| construction_combine(r, req)).await, e)
},
"/construction/derive" => {
ser(deser(so).and_then(|req| construction_derive(r, req)).await, e)
},
"/construction/hash" => {
ser(deser(so).and_then(|req| construction_hash(r, req)).await, e)
},
"/construction/metadata" => {
ser(deser(so).and_then(|req| construction_metadata(r, req)).await, e)
},
"/construction/parse" => {
ser(deser(so).and_then(|req| construction_parse(r, req)).await, e)
},
"/construction/payloads" => {
ser(deser(so).and_then(|req| construction_payloads(r, req)).await, e)
},
"/construction/preprocess" => {
ser(deser(so).and_then(|req| construction_preprocess(r, req)).await, e)
},
"/construction/submit" => {
ser(deser(so).and_then(|req| construction_submit(r, req)).await, e)
},
"/events/blocks" => {
ser(deser(so).and_then(|req| events_blocks(r, req)).await, e)
},
"/mempool" => {
ser(deser(so).and_then(|req| mempool(r, req)).await, e)
},
"/mempool/transaction" => {
ser(deser(so).and_then(|req| mempool_transaction(r, req)).await, e)
},
"/network/list" => {
ser(deser(so).and_then(|req| network_list(r, req)).await, e)
},
"/network/options" => {
ser(deser(so).and_then(|req| network_options(r, req)).await, e)
},
"/network/status" => {
ser(deser(so).and_then(|req| network_status(r, req)).await, e)
},
"/search/transactions" => {
ser(deser(so).and_then(|req| search_transactions(r, req)).await, e)
},
x => {
log::error!("Invalid or unsupported endpoint for rosetta {}", x.to_string());
ser(Err::<Error, ErrorInfo>(error_message(
structs::ErrorCode::UnknownError, format!("unknown endpoint {}", e))
), e)
}
};
Ok(res)
}
pub async fn run_server(relay: Relay) -> RgResult<()> {
let relay2 = relay.clone();
let hello = warp::get().and_then(|| async {
let res: Result<&str, Rejection> = Ok("hello");
res
});
let r = Rosetta {
relay: relay2.clone(),
};
let endpoints = warp::post()
.and(warp::path::full().map(|path: FullPath| {
let p = path.as_str().to_string();
log::info!("warp map func full path {}", p);
p
}))
.map(move |p| (p, r.clone()))
.untuple_one()
.and(warp::body::bytes())
.and_then(handle_warp_request);
let port = relay2.node_config.rosetta_port();
trace!("Running rosetta API on port: {:?}", port.clone());
warp::serve(endpoints.or(hello))
.run(([0, 0, 0, 0], port))
.await;
Ok(())
}