• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-node / 44690193668

20 Nov 2026 06:46AM UTC coverage: 90.659% (-0.2%) from 90.883%
44690193668

push

github

web-flow
Merge pull request #1275 from evoskuil/master

Refactor sources, remove dead code, regen, style.

2 of 2 new or added lines in 1 file covered. (100.0%)

206 existing lines in 12 files now uncovered.

4523 of 4989 relevant lines covered (90.66%)

4834.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.34
/src/full_node.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/node/full_node.hpp>
20

21
#include <bitcoin/node/chasers/chasers.hpp>
22
#include <bitcoin/node/define.hpp>
23
#include <bitcoin/node/sessions/sessions.hpp>
24

25
namespace libbitcoin {
26
namespace node {
27

28
using namespace system;
29
using namespace database;
30
using namespace network;
31
using namespace std::placeholders;
32

33
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
34

35
// net::strand() is safe to call from constructor (non-virtual).
36
full_node::full_node(query& query, const configuration& configuration,
448 ✔
37
    const logger& log) NOEXCEPT
448 ✔
38
  : net(configuration.network, log, configuration.node.services_required(),
448 ✔
39
        configuration.node.services_provided()),
40
    config_(configuration),
448 ✔
41
    start_time_(zulu_time()),
896 ✔
42
    query_(query),
448 ✔
43
    chaser_header_(*this),
448 ✔
44
    chaser_download_(*this),
448 ✔
45
    chaser_validate_(*this),
448 ✔
46
    chaser_confirm_(*this),
448 ✔
47
    chaser_transaction_(*this),
448 ✔
48
    chaser_template_(*this),
448 ✔
49
    chaser_snapshot_(*this),
448 ✔
50
    chaser_storage_(*this)
896 ✔
51
{
52
}
448 ✔
53

54
full_node::~full_node() NOEXCEPT
448 ✔
55
{
56
}
448 ✔
57

58
// Sequences.
59
// ----------------------------------------------------------------------------
60

61
void full_node::start(result_handler&& handler) NOEXCEPT
442 ✔
62
{
63
    if (!query_.is_initialized())
442 ✔
64
    {
65
        handler(error::store_uninitialized);
1 ✔
66
        return;
1 ✔
67
    }
68

69
    if (config_.node.provide_filters && !query_.filter_enabled())
441 ✔
70
    {
71
        handler(error::filter_unindexed);
1 ✔
72
        return;
1 ✔
73
    }
74

75
    // Base (net) invokes do_start().
76
    net::start(std::move(handler));
440 ✔
77
}
78

79
void full_node::do_start(const result_handler& handler) NOEXCEPT
373 ✔
80
{
81
    BC_ASSERT(stranded());
373 ✔
82
    code ec{};
373 ✔
83

84
    if (((ec = chaser_header_.start())) ||
373 ✔
85
        ((ec = chaser_download_.start())) ||
372 ✔
86
        ((ec = chaser_validate_.start())) ||
372 ✔
87
        ((ec = chaser_confirm_.start())) ||
372 ✔
88
        ((ec = chaser_transaction_.start())) ||
372 ✔
89
        ((ec = chaser_template_.start())) ||
372 ✔
90
        ((ec = chaser_snapshot_.start())) ||
745 ✔
91
        ((ec = chaser_storage_.start())))
372 ✔
92
    {
93
        handler(ec);
1 ✔
94
        return;
1 ✔
95
    }
96

97
    net::do_start(handler);
372 ✔
98
}
99

100
void full_node::run(result_handler&& handler) NOEXCEPT
431 ✔
101
{
102
    // Base (net) invokes do_run().
103
    net::run(std::move(handler));
431 ✔
104
}
431 ✔
105

106
void full_node::do_run(const result_handler& handler) NOEXCEPT
363 ✔
107
{
108
    BC_ASSERT(stranded());
363 ✔
109

110
    if (closed())
363 ✔
111
    {
112
        handler(network::error::service_stopped);
1 ✔
113
        return;
1 ✔
114
    }
115

116
    // Bump sequential chasers to their starting heights.
117
    // This will kick off lagging validations even if not current.
118
    do_notify(error::success, chases::start{});
362 ✔
119

120
    // Start services after network is running.
121
    net::do_run(handler);
362 ✔
122
}
123

124
void full_node::close() NOEXCEPT
448 ✔
125
{
126
    // Base (net) invokes do_close().
127
    net::close();
448 ✔
128

129
    // Block on chaser stop (including dedicated threadpool joins).
130
    chaser_header_.stop();
448 ✔
131
    chaser_download_.stop();
448 ✔
132
    chaser_validate_.stop();
448 ✔
133
    chaser_confirm_.stop();
448 ✔
134
    chaser_transaction_.stop();
448 ✔
135
    chaser_template_.stop();
448 ✔
136
    chaser_snapshot_.stop();
448 ✔
137
    chaser_storage_.stop();
448 ✔
138
}
448 ✔
139

140
// Base (net) invokes do_close().
141
void full_node::do_close() NOEXCEPT
448 ✔
142
{
143
    BC_ASSERT(stranded());
448 ✔
144

145
    // Initiate chaser stopping (including dedicated threadpools).
146
    chaser_header_.stopping(network::error::service_stopped);
448 ✔
147
    chaser_download_.stopping(network::error::service_stopped);
448 ✔
148
    chaser_validate_.stopping(network::error::service_stopped);
448 ✔
149
    chaser_confirm_.stopping(network::error::service_stopped);
448 ✔
150
    chaser_transaction_.stopping(network::error::service_stopped);
448 ✔
151
    chaser_template_.stopping(network::error::service_stopped);
448 ✔
152
    chaser_snapshot_.stopping(network::error::service_stopped);
448 ✔
153
    chaser_storage_.stopping(network::error::service_stopped);
448 ✔
154

155
    event_subscriber_.stop(network::error::service_stopped, chases::stop{});
448 ✔
156
    net::do_close();
448 ✔
157
}
448 ✔
158

159
// Organizers.
160
// ----------------------------------------------------------------------------
161

162
void full_node::organize(const system::chain::header::cptr& header,
3 ✔
163
    organize_handler&& handler) NOEXCEPT
164
{
165
    chaser_header_.organize(header, std::move(handler));
3 ✔
166
}
3 ✔
167

168
void full_node::organize(const system::chain::header::cptr& header,
6,012 ✔
169
    bool milestone, organize_handler&& handler) NOEXCEPT
170
{
171
    chaser_header_.organize(header, milestone, std::move(handler));
6,012 ✔
172
}
6,012 ✔
173

174
void full_node::organize_compact(const system::chain::header::cptr& header,
10 ✔
175
    organize_handler&& handler) NOEXCEPT
176
{
177
    chaser_header_.organize_compact(header, std::move(handler));
10 ✔
178
}
10 ✔
179

180
void full_node::prioritize(const system::hash_digest& hash,
3 ✔
181
    organize_handler&& handler) NOEXCEPT
182
{
183
    chaser_header_.prioritize(hash, std::move(handler));
3 ✔
184
}
3 ✔
185

186
void full_node::get_minimum_work(work_handler&& handler) NOEXCEPT
77 ✔
187
{
188
    chaser_header_.get_minimum_work(std::move(handler));
77 ✔
189
}
77 ✔
190

191
void full_node::submit(const system::chain::transactions_cptr& txs, bool test,
86 ✔
192
    submit_handler&& handler) NOEXCEPT
193
{
194
    chaser_transaction_.submit(txs, test, std::move(handler));
86 ✔
195
}
86 ✔
196

197
void full_node::submit_announced(
25 ✔
198
    const system::chain::transactions_cptr& txs,
199
    submit_handler&& handler) NOEXCEPT
200
{
201
    chaser_transaction_.submit_announced(txs, std::move(handler));
25 ✔
202
}
25 ✔
203

204
void full_node::submit_compact(const system::chain::transactions_cptr& txs,
9 ✔
205
    const database::tx_links& links, const database::header_link& link,
206
    submit_handler&& handler) NOEXCEPT
207
{
208
    chaser_transaction_.submit_compact(txs, links, link, std::move(handler));
9 ✔
209
}
9 ✔
210

211
bool full_node::archive_block(code& ec,
12 ✔
212
    const system::chain::view::block::cptr& block,
213
    const database::header_link& link, bool strong, bool bypass, size_t height,
214
    bool prune, result_handler&& handler) NOEXCEPT
215
{
216
    return chaser_transaction_.archive_block(ec, block, link, strong, bypass,
12 ✔
217
        height, prune, std::move(handler));
12 ✔
218
}
219

220
void full_node::build_template(template_handler&& handler) NOEXCEPT
35 ✔
221
{
222
    chaser_transaction_.build_template(std::move(handler));
35 ✔
223
}
35 ✔
224

225
void full_node::subscribe_pool(pool_notifier&& handler) NOEXCEPT
6 ✔
226
{
227
    chaser_transaction_.subscribe_pool(std::move(handler));
6 ✔
228
}
6 ✔
229

230
void full_node::get_template(template_handler&& handler) NOEXCEPT
7 ✔
231
{
232
    chaser_template_.get_template(std::move(handler));
7 ✔
233
}
7 ✔
234

235
void full_node::get_hashes(map_handler&& handler) NOEXCEPT
74 ✔
236
{
237
    chaser_download_.get_hashes(std::move(handler));
74 ✔
238
}
74 ✔
239

240
void full_node::put_hashes(const map_ptr& map,
33 ✔
241
    result_handler&& handler) NOEXCEPT
242
{
243
    chaser_download_.put_hashes(map, std::move(handler));
33 ✔
244
}
33 ✔
245

246
void full_node::get_unfilled(header_t link, map_handler&& handler) NOEXCEPT
9 ✔
247
{
248
    chaser_download_.get_unfilled(link, std::move(handler));
9 ✔
249
}
9 ✔
250

251
bool full_node::reserve_compact() NOEXCEPT
8 ✔
252
{
253
    auto count = compact_peers_.load();
254
    while (count < maximum_compact_peers)
8 ✔
255
        if (compact_peers_.compare_exchange_weak(count, add1(count)))
14 ✔
256
            return true;
257

258
    return false;
259
}
260

261
void full_node::release_compact() NOEXCEPT
4 ✔
262
{
263
    --compact_peers_;
4 ✔
264
}
4 ✔
265

266
// Events.
267
// ----------------------------------------------------------------------------
268

269
void full_node::notify(const code& ec, event_value value) NOEXCEPT
6,949 ✔
270
{
271
    network::io::post(strand(),
6,949 ✔
272
        std::bind(&full_node::do_notify,
13,898 ✔
273
            this, ec, value));
6,949 ✔
274
}
6,949 ✔
275

276
// private
277
void full_node::do_notify(const code& ec, event_value value) NOEXCEPT
7,311 ✔
278
{
279
    BC_ASSERT(stranded());
7,311 ✔
280
    event_subscriber_.notify(ec, value);
7,311 ✔
281
}
7,311 ✔
282

283
void full_node::notify_one(object_key key, const code& ec,
632 ✔
284
    event_value value) NOEXCEPT
285
{
286
    network::io::post(strand(),
632 ✔
287
        std::bind(&full_node::do_notify_one,
1,264 ✔
288
            this, key, ec, value));
632 ✔
289
}
632 ✔
290

291
// private
292
void full_node::do_notify_one(object_key key, const code& ec,
632 ✔
293
    event_value value) NOEXCEPT
294
{
295
    BC_ASSERT(stranded());
632 ✔
296
    event_subscriber_.notify_one(key, ec, value);
632 ✔
297
}
632 ✔
298

299
object_key full_node::subscribe_chase(event_notifier&& handler) NOEXCEPT
2,695 ✔
300
{
301
    BC_ASSERT(stranded());
2,695 ✔
302
    const auto key = create_key();
2,695 ✔
303
    event_subscriber_.subscribe(std::move(handler), key);
2,695 ✔
304
    return key;
2,695 ✔
305
}
306

307
void full_node::subscribe_chase(event_notifier&& handler,
590 ✔
308
    event_completer&& complete) NOEXCEPT
309
{
310
    network::io::post(strand(),
590 ✔
311
        std::bind(&full_node::do_subscribe_chase,
590 ✔
312
            this, std::move(handler), std::move(complete)));
590 ✔
313
}
590 ✔
314

315
// private
316
void full_node::do_subscribe_chase(const event_notifier& handler,
590 ✔
317
    const event_completer& complete) NOEXCEPT
318
{
319
    BC_ASSERT(stranded());
590 ✔
320
    const auto key = create_key();
590 ✔
321
    complete(event_subscriber_.subscribe(move_copy(handler), key), key);
590 ✔
322
}
590 ✔
323

324
void full_node::unsubscribe_chase(object_key key) NOEXCEPT
627 ✔
325
{
326
    notify_one(key, network::error::service_stopped, chases::stop{});
627 ✔
327
}
627 ✔
328

329
// Suspensions.
330
// ----------------------------------------------------------------------------
331

332
bool full_node::resume() NOEXCEPT
11 ✔
333
{
334
    if (query_.is_full())
11 ✔
335
    {
UNCOV
336
        LOGF("Cannot resume network, disk full.");
×
UNCOV
337
        return false;
×
338
    }
339

340
    if (query_.is_fault())
11 ✔
341
    {
UNCOV
342
        LOGF("Cannot resume network, " << query_.get_fault());
×
UNCOV
343
        return false;
×
344
    }
345

346
    if (query_.is_fault_locked())
11 ✔
347
    {
348
        LOGF("Cannot resume network, fault locked.");
1 ✔
349
        return false;
1 ✔
350
    }
351

352
    LOGS("Resuming network.");
10 ✔
353
    const auto resumed = net::resume();
10 ✔
354
    notify(error::success, chases::resume{});
10 ✔
355
    return resumed;
10 ✔
356
}
357

358
// This is just a best effort, the call may have to be repeated.
359
void full_node::suspend(const code& ec) NOEXCEPT
29 ✔
360
{
361
    LOGS("Suspending network, " << ec.message());
29 ✔
362
    net::suspend(ec);
29 ✔
363
    notify(error::suspended_channel, chases::suspend{});
29 ✔
364
}
29 ✔
365

366
void full_node::fault(const code& ec) NOEXCEPT
17 ✔
367
{
368
    if (query_.is_full())
17 ✔
369
    {
UNCOV
370
        LOGF("Disk full [" << query_.get_space()
×
371
            << "] bytes required following, " << ec.message());
UNCOV
372
        notify(ec, chases::space{});
×
373
    }
374
    else if (query_.is_fault())
17 ✔
375
    {
UNCOV
376
        LOGF("Store fault (unrecoverable) '" << query_.get_fault().message()
×
377
            << "' following, " << ec.message());
UNCOV
378
        /* bool */ query_.set_fault_lock();
×
379
    }
380
    else if (ec)
17 ✔
381
    {
382
        // The cause did not originate from the memory map (!is_fault).
383
        LOGF("Node fault, " << ec.message());
16 ✔
384
        /* bool */ query_.set_fault_lock();
16 ✔
385
    }
386

387
    // If a call does not get all of the channels, subsequent failures do so.
388
    suspend(ec);
17 ✔
389
}
17 ✔
390

391
// Leaves network suspended, caller may want to resume upon success.
392
code full_node::prune(const store::event_handler& handler) NOEXCEPT
5 ✔
393
{
394
    if (query_.is_fault())
5 ✔
UNCOV
395
        return query_.get_code();
×
396

397
    const auto start = logger::now();
5 ✔
398
    suspend(error::store_prune);
5 ✔
399
    const auto ec = query_.prune([&](auto event, auto table) NOEXCEPT
5 ✔
400
    {
401
        // Suspend channels that missed previous suspend events.
402
        if (event == database::event_t::wait_lock)
630 ✔
UNCOV
403
            suspend(error::store_prune);
×
404

405
        handler(event, table);
630 ✔
406
    });
630 ✔
407

408
    net::span<milliseconds>(events::prune_msecs, start);
5 ✔
409
    return ec;
5 ✔
410
}
411

412
// Leaves network suspended, caller may want to resume upon success.
413
code full_node::snapshot(const store::event_handler& handler) NOEXCEPT
3 ✔
414
{
415
    if (query_.is_fault())
3 ✔
UNCOV
416
        return query_.get_code();
×
417

418
    const auto start = logger::now();
3 ✔
419
    suspend(error::store_snapshot);
3 ✔
420
    const auto ec = query_.snapshot([&](auto event, auto table) NOEXCEPT
3 ✔
421
    {
422
        // Suspend channels that missed previous suspend events.
423
        if (event == database::event_t::wait_lock)
297 ✔
UNCOV
424
            suspend(error::store_snapshot);
×
425

426
        handler(event, table);
297 ✔
427
    });
297 ✔
428

429
    net::span<seconds>(events::snapshot_secs, start);
3 ✔
430
    return ec;
3 ✔
431
}
432

433
// Leaves network suspended, caller may want to resume upon success.
434
code full_node::reload(const store::event_handler& handler) NOEXCEPT
2 ✔
435
{
436
    if (!query_.is_full())
2 ✔
437
        return query_.is_fault() ? query_.get_code() : error::success;
2 ✔
438

UNCOV
439
    const auto start = logger::now();
×
UNCOV
440
    suspend(error::store_reload);
×
441

442
    // This will set the store's dirty flag causing increased read activity for
443
    // set(tx/block) due to activation of duplicate detection. The store is
444
    // is however always in a dirty state after startup with non-empty tables.
UNCOV
445
    const auto ec = query_.reload([&](auto event, auto table) NOEXCEPT
×
446
    {
447
        // Suspend channels that missed previous suspend events.
UNCOV
448
        if (event == database::event_t::wait_lock)
×
UNCOV
449
            suspend(error::store_reload);
×
450

UNCOV
451
        handler(event, table);
×
UNCOV
452
    });
×
453

UNCOV
454
    net::span<milliseconds>(events::reload_msecs, start);
×
UNCOV
455
    return ec;
×
456
}
457

458
// Properties.
459
// ----------------------------------------------------------------------------
460

461
query& full_node::archive() const NOEXCEPT
106,708 ✔
462
{
463
    return query_;
106,708 ✔
464
}
465

466
const node::configuration& full_node::node_config() const NOEXCEPT
184 ✔
467
{
468
    return config_;
184 ✔
469
}
470

471
const system::settings& full_node::system_settings() const NOEXCEPT
7,444 ✔
472
{
473
    return config_.bitcoin;
7,444 ✔
474
}
475

476
const database::settings& full_node::database_settings() const NOEXCEPT
480 ✔
477
{
478
    return config_.database;
480 ✔
479
}
480

481
////const network::settings& full_node::network_settings() const NOEXCEPT
482
////{
483
////    return network_.network_settings();
484
////}
485

486
const node::settings& full_node::node_settings() const NOEXCEPT
17,366 ✔
487
{
488
    return config_.node;
17,366 ✔
489
}
490

491
bool full_node::is_current_chain(bool confirmed) const NOEXCEPT
8,462 ✔
492
{
493
    if (is_zero(config_.node.currency_window_minutes))
8,462 ✔
494
        return true;
495

496
    return is_current_time(query_.get_top_timestamp(confirmed));
1,014 ✔
497
}
498

499
bool full_node::is_current_time(uint32_t timestamp) const NOEXCEPT
7,543 ✔
500
{
501
    if (is_zero(config_.node.currency_window_minutes))
7,543 ✔
502
        return true;
503

504
    const wall_clock::time_point time{ seconds{ timestamp } };
1,520 ✔
505
    const auto current = wall_clock::now() - config_.node.currency_window();
1,520 ✔
506
    return time >= current;
1,520 ✔
507
}
508

509
// get_timestamp error results in false (ok).
510
bool full_node::is_current_header(const header_link& link) const NOEXCEPT
425 ✔
511
{
512
    if (is_zero(config_.node.currency_window_minutes))
425 ✔
513
        return true;
514

515
    uint32_t timestamp{};
421 ✔
516
    if (!query_.get_timestamp(timestamp, link))
421 ✔
517
        return false;
518

519
    return is_current_time(timestamp);
420 ✔
520
}
521

522
bool full_node::is_recent() const NOEXCEPT
435 ✔
523
{
524
    if (is_nonzero(config_.node.maximum_height) &&
435 ✔
525
        (query_.get_top_confirmed() >= config_.node.maximum_height))
3 ✔
526
        return true;
527

528
    return is_current_chain(true);
434 ✔
529
}
530

531
time_t full_node::start_time() const NOEXCEPT
7 ✔
532
{
533
    return start_time_;
7 ✔
534
}
535

536
// Methods.
537
// ----------------------------------------------------------------------------
538

539
void full_node::estimate(size_t target, estimate_mode mode,
23 ✔
540
    estimate_handler&& handler) NOEXCEPT
541
{
542
    chaser_transaction_.estimate(target, mode, std::move(handler));
23 ✔
543
}
23 ✔
544

545
void full_node::get_confirmable_txs(confirmable_txs_handler&& handler) NOEXCEPT
10 ✔
546
{
547
    chaser_transaction_.get_confirmable_txs(std::move(handler));
10 ✔
548
}
10 ✔
549

550
void full_node::get_confirmable_fees(
6 ✔
551
    confirmable_fees_handler&& handler) NOEXCEPT
552
{
553
    chaser_transaction_.get_confirmable_fees(std::move(handler));
6 ✔
554
}
6 ✔
555

556
void full_node::performance(object_key key, uint64_t speed,
24 ✔
557
    result_handler&& handler) NOEXCEPT
558
{
559
    chaser_download_.update(key, speed, std::move(handler));
24 ✔
560
}
24 ✔
561

562
// Session attachments.
563
// ----------------------------------------------------------------------------
564

565
network::session_manual::ptr full_node::attach_manual_session() NOEXCEPT
439 ✔
566
{
567
    return net::attach<node::session_manual>(*this);
439 ✔
568
}
569

570
network::session_inbound::ptr full_node::attach_inbound_session() NOEXCEPT
429 ✔
571
{
572
    return net::attach<node::session_inbound>(*this);
429 ✔
573
}
574

575
network::session_outbound::ptr full_node::attach_outbound_session() NOEXCEPT
429 ✔
576
{
577
    return net::attach<node::session_outbound>(*this);
429 ✔
578
}
579

580
BC_POP_WARNING()
581

582
} // namespace node
583
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc