]> git.bitcoin.ninja Git - rapid-gossip-sync-server/blob - src/tracking.rs
Switch to Rust edition 2021
[rapid-gossip-sync-server] / src / tracking.rs
1 use std::collections::hash_map::RandomState;
2 use std::hash::{BuildHasher, Hasher};
3 use std::net::SocketAddr;
4 use std::sync::Arc;
5 use std::time::{Duration, Instant};
6
7 use bitcoin::hashes::hex::ToHex;
8 use bitcoin::secp256k1::PublicKey;
9 use lightning;
10 use lightning::ln::peer_handler::{
11         ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager,
12 };
13 use lightning::routing::gossip::NetworkGraph;
14 use lightning::chain::keysinterface::KeysManager;
15 use tokio::sync::mpsc;
16
17 use crate::{config, TestLogger};
18 use crate::downloader::GossipRouter;
19 use crate::types::{GossipMessage, GossipPeerManager};
20
21 pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender<GossipMessage>,
22                 completion_sender: mpsc::Sender<()>,
23                 network_graph: Arc<NetworkGraph<TestLogger>>) {
24         let mut key = [42; 32];
25         let mut random_data = [43; 32];
26         // Get something psuedo-random from std.
27         let mut key_hasher = RandomState::new().build_hasher();
28         key_hasher.write_u8(1);
29         key[0..8].copy_from_slice(&key_hasher.finish().to_ne_bytes());
30         let mut rand_hasher = RandomState::new().build_hasher();
31         rand_hasher.write_u8(2);
32         random_data[0..8].copy_from_slice(&rand_hasher.finish().to_ne_bytes());
33
34         let keys_manager = Arc::new(KeysManager::new(&key, 0xdeadbeef, 0xdeadbeef));
35
36         let router = Arc::new(GossipRouter::new(network_graph, persistence_sender.clone()));
37
38         let message_handler = MessageHandler {
39                 chan_handler: ErroringMessageHandler::new(),
40                 route_handler: Arc::clone(&router),
41                 onion_message_handler: IgnoringMessageHandler {},
42         };
43         let peer_handler = Arc::new(PeerManager::new(
44                 message_handler,
45                 0xdeadbeef,
46                 &random_data,
47                 TestLogger::new(),
48                 IgnoringMessageHandler {},
49                 keys_manager,
50         ));
51         router.set_pm(Arc::clone(&peer_handler));
52
53         let ph_timer = Arc::clone(&peer_handler);
54         tokio::spawn(async move {
55                 let mut intvl = tokio::time::interval(Duration::from_secs(10));
56                 loop {
57                         intvl.tick().await;
58                         ph_timer.timer_tick_occurred();
59                 }
60         });
61
62         println!("Connecting to Lightning peers...");
63         let peers = config::ln_peers();
64         let mut connected_peer_count = 0;
65
66         for current_peer in peers {
67                 let initial_connection_succeeded = connect_peer(current_peer, Arc::clone(&peer_handler)).await;
68                 if initial_connection_succeeded {
69                         connected_peer_count += 1;
70                 }
71         }
72
73         if connected_peer_count < 1 {
74                 panic!("Failed to connect to any peer.");
75         }
76
77         println!("Connected to {} Lightning peers!", connected_peer_count);
78
79         tokio::spawn(async move {
80                 let mut previous_announcement_count = 0u64;
81                 let mut previous_update_count = 0u64;
82                 let mut is_caught_up_with_gossip = false;
83
84                 let mut i = 0u32;
85                 let mut latest_new_gossip_time = Instant::now();
86                 let mut needs_to_notify_persister = false;
87
88                 loop {
89                         i += 1; // count the background activity
90                         let sleep = tokio::time::sleep(Duration::from_secs(5));
91                         sleep.await;
92
93                         {
94                                 let counter = router.counter.read().unwrap();
95                                 let total_message_count = counter.channel_announcements + counter.channel_updates;
96                                 let new_message_count = total_message_count - previous_announcement_count - previous_update_count;
97
98                                 let was_previously_caught_up_with_gossip = is_caught_up_with_gossip;
99                                 // TODO: make new message threshold (20) adjust based on connected peer count
100                                 is_caught_up_with_gossip = new_message_count < 20 && previous_announcement_count > 0 && previous_update_count > 0;
101                                 if new_message_count > 0 {
102                                         latest_new_gossip_time = Instant::now();
103                                 }
104
105                                 // if we either aren't caught up, or just stopped/started being caught up
106                                 if !is_caught_up_with_gossip || (is_caught_up_with_gossip != was_previously_caught_up_with_gossip) {
107                                         println!(
108                                                 "gossip count (iteration {}): {} (delta: {}):\n\tannouncements: {}\n\t\tmismatched scripts: {}\n\tupdates: {}\n\t\tno HTLC max: {}\n",
109                                                 i,
110                                                 total_message_count,
111                                                 new_message_count,
112                                                 counter.channel_announcements,
113                                                 counter.channel_announcements_with_mismatched_scripts,
114                                                 counter.channel_updates,
115                                                 counter.channel_updates_without_htlc_max_msats
116                                         );
117                                 } else {
118                                         println!("Monitoring for gossip…")
119                                 }
120
121                                 if is_caught_up_with_gossip && !was_previously_caught_up_with_gossip {
122                                         println!("caught up with gossip!");
123                                         needs_to_notify_persister = true;
124                                 } else if !is_caught_up_with_gossip && was_previously_caught_up_with_gossip {
125                                         println!("Received new messages since catching up with gossip!");
126                                 }
127
128                                 let continuous_caught_up_duration = latest_new_gossip_time.elapsed();
129                                 if continuous_caught_up_duration.as_secs() > 600 {
130                                         eprintln!("No new gossip messages in 10 minutes! Something's amiss!");
131                                 }
132
133                                 previous_announcement_count = counter.channel_announcements;
134                                 previous_update_count = counter.channel_updates;
135                         }
136
137                         if needs_to_notify_persister {
138                                 needs_to_notify_persister = false;
139                                 completion_sender.send(()).await.unwrap();
140                         }
141                 }
142         });
143 }
144
145 async fn connect_peer(current_peer: (PublicKey, SocketAddr), peer_manager: GossipPeerManager) -> bool {
146         eprintln!("Connecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string());
147         let connection = lightning_net_tokio::connect_outbound(
148                 Arc::clone(&peer_manager),
149                 current_peer.0,
150                 current_peer.1,
151         ).await;
152         if let Some(disconnection_future) = connection {
153                 eprintln!("Connected to peer {}@{}!", current_peer.0.to_hex(), current_peer.1.to_string());
154                 tokio::spawn(async move {
155                         disconnection_future.await;
156                         loop {
157                                 eprintln!("Reconnecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string());
158                                 if let Some(disconnection_future) = lightning_net_tokio::connect_outbound(
159                                         Arc::clone(&peer_manager),
160                                         current_peer.0,
161                                         current_peer.1,
162                                 ).await {
163                                         disconnection_future.await;
164                                 }
165                                 tokio::time::sleep(Duration::from_secs(10)).await;
166                         }
167                 });
168                 true
169         } else {
170                 eprintln!("Failed to connect to peer {}@{}", current_peer.0.to_hex(), current_peer.1.to_string());
171                 false
172         }
173 }