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
use crate::DiscoveryError;
use diem_config::config::PeerSet;
use diem_time_service::{Interval, TimeService, TimeServiceTrait};
use futures::Stream;
use std::{
path::{Path, PathBuf},
pin::Pin,
task::{Context, Poll},
time::Duration,
};
pub struct FileStream {
file_path: PathBuf,
interval: Pin<Box<Interval>>,
}
impl FileStream {
pub(crate) fn new(
file_path: &Path,
interval_duration: Duration,
time_service: TimeService,
) -> Self {
FileStream {
file_path: file_path.to_path_buf(),
interval: Box::pin(time_service.interval(interval_duration)),
}
}
}
impl Stream for FileStream {
type Item = Result<PeerSet, DiscoveryError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
futures::ready!(self.interval.as_mut().poll_next(cx));
Poll::Ready(Some(match load_file(self.file_path.as_path()) {
Ok(peers) => Ok(peers),
Err(error) => Err(error),
}))
}
}
fn load_file(path: &Path) -> Result<PeerSet, DiscoveryError> {
let contents = std::fs::read_to_string(path).map_err(DiscoveryError::IO)?;
serde_yaml::from_str(&contents).map_err(|err| DiscoveryError::Parsing(err.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::DiscoveryChangeListener;
use channel::Receiver;
use diem_config::{
config::{Peer, PeerRole},
network_id::NetworkContext,
};
use diem_temppath::TempPath;
use diem_types::{network_address::NetworkAddress, PeerId};
use futures::StreamExt;
use network::connectivity_manager::{ConnectivityRequest, DiscoverySource};
use std::{collections::HashSet, str::FromStr, sync::Arc};
use tokio::time::sleep;
fn create_listener(path: Arc<TempPath>) -> Receiver<ConnectivityRequest> {
let check_interval = Duration::from_millis(5);
let time_service = TimeService::real();
let (conn_mgr_reqs_tx, conn_mgr_reqs_rx) =
channel::new(1, &network::counters::PENDING_CONNECTIVITY_MANAGER_REQUESTS);
let listener_task = async move {
let listener = DiscoveryChangeListener::file(
NetworkContext::mock(),
conn_mgr_reqs_tx,
path.as_ref().as_ref(),
check_interval,
time_service,
);
Box::pin(listener).run().await
};
tokio::task::spawn(listener_task);
conn_mgr_reqs_rx
}
fn write_peer_set(peers: &PeerSet, path: &Path) {
let file_contents = serde_yaml::to_vec(peers).unwrap();
std::fs::write(path, file_contents).unwrap();
}
#[tokio::test]
async fn test_file_listener() {
let path = TempPath::new();
path.create_as_file().unwrap();
let path = Arc::new(path);
let peers: PeerSet = PeerSet::new();
write_peer_set(&peers, path.as_ref().as_ref());
let mut conn_mgr_reqs_rx = create_listener(path.clone());
if let Some(ConnectivityRequest::UpdateDiscoveredPeers(
DiscoverySource::File,
actual_peers,
)) = conn_mgr_reqs_rx.next().await
{
assert_eq!(peers, actual_peers)
} else {
panic!("No message sent by discovery")
}
let mut peers = PeerSet::new();
let addr = NetworkAddress::from_str("/ip4/1.2.3.4/tcp/6180/ln-noise-ik/080e287879c918794170e258bfaddd75acac5b3e350419044655e4983a487120/ln-handshake/0").unwrap();
let key = addr.find_noise_proto().unwrap();
let addrs = vec![addr];
let mut keys = HashSet::new();
keys.insert(key);
peers.insert(
PeerId::random(),
Peer::new(addrs, keys, PeerRole::Downstream),
);
let file_contents = serde_yaml::to_vec(&peers).unwrap();
std::fs::write(path.as_ref(), file_contents).unwrap();
while conn_mgr_reqs_rx.next().await.is_none() {}
if let Some(ConnectivityRequest::UpdateDiscoveredPeers(
DiscoverySource::File,
actual_peers,
)) = conn_mgr_reqs_rx.next().await
{
assert_eq!(peers, actual_peers)
} else {
panic!("No message sent by discovery")
}
}
#[tokio::test]
async fn test_no_file() {
let path = TempPath::new();
path.create_as_file().unwrap();
let path = Arc::new(path);
let mut conn_mgr_reqs_rx = create_listener(path.clone());
let writer_task = async move {
sleep(Duration::from_secs(1)).await;
let peers: PeerSet = PeerSet::new();
write_peer_set(&peers, path.as_ref().as_ref());
};
tokio::task::spawn(writer_task);
if let Some(ConnectivityRequest::UpdateDiscoveredPeers(
DiscoverySource::File,
actual_peers,
)) = conn_mgr_reqs_rx.next().await
{
assert_eq!(PeerSet::new(), actual_peers)
} else {
panic!("No message sent by discovery")
}
}
}