Skip to content

Commit 462afa0

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

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 53 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,49 @@ 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> CoinSelectionSource
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<'a>(
531+
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
532+
target_feerate_sat_per_1000_weight: u32,
533+
) -> AsyncResult<'a, CoinSelection> {
534+
self.wallet.select_confirmed_utxos(
535+
claim_id,
536+
must_spend,
537+
must_pay_to,
538+
target_feerate_sat_per_1000_weight,
539+
)
540+
}
541+
542+
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction> {
543+
self.wallet.sign_psbt(psbt)
544+
}
545+
}
546+
497547
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
498548
/// avoid conflicting double spends. If not enough UTXOs are available to do so, conflicting double
499549
/// spends may happen.

lightning/src/ln/functional_test_utils.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen, Watch
1414
use crate::chain::channelmonitor::ChannelMonitor;
1515
use crate::chain::transaction::OutPoint;
1616
use crate::events::{ClaimedHTLC, ClosureReason, Event, HTLCHandlingFailureType, PaidBolt12Invoice, PathFailure, PaymentFailureReason, PaymentPurpose};
17-
use crate::events::bump_transaction::{BumpTransactionEvent, BumpTransactionEventHandler, Wallet, WalletSourceSync, WalletSourceSyncWrapper};
17+
use crate::events::bump_transaction::{BumpTransactionEvent, BumpTransactionEventHandler, WalletSourceSync, WalletSync};
1818
use crate::ln::types::ChannelId;
1919
use crate::types::features::ChannelTypeFeatures;
2020
use crate::types::payment::{PaymentPreimage, PaymentHash, PaymentSecret};
@@ -472,7 +472,7 @@ pub struct Node<'chan_man, 'node_cfg: 'chan_man, 'chan_mon_cfg: 'node_cfg> {
472472
pub wallet_source: Arc<test_utils::TestWalletSource>,
473473
pub bump_tx_handler: BumpTransactionEventHandler<
474474
&'chan_mon_cfg test_utils::TestBroadcaster,
475-
Arc<Wallet<Arc<WalletSourceSyncWrapper<Arc<test_utils::TestWalletSource>>>, &'chan_mon_cfg test_utils::TestLogger>>,
475+
Arc<WalletSync<Arc<test_utils::TestWalletSource>, &'chan_mon_cfg test_utils::TestLogger>>,
476476
&'chan_mon_cfg test_utils::TestKeysInterface,
477477
&'chan_mon_cfg test_utils::TestLogger,
478478
>,
@@ -3422,7 +3422,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
34223422
);
34233423
let gossip_sync = P2PGossipSync::new(cfgs[i].network_graph.as_ref(), None, cfgs[i].logger);
34243424
let wallet_source = Arc::new(test_utils::TestWalletSource::new(SecretKey::from_slice(&[i as u8 + 1; 32]).unwrap()));
3425-
let wallet_source_async = Arc::new(WalletSourceSyncWrapper::new(Arc::clone(&wallet_source)));
3425+
let wallet = Arc::new(WalletSync::new(wallet_source.clone(), cfgs[i].logger));
34263426
nodes.push(Node{
34273427
chain_source: cfgs[i].chain_source, tx_broadcaster: cfgs[i].tx_broadcaster,
34283428
fee_estimator: cfgs[i].fee_estimator, router: &cfgs[i].router,
@@ -3434,9 +3434,9 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
34343434
blocks: Arc::clone(&cfgs[i].tx_broadcaster.blocks),
34353435
connect_style: Rc::clone(&connect_style),
34363436
override_init_features: Rc::clone(&cfgs[i].override_init_features),
3437-
wallet_source: Arc::clone(&wallet_source),
3437+
wallet_source: wallet_source.clone(),
34383438
bump_tx_handler: BumpTransactionEventHandler::new(
3439-
cfgs[i].tx_broadcaster, Arc::new(Wallet::new(wallet_source_async, cfgs[i].logger)),
3439+
cfgs[i].tx_broadcaster, wallet,
34403440
&cfgs[i].keys_manager, cfgs[i].logger,
34413441
),
34423442
})

0 commit comments

Comments
 (0)