+/// This is not exported to bindings users as the bindings concretize everything and have constructors for us
+impl<'a, R: Deref<Target = RapidGossipSync<G, L>>, G: Deref<Target = NetworkGraph<L>>, L: Deref>
+ GossipSync<
+ &P2PGossipSync<G, &'a (dyn UtxoLookup + Send + Sync), L>,
+ R,
+ G,
+ &'a (dyn UtxoLookup + Send + Sync),
+ L,
+ >
+where
+ L::Target: Logger,
+{
+ /// Initializes a new [`GossipSync::Rapid`] variant.
+ pub fn rapid(gossip_sync: R) -> Self {
+ GossipSync::Rapid(gossip_sync)
+ }
+}
+
+/// This is not exported to bindings users as the bindings concretize everything and have constructors for us
+impl<'a, L: Deref>
+ GossipSync<
+ &P2PGossipSync<&'a NetworkGraph<L>, &'a (dyn UtxoLookup + Send + Sync), L>,
+ &RapidGossipSync<&'a NetworkGraph<L>, L>,
+ &'a NetworkGraph<L>,
+ &'a (dyn UtxoLookup + Send + Sync),
+ L,
+ >
+where
+ L::Target: Logger,
+{
+ /// Initializes a new [`GossipSync::None`] variant.
+ pub fn none() -> Self {
+ GossipSync::None
+ }
+}
+
+fn handle_network_graph_update<L: Deref>(
+ network_graph: &NetworkGraph<L>, event: &Event
+) where L::Target: Logger {
+ if let Event::PaymentPathFailed {
+ failure: PathFailure::OnPath { network_update: Some(ref upd) }, .. } = event
+ {
+ network_graph.handle_network_update(upd);
+ }
+}
+
+fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + WriteableScore<'a>>(
+ scorer: &'a S, event: &Event
+) {
+ let mut score = scorer.lock();
+ match event {
+ Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
+ let path = path.iter().collect::<Vec<_>>();
+ score.payment_path_failed(&path, *scid);
+ },
+ Event::PaymentPathFailed { ref path, payment_failed_permanently: true, .. } => {
+ // Reached if the destination explicitly failed it back. We treat this as a successful probe
+ // because the payment made it all the way to the destination with sufficient liquidity.
+ let path = path.iter().collect::<Vec<_>>();
+ score.probe_successful(&path);
+ },
+ Event::PaymentPathSuccessful { path, .. } => {
+ let path = path.iter().collect::<Vec<_>>();
+ score.payment_path_successful(&path);
+ },
+ Event::ProbeSuccessful { path, .. } => {
+ let path = path.iter().collect::<Vec<_>>();
+ score.probe_successful(&path);
+ },
+ Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
+ let path = path.iter().collect::<Vec<_>>();
+ score.probe_failed(&path, *scid);
+ },
+ _ => {},
+ }
+}
+
+macro_rules! define_run_body {
+ ($persister: ident, $chain_monitor: ident, $process_chain_monitor_events: expr,
+ $channel_manager: ident, $process_channel_manager_events: expr,
+ $gossip_sync: ident, $peer_manager: ident, $logger: ident, $scorer: ident,
+ $loop_exit_check: expr, $await: expr, $get_timer: expr, $timer_elapsed: expr,
+ $check_slow_await: expr)
+ => { {
+ log_trace!($logger, "Calling ChannelManager's timer_tick_occurred on startup");
+ $channel_manager.timer_tick_occurred();
+
+ let mut last_freshness_call = $get_timer(FRESHNESS_TIMER);
+ let mut last_ping_call = $get_timer(PING_TIMER);
+ let mut last_prune_call = $get_timer(FIRST_NETWORK_PRUNE_TIMER);
+ let mut last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
+ let mut have_pruned = false;
+
+ loop {
+ $process_channel_manager_events;
+ $process_chain_monitor_events;
+
+ // Note that the PeerManager::process_events may block on ChannelManager's locks,
+ // hence it comes last here. When the ChannelManager finishes whatever it's doing,
+ // we want to ensure we get into `persist_manager` as quickly as we can, especially
+ // without running the normal event processing above and handing events to users.
+ //
+ // Specifically, on an *extremely* slow machine, we may see ChannelManager start
+ // processing a message effectively at any point during this loop. In order to
+ // minimize the time between such processing completing and persisting the updated
+ // ChannelManager, we want to minimize methods blocking on a ChannelManager
+ // generally, and as a fallback place such blocking only immediately before
+ // persistence.
+ $peer_manager.process_events();
+
+ // We wait up to 100ms, but track how long it takes to detect being put to sleep,
+ // see `await_start`'s use below.
+ let mut await_start = None;
+ if $check_slow_await { await_start = Some($get_timer(1)); }
+ let updates_available = $await;
+ let await_slow = if $check_slow_await { $timer_elapsed(&mut await_start.unwrap(), 1) } else { false };
+
+ if updates_available {
+ log_trace!($logger, "Persisting ChannelManager...");
+ $persister.persist_manager(&*$channel_manager)?;
+ log_trace!($logger, "Done persisting ChannelManager.");
+ }
+ // Exit the loop if the background processor was requested to stop.
+ if $loop_exit_check {
+ log_trace!($logger, "Terminating background processor.");
+ break;
+ }
+ if $timer_elapsed(&mut last_freshness_call, FRESHNESS_TIMER) {
+ log_trace!($logger, "Calling ChannelManager's timer_tick_occurred");
+ $channel_manager.timer_tick_occurred();
+ last_freshness_call = $get_timer(FRESHNESS_TIMER);
+ }
+ if await_slow {
+ // On various platforms, we may be starved of CPU cycles for several reasons.
+ // E.g. on iOS, if we've been in the background, we will be entirely paused.
+ // Similarly, if we're on a desktop platform and the device has been asleep, we
+ // may not get any cycles.
+ // We detect this by checking if our max-100ms-sleep, above, ran longer than a
+ // full second, at which point we assume sockets may have been killed (they
+ // appear to be at least on some platforms, even if it has only been a second).
+ // Note that we have to take care to not get here just because user event
+ // processing was slow at the top of the loop. For example, the sample client
+ // may call Bitcoin Core RPCs during event handling, which very often takes
+ // more than a handful of seconds to complete, and shouldn't disconnect all our
+ // peers.
+ log_trace!($logger, "100ms sleep took more than a second, disconnecting peers.");
+ $peer_manager.disconnect_all_peers();
+ last_ping_call = $get_timer(PING_TIMER);
+ } else if $timer_elapsed(&mut last_ping_call, PING_TIMER) {
+ log_trace!($logger, "Calling PeerManager's timer_tick_occurred");
+ $peer_manager.timer_tick_occurred();
+ last_ping_call = $get_timer(PING_TIMER);
+ }
+
+ // Note that we want to run a graph prune once not long after startup before
+ // falling back to our usual hourly prunes. This avoids short-lived clients never
+ // pruning their network graph. We run once 60 seconds after startup before
+ // continuing our normal cadence.
+ if $timer_elapsed(&mut last_prune_call, if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER }) {
+ // The network graph must not be pruned while rapid sync completion is pending
+ if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
+ #[cfg(feature = "std")] {
+ log_trace!($logger, "Pruning and persisting network graph.");
+ network_graph.remove_stale_channels_and_tracking();
+ }
+ #[cfg(not(feature = "std"))] {
+ log_warn!($logger, "Not pruning network graph, consider enabling `std` or doing so manually with remove_stale_channels_and_tracking_with_time.");
+ log_trace!($logger, "Persisting network graph.");
+ }
+
+ if let Err(e) = $persister.persist_graph(network_graph) {
+ log_error!($logger, "Error: Failed to persist network graph, check your disk and permissions {}", e)
+ }
+
+ have_pruned = true;
+ }
+ last_prune_call = $get_timer(NETWORK_PRUNE_TIMER);
+ }
+
+ if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
+ if let Some(ref scorer) = $scorer {
+ log_trace!($logger, "Persisting scorer");
+ if let Err(e) = $persister.persist_scorer(&scorer) {
+ log_error!($logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
+ }
+ }
+ last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
+ }
+ }
+
+ // After we exit, ensure we persist the ChannelManager one final time - this avoids
+ // some races where users quit while channel updates were in-flight, with
+ // ChannelMonitor update(s) persisted without a corresponding ChannelManager update.
+ $persister.persist_manager(&*$channel_manager)?;
+
+ // Persist Scorer on exit
+ if let Some(ref scorer) = $scorer {
+ $persister.persist_scorer(&scorer)?;
+ }
+
+ // Persist NetworkGraph on exit
+ if let Some(network_graph) = $gossip_sync.network_graph() {
+ $persister.persist_graph(network_graph)?;
+ }
+
+ Ok(())
+ } }
+}
+
+#[cfg(feature = "futures")]
+pub(crate) mod futures_util {
+ use core::future::Future;
+ use core::task::{Poll, Waker, RawWaker, RawWakerVTable};
+ use core::pin::Pin;
+ use core::marker::Unpin;
+ pub(crate) struct Selector<
+ A: Future<Output=()> + Unpin, B: Future<Output=()> + Unpin, C: Future<Output=bool> + Unpin
+ > {
+ pub a: A,
+ pub b: B,
+ pub c: C,
+ }
+ pub(crate) enum SelectorOutput {
+ A, B, C(bool),
+ }
+
+ impl<
+ A: Future<Output=()> + Unpin, B: Future<Output=()> + Unpin, C: Future<Output=bool> + Unpin
+ > Future for Selector<A, B, C> {
+ type Output = SelectorOutput;
+ fn poll(mut self: Pin<&mut Self>, ctx: &mut core::task::Context<'_>) -> Poll<SelectorOutput> {
+ match Pin::new(&mut self.a).poll(ctx) {
+ Poll::Ready(()) => { return Poll::Ready(SelectorOutput::A); },
+ Poll::Pending => {},
+ }
+ match Pin::new(&mut self.b).poll(ctx) {
+ Poll::Ready(()) => { return Poll::Ready(SelectorOutput::B); },
+ Poll::Pending => {},
+ }
+ match Pin::new(&mut self.c).poll(ctx) {
+ Poll::Ready(res) => { return Poll::Ready(SelectorOutput::C(res)); },
+ Poll::Pending => {},
+ }
+ Poll::Pending
+ }
+ }
+
+ // If we want to poll a future without an async context to figure out if it has completed or
+ // not without awaiting, we need a Waker, which needs a vtable...we fill it with dummy values
+ // but sadly there's a good bit of boilerplate here.
+ fn dummy_waker_clone(_: *const ()) -> RawWaker { RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE) }
+ fn dummy_waker_action(_: *const ()) { }
+
+ const DUMMY_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
+ dummy_waker_clone, dummy_waker_action, dummy_waker_action, dummy_waker_action);
+ pub(crate) fn dummy_waker() -> Waker { unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE)) } }
+}
+#[cfg(feature = "futures")]
+use futures_util::{Selector, SelectorOutput, dummy_waker};
+#[cfg(feature = "futures")]
+use core::task;
+
+/// Processes background events in a future.
+///
+/// `sleeper` should return a future which completes in the given amount of time and returns a
+/// boolean indicating whether the background processing should exit. Once `sleeper` returns a
+/// future which outputs true, the loop will exit and this function's future will complete.
+///
+/// See [`BackgroundProcessor::start`] for information on which actions this handles.
+///
+/// Requires the `futures` feature. Note that while this method is available without the `std`
+/// feature, doing so will skip calling [`NetworkGraph::remove_stale_channels_and_tracking`],
+/// you should call [`NetworkGraph::remove_stale_channels_and_tracking_with_time`] regularly
+/// manually instead.
+///
+/// The `mobile_interruptable_platform` flag should be set if we're currently running on a
+/// mobile device, where we may need to check for interruption of the application regularly. If you
+/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
+/// are hundreds or thousands of simultaneous process calls running.
+#[cfg(feature = "futures")]
+pub async fn process_events_async<
+ 'a,
+ UL: 'static + Deref + Send + Sync,
+ CF: 'static + Deref + Send + Sync,
+ CW: 'static + Deref + Send + Sync,
+ T: 'static + Deref + Send + Sync,
+ ES: 'static + Deref + Send + Sync,
+ NS: 'static + Deref + Send + Sync,
+ SP: 'static + Deref + Send + Sync,
+ F: 'static + Deref + Send + Sync,
+ R: 'static + Deref + Send + Sync,
+ G: 'static + Deref<Target = NetworkGraph<L>> + Send + Sync,
+ L: 'static + Deref + Send + Sync,
+ P: 'static + Deref + Send + Sync,
+ Descriptor: 'static + SocketDescriptor + Send + Sync,
+ CMH: 'static + Deref + Send + Sync,
+ RMH: 'static + Deref + Send + Sync,
+ OMH: 'static + Deref + Send + Sync,
+ EventHandlerFuture: core::future::Future<Output = ()>,
+ EventHandler: Fn(Event) -> EventHandlerFuture,
+ PS: 'static + Deref + Send,
+ M: 'static + Deref<Target = ChainMonitor<<SP::Target as SignerProvider>::Signer, CF, T, F, L, P>> + Send + Sync,
+ CM: 'static + Deref<Target = ChannelManager<CW, T, ES, NS, SP, F, R, L>> + Send + Sync,
+ PGS: 'static + Deref<Target = P2PGossipSync<G, UL, L>> + Send + Sync,
+ RGS: 'static + Deref<Target = RapidGossipSync<G, L>> + Send,
+ UMH: 'static + Deref + Send + Sync,
+ PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, OMH, L, UMH, NS>> + Send + Sync,
+ S: 'static + Deref<Target = SC> + Send + Sync,
+ SC: for<'b> WriteableScore<'b>,
+ SleepFuture: core::future::Future<Output = bool> + core::marker::Unpin,
+ Sleeper: Fn(Duration) -> SleepFuture
+>(
+ persister: PS, event_handler: EventHandler, chain_monitor: M, channel_manager: CM,
+ gossip_sync: GossipSync<PGS, RGS, G, UL, L>, peer_manager: PM, logger: L, scorer: Option<S>,
+ sleeper: Sleeper, mobile_interruptable_platform: bool,
+) -> Result<(), lightning::io::Error>
+where
+ UL::Target: 'static + UtxoLookup,
+ CF::Target: 'static + chain::Filter,
+ CW::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,