From ebbd62f41d8150b31167c1f5a60efc080f2b2961 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 22 Aug 2022 03:17:09 +0000 Subject: [PATCH] Substantially reduce boilerplate and Arcs in download_gossip --- src/downloader.rs | 20 ++++++++++++---- src/lib.rs | 2 +- src/snapshot.rs | 1 - src/tracking.rs | 58 ++++++++++++----------------------------------- src/types.rs | 2 +- 5 files changed, 33 insertions(+), 50 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index 691a9e6..72e55c5 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -7,8 +7,9 @@ use lightning::util::events::{MessageSendEvent, MessageSendEventsProvider}; use tokio::sync::mpsc; use tokio::sync::mpsc::error::TrySendError; -use crate::{GossipChainAccess, TestLogger}; -use crate::types::GossipMessage; +use crate::TestLogger; +use crate::types::{GossipMessage, GossipChainAccess}; +use crate::verifier::ChainVerifier; pub(crate) struct GossipCounter { pub(crate) channel_announcements: u64, @@ -29,9 +30,20 @@ impl GossipCounter { } pub(crate) struct GossipRouter { - pub(crate) native_router: Arc>>, GossipChainAccess, Arc>>, + native_router: P2PGossipSync>>, GossipChainAccess, Arc>, pub(crate) counter: RwLock, - pub(crate) sender: mpsc::Sender, + sender: mpsc::Sender, +} + +impl GossipRouter { + pub(crate) fn new(network_graph: Arc>>, sender: mpsc::Sender) -> Self { + Self { + native_router: P2PGossipSync::new(network_graph, Some(Arc::new(ChainVerifier::new())), + Arc::new(TestLogger::new())), + counter: RwLock::new(GossipCounter::new()), + sender + } + } } impl MessageSendEventsProvider for GossipRouter { diff --git a/src/lib.rs b/src/lib.rs index c8fb6a7..e9ca343 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ use crate::lookup::DeltaSet; use crate::persistence::GossipPersister; use crate::serialization::UpdateSerializationMechanism; use crate::snapshot::Snapshotter; -use crate::types::{GossipChainAccess, TestLogger}; +use crate::types::TestLogger; mod downloader; mod types; diff --git a/src/snapshot.rs b/src/snapshot.rs index 93be840..ab664ca 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -117,7 +117,6 @@ impl Snapshotter { fs::rename(&pending_snapshot_directory, &finalized_snapshot_directory).expect("Failed to finalize snapshot directory."); fs::rename(&pending_symlink_directory, &finalized_symlink_directory).expect("Failed to finalize symlink directory."); - let remainder = timestamp_seen % round_day_seconds; let time_until_next_day = round_day_seconds - remainder; diff --git a/src/tracking.rs b/src/tracking.rs index b9dab98..af24055 100644 --- a/src/tracking.rs +++ b/src/tracking.rs @@ -1,5 +1,5 @@ use std::net::SocketAddr; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use std::time::{Duration, Instant}; use bitcoin::hashes::hex::ToHex; @@ -8,14 +8,13 @@ use lightning; use lightning::ln::peer_handler::{ ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager, }; -use lightning::routing::gossip::{NetworkGraph, P2PGossipSync}; +use lightning::routing::gossip::NetworkGraph; use rand::{Rng, thread_rng}; use tokio::sync::mpsc; use crate::{config, TestLogger}; -use crate::downloader::{GossipCounter, GossipRouter}; -use crate::types::{GossipChainAccess, GossipMessage, GossipPeerManager}; -use crate::verifier::ChainVerifier; +use crate::downloader::GossipRouter; +use crate::types::{GossipMessage, GossipPeerManager}; pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender, network_graph: Arc>>) { let mut key = [0; 32]; @@ -24,49 +23,26 @@ pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender; - let arc_chain_access = Some(Arc::new(ChainVerifier::new())); - let ignorer = IgnoringMessageHandler {}; - let arc_ignorer = Arc::new(ignorer); - - let errorer = ErroringMessageHandler::new(); - let arc_errorer = Arc::new(errorer); - - let logger = TestLogger::new(); - let arc_logger = Arc::new(logger); - - let router = P2PGossipSync::new( - network_graph.clone(), - arc_chain_access, - Arc::clone(&arc_logger), - ); - let arc_router = Arc::new(router); - let wrapped_router = GossipRouter { - native_router: arc_router, - counter: RwLock::new(GossipCounter::new()), - sender: persistence_sender.clone(), - }; - let arc_wrapped_router = Arc::new(wrapped_router); + let router = Arc::new(GossipRouter::new(network_graph, persistence_sender.clone())); let message_handler = MessageHandler { - chan_handler: arc_errorer, - route_handler: arc_wrapped_router.clone(), + chan_handler: ErroringMessageHandler::new(), + route_handler: Arc::clone(&router), }; - let peer_handler = PeerManager::new( + let peer_handler = Arc::new(PeerManager::new( message_handler, our_node_secret, &random_data, - Arc::clone(&arc_logger), - arc_ignorer, - ); - let arc_peer_handler = Arc::new(peer_handler); + Arc::new(TestLogger::new()), + IgnoringMessageHandler {}, + )); - println!("Connecting to Lightning peers…"); + println!("Connecting to Lightning peers..."); let peers = config::ln_peers(); let mut connected_peer_count = 0; for current_peer in peers { - let initial_connection_succeeded = connect_peer(current_peer, Arc::clone(&arc_peer_handler)).await; + let initial_connection_succeeded = connect_peer(current_peer, Arc::clone(&peer_handler)).await; if initial_connection_succeeded { connected_peer_count += 1; } @@ -78,8 +54,6 @@ pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender; -pub(crate) type GossipPeerManager = Arc, Arc, Arc, Arc>>; +pub(crate) type GossipPeerManager = Arc, Arc, IgnoringMessageHandler>>; #[derive(Debug)] pub(crate) enum GossipMessage { -- 2.39.5