1 use std::fs::OpenOptions;
2 use std::io::{BufWriter, Write};
5 use std::time::{Duration, Instant};
6 use lightning::log_info;
7 use lightning::routing::gossip::NetworkGraph;
8 use lightning::util::logger::Logger;
9 use lightning::util::ser::Writeable;
10 use tokio::sync::mpsc;
13 use crate::types::GossipMessage;
15 const POSTGRES_INSERT_TIMEOUT: Duration = Duration::from_secs(15);
17 pub(crate) struct GossipPersister<L: Deref> where L::Target: Logger {
18 gossip_persistence_receiver: mpsc::Receiver<GossipMessage>,
19 network_graph: Arc<NetworkGraph<L>>,
23 impl<L: Deref> GossipPersister<L> where L::Target: Logger {
24 pub fn new(network_graph: Arc<NetworkGraph<L>>, logger: L) -> (Self, mpsc::Sender<GossipMessage>) {
25 let (gossip_persistence_sender, gossip_persistence_receiver) =
26 mpsc::channel::<GossipMessage>(100);
28 gossip_persistence_receiver,
31 }, gossip_persistence_sender)
34 pub(crate) async fn persist_gossip(&mut self) {
35 let mut client = crate::connect_to_db().await;
38 // initialize the database
39 let initialization = client
40 .execute(config::db_config_table_creation_query(), &[])
42 if let Err(initialization_error) = initialization {
43 panic!("db init error: {}", initialization_error);
46 let cur_schema = client.query("SELECT db_schema FROM config WHERE id = $1", &[&1]).await.unwrap();
47 if !cur_schema.is_empty() {
48 config::upgrade_db(cur_schema[0].get(0), &mut client).await;
51 let preparation = client.execute("set time zone UTC", &[]).await;
52 if let Err(preparation_error) = preparation {
53 panic!("db preparation error: {}", preparation_error);
56 let initialization = client
58 // TODO: figure out a way to fix the id value without Postgres complaining about
59 // its value not being default
60 "INSERT INTO config (id, db_schema) VALUES ($1, $2) ON CONFLICT (id) DO NOTHING",
61 &[&1, &config::SCHEMA_VERSION]
63 if let Err(initialization_error) = initialization {
64 panic!("db init error: {}", initialization_error);
67 let initialization = client
68 .execute(config::db_announcement_table_creation_query(), &[])
70 if let Err(initialization_error) = initialization {
71 panic!("db init error: {}", initialization_error);
74 let initialization = client
76 config::db_channel_update_table_creation_query(),
80 if let Err(initialization_error) = initialization {
81 panic!("db init error: {}", initialization_error);
84 let initialization = client
85 .batch_execute(config::db_index_creation_query())
87 if let Err(initialization_error) = initialization {
88 panic!("db init error: {}", initialization_error);
92 // print log statement every minute
93 let mut latest_persistence_log = Instant::now() - Duration::from_secs(60);
95 let mut latest_graph_cache_time = Instant::now();
96 // TODO: it would be nice to have some sort of timeout here so after 10 seconds of
97 // inactivity, some sort of message could be broadcast signaling the activation of request
99 while let Some(gossip_message) = &self.gossip_persistence_receiver.recv().await {
100 i += 1; // count the persisted gossip messages
102 if latest_persistence_log.elapsed().as_secs() >= 60 {
103 log_info!(self.logger, "Persisting gossip message #{}", i);
104 latest_persistence_log = Instant::now();
107 // has it been ten minutes? Just cache it
108 if latest_graph_cache_time.elapsed().as_secs() >= 600 {
109 self.persist_network_graph();
110 latest_graph_cache_time = Instant::now();
113 match &gossip_message {
114 GossipMessage::ChannelAnnouncement(announcement, seen_override) => {
115 let scid = announcement.contents.short_channel_id as i64;
117 // start with the type prefix, which is already known a priori
118 let mut announcement_signed = Vec::new();
119 announcement.write(&mut announcement_signed).unwrap();
121 if cfg!(test) && seen_override.is_some() {
122 tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
123 .execute("INSERT INTO channel_announcements (\
125 announcement_signed, \
127 ) VALUES ($1, $2, TO_TIMESTAMP($3)) ON CONFLICT (short_channel_id) DO NOTHING", &[
129 &announcement_signed,
130 &(seen_override.unwrap() as f64)
131 ])).await.unwrap().unwrap();
133 tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
134 .execute("INSERT INTO channel_announcements (\
136 announcement_signed \
137 ) VALUES ($1, $2) ON CONFLICT (short_channel_id) DO NOTHING", &[
140 ])).await.unwrap().unwrap();
143 GossipMessage::ChannelUpdate(update, seen_override) => {
144 let scid = update.contents.short_channel_id as i64;
146 let timestamp = update.contents.timestamp as i64;
148 let direction = (update.contents.flags & 1) == 1;
149 let disable = (update.contents.flags & 2) > 0;
151 let cltv_expiry_delta = update.contents.cltv_expiry_delta as i32;
152 let htlc_minimum_msat = update.contents.htlc_minimum_msat as i64;
153 let fee_base_msat = update.contents.fee_base_msat as i32;
154 let fee_proportional_millionths =
155 update.contents.fee_proportional_millionths as i32;
156 let htlc_maximum_msat = update.contents.htlc_maximum_msat as i64;
158 // start with the type prefix, which is already known a priori
159 let mut update_signed = Vec::new();
160 update.write(&mut update_signed).unwrap();
162 let insertion_statement = if cfg!(test) {
163 "INSERT INTO channel_updates (\
173 fee_proportional_millionths, \
176 ) VALUES ($1, $2, TO_TIMESTAMP($3), $4, $5, $6, $7, $8, $9, $10, $11, $12) ON CONFLICT DO NOTHING"
178 "INSERT INTO channel_updates (\
187 fee_proportional_millionths, \
190 ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ON CONFLICT DO NOTHING"
193 // this may not be used outside test cfg
194 let _seen_timestamp = seen_override.unwrap_or(timestamp as u32) as f64;
196 tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
197 .execute(insertion_statement, &[
202 &(update.contents.flags as i16),
208 &fee_proportional_millionths,
211 ])).await.unwrap().unwrap();
217 fn persist_network_graph(&self) {
218 log_info!(self.logger, "Caching network graph…");
219 let cache_path = config::network_graph_cache_path();
220 let file = OpenOptions::new()
226 self.network_graph.remove_stale_channels_and_tracking();
227 let mut writer = BufWriter::new(file);
228 self.network_graph.write(&mut writer).unwrap();
229 writer.flush().unwrap();
230 log_info!(self.logger, "Cached network graph!");