1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! This crate exposes functionality to rapidly sync gossip data, aimed primarily at mobile
12 //! The server sends a compressed response containing differential gossip data. The gossip data is
13 //! formatted compactly, omitting signatures and opportunistically incremental where previous
14 //! channel updates are known (a mechanism that is enabled when the timestamp of the last known
15 //! channel update is communicated). A reference server implementation can be found
16 //! [here](https://github.com/lightningdevkit/rapid-gossip-sync-server).
18 //! An example server request could look as simple as the following. Note that the first ever rapid
19 //! sync should use `0` for `last_sync_timestamp`:
22 //! curl -o rapid_sync.lngossip https://rapidsync.lightningdevkit.org/snapshot/<last_sync_timestamp>
25 //! Then, call the network processing function. In this example, we process the update by reading
26 //! its contents from disk, which we do by calling the `sync_network_graph_with_file_path` method:
29 //! use bitcoin::blockdata::constants::genesis_block;
30 //! use bitcoin::Network;
31 //! use lightning::routing::gossip::NetworkGraph;
32 //! use lightning_rapid_gossip_sync::RapidGossipSync;
34 //! # use lightning::util::logger::{Logger, Record};
35 //! # struct FakeLogger {}
36 //! # impl Logger for FakeLogger {
37 //! # fn log(&self, record: &Record) { unimplemented!() }
39 //! # let logger = FakeLogger {};
41 //! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
42 //! let network_graph = NetworkGraph::new(block_hash, &logger);
43 //! let rapid_sync = RapidGossipSync::new(&network_graph);
44 //! let new_last_sync_timestamp_result = rapid_sync.sync_network_graph_with_file_path(\"./rapid_sync.lngossip\");
47 //! The primary benefit this syncing mechanism provides is that given a trusted server, a
48 //! low-powered client can offload the validation of gossip signatures. This enables a client to
49 //! privately calculate routes for payments, and do so much faster and earlier than requiring a full
50 //! peer-to-peer gossip sync to complete.
52 //! The reason the rapid sync server requires trust is that it could provide bogus data, though at
53 //! worst, all that would result in is a fake network topology, which wouldn't enable the server to
54 //! steal or siphon off funds. It could, however, reduce the client's privacy by forcing all
55 //! payments to be routed via channels the server controls.
57 //! The way a server is meant to calculate this rapid gossip sync data is by using a `latest_seen`
58 //! timestamp provided by the client. It's not included in either channel announcement or update,
59 //! (not least due to announcements not including any timestamps at all, but only a block height)
60 //! but rather, it's a timestamp of when the server saw a particular message.
62 use alloc::str::FromStr;
63 use core::ffi::c_void;
64 use core::convert::Infallible;
65 use bitcoin::hashes::Hash;
66 use crate::c_types::*;
67 #[cfg(feature="no-std")]
68 use alloc::{vec::Vec, boxed::Box};
73 use lightning_rapid_gossip_sync::RapidGossipSync as nativeRapidGossipSyncImport;
74 pub(crate) type nativeRapidGossipSync = nativeRapidGossipSyncImport<&'static lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>, crate::lightning::util::logger::Logger>;
76 /// Rapid Gossip Sync struct
77 /// See [crate-level documentation] for usage.
79 /// [crate-level documentation]: crate
82 pub struct RapidGossipSync {
83 /// A pointer to the opaque Rust object.
85 /// Nearly everywhere, inner must be non-null, however in places where
86 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
87 pub inner: *mut nativeRapidGossipSync,
88 /// Indicates that this is the only struct which contains the same pointer.
90 /// Rust functions which take ownership of an object provided via an argument require
91 /// this to be true and invalidate the object pointed to by inner.
95 impl Drop for RapidGossipSync {
97 if self.is_owned && !<*mut nativeRapidGossipSync>::is_null(self.inner) {
98 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
102 /// Frees any resources used by the RapidGossipSync, if is_owned is set and inner is non-NULL.
104 pub extern "C" fn RapidGossipSync_free(this_obj: RapidGossipSync) { }
106 /// Used only if an object of this type is returned as a trait impl by a method
107 pub(crate) extern "C" fn RapidGossipSync_free_void(this_ptr: *mut c_void) {
108 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRapidGossipSync); }
111 impl RapidGossipSync {
112 pub(crate) fn get_native_ref(&self) -> &'static nativeRapidGossipSync {
113 unsafe { &*ObjOps::untweak_ptr(self.inner) }
115 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRapidGossipSync {
116 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
118 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
119 pub(crate) fn take_inner(mut self) -> *mut nativeRapidGossipSync {
120 assert!(self.is_owned);
121 let ret = ObjOps::untweak_ptr(self.inner);
122 self.inner = core::ptr::null_mut();
126 /// Instantiate a new [`RapidGossipSync`] instance
129 pub extern "C" fn RapidGossipSync_new(network_graph: &crate::lightning::routing::gossip::NetworkGraph) -> crate::lightning_rapid_gossip_sync::RapidGossipSync {
130 let mut ret = lightning_rapid_gossip_sync::RapidGossipSync::new(network_graph.get_native_ref());
131 crate::lightning_rapid_gossip_sync::RapidGossipSync { inner: ObjOps::heap_alloc(ret), is_owned: true }
134 /// Sync gossip data from a file
135 /// Returns the last sync timestamp to be used the next time rapid sync data is queried.
137 /// `network_graph`: The network graph to apply the updates to
139 /// `sync_path`: Path to the file where the gossip update data is located
143 pub extern "C" fn RapidGossipSync_sync_network_graph_with_file_path(this_arg: &crate::lightning_rapid_gossip_sync::RapidGossipSync, mut sync_path: crate::c_types::Str) -> crate::c_types::derived::CResult_u32GraphSyncErrorZ {
144 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.sync_network_graph_with_file_path(sync_path.into_str());
145 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_rapid_gossip_sync::error::GraphSyncError::native_into(e) }).into() };
149 /// Returns whether a rapid gossip sync has completed at least once
152 pub extern "C" fn RapidGossipSync_is_initial_sync_complete(this_arg: &crate::lightning_rapid_gossip_sync::RapidGossipSync) -> bool {
153 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_initial_sync_complete();