Skip to content

Commit 4a9c1a6

Browse files
committed
Allow passing an OutputSweeperSync to the sync-KVStore-async-BP
`OutputSweeper::new_with_kv_store_sync` is a pretty strange API - it allows building an async `OutputSweeper` where the only `await`s are on a sync `KVStore`, ie will immediately block until the IO operation completes. While this isn't broken (futures are allowed to take their time, and async runtimes have to handle this, though they often don't handle it particularly well), its pretty weird. It seems to exist largely for `process_events_async_with_kv_store_sync`, which does async `Event` handling but sync `KVStore` operations (like the existing pre-0.2 "async" background processor). Instead, we allow passing an `OutputSweeperSync` to `process_events_async_with_kv_store_sync`, keeping the API consistent such that a user would use the appropriate `OutputSweeper` variant, but fetching the inner async `OutputSweeper` inside the BP.
1 parent 552cd39 commit 4a9c1a6

File tree

3 files changed

+32
-76
lines changed

3 files changed

+32
-76
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ use lightning::onion_message::messenger::AOnionMessenger;
4848
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
4949
use lightning::routing::scoring::{ScoreUpdate, WriteableScore};
5050
use lightning::routing::utxo::UtxoLookup;
51-
use lightning::sign::ChangeDestinationSource;
52-
#[cfg(feature = "std")]
53-
use lightning::sign::ChangeDestinationSourceSync;
54-
use lightning::sign::EntropySource;
55-
use lightning::sign::OutputSpender;
51+
use lightning::sign::{
52+
ChangeDestinationSource, ChangeDestinationSourceSync, EntropySource, OutputSpender,
53+
};
5654
use lightning::util::logger::Logger;
5755
use lightning::util::persist::{
5856
KVStore, KVStoreSync, KVStoreSyncWrapper, CHANNEL_MANAGER_PERSISTENCE_KEY,
@@ -1361,7 +1359,7 @@ pub async fn process_events_async_with_kv_store_sync<
13611359
D: Deref,
13621360
O: Deref,
13631361
K: Deref,
1364-
OS: Deref<Target = OutputSweeper<T, D, F, CF, KVStoreSyncWrapper<K>, L, O>>,
1362+
OS: Deref<Target = OutputSweeperSync<T, D, F, CF, K, L, O>>,
13651363
S: Deref<Target = SC> + Send + Sync,
13661364
SC: for<'b> WriteableScore<'b>,
13671365
SleepFuture: core::future::Future<Output = bool> + core::marker::Unpin,
@@ -1386,7 +1384,7 @@ where
13861384
PM::Target: APeerManager,
13871385
LM::Target: ALiquidityManager,
13881386
O::Target: OutputSpender,
1389-
D::Target: ChangeDestinationSource,
1387+
D::Target: ChangeDestinationSourceSync,
13901388
K::Target: KVStoreSync,
13911389
{
13921390
let kv_store = KVStoreSyncWrapper(kv_store);
@@ -1399,7 +1397,7 @@ where
13991397
gossip_sync,
14001398
peer_manager,
14011399
liquidity_manager,
1402-
sweeper,
1400+
sweeper.as_ref().map(|os| os.sweeper_async()),
14031401
logger,
14041402
scorer,
14051403
sleeper,

lightning/src/sign/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,12 +1052,11 @@ pub trait ChangeDestinationSourceSync {
10521052
}
10531053

10541054
/// A wrapper around [`ChangeDestinationSource`] to allow for async calls.
1055-
#[cfg(any(test, feature = "_test_utils"))]
1055+
///
1056+
/// You should likely never use this directly but rather allow LDK to build this when required to
1057+
/// build higher-level sync wrappers.
1058+
#[doc(hidden)]
10561059
pub struct ChangeDestinationSourceSyncWrapper<T: Deref>(T)
1057-
where
1058-
T::Target: ChangeDestinationSourceSync;
1059-
#[cfg(not(any(test, feature = "_test_utils")))]
1060-
pub(crate) struct ChangeDestinationSourceSyncWrapper<T: Deref>(T)
10611060
where
10621061
T::Target: ChangeDestinationSourceSync;
10631062

@@ -1080,6 +1079,16 @@ where
10801079
}
10811080
}
10821081

1082+
impl<T: Deref> Deref for ChangeDestinationSourceSyncWrapper<T>
1083+
where
1084+
T::Target: ChangeDestinationSourceSync,
1085+
{
1086+
type Target = Self;
1087+
fn deref(&self) -> &Self {
1088+
self
1089+
}
1090+
}
1091+
10831092
mod sealed {
10841093
use bitcoin::secp256k1::{Scalar, SecretKey};
10851094

lightning/src/util/sweep.rs

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::sign::{
1919
ChangeDestinationSource, ChangeDestinationSourceSync, ChangeDestinationSourceSyncWrapper,
2020
OutputSpender, SpendableOutputDescriptor,
2121
};
22-
use crate::sync::Arc;
2322
use crate::sync::Mutex;
2423
use crate::util::logger::Logger;
2524
use crate::util::persist::{
@@ -353,47 +352,6 @@ where
353352
logger: L,
354353
}
355354

356-
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
357-
OutputSweeper<B, D, E, F, KVStoreSyncWrapper<K>, L, O>
358-
where
359-
B::Target: BroadcasterInterface,
360-
D::Target: ChangeDestinationSource,
361-
E::Target: FeeEstimator,
362-
F::Target: Filter + Send + Sync,
363-
K::Target: KVStoreSync,
364-
L::Target: Logger,
365-
O::Target: OutputSpender,
366-
{
367-
/// Constructs a new [`OutputSweeper`] based on a [`KVStoreSync`].
368-
pub fn new_with_kv_store_sync(
369-
best_block: BestBlock, broadcaster: B, fee_estimator: E, chain_data_source: Option<F>,
370-
output_spender: O, change_destination_source: D, kv_store_sync: K, logger: L,
371-
) -> Self {
372-
let kv_store = KVStoreSyncWrapper(kv_store_sync);
373-
374-
Self::new(
375-
best_block,
376-
broadcaster,
377-
fee_estimator,
378-
chain_data_source,
379-
output_spender,
380-
change_destination_source,
381-
kv_store,
382-
logger,
383-
)
384-
}
385-
386-
/// Reads an [`OutputSweeper`] from the given reader and returns it with a synchronous [`KVStoreSync`].
387-
pub fn read_with_kv_store_sync<R: io::Read>(
388-
reader: &mut R, args: (B, E, Option<F>, O, D, K, L),
389-
) -> Result<Self, DecodeError> {
390-
let kv_store = KVStoreSyncWrapper(args.5);
391-
let args = (args.0, args.1, args.2, args.3, args.4, kv_store, args.6);
392-
393-
Self::read(reader, args)
394-
}
395-
}
396-
397355
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
398356
OutputSweeper<B, D, E, F, K, L, O>
399357
where
@@ -981,15 +939,8 @@ where
981939
L::Target: Logger,
982940
O::Target: OutputSpender,
983941
{
984-
sweeper: OutputSweeper<
985-
B,
986-
Arc<ChangeDestinationSourceSyncWrapper<D>>,
987-
E,
988-
F,
989-
KVStoreSyncWrapper<K>,
990-
L,
991-
O,
992-
>,
942+
sweeper:
943+
OutputSweeper<B, ChangeDestinationSourceSyncWrapper<D>, E, F, KVStoreSyncWrapper<K>, L, O>,
993944
}
994945

995946
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
@@ -1009,7 +960,7 @@ where
1009960
output_spender: O, change_destination_source: D, kv_store: K, logger: L,
1010961
) -> Self {
1011962
let change_destination_source =
1012-
Arc::new(ChangeDestinationSourceSyncWrapper::new(change_destination_source));
963+
ChangeDestinationSourceSyncWrapper::new(change_destination_source);
1013964

1014965
let kv_store = KVStoreSyncWrapper(kv_store);
1015966

@@ -1068,19 +1019,17 @@ where
10681019
self.sweeper.tracked_spendable_outputs()
10691020
}
10701021

1071-
/// Returns the inner async sweeper for testing purposes.
1072-
#[cfg(any(test, feature = "_test_utils"))]
1022+
/// Fetch the inner async sweeper.
1023+
///
1024+
/// In general you shouldn't have much reason to use this - you have a sync [`KVStore`] backing
1025+
/// this [`OutputSweeperSync`], fetching an async [`OutputSweeper`] won't accomplish much, all
1026+
/// the async methods will hang waiting on your sync [`KVStore`] and likely confuse your async
1027+
/// runtime. This exists primarily for LDK-internal use, including outside of this crate.
1028+
#[doc(hidden)]
10731029
pub fn sweeper_async(
10741030
&self,
1075-
) -> &OutputSweeper<
1076-
B,
1077-
Arc<ChangeDestinationSourceSyncWrapper<D>>,
1078-
E,
1079-
F,
1080-
KVStoreSyncWrapper<K>,
1081-
L,
1082-
O,
1083-
> {
1031+
) -> &OutputSweeper<B, ChangeDestinationSourceSyncWrapper<D>, E, F, KVStoreSyncWrapper<K>, L, O>
1032+
{
10841033
&self.sweeper
10851034
}
10861035
}

0 commit comments

Comments
 (0)