forked from libbitcoin/libbitcoin-node
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchaser_validate.cpp
More file actions
371 lines (315 loc) · 10.5 KB
/
chaser_validate.cpp
File metadata and controls
371 lines (315 loc) · 10.5 KB
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
364
365
366
367
368
369
370
371
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/node/chasers/chaser_validate.hpp>
#include <atomic>
#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/full_node.hpp>
namespace libbitcoin {
namespace node {
#define CLASS chaser_validate
using namespace system;
using namespace database;
using namespace std::placeholders;
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
// Independent threadpool and strand (base class strand uses network pool).
chaser_validate::chaser_validate(full_node& node) NOEXCEPT
: chaser(node),
threadpool_(node.node_settings().threads_(),
node.node_settings().thread_priority_()),
independent_strand_(threadpool_.service().get_executor()),
subsidy_interval_(node.system_settings().subsidy_interval_blocks),
initial_subsidy_(node.system_settings().initial_subsidy()),
maximum_backlog_(node.node_settings().maximum_concurrency_()),
node_witness_(node.network_settings().witness_node()),
defer_(node.node_settings().defer_validation),
filter_(!defer_ && node.archive().filter_enabled())
{
}
code chaser_validate::start() NOEXCEPT
{
if (!node_settings().headers_first)
return error::success;
const auto& query = archive();
set_position(query.get_fork());
SUBSCRIBE_EVENTS(handle_event, _1, _2, _3);
return error::success;
}
bool chaser_validate::handle_event(const code&, chase event_,
event_value value) NOEXCEPT
{
if (closed())
return false;
// Stop generating query during suspension.
// Incoming events may already be flushed to the strand at this point.
if (suspended())
return true;
switch (event_)
{
case chase::resume:
case chase::start:
case chase::bump:
{
POST(do_bump, height_t{});
break;
}
case chase::checked:
{
// value is checked block height.
BC_ASSERT(std::holds_alternative<height_t>(value));
POST(do_checked, std::get<height_t>(value));
break;
}
case chase::regressed:
case chase::disorganized:
{
// value is regression branch_point.
BC_ASSERT(std::holds_alternative<height_t>(value));
POST(do_regressed, std::get<height_t>(value));
break;
}
case chase::stop:
{
return false;
}
default:
{
break;
}
}
return true;
}
// Track downloaded
// ----------------------------------------------------------------------------
void chaser_validate::do_regressed(height_t branch_point) NOEXCEPT
{
BC_ASSERT(stranded());
if (branch_point >= position())
return;
set_position(branch_point);
}
void chaser_validate::do_checked(height_t height) NOEXCEPT
{
BC_ASSERT(stranded());
// Cannot validate next block until all previous blocks are archived.
if (height == add1(position()))
do_bumped(height);
}
void chaser_validate::do_bump(height_t) NOEXCEPT
{
BC_ASSERT(stranded());
const auto height = add1(position());
if (archive().is_validateable(height))
do_bumped(height);
}
// Validate (cancellable)
// ----------------------------------------------------------------------------
void chaser_validate::do_bumped(height_t height) NOEXCEPT
{
BC_ASSERT(stranded());
const auto& query = archive();
// Bypass until next event if validation backlog is full.
// Stop when suspended as write error des not terminate asynchronous loop.
while ((backlog_ < maximum_backlog_) && !closed() && !suspended())
{
const auto link = query.to_candidate(height);
const auto ec = query.get_block_state(link);
// Must exit on unassociated so they are not set valid in bypass.
// Given height-based iteration, any block state may be enountered.
if (ec == database::error::unassociated)
return;
const auto bypass = defer_ || is_under_checkpoint(height) ||
query.is_milestone(link);
switch (ec.value())
{
case database::error::unvalidated:
case database::error::unknown_state:
{
if (!bypass || filter_)
post_block(link, bypass);
else
complete_block(error::success, link, height, true);
break;
}
case database::error::block_valid:
case database::error::block_confirmable:
{
complete_block(error::success, link, height, true);
break;
}
case database::error::block_unconfirmable:
{
return;
}
////case database::error::unassociated
default:
{
fault(error::validate1);
return;
}
}
// All posted validations must complete or this is invalid.
// So posted validations continue despite network suspension.
set_position(height++);
}
}
void chaser_validate::post_block(const header_link& link,
bool bypass) NOEXCEPT
{
BC_ASSERT(stranded());
backlog_.fetch_add(one, std::memory_order_relaxed);
PARALLEL(validate_block, link, bypass);
}
// Unstranded (concurrent by block)
// ----------------------------------------------------------------------------
void chaser_validate::validate_block(const header_link& link,
bool bypass) NOEXCEPT
{
if (closed())
return;
code ec{};
chain::context ctx{};
auto& query = archive();
// TODO: implement allocator parameter resulting in full allocation to
// shared_ptr<block>, to optimize deallocate (12% of milestone/filter).
const auto block = query.get_block(link, node_witness_);
if (!block)
{
ec = error::validate2;
}
else if (!query.get_context(ctx, link))
{
ec = error::validate3;
}
else if ((ec = populate(bypass, *block, ctx)))
{
if (!query.set_block_unconfirmable(link))
ec = error::validate4;
}
else if ((ec = validate(bypass, *block, link, ctx)))
{
if (!query.set_block_unconfirmable(link))
ec = error::validate5;
}
complete_block(ec, link, ctx.height, bypass);
// Prevent stall by posting internal event, avoiding external handlers.
if (is_one(backlog_.fetch_sub(one, std::memory_order_relaxed)))
handle_event(error::success, chase::bump, height_t{});
}
code chaser_validate::populate(bool bypass, const chain::block& block,
const chain::context& ctx) NOEXCEPT
{
const auto& query = archive();
if (bypass)
{
// Populating for filters only (no validation metadata required).
block.populate();
if (!query.populate_without_metadata(block))
return system::error::missing_previous_output;
}
else
{
// Internal maturity and time locks are verified here because they are
// the only necessary confirmation checks for internal spends.
if (const auto ec = block.populate_with_metadata(ctx))
return ec;
// Metadata identifies internal spends alowing confirmation bypass.
if (!query.populate_with_metadata(block))
return system::error::missing_previous_output;
}
return error::success;
}
code chaser_validate::validate(bool bypass, const chain::block& block,
const database::header_link& link, const chain::context& ctx) NOEXCEPT
{
auto& query = archive();
if (!bypass)
{
code ec{};
if ((ec = block.accept(ctx, subsidy_interval_, initial_subsidy_)))
return ec;
if ((ec = block.connect(ctx)))
return ec;
// Prevouts optimize confirmation.
if (!query.set_prevouts(link, block))
return error::validate6;
}
if (!query.set_filter_body(link, block))
return error::validate7;
// Valid must be set after set_prevouts and set_filter_body.
if (!bypass && !query.set_block_valid(link))
return error::validate8;
return error::success;
}
// May be either concurrent or stranded.
void chaser_validate::complete_block(const code& ec, const header_link& link,
size_t height, bool bypass) NOEXCEPT
{
if (ec)
{
// Node errors are fatal.
if (node::error::error_category::contains(ec))
{
LOGF("Fault validating [" << height << "] " << ec.message());
fault(ec);
return;
}
// INVALID BLOCK (not a fault)
notify(ec, chase::unvalid, link);
fire(events::block_unconfirmable, height);
LOGR("Invalid block [" << height << "] " << ec.message());
return;
}
// VALID BLOCK
// Under deferral there is no state change, but downloads will stall unless
// the window is closed out, so notify the check chaser of the increment.
notify(ec, chase::valid, possible_wide_cast<height_t>(height));
if (!defer_)
{
fire(events::block_validated, height);
LOGV("Block validated: " << height << (bypass ? " (bypass)" : ""));
}
}
// Overrides due to independent priority thread pool
// ----------------------------------------------------------------------------
void chaser_validate::stopping(const code& ec) NOEXCEPT
{
// Stop threadpool keep-alive, all work must self-terminate to affect join.
threadpool_.stop();
chaser::stopping(ec);
}
void chaser_validate::stop() NOEXCEPT
{
if (!threadpool_.join())
{
BC_ASSERT_MSG(false, "failed to join threadpool");
std::abort();
}
}
network::asio::strand& chaser_validate::strand() NOEXCEPT
{
return independent_strand_;
}
bool chaser_validate::stranded() const NOEXCEPT
{
return independent_strand_.running_in_this_thread();
}
BC_POP_WARNING()
} // namespace node
} // namespace libbitcoin