From e20b701677f14572d8b373a43a99fbd93e397593 Mon Sep 17 00:00:00 2001 From: Max Fang Date: Mon, 24 Apr 2023 19:15:54 -0700 Subject: [PATCH] bugfix: `GET /tx/:txid/status` is not `Option` --- src/async.rs | 8 ++------ src/blocking.rs | 11 +++-------- src/lib.rs | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/async.rs b/src/async.rs index 92badc04..bc1012fc 100644 --- a/src/async.rs +++ b/src/async.rs @@ -99,18 +99,14 @@ impl AsyncClient { } /// Get the status of a [`Transaction`] given its [`Txid`]. - pub async fn get_tx_status(&self, txid: &Txid) -> Result, Error> { + pub async fn get_tx_status(&self, txid: &Txid) -> Result { let resp = self .client .get(&format!("{}/tx/{}/status", self.url, txid)) .send() .await?; - if let StatusCode::NOT_FOUND = resp.status() { - return Ok(None); - } - - Ok(Some(resp.error_for_status()?.json().await?)) + Ok(resp.error_for_status()?.json().await?) } #[deprecated( diff --git a/src/blocking.rs b/src/blocking.rs index 4617914f..8c56c4b4 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -108,20 +108,15 @@ impl BlockingClient { } /// Get the status of a [`Transaction`] given its [`Txid`]. - pub fn get_tx_status(&self, txid: &Txid) -> Result, Error> { + pub fn get_tx_status(&self, txid: &Txid) -> Result { let resp = self .agent .get(&format!("{}/tx/{}/status", self.url, txid)) .call(); match resp { - Ok(resp) => Ok(Some(resp.into_json()?)), - Err(ureq::Error::Status(code, _)) => { - if is_status_not_found(code) { - return Ok(None); - } - Err(Error::HttpResponse(code)) - } + Ok(resp) => Ok(resp.into_json()?), + Err(ureq::Error::Status(code, _)) => Err(Error::HttpResponse(code)), Err(e) => Err(Error::Ureq(e)), } } diff --git a/src/lib.rs b/src/lib.rs index bdd79b07..574618ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -460,10 +460,20 @@ mod test { let _miner = MINER.lock().await; generate_blocks_and_wait(1); - let tx_status = blocking_client.get_tx_status(&txid).unwrap().unwrap(); - let tx_status_async = async_client.get_tx_status(&txid).await.unwrap().unwrap(); + let tx_status = blocking_client.get_tx_status(&txid).unwrap(); + let tx_status_async = async_client.get_tx_status(&txid).await.unwrap(); assert_eq!(tx_status, tx_status_async); assert!(tx_status.confirmed); + + // Bogus txid returns a TxStatus with false, None, None, None + let txid = Txid::hash(b"ayyyy lmao"); + let tx_status = blocking_client.get_tx_status(&txid).unwrap(); + let tx_status_async = async_client.get_tx_status(&txid).await.unwrap(); + assert_eq!(tx_status, tx_status_async); + assert!(!tx_status.confirmed); + assert!(tx_status.block_height.is_none()); + assert!(tx_status.block_hash.is_none()); + assert!(tx_status.block_time.is_none()); } #[cfg(all(feature = "blocking", feature = "async"))]