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
//! Candidate peer selection for outbound connections.
//!
//! The crawler in [`crawl_and_dial`][crate::peer_set::initialize] manages
//! outbound peer connection attempts using the services in this module.
//! Successful connections become peers in the
//! [`PeerSet`](super::set::PeerSet).
//!
//! Candidate selection divides the set of all possible outbound peers into
//! disjoint subsets, using the [`PeerAddrState`](crate::PeerAddrState):
//!
//! 1. [`Responded`] peers, which we have had an outbound connection to.
//! 2. [`NeverAttemptedGossiped`] peers, which we learned about from other peers
//! but have never connected to. This includes gossiped peers, DNS seeder peers,
//! cached peers, canonical addresses from the [`Version`] messages of inbound
//! and outbound connections, and remote IP addresses of inbound connections.
//! 3. [`Failed`] peers, which failed a connection attempt, or had an error
//! during an outbound connection.
//! 4. [`AttemptPending`] peers, which we've recently queued for a connection.
//!
//! Never attempted peers are always available for connection.
//!
//! If a peer's attempted, responded, or failure time is recent
//! (within the liveness limit), we avoid reconnecting to it.
//! Otherwise, we assume that it has disconnected or hung,
//! and attempt reconnection.
//!
//! ```ascii,no_run
//! ┌──────────────────┐
//! │ Config / DNS │
//! ┌───────────│ Seed │───────────┐
//! │ │ Addresses │ │
//! │ └──────────────────┘ │
//! │ │ untrusted_last_seen │
//! │ │ is unknown │
//! ▼ │ ▼
//! ┌──────────────────┐ │ ┌──────────────────┐
//! │ Handshake │ │ │ Peer Set │
//! │ Canonical │──────────┼──────────│ Gossiped │
//! │ Addresses │ │ │ Addresses │
//! └──────────────────┘ │ └──────────────────┘
//! untrusted_last_seen │ provides
//! set to now │ untrusted_last_seen
//! ▼
//! Λ if attempted, responded, or failed:
//! ╱ ╲ ignore gossiped info
//! ▕ ▏ otherwise, if never attempted:
//! ╲ ╱ skip updates to existing fields
//! V
//! ┌───────────────────────────────┼───────────────────────────────┐
//! │ AddressBook │ │
//! │ disjoint `PeerAddrState`s ▼ │
//! │ ┌─────────────┐ ┌─────────────────────────┐ ┌─────────────┐ │
//! │ │ `Responded` │ │`NeverAttemptedGossiped` │ │ `Failed` │ │
//! ┌┼▶│ Peers │ │ Peers │ │ Peers │◀┼┐
//! ││ └─────────────┘ └─────────────────────────┘ └─────────────┘ ││
//! ││ │ │ │ ││
//! ││ #1 oldest_first #2 newest_first #3 oldest_first ││
//! ││ ├──────────────────────┴──────────────────────┘ ││
//! ││ ▼ ││
//! ││ Λ ││
//! ││ ╱ ╲ filter by ││
//! ││ ▕ ▏ is_ready_for_connection_attempt ││
//! ││ ╲ ╱ to remove recent `Responded`, ││
//! ││ V `AttemptPending`, and `Failed` peers ││
//! ││ │ ││
//! ││ │ try outbound connection, ││
//! ││ ▼ update last_attempt to now() ││
//! ││┌────────────────┐ ││
//! │││`AttemptPending`│ ││
//! │││ Peers │ ││
//! ││└────────────────┘ ││
//! │└────────┼──────────────────────────────────────────────────────┘│
//! │ ▼ │
//! │ Λ │
//! │ ╱ ╲ │
//! │ ▕ ▏─────────────────────────────────────────────────────┘
//! │ ╲ ╱ connection failed, update last_failure to now()
//! │ V
//! │ │
//! │ │ connection succeeded
//! │ ▼
//! │ ┌────────────┐
//! │ │ send │
//! │ │peer::Client│
//! │ │to Discover │
//! │ └────────────┘
//! │ │
//! │ ▼
//! │┌───────────────────────────────────────┐
//! ││ when connection succeeds, and every │
//! ││ time we receive a peer heartbeat: │
//! └│ * update state to `Responded` │
//! │ * update last_response to now() │
//! └───────────────────────────────────────┘
//! ```
//!
//! [`Responded`]: crate::PeerAddrState::Responded
//! [`Version`]: crate::protocol::external::types::Version
//! [`NeverAttemptedGossiped`]: crate::PeerAddrState::NeverAttemptedGossiped
//! [`Failed`]: crate::PeerAddrState::Failed
//! [`AttemptPending`]: crate::PeerAddrState::AttemptPending
//
// TODO:
// * show all possible transitions between Attempt/Responded/Failed,
// except Failed -> Responded is invalid, must go through Attempt
use FutureExt;
use ;
use crate::;
use CrawlFanout;
pub use ;
pub use ;
/// The service that chooses the next reconnection candidate.
///
/// # Security
///
/// Rate-limited so new outbound connections are started at least
/// [`MIN_OUTBOUND_PEER_CONNECTION_INTERVAL`][constants::MIN_OUTBOUND_PEER_CONNECTION_INTERVAL]
/// apart. The rate limit is only charged when a candidate is actually
/// yielded: an empty address book returns `None` immediately.
///
/// Clones share the same rate limit.
pub type NextPeerService = ;
/// Builds the candidate selection services used by the crawler:
/// a [`NextPeerService`] and a [`CrawlService`].
///
/// Uses `address_book_service` to choose candidates and store crawled
/// addresses, and `peer_service` to crawl the network for more addresses.
pub
/// Returns the next candidate for a connection attempt, if any are available.
///
/// Returns peers in reconnection order, based on
/// [`AddressBook::reconnection_peers`](crate::AddressBook::reconnection_peers).
///
/// Skips peers that have recently been active, attempted, or failed.
///
/// ## Correctness
///
/// `AttemptPending` peers will become [`Responded`] if they respond, or
/// become `Failed` if they time out or provide a bad response.
///
/// Live [`Responded`] peers will stay live if they keep responding, or
/// become a reconnection candidate if they stop responding.
///
/// ## Security
///
/// Zebra resists distributed denial of service attacks by making sure that
/// new peer connections are initiated at least
/// [`MIN_OUTBOUND_PEER_CONNECTION_INTERVAL`][constants::MIN_OUTBOUND_PEER_CONNECTION_INTERVAL]
/// apart. If a peer was recently provided, then this future will sleep
/// until the rate-limit has passed.
///
/// [`Responded`]: crate::PeerAddrState::Responded
pub async
/// Returns the number of candidate peers that are currently ready for a
/// connection attempt, as selected by [`next_reconnect_peer`].
///
/// The returned count is a snapshot: candidates can become ready or be
/// attempted by other tasks immediately afterwards.
pub async