-
Notifications
You must be signed in to change notification settings - Fork 785
/
vote_processor.cpp
244 lines (207 loc) · 6.99 KB
/
vote_processor.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
#include <nano/lib/stats.hpp>
#include <nano/lib/thread_roles.hpp>
#include <nano/lib/timer.hpp>
#include <nano/node/active_transactions.hpp>
#include <nano/node/node_observers.hpp>
#include <nano/node/nodeconfig.hpp>
#include <nano/node/online_reps.hpp>
#include <nano/node/rep_tiers.hpp>
#include <nano/node/repcrawler.hpp>
#include <nano/node/vote_processor.hpp>
#include <nano/secure/common.hpp>
#include <nano/secure/ledger.hpp>
#include <chrono>
using namespace std::chrono_literals;
nano::vote_processor::vote_processor (vote_processor_config const & config_a, nano::active_transactions & active_a, nano::node_observers & observers_a, nano::stats & stats_a, nano::node_flags & flags_a, nano::logger & logger_a, nano::online_reps & online_reps_a, nano::rep_crawler & rep_crawler_a, nano::ledger & ledger_a, nano::network_params & network_params_a, nano::rep_tiers & rep_tiers_a) :
config{ config_a },
active{ active_a },
observers{ observers_a },
stats{ stats_a },
logger{ logger_a },
online_reps{ online_reps_a },
rep_crawler{ rep_crawler_a },
ledger{ ledger_a },
network_params{ network_params_a },
rep_tiers{ rep_tiers_a }
{
queue.max_size_query = [this] (auto const & origin) {
switch (origin.source)
{
case nano::rep_tier::tier_3:
case nano::rep_tier::tier_2:
case nano::rep_tier::tier_1:
return config.max_pr_queue;
case nano::rep_tier::none:
return config.max_non_pr_queue;
}
debug_assert (false);
return size_t{ 0 };
};
queue.priority_query = [this] (auto const & origin) {
switch (origin.source)
{
case nano::rep_tier::tier_3:
return config.pr_priority * config.pr_priority * config.pr_priority;
case nano::rep_tier::tier_2:
return config.pr_priority * config.pr_priority;
case nano::rep_tier::tier_1:
return config.pr_priority;
case nano::rep_tier::none:
return size_t{ 1 };
}
debug_assert (false);
return size_t{ 0 };
};
}
nano::vote_processor::~vote_processor ()
{
debug_assert (threads.empty ());
}
void nano::vote_processor::start ()
{
debug_assert (threads.empty ());
for (int n = 0; n < config.threads; ++n)
{
threads.emplace_back ([this] () {
nano::thread_role::set (nano::thread_role::name::vote_processing);
run ();
});
}
}
void nano::vote_processor::stop ()
{
{
nano::lock_guard<nano::mutex> lock{ mutex };
stopped = true;
}
condition.notify_all ();
for (auto & thread : threads)
{
thread.join ();
}
threads.clear ();
}
bool nano::vote_processor::vote (std::shared_ptr<nano::vote> const & vote, std::shared_ptr<nano::transport::channel> const & channel)
{
debug_assert (channel != nullptr);
auto const tier = rep_tiers.tier (vote->account);
bool added = false;
{
nano::lock_guard<nano::mutex> guard{ mutex };
added = queue.push (vote, { tier, channel });
}
if (added)
{
stats.inc (nano::stat::type::vote_processor, nano::stat::detail::process);
stats.inc (nano::stat::type::vote_processor_tier, to_stat_detail (tier));
condition.notify_one ();
}
else
{
stats.inc (nano::stat::type::vote_processor, nano::stat::detail::overfill);
stats.inc (nano::stat::type::vote_processor_overfill, to_stat_detail (tier));
}
return added;
}
void nano::vote_processor::run ()
{
nano::unique_lock<nano::mutex> lock{ mutex };
while (!stopped)
{
stats.inc (nano::stat::type::vote_processor, nano::stat::detail::loop);
if (!queue.empty ())
{
run_batch (lock);
debug_assert (!lock.owns_lock ());
lock.lock ();
}
else
{
condition.wait (lock, [&] { return stopped || !queue.empty (); });
}
}
}
void nano::vote_processor::run_batch (nano::unique_lock<nano::mutex> & lock)
{
debug_assert (lock.owns_lock ());
debug_assert (!mutex.try_lock ());
debug_assert (!queue.empty ());
nano::timer<std::chrono::milliseconds> timer;
timer.start ();
auto batch = queue.next_batch (config.batch_size);
lock.unlock ();
for (auto const & [vote, origin] : batch)
{
vote_blocking (vote, origin.channel);
}
total_processed += batch.size ();
if (batch.size () == config.batch_size && timer.stop () > 100ms)
{
logger.debug (nano::log::type::vote_processor, "Processed {} votes in {} milliseconds (rate of {} votes per second)",
batch.size (),
timer.value ().count (),
((batch.size () * 1000ULL) / timer.value ().count ()));
}
}
nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr<nano::vote> const & vote, std::shared_ptr<nano::transport::channel> const & channel)
{
auto result = nano::vote_code::invalid;
if (!vote->validate ()) // false => valid vote
{
auto vote_results = active.vote (vote);
// Aggregate results for individual hashes
bool replay = false;
bool processed = false;
for (auto const & [hash, hash_result] : vote_results)
{
replay |= (hash_result == nano::vote_code::replay);
processed |= (hash_result == nano::vote_code::vote);
}
result = replay ? nano::vote_code::replay : (processed ? nano::vote_code::vote : nano::vote_code::indeterminate);
observers.vote.notify (vote, channel, result);
}
stats.inc (nano::stat::type::vote, to_stat_detail (result));
logger.trace (nano::log::type::vote_processor, nano::log::detail::vote_processed,
nano::log::arg{ "vote", vote },
nano::log::arg{ "result", result });
return result;
}
std::size_t nano::vote_processor::size () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return queue.size ();
}
bool nano::vote_processor::empty () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return queue.empty ();
}
std::unique_ptr<nano::container_info_component> nano::vote_processor::collect_container_info (std::string const & name) const
{
nano::lock_guard<nano::mutex> guard{ mutex };
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "votes", queue.size (), sizeof (decltype (queue)::value_type) }));
composite->add_component (queue.collect_container_info ("queue"));
return composite;
}
/*
* vote_processor_config
*/
nano::error nano::vote_processor_config::serialize (nano::tomlconfig & toml) const
{
toml.put ("max_pr_queue", max_pr_queue, "Maximum number of votes to queue from principal representatives. \ntype:uint64");
toml.put ("max_non_pr_queue", max_non_pr_queue, "Maximum number of votes to queue from non-principal representatives. \ntype:uint64");
toml.put ("pr_priority", pr_priority, "Priority for votes from principal representatives. Higher priority gets processed more frequently. Non-principal representatives have a baseline priority of 1. \ntype:uint64");
toml.put ("threads", threads, "Number of threads to use for processing votes. \ntype:uint64");
toml.put ("batch_size", batch_size, "Maximum number of votes to process in a single batch. \ntype:uint64");
return toml.get_error ();
}
nano::error nano::vote_processor_config::deserialize (nano::tomlconfig & toml)
{
toml.get ("max_pr_queue", max_pr_queue);
toml.get ("max_non_pr_queue", max_non_pr_queue);
toml.get ("pr_priority", pr_priority);
toml.get ("threads", threads);
toml.get ("batch_size", batch_size);
return toml.get_error ();
}