Skip to content

Commit ead3a1d

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

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ pub trait WalletSourceSync {
454454
}
455455

456456
/// A wrapper around [`WalletSourceSync`] to allow for async calls.
457-
pub struct WalletSourceSyncWrapper<T: Deref>(T)
457+
pub(crate) struct WalletSourceSyncWrapper<T: Deref>(T)
458458
where
459459
T::Target: WalletSourceSync;
460460

@@ -494,6 +494,67 @@ where
494494
}
495495
}
496496

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

0 commit comments

Comments
 (0)