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

libbitcoin / libbitcoin-network / 45602458088

25 Nov 2026 11:12AM UTC coverage: 94.31% (+0.3%) from 94.028%
45602458088

push

github

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

Add the stratum v2 messages, registry and body.

1159 of 1175 new or added lines in 39 files covered. (98.64%)

3 existing lines in 2 files now uncovered.

17354 of 18401 relevant lines covered (94.31%)

2087.94 hits per line

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

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

21
#include <bitcoin/network/define.hpp>
22
#include <bitcoin/network/log/log.hpp>
23
#include <bitcoin/network/net/socket.hpp>
24

25
namespace libbitcoin {
26
namespace network {
27

28
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
29

30
// Factory for variable deadline timer pointer construction (or null).
31
inline deadline::ptr make_throttle(const logger& log, io::strand& strand,
327 ✔
32
    uint32_t rate_limit) NOEXCEPT
33
{
34
    return to_bool(rate_limit) ?
327 ✔
35
        system::emplace_shared<deadline>(log, strand) : nullptr;
327 ✔
36
}
37

38
// Created in started state and must be stopped, as subscribers assert if not
39
// stopped. Subscribers may hold protocols even if service not started.
40
proxy::proxy(const socket::ptr& socket, uint32_t rate_limit,
327 ✔
41
    size_t maximum_backlog) NOEXCEPT
327 ✔
42
  : reporter(socket->log),
327 ✔
43
    rate_limit_(rate_limit),
327 ✔
44
    maximum_backlog_(maximum_backlog),
327 ✔
45
    socket_(socket),
327 ✔
46
    throttle_(make_throttle(socket->log, socket->strand(), rate_limit))
654 ✔
47
{
48
}
327 ✔
49

50
proxy::~proxy() NOEXCEPT
327 ✔
51
{
52
    BC_ASSERT_MSG(stopped(), "proxy is not stopped");
327 ✔
53
    if (!stopped()) { LOGF("~proxy is not stopped."); }
327 ✔
54
}
327 ✔
55

56
// Stop (socket/proxy started upon create).
57
// ----------------------------------------------------------------------------
58
// Proxy does not (must not) stop itself.
59

60
void proxy::stop(const code& ec) NOEXCEPT
484 ✔
61
{
62
    if (stopped())
484 ✔
63
    {
64
        // Ends an outstanding lazy stop, otherwise nothing.
65
        socket_->stop();
157 ✔
66
        return;
157 ✔
67
    }
68

69
    // Open batch response closed (written) before socket stops. Batch state
70
    // protected by strand, so only stranded stop can close gracefully
71
    // (unstranded stop, e.g. shutdown, is immediate).
72
    if (stranded() && batched_ && parted_)
327 ✔
73
    {
74
        do_stop(ec);
3 ✔
75
        return;
3 ✔
76
    }
77

78
    finish_stop(ec);
324 ✔
79
}
80

81
// private
82
// Close open batch response, then stop (batch open rides on first part, so
83
// nothing to close unless parted).
84
void proxy::do_stop(const code& ec) NOEXCEPT
3 ✔
85
{
86
    BC_ASSERT(stranded());
3 ✔
87
    using namespace std::placeholders;
88

89
    // Socket not yet stopped, so deferred send still holds queue. Release it
90
    // here so close part is not delayed by throttle.
91
    if (throttle_) throttle_->stop();
3 ✔
92

93
    // This is the final message of the channel.
94
    queue_close(std::bind(&proxy::handle_stop_write,
3 ✔
95
        shared_from_this(), _1, _2, ec));
6 ✔
96
}
3 ✔
97

98
// private
99
void proxy::handle_stop_write(const code&, size_t,
3 ✔
100
    const code& reason) NOEXCEPT
101
{
102
    BC_ASSERT(stranded());
3 ✔
103
    finish_stop(reason);
3 ✔
104
}
3 ✔
105

106
// private
107
// Thread safe (called directly from stop or from the strand).
108
void proxy::finish_stop(const code& ec) NOEXCEPT
327 ✔
109
{
110
    // Stop read loop, stop accepting new work, cancel pending work.
111
    // Graceful close issues follow-on iocontext jobs, so it is not performed
112
    // within socket::stop(), and subsequent socket::stop() terminates it.
113
    if (lazy(ec))
327 ✔
114
        socket_->lazy_stop();
76 ✔
115
    else
116
        socket_->stop();
251 ✔
117

118
    io::post(strand(),
327 ✔
119
        std::bind(&proxy::stopping, shared_from_this(), ec));
654 ✔
120
}
327 ✔
121

122
// A stop for a failed or canceled transport has no peer to close with.
123
bool proxy::lazy(const code& ec) const NOEXCEPT
171 ✔
124
{
125
    return !(ec == error::service_stopped
171 ✔
126
        || ec == error::channel_stopped
95 ✔
127
        || ec == error::operation_canceled
84 ✔
128
        || ec == error::operation_failed
84 ✔
129
        || ec == error::operation_timeout
83 ✔
130
        || ec == error::peer_disconnect
83 ✔
131
        || ec == error::bad_stream
81 ✔
132
        || ec == error::end_of_stream
78 ✔
133
        || ec == error::partial_message
78 ✔
134
        || ec == error::short_read
78 ✔
135
        || ec == error::tls_stream_truncated);
78 ✔
136
}
137

138
// protected
139
// Should not be called internally (invoked by stop()).
140
void proxy::stopping(const code& ec) NOEXCEPT
327 ✔
141
{
142
    BC_ASSERT(stranded());
327 ✔
143

144
    // Release any deferred send (fires pending charge with canceled).
145
    if (throttle_) throttle_->stop();
327 ✔
146

147
    // Release any http message parse in progress.
148
    parser_.reset();
327 ✔
149

150
    // Release queued writes, handlers are not invoked after stop.
151
    queue_.clear();
327 ✔
152
    deferred_.clear();
327 ✔
153

154
    // Post stop handlers to strand and clear/stop accepting subscriptions.
155
    // Code provides information on reason channel stopped.
156
    stop_subscriber_.stop(ec);
327 ✔
157
}
327 ✔
158

159
proxy::object_key proxy::subscribe_stop(result_handler&& handler) NOEXCEPT
446 ✔
160
{
161
    BC_ASSERT(stranded());
446 ✔
162
    using namespace std::placeholders;
163

164
    const auto key = create_key();
446 ✔
165
    stop_subscriber_.subscribe(
446 ✔
166
        std::bind(&proxy::handle_stop, _1, std::move(handler)), key);
892 ✔
167

168
    return key;
446 ✔
169
}
170

171
// private
172
bool proxy::handle_stop(const code& ec,
446 ✔
173
    const result_handler& handler) NOEXCEPT
174
{
175
    handler(ec);
446 ✔
176
    return false;
446 ✔
177
}
178

179
bool proxy::unsubscribe_stop(object_key key) NOEXCEPT
2 ✔
180
{
181
    BC_ASSERT(stranded());
2 ✔
182
    return stop_subscriber_.notify_one(key, error::desubscribed);
2 ✔
183
}
184

185
void proxy::subscribe_stop(result_handler&& handler,
175 ✔
186
    result_handler&& complete) NOEXCEPT
187
{
188
    if (stopped())
175 ✔
189
    {
190
        complete(error::channel_stopped);
5 ✔
191
        handler(error::channel_stopped);
5 ✔
192
        return;
5 ✔
193
    }
194

195
    io::dispatch(strand(),
170 ✔
196
        std::bind(&proxy::do_subscribe_stop,
340 ✔
197
            shared_from_this(), std::move(handler), std::move(complete)));
340 ✔
198
}
199

200
// private
201
void proxy::do_subscribe_stop(const result_handler& handler,
170 ✔
202
    const result_handler& complete) NOEXCEPT
203
{
204
    BC_ASSERT(stranded());
170 ✔
205
    subscribe_stop(move_copy(handler));
170 ✔
206
    complete(error::success);
170 ✔
207
}
170 ✔
208

209
// private
210
proxy::object_key proxy::create_key() NOEXCEPT
446 ✔
211
{
212
    BC_ASSERT(stranded());
446 ✔
213

214
    if (is_zero(++keys_))
446 ✔
215
    {
NEW
216
        BC_ASSERT_MSG(false, "overflow");
×
217
        LOGF("Stop subscriber key overflow.");
218
    }
219

220
    return keys_;
446 ✔
221
}
222

223
// Signal activity.
224
// ----------------------------------------------------------------------------
225
// override reading() to update timers.
226

227
// protected
228
void proxy::reading() NOEXCEPT
54 ✔
229
{
230
}
54 ✔
231

232
// protected
233
void proxy::writing() NOEXCEPT
50 ✔
234
{
235
}
50 ✔
236

237
// private
238
void proxy::do_reading() NOEXCEPT
537 ✔
239
{
240
    io::dispatch(strand(),
537 ✔
241
        std::bind(&proxy::reading, shared_from_this()));
1,074 ✔
242
}
537 ✔
243

244
// Properties.
245
// ----------------------------------------------------------------------------
246

247
io::context& proxy::service() const NOEXCEPT
1 ✔
248
{
249
    return socket_->service();
1 ✔
250
}
251

252
io::strand& proxy::strand() NOEXCEPT
2,487 ✔
253
{
254
    return socket_->strand();
2,487 ✔
255
}
256

257
bool proxy::stranded() const NOEXCEPT
19,955 ✔
258
{
259
    return socket_->stranded();
19,955 ✔
260
}
261

262
bool proxy::stopped() const NOEXCEPT
7,336 ✔
263
{
264
    return socket_->stopped();
7,336 ✔
265
}
266

267
bool proxy::inbound() const NOEXCEPT
797 ✔
268
{
269
    return socket_->inbound();
797 ✔
270
}
271

272
bool proxy::secure() const NOEXCEPT
41 ✔
273
{
274
    return socket_->secure();
41 ✔
275
}
276

277
bool proxy::encrypted() const NOEXCEPT
106 ✔
278
{
279
    return socket_->encrypted();
106 ✔
280
}
281

282
bool proxy::websocket() const NOEXCEPT
251 ✔
283
{
284
    return socket_->websocket();
251 ✔
285
}
286

287
bool proxy::downgraded() const NOEXCEPT
48 ✔
288
{
289
    return socket_->downgraded();
48 ✔
290
}
291

292
bool proxy::detected() const NOEXCEPT
3 ✔
293
{
294
    return socket_->detected();
3 ✔
295
}
296

297
code proxy::accept_websocket(const http::request& request) NOEXCEPT
25 ✔
298
{
299
    return socket_->accept_websocket(request);
25 ✔
300
}
301

302
uint64_t proxy::received() const NOEXCEPT
85 ✔
303
{
304
    return received_.load();
85 ✔
305
}
306

307
uint64_t proxy::sent() const NOEXCEPT
85 ✔
308
{
309
    return sent_.load();
85 ✔
310
}
311

312
const config::address& proxy::address() const NOEXCEPT
123 ✔
313
{
314
    return socket_->address();
123 ✔
315
}
316

317
const config::endpoint& proxy::endpoint() const NOEXCEPT
318 ✔
318
{
319
    return socket_->endpoint();
318 ✔
320
}
321

322
const config::endpoint& proxy::binding() const NOEXCEPT
4 ✔
323
{
324
    return socket_->binding();
4 ✔
325
}
326

327
size_t proxy::slot() const NOEXCEPT
3 ✔
328
{
329
    return socket_->slot();
3 ✔
330
}
331

332
BC_POP_WARNING()
333

334
} // namespace network
335
} // 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