Skip to content

Commit 0bfacff

Browse files
committed
move sync to sweeper
1 parent 118a658 commit 0bfacff

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

lightning-background-processor/src/lib.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -992,22 +992,6 @@ impl BackgroundProcessor {
992992
}
993993
event_handler.handle_event(event)
994994
};
995-
let sweeper_handler = || {
996-
let mut fut = Box::pin(sweeper.regenerate_and_broadcast_spend_if_necessary_locked());
997-
let mut waker = dummy_waker();
998-
let mut ctx = task::Context::from_waker(&mut waker);
999-
match fut.as_mut().poll(&mut ctx) {
1000-
task::Poll::Ready(result) => {
1001-
if let Err(_) = result {
1002-
log_error!(logger, "Failed to sweep output")
1003-
}
1004-
},
1005-
task::Poll::Pending => {
1006-
// In a sync context, we can't wait for the future to complete.
1007-
debug_assert!(false);
1008-
},
1009-
}
1010-
};
1011995
define_run_body!(
1012996
persister,
1013997
chain_monitor,
@@ -1020,7 +1004,7 @@ impl BackgroundProcessor {
10201004
},
10211005
peer_manager,
10221006
gossip_sync,
1023-
sweeper_handler,
1007+
sweeper.regenerate_and_broadcast_spend_if_necessary_sync(),
10241008
logger,
10251009
scorer,
10261010
stop_thread.load(Ordering::Acquire),

lightning/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
//! * `grind_signatures`
3131
3232
#![cfg_attr(not(any(test, fuzzing, feature = "_test_utils")), deny(missing_docs))]
33-
#![cfg_attr(not(any(test, feature = "_test_utils")), forbid(unsafe_code))]
3433

3534
#![deny(rustdoc::broken_intra_doc_links)]
3635
#![deny(rustdoc::private_intra_doc_links)]

lightning/src/util/async_poll.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::prelude::*;
1313
use core::future::Future;
1414
use core::marker::Unpin;
1515
use core::pin::Pin;
16-
use core::task::{Context, Poll};
16+
use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
1717

1818
pub(crate) enum ResultFuture<F: Future<Output = Result<(), E>>, E: Copy + Unpin> {
1919
Pending(F),
@@ -74,3 +74,22 @@ impl<F: Future<Output = Result<(), E>> + Unpin, E: Copy + Unpin> Future
7474
}
7575
}
7676
}
77+
78+
// If we want to poll a future without an async context to figure out if it has completed or
79+
// not without awaiting, we need a Waker, which needs a vtable...we fill it with dummy values
80+
// but sadly there's a good bit of boilerplate here.
81+
fn dummy_waker_clone(_: *const ()) -> RawWaker {
82+
RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE)
83+
}
84+
fn dummy_waker_action(_: *const ()) {}
85+
86+
const DUMMY_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
87+
dummy_waker_clone,
88+
dummy_waker_action,
89+
dummy_waker_action,
90+
dummy_waker_action,
91+
);
92+
93+
pub(crate) fn dummy_waker() -> Waker {
94+
unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE)) }
95+
}

lightning/src/util/sweep.rs

+20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ use bitcoin::locktime::absolute::LockTime;
3030
use bitcoin::secp256k1::Secp256k1;
3131
use bitcoin::{BlockHash, ScriptBuf, Transaction, Txid};
3232

33+
use core::future::Future;
3334
use core::ops::Deref;
35+
use core::task;
36+
37+
use super::async_poll::dummy_waker;
3438

3539
/// The number of blocks we wait before we prune the tracked spendable outputs.
3640
pub const PRUNE_DELAY_BLOCKS: u32 = ARCHIVAL_DELAY_BLOCKS + ANTI_REORG_DELAY;
@@ -449,6 +453,22 @@ where
449453
self.sweeper_state.lock().unwrap().best_block
450454
}
451455

456+
/// Regenerates and broadcasts the spending transaction for any outputs that are pending
457+
pub fn regenerate_and_broadcast_spend_if_necessary_sync(&self) -> Result<(), ()> {
458+
let mut fut = Box::pin(self.regenerate_and_broadcast_spend_if_necessary_locked());
459+
let mut waker = dummy_waker();
460+
let mut ctx = task::Context::from_waker(&mut waker);
461+
match fut.as_mut().poll(&mut ctx) {
462+
task::Poll::Ready(result) => {
463+
result
464+
},
465+
task::Poll::Pending => {
466+
// In a sync context, we can't wait for the future to complete.
467+
panic!("task not ready");
468+
},
469+
}
470+
}
471+
452472
/// Regenerates and broadcasts the spending transaction for any outputs that are pending
453473
pub async fn regenerate_and_broadcast_spend_if_necessary_locked(&self) -> Result<(), ()> {
454474
// Collect spendable output descriptors.

0 commit comments

Comments
 (0)