|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! This module provides synchronous wrappers around [`BumpTransactionEventHandler`] and related types. |
| 11 | +
|
| 12 | +use core::future::Future; |
| 13 | +use core::ops::Deref; |
| 14 | +use core::task; |
| 15 | + |
| 16 | +use crate::chain::chaininterface::BroadcasterInterface; |
| 17 | +use crate::chain::ClaimId; |
| 18 | +use crate::prelude::*; |
| 19 | +use crate::sign::SignerProvider; |
| 20 | +use crate::sync::Arc; |
| 21 | +use crate::util::async_poll::{dummy_waker, AsyncResult, MaybeSend, MaybeSync}; |
| 22 | +use crate::util::logger::Logger; |
| 23 | + |
| 24 | +use bitcoin::{Psbt, ScriptBuf, Transaction, TxOut}; |
| 25 | + |
| 26 | +use super::BumpTransactionEvent; |
| 27 | +use super::{ |
| 28 | + BumpTransactionEventHandler, CoinSelection, CoinSelectionSource, Input, Utxo, Wallet, |
| 29 | + WalletSource, |
| 30 | +}; |
| 31 | + |
| 32 | +/// A synchronous version of the [`WalletSource`] trait. |
| 33 | +pub trait WalletSourceSync { |
| 34 | + /// A synchronous version of [`WalletSource::list_confirmed_utxos`]. |
| 35 | + fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()>; |
| 36 | + /// A synchronous version of [`WalletSource::get_change_script`]. |
| 37 | + fn get_change_script(&self) -> Result<ScriptBuf, ()>; |
| 38 | + /// A Synchronous version of [`WalletSource::sign_psbt`]. |
| 39 | + fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()>; |
| 40 | +} |
| 41 | + |
| 42 | +pub(crate) struct WalletSourceSyncWrapper<T: Deref>(T) |
| 43 | +where |
| 44 | + T::Target: WalletSourceSync; |
| 45 | + |
| 46 | +impl<T: Deref> WalletSource for WalletSourceSyncWrapper<T> |
| 47 | +where |
| 48 | + T::Target: WalletSourceSync, |
| 49 | +{ |
| 50 | + fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>> { |
| 51 | + let utxos = self.0.list_confirmed_utxos(); |
| 52 | + Box::pin(async move { utxos }) |
| 53 | + } |
| 54 | + |
| 55 | + fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf> { |
| 56 | + let script = self.0.get_change_script(); |
| 57 | + Box::pin(async move { script }) |
| 58 | + } |
| 59 | + |
| 60 | + fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction> { |
| 61 | + let signed_psbt = self.0.sign_psbt(psbt); |
| 62 | + Box::pin(async move { signed_psbt }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// A synchronous wrapper around [`Wallet`] to be used in contexts where async is not available. |
| 67 | +pub struct WalletSync<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> |
| 68 | +where |
| 69 | + W::Target: WalletSourceSync + MaybeSend, |
| 70 | + L::Target: Logger + MaybeSend, |
| 71 | +{ |
| 72 | + wallet: Wallet<Arc<WalletSourceSyncWrapper<W>>, L>, |
| 73 | +} |
| 74 | + |
| 75 | +impl<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> WalletSync<W, L> |
| 76 | +where |
| 77 | + W::Target: WalletSourceSync + MaybeSend, |
| 78 | + L::Target: Logger + MaybeSend, |
| 79 | +{ |
| 80 | + /// Constructs a new [`WalletSync`] instance. |
| 81 | + pub fn new(source: W, logger: L) -> Self { |
| 82 | + Self { wallet: Wallet::new(Arc::new(WalletSourceSyncWrapper(source)), logger) } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> CoinSelectionSourceSync |
| 87 | + for WalletSync<W, L> |
| 88 | +where |
| 89 | + W::Target: WalletSourceSync + MaybeSend + MaybeSync, |
| 90 | + L::Target: Logger + MaybeSend + MaybeSync, |
| 91 | +{ |
| 92 | + fn select_confirmed_utxos( |
| 93 | + &self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &[TxOut], |
| 94 | + target_feerate_sat_per_1000_weight: u32, |
| 95 | + ) -> Result<CoinSelection, ()> { |
| 96 | + let mut fut = self.wallet.select_confirmed_utxos( |
| 97 | + claim_id, |
| 98 | + must_spend, |
| 99 | + must_pay_to, |
| 100 | + target_feerate_sat_per_1000_weight, |
| 101 | + ); |
| 102 | + let mut waker = dummy_waker(); |
| 103 | + let mut ctx = task::Context::from_waker(&mut waker); |
| 104 | + match fut.as_mut().poll(&mut ctx) { |
| 105 | + task::Poll::Ready(result) => result, |
| 106 | + task::Poll::Pending => { |
| 107 | + unreachable!( |
| 108 | + "Wallet::select_confirmed_utxos should not be pending in a sync context" |
| 109 | + ); |
| 110 | + }, |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> { |
| 115 | + let mut fut = self.wallet.sign_psbt(psbt); |
| 116 | + let mut waker = dummy_waker(); |
| 117 | + let mut ctx = task::Context::from_waker(&mut waker); |
| 118 | + match fut.as_mut().poll(&mut ctx) { |
| 119 | + task::Poll::Ready(result) => result, |
| 120 | + task::Poll::Pending => { |
| 121 | + unreachable!("Wallet::sign_psbt should not be pending in a sync context"); |
| 122 | + }, |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/// A synchronous version of the [`CoinSelectionSource`] trait. |
| 128 | +pub trait CoinSelectionSourceSync { |
| 129 | + /// A synchronous version of [`CoinSelectionSource::select_confirmed_utxos`]. |
| 130 | + fn select_confirmed_utxos( |
| 131 | + &self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &[TxOut], |
| 132 | + target_feerate_sat_per_1000_weight: u32, |
| 133 | + ) -> Result<CoinSelection, ()>; |
| 134 | + |
| 135 | + /// A synchronous version of [`CoinSelectionSource::sign_psbt`]. |
| 136 | + fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()>; |
| 137 | +} |
| 138 | + |
| 139 | +struct CoinSelectionSourceSyncWrapper<T: Deref>(T) |
| 140 | +where |
| 141 | + T::Target: CoinSelectionSourceSync; |
| 142 | + |
| 143 | +impl<T: Deref> CoinSelectionSource for CoinSelectionSourceSyncWrapper<T> |
| 144 | +where |
| 145 | + T::Target: CoinSelectionSourceSync, |
| 146 | +{ |
| 147 | + fn select_confirmed_utxos<'a>( |
| 148 | + &'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut], |
| 149 | + target_feerate_sat_per_1000_weight: u32, |
| 150 | + ) -> AsyncResult<'a, CoinSelection> { |
| 151 | + let coins = self.0.select_confirmed_utxos( |
| 152 | + claim_id, |
| 153 | + must_spend, |
| 154 | + must_pay_to, |
| 155 | + target_feerate_sat_per_1000_weight, |
| 156 | + ); |
| 157 | + Box::pin(async move { coins }) |
| 158 | + } |
| 159 | + |
| 160 | + fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction> { |
| 161 | + let psbt = self.0.sign_psbt(psbt); |
| 162 | + Box::pin(async move { psbt }) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +/// A synchronous wrapper around [`BumpTransactionEventHandler`] to be used in contexts where async is not available. |
| 167 | +pub struct BumpTransactionEventHandlerSync<B: Deref, C: Deref, SP: Deref, L: Deref> |
| 168 | +where |
| 169 | + B::Target: BroadcasterInterface, |
| 170 | + C::Target: CoinSelectionSourceSync, |
| 171 | + SP::Target: SignerProvider, |
| 172 | + L::Target: Logger, |
| 173 | +{ |
| 174 | + bump_transaction_event_handler: |
| 175 | + Arc<BumpTransactionEventHandler<B, Arc<CoinSelectionSourceSyncWrapper<C>>, SP, L>>, |
| 176 | +} |
| 177 | + |
| 178 | +impl<B: Deref, C: Deref, SP: Deref, L: Deref> BumpTransactionEventHandlerSync<B, C, SP, L> |
| 179 | +where |
| 180 | + B::Target: BroadcasterInterface, |
| 181 | + C::Target: CoinSelectionSourceSync, |
| 182 | + SP::Target: SignerProvider, |
| 183 | + L::Target: Logger, |
| 184 | +{ |
| 185 | + /// Constructs a new instance of [`BumpTransactionEventHandlerSync`]. |
| 186 | + pub fn new(broadcaster: B, utxo_source: C, signer_provider: SP, logger: L) -> Self { |
| 187 | + let bump_transaction_event_handler = Arc::new(BumpTransactionEventHandler::new( |
| 188 | + broadcaster, |
| 189 | + Arc::new(CoinSelectionSourceSyncWrapper(utxo_source)), |
| 190 | + signer_provider, |
| 191 | + logger, |
| 192 | + )); |
| 193 | + Self { bump_transaction_event_handler } |
| 194 | + } |
| 195 | + |
| 196 | + /// Handles all variants of [`BumpTransactionEvent`]. |
| 197 | + pub fn handle_event(&self, event: &BumpTransactionEvent) { |
| 198 | + let mut fut = Box::pin(self.bump_transaction_event_handler.handle_event(event)); |
| 199 | + let mut waker = dummy_waker(); |
| 200 | + let mut ctx = task::Context::from_waker(&mut waker); |
| 201 | + match fut.as_mut().poll(&mut ctx) { |
| 202 | + task::Poll::Ready(result) => result, |
| 203 | + task::Poll::Pending => { |
| 204 | + // In a sync context, we can't wait for the future to complete. |
| 205 | + unreachable!("BumpTransactionEventHandlerSync::handle_event should not be pending in a sync context"); |
| 206 | + }, |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments