]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Update to LDK 0.0.106
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 23 Mar 2022 18:01:05 +0000 (13:01 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Mon, 4 Apr 2022 19:44:06 +0000 (14:44 -0500)
Cargo.toml
src/disk.rs
src/main.rs

index 8e3fe5af8d8d1181f4cd54a99d8cf589d72d8d04..c67325393f536e2de23611a80ad93456b2a1195d 100644 (file)
@@ -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"
index af565bc5b8afac37e05c43bb2e6f2ce855800f47..8a54592201d65610965ded747216fc961505329a 100644 (file)
@@ -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)) {
index cdbb178a5f82b64fe3c60ec196176045137badef..07afa66436164c15b0b5af0e018fc8135549d917 100644 (file)
@@ -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<E> = payment::InvoicePayer<
 
 type Router = DefaultRouter<Arc<NetworkGraph>, Arc<FilesystemLogger>>;
 
+struct DataPersister {
+       data_dir: String,
+}
+
+impl
+       Persister<
+               InMemorySigner,
+               Arc<ChainMonitor>,
+               Arc<BitcoindClient>,
+               Arc<KeysManager>,
+               Arc<BitcoindClient>,
+               Arc<FilesystemLogger>,
+       > 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<ChannelManager>, bitcoind_client: Arc<BitcoindClient>,
        keys_manager: Arc<KeysManager>, inbound_payments: PaymentInfoStorage,
@@ -490,22 +520,6 @@ async fn start_ldk() {
                None::<Arc<dyn chain::Access + Send + Sync>>,
                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<ChannelManager> = 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(),