Skip to content

Commit d8f4c67

Browse files
committed
f: add WalletSync
1 parent 4107b83 commit d8f4c67

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,11 @@ pub trait CoinSelectionSourceSync {
375375
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()>;
376376
}
377377

378-
/// A wrapper around [`CoinSelectionSourceSync`] to allow for async calls. This wrapper isn't intended to be used
379-
/// directly, because that would risk blocking an async context.
378+
/// A wrapper around [`CoinSelectionSourceSync`] to allow for async calls.
379+
///
380+
/// This wrapper isn't intended to be used directly, because that would risk blocking an async context. Instead, it is
381+
/// built for you in [`BumpTransactionEventHandlerSync::new`].
382+
#[doc(hidden)]
380383
#[cfg(any(test, feature = "_test_utils"))]
381384
pub struct CoinSelectionSourceSyncWrapper<T: Deref>(T)
382385
where
@@ -454,7 +457,11 @@ pub trait WalletSourceSync {
454457
}
455458

456459
/// A wrapper around [`WalletSourceSync`] to allow for async calls.
457-
pub struct WalletSourceSyncWrapper<T: Deref>(T)
460+
///
461+
/// This wrapper isn't intended to be used directly, because that would risk blocking an async context. Instead, it is
462+
/// built for you in [`WalletSync::new`].
463+
#[doc(hidden)]
464+
pub(crate) struct WalletSourceSyncWrapper<T: Deref>(T)
458465
where
459466
T::Target: WalletSourceSync;
460467

@@ -494,6 +501,67 @@ where
494501
}
495502
}
496503

504+
/// A synchronous wrapper around [`Wallet`] to be used in contexts where async is not available.
505+
pub struct WalletSync<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend>
506+
where
507+
W::Target: WalletSourceSync + MaybeSend,
508+
L::Target: Logger + MaybeSend,
509+
{
510+
wallet: Wallet<Arc<WalletSourceSyncWrapper<W>>, L>,
511+
}
512+
513+
impl<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> WalletSync<W, L>
514+
where
515+
W::Target: WalletSourceSync + MaybeSend,
516+
L::Target: Logger + MaybeSend,
517+
{
518+
/// Constructs a new [`WalletSync`] instance.
519+
pub fn new(source: W, logger: L) -> Self {
520+
Self { wallet: Wallet::new(Arc::new(WalletSourceSyncWrapper::new(source)), logger) }
521+
}
522+
}
523+
524+
impl<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> CoinSelectionSourceSync
525+
for WalletSync<W, L>
526+
where
527+
W::Target: WalletSourceSync + MaybeSend + MaybeSync,
528+
L::Target: Logger + MaybeSend + MaybeSync,
529+
{
530+
fn select_confirmed_utxos(
531+
&self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &[TxOut],
532+
target_feerate_sat_per_1000_weight: u32,
533+
) -> Result<CoinSelection, ()> {
534+
let mut fut = Box::pin(self.wallet.select_confirmed_utxos(
535+
claim_id,
536+
must_spend,
537+
must_pay_to,
538+
target_feerate_sat_per_1000_weight,
539+
));
540+
let mut waker = dummy_waker();
541+
let mut ctx = task::Context::from_waker(&mut waker);
542+
match fut.as_mut().poll(&mut ctx) {
543+
task::Poll::Ready(result) => result,
544+
task::Poll::Pending => {
545+
unreachable!(
546+
"Wallet::select_confirmed_utxos should not be pending in a sync context"
547+
);
548+
},
549+
}
550+
}
551+
552+
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
553+
let mut fut = Box::pin(self.wallet.sign_psbt(psbt));
554+
let mut waker = dummy_waker();
555+
let mut ctx = task::Context::from_waker(&mut waker);
556+
match fut.as_mut().poll(&mut ctx) {
557+
task::Poll::Ready(result) => result,
558+
task::Poll::Pending => {
559+
unreachable!("Wallet::sign_psbt should not be pending in a sync context");
560+
},
561+
}
562+
}
563+
}
564+
497565
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
498566
/// avoid conflicting double spends. If not enough UTXOs are available to do so, conflicting double
499567
/// spends may happen.

0 commit comments

Comments
 (0)