From: Jeffrey Czyz Date: Wed, 23 Mar 2022 18:01:05 +0000 (-0500) Subject: Update to LDK 0.0.106 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=8decf4bb58ab615f06e2a7e3cac6e4ef32634a62;p=ldk-sample Update to LDK 0.0.106 --- diff --git a/Cargo.toml b/Cargo.toml index 8e3fe5a..c673253 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,12 +8,12 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lightning = { version = "0.0.105", features = ["max_level_trace"] } -lightning-block-sync = { version = "0.0.105", features = [ "rpc-client" ] } -lightning-invoice = { version = "0.13" } -lightning-net-tokio = { version = "0.0.105" } -lightning-persister = { version = "0.0.105" } -lightning-background-processor = { version = "0.0.105" } +lightning = { version = "0.0.106", features = ["max_level_trace"] } +lightning-block-sync = { version = "0.0.106", features = [ "rpc-client" ] } +lightning-invoice = { version = "0.14" } +lightning-net-tokio = { version = "0.0.106" } +lightning-persister = { version = "0.0.106" } +lightning-background-processor = { version = "0.0.106" } base64 = "0.13.0" bitcoin = "0.27" diff --git a/src/disk.rs b/src/disk.rs index af565bc..8a54592 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -73,19 +73,6 @@ pub(crate) fn read_channel_peer_data( Ok(peer_data) } -pub(crate) fn persist_network(path: &Path, network_graph: &NetworkGraph) -> std::io::Result<()> { - let mut tmp_path = path.to_path_buf().into_os_string(); - tmp_path.push(".tmp"); - let file = fs::OpenOptions::new().write(true).create(true).open(&tmp_path)?; - let write_res = network_graph.write(&mut BufWriter::new(file)); - if let Err(e) = write_res.and_then(|_| fs::rename(&tmp_path, path)) { - let _ = fs::remove_file(&tmp_path); - Err(e) - } else { - Ok(()) - } -} - pub(crate) fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph { if let Ok(file) = File::open(path) { if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file)) { diff --git a/src/main.rs b/src/main.rs index cdbb178..07afa66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ use lightning::routing::scoring::ProbabilisticScorer; use lightning::util::config::UserConfig; use lightning::util::events::{Event, PaymentPurpose}; use lightning::util::ser::ReadableArgs; -use lightning_background_processor::BackgroundProcessor; +use lightning_background_processor::{BackgroundProcessor, Persister}; use lightning_block_sync::init; use lightning_block_sync::poll; use lightning_block_sync::SpvClient; @@ -108,6 +108,36 @@ pub(crate) type InvoicePayer = payment::InvoicePayer< type Router = DefaultRouter, Arc>; +struct DataPersister { + data_dir: String, +} + +impl + Persister< + InMemorySigner, + Arc, + Arc, + Arc, + Arc, + Arc, + > for DataPersister +{ + fn persist_manager(&self, channel_manager: &ChannelManager) -> Result<(), std::io::Error> { + FilesystemPersister::persist_manager(self.data_dir.clone(), channel_manager) + } + + fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), std::io::Error> { + if FilesystemPersister::persist_network_graph(self.data_dir.clone(), network_graph).is_err() + { + // Persistence errors here are non-fatal as we can just fetch the routing graph + // again later, but they may indicate a disk error which could be fatal elsewhere. + eprintln!("Warning: Failed to persist network graph, check your disk and permissions"); + } + + Ok(()) + } +} + async fn handle_ldk_events( channel_manager: Arc, bitcoind_client: Arc, keys_manager: Arc, inbound_payments: PaymentInfoStorage, @@ -490,22 +520,6 @@ async fn start_ldk() { None::>, logger.clone(), )); - let network_graph_persist = Arc::clone(&network_graph); - tokio::spawn(async move { - let mut interval = tokio::time::interval(Duration::from_secs(600)); - loop { - interval.tick().await; - if disk::persist_network(Path::new(&network_graph_path), &network_graph_persist) - .is_err() - { - // Persistence errors here are non-fatal as we can just fetch the routing graph - // again later, but they may indicate a disk error which could be fatal elsewhere. - eprintln!( - "Warning: Failed to persist network graph, check your disk and permissions" - ); - } - } - }); // Step 12: Initialize the PeerManager let channel_manager: Arc = Arc::new(channel_manager); @@ -611,7 +625,11 @@ async fn start_ldk() { }); // Step 17: Create InvoicePayer - let router = DefaultRouter::new(network_graph.clone(), logger.clone()); + let router = DefaultRouter::new( + network_graph.clone(), + logger.clone(), + keys_manager.get_secure_random_bytes(), + ); let invoice_payer = Arc::new(InvoicePayer::new( channel_manager.clone(), router, @@ -621,14 +639,12 @@ async fn start_ldk() { payment::RetryAttempts(5), )); - // Step 18: Persist ChannelManager - let data_dir = ldk_data_dir.clone(); - let persist_channel_manager_callback = - move |node: &ChannelManager| FilesystemPersister::persist_manager(data_dir.clone(), &*node); + // Step 18: Persist ChannelManager and NetworkGraph + let persister = DataPersister { data_dir: ldk_data_dir.clone() }; // Step 19: Background Processing let background_processor = BackgroundProcessor::start( - persist_channel_manager_callback, + persister, invoice_payer.clone(), chain_monitor.clone(), channel_manager.clone(),