forked from nanocurrency/nano-node
/
work.cpp
213 lines (199 loc) · 5.72 KB
/
work.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
#include <nano/lib/work.hpp>
#include <nano/lib/blocks.hpp>
#include <nano/node/xorshift.hpp>
#include <future>
bool nano::work_validate (nano::block_hash const & root_a, uint64_t work_a, uint64_t * difficulty_a)
{
auto value (nano::work_value (root_a, work_a));
if (difficulty_a != nullptr)
{
*difficulty_a = value;
}
return value < nano::work_pool::publish_threshold;
}
bool nano::work_validate (nano::block const & block_a, uint64_t * difficulty_a)
{
return work_validate (block_a.root (), block_a.block_work (), difficulty_a);
}
uint64_t nano::work_value (nano::block_hash const & root_a, uint64_t work_a)
{
uint64_t result;
blake2b_state hash;
blake2b_init (&hash, sizeof (result));
blake2b_update (&hash, reinterpret_cast<uint8_t *> (&work_a), sizeof (work_a));
blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&result), sizeof (result));
return result;
}
nano::work_pool::work_pool (unsigned max_threads_a, std::function<boost::optional<uint64_t> (nano::uint256_union const &)> opencl_a) :
ticket (0),
done (false),
opencl (opencl_a)
{
static_assert (ATOMIC_INT_LOCK_FREE == 2, "Atomic int needed");
boost::thread::attributes attrs;
nano::thread_attributes::set (attrs);
auto count (nano::is_test_network ? 1 : std::min (max_threads_a, std::max (1u, boost::thread::hardware_concurrency ())));
for (auto i (0); i < count; ++i)
{
auto thread (boost::thread (attrs, [this, i]() {
nano::thread_role::set (nano::thread_role::name::work);
nano::work_thread_reprioritize ();
loop (i);
}));
threads.push_back (std::move (thread));
}
}
nano::work_pool::~work_pool ()
{
stop ();
for (auto & i : threads)
{
i.join ();
}
}
void nano::work_pool::loop (uint64_t thread)
{
// Quick RNG for work attempts.
xorshift1024star rng;
nano::random_pool.GenerateBlock (reinterpret_cast<uint8_t *> (rng.s.data ()), rng.s.size () * sizeof (decltype (rng.s)::value_type));
uint64_t work;
uint64_t output;
blake2b_state hash;
blake2b_init (&hash, sizeof (output));
std::unique_lock<std::mutex> lock (mutex);
while (!done || !pending.empty ())
{
auto empty (pending.empty ());
if (thread == 0)
{
// Only work thread 0 notifies work observers
work_observers.notify (!empty);
}
if (!empty)
{
auto current_l (pending.front ());
int ticket_l (ticket);
lock.unlock ();
output = 0;
// ticket != ticket_l indicates a different thread found a solution and we should stop
while (ticket == ticket_l && output < current_l.difficulty)
{
// Don't query main memory every iteration in order to reduce memory bus traffic
// All operations here operate on stack memory
// Count iterations down to zero since comparing to zero is easier than comparing to another number
unsigned iteration (256);
while (iteration && output < current_l.difficulty)
{
work = rng.next ();
blake2b_update (&hash, reinterpret_cast<uint8_t *> (&work), sizeof (work));
blake2b_update (&hash, current_l.item.bytes.data (), current_l.item.bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&output), sizeof (output));
blake2b_init (&hash, sizeof (output));
iteration -= 1;
}
}
lock.lock ();
if (ticket == ticket_l)
{
// If the ticket matches what we started with, we're the ones that found the solution
assert (output >= nano::work_pool::publish_threshold);
assert (work_value (current_l.item, work) == output);
// Signal other threads to stop their work next time they check ticket
++ticket;
pending.pop_front ();
lock.unlock ();
current_l.callback (work);
lock.lock ();
}
else
{
// A different thread found a solution
}
}
else
{
// Wait for a work request
producer_condition.wait (lock);
}
}
}
void nano::work_pool::cancel (nano::uint256_union const & root_a)
{
std::lock_guard<std::mutex> lock (mutex);
if (!pending.empty ())
{
if (pending.front ().item == root_a)
{
++ticket;
}
}
pending.remove_if ([&root_a](decltype (pending)::value_type const & item_a) {
bool result;
if (item_a.item == root_a)
{
item_a.callback (boost::none);
result = true;
}
else
{
result = false;
}
return result;
});
}
void nano::work_pool::stop ()
{
{
std::lock_guard<std::mutex> lock (mutex);
done = true;
}
producer_condition.notify_all ();
}
void nano::work_pool::generate (nano::uint256_union const & root_a, std::function<void(boost::optional<uint64_t> const &)> callback_a, uint64_t difficulty_a)
{
assert (!root_a.is_zero ());
boost::optional<uint64_t> result;
if (opencl)
{
result = opencl (root_a);
}
if (!result)
{
{
std::lock_guard<std::mutex> lock (mutex);
pending.push_back ({ root_a, callback_a, difficulty_a });
}
producer_condition.notify_all ();
}
else
{
callback_a (result);
}
}
uint64_t nano::work_pool::generate (nano::uint256_union const & hash_a, uint64_t difficulty_a)
{
std::promise<boost::optional<uint64_t>> work;
generate (hash_a, [&work](boost::optional<uint64_t> work_a) {
work.set_value (work_a);
},
difficulty_a);
auto result (work.get_future ().get ());
return result.value ();
}
namespace nano
{
std::unique_ptr<seq_con_info_component> collect_seq_con_info (work_pool & work_pool, const std::string & name)
{
auto composite = std::make_unique<seq_con_info_composite> (name);
size_t count = 0;
{
std::lock_guard<std::mutex> (work_pool.mutex);
count = work_pool.pending.size ();
}
auto sizeof_element = sizeof (decltype (work_pool.pending)::value_type);
composite->add_component (std::make_unique<seq_con_info_leaf> (seq_con_info{ "pending", count, sizeof_element }));
composite->add_component (collect_seq_con_info (work_pool.work_observers, "work_observers"));
return composite;
}
}