Skip to content

Commit 90d7104

Browse files
committed
try out sync sweeper wrapper - does not compile
1 parent d6051d9 commit 90d7104

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lightning/src/sign/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use crate::sign::taproot::TaprootChannelSigner;
6868
use crate::util::atomic_counter::AtomicCounter;
6969
use core::convert::TryInto;
7070
use core::future::Future;
71+
use core::ops::Deref;
7172
use core::pin::Pin;
7273
use core::sync::atomic::{AtomicUsize, Ordering};
7374
#[cfg(taproot)]
@@ -1007,6 +1008,13 @@ pub trait ChangeDestinationSourceSync {
10071008
/// A wrapper around [`ChangeDestinationSource`] to allow for async calls.
10081009
pub struct ChangeDestinationSourceSyncWrapper<T>(T) where T: ChangeDestinationSourceSync;
10091010

1011+
impl<T: ChangeDestinationSourceSync> ChangeDestinationSourceSyncWrapper<T> {
1012+
/// Creates a new [`ChangeDestinationSourceSyncWrapper`].
1013+
pub fn new(source: T) -> Self {
1014+
Self(source)
1015+
}
1016+
}
1017+
10101018
impl<T: ChangeDestinationSourceSync> ChangeDestinationSource for ChangeDestinationSourceSyncWrapper<T> {
10111019
fn get_change_destination_script<'a>(&self) -> AsyncGetChangeDestinationScriptResult<'a, ScriptBuf> {
10121020
let script = self.0.get_change_destination_script();

lightning/src/util/sweep.rs

+56-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::io;
1515
use crate::ln::msgs::DecodeError;
1616
use crate::ln::types::ChannelId;
1717
use crate::prelude::*;
18-
use crate::sign::{ChangeDestinationSource, OutputSpender, SpendableOutputDescriptor};
18+
use crate::sign::{ChangeDestinationSource, ChangeDestinationSourceSync, ChangeDestinationSourceSyncWrapper, OutputSpender, SpendableOutputDescriptor};
1919
use crate::sync::Mutex;
2020
use crate::util::logger::Logger;
2121
use crate::util::persist::{
@@ -33,6 +33,7 @@ use bitcoin::{BlockHash, ScriptBuf, Transaction, Txid};
3333
use core::future::Future;
3434
use core::ops::Deref;
3535
use core::task;
36+
use std::sync::Arc;
3637

3738
use super::async_poll::dummy_waker;
3839

@@ -356,6 +357,60 @@ where
356357
logger: L,
357358
}
358359

360+
pub struct OutputSweeperSync<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
361+
where
362+
B::Target: BroadcasterInterface,
363+
D::Target: ChangeDestinationSource,
364+
E::Target: FeeEstimator,
365+
F::Target: Filter + Sync + Send,
366+
K::Target: KVStore,
367+
L::Target: Logger,
368+
O::Target: OutputSpender,
369+
{
370+
sweeper: OutputSweeper<B, D, E, F, K, L, O>,
371+
}
372+
373+
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
374+
OutputSweeperSync<B, D, E, F, K, L, O>
375+
where
376+
B::Target: BroadcasterInterface,
377+
D::Target: ChangeDestinationSource,
378+
E::Target: FeeEstimator,
379+
F::Target: Filter + Sync + Send,
380+
K::Target: KVStore,
381+
L::Target: Logger,
382+
O::Target: OutputSpender,
383+
{
384+
fn new<DA: ChangeDestinationSourceSync>(
385+
best_block: BestBlock, broadcaster: B, fee_estimator: E, chain_data_source: Option<F>,
386+
output_spender: O, change_destination_source: DA, kv_store: K, logger: L,
387+
) -> Self {
388+
let change_destination_source = Arc::new(ChangeDestinationSourceSyncWrapper::new(change_destination_source));
389+
390+
let sweeper = OutputSweeper::new(
391+
best_block, broadcaster, fee_estimator, chain_data_source, output_spender,
392+
change_destination_source, kv_store, logger,
393+
);
394+
Self { sweeper }
395+
}
396+
397+
/// Regenerates and broadcasts the spending transaction for any outputs that are pending
398+
pub fn regenerate_and_broadcast_spend_if_necessary(&self) -> Result<(), ()> {
399+
let mut fut = Box::pin(self.sweeper.regenerate_and_broadcast_spend_if_necessary_async());
400+
let mut waker = dummy_waker();
401+
let mut ctx = task::Context::from_waker(&mut waker);
402+
match fut.as_mut().poll(&mut ctx) {
403+
task::Poll::Ready(result) => {
404+
result
405+
},
406+
task::Poll::Pending => {
407+
// In a sync context, we can't wait for the future to complete.
408+
panic!("task not ready");
409+
},
410+
}
411+
}
412+
}
413+
359414
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
360415
OutputSweeper<B, D, E, F, K, L, O>
361416
where

0 commit comments

Comments
 (0)