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

libbitcoin / libbitcoin-network / 38012391247

10 Oct 2026 01:14AM UTC coverage: 94.985% (+0.04%) from 94.948%
38012391247

push

github

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

Fix debug build break.

10852 of 11425 relevant lines covered (94.98%)

667.88 hits per line

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

89.89
/src/protocols/protocol_peer.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/protocols/protocol_peer.hpp>
20

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

30
namespace libbitcoin {
31
namespace network {
32

33
#define CLASS protocol_peer
34

35
using namespace system;
36
using namespace messages::peer;
37
using namespace std::placeholders;
38

39
protocol_peer::protocol_peer(const session::ptr& session,
269 ✔
40
    const channel::ptr& channel) NOEXCEPT
269 ✔
41
  : protocol(session, channel),
42
    channel_(std::dynamic_pointer_cast<channel_peer>(channel)),
269 ✔
43
    session_(std::dynamic_pointer_cast<session_peer>(session))
269 ✔
44
{
45
}
269 ✔
46

47
// Properties.
48
// ----------------------------------------------------------------------------
49
// The public properties may be accessed outside the strand, except during
50
// handshake protocol operation. Thread safety requires that setters are never
51
// invoked outside of the handshake protocol (start handler).
52

53
bool protocol_peer::encrypted() const NOEXCEPT
103 ✔
54
{
55
    return channel_->encrypted();
103 ✔
56
}
57

58
size_t protocol_peer::start_height() const NOEXCEPT
107 ✔
59
{
60
    return channel_->start_height();
107 ✔
61
}
62

63
version::cptr protocol_peer::peer_version() const NOEXCEPT
4 ✔
64
{
65
    return channel_->peer_version();
4 ✔
66
}
67

68
// Call only from handshake (version protocol), for thread safety.
69
void protocol_peer::set_peer_version(const version::cptr& value) NOEXCEPT
95 ✔
70
{
71
    channel_->set_peer_version(value);
95 ✔
72
}
95 ✔
73

74
uint32_t protocol_peer::negotiated_version() const NOEXCEPT
14 ✔
75
{
76
    return channel_->negotiated_version();
14 ✔
77
}
78

79
// Call only from handshake (version protocol), for thread safety.
80
void protocol_peer::set_negotiated_version(uint32_t value) NOEXCEPT
95 ✔
81
{
82
    channel_->set_negotiated_version(value);
95 ✔
83
}
95 ✔
84

85
bool protocol_peer::wants_address_v2() const NOEXCEPT
2 ✔
86
{
87
    return channel_->wants_address_v2();
2 ✔
88
}
89

90
// Call only from handshake (version protocol), for thread safety.
91
void protocol_peer::set_wants_address_v2() NOEXCEPT
8 ✔
92
{
93
    channel_->set_wants_address_v2();
8 ✔
94
}
8 ✔
95

96
bool protocol_peer::accepts_compact_blocks() const NOEXCEPT
×
97
{
98
    return channel_->accepts_compact_blocks();
×
99
}
100

101
bool protocol_peer::wants_compact_blocks() const NOEXCEPT
×
102
{
103
    return channel_->wants_compact_blocks();
×
104
}
105

106
// Call only from the version protocol, for thread safety.
107
void protocol_peer::set_compact_blocks(bool high_bandwidth) NOEXCEPT
×
108
{
109
    channel_->set_compact_blocks(high_bandwidth);
×
110
}
×
111

112
bool protocol_peer::wants_witness_tx() const NOEXCEPT
×
113
{
114
    return channel_->wants_witness_tx();
×
115
}
116

117
// Call only from handshake (version protocol), for thread safety.
118
void protocol_peer::set_wants_witness_tx() NOEXCEPT
1 ✔
119
{
120
    channel_->set_wants_witness_tx();
1 ✔
121
}
1 ✔
122

123
address protocol_peer::selfs() const NOEXCEPT
9 ✔
124
{
125
    const auto time_now = unix_time();
9 ✔
126
    const auto services = session_->services_provided();
9 ✔
127
    auto selfs = network_settings().inbound.selfs;
9 ✔
128

129
    // The sam self is derived from its key, not configured.
130
    if (const auto& self = network_settings().inbound.self)
9 ✔
131
        selfs.push_back(self);
2 ✔
132

133
    address message{};
9 ✔
134
    message.addresses.reserve(selfs.size());
9 ✔
135
    for (const auto& self: selfs)
23 ✔
136
    {
137
        auto item = self.to_address_item(time_now, services);
14 ✔
138
        if (network_settings().gossiped(item))
14 ✔
139
        {
140
            message.addresses.push_back(std::move(item));
141
        }
142
        else
143
        {
144
            LOGN("Self [" << self << "] network is not gossiped.");
9 ✔
145
        }
146
    }
147

148
    return message;
9 ✔
149
}
9 ✔
150

151
const registry::counters_t& protocol_peer::sent_by_message() const NOEXCEPT
4 ✔
152
{
153
    return channel_->sent_by_message();
4 ✔
154
}
155

156
const registry::counters_t& protocol_peer::received_by_message() const NOEXCEPT
4 ✔
157
{
158
    return channel_->received_by_message();
4 ✔
159
}
160

161
steady_clock::duration protocol_peer::ping_time() const NOEXCEPT
2 ✔
162
{
163
    return channel_->ping_time();
2 ✔
164
}
165

166
steady_clock::duration protocol_peer::minimum_ping_time() const NOEXCEPT
2 ✔
167
{
168
    return channel_->minimum_ping_time();
2 ✔
169
}
170

171
steady_clock::duration protocol_peer::pending_ping_time() const NOEXCEPT
2 ✔
172
{
173
    return channel_->pending_ping_time();
2 ✔
174
}
175

176
void protocol_peer::set_ping() NOEXCEPT
74 ✔
177
{
178
    channel_->set_ping();
74 ✔
179
}
74 ✔
180

181
void protocol_peer::set_pong() NOEXCEPT
4 ✔
182
{
183
    channel_->set_pong();
4 ✔
184
}
4 ✔
185

186
uint64_t protocol_peer::minimum_fee() const NOEXCEPT
2 ✔
187
{
188
    return channel_->minimum_fee();
2 ✔
189
}
190

191
void protocol_peer::set_minimum_fee(uint64_t value) NOEXCEPT
2 ✔
192
{
193
    channel_->set_minimum_fee(value);
2 ✔
194
}
2 ✔
195

196
// Addresses.
197
// ----------------------------------------------------------------------------
198
// Channel and network strands share same pool, and as long as a job is
199
// running in the pool, it will continue to accept work. Therefore handlers
200
// will not be orphaned during a stop as long as they remain in the pool.
201

202
size_t protocol_peer::address_count() const NOEXCEPT
24 ✔
203
{
204
    return session_->address_count();
24 ✔
205
}
206

207
void protocol_peer::fetch(address_handler&& handler) NOEXCEPT
8 ✔
208
{
209
    session_->fetch(
8 ✔
210
        BIND(handle_fetch, _1, _2, std::move(handler)));
8 ✔
211
}
8 ✔
212

213
void protocol_peer::handle_fetch(const code& ec, const address_cptr& message,
8 ✔
214
    const address_handler& handler) NOEXCEPT
215
{
216
    // Return to channel strand.
217
    boost::asio::post(channel_->strand(),
8 ✔
218
        std::bind(handler, ec, message));
8 ✔
219
}
8 ✔
220

221
void protocol_peer::save(const address_cptr& message,
24 ✔
222
    count_handler&& handler) NOEXCEPT
223
{
224
    session_->save(message,
24 ✔
225
        BIND(handle_save, _1, _2, std::move(handler)));
24 ✔
226
}
24 ✔
227

228
void protocol_peer::handle_save(const code& ec, size_t accepted,
24 ✔
229
    const count_handler& handler) NOEXCEPT
230
{
231
    // Return to channel strand.
232
    boost::asio::post(channel_->strand(),
24 ✔
233
        std::bind(handler, ec, accepted));
24 ✔
234
}
24 ✔
235

236
} // namespace network
237
} // 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