]> Untitled Git - bdk/commitdiff
Fix reqwest blockchain test
authorrajarshimaitra <rajarshi149@gmail.com>
Tue, 31 Aug 2021 08:56:52 +0000 (14:26 +0530)
committerrajarshimaitra <rajarshi149@gmail.com>
Tue, 14 Sep 2021 05:59:28 +0000 (11:29 +0530)
- add back await_or_block! to bdk-macros
- use await_or_block! in reqwest tests

macros/src/lib.rs
src/blockchain/esplora/reqwest.rs

index 3ba8741c2993b6c74936317572556445fcafcce0..74eda5cf4e46132bcaac4663ae6aa8230280dd50 100644 (file)
@@ -121,3 +121,26 @@ pub fn maybe_await(expr: TokenStream) -> TokenStream {
 
     quoted.into()
 }
+
+/// Awaits if target_arch is "wasm32", uses `tokio::Runtime::block_on()` otherwise
+///
+/// Requires the `tokio` crate as a dependecy with `rt-core` or `rt-threaded` to build on non-wasm32 platforms.
+#[proc_macro]
+pub fn await_or_block(expr: TokenStream) -> TokenStream {
+    let expr: proc_macro2::TokenStream = expr.into();
+    let quoted = quote! {
+        {
+            #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
+            {
+                tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(#expr)
+            }
+
+            #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
+            {
+                #expr.await
+            }
+        }
+    };
+
+    quoted.into()
+}
index 056c391345f8912fc7be4eecb19a270da55a2958..df3132da05ffa0ec62478c6f4944ce340e222e6e 100644 (file)
@@ -106,19 +106,19 @@ impl Blockchain for EsploraBlockchain {
     }
 
     fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
-        Ok(self.url_client._get_tx(txid).await?)
+        Ok(await_or_block!(self.url_client._get_tx(txid))?)
     }
 
     fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
-        Ok(self.url_client._broadcast(tx).await?)
+        Ok(await_or_block!(self.url_client._broadcast(tx))?)
     }
 
     fn get_height(&self) -> Result<u32, Error> {
-        Ok(self.url_client._get_height().await?)
+        Ok(await_or_block!(self.url_client._get_height())?)
     }
 
     fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
-        let estimates = self.url_client._get_fee_estimates().await?;
+        let estimates = await_or_block!(self.url_client._get_fee_estimates())?;
         super::into_fee_rate(target, estimates)
     }
 }
@@ -287,10 +287,10 @@ impl ElectrumLikeSync for UrlClient {
             for script in chunk {
                 futs.push(self._script_get_history(script));
             }
-            let partial_results: Vec<Vec<ElsGetHistoryRes>> = futs.try_collect().await?;
+            let partial_results: Vec<Vec<ElsGetHistoryRes>> = await_or_block!(futs.try_collect())?;
             results.extend(partial_results);
         }
-        Ok(stream::iter(results).collect().await)
+        Ok(await_or_block!(stream::iter(results).collect()))
     }
 
     fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid>>(
@@ -303,10 +303,10 @@ impl ElectrumLikeSync for UrlClient {
             for txid in chunk {
                 futs.push(self._get_tx_no_opt(txid));
             }
-            let partial_results: Vec<Transaction> = futs.try_collect().await?;
+            let partial_results: Vec<Transaction> = await_or_block!(futs.try_collect())?;
             results.extend(partial_results);
         }
-        Ok(stream::iter(results).collect().await)
+        Ok(await_or_block!(stream::iter(results).collect()))
     }
 
     fn els_batch_block_header<I: IntoIterator<Item = u32>>(
@@ -319,10 +319,10 @@ impl ElectrumLikeSync for UrlClient {
             for height in chunk {
                 futs.push(self._get_header(height));
             }
-            let partial_results: Vec<BlockHeader> = futs.try_collect().await?;
+            let partial_results: Vec<BlockHeader> = await_or_block!(futs.try_collect())?;
             results.extend(partial_results);
         }
-        Ok(stream::iter(results).collect().await)
+        Ok(await_or_block!(stream::iter(results).collect()))
     }
 }