From c928246bdd0381085311268fbc0840c7496a2352 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Wed, 16 Aug 2023 21:35:55 -0700 Subject: [PATCH] Create method for obtaining UTC-prepared Postgres client. --- src/lib.rs | 23 ++++++++++++++++------- src/lookup.rs | 8 +------- src/persistence.rs | 16 ++++++---------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b6281bc..550ed79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ use lightning::routing::gossip::{NetworkGraph, NodeId}; use lightning::util::logger::Logger; use lightning::util::ser::{ReadableArgs, Writeable}; use tokio::sync::mpsc; +use tokio_postgres::{Client, NoTls}; use crate::lookup::DeltaSet; use crate::persistence::GossipPersister; @@ -110,6 +111,20 @@ impl RapidSyncProcessor where L::Ta } } +pub(crate) async fn connect_to_db() -> Client { + let connection_config = config::db_connection_config(); + let (client, connection) = connection_config.connect(NoTls).await.unwrap(); + + tokio::spawn(async move { + if let Err(e) = connection.await { + panic!("connection error: {}", e); + } + }); + + client.execute("set time zone UTC", &[]).await.unwrap(); + client +} + /// This method generates a no-op blob that can be used as a delta where none exists. /// /// The primary purpose of this method is the scenario of a client retrieving and processing a @@ -142,16 +157,10 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec { } async fn serialize_delta(network_graph: Arc>, last_sync_timestamp: u32, logger: L) -> SerializedResponse where L::Target: Logger { - let (client, connection) = lookup::connect_to_db().await; + let client = connect_to_db().await; network_graph.remove_stale_channels_and_tracking(); - tokio::spawn(async move { - if let Err(e) = connection.await { - panic!("connection error: {}", e); - } - }); - let mut output: Vec = vec![]; // set a flag if the chain hash is prepended diff --git a/src/lookup.rs b/src/lookup.rs index eb0c8ee..696b4d0 100644 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -7,8 +7,7 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH}; use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate, UnsignedChannelAnnouncement, UnsignedChannelUpdate}; use lightning::routing::gossip::NetworkGraph; use lightning::util::ser::Readable; -use tokio_postgres::{Client, Connection, NoTls, Socket}; -use tokio_postgres::tls::NoTlsStream; +use tokio_postgres::Client; use futures::StreamExt; use lightning::log_info; @@ -68,11 +67,6 @@ impl Default for DirectedUpdateDelta { } } -pub(super) async fn connect_to_db() -> (Client, Connection) { - let connection_config = config::db_connection_config(); - connection_config.connect(NoTls).await.unwrap() -} - /// Fetch all the channel announcements that are presently in the network graph, regardless of /// whether they had been seen before. /// Also include all announcements for which the first update was announced diff --git a/src/persistence.rs b/src/persistence.rs index f638894..8bb7b18 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -8,7 +8,6 @@ use lightning::routing::gossip::NetworkGraph; use lightning::util::logger::Logger; use lightning::util::ser::Writeable; use tokio::sync::mpsc; -use tokio_postgres::NoTls; use crate::config; use crate::types::GossipMessage; @@ -33,15 +32,7 @@ impl GossipPersister where L::Target: Logger { } pub(crate) async fn persist_gossip(&mut self) { - let connection_config = config::db_connection_config(); - let (mut client, connection) = - connection_config.connect(NoTls).await.unwrap(); - - tokio::spawn(async move { - if let Err(e) = connection.await { - panic!("connection error: {}", e); - } - }); + let mut client = crate::connect_to_db().await; { // initialize the database @@ -57,6 +48,11 @@ impl GossipPersister where L::Target: Logger { config::upgrade_db(cur_schema[0].get(0), &mut client).await; } + let preparation = client.execute("set time zone UTC", &[]).await; + if let Err(preparation_error) = preparation { + panic!("db preparation error: {}", preparation_error); + } + let initialization = client .execute( // TODO: figure out a way to fix the id value without Postgres complaining about -- 2.30.2