Skip to content

Commit

Permalink
arg verify chain + read_timeout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
deevope committed Dec 26, 2021
1 parent 880a65e commit 1e8ba0c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
38 changes: 25 additions & 13 deletions api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn get<T>(url: &str, api_secret: Option<String>) -> Result<T, Error>
where
for<'de> T: Deserialize<'de>,
{
handle_request(build_request(url, "GET", api_secret, None)?)
handle_request(build_request(url, "GET", api_secret, None)?, None)
}

/// Helper function to easily issue an async HTTP GET request against a given
Expand All @@ -52,21 +52,26 @@ where
/// on a given URL that returns nothing. Handles request
/// building and response code checking.
pub fn get_no_ret(url: &str, api_secret: Option<String>) -> Result<(), Error> {
send_request(build_request(url, "GET", api_secret, None)?)?;
send_request(build_request(url, "GET", api_secret, None)?, None)?;
Ok(())
}

/// Helper function to easily issue a HTTP POST request with the provided JSON
/// object as body on a given URL that returns a JSON object. Handles request
/// building, JSON serialization and deserialization, and response code
/// checking.
pub fn post<IN, OUT>(url: &str, api_secret: Option<String>, input: &IN) -> Result<OUT, Error>
pub fn post<IN, OUT>(
url: &str,
api_secret: Option<String>,
input: &IN,
read_timeout: Option<u64>,
) -> Result<OUT, Error>
where
IN: Serialize,
for<'de> OUT: Deserialize<'de>,
{
let req = create_post_request(url, api_secret, input)?;
handle_request(req)
handle_request(req, read_timeout)
}

/// Helper function to easily issue an async HTTP POST request with the
Expand Down Expand Up @@ -94,7 +99,7 @@ pub fn post_no_ret<IN>(url: &str, api_secret: Option<String>, input: &IN) -> Res
where
IN: Serialize,
{
send_request(create_post_request(url, api_secret, input)?)?;
send_request(create_post_request(url, api_secret, input)?, None)?;
Ok(())
}

Expand All @@ -110,7 +115,7 @@ pub async fn post_no_ret_async<IN>(
where
IN: Serialize,
{
send_request_async(create_post_request(url, api_secret, input)?).await?;
send_request_async(create_post_request(url, api_secret, input)?, None).await?;
Ok(())
}

Expand Down Expand Up @@ -155,11 +160,11 @@ where
build_request(url, "POST", api_secret, Some(json))
}

fn handle_request<T>(req: Request<Body>) -> Result<T, Error>
fn handle_request<T>(req: Request<Body>, read_timeout: Option<u64>) -> Result<T, Error>
where
for<'de> T: Deserialize<'de>,
{
let data = send_request(req)?;
let data = send_request(req, read_timeout)?;
serde_json::from_str(&data).map_err(|e| {
e.context(ErrorKind::ResponseError("Cannot parse response".to_owned()))
.into()
Expand All @@ -170,17 +175,24 @@ async fn handle_request_async<T>(req: Request<Body>) -> Result<T, Error>
where
for<'de> T: Deserialize<'de> + Send + 'static,
{
let data = send_request_async(req).await?;
let data = send_request_async(req, None).await?;
let ser = serde_json::from_str(&data)
.map_err(|e| e.context(ErrorKind::ResponseError("Cannot parse response".to_owned())))?;
Ok(ser)
}

async fn send_request_async(req: Request<Body>) -> Result<String, Error> {
async fn send_request_async(
req: Request<Body>,
read_timeout: Option<u64>,
) -> Result<String, Error> {
let https = hyper_rustls::HttpsConnector::new();
let read_timeout = match read_timeout {
Some(v) => Duration::from_secs(v),
None => Duration::from_secs(20),
};
let mut connector = TimeoutConnector::new(https);
connector.set_connect_timeout(Some(Duration::from_secs(20)));
connector.set_read_timeout(Some(Duration::from_secs(20)));
connector.set_read_timeout(Some(read_timeout));
connector.set_write_timeout(Some(Duration::from_secs(20)));
let client = Client::builder().build::<_, Body>(connector);

Expand All @@ -205,11 +217,11 @@ async fn send_request_async(req: Request<Body>) -> Result<String, Error> {
Ok(String::from_utf8_lossy(&raw).to_string())
}

pub fn send_request(req: Request<Body>) -> Result<String, Error> {
pub fn send_request(req: Request<Body>, read_timeout: Option<u64>) -> Result<String, Error> {
let mut rt = Builder::new()
.basic_scheduler()
.enable_all()
.build()
.map_err(|e| ErrorKind::RequestError(format!("{}", e)))?;
rt.block_on(send_request_async(req))
rt.block_on(send_request_async(req, read_timeout))
}
2 changes: 1 addition & 1 deletion servers/src/mining/mine_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn create_coinbase(dest: &str, block_fees: &BlockFees) -> Result<CbData, Error>

trace!("Sending build_coinbase request: {}", req_body);
let req = api::client::create_post_request(url.as_str(), None, &req_body)?;
let res: String = api::client::send_request(req).map_err(|e| {
let res: String = api::client::send_request(req, None).map_err(|e| {
let report = format!(
"Failed to get coinbase from {}. Is the wallet listening? {}",
dest, e
Expand Down
12 changes: 10 additions & 2 deletions src/bin/cmd/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ impl HTTPNodeClient {
method: &str,
params: &serde_json::Value,
) -> Result<D, Error> {
let read_timeout: Option<u64> = match method {
"validate_chain" => Some(5000),
_ => None,
};
let url = format!("http://{}{}", self.node_url, ENDPOINT);
let req = build_request(method, params);
let res =
client::post::<Request, Response>(url.as_str(), self.node_api_secret.clone(), &req);
let res = client::post::<Request, Response>(
url.as_str(),
self.node_api_secret.clone(),
&req,
read_timeout,
);

match res {
Err(e) => {
Expand Down

0 comments on commit 1e8ba0c

Please sign in to comment.