-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathconnection.rs
More file actions
338 lines (308 loc) · 9.64 KB
/
Copy pathconnection.rs
File metadata and controls
338 lines (308 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use std::collections::hash_map::{self, HashMap};
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::msgs::SocketAddress;
use crate::config::TorConfig;
use crate::logger::{log_debug, log_error, log_info, LdkLogger};
use crate::types::{KeysManager, PeerManager};
use crate::Error;
type PendingConnections =
Mutex<HashMap<PublicKey, Vec<tokio::sync::oneshot::Sender<Result<(), Error>>>>>;
struct PendingConnectionGuard<'a> {
pending_connections: &'a PendingConnections,
node_id: PublicKey,
active: bool,
}
impl<'a> PendingConnectionGuard<'a> {
fn new(pending_connections: &'a PendingConnections, node_id: PublicKey) -> Self {
Self { pending_connections, node_id, active: true }
}
fn disarm(&mut self) {
self.active = false;
}
}
impl Drop for PendingConnectionGuard<'_> {
fn drop(&mut self) {
if !self.active {
return;
}
let mut pending_connections = self.pending_connections.lock().expect("lock");
if let Some(subscribers) = pending_connections.remove(&self.node_id) {
for subscriber in subscribers {
let _ = subscriber.send(Err(Error::ConnectionFailed));
}
}
}
}
pub(crate) struct ConnectionManager<L: Deref + Clone + Sync + Send>
where
L::Target: LdkLogger,
{
pending_connections: PendingConnections,
peer_manager: Arc<PeerManager>,
tor_proxy_config: Option<TorConfig>,
keys_manager: Arc<KeysManager>,
logger: L,
}
impl<L: Deref + Clone + Sync + Send> ConnectionManager<L>
where
L::Target: LdkLogger,
{
pub(crate) fn new(
peer_manager: Arc<PeerManager>, tor_proxy_config: Option<TorConfig>,
keys_manager: Arc<KeysManager>, logger: L,
) -> Self {
let pending_connections = Mutex::new(HashMap::new());
Self { pending_connections, peer_manager, tor_proxy_config, keys_manager, logger }
}
pub(crate) async fn connect_peer_if_necessary(
&self, node_id: PublicKey, addr: SocketAddress,
) -> Result<(), Error> {
if self.peer_manager.peer_by_node_id(&node_id).is_some() {
return Ok(());
}
self.do_connect_peer(node_id, addr).await
}
pub(crate) fn disconnect_peer(&self, node_id: PublicKey) {
self.peer_manager.disconnect_by_node_id(node_id);
}
pub(crate) async fn do_connect_peer(
&self, node_id: PublicKey, addr: SocketAddress,
) -> Result<(), Error> {
// If another task is already connecting, subscribe to its result instead of starting a
// duplicate attempt.
if let Some(pending_connection_ready_receiver) =
self.register_or_subscribe_pending_connection(&node_id)
{
return pending_connection_ready_receiver.await.map_err(|e| {
debug_assert!(false, "Failed to receive connection result: {:?}", e);
log_error!(self.logger, "Failed to receive connection result: {:?}", e);
Error::ConnectionFailed
})?;
}
let mut pending_connection =
PendingConnectionGuard::new(&self.pending_connections, node_id);
let res = self.do_connect_peer_internal(node_id, addr).await;
self.propagate_result_to_subscribers(&node_id, res);
pending_connection.disarm();
res
}
async fn do_connect_peer_internal(
&self, node_id: PublicKey, addr: SocketAddress,
) -> Result<(), Error> {
log_info!(self.logger, "Connecting to peer: {}@{}", node_id, addr);
match addr {
SocketAddress::OnionV2(old_onion_addr) => {
log_error!(
self.logger,
"Failed to resolve network address {:?}: Resolution of OnionV2 addresses is currently unsupported.",
old_onion_addr
);
Err(Error::InvalidSocketAddress)
},
SocketAddress::OnionV3 { .. } => {
let proxy_config = self.tor_proxy_config.as_ref().ok_or_else(|| {
log_error!(
self.logger,
"Failed to resolve network address {:?}: Tor usage is not configured.",
addr
);
Error::InvalidSocketAddress
})?;
let resolved_addrs: Vec<_> =
tokio::net::lookup_host(proxy_config.proxy_address.to_string())
.await
.map_err(|e| {
log_error!(
self.logger,
"Failed to resolve Tor proxy network address {}: {}",
proxy_config.proxy_address,
e
);
Error::InvalidSocketAddress
})?
.collect();
if resolved_addrs.is_empty() {
log_error!(
self.logger,
"Failed to resolve Tor proxy network address {}",
proxy_config.proxy_address
);
return Err(Error::InvalidSocketAddress);
}
let mut res = Err(Error::ConnectionFailed);
let mut had_failures = false;
for proxy_addr in resolved_addrs {
let connection_future = lightning_net_tokio::tor_connect_outbound(
Arc::clone(&self.peer_manager),
node_id,
addr.clone(),
proxy_addr,
Arc::clone(&self.keys_manager),
);
res = self.await_connection(connection_future, node_id, addr.clone()).await;
if res.is_ok() {
if had_failures {
log_info!(
self.logger,
"Successfully connected to peer {}@{} via resolved proxy address {} after previous attempts failed.",
node_id, addr, proxy_addr
);
}
break;
}
had_failures = true;
log_debug!(
self.logger,
"Failed to connect to peer {}@{} via resolved proxy address {}.",
node_id,
addr,
proxy_addr
);
}
res
},
_ => {
let resolved_addrs: Vec<_> = tokio::net::lookup_host(addr.to_string())
.await
.map_err(|e| {
log_error!(
self.logger,
"Failed to resolve network address {}: {}",
addr,
e
);
Error::InvalidSocketAddress
})?
.collect();
if resolved_addrs.is_empty() {
log_error!(self.logger, "Failed to resolve network address {}", addr);
return Err(Error::InvalidSocketAddress);
}
let mut res = Err(Error::ConnectionFailed);
let mut had_failures = false;
for socket_addr in resolved_addrs {
let connection_future = lightning_net_tokio::connect_outbound(
Arc::clone(&self.peer_manager),
node_id,
socket_addr,
);
res = self.await_connection(connection_future, node_id, addr.clone()).await;
if res.is_ok() {
if had_failures {
log_info!(
self.logger,
"Successfully connected to peer {}@{} via resolved address {} after previous attempts failed.",
node_id, addr, socket_addr
);
}
break;
}
had_failures = true;
log_debug!(
self.logger,
"Failed to connect to peer {}@{} via resolved address {}.",
node_id,
addr,
socket_addr
);
}
res
},
}
}
async fn await_connection<F, CF>(
&self, connection_future: F, node_id: PublicKey, addr: SocketAddress,
) -> Result<(), Error>
where
F: std::future::Future<Output = Option<CF>>,
CF: std::future::Future<Output = ()>,
{
match connection_future.await {
Some(connection_closed_future) => {
let mut connection_closed_future = Box::pin(connection_closed_future);
loop {
tokio::select! {
_ = &mut connection_closed_future => {
log_info!(self.logger, "Peer connection closed: {}@{}", node_id, addr);
break Err(Error::ConnectionFailed);
},
_ = tokio::time::sleep(Duration::from_millis(10)) => {},
};
match self.peer_manager.peer_by_node_id(&node_id) {
Some(_) => break Ok(()),
None => continue,
}
}
},
None => {
log_error!(self.logger, "Failed to connect to peer: {}@{}", node_id, addr);
Err(Error::ConnectionFailed)
},
}
}
fn register_or_subscribe_pending_connection(
&self, node_id: &PublicKey,
) -> Option<tokio::sync::oneshot::Receiver<Result<(), Error>>> {
let mut pending_connections_lock = self.pending_connections.lock().expect("lock");
match pending_connections_lock.entry(*node_id) {
hash_map::Entry::Occupied(mut entry) => {
let (tx, rx) = tokio::sync::oneshot::channel();
entry.get_mut().retain(|subscriber| !subscriber.is_closed());
entry.get_mut().push(tx);
Some(rx)
},
hash_map::Entry::Vacant(entry) => {
entry.insert(Vec::new());
None
},
}
}
fn propagate_result_to_subscribers(&self, node_id: &PublicKey, res: Result<(), Error>) {
// Send the result to any other tasks that might be waiting on it by now.
let mut pending_connections_lock = self.pending_connections.lock().expect("lock");
if let Some(connection_ready_senders) = pending_connections_lock.remove(node_id) {
for sender in connection_ready_senders {
let _ = sender.send(res).map_err(|e| {
debug_assert!(
false,
"Failed to send connection result to subscribers: {:?}",
e
);
log_error!(
self.logger,
"Failed to send connection result to subscribers: {:?}",
e
);
});
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pending_connection_guard_notifies_subscribers_when_abandoned() {
let node_id: PublicKey =
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798".parse().unwrap();
let pending_connections = Mutex::new(HashMap::new());
let (sender, mut receiver) = tokio::sync::oneshot::channel();
pending_connections.lock().expect("lock").insert(node_id, vec![sender]);
let connection_guard = PendingConnectionGuard::new(&pending_connections, node_id);
drop(connection_guard);
assert!(
pending_connections.lock().expect("lock").is_empty(),
"abandoned connection attempt should remove pending state"
);
assert_eq!(receiver.try_recv(), Ok(Err(Error::ConnectionFailed)));
}
}