Merge pull request #29 from arik-so/2023-04-empty-blob-generator
[rapid-gossip-sync-server] / src / lib.rs
1 #![deny(unsafe_code)]
2 #![deny(broken_intra_doc_links)]
3 #![deny(private_intra_doc_links)]
4 #![deny(non_upper_case_globals)]
5 #![deny(non_camel_case_types)]
6 #![deny(non_snake_case)]
7 #![deny(unused_mut)]
8 #![deny(unused_variables)]
9 #![deny(unused_imports)]
10
11 extern crate core;
12
13 use std::collections::{HashMap, HashSet};
14 use std::fs::File;
15 use std::io::BufReader;
16 use std::sync::Arc;
17
18 use lightning::routing::gossip::{NetworkGraph, NodeId};
19 use lightning::util::ser::{ReadableArgs, Writeable};
20 use tokio::sync::mpsc;
21 use crate::lookup::DeltaSet;
22
23 use crate::persistence::GossipPersister;
24 use crate::serialization::UpdateSerializationMechanism;
25 use crate::snapshot::Snapshotter;
26 use crate::types::TestLogger;
27
28 mod downloader;
29 mod types;
30 mod tracking;
31 mod lookup;
32 mod persistence;
33 mod serialization;
34 mod snapshot;
35 mod config;
36 mod hex_utils;
37 mod verifier;
38
39 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
40 /// sync formats arise in the future.
41 ///
42 /// The fourth byte is the protocol version in case our format gets updated.
43 const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
44
45 pub struct RapidSyncProcessor {
46         network_graph: Arc<NetworkGraph<TestLogger>>,
47 }
48
49 pub struct SerializedResponse {
50         pub data: Vec<u8>,
51         pub message_count: u32,
52         pub announcement_count: u32,
53         pub update_count: u32,
54         pub update_count_full: u32,
55         pub update_count_incremental: u32,
56 }
57
58 impl RapidSyncProcessor {
59         pub fn new() -> Self {
60                 let network = config::network();
61                 let logger = TestLogger::new();
62                 let network_graph = if let Ok(file) = File::open(&config::network_graph_cache_path()) {
63                         println!("Initializing from cached network graph…");
64                         let mut buffered_reader = BufReader::new(file);
65                         let network_graph_result = NetworkGraph::read(&mut buffered_reader, logger);
66                         if let Ok(network_graph) = network_graph_result {
67                                 println!("Initialized from cached network graph!");
68                                 network_graph
69                         } else {
70                                 println!("Initialization from cached network graph failed: {}", network_graph_result.err().unwrap());
71                                 NetworkGraph::new(network, logger)
72                         }
73                 } else {
74                         NetworkGraph::new(network, logger)
75                 };
76                 let arc_network_graph = Arc::new(network_graph);
77                 Self {
78                         network_graph: arc_network_graph,
79                 }
80         }
81
82         pub async fn start_sync(&self) {
83                 // means to indicate sync completion status within this module
84                 let (sync_completion_sender, mut sync_completion_receiver) = mpsc::channel::<()>(1);
85
86                 if config::DOWNLOAD_NEW_GOSSIP {
87                         let (mut persister, persistence_sender) = GossipPersister::new(Arc::clone(&self.network_graph));
88
89                         println!("Starting gossip download");
90                         tokio::spawn(tracking::download_gossip(persistence_sender, sync_completion_sender,
91                                 Arc::clone(&self.network_graph)));
92                         println!("Starting gossip db persistence listener");
93                         tokio::spawn(async move { persister.persist_gossip().await; });
94                 } else {
95                         sync_completion_sender.send(()).await.unwrap();
96                 }
97
98                 let sync_completion = sync_completion_receiver.recv().await;
99                 if sync_completion.is_none() {
100                         panic!("Sync failed!");
101                 }
102                 println!("Initial sync complete!");
103
104                 // start the gossip snapshotting service
105                 Snapshotter::new(Arc::clone(&self.network_graph)).snapshot_gossip().await;
106         }
107 }
108
109 /// This method generates a no-op blob that can be used as a delta where none exists.
110 ///
111 /// The primary purpose of this method is the scenario of a client retrieving and processing a
112 /// given snapshot, and then immediately retrieving the would-be next snapshot at the timestamp
113 /// indicated by the one that was just processed.
114 /// Previously, there would not be a new snapshot to be processed for that particular timestamp yet,
115 /// and the server would return a 404 error.
116 ///
117 /// In principle, this method could also be used to address another unfortunately all too common
118 /// pitfall: requesting snapshots from intermediate timestamps, i. e. those that are not multiples
119 /// of our granularity constant. Note that for that purpose, this method could be very dangerous,
120 /// because if consumed, the `timestamp` value calculated here will overwrite the timestamp that
121 /// the client previously had, which could result in duplicated or omitted gossip down the line.
122 fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
123         let mut blob = GOSSIP_PREFIX.to_vec();
124
125         let network = config::network();
126         let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
127         let chain_hash = genesis_block.block_hash();
128         chain_hash.write(&mut blob).unwrap();
129
130         let blob_timestamp = Snapshotter::round_down_to_nearest_multiple(current_timestamp, config::SNAPSHOT_CALCULATION_INTERVAL as u64) as u32;
131         blob_timestamp.write(&mut blob).unwrap();
132
133         0u32.write(&mut blob).unwrap(); // node count
134         0u32.write(&mut blob).unwrap(); // announcement count
135         0u32.write(&mut blob).unwrap(); // update count
136
137         blob
138 }
139
140 async fn serialize_delta(network_graph: Arc<NetworkGraph<TestLogger>>, last_sync_timestamp: u32, consider_intermediate_updates: bool) -> SerializedResponse {
141         let (client, connection) = lookup::connect_to_db().await;
142
143         network_graph.remove_stale_channels_and_tracking();
144
145         tokio::spawn(async move {
146                 if let Err(e) = connection.await {
147                         panic!("connection error: {}", e);
148                 }
149         });
150
151         let mut output: Vec<u8> = vec![];
152
153         // set a flag if the chain hash is prepended
154         // chain hash only necessary if either channel announcements or non-incremental updates are present
155         // for announcement-free incremental-only updates, chain hash can be skipped
156
157         let mut node_id_set: HashSet<NodeId> = HashSet::new();
158         let mut node_id_indices: HashMap<NodeId, usize> = HashMap::new();
159         let mut node_ids: Vec<NodeId> = Vec::new();
160         let mut duplicate_node_ids: i32 = 0;
161
162         let mut get_node_id_index = |node_id: NodeId| {
163                 if node_id_set.insert(node_id) {
164                         node_ids.push(node_id);
165                         let index = node_ids.len() - 1;
166                         node_id_indices.insert(node_id, index);
167                         return index;
168                 }
169                 duplicate_node_ids += 1;
170                 node_id_indices[&node_id]
171         };
172
173         let mut delta_set = DeltaSet::new();
174         lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp).await;
175         println!("announcement channel count: {}", delta_set.len());
176         lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp, consider_intermediate_updates).await;
177         println!("update-fetched channel count: {}", delta_set.len());
178         lookup::filter_delta_set(&mut delta_set);
179         println!("update-filtered channel count: {}", delta_set.len());
180         let serialization_details = serialization::serialize_delta_set(delta_set, last_sync_timestamp);
181
182         // process announcements
183         // write the number of channel announcements to the output
184         let announcement_count = serialization_details.announcements.len() as u32;
185         announcement_count.write(&mut output).unwrap();
186         let mut previous_announcement_scid = 0;
187         for current_announcement in serialization_details.announcements {
188                 let id_index_1 = get_node_id_index(current_announcement.node_id_1);
189                 let id_index_2 = get_node_id_index(current_announcement.node_id_2);
190                 let mut stripped_announcement = serialization::serialize_stripped_channel_announcement(&current_announcement, id_index_1, id_index_2, previous_announcement_scid);
191                 output.append(&mut stripped_announcement);
192
193                 previous_announcement_scid = current_announcement.short_channel_id;
194         }
195
196         // process updates
197         let mut previous_update_scid = 0;
198         let update_count = serialization_details.updates.len() as u32;
199         update_count.write(&mut output).unwrap();
200
201         let default_update_values = serialization_details.full_update_defaults;
202         if update_count > 0 {
203                 default_update_values.cltv_expiry_delta.write(&mut output).unwrap();
204                 default_update_values.htlc_minimum_msat.write(&mut output).unwrap();
205                 default_update_values.fee_base_msat.write(&mut output).unwrap();
206                 default_update_values.fee_proportional_millionths.write(&mut output).unwrap();
207                 default_update_values.htlc_maximum_msat.write(&mut output).unwrap();
208         }
209
210         let mut update_count_full = 0;
211         let mut update_count_incremental = 0;
212         for current_update in serialization_details.updates {
213                 match &current_update.mechanism {
214                         UpdateSerializationMechanism::Full => {
215                                 update_count_full += 1;
216                         }
217                         UpdateSerializationMechanism::Incremental(_) => {
218                                 update_count_incremental += 1;
219                         }
220                 };
221
222                 let mut stripped_update = serialization::serialize_stripped_channel_update(&current_update, &default_update_values, previous_update_scid);
223                 output.append(&mut stripped_update);
224
225                 previous_update_scid = current_update.update.short_channel_id;
226         }
227
228         // some stats
229         let message_count = announcement_count + update_count;
230
231         let mut prefixed_output = GOSSIP_PREFIX.to_vec();
232
233         // always write the chain hash
234         serialization_details.chain_hash.write(&mut prefixed_output).unwrap();
235         // always write the latest seen timestamp
236         let latest_seen_timestamp = serialization_details.latest_seen;
237         let overflow_seconds = latest_seen_timestamp % config::SNAPSHOT_CALCULATION_INTERVAL;
238         let serialized_seen_timestamp = latest_seen_timestamp.saturating_sub(overflow_seconds);
239         serialized_seen_timestamp.write(&mut prefixed_output).unwrap();
240
241         let node_id_count = node_ids.len() as u32;
242         node_id_count.write(&mut prefixed_output).unwrap();
243
244         for current_node_id in node_ids {
245                 current_node_id.write(&mut prefixed_output).unwrap();
246         }
247
248         prefixed_output.append(&mut output);
249
250         println!("duplicated node ids: {}", duplicate_node_ids);
251         println!("latest seen timestamp: {:?}", serialization_details.latest_seen);
252
253         SerializedResponse {
254                 data: prefixed_output,
255                 message_count,
256                 announcement_count,
257                 update_count,
258                 update_count_full,
259                 update_count_incremental,
260         }
261 }