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

libbitcoin / libbitcoin-node / 35934514310

23 Sep 2026 11:37PM UTC coverage: 42.816% (+0.6%) from 42.178%
35934514310

push

github

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

Close weak headers DoS surface.

48 of 500 new or added lines in 6 files covered. (9.6%)

10 existing lines in 6 files now uncovered.

2855 of 6668 relevant lines covered (42.82%)

1105.85 hits per line

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

54.42
/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_header_(*this),
12 ✔
45
    chaser_check_(*this),
12 ✔
46
    chaser_validate_(*this),
12 ✔
47
    chaser_confirm_(*this),
12 ✔
48
    chaser_transaction_(*this),
12 ✔
49
    chaser_template_(*this),
12 ✔
50
    chaser_estimate_(*this),
12 ✔
51
    chaser_snapshot_(*this),
12 ✔
52
    chaser_storage_(*this)
24 ✔
53
{
54
}
12 ✔
55

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

60
// Sequences.
61
// ----------------------------------------------------------------------------
62

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

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

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

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

94
    net::do_start(handler);
12 ✔
95
}
96

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

103
void full_node::do_run(const result_handler& handler) NOEXCEPT
12 ✔
104
{
105
    BC_ASSERT(stranded());
12 ✔
106

107
    if (closed())
12 ✔
108
    {
109
        handler(network::error::service_stopped);
×
110
        return;
×
111
    }
112

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

117
    // Start services after network is running.
118
    net::do_run(handler);
12 ✔
119
}
120

121
void full_node::close() NOEXCEPT
12 ✔
122
{
123
    // Base (net) invokes do_close().
124
    net::close();
12 ✔
125

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

138
// Base (net) invokes do_close().
139
void full_node::do_close() NOEXCEPT
12 ✔
140
{
141
    BC_ASSERT(stranded());
12 ✔
142

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

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

158
// Organizers.
159
// ----------------------------------------------------------------------------
160

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

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

173
void full_node::prioritize(const system::hash_digest& hash,
×
174
    organize_handler&& handler) NOEXCEPT
175
{
NEW
176
    chaser_header_.prioritize(hash, std::move(handler));
×
NEW
177
}
×
178

NEW
179
void full_node::get_minimum_work(work_handler&& handler) NOEXCEPT
×
180
{
NEW
181
    chaser_header_.get_minimum_work(std::move(handler));
×
UNCOV
182
}
×
183

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

190
void full_node::get_hashes(map_handler&& handler) NOEXCEPT
×
191
{
192
    chaser_check_.get_hashes(std::move(handler));
×
193
}
×
194

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

201
// Events.
202
// ----------------------------------------------------------------------------
203

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

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

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

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

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

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

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

259
void full_node::unsubscribe_chase(object_key key) NOEXCEPT
32 ✔
260
{
261
    notify_one(key, network::error::service_stopped, chases::stop{});
32 ✔
262
}
32 ✔
263

264
// Suspensions.
265
// ----------------------------------------------------------------------------
266

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

275
    if (query_.is_fault())
×
276
    {
277
        LOGF("Cannot resume network, " << query_.get_fault());
×
278
        return false;
×
279
    }
280

281
    LOGS("Resuming network.");
×
282
    notify(error::success, chases::resume{});
×
283
    return net::resume();
×
284
}
285

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

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

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

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

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

332
        handler(event, table);
×
333
    });
×
334

335
    net::span<milliseconds>(events::prune_msecs, start);
336
    return ec;
×
337
}
338

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

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

353
        handler(event, table);
×
354
    });
×
355

356
    net::span<seconds>(events::snapshot_secs, start);
357
    return ec;
×
358
}
359

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

366
    const auto start = logger::now();
×
367
    suspend(error::store_reload);
×
368

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

378
        handler(event, table);
×
379
    });
×
380

381
    net::span<milliseconds>(events::reload_msecs, start);
382
    return ec;
×
383
}
384

385
// Properties.
386
// ----------------------------------------------------------------------------
387

388
query& full_node::archive() const NOEXCEPT
145 ✔
389
{
390
    return query_;
145 ✔
391
}
392

393
const node::configuration& full_node::node_config() const NOEXCEPT
12 ✔
394
{
395
    return config_;
12 ✔
396
}
397

398
const system::settings& full_node::system_settings() const NOEXCEPT
192 ✔
399
{
400
    return config_.bitcoin;
192 ✔
401
}
402

403
const database::settings& full_node::database_settings() const NOEXCEPT
12 ✔
404
{
405
    return config_.database;
12 ✔
406
}
407

408
////const network::settings& full_node::network_settings() const NOEXCEPT
409
////{
410
////    return network_.network_settings();
411
////}
412

413
const node::settings& full_node::node_settings() const NOEXCEPT
273 ✔
414
{
415
    return config_.node;
273 ✔
416
}
417

418
bool full_node::is_current_chain(bool confirmed) const NOEXCEPT
50 ✔
419
{
420
    if (is_zero(config_.node.currency_window_minutes))
50 ✔
421
        return true;
422

423
    return is_current_time(query_.get_top_timestamp(confirmed));
50 ✔
424
}
425

426
bool full_node::is_current_time(uint32_t timestamp) const NOEXCEPT
50 ✔
427
{
428
    if (is_zero(config_.node.currency_window_minutes))
50 ✔
429
        return true;
430

431
    const auto time = wall_clock::from_time_t(timestamp);
50 ✔
432
    const auto current = wall_clock::now() - config_.node.currency_window();
50 ✔
433
    return time >= current;
50 ✔
434
}
435

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

442
    uint32_t timestamp{};
×
443
    if (!query_.get_timestamp(timestamp, link))
×
444
        return false;
445

446
    return is_current_time(timestamp);
×
447
}
448

449
bool full_node::is_recent() const NOEXCEPT
12 ✔
450
{
451
    if (is_nonzero(config_.node.maximum_height) &&
12 ✔
452
        (query_.get_top_confirmed() >= config_.node.maximum_height))
×
453
        return true;
454

455
    return is_current_chain(true);
12 ✔
456
}
457

458
time_t full_node::start_time() const NOEXCEPT
×
459
{
460
    return start_time_;
×
461
}
462

463
// Methods.
464
// ----------------------------------------------------------------------------
465

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

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

478
// Session attachments.
479
// ----------------------------------------------------------------------------
480

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

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

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

496
BC_POP_WARNING()
497

498
} // namespace node
499
} // 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