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

libbitcoin / libbitcoin-network / 35561354381

21 Sep 2026 04:31AM UTC coverage: 48.498% (+0.04%) from 48.46%
35561354381

push

github

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

Implement outbound notification HWM.

110 of 147 new or added lines in 15 files covered. (74.83%)

21 existing lines in 11 files now uncovered.

28017 of 57769 relevant lines covered (48.5%)

163473.21 hits per line

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

53.51
/src/settings.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/network/settings.hpp>
20

21
#include <filesystem>
22
#include <bitcoin/network/async/async.hpp>
23
#include <bitcoin/network/config/config.hpp>
24
#include <bitcoin/network/messages/messages.hpp>
25

26
namespace libbitcoin {
27
namespace network {
28

29
using namespace system;
30
using namespace messages::peer;
31

32
// Default (zero) threads is capped at this many hardware threads.
33
constexpr auto maximum_default_threads = 32_size;
34

35
// socks5
36
// ----------------------------------------------------------------------------
37

38
settings::socks5::socks5() NOEXCEPT
377 ✔
39
{
40
}
×
41

42
bool settings::socks5::proxied() const NOEXCEPT
57 ✔
43
{
44
    return is_nonzero(socks.port());
57 ✔
45
}
46

47
bool settings::socks5::authenticated() const NOEXCEPT
3 ✔
48
{
49
    return !(username.empty() && password.empty());
3 ✔
50
}
51

52
// sam
53
// ----------------------------------------------------------------------------
54

55
settings::sam::sam() NOEXCEPT
200 ✔
56
{
57
}
200 ✔
58

59
// geti2p.net/en/docs/api/samv3
60
// A destination is a fixed prefix and a length-prefixed certificate.
61
constexpr auto destination_size = 387_size;
62
constexpr auto certificate_position = 385_size;
63

64
// I2P has no ports.
65
constexpr uint16_t sam_port = 0;
66

67
// The destination (public) prefix of a private key, empty if truncated.
68
static data_chunk to_destination(const data_chunk& private_key) NOEXCEPT
×
69
{
70
    read::bytes::copy source(private_key);
×
71
    source.skip_bytes(certificate_position);
×
72
    const auto size = destination_size + source.read_2_bytes_big_endian();
×
73

74
    if (!source || private_key.size() < size)
×
75
        return {};
×
76

77
    return { private_key.begin(), std::next(private_key.begin(), size) };
×
78
}
79

80
// The address of a stored private key (base64), unspecified if invalid.
81
config::address settings::sam::to_self(const std::string& key) NOEXCEPT
×
82
{
83
    data_chunk private_key{};
×
84
    if (!decode_base64(private_key, key))
×
85
        return {};
×
86

87
    const auto destination = to_destination(private_key);
×
88
    if (destination.empty())
×
89
        return {};
×
90

91
    return address_item
×
92
    {
93
        unix_time(), service::node_none, i2p_t{ sha256_hash(destination) },
×
94
        sam_port
95
    };
×
96
}
97

98
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
99
code settings::sam::initialize() NOEXCEPT
×
100
{
101
    if (key_path.empty())
×
102
        return error::success;
×
103

104
    try
105
    {
106
        ifstream file{ key_path, ifstream::in };
×
107
        if (file.good() && !!std::getline(file, key) && !key.empty())
×
108
            self = to_self(key);
×
109
    }
×
110
    catch (const std::exception&)
×
111
    {
112
        key.clear();
113
    }
×
114

115
    return error::success;
×
116
}
117
BC_POP_WARNING()
118

119
bool settings::sam::bridged() const NOEXCEPT
20 ✔
120
{
121
    return is_nonzero(bridge.port());
20 ✔
122
}
123

124
bool settings::sam::authenticated() const NOEXCEPT
×
125
{
126
    return !(username.empty() && password.empty());
×
127
}
128

129
// tcp_server
130
// ----------------------------------------------------------------------------
131

132
settings::tcp_server::tcp_server(const std::string_view& logging_name) NOEXCEPT
701 ✔
133
  : name(logging_name)
701 ✔
134
{
135
}
701 ✔
136

137
bool settings::tcp_server::enabled() const NOEXCEPT
57 ✔
138
{
139
    return to_bool(connections);
57 ✔
140
}
141

142
steady_clock::duration settings::tcp_server::inactivity() const NOEXCEPT
45 ✔
143
{
144
    return minutes{ inactivity_minutes };
45 ✔
145
}
146

147
steady_clock::duration settings::tcp_server::expiration() const NOEXCEPT
45 ✔
148
{
149
    return minutes{ expiration_minutes };
45 ✔
150
}
151

152
// secure_server
153
// ----------------------------------------------------------------------------
154

155
settings::secure_server::secure_server(
7 ✔
156
    const std::string_view& logging_name) NOEXCEPT
7 ✔
157
  : tcp_server(logging_name)
7 ✔
158
{
159
}
7 ✔
160

161
bool settings::secure_server::secure() const NOEXCEPT
×
162
{
163
    return false;
×
164
}
165

166
bool settings::secure_server::authenticate() const NOEXCEPT
×
167
{
168
    return false;
×
169
}
170

171
code settings::secure_server::initialize_context() NOEXCEPT
×
172
{
173
    return error::success;
×
174
}
175

176
settings::transport settings::secure_server::clear_context() const NOEXCEPT
×
177
{
178
    return {};
×
179
}
180

181
settings::transport settings::secure_server::secure_context() const NOEXCEPT
×
182
{
183
    return {};
×
184
}
185

186
// tls_server
187
// ----------------------------------------------------------------------------
188

189
settings::tls_server::tls_server(const std::string_view& logging_name) NOEXCEPT
7 ✔
190
  : secure_server(logging_name)
7 ✔
191
{
192
}
7 ✔
193

194
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
195
code settings::tls_server::initialize_context() NOEXCEPT
×
196
{
UNCOV
197
    if (context)
×
198
        return error::operation_failed;
×
199

200
    context = std::make_unique<asio::ssl::context>(asio::ssl::version);
×
201
    if (!secure())
×
202
        return error::success;
×
203

204
    boost_code ec{};
×
205
    context->set_options(asio::ssl::options, ec);
×
206
    if (ec) return error::tls_set_options;
×
207

208
    context->use_certificate_chain_file(cert_path.string(), ec);
×
209
    if (ec) return error::tls_use_certificate;
×
210

211
    if (!key_pass.empty())
×
212
        context->set_password_callback([&](auto, auto){ return key_pass; });
×
213

214
    constexpr auto pem = asio::ssl::context::pem;
215
    context->use_private_key_file(key_path.string(), pem, ec);
×
216
    if (ec) return error::tls_use_private_key;
×
217

218
    if (authenticate())
×
219
    {
220
        context->set_verify_mode(asio::ssl::authenticate);
×
221
        if (cert_auth.empty())
×
222
        {
223
            context->set_default_verify_paths(ec);
×
224
            if (ec) return error::tls_set_default_verify;
×
225
        }
226
        else
227
        {
228
            context->add_verify_path(cert_auth.string(), ec);
×
229
            if (ec) return error::tls_set_add_verify;
×
230
        }
231
    }
232

233
    return error::success;
×
234
}
235
BC_POP_WARNING();
236

237
bool settings::tls_server::secure() const NOEXCEPT
7 ✔
238
{
239
    return !safes.empty() && !cert_path.empty() && !key_path.empty();
7 ✔
240
}
241

242
bool settings::tls_server::authenticate() const NOEXCEPT
×
243
{
244
    return secure() && !cert_auth.empty();
×
245
}
246

247
settings::transport settings::tls_server::secure_context() const NOEXCEPT
×
248
{
249
    BC_ASSERT(context);
×
250
    return std::ref(*context);
×
251
}
252

253
// zmtp_server
254
// ----------------------------------------------------------------------------
255

256
settings::zmtp_server::zmtp_server(
×
257
    const std::string_view& logging_name) NOEXCEPT
×
258
  : secure_server(logging_name)
×
259
{
260
}
×
261

262
bool settings::zmtp_server::secure() const NOEXCEPT
×
263
{
264
    const system::data_chunk& secret = key;
×
265
    return !safes.empty() && !secret.empty();
×
266
}
267

268
bool settings::zmtp_server::authenticate() const NOEXCEPT
×
269
{
270
    return secure() && !certs.empty();
×
271
}
272

273
code settings::zmtp_server::initialize_context() NOEXCEPT
×
274
{
275
    if (context)
×
276
        return error::operation_failed;
×
277

278
    system::data_stack clients{};
×
279
    for (const system::data_chunk& cert: certs)
×
280
        clients.push_back(cert);
×
281

282
    context = std::make_unique<zmtp::context>(key, clients);
×
283

284
    if (!secure())
×
285
        return error::success;
×
286

287
    return context->curve() ? error::success : error::invalid_configuration;
×
288
}
×
289

290
settings::transport settings::zmtp_server::clear_context() const NOEXCEPT
×
291
{
292
    return std::cref(clear);
×
293
}
294

295
settings::transport settings::zmtp_server::secure_context() const NOEXCEPT
×
296
{
297
    BC_ASSERT(context);
×
298
    return std::cref(*context);
×
299
}
300

301
// http_server
302
// ----------------------------------------------------------------------------
303

304
system::string_list settings::http_server::host_names() const NOEXCEPT
2 ✔
305
{
306
    // secure changes default port from 80 to 443.
307
    const auto port = secure() ? http::default_tls : http::default_http;
2 ✔
308
    return config::to_host_names(hosts, port);
2 ✔
309
}
310

311
system::string_list settings::http_server::origin_names() const NOEXCEPT
2 ✔
312
{
313
    // secure changes default port from 80 to 443.
314
    const auto port = secure() ? http::default_tls : http::default_http;
2 ✔
315
    return config::to_host_names(hosts, port);
2 ✔
316
}
317

318
// local
319
static config::credentials::const_iterator to_credential(
7 ✔
320
    const config::credentials& credentials,
321
    const hash_digest& digest) NOEXCEPT
322
{
323
    return std::find_if(credentials.begin(), credentials.end(),
7 ✔
324
        [&digest](const config::credential& credential) NOEXCEPT
7 ✔
325
        {
326
            return credential.digest() == digest;
7 ✔
327
        });
7 ✔
328
}
329

330
bool settings::http_server::authorize() const NOEXCEPT
10 ✔
331
{
332
    return !credentials.empty();
10 ✔
333
}
334

335
bool settings::http_server::authorized(const hash_digest& digest) const NOEXCEPT
3 ✔
336
{
337
    return to_credential(credentials, digest) != credentials.end();
3 ✔
338
}
339

340
bool settings::http_server::permitted(const hash_digest& digest,
4 ✔
341
    const std::string& method) const NOEXCEPT
342
{
343
    const auto credential = to_credential(credentials, digest);
4 ✔
344
    return credential != credentials.end() && credential->permitted(method);
4 ✔
345
}
346

347
// websocket_server
348
// ----------------------------------------------------------------------------
349

350
// [outbound]
351
// ----------------------------------------------------------------------------
352

353
bool settings::peer_outbound::enabled() const NOEXCEPT
20 ✔
354
{
355
    return settings::tcp_server::enabled()
20 ✔
356
        && to_bool(host_pool_capacity)
14 ✔
357
        && to_bool(connect_batch_size);
31 ✔
358
}
359

360
size_t settings::peer_outbound::minimum_address_count() const NOEXCEPT
21 ✔
361
{
362
    // Cannot overflow size_t as long as both are uint16_t.
363
    return connect_batch_size * peer_outbound::connections;
21 ✔
364
}
365

366
steady_clock::duration settings::peer_outbound::seeding_timeout() const NOEXCEPT
18 ✔
367
{
368
    return seconds(seeding_timeout_seconds);
18 ✔
369
}
370

371
// [inbound]
372
// ----------------------------------------------------------------------------
373

374
config::address settings::peer_inbound::first_self() const NOEXCEPT
4 ✔
375
{
376
    return selfs.empty() ? config::address{} : selfs.front();
4 ✔
377
}
378

379
bool settings::peer_inbound::advertise() const NOEXCEPT
10 ✔
380
{
381
    return enabled() && (!selfs.empty() || bridged());
10 ✔
382
}
383

384
bool settings::peer_inbound::enabled() const NOEXCEPT
33 ✔
385
{
386
    return settings::tcp_server::enabled() && (!binds.empty() || bridged());
33 ✔
387
}
388

389
// [manual]
390
// ----------------------------------------------------------------------------
391

392
void settings::peer_manual::initialize() NOEXCEPT
3 ✔
393
{
394
    BC_ASSERT_MSG(friends.empty(), "friends not empty");
3 ✔
395

396
    // Dynamic conversion of peers is O(N^2), so set on initialize.
397
    // This converts endpoints to addresses so will produce the default
398
    // address for any hosts that are DNS names (i.e. not IP addresses).
399
    friends = system::projection<network::config::addresses>(peers);
6 ✔
400
}
3 ✔
401

402
bool settings::peer_manual::peered(const address_item& item) const NOEXCEPT
27 ✔
403
{
404
    // Friends should be mapped from peers by initialize().
405
    return contains(friends, item);
27 ✔
406
}
407

408
bool settings::peer_manual::enabled() const NOEXCEPT
27 ✔
409
{
410
    // connections field is not currently used, and this applies only to
411
    // initial configuration of manual connections, as they can be added.
412
    return /* settings::tcp_server::enabled() && */ !peers.empty();
27 ✔
413
}
414

415
// [network]
416
// ----------------------------------------------------------------------------
417

418
static_assert(heading::maximum_payload(true) == 4'000'000);
419
static_assert(heading::maximum_payload(false) == 1'800'003);
420

421
static uint32_t identifier_from_context(chain::selection context) NOEXCEPT
182 ✔
422
{
423
    switch (context)
182 ✔
424
    {
425
        case chain::selection::mainnet:  return 3652501241;
426
        case chain::selection::testnet3: return 118034699;
1 ✔
427
        case chain::selection::regtest:  return 3669344250;
1 ✔
428
        default: return 0;
×
429
    }
430
}
431

432
settings::settings(chain::selection context) NOEXCEPT
182 ✔
433
  : outbound{ context },
182 ✔
434
    inbound{ context },
182 ✔
435
    manual{ context },
182 ✔
436
    identifier(identifier_from_context(context))
364 ✔
437
{
438
}
182 ✔
439

440
// Derived values are read and computed here, and are const thereafter.
441
code settings::initialize() NOEXCEPT
×
442
{
443
    peer.identifier = identifier;
×
444
    manual.initialize();
×
445
    return inbound.initialize();
×
446
}
447

448
settings::transport settings::peer_context() const NOEXCEPT
×
449
{
450
    return std::cref(peer);
×
451
}
452

453
size_t settings::threads_() const NOEXCEPT
98 ✔
454
{
455
    // Zero implies the lesser of hardware threads and the default cap.
456
    return to_bool(threads) ? threads :
195 ✔
457
        std::min(cores(), maximum_default_threads);
97 ✔
458
}
459

460
// Randomized from 50% to maximum milliseconds (specified in seconds).
461
steady_clock::duration settings::retry_timeout() const NOEXCEPT
16 ✔
462
{
463
    const auto from = retry_timeout_seconds * 500_u64;
16 ✔
464
    const auto to = retry_timeout_seconds * 1'000_u64;
16 ✔
465
    return milliseconds{ system::maybe_random::next(from, to) };
16 ✔
466
}
467

468
steady_clock::duration settings::channel_handshake() const NOEXCEPT
3 ✔
469
{
470
    return seconds(handshake_timeout_seconds);
3 ✔
471
}
472

473
steady_clock::duration settings::channel_heartbeat() const NOEXCEPT
4 ✔
474
{
475
    return minutes(channel_heartbeat_minutes);
4 ✔
476
}
477

478
steady_clock::duration settings::maximum_skew() const NOEXCEPT
1 ✔
479
{
480
    return minutes(maximum_skew_minutes);
1 ✔
481
}
482

483
std::filesystem::path settings::file() const NOEXCEPT
×
484
{
485
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
486
    return path / "hosts.cache";
×
487
    BC_POP_WARNING()
488
}
489

490
bool settings::gossip_v2() const NOEXCEPT
4 ✔
491
{
492
    // Only tor and i2p addresses are unrepresentable in the v1 protocol.
493
    return gossip_tor || gossip_i2p;
4 ✔
494
}
495

496
// Randomized from 50% to maximum milliseconds (specified in seconds).
497
steady_clock::duration settings::connect_timeout(
78 ✔
498
    const tcp_server& options) const NOEXCEPT
499
{
500
    const auto seconds_ = to_bool(options.connect_timeout_seconds) ?
78 ✔
501
        options.connect_timeout_seconds : connect_timeout_seconds;
502

503
    const auto from = seconds_ * 500_u64;
78 ✔
504
    const auto to = seconds_ * 1'000_u64;
78 ✔
505
    return milliseconds{ system::maybe_random::next(from, to) };
78 ✔
506
}
507

508
uint32_t settings::rate_limited(const tcp_server& options) const NOEXCEPT
43 ✔
509
{
510
    return to_bool(rate_limit) && to_bool(options.rate_limit) ?
86 ✔
511
        std::min(rate_limit, options.rate_limit) :
2 ✔
512
        std::max(rate_limit, options.rate_limit);
41 ✔
513
}
514

515
bool settings::connectable(const address_item& item) const NOEXCEPT
17 ✔
516
{
517
    // A proxy connects by name, otherwise only an ip address is routable.
518
    return is_v4(item.address) || is_v6(item.address) ||
519
        is_cjdns(item.address) || (outbound.proxied() && is_named(item.address));
9 ✔
520
}
521

522
bool settings::gossiped(const address_item& item) const NOEXCEPT
19 ✔
523
{
524
    switch (item.address.index())
19 ✔
525
    {
526
        case ipv4_t::id:
3 ✔
527
            return gossip_ipv4;
3 ✔
528
        case ipv6_t::id:
7 ✔
529
        case cjdns_t::id:
530
            return gossip_ipv6;
7 ✔
531
        case torv3_t::id:
4 ✔
532
            return gossip_tor;
4 ✔
533
        case i2p_t::id:
2 ✔
534
            return gossip_i2p;
2 ✔
535
        default:
536
            return false;
537
    }
538
}
539

540
bool settings::unsupported(const address_item& item) const NOEXCEPT
12 ✔
541
{
542
    return to_bool(bit_and(item.services, invalid_services));
12 ✔
543
}
544

545
bool settings::blacklisted(const address_item& item) const NOEXCEPT
21 ✔
546
{
547
    return contains(blacklists, item);
21 ✔
548
}
549

550
bool settings::whitelisted(const address_item& item) const NOEXCEPT
22 ✔
551
{
552
    return whitelists.empty() || contains(whitelists, item);
22 ✔
553
}
554

555
bool settings::excluded(const address_item& item) const NOEXCEPT
1 ✔
556
{
557
    return !is_specified(item)
1 ✔
558
        || !gossiped(item)
×
559
        || unsupported(item)
×
560
        || manual.peered(item)
×
561
        || blacklisted(item)
×
562
        || !whitelisted(item);
1 ✔
563
}
564

565
} // namespace network
566
} // 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