Make async esplora futures Send

This commit is contained in:
LLFourn 2023-03-10 12:12:00 +11:00
parent 5dd4ce74cf
commit 2fcf9c4adb
No known key found for this signature in database
GPG Key ID: A27093B54DA11F65

View File

@ -12,7 +12,9 @@ use futures::stream::{FuturesOrdered, TryStreamExt};
use crate::map_confirmation_time; use crate::map_confirmation_time;
#[async_trait(?Send)] #[cfg(feature = "async")]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait EsploraAsyncExt { pub trait EsploraAsyncExt {
/// Scan the blockchain (via esplora) for the data specified and returns a [`KeychainScan`]. /// Scan the blockchain (via esplora) for the data specified and returns a [`KeychainScan`].
/// ///
@ -28,12 +30,15 @@ pub trait EsploraAsyncExt {
/// ///
/// [`ChainPosition`]: bdk_chain::sparse_chain::ChainPosition /// [`ChainPosition`]: bdk_chain::sparse_chain::ChainPosition
#[allow(clippy::result_large_err)] // FIXME #[allow(clippy::result_large_err)] // FIXME
async fn scan<K: Ord + Clone>( async fn scan<K: Ord + Clone + Send>(
&self, &self,
local_chain: &BTreeMap<u32, BlockHash>, local_chain: &BTreeMap<u32, BlockHash>,
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, Script)>>, keychain_spks: BTreeMap<
txids: impl IntoIterator<Item = Txid>, K,
outpoints: impl IntoIterator<Item = OutPoint>, impl IntoIterator<IntoIter = impl Iterator<Item = (u32, Script)> + Send> + Send,
>,
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
stop_gap: usize, stop_gap: usize,
parallel_requests: usize, parallel_requests: usize,
) -> Result<KeychainScan<K, ConfirmationTime>, Error>; ) -> Result<KeychainScan<K, ConfirmationTime>, Error>;
@ -45,9 +50,9 @@ pub trait EsploraAsyncExt {
async fn scan_without_keychain( async fn scan_without_keychain(
&self, &self,
local_chain: &BTreeMap<u32, BlockHash>, local_chain: &BTreeMap<u32, BlockHash>,
misc_spks: impl IntoIterator<Item = Script>, misc_spks: impl IntoIterator<IntoIter = impl Iterator<Item = Script> + Send> + Send,
txids: impl IntoIterator<Item = Txid>, txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
outpoints: impl IntoIterator<Item = OutPoint>, outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
parallel_requests: usize, parallel_requests: usize,
) -> Result<ChainGraph<ConfirmationTime>, Error> { ) -> Result<ChainGraph<ConfirmationTime>, Error> {
let wallet_scan = self let wallet_scan = self
@ -72,17 +77,25 @@ pub trait EsploraAsyncExt {
} }
} }
#[async_trait(?Send)] #[cfg(feature = "async")]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl EsploraAsyncExt for esplora_client::AsyncClient { impl EsploraAsyncExt for esplora_client::AsyncClient {
async fn scan<K: Ord + Clone>( #[allow(clippy::result_large_err)] // FIXME
async fn scan<K: Ord + Clone + Send>(
&self, &self,
local_chain: &BTreeMap<u32, BlockHash>, local_chain: &BTreeMap<u32, BlockHash>,
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, Script)>>, keychain_spks: BTreeMap<
txids: impl IntoIterator<Item = Txid>, K,
outpoints: impl IntoIterator<Item = OutPoint>, impl IntoIterator<IntoIter = impl Iterator<Item = (u32, Script)> + Send> + Send,
>,
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
stop_gap: usize, stop_gap: usize,
parallel_requests: usize, parallel_requests: usize,
) -> Result<KeychainScan<K, ConfirmationTime>, Error> { ) -> Result<KeychainScan<K, ConfirmationTime>, Error> {
let txids = txids.into_iter();
let outpoints = outpoints.into_iter();
let parallel_requests = parallel_requests.max(1); let parallel_requests = parallel_requests.max(1);
let mut scan = KeychainScan::default(); let mut scan = KeychainScan::default();
let update = &mut scan.update; let update = &mut scan.update;
@ -204,7 +217,7 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
} }
} }
for txid in txids.into_iter() { for txid in txids {
let (tx, tx_status) = let (tx, tx_status) =
match (self.get_tx(&txid).await?, self.get_tx_status(&txid).await?) { match (self.get_tx(&txid).await?, self.get_tx_status(&txid).await?) {
(Some(tx), Some(tx_status)) => (tx, tx_status), (Some(tx), Some(tx_status)) => (tx, tx_status),
@ -227,7 +240,7 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
} }
} }
for op in outpoints.into_iter() { for op in outpoints {
let mut op_txs = Vec::with_capacity(2); let mut op_txs = Vec::with_capacity(2);
if let (Some(tx), Some(tx_status)) = ( if let (Some(tx), Some(tx_status)) = (
self.get_tx(&op.txid).await?, self.get_tx(&op.txid).await?,