Skip to content

Commit aa4c6f6

Browse files
committed
Make background-processor no-std-friendly (ish)
This makes `background-processor` build without `std` at all. This isn't particularly useful in the general no-std case as `background-processor` is only useful with the `futures` feature, and async will generally need `std` in some way or another. Still, it ensures we don't end up reintroducing a dependency on the current time, which breaks `wasm` use-cases.
1 parent e59b384 commit aa4c6f6

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

lightning-background-processor/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ rustdoc-args = ["--cfg", "docsrs"]
1515

1616
[features]
1717
futures = [ "futures-util" ]
18+
std = ["lightning/std", "lightning-rapid-gossip-sync/std"]
19+
20+
default = ["std"]
1821

1922
[dependencies]
20-
bitcoin = "0.29.0"
21-
lightning = { version = "0.0.113", path = "../lightning", features = ["std"] }
22-
lightning-rapid-gossip-sync = { version = "0.0.113", path = "../lightning-rapid-gossip-sync" }
23+
bitcoin = { version = "0.29.0", default-features = false }
24+
lightning = { version = "0.0.113", path = "../lightning", default-features = false }
25+
lightning-rapid-gossip-sync = { version = "0.0.113", path = "../lightning-rapid-gossip-sync", default-features = false }
2326
futures-util = { version = "0.3", default-features = false, features = ["async-await-macro"], optional = true }
2427

2528
[dev-dependencies]

lightning-background-processor/src/lib.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1313

14+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
15+
16+
#[cfg(any(test, feature = "std"))]
17+
extern crate core;
18+
1419
#[macro_use] extern crate lightning;
1520
extern crate lightning_rapid_gossip_sync;
1621

@@ -28,12 +33,19 @@ use lightning::util::events::{Event, EventHandler, EventsProvider};
2833
use lightning::util::logger::Logger;
2934
use lightning::util::persist::Persister;
3035
use lightning_rapid_gossip_sync::RapidGossipSync;
36+
use lightning::io;
37+
38+
use core::ops::Deref;
39+
use core::time::Duration;
40+
41+
#[cfg(feature = "std")]
3142
use std::sync::Arc;
32-
use std::sync::atomic::{AtomicBool, Ordering};
33-
use std::thread;
34-
use std::thread::JoinHandle;
35-
use std::time::{Duration, Instant};
36-
use std::ops::Deref;
43+
#[cfg(feature = "std")]
44+
use core::sync::atomic::{AtomicBool, Ordering};
45+
#[cfg(feature = "std")]
46+
use std::thread::{self, JoinHandle};
47+
#[cfg(feature = "std")]
48+
use std::time::Instant;
3749

3850
#[cfg(feature = "futures")]
3951
use futures_util::{select_biased, future::FutureExt, task};
@@ -62,6 +74,7 @@ use futures_util::{select_biased, future::FutureExt, task};
6274
///
6375
/// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
6476
/// [`Event`]: lightning::util::events::Event
77+
#[cfg(feature = "std")]
6578
#[must_use = "BackgroundProcessor will immediately stop on drop. It should be stored until shutdown."]
6679
pub struct BackgroundProcessor {
6780
stop_thread: Arc<AtomicBool>,
@@ -285,8 +298,14 @@ macro_rules! define_run_body {
285298
if $timer_elapsed(&mut last_prune_call, if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER }) {
286299
// The network graph must not be pruned while rapid sync completion is pending
287300
if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
288-
log_trace!($logger, "Pruning and persisting network graph.");
289-
network_graph.remove_stale_channels_and_tracking();
301+
#[cfg(feature = "std")] {
302+
log_trace!($logger, "Pruning and persisting network graph.");
303+
network_graph.remove_stale_channels_and_tracking();
304+
}
305+
#[cfg(not(feature = "std"))] {
306+
log_warn!($logger, "Not pruning network graph, consider enabling `std` or doing so manually with remove_stale_channels_and_tracking_with_time.");
307+
log_trace!($logger, "Persisting network graph.");
308+
}
290309

291310
if let Err(e) = $persister.persist_graph(network_graph) {
292311
log_error!($logger, "Error: Failed to persist network graph, check your disk and permissions {}", e)
@@ -334,6 +353,11 @@ macro_rules! define_run_body {
334353
/// future which outputs true, the loop will exit and this function's future will complete.
335354
///
336355
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
356+
///
357+
/// Requires the `futures` feature. Note that while this method is available without the `std`
358+
/// feature, doing so will skip calling [`NetworkGraph::remove_stale_channels_and_tracking`],
359+
/// you should call [`NetworkGraph::remove_stale_channels_and_tracking_with_time`] regularly
360+
/// manually instead.
337361
#[cfg(feature = "futures")]
338362
pub async fn process_events_async<
339363
'a,
@@ -370,7 +394,7 @@ pub async fn process_events_async<
370394
persister: PS, event_handler: EventHandler, chain_monitor: M, channel_manager: CM,
371395
gossip_sync: GossipSync<PGS, RGS, G, CA, L>, peer_manager: PM, logger: L, scorer: Option<S>,
372396
sleeper: Sleeper,
373-
) -> Result<(), std::io::Error>
397+
) -> Result<(), io::Error>
374398
where
375399
CA::Target: 'static + chain::Access,
376400
CF::Target: 'static + chain::Filter,
@@ -419,6 +443,7 @@ where
419443
})
420444
}
421445

446+
#[cfg(feature = "std")]
422447
impl BackgroundProcessor {
423448
/// Start a background thread that takes care of responsibilities enumerated in the [top-level
424449
/// documentation].
@@ -574,13 +599,14 @@ impl BackgroundProcessor {
574599
}
575600
}
576601

602+
#[cfg(feature = "std")]
577603
impl Drop for BackgroundProcessor {
578604
fn drop(&mut self) {
579605
self.stop_and_join_thread().unwrap();
580606
}
581607
}
582608

583-
#[cfg(test)]
609+
#[cfg(all(feature = "std", test))]
584610
mod tests {
585611
use bitcoin::blockdata::block::BlockHeader;
586612
use bitcoin::blockdata::constants::genesis_block;

no-std-check/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ default = ["lightning/no-std", "lightning-invoice/no-std", "lightning-rapid-goss
1010
lightning = { path = "../lightning", default-features = false }
1111
lightning-invoice = { path = "../lightning-invoice", default-features = false }
1212
lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync", default-features = false }
13+
lightning-background-processor = { path = "../lightning-background-processor", features = ["futures"], default-features = false }

0 commit comments

Comments
 (0)