1 use std::collections::hash_map::RandomState;
2 use std::hash::{BuildHasher, Hasher};
3 use std::net::SocketAddr;
5 use std::time::{Duration, Instant};
7 use bitcoin::hashes::hex::ToHex;
8 use bitcoin::secp256k1::PublicKey;
10 use lightning::ln::peer_handler::{
11 ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager,
13 use lightning::routing::gossip::NetworkGraph;
14 use lightning::chain::keysinterface::KeysManager;
15 use tokio::sync::mpsc;
17 use crate::{config, TestLogger};
18 use crate::downloader::GossipRouter;
19 use crate::types::{GossipMessage, GossipPeerManager};
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());
34 let keys_manager = Arc::new(KeysManager::new(&key, 0xdeadbeef, 0xdeadbeef));
36 let router = Arc::new(GossipRouter::new(network_graph, persistence_sender.clone()));
38 let message_handler = MessageHandler {
39 chan_handler: ErroringMessageHandler::new(),
40 route_handler: Arc::clone(&router),
41 onion_message_handler: IgnoringMessageHandler {},
43 let peer_handler = Arc::new(PeerManager::new(
48 IgnoringMessageHandler {},
51 router.set_pm(Arc::clone(&peer_handler));
53 let ph_timer = Arc::clone(&peer_handler);
54 tokio::spawn(async move {
55 let mut intvl = tokio::time::interval(Duration::from_secs(10));
58 ph_timer.timer_tick_occurred();
62 println!("Connecting to Lightning peers...");
63 let peers = config::ln_peers();
64 let mut connected_peer_count = 0;
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;
73 if connected_peer_count < 1 {
74 panic!("Failed to connect to any peer.");
77 println!("Connected to {} Lightning peers!", connected_peer_count);
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;
85 let mut latest_new_gossip_time = Instant::now();
86 let mut needs_to_notify_persister = false;
89 i += 1; // count the background activity
90 let sleep = tokio::time::sleep(Duration::from_secs(5));
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;
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();
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) {
108 "gossip count (iteration {}): {} (delta: {}):\n\tannouncements: {}\n\t\tmismatched scripts: {}\n\tupdates: {}\n\t\tno HTLC max: {}\n",
112 counter.channel_announcements,
113 counter.channel_announcements_with_mismatched_scripts,
114 counter.channel_updates,
115 counter.channel_updates_without_htlc_max_msats
118 println!("Monitoring for gossip…")
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!");
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!");
133 previous_announcement_count = counter.channel_announcements;
134 previous_update_count = counter.channel_updates;
137 if needs_to_notify_persister {
138 needs_to_notify_persister = false;
139 completion_sender.send(()).await.unwrap();
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),
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;
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),
163 disconnection_future.await;
165 tokio::time::sleep(Duration::from_secs(10)).await;
170 eprintln!("Failed to connect to peer {}@{}", current_peer.0.to_hex(), current_peer.1.to_string());