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

libbitcoin / libbitcoin-node / 35678114419

22 Sep 2026 02:04AM UTC coverage: 42.054% (-0.1%) from 42.155%
35678114419

push

github

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

Replace the chase event enum parameter with typed event payloads.

37 of 127 new or added lines in 20 files covered. (29.13%)

30 existing lines in 10 files now uncovered.

2826 of 6720 relevant lines covered (42.05%)

1098.74 hits per line

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

54.35
/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/estimator.hpp>
24
#include <bitcoin/node/sessions/sessions.hpp>
25

26
namespace libbitcoin {
27
namespace node {
28

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

34
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
35

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

57
full_node::~full_node() NOEXCEPT
12 ✔
58
{
59
}
24 ✔
60

61
// Sequences.
62
// ----------------------------------------------------------------------------
63

64
void full_node::start(result_handler&& handler) NOEXCEPT
12 ✔
65
{
66
    if (!query_.is_initialized())
12 ✔
67
    {
68
        handler(error::store_uninitialized);
×
69
        return;
×
70
    }
71

72
    // Base (net) invokes do_start().
73
    net::start(std::move(handler));
12 ✔
74
}
75

76
void full_node::do_start(const result_handler& handler) NOEXCEPT
12 ✔
77
{
78
    BC_ASSERT(stranded());
12 ✔
79
    code ec{};
80

81
    if (((ec = (config_.node.headers_first ?
24 ✔
82
            chaser_header_.start() :
12 ✔
83
            chaser_block_.start()))) ||
12 ✔
84
        ((ec = chaser_check_.start())) ||
24 ✔
85
        ((ec = chaser_validate_.start())) ||
24 ✔
86
        ((ec = chaser_confirm_.start())) ||
24 ✔
87
        ((ec = chaser_transaction_.start())) ||
24 ✔
88
        ((ec = chaser_template_.start())) ||
24 ✔
89
        ((ec = chaser_estimate_.start())) ||
24 ✔
90
        ((ec = chaser_snapshot_.start())) ||
36 ✔
91
        ((ec = chaser_storage_.start())))
12 ✔
92
    {
93
        handler(ec);
×
94
        return;
×
95
    }
96

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

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

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

110
    if (closed())
12 ✔
111
    {
112
        handler(network::error::service_stopped);
×
113
        return;
×
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{});
12 ✔
119

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

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

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

142
// Base (net) invokes do_close().
143
void full_node::do_close() NOEXCEPT
12 ✔
144
{
145
    BC_ASSERT(stranded());
12 ✔
146

147
    // Initiate chaser stopping (including dedicated threadpools).
148
    chaser_header_.stopping(network::error::service_stopped);
12 ✔
149
    chaser_block_.stopping(network::error::service_stopped);
12 ✔
150
    chaser_check_.stopping(network::error::service_stopped);
12 ✔
151
    chaser_validate_.stopping(network::error::service_stopped);
12 ✔
152
    chaser_confirm_.stopping(network::error::service_stopped);
12 ✔
153
    chaser_transaction_.stopping(network::error::service_stopped);
12 ✔
154
    chaser_template_.stopping(network::error::service_stopped);
12 ✔
155
    chaser_estimate_.stopping(network::error::service_stopped);
12 ✔
156
    chaser_snapshot_.stopping(network::error::service_stopped);
12 ✔
157
    chaser_storage_.stopping(network::error::service_stopped);
12 ✔
158

159
    event_subscriber_.stop(network::error::service_stopped, chases::stop{});
12 ✔
160
    net::do_close();
12 ✔
161
}
12 ✔
162

163
// Organizers.
164
// ----------------------------------------------------------------------------
165

166
void full_node::organize(const system::chain::header::cptr& header,
×
167
    organize_handler&& handler) NOEXCEPT
168
{
169
    chaser_header_.organize(header, std::move(handler));
×
170
}
×
171

172
void full_node::organize(const system::chain::block::cptr& block,
×
173
    organize_handler&& handler) NOEXCEPT
174
{
175
    chaser_block_.organize(block, std::move(handler));
×
176
}
×
177

178
void full_node::prioritize(const system::hash_digest& hash,
×
179
    organize_handler&& handler) NOEXCEPT
180
{
181
    if (config_.node.headers_first)
×
182
        chaser_header_.prioritize(hash, std::move(handler));
×
183
    else
184
        chaser_block_.prioritize(hash, std::move(handler));
×
185
}
×
186

187
void full_node::submit(const system::chain::transactions_cptr& txs, bool test,
×
188
    submit_handler&& handler) NOEXCEPT
189
{
190
    chaser_transaction_.submit(txs, test, std::move(handler));
×
191
}
×
192

193
void full_node::get_hashes(map_handler&& handler) NOEXCEPT
×
194
{
195
    chaser_check_.get_hashes(std::move(handler));
×
196
}
×
197

198
void full_node::put_hashes(const map_ptr& map,
×
199
    result_handler&& handler) NOEXCEPT
200
{
201
    chaser_check_.put_hashes(map, std::move(handler));
×
202
}
×
203

204
// Events.
205
// ----------------------------------------------------------------------------
206

NEW
207
void full_node::notify(const code& ec, event_value value) NOEXCEPT
×
208
{
209
    boost::asio::post(strand(),
×
210
        std::bind(&full_node::do_notify,
×
211
            this, ec, value));
212
}
×
213

214
// private
215
void full_node::do_notify(const code& ec, event_value value) NOEXCEPT
12 ✔
216
{
217
    BC_ASSERT(stranded());
12 ✔
218
    event_subscriber_.notify(ec, value);
12 ✔
219
}
12 ✔
220

221
void full_node::notify_one(object_key key, const code& ec,
34 ✔
222
    event_value value) NOEXCEPT
223
{
224
    boost::asio::post(strand(),
34 ✔
225
        std::bind(&full_node::do_notify_one,
34 ✔
226
            this, key, ec, value));
227
}
34 ✔
228

229
// private
230
void full_node::do_notify_one(object_key key, const code& ec,
34 ✔
231
    event_value value) NOEXCEPT
232
{
233
    BC_ASSERT(stranded());
34 ✔
234
    event_subscriber_.notify_one(key, ec, value);
34 ✔
235
}
34 ✔
236

237
object_key full_node::subscribe_chase(event_notifier&& handler) NOEXCEPT
96 ✔
238
{
239
    BC_ASSERT(stranded());
96 ✔
240
    const auto key = create_key();
96 ✔
241
    event_subscriber_.subscribe(std::move(handler), key);
96 ✔
242
    return key;
96 ✔
243
}
244

245
void full_node::subscribe_chase(event_notifier&& handler,
23 ✔
246
    event_completer&& complete) NOEXCEPT
247
{
248
    boost::asio::post(strand(),
23 ✔
249
        std::bind(&full_node::do_subscribe_chase,
23 ✔
250
            this, std::move(handler), std::move(complete)));
251
}
23 ✔
252

253
// private
254
void full_node::do_subscribe_chase(const event_notifier& handler,
23 ✔
255
    const event_completer& complete) NOEXCEPT
256
{
257
    BC_ASSERT(stranded());
23 ✔
258
    const auto key = create_key();
23 ✔
259
    complete(event_subscriber_.subscribe(move_copy(handler), key), key);
23 ✔
260
}
23 ✔
261

262
void full_node::unsubscribe_chase(object_key key) NOEXCEPT
34 ✔
263
{
264
    notify_one(key, network::error::service_stopped, chases::stop{});
34 ✔
265
}
34 ✔
266

267
// Suspensions.
268
// ----------------------------------------------------------------------------
269

270
bool full_node::resume() NOEXCEPT
×
271
{
272
    if (query_.is_full())
×
273
    {
274
        LOGF("Cannot resume network, disk full.");
×
275
        return false;
×
276
    }
277

278
    if (query_.is_fault())
×
279
    {
280
        LOGF("Cannot resume network, " << query_.get_fault());
×
281
        return false;
×
282
    }
283

284
    LOGS("Resuming network.");
×
NEW
285
    notify(error::success, chases::resume{});
×
286
    return net::resume();
×
287
}
288

289
// This is just a best effort, the call may have to be repeated.
290
void full_node::suspend(const code& ec) NOEXCEPT
×
291
{
292
    LOGS("Suspending network, " << ec.message());
×
293
    net::suspend(ec);
×
NEW
294
    notify(error::suspended_channel, chases::suspend{});
×
295
}
×
296

297
void full_node::fault(const code& ec) NOEXCEPT
×
298
{
299
    if (query_.is_full())
×
300
    {
301
        LOGF("Disk full [" << query_.get_space()
×
302
            << "] bytes required following, " << ec.message());
NEW
303
        notify(ec, chases::space{});
×
304
    }
305
    else if (query_.is_fault())
×
306
    {
307
        LOGF("Store fault (unrecoverable) '" << query_.get_fault().message()
×
308
            << "' following, " << ec.message());
309
    }
310
    else if (ec)
×
311
    {
312
        // This is a resumable error condition, arising from calling fault(ec)
313
        // when the cause did not originate from the memory map (!is_fault).
314
        LOGF("Node fault, " << ec.message());
×
315
    }
316

317
    // If a call does not get all of the channels, subsequent failures do so.
318
    suspend(ec);
×
319
}
×
320

321
// Leaves network suspended, caller may want to resume upon success.
322
code full_node::prune(const store::event_handler& handler) NOEXCEPT
×
323
{
324
    if (query_.is_fault())
×
325
        return query_.get_code();
×
326

327
    const auto start = logger::now();
×
328
    suspend(error::store_prune);
×
329
    const auto ec = query_.prune([&](auto event, auto table) NOEXCEPT
×
330
    {
331
        // Suspend channels that missed previous suspend events.
332
        if (event == database::event_t::wait_lock)
×
333
            suspend(error::store_prune);
×
334

335
        handler(event, table);
×
336
    });
×
337

338
    net::span<milliseconds>(events::prune_msecs, start);
339
    return ec;
×
340
}
341

342
// Leaves network suspended, caller may want to resume upon success.
343
code full_node::snapshot(const store::event_handler& handler) NOEXCEPT
×
344
{
345
    if (query_.is_fault())
×
346
        return query_.get_code();
×
347

348
    const auto start = logger::now();
×
349
    suspend(error::store_snapshot);
×
350
    const auto ec = query_.snapshot([&](auto event, auto table) NOEXCEPT
×
351
    {
352
        // Suspend channels that missed previous suspend events.
353
        if (event == database::event_t::wait_lock)
×
354
            suspend(error::store_snapshot);
×
355

356
        handler(event, table);
×
357
    });
×
358

359
    net::span<seconds>(events::snapshot_secs, start);
360
    return ec;
×
361
}
362

363
// Leaves network suspended, caller may want to resume upon success.
364
code full_node::reload(const store::event_handler& handler) NOEXCEPT
×
365
{
366
    if (!query_.is_full())
×
367
        return query_.is_fault() ? query_.get_code() : error::success;
×
368

369
    const auto start = logger::now();
×
370
    suspend(error::store_reload);
×
371

372
    // This will set the store's dirty flag causing increased read activity for
373
    // set(tx/block) due to activation of duplicate detection. The store is
374
    // is however always in a dirty state after startup with non-empty tables.
375
    const auto ec = query_.reload([&](auto event, auto table) NOEXCEPT
×
376
    {
377
        // Suspend channels that missed previous suspend events.
378
        if (event == database::event_t::wait_lock)
×
379
            suspend(error::store_reload);
×
380

381
        handler(event, table);
×
382
    });
×
383

384
    net::span<milliseconds>(events::reload_msecs, start);
385
    return ec;
×
386
}
387

388
// Properties.
389
// ----------------------------------------------------------------------------
390

391
query& full_node::archive() const NOEXCEPT
157 ✔
392
{
393
    return query_;
157 ✔
394
}
395

396
const node::configuration& full_node::node_config() const NOEXCEPT
12 ✔
397
{
398
    return config_;
12 ✔
399
}
400

401
const system::settings& full_node::system_settings() const NOEXCEPT
240 ✔
402
{
403
    return config_.bitcoin;
240 ✔
404
}
405

406
const database::settings& full_node::database_settings() const NOEXCEPT
12 ✔
407
{
408
    return config_.database;
12 ✔
409
}
410

411
////const network::settings& full_node::network_settings() const NOEXCEPT
412
////{
413
////    return network_.network_settings();
414
////}
415

416
const node::settings& full_node::node_settings() const NOEXCEPT
272 ✔
417
{
418
    return config_.node;
272 ✔
419
}
420

421
bool full_node::is_current_chain(bool confirmed) const NOEXCEPT
38 ✔
422
{
423
    if (is_zero(config_.node.currency_window_minutes))
38 ✔
424
        return true;
425

426
    return is_current_time(query_.get_top_timestamp(confirmed));
38 ✔
427
}
428

429
bool full_node::is_current_time(uint32_t timestamp) const NOEXCEPT
38 ✔
430
{
431
    if (is_zero(config_.node.currency_window_minutes))
38 ✔
432
        return true;
433

434
    const auto time = wall_clock::from_time_t(timestamp);
38 ✔
435
    const auto current = wall_clock::now() - config_.node.currency_window();
38 ✔
436
    return time >= current;
38 ✔
437
}
438

439
// get_timestamp error results in false (ok).
440
bool full_node::is_current_header(const header_link& link) const NOEXCEPT
×
441
{
442
    if (is_zero(config_.node.currency_window_minutes))
×
443
        return true;
444

445
    uint32_t timestamp{};
×
446
    if (!query_.get_timestamp(timestamp, link))
×
447
        return false;
448

449
    return is_current_time(timestamp);
×
450
}
451

452
bool full_node::is_recent() const NOEXCEPT
×
453
{
454
    if (is_nonzero(config_.node.maximum_height) &&
×
455
        (query_.get_top_confirmed() >= config_.node.maximum_height))
×
456
        return true;
457

458
    return is_current_chain(true);
×
459
}
460

461
time_t full_node::start_time() const NOEXCEPT
×
462
{
463
    return start_time_;
×
464
}
465

466
// Methods.
467
// ----------------------------------------------------------------------------
468

469
void full_node::estimate(size_t target, estimator::mode mode,
×
470
    estimate_handler&& handler) NOEXCEPT
471
{
472
    chaser_estimate_.estimate(target, mode, std::move(handler));
×
473
}
×
474

475
void full_node::performance(object_key key, uint64_t speed,
×
476
    result_handler&& handler) NOEXCEPT
477
{
478
    chaser_check_.update(key, speed, std::move(handler));
×
479
}
×
480

481
// Session attachments.
482
// ----------------------------------------------------------------------------
483

484
network::session_manual::ptr full_node::attach_manual_session() NOEXCEPT
12 ✔
485
{
486
    return net::attach<node::session_manual>(*this);
12 ✔
487
}
488

489
network::session_inbound::ptr full_node::attach_inbound_session() NOEXCEPT
12 ✔
490
{
491
    return net::attach<node::session_inbound>(*this);
12 ✔
492
}
493

494
network::session_outbound::ptr full_node::attach_outbound_session() NOEXCEPT
12 ✔
495
{
496
    return net::attach<node::session_outbound>(*this);
12 ✔
497
}
498

499
BC_POP_WARNING()
500

501
} // namespace node
502
} // 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