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

libbitcoin / libbitcoin-network / 34367935460

09 Sep 2026 03:05PM UTC coverage: 16.122% (+0.002%) from 16.12%
34367935460

push

github

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

Publish interface subset by its own methods type.

8725 of 54120 relevant lines covered (16.12%)

121.66 hits per line

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

75.67
/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

40
net::net(const settings& settings, const logger& log,
97 ✔
41
    uint64_t required_services) NOEXCEPT
97 ✔
42
  : settings_(settings),
97 ✔
43
    p2ps_{ settings.identifier },
97 ✔
44
    threadpool_(settings.threads_()),
97 ✔
45
    strand_(threadpool_.service().get_executor()),
97 ✔
46
    hosts_(settings, log, required_services),
97 ✔
47
    reporter(log)
194 ✔
48
{
49
    ////LOG_LOG("Aplication log compiled..: ", news_defined);
50
    ////LOG_LOG("News log compiled........: ", news_defined);
51
    ////LOG_LOG("Session log compiled.....: ", session_defined);
52
    ////LOG_LOG("Protocol log compiled....: ", protocol_defined);
53
    ////LOG_LOG("ProXy log compiled.......: ", proxy_defined);
54
    ////LOG_LOG("Remote log compiled......: ", remote_defined);
55
    ////LOG_LOG("Fault log compiled.......: ", fault_defined);
56
    ////LOG_LOG("Quitting log compiled....: ", quitting_defined);
57
    ////LOG_LOG("Object log compiled......: ", objects_defined);
58
    ////LOG_LOG("Verbose log compiled.....: ", verbose_defined);
59
}
97 ✔
60

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

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

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

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

82
    // bip324 (v2) inbound acceptance, v1 peers detected and passed through.
83
    const auto accept = settings.enable_privacy &&
×
84
        std::holds_alternative<std::monostate>(context) ?
3 ✔
85
            socket::context{ std::cref(p2ps_) } : context;
3 ✔
86

87
    socket::parameters params
3 ✔
88
    {
89
        .connect_timeout = settings.connect_timeout(),
3 ✔
90
        .maximum_request = settings.inbound.maximum_request,
3 ✔
91
        .context = accept
92
    };
3 ✔
93

94
    return emplace_shared<acceptor>(log, strand(), service(),
6 ✔
95
        accept_suspended_, std::move(params));
3 ✔
96
}
97

98
// outbound (general)
99
connector::ptr net::create_connector(const settings::socks5& socks,
45 ✔
100
    const steady_clock::duration& connect_timeout,
101
    uint32_t maximum_request) NOEXCEPT
102
{
103
    socket::parameters params
45 ✔
104
    {
105
        .connect_timeout = connect_timeout,
106
        .maximum_request = maximum_request
45 ✔
107
    };
45 ✔
108

109
    if (network_settings().enable_privacy)
45 ✔
110
        params.context = std::cref(p2ps_);
×
111

112
    if (socks.proxied())
45 ✔
113
        return emplace_shared<connector_socks>(log, strand(), service(),
×
114
            connect_suspended_, std::move(params), socks);
×
115

116
    // Above can handle both proxy and non-proxy, but this is more efficient.
117
    return emplace_shared<connector>(log, strand(), service(),
90 ✔
118
        connect_suspended_, std::move(params));
45 ✔
119
}
120

121
// outbound (seed)
122
connector::ptr net::create_seed_connector() NOEXCEPT
14 ✔
123
{
124
    const auto& settings = network_settings();
14 ✔
125

126
    return create_connector(settings.outbound,
14 ✔
127
        settings.outbound.seeding_timeout(),
28 ✔
128
        settings.outbound.maximum_request);
14 ✔
129
}
130

131
// outbound (manual)
132
connector::ptr net::create_manual_connector() NOEXCEPT
14 ✔
133
{
134
    const auto& settings = network_settings();
14 ✔
135

136
    return create_connector(settings.manual,
14 ✔
137
        settings.connect_timeout(),
28 ✔
138
        settings.manual.maximum_request);
14 ✔
139
}
140

141
// outbound (batch)
142
connectors_ptr net::create_connectors(size_t count) NOEXCEPT
9 ✔
143
{
144
    const auto& settings = network_settings();
9 ✔
145
    const auto connects = to_shared<connectors>();
146
    connects->reserve(count);
9 ✔
147

148
    for (size_t connect{}; connect < count; ++connect)
66 ✔
149
        connects->push_back(create_connector(settings.outbound,
57 ✔
150
            settings.connect_timeout(),
57 ✔
151
            settings.outbound.maximum_request));
57 ✔
152

153
    return connects;
9 ✔
154
}
155

156
// Start sequence.
157
// ----------------------------------------------------------------------------
158

159
void net::start(result_handler&& handler) NOEXCEPT
28 ✔
160
{
161
    // Threadpool is started on construct, stopped only by work starvation.
162
    boost::asio::post(strand_,
28 ✔
163
        std::bind(&net::do_start, this, std::move(handler)));
28 ✔
164
}
28 ✔
165

166
void net::do_start(const result_handler& handler) NOEXCEPT
28 ✔
167
{
168
    BC_ASSERT(stranded());
28 ✔
169
    manual_ = attach_manual_session();
56 ✔
170
    manual_->start(std::bind(&net::handle_start, this, _1, handler));
28 ✔
171
}
28 ✔
172

173
void net::handle_start(const code& ec, const result_handler& handler) NOEXCEPT
28 ✔
174
{
175
    BC_ASSERT(stranded());
28 ✔
176

177
    if (ec)
28 ✔
178
    {
179
        handler(ec);
4 ✔
180
        return;
4 ✔
181
    }
182

183
    // Deserialize hosts from file.
184
    if (const auto error_code = start_hosts())
24 ✔
185
    {
186
        LOGF("Hosts file failed to deserialize, " << error_code.message());
4 ✔
187
        handler(error_code);
2 ✔
188
        return;
2 ✔
189
    }
190

191
    attach_seed_session()->start(move_copy(handler));
88 ✔
192
}
193

194
// Run sequence (seeding may be ongoing after its handler is invoked).
195
// ----------------------------------------------------------------------------
196

197
void net::run(result_handler&& handler) NOEXCEPT
19 ✔
198
{
199
    // Public methods can complete on caller thread.
200
    if (closed())
19 ✔
201
    {
202
        handler(error::service_stopped);
5 ✔
203
        return;
5 ✔
204
    }
205

206
    boost::asio::post(strand_,
14 ✔
207
        std::bind(&net::do_run, this, std::move(handler)));
28 ✔
208
}
209

210
void net::do_run(const result_handler& handler) NOEXCEPT
14 ✔
211
{
212
    BC_ASSERT(stranded());
14 ✔
213

214
    if (closed())
14 ✔
215
    {
216
        handler(error::service_stopped);
×
217
        return;
×
218
    }
219

220
    // Start manual connections.
221
    for (const auto& peer: network_settings().manual.peers)
19 ✔
222
        do_connect(peer);
5 ✔
223

224
    // Start inbound connections.
225
    attach_inbound_session()->start(
42 ✔
226
        std::bind(&net::handle_run, this, _1, handler));
28 ✔
227
}
228

229
void net::handle_run(const code& ec, const result_handler& handler) NOEXCEPT
14 ✔
230
{
231
    BC_ASSERT(stranded());
14 ✔
232

233
    if (ec)
14 ✔
234
    {
235
        handler(ec);
2 ✔
236
        return;
2 ✔
237
    }
238

239
    attach_outbound_session()->start(move_copy(handler));
48 ✔
240
}
241

242
// Shutdown sequence.
243
// ----------------------------------------------------------------------------
244

245
// Results in std::abort if called from a thread within the threadpool.
246
void net::close() NOEXCEPT
110 ✔
247
{
248
    closed_.store(true);
249
    boost::asio::post(strand_,
110 ✔
250
        std::bind(&net::do_close, this));
110 ✔
251

252
    // Blocks on join of all threadpool threads.
253
    if (!threadpool_.join())
110 ✔
254
    {
255
        BC_ASSERT_MSG(false, "failed to join threadpool");
×
256
        std::abort();
257
    }
258

259
    // Serialize hosts to file.
260
    if (const auto error_code = stop_hosts())
110 ✔
261
    {
262
        LOGF("Hosts file failed to serialize, " << error_code.message());
×
263
    }
264
}
110 ✔
265

266
void net::do_close() NOEXCEPT
97 ✔
267
{
268
    BC_ASSERT(stranded());
97 ✔
269

270
    // Release reference to manual session (also held by stop subscriber).
271
    if (manual_) manual_.reset();
97 ✔
272

273
    // Notify and delete all stop subscribers (all sessions).
274
    stop_subscriber_.stop(error::service_stopped);
97 ✔
275

276
    // Notify and delete subscribers to channel notifications.
277
    connect_subscriber_.stop_default(error::service_stopped);
97 ✔
278

279
    // Notify and delete subscribers to message broadcast notifications.
280
    broadcaster_.stop(error::service_stopped);
97 ✔
281

282
    // Stop threadpool keep-alive, all work must self-terminate to affect join.
283
    threadpool_.stop();
97 ✔
284
}
97 ✔
285

286
bool net::closed() const NOEXCEPT
33 ✔
287
{
288
    return closed_.load();
33 ✔
289
}
290

291
// Suspensions.
292
// ----------------------------------------------------------------------------
293
// Service connections are managed independently from P2P.
294

295
void net::suspend_services() NOEXCEPT
×
296
{
297
    service_suspended_.store(true);
298
}
×
299

300
void net::resume_services() NOEXCEPT
×
301
{
302
    service_suspended_.store(false);
303
}
×
304

305
void net::suspend(const code&) NOEXCEPT
×
306
{
307
    suspend_acceptors();
×
308
    suspend_connectors();
×
309
}
×
310

311
bool net::resume() NOEXCEPT
×
312
{
313
    resume_acceptors();
×
314
    resume_connectors();
×
315
    return true;
×
316
}
317

318
bool net::suspended() const NOEXCEPT
×
319
{
320
    return connect_suspended_.load() || accept_suspended_.load();
×
321
}
322

323
void net::suspend_acceptors() NOEXCEPT
×
324
{
325
    accept_suspended_.store(true);
326
}
×
327

328
void net::resume_acceptors() NOEXCEPT
×
329
{
330
    accept_suspended_.store(false);
331
}
×
332

333
void net::suspend_connectors() NOEXCEPT
×
334
{
335
    connect_suspended_.store(true);
336
}
×
337

338
void net::resume_connectors() NOEXCEPT
×
339
{
340
    connect_suspended_.store(false);
341
}
×
342

343
// Properties.
344
// ----------------------------------------------------------------------------
345

346
const settings& net::network_settings() const NOEXCEPT
544 ✔
347
{
348
    return settings_;
544 ✔
349
}
350

351
asio::context& net::service() NOEXCEPT
99 ✔
352
{
353
    return threadpool_.service();
99 ✔
354
}
355

356
asio::strand& net::strand() NOEXCEPT
255 ✔
357
{
358
    return strand_;
255 ✔
359
}
360

361
bool net::stranded() const NOEXCEPT
1,486 ✔
362
{
363
    return strand_.running_in_this_thread();
1,486 ✔
364
}
365

366

367
// Subscriptions.
368
// ----------------------------------------------------------------------------
369
// Channel and network strands share same pool, and as long as a job is
370
// running in the pool, it will continue to accept work. Therefore handlers
371
// will not be orphaned during a stop as long as they remain in the pool.
372
// But when entering from outside the pool (such as subscribe) handler must be
373
// invoked when stopped as the handler will go uninvoked if the pool empties.
374

375
// public
376
void net::subscribe_connect(channel_notifier&& handler,
2 ✔
377
    channel_completer&& complete) NOEXCEPT
378
{
379
    // Public methods can complete on caller thread.
380
    if (closed())
2 ✔
381
    {
382
        complete(error::service_stopped, {});
1 ✔
383
        handler(error::service_stopped, {});
1 ✔
384
        return;
1 ✔
385
    }
386

387
    boost::asio::post(strand_,
1 ✔
388
        std::bind(&net::do_subscribe_connect,
2 ✔
389
            this, std::move(handler), std::move(complete)));
390
}
391

392
void net::do_subscribe_connect(const channel_notifier& handler,
1 ✔
393
    const channel_completer& complete) NOEXCEPT
394
{
395
    BC_ASSERT(stranded());
1 ✔
396

397
    const auto key = create_key();
1 ✔
398
    complete(connect_subscriber_.subscribe(move_copy(handler), key), key);
1 ✔
399
}
1 ✔
400

401
// protected
402
void net::notify_connect(const channel::ptr& channel) NOEXCEPT
4 ✔
403
{
404
    boost::asio::post(strand_,
4 ✔
405
        std::bind(&net::do_notify_connect, this, channel));
4 ✔
406
}
4 ✔
407

408
void net::do_notify_connect(const channel::ptr& channel) NOEXCEPT
4 ✔
409
{
410
    BC_ASSERT(stranded());
4 ✔
411
    connect_subscriber_.notify(error::success, channel);
4 ✔
412
}
4 ✔
413

414
void net::unsubscribe_connect(object_key key) NOEXCEPT
×
415
{
416
    boost::asio::post(strand_,
×
417
        std::bind(&net::do_unsubscribe_connect, this, key));
×
418
}
×
419

420
void net::do_unsubscribe_connect(object_key key) NOEXCEPT
×
421
{
422
    BC_ASSERT(stranded());
×
423
    connect_subscriber_.notify_one(key, error::desubscribed, nullptr);
×
424
}
×
425

426
// protected
427
void net::subscribe_close(stop_handler&& handler) NOEXCEPT
×
428
{
429
    BC_ASSERT(stranded());
×
430
    subscribe_close(std::move(handler), create_key());
×
431
}
×
432

433
// private
434
code net::subscribe_close(stop_handler&& handler, object_key key) NOEXCEPT
77 ✔
435
{
436
    BC_ASSERT(stranded());
77 ✔
437
    return stop_subscriber_.subscribe(std::move(handler), key);
77 ✔
438
}
439

440
// public
441
void net::subscribe_close(stop_handler&& handler,
2 ✔
442
    stop_completer&& complete) NOEXCEPT
443
{
444
    // Public methods can complete on caller thread.
445
    if (closed())
2 ✔
446
    {
447
        complete(error::service_stopped, {});
1 ✔
448
        handler(error::service_stopped);
1 ✔
449
        return;
1 ✔
450
    }
451

452
    boost::asio::post(strand_,
1 ✔
453
        std::bind(&net::do_subscribe_close,
2 ✔
454
            this, std::move(handler), std::move(complete)));
455
}
456

457
void net::do_subscribe_close(const stop_handler& handler,
1 ✔
458
    const stop_completer& complete) NOEXCEPT
459
{
460
    BC_ASSERT(stranded());
1 ✔
461
    
462
    const auto key = create_key();
1 ✔
463
    complete(subscribe_close(move_copy(handler), key), key);
1 ✔
464
}
1 ✔
465

466
void net::unsubscribe_close(object_key key) NOEXCEPT
27 ✔
467
{
468
    boost::asio::post(strand_,
27 ✔
469
        std::bind(&net::do_unsubscribe_close, this, key));
27 ✔
470
}
27 ✔
471

472
void net::do_unsubscribe_close(object_key key) NOEXCEPT
27 ✔
473
{
474
    BC_ASSERT(stranded());
27 ✔
475
    stop_subscriber_.notify_one(key, error::desubscribed);
27 ✔
476
}
27 ✔
477

478
// At one object/session/ns, this overflows in ~585 years (and handled).
479
net::object_key net::create_key() NOEXCEPT
150 ✔
480
{
481
    BC_ASSERT(stranded());
150 ✔
482

483
    if (is_zero(++keys_))
150 ✔
484
    {
485
        BC_ASSERT_MSG(false, "overflow");
×
486
        LOGF("Session object overflow.");
487
    }
488

489
    return keys_;
150 ✔
490
}
491

492
// P2P Manual connections.
493
// ----------------------------------------------------------------------------
494

495
void net::connect(const config::endpoint& endpoint) NOEXCEPT
4 ✔
496
{
497
    boost::asio::post(strand_,
4 ✔
498
        std::bind(&net::do_connect, this, endpoint));
4 ✔
499
}
4 ✔
500

501
void net::do_connect(const config::endpoint& endpoint) NOEXCEPT
9 ✔
502
{
503
    BC_ASSERT(stranded());
9 ✔
504
    if (manual_) manual_->connect(endpoint);
9 ✔
505
}
9 ✔
506

507
void net::connect(const config::endpoint& endpoint,
2 ✔
508
    channel_notifier&& handler) NOEXCEPT
509
{
510
    // Public methods can complete on caller thread.
511
    if (closed())
2 ✔
512
    {
513
        handler(error::service_stopped, {});
×
514
        return;
×
515
    }
516

517
    boost::asio::post(strand_,
2 ✔
518
        std::bind(&net::do_connect_handled, this, endpoint,
4 ✔
519
            std::move(handler)));
520
}
521

522
void net::do_connect_handled(const config::endpoint& endpoint,
2 ✔
523
    const channel_notifier& handler) NOEXCEPT
524
{
525
    BC_ASSERT(stranded());
2 ✔
526

527
    if (manual_)
2 ✔
528
        manual_->connect(endpoint, move_copy(handler));
2 ✔
529
    else
530
        handler(error::service_stopped, nullptr);
1 ✔
531
}
2 ✔
532

533
// P2P Administration.
534
// ----------------------------------------------------------------------------
535

536
void net::dump_addresses(address_handler&& handler) NOEXCEPT
1 ✔
537
{
538
    fetch(std::move(handler));
1 ✔
539
}
1 ✔
540

541
// P2P Properties.
542
// ----------------------------------------------------------------------------
543

544
size_t net::address_count() const NOEXCEPT
7 ✔
545
{
546
    return hosts_.count();
7 ✔
547
}
548

549
config::address_counts net::address_counts() const NOEXCEPT
1 ✔
550
{
551
    return hosts_.counts();
1 ✔
552
}
553

554
size_t net::reserved_count() const NOEXCEPT
×
555
{
556
    return hosts_.reserved();
×
557
}
558

559
size_t net::channel_count() const NOEXCEPT
2 ✔
560
{
561
    return total_channel_count_;
2 ✔
562
}
563

564
size_t net::inbound_channel_count() const NOEXCEPT
7 ✔
565
{
566
    return inbound_channel_count_;
7 ✔
567
}
568

569
// P2P hosts collection.
570
// ----------------------------------------------------------------------------
571
// Protected, called from session (network strand) and channel (network pool).
572

573
// private
574
code net::start_hosts() NOEXCEPT
20 ✔
575
{
576
    return hosts_.start();
20 ✔
577
}
578

579
// private
580
code net::stop_hosts() NOEXCEPT
110 ✔
581
{
582
    return hosts_.stop();
110 ✔
583
}
584

585
void net::take(address_item_handler&& handler) NOEXCEPT
3 ✔
586
{
587
    boost::asio::post(strand_,
3 ✔
588
        std::bind(&net::do_take, this, std::move(handler)));
3 ✔
589
}
3 ✔
590

591
void net::do_take(const address_item_handler& handler) NOEXCEPT
3 ✔
592
{
593
    BC_ASSERT(stranded());
3 ✔
594
    hosts_.take(move_copy(handler));
3 ✔
595
}
3 ✔
596

597
void net::restore(const address_item_cptr& address,
14 ✔
598
    result_handler&& handler) NOEXCEPT
599
{
600
    boost::asio::post(strand_,
14 ✔
601
        std::bind(&net::do_restore, this, address, std::move(handler)));
14 ✔
602
}
14 ✔
603

604
void net::do_restore(const address_item_cptr& address,
14 ✔
605
    const result_handler& handler) NOEXCEPT
606
{
607
    BC_ASSERT(stranded());
14 ✔
608
    hosts_.restore(address, move_copy(handler));
14 ✔
609
}
14 ✔
610

611
void net::fetch(address_handler&& handler) NOEXCEPT
1 ✔
612
{
613
    boost::asio::post(strand_,
1 ✔
614
        std::bind(&net::do_fetch, this, std::move(handler)));
1 ✔
615
}
1 ✔
616

617
void net::do_fetch(const address_handler& handler) NOEXCEPT
1 ✔
618
{
619
    BC_ASSERT(stranded());
1 ✔
620

621
    // Accelerate stop, since hosts keeps running until all threads closed.
622
    if (closed())
1 ✔
623
    {
624
        handler(error::service_stopped, {});
×
625
        return;
×
626
    }
627

628
    hosts_.fetch(move_copy(handler));
2 ✔
629
}
630

631
void net::save(const address_cptr& message, count_handler&& handler) NOEXCEPT
×
632
{
633
    boost::asio::post(strand_,
×
634
        std::bind(&net::do_save, this, message, std::move(handler)));
×
635
}
×
636

637
void net::do_save(const address_cptr& message,
×
638
    const count_handler& handler) NOEXCEPT
639
{
640
    BC_ASSERT(stranded());
×
641

642
    // Accelerate stop, since hosts keeps running until all threads closed.
643
    if (closed())
×
644
    {
645
        handler(error::service_stopped, {});
×
646
        return;
×
647
    }
648

649
    hosts_.save(message, move_copy(handler));
×
650
}
651

652
// P2P loopback detection.
653
// ----------------------------------------------------------------------------
654

655
bool net::store_nonce(const channel_peer& channel) NOEXCEPT
16 ✔
656
{
657
    BC_ASSERT(stranded());
16 ✔
658

659
    if (network_settings().inbound.enable_loopback || channel.inbound())
16 ✔
660
        return true;
16 ✔
661

662
    if (!nonces_.insert(channel.nonce()).second)
×
663
    {
664
        LOGF("Failed to store nonce for [" << channel.endpoint() << "].");
×
665
        return false;
×
666
    }
667

668
    return true;
669
}
670

671
bool net::unstore_nonce(const channel_peer& channel) NOEXCEPT
16 ✔
672
{
673
    BC_ASSERT(stranded());
16 ✔
674

675
    if (network_settings().inbound.enable_loopback || channel.inbound())
16 ✔
676
        return true;
16 ✔
677

678
    if (!to_bool(nonces_.erase(channel.nonce())))
×
679
    {
680
        LOGF("Failed to unstore nonce for [" << channel.endpoint() << "].");
×
681
        return false;
×
682
    }
683

684
    return true;
685
}
686

687
bool net::is_loopback(const channel_peer& channel) const NOEXCEPT
5 ✔
688
{
689
    BC_ASSERT(stranded());
5 ✔
690

691
    if (network_settings().inbound.enable_loopback || !channel.inbound())
5 ✔
692
        return false;
×
693

694
    return to_bool(nonces_.count(channel.peer_version()->nonce));
10 ✔
695
}
696

697
// P2P channel counting with address deconfliction.
698
// ----------------------------------------------------------------------------
699

700
code net::count_channel(const channel_peer& channel) NOEXCEPT
5 ✔
701
{
702
    BC_ASSERT(stranded());
5 ✔
703

704
    if (closed())
5 ✔
705
        return error::service_stopped;
×
706

707
    if (is_loopback(channel))
5 ✔
708
    {
709
        LOGS("Loopback detected from [" << channel.endpoint() << "].");
×
710
        return error::accept_failed;
×
711
    }
712

713
    if (channel.inbound() && is_zero(add1(inbound_channel_count_.load())))
5 ✔
714
    {
715
        LOGF("Overflow: inbound channel count.");
×
716
        return error::channel_overflow;
×
717
    }
718

719
    if (!channel.quiet() && is_zero(add1(total_channel_count_.load())))
5 ✔
720
    {
721
        LOGF("Overflow: total channel count.");
×
722
        return error::channel_overflow;
×
723
    }
724

725
    if (!hosts_.reserve(channel.endpoint()))
5 ✔
726
    {
727
        LOGS("Duplicate connection to [" << channel.endpoint() << "].");
×
728
        return error::address_in_use;
×
729
    }
730

731
    if (channel.inbound())
5 ✔
732
        ++inbound_channel_count_;
733

734
    if (!channel.quiet())
5 ✔
735
        ++total_channel_count_;
736

737
    return error::success;
5 ✔
738
}
739

740
void net::uncount_channel(const channel_peer& channel) NOEXCEPT
5 ✔
741
{
742
    BC_ASSERT(stranded());
5 ✔
743

744
    hosts_.unreserve(channel.endpoint());
5 ✔
745

746
    if (channel.inbound() && is_zero(inbound_channel_count_.load()))
5 ✔
747
    {
748
        LOGF("Underflow: inbound channel count.");
×
749
        return;
×
750
    }
751

752
    if (!channel.quiet() && is_zero(total_channel_count_.load()))
5 ✔
753
    {
754
        LOGF("Underflow: total channel count.");
×
755
        return;
×
756
    }
757

758
    if (channel.inbound())
5 ✔
759
        --inbound_channel_count_;
760

761
    if (!channel.quiet())
5 ✔
762
        --total_channel_count_;
763
}
764

765
// Specializations (protected).
766
// ----------------------------------------------------------------------------
767

768
session_seed::ptr net::attach_seed_session() NOEXCEPT
5 ✔
769
{
770
    BC_ASSERT(stranded());
5 ✔
771
    return attach<session_seed>(*this);
5 ✔
772
}
773

774
session_manual::ptr net::attach_manual_session() NOEXCEPT
20 ✔
775
{
776
    BC_ASSERT(stranded());
20 ✔
777
    return attach<session_manual>(*this);
20 ✔
778
}
779

780
session_inbound::ptr net::attach_inbound_session() NOEXCEPT
4 ✔
781
{
782
    BC_ASSERT(stranded());
4 ✔
783
    return attach<session_inbound>(*this);
4 ✔
784
}
785

786
session_outbound::ptr net::attach_outbound_session() NOEXCEPT
4 ✔
787
{
788
    BC_ASSERT(stranded());
4 ✔
789
    return attach<session_outbound>(*this);
4 ✔
790
}
791

792
BC_POP_WARNING()
793

794
} // namespace network
795
} // 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