• 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

71.58
/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,
97 ✔
42
    uint64_t required_services, uint64_t provided_services) NOEXCEPT
97 ✔
43
  : settings_(settings),
97 ✔
44
    privacy_(to_bool(bit_and(provided_services, to_value(encryption)))),
97 ✔
45
    threadpool_(settings.threads_()),
97 ✔
46
    strand_(threadpool_.service().get_executor()),
97 ✔
47
    hosts_(settings, log, required_services),
97 ✔
48
    reporter(log)
194 ✔
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
}
97 ✔
61

62
net::~net() NOEXCEPT
97 ✔
63
{
64
    // Weak references in threadpool closures safe as net joins threads here.
65
    net::close();
97 ✔
66
}
291 ✔
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
3 ✔
80
{
81
    const auto& settings = network_settings();
3 ✔
82

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

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

97
    return emplace_shared<acceptor>(log, strand(), service(),
6 ✔
98
        accept_suspended_, std::move(params));
3 ✔
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
    {
NEW
109
        .connect_timeout = settings.connect_timeout(settings.inbound),
×
110
        .maximum_request = settings.inbound.maximum_request,
×
NEW
111
        .minimum_buffer = settings.inbound.minimum_buffer,
×
NEW
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) 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
    };
45 ✔
131

132
    if (privacy_)
45 ✔
133
        params.context = network_settings().peer_context();
×
134

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

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

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

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

157
    return to_connector(settings.outbound, settings.outbound,
14 ✔
158
        settings.outbound.seeding_timeout());
14 ✔
159
}
160

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

166
    return create_connector(settings.manual, settings.manual);
14 ✔
167
}
168

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

176
    for (size_t connect{}; connect < count; ++connect)
66 ✔
177
        connects->push_back(create_connector(settings.outbound,
114 ✔
178
            settings.outbound));
179

180
    return connects;
9 ✔
181
}
182

183
// Start sequence.
184
// ----------------------------------------------------------------------------
185

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

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

200
void net::handle_start(const code& ec, const result_handler& handler) NOEXCEPT
28 ✔
201
{
202
    BC_ASSERT(stranded());
28 ✔
203

204
    if (ec)
28 ✔
205
    {
206
        handler(ec);
4 ✔
207
        return;
4 ✔
208
    }
209

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

218
    attach_seed_session()->start(move_copy(handler));
88 ✔
219
}
220

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

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

233
    boost::asio::post(strand_,
14 ✔
234
        std::bind(&net::do_run, this, std::move(handler)));
28 ✔
235
}
236

237
void net::do_run(const result_handler& handler) NOEXCEPT
14 ✔
238
{
239
    BC_ASSERT(stranded());
14 ✔
240

241
    if (closed())
14 ✔
242
    {
243
        handler(error::service_stopped);
×
244
        return;
×
245
    }
246

247
    // Start manual connections.
248
    for (const auto& peer: network_settings().manual.peers)
19 ✔
249
        do_connect(peer);
5 ✔
250

251
    // Start inbound connections.
252
    attach_inbound_session()->start(
42 ✔
253
        std::bind(&net::handle_run, this, _1, handler));
28 ✔
254
}
255

256
void net::handle_run(const code& ec, const result_handler& handler) NOEXCEPT
14 ✔
257
{
258
    BC_ASSERT(stranded());
14 ✔
259

260
    if (ec)
14 ✔
261
    {
262
        handler(ec);
2 ✔
263
        return;
2 ✔
264
    }
265

266
    attach_outbound_session()->start(move_copy(handler));
48 ✔
267
}
268

269
// Shutdown sequence.
270
// ----------------------------------------------------------------------------
271

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

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

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

293
void net::do_close() NOEXCEPT
97 ✔
294
{
295
    BC_ASSERT(stranded());
97 ✔
296

297
    // Release reference to manual session (also held by stop subscriber).
298
    if (manual_) manual_.reset();
97 ✔
299

300
    // Notify and delete all stop subscribers (all sessions).
301
    stop_subscriber_.stop(error::service_stopped);
97 ✔
302

303
    // Notify and delete subscribers to channel notifications.
304
    connect_subscriber_.stop_default(error::service_stopped);
97 ✔
305

306
    // Notify and delete subscribers to message broadcast notifications.
307
    broadcaster_.stop(error::service_stopped);
97 ✔
308

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

313
bool net::closed() const NOEXCEPT
34 ✔
314
{
315
    return closed_.load();
34 ✔
316
}
317

318
// Suspensions.
319
// ----------------------------------------------------------------------------
320
// Service connections are managed independently from P2P.
321

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

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

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

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

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

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

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

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

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

370
// Properties.
371
// ----------------------------------------------------------------------------
372

373
const settings& net::network_settings() const NOEXCEPT
575 ✔
374
{
375
    return settings_;
575 ✔
376
}
377

378
asio::context& net::service() NOEXCEPT
99 ✔
379
{
380
    return threadpool_.service();
99 ✔
381
}
382

383
asio::strand& net::strand() NOEXCEPT
252 ✔
384
{
385
    return strand_;
252 ✔
386
}
387

388
bool net::stranded() const NOEXCEPT
1,488 ✔
389
{
390
    return strand_.running_in_this_thread();
1,488 ✔
391
}
392

393

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

516
    return keys_;
150 ✔
517
}
518

519
// P2P Manual connections.
520
// ----------------------------------------------------------------------------
521

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

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

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

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

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

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

560
// P2P Administration.
561
// ----------------------------------------------------------------------------
562

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

568
// P2P Properties.
569
// ----------------------------------------------------------------------------
570

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

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

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

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

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

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

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

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

615
size_t net::inbound_channel_count() const NOEXCEPT
7 ✔
616
{
617
    return inbound_channel_count_;
7 ✔
618
}
619

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

624
// private
625
code net::start_hosts() NOEXCEPT
20 ✔
626
{
627
    return hosts_.start();
20 ✔
628
}
629

630
// private
631
code net::stop_hosts() NOEXCEPT
110 ✔
632
{
633
    return hosts_.stop();
110 ✔
634
}
635

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

642
void net::do_take(const address_item_handler& handler) NOEXCEPT
3 ✔
643
{
644
    BC_ASSERT(stranded());
3 ✔
645
    hosts_.take(move_copy(handler));
3 ✔
646
}
3 ✔
647

648
void net::restore(const address_item_cptr& address,
12 ✔
649
    result_handler&& handler) NOEXCEPT
650
{
651
    boost::asio::post(strand_,
12 ✔
652
        std::bind(&net::do_restore, this, address, std::move(handler)));
12 ✔
653
}
12 ✔
654

655
void net::do_restore(const address_item_cptr& address,
12 ✔
656
    const result_handler& handler) NOEXCEPT
657
{
658
    BC_ASSERT(stranded());
12 ✔
659
    hosts_.restore(address, move_copy(handler));
12 ✔
660
}
12 ✔
661

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

668
void net::do_fetch(const address_handler& handler) NOEXCEPT
1 ✔
669
{
670
    BC_ASSERT(stranded());
1 ✔
671

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

679
    hosts_.fetch(move_copy(handler));
2 ✔
680
}
681

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

688
void net::do_save(const address_cptr& message,
×
689
    const count_handler& handler) NOEXCEPT
690
{
691
    BC_ASSERT(stranded());
×
692

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

700
    hosts_.save(message, move_copy(handler));
×
701
}
702

703
// P2P self address.
704
// ----------------------------------------------------------------------------
705

706
// P2P loopback detection.
707
// ----------------------------------------------------------------------------
708

709
bool net::store_nonce(const channel_peer& channel) NOEXCEPT
16 ✔
710
{
711
    BC_ASSERT(stranded());
16 ✔
712

713
    if (network_settings().inbound.enable_loopback || channel.inbound())
16 ✔
714
        return true;
16 ✔
715

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

722
    return true;
723
}
724

725
bool net::unstore_nonce(const channel_peer& channel) NOEXCEPT
16 ✔
726
{
727
    BC_ASSERT(stranded());
16 ✔
728

729
    if (network_settings().inbound.enable_loopback || channel.inbound())
16 ✔
730
        return true;
16 ✔
731

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

738
    return true;
739
}
740

741
bool net::is_loopback(const channel_peer& channel) const NOEXCEPT
4 ✔
742
{
743
    BC_ASSERT(stranded());
4 ✔
744

745
    if (network_settings().inbound.enable_loopback || !channel.inbound())
4 ✔
746
        return false;
×
747

748
    return to_bool(nonces_.count(channel.peer_version()->nonce));
8 ✔
749
}
750

751
// P2P channel counting with address deconfliction.
752
// ----------------------------------------------------------------------------
753

754
code net::count_channel(const channel_peer& channel) NOEXCEPT
6 ✔
755
{
756
    BC_ASSERT(stranded());
6 ✔
757

758
    if (closed())
6 ✔
759
        return error::service_stopped;
2 ✔
760

761
    if (is_loopback(channel))
4 ✔
762
    {
763
        LOGS("Loopback detected from [" << channel.endpoint() << "].");
×
764
        return error::accept_failed;
×
765
    }
766

767
    if (channel.inbound() && is_zero(add1(inbound_channel_count_.load())))
4 ✔
768
    {
769
        LOGF("Overflow: inbound channel count.");
×
770
        return error::channel_overflow;
×
771
    }
772

773
    if (!channel.quiet() && is_zero(add1(total_channel_count_.load())))
4 ✔
774
    {
775
        LOGF("Overflow: total channel count.");
×
776
        return error::channel_overflow;
×
777
    }
778

779
    if (!hosts_.reserve(channel.endpoint()))
4 ✔
780
    {
781
        LOGS("Duplicate connection to [" << channel.endpoint() << "].");
×
782
        return error::address_in_use;
×
783
    }
784

785
    if (channel.inbound())
4 ✔
786
        ++inbound_channel_count_;
787

788
    if (!channel.quiet())
4 ✔
789
        ++total_channel_count_;
790

791
    actives_.push_back(channel.identifier());
4 ✔
792
    return error::success;
4 ✔
793
}
794

795
void net::uncount_channel(const channel_peer& channel) NOEXCEPT
4 ✔
796
{
797
    BC_ASSERT(stranded());
4 ✔
798

799
    hosts_.unreserve(channel.endpoint());
4 ✔
800
    peer_sent_ = ceilinged_add(peer_sent_.load(), channel.sent());
4 ✔
801
    peer_received_ = ceilinged_add(peer_received_.load(), channel.received());
4 ✔
802
    std::erase(actives_, channel.identifier());
4 ✔
803

804
    if (channel.inbound() && is_zero(inbound_channel_count_.load()))
4 ✔
805
    {
806
        LOGF("Underflow: inbound channel count.");
×
807
        return;
×
808
    }
809

810
    if (!channel.quiet() && is_zero(total_channel_count_.load()))
4 ✔
811
    {
812
        LOGF("Underflow: total channel count.");
×
813
        return;
×
814
    }
815

816
    if (channel.inbound())
4 ✔
817
        --inbound_channel_count_;
818

819
    if (!channel.quiet())
4 ✔
820
        --total_channel_count_;
821
}
822

823
// Specializations (protected).
824
// ----------------------------------------------------------------------------
825

826
session_seed::ptr net::attach_seed_session() NOEXCEPT
5 ✔
827
{
828
    BC_ASSERT(stranded());
5 ✔
829
    return attach<session_seed>(*this);
5 ✔
830
}
831

832
session_manual::ptr net::attach_manual_session() NOEXCEPT
20 ✔
833
{
834
    BC_ASSERT(stranded());
20 ✔
835
    return attach<session_manual>(*this);
20 ✔
836
}
837

838
session_inbound::ptr net::attach_inbound_session() NOEXCEPT
4 ✔
839
{
840
    BC_ASSERT(stranded());
4 ✔
841
    return attach<session_inbound>(*this);
4 ✔
842
}
843

844
session_outbound::ptr net::attach_outbound_session() NOEXCEPT
4 ✔
845
{
846
    BC_ASSERT(stranded());
4 ✔
847
    return attach<session_outbound>(*this);
4 ✔
848
}
849

850
BC_POP_WARNING()
851

852
} // namespace network
853
} // 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