|
78 | 78 | mod balance; |
79 | 79 | mod builder; |
80 | 80 | mod config; |
| 81 | +mod connection; |
81 | 82 | mod error; |
82 | 83 | mod event; |
83 | 84 | mod fee_estimator; |
@@ -124,6 +125,7 @@ use config::{ |
124 | 125 | LDK_PAYMENT_RETRY_TIMEOUT, NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL, |
125 | 126 | RGS_SYNC_INTERVAL, WALLET_SYNC_INTERVAL_MINIMUM_SECS, |
126 | 127 | }; |
| 128 | +use connection::{connect_peer_if_necessary, do_connect_peer}; |
127 | 129 | use event::{EventHandler, EventQueue}; |
128 | 130 | use gossip::GossipSource; |
129 | 131 | use liquidity::LiquiditySource; |
@@ -1847,56 +1849,3 @@ pub struct NodeStatus { |
1847 | 1849 | /// Will be `None` if we have no public channels or we haven't broadcasted since the [`Node`] was initialized. |
1848 | 1850 | pub latest_node_announcement_broadcast_timestamp: Option<u64>, |
1849 | 1851 | } |
1850 | | - |
1851 | | -async fn connect_peer_if_necessary( |
1852 | | - node_id: PublicKey, addr: SocketAddress, peer_manager: Arc<PeerManager>, |
1853 | | - logger: Arc<FilesystemLogger>, |
1854 | | -) -> Result<(), Error> { |
1855 | | - if peer_manager.peer_by_node_id(&node_id).is_some() { |
1856 | | - return Ok(()); |
1857 | | - } |
1858 | | - |
1859 | | - do_connect_peer(node_id, addr, peer_manager, logger).await |
1860 | | -} |
1861 | | - |
1862 | | -async fn do_connect_peer( |
1863 | | - node_id: PublicKey, addr: SocketAddress, peer_manager: Arc<PeerManager>, |
1864 | | - logger: Arc<FilesystemLogger>, |
1865 | | -) -> Result<(), Error> { |
1866 | | - log_info!(logger, "Connecting to peer: {}@{}", node_id, addr); |
1867 | | - |
1868 | | - let socket_addr = addr |
1869 | | - .to_socket_addrs() |
1870 | | - .map_err(|e| { |
1871 | | - log_error!(logger, "Failed to resolve network address: {}", e); |
1872 | | - Error::InvalidSocketAddress |
1873 | | - })? |
1874 | | - .next() |
1875 | | - .ok_or(Error::ConnectionFailed)?; |
1876 | | - |
1877 | | - match lightning_net_tokio::connect_outbound(Arc::clone(&peer_manager), node_id, socket_addr) |
1878 | | - .await |
1879 | | - { |
1880 | | - Some(connection_closed_future) => { |
1881 | | - let mut connection_closed_future = Box::pin(connection_closed_future); |
1882 | | - loop { |
1883 | | - match futures::poll!(&mut connection_closed_future) { |
1884 | | - std::task::Poll::Ready(_) => { |
1885 | | - log_info!(logger, "Peer connection closed: {}@{}", node_id, addr); |
1886 | | - return Err(Error::ConnectionFailed); |
1887 | | - }, |
1888 | | - std::task::Poll::Pending => {}, |
1889 | | - } |
1890 | | - // Avoid blocking the tokio context by sleeping a bit |
1891 | | - match peer_manager.peer_by_node_id(&node_id) { |
1892 | | - Some(_) => return Ok(()), |
1893 | | - None => tokio::time::sleep(Duration::from_millis(10)).await, |
1894 | | - } |
1895 | | - } |
1896 | | - }, |
1897 | | - None => { |
1898 | | - log_error!(logger, "Failed to connect to peer: {}@{}", node_id, addr); |
1899 | | - Err(Error::ConnectionFailed) |
1900 | | - }, |
1901 | | - } |
1902 | | -} |
0 commit comments