/
repcrawler.cpp
363 lines (337 loc) · 11.5 KB
/
repcrawler.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <nano/node/node.hpp>
#include <nano/node/repcrawler.hpp>
#include <boost/format.hpp>
nano::rep_crawler::rep_crawler (nano::node & node_a) :
node (node_a)
{
if (!node.flags.disable_rep_crawler)
{
node.observers.endpoint.add ([this] (std::shared_ptr<nano::transport::channel> const & channel_a) {
this->query (channel_a);
});
}
}
void nano::rep_crawler::remove (nano::block_hash const & hash_a)
{
nano::lock_guard<nano::mutex> lock (active_mutex);
active.erase (hash_a);
}
void nano::rep_crawler::start ()
{
ongoing_crawl ();
}
void nano::rep_crawler::validate ()
{
decltype (responses) responses_l;
{
nano::lock_guard<nano::mutex> lock (active_mutex);
responses_l.swap (responses);
}
auto minimum = node.minimum_principal_weight ();
for (auto const & i : responses_l)
{
auto & vote = i.second;
auto & channel = i.first;
debug_assert (channel != nullptr);
if (channel->get_type () != nano::transport::transport_type::loopback)
{
nano::uint128_t rep_weight = node.ledger.weight (vote->account);
if (rep_weight > minimum)
{
auto updated_or_inserted = false;
nano::unique_lock<nano::mutex> lock (probable_reps_mutex);
auto existing (probable_reps.find (vote->account));
if (existing != probable_reps.end ())
{
probable_reps.modify (existing, [rep_weight, &updated_or_inserted, &vote, &channel] (nano::representative & info) {
info.last_response = std::chrono::steady_clock::now ();
// Update if representative channel was changed
if (info.channel->get_endpoint () != channel->get_endpoint ())
{
debug_assert (info.account == vote->account);
updated_or_inserted = true;
info.weight = rep_weight;
info.channel = channel;
}
});
}
else
{
probable_reps.emplace (nano::representative (vote->account, rep_weight, channel));
updated_or_inserted = true;
}
lock.unlock ();
if (updated_or_inserted)
{
node.logger.try_log (boost::str (boost::format ("Found a representative at %1%") % channel->to_string ()));
}
}
}
}
}
void nano::rep_crawler::ongoing_crawl ()
{
auto now (std::chrono::steady_clock::now ());
auto total_weight_l (total_weight ());
cleanup_reps ();
update_weights ();
validate ();
query (get_crawl_targets (total_weight_l));
auto sufficient_weight (total_weight_l > node.online_reps.delta ());
// If online weight drops below minimum, reach out to preconfigured peers
if (!sufficient_weight)
{
node.keepalive_preconfigured (node.config.preconfigured_peers);
}
// Reduce crawl frequency when there's enough total peer weight
unsigned next_run_ms = node.network_params.network.is_dev_network () ? 100 : sufficient_weight ? 7000 : 3000;
std::weak_ptr<nano::node> node_w (node.shared ());
node.workers.add_timed_task (now + std::chrono::milliseconds (next_run_ms), [node_w, this] () {
if (auto node_l = node_w.lock ())
{
this->ongoing_crawl ();
}
});
}
std::vector<std::shared_ptr<nano::transport::channel>> nano::rep_crawler::get_crawl_targets (nano::uint128_t total_weight_a)
{
constexpr size_t conservative_count = 10;
constexpr size_t aggressive_count = 40;
// Crawl more aggressively if we lack sufficient total peer weight.
bool sufficient_weight (total_weight_a > node.online_reps.delta ());
uint16_t required_peer_count = sufficient_weight ? conservative_count : aggressive_count;
// Add random peers. We do this even if we have enough weight, in order to pick up reps
// that didn't respond when first observed. If the current total weight isn't sufficient, this
// will be more aggressive. When the node first starts, the rep container is empty and all
// endpoints will originate from random peers.
required_peer_count += required_peer_count / 2;
// The rest of the endpoints are picked randomly
auto random_peers (node.network.random_set (required_peer_count, 0, true)); // Include channels with ephemeral remote ports
std::vector<std::shared_ptr<nano::transport::channel>> result;
result.insert (result.end (), random_peers.begin (), random_peers.end ());
return result;
}
void nano::rep_crawler::query (std::vector<std::shared_ptr<nano::transport::channel>> const & channels_a)
{
auto transaction (node.store.tx_begin_read ());
auto hash_root (node.ledger.hash_root_random (transaction));
{
nano::lock_guard<nano::mutex> lock (active_mutex);
// Don't send same block multiple times in tests
if (node.network_params.network.is_dev_network ())
{
for (auto i (0); active.count (hash_root.first) != 0 && i < 4; ++i)
{
hash_root = node.ledger.hash_root_random (transaction);
}
}
active.insert (hash_root.first);
}
if (!channels_a.empty ())
{
node.active.erase_recently_confirmed (hash_root.first);
}
for (auto i (channels_a.begin ()), n (channels_a.end ()); i != n; ++i)
{
debug_assert (*i != nullptr);
on_rep_request (*i);
node.network.send_confirm_req (*i, hash_root);
}
// A representative must respond with a vote within the deadline
std::weak_ptr<nano::node> node_w (node.shared ());
node.workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::seconds (5), [node_w, hash = hash_root.first] () {
if (auto node_l = node_w.lock ())
{
auto target_finished_processed (node_l->vote_processor.total_processed + node_l->vote_processor.size ());
node_l->rep_crawler.throttled_remove (hash, target_finished_processed);
}
});
}
void nano::rep_crawler::query (std::shared_ptr<nano::transport::channel> const & channel_a)
{
std::vector<std::shared_ptr<nano::transport::channel>> peers;
peers.emplace_back (channel_a);
query (peers);
}
void nano::rep_crawler::throttled_remove (nano::block_hash const & hash_a, uint64_t const target_finished_processed)
{
if (node.vote_processor.total_processed >= target_finished_processed)
{
remove (hash_a);
}
else
{
std::weak_ptr<nano::node> node_w (node.shared ());
node.workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::seconds (5), [node_w, hash_a, target_finished_processed] () {
if (auto node_l = node_w.lock ())
{
node_l->rep_crawler.throttled_remove (hash_a, target_finished_processed);
}
});
}
}
bool nano::rep_crawler::is_pr (nano::transport::channel const & channel_a) const
{
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
auto existing = probable_reps.get<tag_channel_ref> ().find (channel_a);
bool result = false;
if (existing != probable_reps.get<tag_channel_ref> ().end ())
{
result = existing->weight > node.minimum_principal_weight ();
}
return result;
}
bool nano::rep_crawler::response (std::shared_ptr<nano::transport::channel> const & channel_a, std::shared_ptr<nano::vote> const & vote_a)
{
bool error = true;
nano::lock_guard<nano::mutex> lock (active_mutex);
for (auto i = vote_a->begin (), n = vote_a->end (); i != n; ++i)
{
if (active.count (*i) != 0)
{
responses.emplace_back (channel_a, vote_a);
error = false;
break;
}
}
return error;
}
nano::uint128_t nano::rep_crawler::total_weight () const
{
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
nano::uint128_t result (0);
for (auto i (probable_reps.get<tag_weight> ().begin ()), n (probable_reps.get<tag_weight> ().end ()); i != n; ++i)
{
auto weight (i->weight.number ());
if (weight > 0)
{
result = result + weight;
}
else
{
break;
}
}
return result;
}
void nano::rep_crawler::on_rep_request (std::shared_ptr<nano::transport::channel> const & channel_a)
{
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
if (channel_a->get_tcp_endpoint ().address () != boost::asio::ip::address_v6::any ())
{
probably_rep_t::index<tag_channel_ref>::type & channel_ref_index = probable_reps.get<tag_channel_ref> ();
// Find and update the timestamp on all reps available on the endpoint (a single host may have multiple reps)
auto itr_pair = channel_ref_index.equal_range (*channel_a);
for (; itr_pair.first != itr_pair.second; itr_pair.first++)
{
channel_ref_index.modify (itr_pair.first, [] (nano::representative & value_a) {
value_a.last_request = std::chrono::steady_clock::now ();
});
}
}
}
void nano::rep_crawler::cleanup_reps ()
{
std::vector<std::shared_ptr<nano::transport::channel>> channels;
{
// Check known rep channels
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
auto iterator (probable_reps.get<tag_last_request> ().begin ());
while (iterator != probable_reps.get<tag_last_request> ().end ())
{
if (iterator->channel->get_tcp_endpoint ().address () != boost::asio::ip::address_v6::any ())
{
channels.push_back (iterator->channel);
++iterator;
}
else
{
// Remove reps with closed channels
iterator = probable_reps.get<tag_last_request> ().erase (iterator);
}
}
}
// Remove reps with inactive channels
for (auto const & i : channels)
{
bool equal (false);
if (i->get_type () == nano::transport::transport_type::tcp)
{
auto find_channel (node.network.tcp_channels.find_channel (i->get_tcp_endpoint ()));
if (find_channel != nullptr && *find_channel == *static_cast<nano::transport::channel_tcp *> (i.get ()))
{
equal = true;
}
}
else if (i->get_type () == nano::transport::transport_type::udp)
{
auto find_channel (node.network.udp_channels.channel (i->get_endpoint ()));
if (find_channel != nullptr && *find_channel == *static_cast<nano::transport::channel_udp *> (i.get ()))
{
equal = true;
}
}
if (!equal)
{
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
probable_reps.get<tag_channel_ref> ().erase (*i);
}
}
}
void nano::rep_crawler::update_weights ()
{
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
for (auto i (probable_reps.get<tag_last_request> ().begin ()), n (probable_reps.get<tag_last_request> ().end ()); i != n;)
{
auto weight (node.ledger.weight (i->account));
if (weight > 0)
{
if (i->weight.number () != weight)
{
probable_reps.get<tag_last_request> ().modify (i, [weight] (nano::representative & info) {
info.weight = weight;
});
}
++i;
}
else
{
// Erase non representatives
i = probable_reps.get<tag_last_request> ().erase (i);
}
}
}
std::vector<nano::representative> nano::rep_crawler::representatives (size_t count_a, nano::uint128_t const weight_a, boost::optional<decltype (nano::protocol_constants::protocol_version)> const & opt_version_min_a)
{
auto version_min (opt_version_min_a.value_or (node.network_params.protocol.protocol_version_min ()));
std::vector<representative> result;
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
for (auto i (probable_reps.get<tag_weight> ().begin ()), n (probable_reps.get<tag_weight> ().end ()); i != n && result.size () < count_a; ++i)
{
if (i->weight > weight_a && i->channel->get_network_version () >= version_min)
{
result.push_back (*i);
}
}
return result;
}
std::vector<nano::representative> nano::rep_crawler::principal_representatives (size_t count_a, boost::optional<decltype (nano::protocol_constants::protocol_version)> const & opt_version_min_a)
{
return representatives (count_a, node.minimum_principal_weight (), opt_version_min_a);
}
std::vector<std::shared_ptr<nano::transport::channel>> nano::rep_crawler::representative_endpoints (size_t count_a)
{
std::vector<std::shared_ptr<nano::transport::channel>> result;
auto reps (representatives (count_a));
for (auto const & rep : reps)
{
result.push_back (rep.channel);
}
return result;
}
/** Total number of representatives */
size_t nano::rep_crawler::representative_count ()
{
nano::lock_guard<nano::mutex> lock (probable_reps_mutex);
return probable_reps.size ();
}