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 client functionality to rapidly sync gossip data, aimed primarily at mobile
12 //! The rapid gossip sync server will provide a compressed response containing differential gossip
13 //! data. The gossip data is formatted compactly, omitting signatures and opportunistically
14 //! incremental where previous channel updates are known. This mechanism is enabled when the
15 //! timestamp of the last known channel update is communicated. A reference server implementation
16 //! can be found [on Github](https://github.com/lightningdevkit/rapid-gossip-sync-server).
18 //! The primary benefit of this syncing mechanism is that it allows a low-powered client to offload
19 //! the validation of gossip signatures to a semi-trusted server. This enables the client to
20 //! privately calculate routes for payments, and to do so much faster than requiring a full
21 //! peer-to-peer gossip sync to complete.
23 //! The server calculates its response on the basis of a client-provided `latest_seen` timestamp,
24 //! i.e., the server will return all rapid gossip sync data it has seen after the given timestamp.
27 //! Firstly, the data needs to be retrieved from the server. For example, you could use the server
28 //! at <https://rapidsync.lightningdevkit.org> with the following request format:
31 //! curl -o rapid_sync.lngossip https://rapidsync.lightningdevkit.org/snapshot/<last_sync_timestamp>
33 //! Note that the first ever rapid sync should use `0` for `last_sync_timestamp`.
35 //! After the gossip data snapshot has been downloaded, one of the client's graph processing
36 //! functions needs to be called. In this example, we process the update by reading its contents
37 //! from disk, which we do by calling [`RapidGossipSync::update_network_graph`]:
40 //! use bitcoin::blockdata::constants::genesis_block;
41 //! use bitcoin::Network;
42 //! use lightning::routing::gossip::NetworkGraph;
43 //! use lightning_rapid_gossip_sync::RapidGossipSync;
45 //! # use lightning::util::logger::{Logger, Record};
46 //! # struct FakeLogger {}
47 //! # impl Logger for FakeLogger {
48 //! # fn log(&self, record: &Record) { unimplemented!() }
50 //! # let logger = FakeLogger {};
52 //! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
53 //! let network_graph = NetworkGraph::new(block_hash, &logger);
54 //! let rapid_sync = RapidGossipSync::new(&network_graph);
55 //! let snapshot_contents: &[u8] = &[0; 0];
56 //! let new_last_sync_timestamp_result = rapid_sync.update_network_graph(snapshot_contents);
59 use alloc::str::FromStr;
60 use core::ffi::c_void;
61 use core::convert::Infallible;
62 use bitcoin::hashes::Hash;
63 use crate::c_types::*;
64 #[cfg(feature="no-std")]
65 use alloc::{vec::Vec, boxed::Box};
70 use alloc::str::FromStr;
71 use core::ffi::c_void;
72 use core::convert::Infallible;
73 use bitcoin::hashes::Hash;
74 use crate::c_types::*;
75 #[cfg(feature="no-std")]
76 use alloc::{vec::Vec, boxed::Box};
80 use lightning_rapid_gossip_sync::RapidGossipSync as nativeRapidGossipSyncImport;
81 pub(crate) type nativeRapidGossipSync = nativeRapidGossipSyncImport<&'static lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>, crate::lightning::util::logger::Logger>;
83 /// The main Rapid Gossip Sync object.
85 /// See [crate-level documentation] for usage.
87 /// [crate-level documentation]: crate
90 pub struct RapidGossipSync {
91 /// A pointer to the opaque Rust object.
93 /// Nearly everywhere, inner must be non-null, however in places where
94 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
95 pub inner: *mut nativeRapidGossipSync,
96 /// Indicates that this is the only struct which contains the same pointer.
98 /// Rust functions which take ownership of an object provided via an argument require
99 /// this to be true and invalidate the object pointed to by inner.
103 impl Drop for RapidGossipSync {
105 if self.is_owned && !<*mut nativeRapidGossipSync>::is_null(self.inner) {
106 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
110 /// Frees any resources used by the RapidGossipSync, if is_owned is set and inner is non-NULL.
112 pub extern "C" fn RapidGossipSync_free(this_obj: RapidGossipSync) { }
114 /// Used only if an object of this type is returned as a trait impl by a method
115 pub(crate) extern "C" fn RapidGossipSync_free_void(this_ptr: *mut c_void) {
116 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRapidGossipSync); }
119 impl RapidGossipSync {
120 pub(crate) fn get_native_ref(&self) -> &'static nativeRapidGossipSync {
121 unsafe { &*ObjOps::untweak_ptr(self.inner) }
123 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRapidGossipSync {
124 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
126 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
127 pub(crate) fn take_inner(mut self) -> *mut nativeRapidGossipSync {
128 assert!(self.is_owned);
129 let ret = ObjOps::untweak_ptr(self.inner);
130 self.inner = core::ptr::null_mut();
134 /// Instantiate a new [`RapidGossipSync`] instance.
137 pub extern "C" fn RapidGossipSync_new(network_graph: &crate::lightning::routing::gossip::NetworkGraph) -> crate::lightning_rapid_gossip_sync::RapidGossipSync {
138 let mut ret = lightning_rapid_gossip_sync::RapidGossipSync::new(network_graph.get_native_ref());
139 crate::lightning_rapid_gossip_sync::RapidGossipSync { inner: ObjOps::heap_alloc(ret), is_owned: true }
142 /// Update network graph from binary data.
143 /// Returns the last sync timestamp to be used the next time rapid sync data is queried.
145 /// `network_graph`: network graph to be updated
147 /// `update_data`: `&[u8]` binary stream that comprises the update data
150 pub extern "C" fn RapidGossipSync_update_network_graph(this_arg: &crate::lightning_rapid_gossip_sync::RapidGossipSync, mut update_data: crate::c_types::u8slice) -> crate::c_types::derived::CResult_u32GraphSyncErrorZ {
151 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.update_network_graph(update_data.to_slice());
152 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() };
156 /// Returns whether a rapid gossip sync has completed at least once.
159 pub extern "C" fn RapidGossipSync_is_initial_sync_complete(this_arg: &crate::lightning_rapid_gossip_sync::RapidGossipSync) -> bool {
160 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_initial_sync_complete();