From cf326fd7a9f42c58a24788943745f066ecb5f641 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Wed, 2 Aug 2023 16:00:36 -0700 Subject: [PATCH] Switch Logger from Arc to Deref. --- src/downloader.rs | 15 ++++++++------- src/lib.rs | 17 +++++++++-------- src/lookup.rs | 4 ++-- src/persistence.rs | 9 +++++---- src/snapshot.rs | 9 +++++---- src/tracking.rs | 11 ++++++----- src/types.rs | 2 +- src/verifier.rs | 13 +++++++------ 8 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index 0c672e3..34a5c66 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use std::sync::{Arc, RwLock}; use bitcoin::secp256k1::PublicKey; @@ -30,16 +31,16 @@ impl GossipCounter { } } -pub(crate) struct GossipRouter { - native_router: P2PGossipSync>>, GossipChainAccess, Arc>, +pub(crate) struct GossipRouter where L::Target: Logger { + native_router: P2PGossipSync>, GossipChainAccess, L>, pub(crate) counter: RwLock, sender: mpsc::Sender, verifier: Arc>, - outbound_gossiper: Arc>>, GossipChainAccess, Arc>>, + outbound_gossiper: Arc>, GossipChainAccess, L>>, } -impl GossipRouter { - pub(crate) fn new(network_graph: Arc>>, sender: mpsc::Sender, logger: Arc) -> Self { +impl GossipRouter where L::Target: Logger { + pub(crate) fn new(network_graph: Arc>, sender: mpsc::Sender, logger: L) -> Self { let outbound_gossiper = Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, logger.clone())); let verifier = Arc::new(ChainVerifier::new(Arc::clone(&network_graph), Arc::clone(&outbound_gossiper))); Self { @@ -83,7 +84,7 @@ impl GossipRouter { } } -impl MessageSendEventsProvider for GossipRouter { +impl MessageSendEventsProvider for GossipRouter where L::Target: Logger { fn get_and_clear_pending_msg_events(&self) -> Vec { let gossip_evs = self.outbound_gossiper.get_and_clear_pending_msg_events(); for ev in gossip_evs { @@ -102,7 +103,7 @@ impl MessageSendEventsProvider for GossipRouter { } } -impl RoutingMessageHandler for GossipRouter { +impl RoutingMessageHandler for GossipRouter where L::Target: Logger { fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result { self.native_router.handle_node_announcement(msg) } diff --git a/src/lib.rs b/src/lib.rs index 363d4ae..803c077 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ extern crate core; use std::collections::{HashMap, HashSet}; use std::fs::File; use std::io::BufReader; +use std::ops::Deref; use std::sync::Arc; use lightning::routing::gossip::{NetworkGraph, NodeId}; @@ -43,9 +44,9 @@ pub mod types; /// The fourth byte is the protocol version in case our format gets updated. const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1]; -pub struct RapidSyncProcessor { - network_graph: Arc>>, - logger: Arc +pub struct RapidSyncProcessor where L::Target: Logger { + network_graph: Arc>, + logger: L } pub struct SerializedResponse { @@ -57,8 +58,8 @@ pub struct SerializedResponse { pub update_count_incremental: u32, } -impl RapidSyncProcessor { - pub fn new(logger: Arc) -> Self { +impl RapidSyncProcessor where L::Target: Logger { + pub fn new(logger: L) -> Self { let network = config::network(); let network_graph = if let Ok(file) = File::open(&config::network_graph_cache_path()) { println!("Initializing from cached network graph…"); @@ -90,7 +91,7 @@ impl RapidSyncProcessor { println!("Starting gossip download"); tokio::spawn(tracking::download_gossip(persistence_sender, sync_completion_sender, - Arc::clone(&self.network_graph), Arc::clone(&self.logger))); + Arc::clone(&self.network_graph), self.logger.clone())); println!("Starting gossip db persistence listener"); tokio::spawn(async move { persister.persist_gossip().await; }); } else { @@ -129,7 +130,7 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec { let chain_hash = genesis_block.block_hash(); chain_hash.write(&mut blob).unwrap(); - let blob_timestamp = Snapshotter::::round_down_to_nearest_multiple(current_timestamp, config::SNAPSHOT_CALCULATION_INTERVAL as u64) as u32; + let blob_timestamp = Snapshotter::>::round_down_to_nearest_multiple(current_timestamp, config::SNAPSHOT_CALCULATION_INTERVAL as u64) as u32; blob_timestamp.write(&mut blob).unwrap(); 0u32.write(&mut blob).unwrap(); // node count @@ -139,7 +140,7 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec { blob } -async fn serialize_delta(network_graph: Arc>>, last_sync_timestamp: u32) -> SerializedResponse { +async fn serialize_delta(network_graph: Arc>, last_sync_timestamp: u32) -> SerializedResponse where L::Target: Logger { let (client, connection) = lookup::connect_to_db().await; network_graph.remove_stale_channels_and_tracking(); diff --git a/src/lookup.rs b/src/lookup.rs index 79fb84e..9e56199 100644 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -1,6 +1,6 @@ use std::collections::{BTreeMap, HashSet}; use std::io::Cursor; -use std::ops::Add; +use std::ops::{Add, Deref}; use std::sync::Arc; use std::time::{Duration, Instant, SystemTime}; @@ -76,7 +76,7 @@ pub(super) async fn connect_to_db() -> (Client, Connection) /// whether they had been seen before. /// Also include all announcements for which the first update was announced /// after `last_sync_timestamp` -pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, network_graph: Arc>>, client: &Client, last_sync_timestamp: u32) { +pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, network_graph: Arc>, client: &Client, last_sync_timestamp: u32) where L::Target: Logger { println!("Obtaining channel ids from network graph"); let last_sync_timestamp_object = SystemTime::UNIX_EPOCH.add(Duration::from_secs(last_sync_timestamp as u64)); let channel_ids = { diff --git a/src/persistence.rs b/src/persistence.rs index 22abf02..e2d9eb0 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -1,5 +1,6 @@ use std::fs::OpenOptions; use std::io::{BufWriter, Write}; +use std::ops::Deref; use std::sync::Arc; use std::time::{Duration, Instant}; use lightning::routing::gossip::NetworkGraph; @@ -13,13 +14,13 @@ use crate::types::GossipMessage; const POSTGRES_INSERT_TIMEOUT: Duration = Duration::from_secs(15); -pub(crate) struct GossipPersister { +pub(crate) struct GossipPersister where L::Target: Logger { gossip_persistence_receiver: mpsc::Receiver, - network_graph: Arc>>, + network_graph: Arc>, } -impl GossipPersister { - pub fn new(network_graph: Arc>>) -> (Self, mpsc::Sender) { +impl GossipPersister where L::Target: Logger { + pub fn new(network_graph: Arc>) -> (Self, mpsc::Sender) { let (gossip_persistence_sender, gossip_persistence_receiver) = mpsc::channel::(100); (GossipPersister { diff --git a/src/snapshot.rs b/src/snapshot.rs index c81cc35..05cc63e 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::fs; +use std::ops::Deref; use std::os::unix::fs::symlink; use std::sync::Arc; use std::time::{Duration, SystemTime, UNIX_EPOCH}; @@ -10,12 +11,12 @@ use lightning::util::logger::Logger; use crate::config; use crate::config::cache_path; -pub(crate) struct Snapshotter { - network_graph: Arc>>, +pub(crate) struct Snapshotter where L::Target: Logger { + network_graph: Arc>, } -impl Snapshotter { - pub fn new(network_graph: Arc>>) -> Self { +impl Snapshotter where L::Target: Logger { + pub fn new(network_graph: Arc>) -> Self { Self { network_graph } } diff --git a/src/tracking.rs b/src/tracking.rs index 2935eb2..99166af 100644 --- a/src/tracking.rs +++ b/src/tracking.rs @@ -1,6 +1,7 @@ use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hasher}; use std::net::SocketAddr; +use std::ops::Deref; use std::sync::Arc; use std::time::{Duration, Instant}; @@ -19,11 +20,11 @@ use crate::config; use crate::downloader::GossipRouter; use crate::types::{GossipMessage, GossipPeerManager}; -pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender, +pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender, completion_sender: mpsc::Sender<()>, - network_graph: Arc>>, - logger: Arc -) { + network_graph: Arc>, + logger: L +) where L::Target: Logger { let mut key = [42; 32]; let mut random_data = [43; 32]; // Get something psuedo-random from std. @@ -145,7 +146,7 @@ pub(crate) async fn download_gossip(persisten }); } -async fn connect_peer(current_peer: (PublicKey, SocketAddr), peer_manager: GossipPeerManager) -> bool { +async fn connect_peer(current_peer: (PublicKey, SocketAddr), peer_manager: GossipPeerManager) -> bool where L::Target: Logger { eprintln!("Connecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string()); let connection = lightning_net_tokio::connect_outbound( Arc::clone(&peer_manager), diff --git a/src/types.rs b/src/types.rs index b18cbe7..6fafbd3 100644 --- a/src/types.rs +++ b/src/types.rs @@ -9,7 +9,7 @@ use crate::downloader::GossipRouter; use crate::verifier::ChainVerifier; pub(crate) type GossipChainAccess = Arc>; -pub(crate) type GossipPeerManager = Arc>, IgnoringMessageHandler, Arc, IgnoringMessageHandler, Arc>>; +pub(crate) type GossipPeerManager = Arc>, IgnoringMessageHandler, L, IgnoringMessageHandler, Arc>>; #[derive(Debug)] pub(crate) enum GossipMessage { diff --git a/src/verifier.rs b/src/verifier.rs index a88f2b7..872a501 100644 --- a/src/verifier.rs +++ b/src/verifier.rs @@ -1,4 +1,5 @@ use std::convert::TryInto; +use std::ops::Deref; use std::sync::Arc; use std::sync::Mutex; @@ -15,17 +16,17 @@ use lightning_block_sync::rest::RestClient; use crate::config; use crate::types::GossipPeerManager; -pub(crate) struct ChainVerifier { +pub(crate) struct ChainVerifier where L::Target: Logger { rest_client: Arc, - graph: Arc>>, - outbound_gossiper: Arc>>, Arc, Arc>>, + graph: Arc>, + outbound_gossiper: Arc>, Arc, L>>, peer_handler: Mutex>>, } struct RestBinaryResponse(Vec); -impl ChainVerifier { - pub(crate) fn new(graph: Arc>>, outbound_gossiper: Arc>>, Arc, Arc>>) -> Self { +impl ChainVerifier where L::Target: Logger { + pub(crate) fn new(graph: Arc>, outbound_gossiper: Arc>, Arc, L>>) -> Self { ChainVerifier { rest_client: Arc::new(RestClient::new(config::bitcoin_rest_endpoint()).unwrap()), outbound_gossiper, @@ -73,7 +74,7 @@ impl ChainVerifier { } } -impl UtxoLookup for ChainVerifier { +impl UtxoLookup for ChainVerifier where L::Target: Logger { fn get_utxo(&self, _genesis_hash: &BlockHash, short_channel_id: u64) -> UtxoResult { let res = UtxoFuture::new(); let fut = res.clone(); -- 2.39.5