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

libbitcoin / libbitcoin-network / 45096769114

23 Nov 2026 01:49AM UTC coverage: 94.187% (-0.03%) from 94.216%
45096769114

push

github

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

http compliance fixes.

39 of 54 new or added lines in 5 files covered. (72.22%)

165 existing lines in 20 files now uncovered.

16204 of 17204 relevant lines covered (94.19%)

2171.08 hits per line

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

96.47
/src/net.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/net.hpp>
20

21
#include <bitcoin/network/async/async.hpp>
22
#include <bitcoin/network/channels/channels.hpp>
23
#include <bitcoin/network/config/config.hpp>
24
#include <bitcoin/network/define.hpp>
25
#include <bitcoin/network/log/log.hpp>
26
#include <bitcoin/network/messages/messages.hpp>
27
#include <bitcoin/network/net/net.hpp>
28
#include <bitcoin/network/protocols/protocols.hpp>
29
#include <bitcoin/network/sessions/sessions.hpp>
30
#include <bitcoin/network/settings.hpp>
31

32
namespace libbitcoin {
33
namespace network {
34

35
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
36

37
using namespace system;
38
using namespace std::placeholders;
39
constexpr auto encryption = messages::peer::service::node_encrypted_transport;
40

41
// Seeding is not repeated within this many seeding timeouts of completion.
42
constexpr auto seed_backoff = 10;
43

44
net::net(const settings& settings, const logger& log,
286 ✔
45
    uint64_t required_services, uint64_t provided_services) NOEXCEPT
286 ✔
46
  : reporter(log),
47
    settings_(settings),
286 ✔
48
    privacy_(to_bool(bit_and(provided_services, to_value(encryption)))),
286 ✔
49
    threadpool_(settings.threads_()),
286 ✔
50
    strand_(threadpool_.service().get_executor()),
286 ✔
51
    hosts_(settings, log, required_services)
572 ✔
52
{
53
    ////LOG_LOG("Aplication log compiled..: ", news_defined);
54
    ////LOG_LOG("News log compiled........: ", news_defined);
55
    ////LOG_LOG("Session log compiled.....: ", session_defined);
56
    ////LOG_LOG("Protocol log compiled....: ", protocol_defined);
57
    ////LOG_LOG("ProXy log compiled.......: ", proxy_defined);
58
    ////LOG_LOG("Remote log compiled......: ", remote_defined);
59
    ////LOG_LOG("Fault log compiled.......: ", fault_defined);
60
    ////LOG_LOG("Quitting log compiled....: ", quitting_defined);
61
    ////LOG_LOG("Object log compiled......: ", objects_defined);
62
    ////LOG_LOG("Verbose log compiled.....: ", verbose_defined);
63
}
286 ✔
64

65
net::~net() NOEXCEPT
286 ✔
66
{
67
    // Weak references in threadpool closures safe as net joins threads here.
68
    net::close();
286 ✔
69
}
286 ✔
70

71
// I/O factories.
72
// ----------------------------------------------------------------------------
73

74
// server
75
acceptor::ptr net::create_service(socket::parameters&& params) NOEXCEPT
7 ✔
76
{
77
    return emplace_shared<acceptor>(log, strand(), service(),
78
        service_suspended_, std::move(params));
7 ✔
79
}
80

81
// inbound
82
acceptor::ptr net::create_acceptor(const socket::context& context) NOEXCEPT
84 ✔
83
{
84
    const auto& settings = network_settings();
84 ✔
85

86
    // bip324 (v2) inbound acceptance, v1 peers detected and passed through.
87
    const auto unset = std::holds_alternative<std::monostate>(context);
84 ✔
88
    const auto accept = privacy_ && unset ? settings.peer_context() : context;
84 ✔
89

90
    socket::parameters params
84 ✔
91
    {
92
        .connect_timeout = settings.connect_timeout(settings.inbound),
84 ✔
93
        .maximum_request = settings.inbound.maximum_request,
84 ✔
94
        .minimum_buffer = settings.inbound.minimum_response,
84 ✔
95
        .maximum_buffer = settings.inbound.maximum_buffer,
84 ✔
96
        .context = accept
97
    };
84 ✔
98

99
    return emplace_shared<acceptor>(log, strand(), service(),
100
        accept_suspended_, std::move(params));
84 ✔
101
}
102

103
// inbound (sam)
104
acceptor_sam::ptr net::create_acceptor_sam() NOEXCEPT
2 ✔
105
{
106
    const auto& settings = network_settings();
2 ✔
107

108
    // bip324 (v2) inbound acceptance, v1 peers detected and passed through.
109
    socket::parameters params
2 ✔
110
    {
111
        .connect_timeout = settings.connect_timeout(settings.inbound),
2 ✔
112
        .maximum_request = settings.inbound.maximum_request,
2 ✔
113
        .minimum_buffer = settings.inbound.minimum_response,
2 ✔
114
        .maximum_buffer = settings.inbound.maximum_buffer,
2 ✔
115
        .context = privacy_ ? settings.peer_context() : socket::context{}
1 ✔
116
    };
3 ✔
117

118
    return emplace_shared<acceptor_sam>(log, strand(), service(),
119
        accept_suspended_, std::move(params), settings.inbound);
2 ✔
120
}
121

122
connector::ptr net::to_connector(const settings::socks5& socks,
89 ✔
123
    const settings::tcp_server& options,
124
    const steady_clock::duration& connect_timeout, size_t slot) NOEXCEPT
125
{
126
    socket::parameters params
89 ✔
127
    {
128
        .connect_timeout = connect_timeout,
129
        .maximum_request = options.maximum_request,
89 ✔
130
        .minimum_buffer = options.minimum_response,
89 ✔
131
        .maximum_buffer = options.maximum_buffer,
89 ✔
132
        .bind = options.binding(slot),
89 ✔
133
        .slot = slot
134
    };
89 ✔
135

136
    if (privacy_)
89 ✔
137
        params.context = network_settings().peer_context();
1 ✔
138

139
    if (socks.proxied())
89 ✔
140
        return emplace_shared<connector_socks>(log, strand(), service(),
6 ✔
141
            connect_suspended_, std::move(params), socks);
6 ✔
142

143
    // Above can handle both proxy and non-proxy, but this is more efficient.
144
    return emplace_shared<connector>(log, strand(), service(),
145
        connect_suspended_, std::move(params));
86 ✔
146
}
147

148
// outbound (general)
149
connector::ptr net::create_connector(const settings::socks5& socks,
157 ✔
150
    const settings::tcp_server& options, size_t slot) NOEXCEPT
151
{
152
    return to_connector(socks, options,
157 ✔
153
        network_settings().connect_timeout(options), slot);
157 ✔
154
}
155

156
// outbound (seed)
157
connector::ptr net::create_seed_connector(size_t slot) NOEXCEPT
52 ✔
158
{
159
    const auto& settings = network_settings();
52 ✔
160

161
    return to_connector(settings.outbound, settings.outbound,
52 ✔
162
        settings.outbound.seeding_timeout(), slot);
52 ✔
163
}
164

165
// outbound (manual)
166
connector::ptr net::create_manual_connector(size_t slot) NOEXCEPT
35 ✔
167
{
168
    const auto& settings = network_settings();
35 ✔
169

170
    return create_connector(settings.manual, settings.manual, slot);
35 ✔
171
}
172

173
// outbound (batch)
174
connectors_ptr net::create_connectors(size_t count, size_t slot) NOEXCEPT
59 ✔
175
{
176
    const auto& settings = network_settings();
59 ✔
177
    const auto connects = to_shared<connectors>();
59 ✔
178
    connects->reserve(count);
59 ✔
179

180
    for (size_t connect{}; connect < count; ++connect)
181 ✔
181
        connects->push_back(create_connector(settings.outbound,
122 ✔
182
            settings.outbound, slot));
183

184
    return connects;
59 ✔
UNCOV
185
}
×
186

187
// Start sequence.
188
// ----------------------------------------------------------------------------
189

190
void net::start(result_handler&& handler) NOEXCEPT
147 ✔
191
{
192
    // Threadpool is started on construct, stopped only by work starvation.
193
    io::post(strand_,
147 ✔
194
        std::bind(&net::do_start, this, std::move(handler)));
294 ✔
195
}
147 ✔
196

197
void net::do_start(const result_handler& handler) NOEXCEPT
147 ✔
198
{
199
    BC_ASSERT(stranded());
147 ✔
200
    raise_descriptor_limit();
147 ✔
201
    manual_ = attach_manual_session();
147 ✔
202
    manual_->start(std::bind(&net::handle_start, this, _1, handler));
147 ✔
203
}
147 ✔
204

205
void net::handle_start(const code& ec, const result_handler& handler) NOEXCEPT
147 ✔
206
{
207
    BC_ASSERT(stranded());
147 ✔
208

209
    if (ec)
147 ✔
210
    {
211
        handler(ec);
4 ✔
212
        return;
4 ✔
213
    }
214

215
    // Deserialize hosts from file.
216
    if (const auto error_code = start_hosts())
143 ✔
217
    {
218
        LOGF("Hosts file failed to deserialize, " << error_code.message());
2 ✔
219
        handler(error_code);
2 ✔
220
        return;
2 ✔
221
    }
222

223
    seed_ = attach_seed_session();
141 ✔
224
    seed_->start(move_copy(handler));
141 ✔
225
}
226

227
// Run sequence (seeding may be ongoing after its handler is invoked).
228
// ----------------------------------------------------------------------------
229

230
void net::run(result_handler&& handler) NOEXCEPT
113 ✔
231
{
232
    // Public methods can complete on caller thread.
233
    if (closed())
113 ✔
234
    {
235
        handler(error::service_stopped);
5 ✔
236
        return;
5 ✔
237
    }
238

239
    io::post(strand_,
108 ✔
240
        std::bind(&net::do_run, this, std::move(handler)));
216 ✔
241
}
242

243
void net::do_run(const result_handler& handler) NOEXCEPT
109 ✔
244
{
245
    BC_ASSERT(stranded());
109 ✔
246

247
    if (closed())
109 ✔
248
    {
249
        handler(error::service_stopped);
1 ✔
250
        return;
1 ✔
251
    }
252

253
    // Start manual connections.
254
    for (const auto& peer: network_settings().manual.peers)
113 ✔
255
        do_connect(peer);
5 ✔
256

257
    // Start inbound connections.
258
    attach_inbound_session()->start(
324 ✔
259
        std::bind(&net::handle_run, this, _1, handler));
216 ✔
260
}
261

262
void net::handle_run(const code& ec, const result_handler& handler) NOEXCEPT
108 ✔
263
{
264
    BC_ASSERT(stranded());
108 ✔
265

266
    if (ec)
108 ✔
267
    {
268
        handler(ec);
2 ✔
269
        return;
2 ✔
270
    }
271

272
    attach_outbound_session()->start(move_copy(handler));
106 ✔
273
}
274

275
// Shutdown sequence.
276
// ----------------------------------------------------------------------------
277

278
// Results in std::abort if called from a thread within the threadpool.
279
void net::close() NOEXCEPT
428 ✔
280
{
281
    closed_.store(true);
428 ✔
282
    io::post(strand_,
428 ✔
283
        std::bind(&net::do_close, this));
428 ✔
284

285
    // Blocks on join of all threadpool threads.
286
    if (!threadpool_.join())
428 ✔
287
    {
UNCOV
288
        BC_ASSERT_MSG(false, "failed to join threadpool");
×
289
        std::abort();
290
    }
291

292
    // Serialize hosts to file.
293
    if (const auto error_code = stop_hosts())
428 ✔
294
    {
UNCOV
295
        LOGF("Hosts file failed to serialize, " << error_code.message());
×
296
    }
297
}
428 ✔
298

299
void net::do_close() NOEXCEPT
286 ✔
300
{
301
    BC_ASSERT(stranded());
286 ✔
302

303
    // Release session references (also held by stop subscriber).
304
    if (manual_) manual_.reset();
286 ✔
305
    if (seed_) seed_.reset();
286 ✔
306

307
    // Notify and delete all stop subscribers (all sessions).
308
    stop_subscriber_.stop(error::service_stopped);
286 ✔
309
    service_subscriber_.stop(error::service_stopped);
286 ✔
310

311
    // Notify and delete subscribers to channel notifications.
312
    connect_subscriber_.stop_default(error::service_stopped);
286 ✔
313

314
    // Notify and delete subscribers to message broadcast notifications.
315
    broadcaster_.stop(error::service_stopped);
286 ✔
316

317
    // Stop threadpool keep-alive, all work must self-terminate to affect join.
318
    threadpool_.stop();
286 ✔
319
}
286 ✔
320

321
bool net::closed() const NOEXCEPT
368 ✔
322
{
323
    return closed_.load();
368 ✔
324
}
325

326
// Suspensions.
327
// ----------------------------------------------------------------------------
328
// Service connections are managed independently from P2P.
329

330
void net::suspend_services() NOEXCEPT
1 ✔
331
{
332
    service_suspended_.store(true);
1 ✔
333
}
1 ✔
334

UNCOV
335
void net::resume_services() NOEXCEPT
×
336
{
UNCOV
337
    service_suspended_.store(false);
×
338
}
×
339

340
void net::suspend(const code&) NOEXCEPT
2 ✔
341
{
342
    suspend_acceptors();
2 ✔
343
    suspend_connectors();
2 ✔
344
}
2 ✔
345

346
bool net::resume() NOEXCEPT
1 ✔
347
{
348
    resume_acceptors();
1 ✔
349
    resume_connectors();
1 ✔
350
    return true;
1 ✔
351
}
352

353
void net::stop_services(const code& ec) NOEXCEPT
1 ✔
354
{
355
    io::post(strand_,
1 ✔
356
        std::bind(&net::do_stop_services, this, ec));
1 ✔
357
}
1 ✔
358

359
void net::do_stop_services(const code& ec) NOEXCEPT
1 ✔
360
{
361
    BC_ASSERT(stranded());
1 ✔
362
    suspend_services();
1 ✔
363
    service_subscriber_.stop(ec);
1 ✔
364
}
1 ✔
365

366
bool net::suspended() const NOEXCEPT
3 ✔
367
{
368
    return connect_suspended_.load() || accept_suspended_.load();
3 ✔
369
}
370

371
void net::suspend_acceptors() NOEXCEPT
2 ✔
372
{
373
    accept_suspended_.store(true);
2 ✔
374
}
2 ✔
375

376
void net::resume_acceptors() NOEXCEPT
1 ✔
377
{
378
    accept_suspended_.store(false);
1 ✔
379
}
1 ✔
380

381
void net::suspend_connectors() NOEXCEPT
2 ✔
382
{
383
    connect_suspended_.store(true);
2 ✔
384
}
2 ✔
385

386
void net::resume_connectors() NOEXCEPT
1 ✔
387
{
388
    connect_suspended_.store(false);
1 ✔
389
}
1 ✔
390

391
// Properties.
392
// ----------------------------------------------------------------------------
393

394
const settings& net::network_settings() const NOEXCEPT
5,389 ✔
395
{
396
    return settings_;
5,389 ✔
397
}
398

399
io::context& net::service() NOEXCEPT
322 ✔
400
{
401
    return threadpool_.service();
322 ✔
402
}
403

404
io::strand& net::strand() NOEXCEPT
1,255 ✔
405
{
406
    return strand_;
1,255 ✔
407
}
408

409
bool net::stranded() const NOEXCEPT
9,973 ✔
410
{
411
    return strand_.running_in_this_thread();
9,973 ✔
412
}
413

414

415
// Subscriptions.
416
// ----------------------------------------------------------------------------
417
// Channel and network strands share same pool, and as long as job is running in
418
// pool, it continues to accept work. Therefore handlers are not orphaned during
419
// stop as long as they remain in pool. But when entering from outside pool
420
// (such as subscribe) handler must be invoked when stopped, as handler goes
421
// uninvoked if pool empties.
422

423
// public
424
void net::subscribe_connect(channel_notifier&& handler,
3 ✔
425
    channel_completer&& complete) NOEXCEPT
426
{
427
    // Public methods can complete on caller thread.
428
    if (closed())
3 ✔
429
    {
430
        complete(error::service_stopped, {});
1 ✔
431
        handler(error::service_stopped, {});
1 ✔
432
        return;
1 ✔
433
    }
434

435
    io::post(strand_,
2 ✔
436
        std::bind(&net::do_subscribe_connect,
2 ✔
437
            this, std::move(handler), std::move(complete)));
4 ✔
438
}
439

440
void net::do_subscribe_connect(const channel_notifier& handler,
2 ✔
441
    const channel_completer& complete) NOEXCEPT
442
{
443
    BC_ASSERT(stranded());
2 ✔
444

445
    const auto key = create_key();
2 ✔
446
    complete(connect_subscriber_.subscribe(move_copy(handler), key), key);
2 ✔
447
}
2 ✔
448

449
// protected
450
void net::notify_connect(const channel::ptr& channel) NOEXCEPT
64 ✔
451
{
452
    io::post(strand_,
64 ✔
453
        std::bind(&net::do_notify_connect, this, channel));
128 ✔
454
}
64 ✔
455

456
void net::do_notify_connect(const channel::ptr& channel) NOEXCEPT
64 ✔
457
{
458
    BC_ASSERT(stranded());
64 ✔
459
    connect_subscriber_.notify(error::success, channel);
64 ✔
460
}
64 ✔
461

462
void net::unsubscribe_connect(object_key key) NOEXCEPT
1 ✔
463
{
464
    io::post(strand_,
1 ✔
465
        std::bind(&net::do_unsubscribe_connect, this, key));
1 ✔
466
}
1 ✔
467

468
void net::do_unsubscribe_connect(object_key key) NOEXCEPT
1 ✔
469
{
470
    BC_ASSERT(stranded());
1 ✔
471
    connect_subscriber_.notify_one(key, error::desubscribed, nullptr);
1 ✔
472
}
1 ✔
473

474
// protected
475
void net::subscribe_close(stop_handler&& handler) NOEXCEPT
1 ✔
476
{
477
    BC_ASSERT(stranded());
1 ✔
478
    subscribe_close(std::move(handler), create_key());
1 ✔
479
}
1 ✔
480

481
// private
482
code net::subscribe_close(stop_handler&& handler, object_key key) NOEXCEPT
514 ✔
483
{
484
    BC_ASSERT(stranded());
514 ✔
485
    return stop_subscriber_.subscribe(std::move(handler), key);
514 ✔
486
}
487

488
code net::subscribe_service(stop_handler&& handler, object_key key) NOEXCEPT
3 ✔
489
{
490
    BC_ASSERT(stranded());
3 ✔
491
    return service_subscriber_.subscribe(std::move(handler), key);
3 ✔
492
}
493

494
// public
495
void net::subscribe_close(stop_handler&& handler,
2 ✔
496
    stop_completer&& complete) NOEXCEPT
497
{
498
    // Public methods can complete on caller thread.
499
    if (closed())
2 ✔
500
    {
501
        complete(error::service_stopped, {});
1 ✔
502
        handler(error::service_stopped);
1 ✔
503
        return;
1 ✔
504
    }
505

506
    io::post(strand_,
1 ✔
507
        std::bind(&net::do_subscribe_close,
1 ✔
508
            this, std::move(handler), std::move(complete)));
2 ✔
509
}
510

511
void net::do_subscribe_close(const stop_handler& handler,
1 ✔
512
    const stop_completer& complete) NOEXCEPT
513
{
514
    BC_ASSERT(stranded());
1 ✔
515
    
516
    const auto key = create_key();
1 ✔
517
    complete(subscribe_close(move_copy(handler), key), key);
1 ✔
518
}
1 ✔
519

520
void net::unsubscribe_close(object_key key) NOEXCEPT
226 ✔
521
{
522
    io::post(strand_,
226 ✔
523
        std::bind(&net::do_unsubscribe_close, this, key));
226 ✔
524
}
226 ✔
525

526
void net::do_unsubscribe_close(object_key key) NOEXCEPT
226 ✔
527
{
528
    BC_ASSERT(stranded());
226 ✔
529
    stop_subscriber_.notify_one(key, error::desubscribed);
226 ✔
530
}
226 ✔
531

532
// Broadcasts.
533
// ----------------------------------------------------------------------------
534

535
void net::demand_wire_tx(bool demand) NOEXCEPT
4 ✔
536
{
537
    if (demand)
4 ✔
538
        ++wire_tx_demand_;
2 ✔
539
    else
540
        --wire_tx_demand_;
2 ✔
541
}
4 ✔
542

543
void net::demand_wire_block(bool demand) NOEXCEPT
4 ✔
544
{
545
    if (demand)
4 ✔
546
        ++wire_block_demand_;
2 ✔
547
    else
548
        --wire_block_demand_;
2 ✔
549
}
4 ✔
550

551
bool net::wire_tx_demanded() const NOEXCEPT
5 ✔
552
{
553
    return is_nonzero(wire_tx_demand_.load());
5 ✔
554
}
555

556
bool net::wire_block_demanded() const NOEXCEPT
5 ✔
557
{
558
    return is_nonzero(wire_block_demand_.load());
5 ✔
559
}
560

561
// At one object/session/ns, this overflows in ~585 years (and handled).
562
net::object_key net::create_key() NOEXCEPT
1,020 ✔
563
{
564
    BC_ASSERT(stranded());
1,020 ✔
565

566
    if (is_zero(++keys_))
1,020 ✔
567
    {
UNCOV
568
        BC_ASSERT_MSG(false, "overflow");
×
569
        LOGF("Session object overflow.");
570
    }
571

572
    return keys_;
1,020 ✔
573
}
574

575
// P2P Manual connections.
576
// ----------------------------------------------------------------------------
577

578
void net::connect(const config::endpoint& endpoint) NOEXCEPT
18 ✔
579
{
580
    io::post(strand_,
18 ✔
581
        std::bind(&net::do_connect, this, endpoint));
36 ✔
582
}
18 ✔
583

584
void net::do_connect(const config::endpoint& endpoint) NOEXCEPT
23 ✔
585
{
586
    BC_ASSERT(stranded());
23 ✔
587
    if (manual_) manual_->connect(endpoint);
23 ✔
588
}
23 ✔
589

590
void net::connect(const config::endpoint& endpoint,
10 ✔
591
    channel_notifier&& handler) NOEXCEPT
592
{
593
    // Public methods can complete on caller thread.
594
    if (closed())
10 ✔
595
    {
596
        handler(error::service_stopped, {});
1 ✔
597
        return;
1 ✔
598
    }
599

600
    io::post(strand_,
9 ✔
601
        std::bind(&net::do_connect_handled, this, endpoint,
9 ✔
602
            std::move(handler)));
9 ✔
603
}
604

605
void net::do_connect_handled(const config::endpoint& endpoint,
9 ✔
606
    const channel_notifier& handler) NOEXCEPT
607
{
608
    BC_ASSERT(stranded());
9 ✔
609

610
    if (manual_)
9 ✔
611
        manual_->connect(endpoint, move_copy(handler));
8 ✔
612
    else
613
        handler(error::service_stopped, nullptr);
1 ✔
614
}
9 ✔
615

616
// P2P Administration.
617
// ----------------------------------------------------------------------------
618

619
void net::dump_addresses(address_handler&& handler) NOEXCEPT
1 ✔
620
{
621
    fetch(std::move(handler));
1 ✔
622
}
1 ✔
623

624
// P2P Properties.
625
// ----------------------------------------------------------------------------
626

627
size_t net::address_count() const NOEXCEPT
88 ✔
628
{
629
    return hosts_.count();
88 ✔
630
}
631

632
config::address_counts net::address_counts() const NOEXCEPT
1 ✔
633
{
634
    return hosts_.counts();
1 ✔
635
}
636

637
size_t net::reserved_count() const NOEXCEPT
3 ✔
638
{
639
    return hosts_.reserved();
3 ✔
640
}
641

642
void net::fetch_totals(totals_handler&& handler) NOEXCEPT
10 ✔
643
{
644
    if (closed())
10 ✔
645
    {
646
        handler(error::service_stopped, {});
1 ✔
647
        return;
1 ✔
648
    }
649

650
    io::post(strand_,
9 ✔
651
        std::bind(&net::do_fetch_totals, this, std::move(handler)));
18 ✔
652
}
1 ✔
653

654
void net::do_fetch_totals(const totals_handler& handler) NOEXCEPT
9 ✔
655
{
656
    BC_ASSERT(stranded());
9 ✔
657

658
    handler(error::success,
9 ✔
659
    {
660
        peer_sent_.load(),
661
        peer_received_.load(),
662
        actives_,
9 ✔
663
    });
664
}
27 ✔
665

666
size_t net::channel_count() const NOEXCEPT
6 ✔
667
{
668
    return total_channel_count_;
6 ✔
669
}
670

671
size_t net::inbound_channel_count() const NOEXCEPT
152 ✔
672
{
673
    return inbound_channel_count_;
152 ✔
674
}
675

676
// P2P hosts collection.
677
// ----------------------------------------------------------------------------
678
// Protected, called from session (network strand) and channel (network pool).
679

680
// private
681
code net::start_hosts() NOEXCEPT
130 ✔
682
{
683
    return hosts_.start();
130 ✔
684
}
685

686
// private
687
code net::stop_hosts() NOEXCEPT
428 ✔
688
{
689
    return hosts_.stop();
428 ✔
690
}
691

692
void net::take(hosts::family family, address_item_handler&& handler) NOEXCEPT
3 ✔
693
{
694
    io::post(strand_,
3 ✔
695
        std::bind(&net::do_take, this, family, std::move(handler)));
6 ✔
696
}
3 ✔
697

698
void net::do_take(hosts::family family,
3 ✔
699
    const address_item_handler& handler) NOEXCEPT
700
{
701
    BC_ASSERT(stranded());
3 ✔
702
    hosts_.take(family, move_copy(handler));
3 ✔
703
}
3 ✔
704

705
void net::restore(const address_item_cptr& address,
16 ✔
706
    result_handler&& handler) NOEXCEPT
707
{
708
    io::post(strand_,
16 ✔
709
        std::bind(&net::do_restore, this, address, std::move(handler)));
32 ✔
710
}
16 ✔
711

712
void net::do_restore(const address_item_cptr& address,
16 ✔
713
    const result_handler& handler) NOEXCEPT
714
{
715
    BC_ASSERT(stranded());
16 ✔
716
    hosts_.restore(address, move_copy(handler));
16 ✔
717
}
16 ✔
718

719
void net::fetch(address_handler&& handler) NOEXCEPT
10 ✔
720
{
721
    io::post(strand_,
10 ✔
722
        std::bind(&net::do_fetch, this, std::move(handler)));
20 ✔
723
}
10 ✔
724

725
void net::do_fetch(const address_handler& handler) NOEXCEPT
10 ✔
726
{
727
    BC_ASSERT(stranded());
10 ✔
728

729
    // Accelerate stop, since hosts keeps running until all threads closed.
730
    if (closed())
10 ✔
731
    {
732
        handler(error::service_stopped, {});
1 ✔
733
        return;
1 ✔
734
    }
735

736
    hosts_.fetch(move_copy(handler));
9 ✔
737
}
738

739
void net::save(const address_cptr& message, count_handler&& handler) NOEXCEPT
26 ✔
740
{
741
    io::post(strand_,
26 ✔
742
        std::bind(&net::do_save, this, message, std::move(handler)));
52 ✔
743
}
26 ✔
744

745
void net::do_save(const address_cptr& message,
26 ✔
746
    const count_handler& handler) NOEXCEPT
747
{
748
    BC_ASSERT(stranded());
26 ✔
749

750
    // Accelerate stop, since hosts keeps running until all threads closed.
751
    if (closed())
26 ✔
752
    {
753
        handler(error::service_stopped, {});
1 ✔
754
        return;
1 ✔
755
    }
756

757
    hosts_.save(message, move_copy(handler));
25 ✔
758
}
759

760
void net::seed() NOEXCEPT
16 ✔
761
{
762
    io::post(strand_,
16 ✔
763
        std::bind(&net::do_seed, this));
16 ✔
764
}
16 ✔
765

766
void net::do_seed() NOEXCEPT
16 ✔
767
{
768
    BC_ASSERT(stranded());
16 ✔
769

770
    // address_not_found may only mean that no pooled address suits the slot.
771
    const auto& outbound = network_settings().outbound;
16 ✔
772
    if (closed() || outbound.seeds.empty() ||
28 ✔
773
        address_count() >= outbound.minimum_address_count())
12 ✔
774
        return;
9 ✔
775

776
    BC_ASSERT(seed_);
11 ✔
777
    const auto completed = seed_->completed();
11 ✔
778
    const auto backoff = outbound.seeding_timeout() * seed_backoff;
11 ✔
779

780
    if (seed_->seeding() || (completed != steady_clock::time_point{} &&
11 ✔
781
        (steady_clock::now() - completed) < backoff))
3 ✔
782
        return;
4 ✔
783

784
    seed_ = attach_seed_session();
7 ✔
785
    seed_->start([](const code&) NOEXCEPT {});
7 ✔
786
}
787

788
// P2P self address.
789
// ----------------------------------------------------------------------------
790

791
// P2P loopback detection.
792
// ----------------------------------------------------------------------------
793

794
bool net::store_nonce(const channel_peer& channel) NOEXCEPT
128 ✔
795
{
796
    BC_ASSERT(stranded());
128 ✔
797

798
    if (network_settings().inbound.enable_loopback || channel.inbound())
128 ✔
799
        return true;
85 ✔
800

801
    if (!nonces_.insert(channel.nonce()).second)
43 ✔
802
    {
803
        LOGF("Failed to store nonce for [" << channel.endpoint() << "].");
1 ✔
804
        return false;
1 ✔
805
    }
806

807
    return true;
808
}
809

810
bool net::unstore_nonce(const channel_peer& channel) NOEXCEPT
127 ✔
811
{
812
    BC_ASSERT(stranded());
127 ✔
813

814
    if (network_settings().inbound.enable_loopback || channel.inbound())
127 ✔
815
        return true;
85 ✔
816

817
    if (!to_bool(nonces_.erase(channel.nonce())))
42 ✔
818
    {
819
        LOGF("Failed to unstore nonce for [" << channel.endpoint() << "].");
1 ✔
820
        return false;
1 ✔
821
    }
822

823
    return true;
824
}
825

826
bool net::is_loopback(const channel_peer& channel) const NOEXCEPT
83 ✔
827
{
828
    BC_ASSERT(stranded());
83 ✔
829

830
    if (network_settings().inbound.enable_loopback || !channel.inbound())
83 ✔
831
        return false;
39 ✔
832

833
    return to_bool(nonces_.count(channel.peer_version()->nonce));
44 ✔
834
}
835

836
// P2P channel counting with address deconfliction.
837
// ----------------------------------------------------------------------------
838

839
code net::count_channel(const channel_peer& channel) NOEXCEPT
85 ✔
840
{
841
    BC_ASSERT(stranded());
85 ✔
842

843
    if (closed())
85 ✔
844
        return error::service_stopped;
2 ✔
845

846
    if (is_loopback(channel))
83 ✔
847
    {
848
        LOGS("Loopback detected from [" << channel.endpoint() << "].");
1 ✔
849
        return error::accept_failed;
1 ✔
850
    }
851

852
    if (channel.inbound() && is_zero(add1(inbound_channel_count_.load())))
125 ✔
853
    {
UNCOV
854
        LOGF("Overflow: inbound channel count.");
×
855
        return error::channel_overflow;
×
856
    }
857

858
    if (!channel.quiet() && is_zero(add1(total_channel_count_.load())))
148 ✔
859
    {
UNCOV
860
        LOGF("Overflow: total channel count.");
×
861
        return error::channel_overflow;
×
862
    }
863

864
    if (!hosts_.reserve(channel.endpoint()))
82 ✔
865
    {
866
        LOGS("Duplicate connection to [" << channel.endpoint() << "].");
1 ✔
867
        return error::address_in_use;
1 ✔
868
    }
869

870
    if (channel.inbound())
81 ✔
871
        ++inbound_channel_count_;
43 ✔
872

873
    if (!channel.quiet())
81 ✔
874
        ++total_channel_count_;
65 ✔
875

876
    actives_.push_back(channel.identifier());
81 ✔
877
    return error::success;
81 ✔
878
}
879

880
void net::uncount_channel(const channel_peer& channel) NOEXCEPT
81 ✔
881
{
882
    BC_ASSERT(stranded());
81 ✔
883

884
    hosts_.unreserve(channel.endpoint());
81 ✔
885
    peer_sent_ = ceilinged_add(peer_sent_.load(), channel.sent());
162 ✔
886
    peer_received_ = ceilinged_add(peer_received_.load(), channel.received());
162 ✔
887
    std::erase(actives_, channel.identifier());
81 ✔
888

889
    if (channel.inbound() && is_zero(inbound_channel_count_.load()))
124 ✔
890
    {
UNCOV
891
        LOGF("Underflow: inbound channel count.");
×
892
        return;
×
893
    }
894

895
    if (!channel.quiet() && is_zero(total_channel_count_.load()))
146 ✔
896
    {
UNCOV
897
        LOGF("Underflow: total channel count.");
×
898
        return;
×
899
    }
900

901
    if (channel.inbound())
81 ✔
902
        --inbound_channel_count_;
43 ✔
903

904
    if (!channel.quiet())
81 ✔
905
        --total_channel_count_;
65 ✔
906
}
907

908
// Specializations (protected).
909
// ----------------------------------------------------------------------------
910

911
session_seed::ptr net::attach_seed_session() NOEXCEPT
108 ✔
912
{
913
    BC_ASSERT(stranded());
108 ✔
914
    return attach<session_seed>(*this);
108 ✔
915
}
916

917
session_manual::ptr net::attach_manual_session() NOEXCEPT
130 ✔
918
{
919
    BC_ASSERT(stranded());
130 ✔
920
    return attach<session_manual>(*this);
130 ✔
921
}
922

923
session_inbound::ptr net::attach_inbound_session() NOEXCEPT
83 ✔
924
{
925
    BC_ASSERT(stranded());
83 ✔
926
    return attach<session_inbound>(*this);
83 ✔
927
}
928

929
session_outbound::ptr net::attach_outbound_session() NOEXCEPT
91 ✔
930
{
931
    BC_ASSERT(stranded());
91 ✔
932
    return attach<session_outbound>(*this);
91 ✔
933
}
934

935
BC_POP_WARNING()
936

937
} // namespace network
938
} // 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