Lift std check to function definition
[rust-lightning] / lightning-rapid-gossip-sync / src / lib.rs
index 278cede397411720415b89d20a6c9ccaecb1160d..c8f140cc18955d8225bbf300fcda2aee6ddae267 100644 (file)
@@ -1,30 +1,44 @@
+// Prefix these with `rustdoc::` when we update our MSRV to be >= 1.52 to remove warnings.
+#![deny(broken_intra_doc_links)]
+#![deny(private_intra_doc_links)]
+
 #![deny(missing_docs)]
 #![deny(unsafe_code)]
-#![deny(broken_intra_doc_links)]
 #![deny(non_upper_case_globals)]
 #![deny(non_camel_case_types)]
 #![deny(non_snake_case)]
 #![deny(unused_mut)]
 #![deny(unused_variables)]
 #![deny(unused_imports)]
-//! This crate exposes functionality to rapidly sync gossip data, aimed primarily at mobile
+//! This crate exposes client functionality to rapidly sync gossip data, aimed primarily at mobile
 //! devices.
 //!
-//! The server sends a compressed response containing differential gossip data. The gossip data is
-//! formatted compactly, omitting signatures and opportunistically incremental where previous
-//! channel updates are known (a mechanism that is enabled when the timestamp of the last known
-//! channel update is communicated). A reference server implementation can be found
-//! [here](https://github.com/lightningdevkit/rapid-gossip-sync-server).
+//! The rapid gossip sync server will provide a compressed response containing differential gossip
+//! data. The gossip data is formatted compactly, omitting signatures and opportunistically
+//! incremental where previous channel updates are known. This mechanism is enabled when the
+//! timestamp of the last known channel update is communicated. A reference server implementation
+//! can be found [on Github](https://github.com/lightningdevkit/rapid-gossip-sync-server).
+//!
+//! The primary benefit of this syncing mechanism is that it allows a low-powered client to offload
+//! the validation of gossip signatures to a semi-trusted server. This enables the client to
+//! privately calculate routes for payments, and to do so much faster than requiring a full
+//! peer-to-peer gossip sync to complete.
+//!
+//! The server calculates its response on the basis of a client-provided `latest_seen` timestamp,
+//! i.e., the server will return all rapid gossip sync data it has seen after the given timestamp.
 //!
-//! An example server request could look as simple as the following. Note that the first ever rapid
-//! sync should use `0` for `last_sync_timestamp`:
+//! # Getting Started
+//! Firstly, the data needs to be retrieved from the server. For example, you could use the server
+//! at <https://rapidsync.lightningdevkit.org> with the following request format:
 //!
 //! ```shell
 //! curl -o rapid_sync.lngossip https://rapidsync.lightningdevkit.org/snapshot/<last_sync_timestamp>
 //! ```
+//! Note that the first ever rapid sync should use `0` for `last_sync_timestamp`.
 //!
-//! Then, call the network processing function. In this example, we process the update by reading
-//! its contents from disk, which we do by calling the `sync_network_graph_with_file_path` method:
+//! After the gossip data snapshot has been downloaded, one of the client's graph processing
+//! functions needs to be called. In this example, we process the update by reading its contents
+//! from disk, which we do by calling [`RapidGossipSync::update_network_graph`]:
 //!
 //! ```
 //! use bitcoin::blockdata::constants::genesis_block;
 //! use lightning::routing::gossip::NetworkGraph;
 //! use lightning_rapid_gossip_sync::RapidGossipSync;
 //!
-//! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
-//! let network_graph = NetworkGraph::new(block_hash);
-//! let rapid_sync = RapidGossipSync::new(&network_graph);
-//! let new_last_sync_timestamp_result = rapid_sync.sync_network_graph_with_file_path("./rapid_sync.lngossip");
-//! ```
-//!
-//! The primary benefit this syncing mechanism provides is that given a trusted server, a
-//! low-powered client can offload the validation of gossip signatures. This enables a client to
-//! privately calculate routes for payments, and do so much faster and earlier than requiring a full
-//! peer-to-peer gossip sync to complete.
+//! # use lightning::util::logger::{Logger, Record};
+//! # struct FakeLogger {}
+//! # impl Logger for FakeLogger {
+//! #     fn log(&self, record: &Record) { }
+//! # }
+//! # let logger = FakeLogger {};
 //!
-//! The reason the rapid sync server requires trust is that it could provide bogus data, though at
-//! worst, all that would result in is a fake network topology, which wouldn't enable the server to
-//! steal or siphon off funds. It could, however, reduce the client's privacy by forcing all
-//! payments to be routed via channels the server controls.
-//!
-//! The way a server is meant to calculate this rapid gossip sync data is by using a `latest_seen`
-//! timestamp provided by the client. It's not included in either channel announcement or update,
-//! (not least due to announcements not including any timestamps at all, but only a block height)
-//! but rather, it's a timestamp of when the server saw a particular message.
+//! let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
+//! let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
+//! let snapshot_contents: &[u8] = &[0; 0];
+//! // In no-std you need to provide the current time in unix epoch seconds
+//! // otherwise you can use update_network_graph
+//! let current_time_unix = 0;
+//! let new_last_sync_timestamp_result = rapid_sync.update_network_graph_no_std(snapshot_contents, Some(current_time_unix));
+//! ```
+
+#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
 
 // Allow and import test features for benching
 #![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
 #[cfg(all(test, feature = "_bench_unstable"))]
 extern crate test;
 
+#[cfg(not(feature = "std"))]
+extern crate alloc;
+
+#[cfg(feature = "std")]
 use std::fs::File;
-use std::ops::Deref;
-use std::sync::atomic::{AtomicBool, Ordering};
+use core::ops::Deref;
+use core::sync::atomic::{AtomicBool, Ordering};
 
+use lightning::io;
 use lightning::routing::gossip::NetworkGraph;
+use lightning::util::logger::Logger;
 
-use crate::error::GraphSyncError;
+pub use crate::error::GraphSyncError;
 
 /// Error types that these functions can return
-pub mod error;
+mod error;
 
 /// Core functionality of this crate
-pub mod processing;
+mod processing;
 
-/// Rapid Gossip Sync struct
+/// The main Rapid Gossip Sync object.
+///
 /// See [crate-level documentation] for usage.
 ///
 /// [crate-level documentation]: crate
-pub struct RapidGossipSync<NG: Deref<Target=NetworkGraph>> {
+pub struct RapidGossipSync<NG: Deref<Target=NetworkGraph<L>>, L: Deref>
+where L::Target: Logger {
        network_graph: NG,
+       logger: L,
        is_initial_sync_complete: AtomicBool
 }
 
-impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
-       /// Instantiate a new [`RapidGossipSync`] instance
-       pub fn new(network_graph: NG) -> Self {
+impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L::Target: Logger {
+       /// Instantiate a new [`RapidGossipSync`] instance.
+       pub fn new(network_graph: NG, logger: L) -> Self {
                Self {
                        network_graph,
+                       logger,
                        is_initial_sync_complete: AtomicBool::new(false)
                }
        }
 
-       /// Sync gossip data from a file
+       /// Sync gossip data from a file.
        /// Returns the last sync timestamp to be used the next time rapid sync data is queried.
        ///
        /// `network_graph`: The network graph to apply the updates to
        ///
        /// `sync_path`: Path to the file where the gossip update data is located
        ///
+       #[cfg(feature = "std")]
        pub fn sync_network_graph_with_file_path(
                &self,
                sync_path: &str,
@@ -105,29 +127,50 @@ impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
                self.update_network_graph_from_byte_stream(&mut file)
        }
 
+       /// Update network graph from binary data.
+       /// Returns the last sync timestamp to be used the next time rapid sync data is queried.
+       ///
+       /// `update_data`: `&[u8]` binary stream that comprises the update data
+       #[cfg(feature = "std")]
+       pub fn update_network_graph(&self, update_data: &[u8]) -> Result<u32, GraphSyncError> {
+               let mut read_cursor = io::Cursor::new(update_data);
+               self.update_network_graph_from_byte_stream(&mut read_cursor)
+       }
+
+       /// Update network graph from binary data.
+       /// Returns the last sync timestamp to be used the next time rapid sync data is queried.
+       ///
+       /// `update_data`: `&[u8]` binary stream that comprises the update data
+       /// `current_time_unix`: `Option<u64>` optional current timestamp to verify data age
+       pub fn update_network_graph_no_std(&self, update_data: &[u8], current_time_unix: Option<u64>) -> Result<u32, GraphSyncError> {
+               let mut read_cursor = io::Cursor::new(update_data);
+               self.update_network_graph_from_byte_stream_no_std(&mut read_cursor, current_time_unix)
+       }
+
        /// Gets a reference to the underlying [`NetworkGraph`] which was provided in
        /// [`RapidGossipSync::new`].
        ///
-       /// (C-not exported) as bindings don't support a reference-to-a-reference yet
+       /// This is not exported to bindings users as bindings don't support a reference-to-a-reference yet
        pub fn network_graph(&self) -> &NG {
                &self.network_graph
        }
 
-       /// Returns whether a rapid gossip sync has completed at least once
+       /// Returns whether a rapid gossip sync has completed at least once.
        pub fn is_initial_sync_complete(&self) -> bool {
                self.is_initial_sync_complete.load(Ordering::Acquire)
        }
 }
 
+#[cfg(feature = "std")]
 #[cfg(test)]
 mod tests {
        use std::fs;
 
-       use bitcoin::blockdata::constants::genesis_block;
        use bitcoin::Network;
 
        use lightning::ln::msgs::DecodeError;
        use lightning::routing::gossip::NetworkGraph;
+       use lightning::util::test_utils::TestLogger;
        use crate::RapidGossipSync;
 
        #[test]
@@ -144,18 +187,17 @@ mod tests {
                                fs::create_dir_all(graph_sync_test_directory).unwrap();
 
                                let graph_sync_test_file = test.get_test_file_path();
-                               fs::write(&graph_sync_test_file, valid_response).unwrap();
+                               fs::write(graph_sync_test_file, valid_response).unwrap();
 
                                test
                        }
+
                        fn get_test_directory(&self) -> String {
-                               let graph_sync_test_directory = self.directory.clone() + "/graph-sync-tests";
-                               graph_sync_test_directory
+                               self.directory.clone() + "/graph-sync-tests"
                        }
+
                        fn get_test_file_path(&self) -> String {
-                               let graph_sync_test_directory = self.get_test_directory();
-                               let graph_sync_test_file = graph_sync_test_directory.to_owned() + "/test_data.lngossip";
-                               graph_sync_test_file
+                               self.get_test_directory() + "/test_data.lngossip"
                        }
                }
 
@@ -186,12 +228,12 @@ mod tests {
                let sync_test = FileSyncTest::new(tmp_directory, &valid_response);
                let graph_sync_test_file = sync_test.get_test_file_path();
 
-               let block_hash = genesis_block(Network::Bitcoin).block_hash();
-               let network_graph = NetworkGraph::new(block_hash);
+               let logger = TestLogger::new();
+               let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
 
                assert_eq!(network_graph.read_only().channels().len(), 0);
 
-               let rapid_sync = RapidGossipSync::new(&network_graph);
+               let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
                let sync_result = rapid_sync.sync_network_graph_with_file_path(&graph_sync_test_file);
 
                if sync_result.is_err() {
@@ -218,17 +260,17 @@ mod tests {
 
        #[test]
        fn measure_native_read_from_file() {
-               let block_hash = genesis_block(Network::Bitcoin).block_hash();
-               let network_graph = NetworkGraph::new(block_hash);
+               let logger = TestLogger::new();
+               let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
 
                assert_eq!(network_graph.read_only().channels().len(), 0);
 
-               let rapid_sync = RapidGossipSync::new(&network_graph);
+               let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
                let start = std::time::Instant::now();
                let sync_result = rapid_sync
                        .sync_network_graph_with_file_path("./res/full_graph.lngossip");
                if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
-                       let error_string = format!("Input file lightning-rapid-gossip-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-bc08df7542-2022-05-05.bin\n\n{:?}", io_error);
+                       let error_string = format!("Input file lightning-rapid-gossip-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-285cb27df79-2022-07-21.bin\n\n{:?}", io_error);
                        #[cfg(not(require_route_graph_test))]
                        {
                                println!("{}", error_string);
@@ -249,20 +291,20 @@ mod tests {
 pub mod bench {
        use test::Bencher;
 
-       use bitcoin::blockdata::constants::genesis_block;
        use bitcoin::Network;
 
        use lightning::ln::msgs::DecodeError;
        use lightning::routing::gossip::NetworkGraph;
+       use lightning::util::test_utils::TestLogger;
 
        use crate::RapidGossipSync;
 
        #[bench]
        fn bench_reading_full_graph_from_file(b: &mut Bencher) {
-               let block_hash = genesis_block(Network::Bitcoin).block_hash();
+               let logger = TestLogger::new();
                b.iter(|| {
-                       let network_graph = NetworkGraph::new(block_hash);
-                       let rapid_sync = RapidGossipSync::new(&network_graph);
+                       let network_graph = NetworkGraph::new(Network::Bitcoin, &logger);
+                       let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
                        let sync_result = rapid_sync.sync_network_graph_with_file_path("./res/full_graph.lngossip");
                        if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
                                let error_string = format!("Input file lightning-rapid-gossip-sync/res/full_graph.lngossip is missing! Download it from https://bitcoin.ninja/ldk-compressed_graph-bc08df7542-2022-05-05.bin\n\n{:?}", io_error);