Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ use lightning::onion_message::messenger::AOnionMessenger;
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
use lightning::routing::scoring::{ScoreUpdate, WriteableScore};
use lightning::routing::utxo::UtxoLookup;
use lightning::sign::ChangeDestinationSource;
#[cfg(feature = "std")]
use lightning::sign::ChangeDestinationSourceSync;
use lightning::sign::EntropySource;
use lightning::sign::OutputSpender;
use lightning::sign::{
ChangeDestinationSource, ChangeDestinationSourceSync, EntropySource, OutputSpender,
};
use lightning::util::logger::Logger;
use lightning::util::persist::{
KVStore, KVStoreSync, KVStoreSyncWrapper, CHANNEL_MANAGER_PERSISTENCE_KEY,
Expand All @@ -61,9 +59,7 @@ use lightning::util::persist::{
NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY,
SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
};
use lightning::util::sweep::OutputSweeper;
#[cfg(feature = "std")]
use lightning::util::sweep::OutputSweeperSync;
use lightning::util::sweep::{OutputSweeper, OutputSweeperSync};
#[cfg(feature = "std")]
use lightning::util::wakers::Sleeper;
use lightning_rapid_gossip_sync::RapidGossipSync;
Expand Down Expand Up @@ -1361,7 +1357,7 @@ pub async fn process_events_async_with_kv_store_sync<
D: Deref,
O: Deref,
K: Deref,
OS: Deref<Target = OutputSweeper<T, D, F, CF, KVStoreSyncWrapper<K>, L, O>>,
OS: Deref<Target = OutputSweeperSync<T, D, F, CF, K, L, O>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear to me now why I didn't do this in the first place.

S: Deref<Target = SC> + Send + Sync,
SC: for<'b> WriteableScore<'b>,
SleepFuture: core::future::Future<Output = bool> + core::marker::Unpin,
Expand All @@ -1386,7 +1382,7 @@ where
PM::Target: APeerManager,
LM::Target: ALiquidityManager,
O::Target: OutputSpender,
D::Target: ChangeDestinationSource,
D::Target: ChangeDestinationSourceSync,
K::Target: KVStoreSync,
{
let kv_store = KVStoreSyncWrapper(kv_store);
Expand All @@ -1399,7 +1395,7 @@ where
gossip_sync,
peer_manager,
liquidity_manager,
sweeper,
sweeper.as_ref().map(|os| os.sweeper_async()),
logger,
scorer,
sleeper,
Expand Down
19 changes: 14 additions & 5 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,12 +1052,11 @@ pub trait ChangeDestinationSourceSync {
}

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

Expand All @@ -1080,6 +1079,16 @@ where
}
}

impl<T: Deref> Deref for ChangeDestinationSourceSyncWrapper<T>
where
T::Target: ChangeDestinationSourceSync,
{
type Target = Self;
fn deref(&self) -> &Self {
self
}
}

mod sealed {
use bitcoin::secp256k1::{Scalar, SecretKey};

Expand Down
75 changes: 12 additions & 63 deletions lightning/src/util/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::sign::{
ChangeDestinationSource, ChangeDestinationSourceSync, ChangeDestinationSourceSyncWrapper,
OutputSpender, SpendableOutputDescriptor,
};
use crate::sync::Arc;
use crate::sync::Mutex;
use crate::util::logger::Logger;
use crate::util::persist::{
Expand Down Expand Up @@ -353,47 +352,6 @@ where
logger: L,
}

impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
OutputSweeper<B, D, E, F, KVStoreSyncWrapper<K>, L, O>
where
B::Target: BroadcasterInterface,
D::Target: ChangeDestinationSource,
E::Target: FeeEstimator,
F::Target: Filter + Send + Sync,
K::Target: KVStoreSync,
L::Target: Logger,
O::Target: OutputSpender,
{
/// Constructs a new [`OutputSweeper`] based on a [`KVStoreSync`].
pub fn new_with_kv_store_sync(
best_block: BestBlock, broadcaster: B, fee_estimator: E, chain_data_source: Option<F>,
output_spender: O, change_destination_source: D, kv_store_sync: K, logger: L,
) -> Self {
let kv_store = KVStoreSyncWrapper(kv_store_sync);

Self::new(
best_block,
broadcaster,
fee_estimator,
chain_data_source,
output_spender,
change_destination_source,
kv_store,
logger,
)
}

/// Reads an [`OutputSweeper`] from the given reader and returns it with a synchronous [`KVStoreSync`].
pub fn read_with_kv_store_sync<R: io::Read>(
reader: &mut R, args: (B, E, Option<F>, O, D, K, L),
) -> Result<Self, DecodeError> {
let kv_store = KVStoreSyncWrapper(args.5);
let args = (args.0, args.1, args.2, args.3, args.4, kv_store, args.6);

Self::read(reader, args)
}
}

impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
OutputSweeper<B, D, E, F, K, L, O>
where
Expand Down Expand Up @@ -981,15 +939,8 @@ where
L::Target: Logger,
O::Target: OutputSpender,
{
sweeper: OutputSweeper<
B,
Arc<ChangeDestinationSourceSyncWrapper<D>>,
E,
F,
KVStoreSyncWrapper<K>,
L,
O,
>,
sweeper:
OutputSweeper<B, ChangeDestinationSourceSyncWrapper<D>, E, F, KVStoreSyncWrapper<K>, L, O>,
}

impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
Expand All @@ -1009,7 +960,7 @@ where
output_spender: O, change_destination_source: D, kv_store: K, logger: L,
) -> Self {
let change_destination_source =
Arc::new(ChangeDestinationSourceSyncWrapper::new(change_destination_source));
ChangeDestinationSourceSyncWrapper::new(change_destination_source);

let kv_store = KVStoreSyncWrapper(kv_store);

Expand Down Expand Up @@ -1068,19 +1019,17 @@ where
self.sweeper.tracked_spendable_outputs()
}

/// Returns the inner async sweeper for testing purposes.
#[cfg(any(test, feature = "_test_utils"))]
/// Fetch the inner async sweeper.
///
/// In general you shouldn't have much reason to use this - you have a sync [`KVStore`] backing
/// this [`OutputSweeperSync`], fetching an async [`OutputSweeper`] won't accomplish much, all
/// the async methods will hang waiting on your sync [`KVStore`] and likely confuse your async
/// runtime. This exists primarily for LDK-internal use, including outside of this crate.
#[doc(hidden)]
pub fn sweeper_async(
&self,
) -> &OutputSweeper<
B,
Arc<ChangeDestinationSourceSyncWrapper<D>>,
E,
F,
KVStoreSyncWrapper<K>,
L,
O,
> {
) -> &OutputSweeper<B, ChangeDestinationSourceSyncWrapper<D>, E, F, KVStoreSyncWrapper<K>, L, O>
{
&self.sweeper
}
}
Expand Down