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

libbitcoin / libbitcoin-node / 44457441667

19 Nov 2026 04:18AM UTC coverage: 90.883% (-0.1%) from 90.987%
44457441667

push

github

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

Fault lock the store on a node fault and refuse resume.

9 of 16 new or added lines in 2 files covered. (56.25%)

2 existing lines in 2 files now uncovered.

4456 of 4903 relevant lines covered (90.88%)

4912.24 hits per line

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

91.89
/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,
425 ✔
37
    const logger& log) NOEXCEPT
425 ✔
38
  : net(configuration.network, log, configuration.node.services_required(),
425 ✔
39
        configuration.node.services_provided()),
40
    config_(configuration),
425 ✔
41
    start_time_(zulu_time()),
850 ✔
42
    query_(query),
425 ✔
43
    chaser_header_(*this),
425 ✔
44
    chaser_download_(*this),
425 ✔
45
    chaser_validate_(*this),
425 ✔
46
    chaser_confirm_(*this),
425 ✔
47
    chaser_transaction_(*this),
425 ✔
48
    chaser_template_(*this),
425 ✔
49
    chaser_snapshot_(*this),
425 ✔
50
    chaser_storage_(*this)
850 ✔
51
{
52
}
425 ✔
53

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

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

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

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

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

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

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

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

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

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

110
    if (closed())
340 ✔
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{});
339 ✔
119

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

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

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

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

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

155
    event_subscriber_.stop(network::error::service_stopped, chases::stop{});
425 ✔
156
    net::do_close();
425 ✔
157
}
425 ✔
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,
8 ✔
175
    organize_handler&& handler) NOEXCEPT
176
{
177
    chaser_header_.organize_compact(header, std::move(handler));
8 ✔
178
}
8 ✔
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
69 ✔
187
{
188
    chaser_header_.get_minimum_work(std::move(handler));
69 ✔
189
}
69 ✔
190

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

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

204
void full_node::submit_compact(const system::chain::transactions_cptr& txs,
8 ✔
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));
8 ✔
209
}
8 ✔
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
34 ✔
221
{
222
    chaser_transaction_.build_template(std::move(handler));
34 ✔
223
}
34 ✔
224

225
void full_node::subscribe_pool(pool_notifier&& handler) NOEXCEPT
5 ✔
226
{
227
    chaser_transaction_.subscribe_pool(std::move(handler));
5 ✔
228
}
5 ✔
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
71 ✔
236
{
237
    chaser_download_.get_hashes(std::move(handler));
71 ✔
238
}
71 ✔
239

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

246
// Events.
247
// ----------------------------------------------------------------------------
248

249
void full_node::notify(const code& ec, event_value value) NOEXCEPT
6,945 ✔
250
{
251
    network::io::post(strand(),
6,945 ✔
252
        std::bind(&full_node::do_notify,
13,890 ✔
253
            this, ec, value));
6,945 ✔
254
}
6,945 ✔
255

256
// private
257
void full_node::do_notify(const code& ec, event_value value) NOEXCEPT
7,284 ✔
258
{
259
    BC_ASSERT(stranded());
7,284 ✔
260
    event_subscriber_.notify(ec, value);
7,284 ✔
261
}
7,284 ✔
262

263
void full_node::notify_one(object_key key, const code& ec,
596 ✔
264
    event_value value) NOEXCEPT
265
{
266
    network::io::post(strand(),
596 ✔
267
        std::bind(&full_node::do_notify_one,
1,192 ✔
268
            this, key, ec, value));
596 ✔
269
}
596 ✔
270

271
// private
272
void full_node::do_notify_one(object_key key, const code& ec,
596 ✔
273
    event_value value) NOEXCEPT
274
{
275
    BC_ASSERT(stranded());
596 ✔
276
    event_subscriber_.notify_one(key, ec, value);
596 ✔
277
}
596 ✔
278

279
object_key full_node::subscribe_chase(event_notifier&& handler) NOEXCEPT
2,534 ✔
280
{
281
    BC_ASSERT(stranded());
2,534 ✔
282
    const auto key = create_key();
2,534 ✔
283
    event_subscriber_.subscribe(std::move(handler), key);
2,534 ✔
284
    return key;
2,534 ✔
285
}
286

287
void full_node::subscribe_chase(event_notifier&& handler,
563 ✔
288
    event_completer&& complete) NOEXCEPT
289
{
290
    network::io::post(strand(),
563 ✔
291
        std::bind(&full_node::do_subscribe_chase,
563 ✔
292
            this, std::move(handler), std::move(complete)));
563 ✔
293
}
563 ✔
294

295
// private
296
void full_node::do_subscribe_chase(const event_notifier& handler,
563 ✔
297
    const event_completer& complete) NOEXCEPT
298
{
299
    BC_ASSERT(stranded());
563 ✔
300
    const auto key = create_key();
563 ✔
301
    complete(event_subscriber_.subscribe(move_copy(handler), key), key);
563 ✔
302
}
563 ✔
303

304
void full_node::unsubscribe_chase(object_key key) NOEXCEPT
591 ✔
305
{
306
    notify_one(key, network::error::service_stopped, chases::stop{});
591 ✔
307
}
591 ✔
308

309
// Suspensions.
310
// ----------------------------------------------------------------------------
311

312
bool full_node::resume() NOEXCEPT
12 ✔
313
{
314
    if (query_.is_full())
12 ✔
315
    {
316
        LOGF("Cannot resume network, disk full.");
×
317
        return false;
×
318
    }
319

320
    if (query_.is_fault())
12 ✔
321
    {
322
        LOGF("Cannot resume network, " << query_.get_fault());
×
323
        return false;
×
324
    }
325

326
    if (query_.is_fault_locked())
12 ✔
327
    {
328
        LOGF("Cannot resume network, fault locked.");
1 ✔
329
        return false;
1 ✔
330
    }
331

332
    LOGS("Resuming network.");
11 ✔
333
    const auto resumed = net::resume();
11 ✔
334
    notify(error::success, chases::resume{});
11 ✔
335
    return resumed;
11 ✔
336
}
337

338
// This is just a best effort, the call may have to be repeated.
339
void full_node::suspend(const code& ec) NOEXCEPT
30 ✔
340
{
341
    LOGS("Suspending network, " << ec.message());
30 ✔
342
    net::suspend(ec);
30 ✔
343
    notify(error::suspended_channel, chases::suspend{});
30 ✔
344
}
30 ✔
345

346
void full_node::fault(const code& ec) NOEXCEPT
17 ✔
347
{
348
    if (query_.is_full())
17 ✔
349
    {
350
        LOGF("Disk full [" << query_.get_space()
×
351
            << "] bytes required following, " << ec.message());
352
        notify(ec, chases::space{});
×
353
    }
354
    else if (query_.is_fault())
17 ✔
355
    {
356
        LOGF("Store fault (unrecoverable) '" << query_.get_fault().message()
×
357
            << "' following, " << ec.message());
NEW
358
        /* bool */ query_.set_fault_lock();
×
359
    }
360
    else if (ec)
17 ✔
361
    {
362
        // The cause did not originate from the memory map (!is_fault).
363
        LOGF("Node fault, " << ec.message());
16 ✔
364
        /* bool */ query_.set_fault_lock();
16 ✔
365
    }
366

367
    // If a call does not get all of the channels, subsequent failures do so.
368
    suspend(ec);
17 ✔
369
}
17 ✔
370

371
// Leaves network suspended, caller may want to resume upon success.
372
code full_node::prune(const store::event_handler& handler) NOEXCEPT
6 ✔
373
{
374
    if (query_.is_fault())
6 ✔
375
        return query_.get_code();
×
376

377
    const auto start = logger::now();
6 ✔
378
    suspend(error::store_prune);
6 ✔
379
    const auto ec = query_.prune([&](auto event, auto table) NOEXCEPT
6 ✔
380
    {
381
        // Suspend channels that missed previous suspend events.
382
        if (event == database::event_t::wait_lock)
756 ✔
383
            suspend(error::store_prune);
×
384

385
        handler(event, table);
756 ✔
386
    });
756 ✔
387

388
    net::span<milliseconds>(events::prune_msecs, start);
6 ✔
389
    return ec;
6 ✔
390
}
391

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

398
    const auto start = logger::now();
3 ✔
399
    suspend(error::store_snapshot);
3 ✔
400
    const auto ec = query_.snapshot([&](auto event, auto table) NOEXCEPT
3 ✔
401
    {
402
        // Suspend channels that missed previous suspend events.
403
        if (event == database::event_t::wait_lock)
297 ✔
404
            suspend(error::store_snapshot);
×
405

406
        handler(event, table);
297 ✔
407
    });
297 ✔
408

409
    net::span<seconds>(events::snapshot_secs, start);
3 ✔
410
    return ec;
3 ✔
411
}
412

413
// Leaves network suspended, caller may want to resume upon success.
414
code full_node::reload(const store::event_handler& handler) NOEXCEPT
2 ✔
415
{
416
    if (!query_.is_full())
2 ✔
417
        return query_.is_fault() ? query_.get_code() : error::success;
2 ✔
418

419
    const auto start = logger::now();
×
420
    suspend(error::store_reload);
×
421

422
    // This will set the store's dirty flag causing increased read activity for
423
    // set(tx/block) due to activation of duplicate detection. The store is
424
    // is however always in a dirty state after startup with non-empty tables.
425
    const auto ec = query_.reload([&](auto event, auto table) NOEXCEPT
×
426
    {
427
        // Suspend channels that missed previous suspend events.
428
        if (event == database::event_t::wait_lock)
×
429
            suspend(error::store_reload);
×
430

431
        handler(event, table);
×
432
    });
×
433

434
    net::span<milliseconds>(events::reload_msecs, start);
×
435
    return ec;
×
436
}
437

438
// Properties.
439
// ----------------------------------------------------------------------------
440

441
query& full_node::archive() const NOEXCEPT
106,348 ✔
442
{
443
    return query_;
106,348 ✔
444
}
445

446
const node::configuration& full_node::node_config() const NOEXCEPT
175 ✔
447
{
448
    return config_;
175 ✔
449
}
450

451
const system::settings& full_node::system_settings() const NOEXCEPT
7,063 ✔
452
{
453
    return config_.bitcoin;
7,063 ✔
454
}
455

456
const database::settings& full_node::database_settings() const NOEXCEPT
457 ✔
457
{
458
    return config_.database;
457 ✔
459
}
460

461
////const network::settings& full_node::network_settings() const NOEXCEPT
462
////{
463
////    return network_.network_settings();
464
////}
465

466
const node::settings& full_node::node_settings() const NOEXCEPT
16,757 ✔
467
{
468
    return config_.node;
16,757 ✔
469
}
470

471
bool full_node::is_current_chain(bool confirmed) const NOEXCEPT
8,318 ✔
472
{
473
    if (is_zero(config_.node.currency_window_minutes))
8,318 ✔
474
        return true;
475

476
    return is_current_time(query_.get_top_timestamp(confirmed));
984 ✔
477
}
478

479
bool full_node::is_current_time(uint32_t timestamp) const NOEXCEPT
7,510 ✔
480
{
481
    if (is_zero(config_.node.currency_window_minutes))
7,510 ✔
482
        return true;
483

484
    const wall_clock::time_point time{ seconds{ timestamp } };
1,490 ✔
485
    const auto current = wall_clock::now() - config_.node.currency_window();
1,490 ✔
486
    return time >= current;
1,490 ✔
487
}
488

489
// get_timestamp error results in false (ok).
490
bool full_node::is_current_header(const header_link& link) const NOEXCEPT
425 ✔
491
{
492
    if (is_zero(config_.node.currency_window_minutes))
425 ✔
493
        return true;
494

495
    uint32_t timestamp{};
421 ✔
496
    if (!query_.get_timestamp(timestamp, link))
421 ✔
497
        return false;
498

499
    return is_current_time(timestamp);
420 ✔
500
}
501

502
bool full_node::is_recent() const NOEXCEPT
412 ✔
503
{
504
    if (is_nonzero(config_.node.maximum_height) &&
412 ✔
505
        (query_.get_top_confirmed() >= config_.node.maximum_height))
3 ✔
506
        return true;
507

508
    return is_current_chain(true);
411 ✔
509
}
510

511
time_t full_node::start_time() const NOEXCEPT
7 ✔
512
{
513
    return start_time_;
7 ✔
514
}
515

516
// Methods.
517
// ----------------------------------------------------------------------------
518

519
void full_node::estimate(size_t target, estimate_mode mode,
23 ✔
520
    estimate_handler&& handler) NOEXCEPT
521
{
522
    chaser_transaction_.estimate(target, mode, std::move(handler));
23 ✔
523
}
23 ✔
524

525
void full_node::performance(object_key key, uint64_t speed,
24 ✔
526
    result_handler&& handler) NOEXCEPT
527
{
528
    chaser_download_.update(key, speed, std::move(handler));
24 ✔
529
}
24 ✔
530

531
// Session attachments.
532
// ----------------------------------------------------------------------------
533

534
network::session_manual::ptr full_node::attach_manual_session() NOEXCEPT
416 ✔
535
{
536
    return net::attach<node::session_manual>(*this);
416 ✔
537
}
538

539
network::session_inbound::ptr full_node::attach_inbound_session() NOEXCEPT
406 ✔
540
{
541
    return net::attach<node::session_inbound>(*this);
406 ✔
542
}
543

544
network::session_outbound::ptr full_node::attach_outbound_session() NOEXCEPT
406 ✔
545
{
546
    return net::attach<node::session_outbound>(*this);
406 ✔
547
}
548

549
BC_POP_WARNING()
550

551
} // namespace node
552
} // 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