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

libbitcoin / libbitcoin-network / 36270395112

26 Sep 2026 08:41PM UTC coverage: 47.753% (-2.1%) from 49.886%
36270395112

push

github

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

Regenerate artifacts.

25298 of 52977 relevant lines covered (47.75%)

177897.81 hits per line

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

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

62
net::~net() NOEXCEPT
101 ✔
63
{
64
    // Weak references in threadpool closures safe as net joins threads here.
65
    net::close();
101 ✔
66
}
303 ✔
67

68
// I/O factories.
69
// ----------------------------------------------------------------------------
70

71
// server
72
acceptor::ptr net::create_service(socket::parameters&& params) NOEXCEPT
×
73
{
74
    return emplace_shared<acceptor>(log, strand(), service(),
×
75
        service_suspended_, std::move(params));
×
76
}
77

78
// inbound
79
acceptor::ptr net::create_acceptor(const socket::context& context) NOEXCEPT
4 ✔
80
{
81
    const auto& settings = network_settings();
4 ✔
82

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

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

97
    return emplace_shared<acceptor>(log, strand(), service(),
8 ✔
98
        accept_suspended_, std::move(params));
4 ✔
99
}
100

101
// inbound (sam)
102
acceptor_sam::ptr net::create_acceptor_sam() NOEXCEPT
×
103
{
104
    const auto& settings = network_settings();
×
105

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

116
    return emplace_shared<acceptor_sam>(log, strand(), service(),
×
117
        accept_suspended_, std::move(params), settings.inbound);
×
118
}
119

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

134
    if (privacy_)
45 ✔
135
        params.context = network_settings().peer_context();
×
136

137
    if (socks.proxied())
45 ✔
138
        return emplace_shared<connector_socks>(log, strand(), service(),
×
139
            connect_suspended_, std::move(params), socks);
×
140

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

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

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

159
    return to_connector(settings.outbound, settings.outbound,
14 ✔
160
        settings.outbound.seeding_timeout(), slot);
14 ✔
161
}
162

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

168
    return create_connector(settings.manual, settings.manual, slot);
14 ✔
169
}
170

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

178
    for (size_t connect{}; connect < count; ++connect)
78 ✔
179
        connects->push_back(create_connector(settings.outbound,
126 ✔
180
            settings.outbound, slot));
181

182
    return connects;
15 ✔
183
}
184

185
// Start sequence.
186
// ----------------------------------------------------------------------------
187

188
void net::start(result_handler&& handler) NOEXCEPT
29 ✔
189
{
190
    // Threadpool is started on construct, stopped only by work starvation.
191
    boost::asio::post(strand_,
29 ✔
192
        std::bind(&net::do_start, this, std::move(handler)));
29 ✔
193
}
29 ✔
194

195
void net::do_start(const result_handler& handler) NOEXCEPT
29 ✔
196
{
197
    BC_ASSERT(stranded());
29 ✔
198
    manual_ = attach_manual_session();
58 ✔
199
    manual_->start(std::bind(&net::handle_start, this, _1, handler));
29 ✔
200
}
29 ✔
201

202
void net::handle_start(const code& ec, const result_handler& handler) NOEXCEPT
29 ✔
203
{
204
    BC_ASSERT(stranded());
29 ✔
205

206
    if (ec)
29 ✔
207
    {
208
        handler(ec);
4 ✔
209
        return;
4 ✔
210
    }
211

212
    // Deserialize hosts from file.
213
    if (const auto error_code = start_hosts())
25 ✔
214
    {
215
        LOGF("Hosts file failed to deserialize, " << error_code.message());
4 ✔
216
        handler(error_code);
2 ✔
217
        return;
2 ✔
218
    }
219

220
    attach_seed_session()->start(move_copy(handler));
92 ✔
221
}
222

223
// Run sequence (seeding may be ongoing after its handler is invoked).
224
// ----------------------------------------------------------------------------
225

226
void net::run(result_handler&& handler) NOEXCEPT
20 ✔
227
{
228
    // Public methods can complete on caller thread.
229
    if (closed())
20 ✔
230
    {
231
        handler(error::service_stopped);
5 ✔
232
        return;
5 ✔
233
    }
234

235
    boost::asio::post(strand_,
15 ✔
236
        std::bind(&net::do_run, this, std::move(handler)));
30 ✔
237
}
238

239
void net::do_run(const result_handler& handler) NOEXCEPT
15 ✔
240
{
241
    BC_ASSERT(stranded());
15 ✔
242

243
    if (closed())
15 ✔
244
    {
245
        handler(error::service_stopped);
×
246
        return;
×
247
    }
248

249
    // Start manual connections.
250
    for (const auto& peer: network_settings().manual.peers)
20 ✔
251
        do_connect(peer);
5 ✔
252

253
    // Start inbound connections.
254
    attach_inbound_session()->start(
45 ✔
255
        std::bind(&net::handle_run, this, _1, handler));
30 ✔
256
}
257

258
void net::handle_run(const code& ec, const result_handler& handler) NOEXCEPT
15 ✔
259
{
260
    BC_ASSERT(stranded());
15 ✔
261

262
    if (ec)
15 ✔
263
    {
264
        handler(ec);
2 ✔
265
        return;
2 ✔
266
    }
267

268
    attach_outbound_session()->start(move_copy(handler));
52 ✔
269
}
270

271
// Shutdown sequence.
272
// ----------------------------------------------------------------------------
273

274
// Results in std::abort if called from a thread within the threadpool.
275
void net::close() NOEXCEPT
115 ✔
276
{
277
    closed_.store(true);
278
    boost::asio::post(strand_,
115 ✔
279
        std::bind(&net::do_close, this));
115 ✔
280

281
    // Blocks on join of all threadpool threads.
282
    if (!threadpool_.join())
115 ✔
283
    {
284
        BC_ASSERT_MSG(false, "failed to join threadpool");
×
285
        std::abort();
286
    }
287

288
    // Serialize hosts to file.
289
    if (const auto error_code = stop_hosts())
115 ✔
290
    {
291
        LOGF("Hosts file failed to serialize, " << error_code.message());
×
292
    }
293
}
115 ✔
294

295
void net::do_close() NOEXCEPT
101 ✔
296
{
297
    BC_ASSERT(stranded());
101 ✔
298

299
    // Release reference to manual session (also held by stop subscriber).
300
    if (manual_) manual_.reset();
101 ✔
301

302
    // Notify and delete all stop subscribers (all sessions).
303
    stop_subscriber_.stop(error::service_stopped);
101 ✔
304

305
    // Notify and delete subscribers to channel notifications.
306
    connect_subscriber_.stop_default(error::service_stopped);
101 ✔
307

308
    // Notify and delete subscribers to message broadcast notifications.
309
    broadcaster_.stop(error::service_stopped);
101 ✔
310

311
    // Stop threadpool keep-alive, all work must self-terminate to affect join.
312
    threadpool_.stop();
101 ✔
313
}
101 ✔
314

315
bool net::closed() const NOEXCEPT
36 ✔
316
{
317
    return closed_.load();
36 ✔
318
}
319

320
// Suspensions.
321
// ----------------------------------------------------------------------------
322
// Service connections are managed independently from P2P.
323

324
void net::suspend_services() NOEXCEPT
×
325
{
326
    service_suspended_.store(true);
327
}
×
328

329
void net::resume_services() NOEXCEPT
×
330
{
331
    service_suspended_.store(false);
332
}
×
333

334
void net::suspend(const code&) NOEXCEPT
×
335
{
336
    suspend_acceptors();
×
337
    suspend_connectors();
×
338
}
×
339

340
bool net::resume() NOEXCEPT
×
341
{
342
    resume_acceptors();
×
343
    resume_connectors();
×
344
    return true;
×
345
}
346

347
bool net::suspended() const NOEXCEPT
×
348
{
349
    return connect_suspended_.load() || accept_suspended_.load();
×
350
}
351

352
void net::suspend_acceptors() NOEXCEPT
×
353
{
354
    accept_suspended_.store(true);
355
}
×
356

357
void net::resume_acceptors() NOEXCEPT
×
358
{
359
    accept_suspended_.store(false);
360
}
×
361

362
void net::suspend_connectors() NOEXCEPT
×
363
{
364
    connect_suspended_.store(true);
365
}
×
366

367
void net::resume_connectors() NOEXCEPT
×
368
{
369
    connect_suspended_.store(false);
370
}
×
371

372
// Properties.
373
// ----------------------------------------------------------------------------
374

375
const settings& net::network_settings() const NOEXCEPT
674 ✔
376
{
377
    return settings_;
674 ✔
378
}
379

380
asio::context& net::service() NOEXCEPT
106 ✔
381
{
382
    return threadpool_.service();
106 ✔
383
}
384

385
asio::strand& net::strand() NOEXCEPT
287 ✔
386
{
387
    return strand_;
287 ✔
388
}
389

390
bool net::stranded() const NOEXCEPT
1,678 ✔
391
{
392
    return strand_.running_in_this_thread();
1,678 ✔
393
}
394

395

396
// Subscriptions.
397
// ----------------------------------------------------------------------------
398
// Channel and network strands share same pool, and as long as a job is
399
// running in the pool, it will continue to accept work. Therefore handlers
400
// will not be orphaned during a stop as long as they remain in the pool.
401
// But when entering from outside the pool (such as subscribe) handler must be
402
// invoked when stopped as the handler will go uninvoked if the pool empties.
403

404
// public
405
void net::subscribe_connect(channel_notifier&& handler,
2 ✔
406
    channel_completer&& complete) NOEXCEPT
407
{
408
    // Public methods can complete on caller thread.
409
    if (closed())
2 ✔
410
    {
411
        complete(error::service_stopped, {});
1 ✔
412
        handler(error::service_stopped, {});
1 ✔
413
        return;
1 ✔
414
    }
415

416
    boost::asio::post(strand_,
1 ✔
417
        std::bind(&net::do_subscribe_connect,
2 ✔
418
            this, std::move(handler), std::move(complete)));
419
}
420

421
void net::do_subscribe_connect(const channel_notifier& handler,
1 ✔
422
    const channel_completer& complete) NOEXCEPT
423
{
424
    BC_ASSERT(stranded());
1 ✔
425

426
    const auto key = create_key();
1 ✔
427
    complete(connect_subscriber_.subscribe(move_copy(handler), key), key);
1 ✔
428
}
1 ✔
429

430
// protected
431
void net::notify_connect(const channel::ptr& channel) NOEXCEPT
5 ✔
432
{
433
    boost::asio::post(strand_,
5 ✔
434
        std::bind(&net::do_notify_connect, this, channel));
5 ✔
435
}
5 ✔
436

437
void net::do_notify_connect(const channel::ptr& channel) NOEXCEPT
5 ✔
438
{
439
    BC_ASSERT(stranded());
5 ✔
440
    connect_subscriber_.notify(error::success, channel);
5 ✔
441
}
5 ✔
442

443
void net::unsubscribe_connect(object_key key) NOEXCEPT
×
444
{
445
    boost::asio::post(strand_,
×
446
        std::bind(&net::do_unsubscribe_connect, this, key));
×
447
}
×
448

449
void net::do_unsubscribe_connect(object_key key) NOEXCEPT
×
450
{
451
    BC_ASSERT(stranded());
×
452
    connect_subscriber_.notify_one(key, error::desubscribed, nullptr);
×
453
}
×
454

455
// protected
456
void net::subscribe_close(stop_handler&& handler) NOEXCEPT
×
457
{
458
    BC_ASSERT(stranded());
×
459
    subscribe_close(std::move(handler), create_key());
×
460
}
×
461

462
// private
463
code net::subscribe_close(stop_handler&& handler, object_key key) NOEXCEPT
81 ✔
464
{
465
    BC_ASSERT(stranded());
81 ✔
466
    return stop_subscriber_.subscribe(std::move(handler), key);
81 ✔
467
}
468

469
// public
470
void net::subscribe_close(stop_handler&& handler,
2 ✔
471
    stop_completer&& complete) NOEXCEPT
472
{
473
    // Public methods can complete on caller thread.
474
    if (closed())
2 ✔
475
    {
476
        complete(error::service_stopped, {});
1 ✔
477
        handler(error::service_stopped);
1 ✔
478
        return;
1 ✔
479
    }
480

481
    boost::asio::post(strand_,
1 ✔
482
        std::bind(&net::do_subscribe_close,
2 ✔
483
            this, std::move(handler), std::move(complete)));
484
}
485

486
void net::do_subscribe_close(const stop_handler& handler,
1 ✔
487
    const stop_completer& complete) NOEXCEPT
488
{
489
    BC_ASSERT(stranded());
1 ✔
490
    
491
    const auto key = create_key();
1 ✔
492
    complete(subscribe_close(move_copy(handler), key), key);
1 ✔
493
}
1 ✔
494

495
void net::unsubscribe_close(object_key key) NOEXCEPT
29 ✔
496
{
497
    boost::asio::post(strand_,
29 ✔
498
        std::bind(&net::do_unsubscribe_close, this, key));
29 ✔
499
}
29 ✔
500

501
void net::do_unsubscribe_close(object_key key) NOEXCEPT
29 ✔
502
{
503
    BC_ASSERT(stranded());
29 ✔
504
    stop_subscriber_.notify_one(key, error::desubscribed);
29 ✔
505
}
29 ✔
506

507
// At one object/session/ns, this overflows in ~585 years (and handled).
508
net::object_key net::create_key() NOEXCEPT
168 ✔
509
{
510
    BC_ASSERT(stranded());
168 ✔
511

512
    if (is_zero(++keys_))
168 ✔
513
    {
514
        BC_ASSERT_MSG(false, "overflow");
×
515
        LOGF("Session object overflow.");
516
    }
517

518
    return keys_;
168 ✔
519
}
520

521
// P2P Manual connections.
522
// ----------------------------------------------------------------------------
523

524
void net::connect(const config::endpoint& endpoint) NOEXCEPT
4 ✔
525
{
526
    boost::asio::post(strand_,
4 ✔
527
        std::bind(&net::do_connect, this, endpoint));
4 ✔
528
}
4 ✔
529

530
void net::do_connect(const config::endpoint& endpoint) NOEXCEPT
9 ✔
531
{
532
    BC_ASSERT(stranded());
9 ✔
533
    if (manual_) manual_->connect(endpoint);
9 ✔
534
}
9 ✔
535

536
void net::connect(const config::endpoint& endpoint,
2 ✔
537
    channel_notifier&& handler) NOEXCEPT
538
{
539
    // Public methods can complete on caller thread.
540
    if (closed())
2 ✔
541
    {
542
        handler(error::service_stopped, {});
×
543
        return;
×
544
    }
545

546
    boost::asio::post(strand_,
2 ✔
547
        std::bind(&net::do_connect_handled, this, endpoint,
4 ✔
548
            std::move(handler)));
549
}
550

551
void net::do_connect_handled(const config::endpoint& endpoint,
2 ✔
552
    const channel_notifier& handler) NOEXCEPT
553
{
554
    BC_ASSERT(stranded());
2 ✔
555

556
    if (manual_)
2 ✔
557
        manual_->connect(endpoint, move_copy(handler));
2 ✔
558
    else
559
        handler(error::service_stopped, nullptr);
1 ✔
560
}
2 ✔
561

562
// P2P Administration.
563
// ----------------------------------------------------------------------------
564

565
void net::dump_addresses(address_handler&& handler) NOEXCEPT
1 ✔
566
{
567
    fetch(std::move(handler));
1 ✔
568
}
1 ✔
569

570
// P2P Properties.
571
// ----------------------------------------------------------------------------
572

573
size_t net::address_count() const NOEXCEPT
7 ✔
574
{
575
    return hosts_.count();
7 ✔
576
}
577

578
config::address_counts net::address_counts() const NOEXCEPT
1 ✔
579
{
580
    return hosts_.counts();
1 ✔
581
}
582

583
size_t net::reserved_count() const NOEXCEPT
×
584
{
585
    return hosts_.reserved();
×
586
}
587

588
void net::fetch_totals(totals_handler&& handler) NOEXCEPT
×
589
{
590
    if (closed())
×
591
    {
592
        handler(error::service_stopped, {});
×
593
        return;
×
594
    }
595

596
    boost::asio::post(strand_,
×
597
        std::bind(&net::do_fetch_totals, this, std::move(handler)));
×
598
}
×
599

600
void net::do_fetch_totals(const totals_handler& handler) NOEXCEPT
×
601
{
602
    BC_ASSERT(stranded());
×
603

604
    handler(error::success,
×
605
    {
606
        peer_sent_.load(),
607
        peer_received_.load(),
608
        actives_,
×
609
    });
610
}
×
611

612
size_t net::channel_count() const NOEXCEPT
2 ✔
613
{
614
    return total_channel_count_;
2 ✔
615
}
616

617
size_t net::inbound_channel_count() const NOEXCEPT
8 ✔
618
{
619
    return inbound_channel_count_;
8 ✔
620
}
621

622
// P2P hosts collection.
623
// ----------------------------------------------------------------------------
624
// Protected, called from session (network strand) and channel (network pool).
625

626
// private
627
code net::start_hosts() NOEXCEPT
21 ✔
628
{
629
    return hosts_.start();
21 ✔
630
}
631

632
// private
633
code net::stop_hosts() NOEXCEPT
115 ✔
634
{
635
    return hosts_.stop();
115 ✔
636
}
637

638
void net::take(hosts::family family, address_item_handler&& handler) NOEXCEPT
3 ✔
639
{
640
    boost::asio::post(strand_,
3 ✔
641
        std::bind(&net::do_take, this, family, std::move(handler)));
3 ✔
642
}
3 ✔
643

644
void net::do_take(hosts::family family,
3 ✔
645
    const address_item_handler& handler) NOEXCEPT
646
{
647
    BC_ASSERT(stranded());
3 ✔
648
    hosts_.take(family, move_copy(handler));
3 ✔
649
}
3 ✔
650

651
void net::restore(const address_item_cptr& address,
17 ✔
652
    result_handler&& handler) NOEXCEPT
653
{
654
    boost::asio::post(strand_,
17 ✔
655
        std::bind(&net::do_restore, this, address, std::move(handler)));
17 ✔
656
}
17 ✔
657

658
void net::do_restore(const address_item_cptr& address,
17 ✔
659
    const result_handler& handler) NOEXCEPT
660
{
661
    BC_ASSERT(stranded());
17 ✔
662
    hosts_.restore(address, move_copy(handler));
17 ✔
663
}
17 ✔
664

665
void net::fetch(address_handler&& handler) NOEXCEPT
1 ✔
666
{
667
    boost::asio::post(strand_,
1 ✔
668
        std::bind(&net::do_fetch, this, std::move(handler)));
1 ✔
669
}
1 ✔
670

671
void net::do_fetch(const address_handler& handler) NOEXCEPT
1 ✔
672
{
673
    BC_ASSERT(stranded());
1 ✔
674

675
    // Accelerate stop, since hosts keeps running until all threads closed.
676
    if (closed())
1 ✔
677
    {
678
        handler(error::service_stopped, {});
×
679
        return;
×
680
    }
681

682
    hosts_.fetch(move_copy(handler));
2 ✔
683
}
684

685
void net::save(const address_cptr& message, count_handler&& handler) NOEXCEPT
×
686
{
687
    boost::asio::post(strand_,
×
688
        std::bind(&net::do_save, this, message, std::move(handler)));
×
689
}
×
690

691
void net::do_save(const address_cptr& message,
×
692
    const count_handler& handler) NOEXCEPT
693
{
694
    BC_ASSERT(stranded());
×
695

696
    // Accelerate stop, since hosts keeps running until all threads closed.
697
    if (closed())
×
698
    {
699
        handler(error::service_stopped, {});
×
700
        return;
×
701
    }
702

703
    hosts_.save(message, move_copy(handler));
×
704
}
705

706
// P2P self address.
707
// ----------------------------------------------------------------------------
708

709
// P2P loopback detection.
710
// ----------------------------------------------------------------------------
711

712
bool net::store_nonce(const channel_peer& channel) NOEXCEPT
17 ✔
713
{
714
    BC_ASSERT(stranded());
17 ✔
715

716
    if (network_settings().inbound.enable_loopback || channel.inbound())
17 ✔
717
        return true;
17 ✔
718

719
    if (!nonces_.insert(channel.nonce()).second)
×
720
    {
721
        LOGF("Failed to store nonce for [" << channel.endpoint() << "].");
×
722
        return false;
×
723
    }
724

725
    return true;
726
}
727

728
bool net::unstore_nonce(const channel_peer& channel) NOEXCEPT
17 ✔
729
{
730
    BC_ASSERT(stranded());
17 ✔
731

732
    if (network_settings().inbound.enable_loopback || channel.inbound())
17 ✔
733
        return true;
17 ✔
734

735
    if (!to_bool(nonces_.erase(channel.nonce())))
×
736
    {
737
        LOGF("Failed to unstore nonce for [" << channel.endpoint() << "].");
×
738
        return false;
×
739
    }
740

741
    return true;
742
}
743

744
bool net::is_loopback(const channel_peer& channel) const NOEXCEPT
6 ✔
745
{
746
    BC_ASSERT(stranded());
6 ✔
747

748
    if (network_settings().inbound.enable_loopback || !channel.inbound())
6 ✔
749
        return false;
×
750

751
    return to_bool(nonces_.count(channel.peer_version()->nonce));
12 ✔
752
}
753

754
// P2P channel counting with address deconfliction.
755
// ----------------------------------------------------------------------------
756

757
code net::count_channel(const channel_peer& channel) NOEXCEPT
6 ✔
758
{
759
    BC_ASSERT(stranded());
6 ✔
760

761
    if (closed())
6 ✔
762
        return error::service_stopped;
×
763

764
    if (is_loopback(channel))
6 ✔
765
    {
766
        LOGS("Loopback detected from [" << channel.endpoint() << "].");
×
767
        return error::accept_failed;
×
768
    }
769

770
    if (channel.inbound() && is_zero(add1(inbound_channel_count_.load())))
6 ✔
771
    {
772
        LOGF("Overflow: inbound channel count.");
×
773
        return error::channel_overflow;
×
774
    }
775

776
    if (!channel.quiet() && is_zero(add1(total_channel_count_.load())))
6 ✔
777
    {
778
        LOGF("Overflow: total channel count.");
×
779
        return error::channel_overflow;
×
780
    }
781

782
    if (!hosts_.reserve(channel.endpoint()))
6 ✔
783
    {
784
        LOGS("Duplicate connection to [" << channel.endpoint() << "].");
×
785
        return error::address_in_use;
×
786
    }
787

788
    if (channel.inbound())
6 ✔
789
        ++inbound_channel_count_;
790

791
    if (!channel.quiet())
6 ✔
792
        ++total_channel_count_;
793

794
    actives_.push_back(channel.identifier());
6 ✔
795
    return error::success;
6 ✔
796
}
797

798
void net::uncount_channel(const channel_peer& channel) NOEXCEPT
6 ✔
799
{
800
    BC_ASSERT(stranded());
6 ✔
801

802
    hosts_.unreserve(channel.endpoint());
6 ✔
803
    peer_sent_ = ceilinged_add(peer_sent_.load(), channel.sent());
6 ✔
804
    peer_received_ = ceilinged_add(peer_received_.load(), channel.received());
6 ✔
805
    std::erase(actives_, channel.identifier());
6 ✔
806

807
    if (channel.inbound() && is_zero(inbound_channel_count_.load()))
6 ✔
808
    {
809
        LOGF("Underflow: inbound channel count.");
×
810
        return;
×
811
    }
812

813
    if (!channel.quiet() && is_zero(total_channel_count_.load()))
6 ✔
814
    {
815
        LOGF("Underflow: total channel count.");
×
816
        return;
×
817
    }
818

819
    if (channel.inbound())
6 ✔
820
        --inbound_channel_count_;
821

822
    if (!channel.quiet())
6 ✔
823
        --total_channel_count_;
824
}
825

826
// Specializations (protected).
827
// ----------------------------------------------------------------------------
828

829
session_seed::ptr net::attach_seed_session() NOEXCEPT
6 ✔
830
{
831
    BC_ASSERT(stranded());
6 ✔
832
    return attach<session_seed>(*this);
6 ✔
833
}
834

835
session_manual::ptr net::attach_manual_session() NOEXCEPT
21 ✔
836
{
837
    BC_ASSERT(stranded());
21 ✔
838
    return attach<session_manual>(*this);
21 ✔
839
}
840

841
session_inbound::ptr net::attach_inbound_session() NOEXCEPT
5 ✔
842
{
843
    BC_ASSERT(stranded());
5 ✔
844
    return attach<session_inbound>(*this);
5 ✔
845
}
846

847
session_outbound::ptr net::attach_outbound_session() NOEXCEPT
5 ✔
848
{
849
    BC_ASSERT(stranded());
5 ✔
850
    return attach<session_outbound>(*this);
5 ✔
851
}
852

853
BC_POP_WARNING()
854

855
} // namespace network
856
} // 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