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

libbitcoin / libbitcoin-server / 44845974736

21 Nov 2026 12:45AM UTC coverage: 95.044% (-0.1%) from 95.15%
44845974736

push

github

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

btcd cleanup, return bad target on versioned native/admin fail to parse.

49 of 66 new or added lines in 8 files covered. (74.24%)

14 existing lines in 4 files now uncovered.

12829 of 13498 relevant lines covered (95.04%)

451.12 hits per line

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

93.75
/src/protocols/admin/protocol_admin.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/server/protocols/protocol_admin.hpp>
20

21
#include <atomic>
22
#include <chrono>
23
#include <bitcoin/server/define.hpp>
24
#include <bitcoin/server/parsers/parsers.hpp>
25
#include <bitcoin/server/serializers/serializers.hpp>
26
#include <bitcoin/server/utilities/utilities.hpp>
27

28
namespace libbitcoin {
29
namespace server {
30

31
using namespace system;
32
using namespace network;
33
using namespace network::http;
34
using namespace std::placeholders;
35
constexpr auto relaxed = std::memory_order_relaxed;
36

37
#define CLASS protocol_admin
38
#define SUBSCRIBE_ADMIN(method, ...) \
39
    subscribe<CLASS>(&CLASS::method, __VA_ARGS__)
40

41
// Limited by json number domain and sentinel bit.
42
static_assert(node::events::unknown < 53u);
43
static_assert(network::levels::verbose < 53u);
44

45
// Start.
46
// ----------------------------------------------------------------------------
47

48
void protocol_admin::start() NOEXCEPT
34 ✔
49
{
50
    BC_ASSERT(stranded());
34 ✔
51

52
    if (started())
34 ✔
53
        return;
54

55
    // Subscription methods.
56
    SUBSCRIBE_ADMIN(handle_get_log_subscribe, _1, _2, _3, _4);
34 ✔
57
    SUBSCRIBE_ADMIN(handle_get_event_subscribe, _1, _2, _3, _4);
34 ✔
58
    protocol_native::start();
34 ✔
59
}
60

61
void protocol_admin::stopping(const code& ec) NOEXCEPT
34 ✔
62
{
63
    BC_ASSERT(stranded());
34 ✔
64
    log_interest_->set(zero);
34 ✔
65
    admin_dispatcher_.stop(ec);
34 ✔
66
    protocol_native::stopping(ec);
34 ✔
67
}
34 ✔
68

69
// Dispatch.
70
// ----------------------------------------------------------------------------
71

72
bool protocol_admin::try_dispatch_object(const http::request& request) NOEXCEPT
22 ✔
73
{
74
    BC_ASSERT(stranded());
22 ✔
75

76
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
77
    const auto target = request.target();
22 ✔
78
    BC_POP_WARNING()
79

80
    rpc::request_t model{};
22 ✔
81
    if (const auto ec = admin_target(model, target))
22 ✔
82
    {
83
        // An unrecognized admin target may be a native interface request.
84
        if (ec == error::invalid_target)
6 ✔
85
            return protocol_native::try_dispatch_object(request);
6 ✔
86

87
        // A target without a version is retried as a page request.
88
        if (ec == error::missing_version || ec == error::empty_path)
1 ✔
NEW
89
            return false;
×
90

91
        send_bad_target(ec, *reset_request());
1 ✔
92
        return true;
1 ✔
93
    }
94

95
    // No media defaults injected for an http request.
96
    if (!admin_query(model, request))
16 ✔
97
    {
98
        send_bad_request();
4 ✔
99
        return true;
100
    }
101

102
    const auto media = strip_media(model);
12 ✔
103
    if (media == media_type::unknown)
12 ✔
104
    {
105
        send_not_acceptable();
1 ✔
106
        return true;
107
    }
108

109
    // Falls through to html page dispatch.
110
    if (media == media_type::text_html)
11 ✔
111
        return false;
112

113
    // The credential may be restricted to a subset of interface methods.
114
    if (!permitted(model.method))
10 ✔
115
    {
116
        send_forbidden();
1 ✔
117
        return true;
118
    }
119

120
    if (admin_dispatcher_.notify(model))
9 ✔
121
        send_bad_request();
×
122

123
    return true;
124
}
22 ✔
125

126
void protocol_admin::dispatch_websocket(const http::request& request) NOEXCEPT
20 ✔
127
{
128
    BC_ASSERT(stranded());
20 ✔
129

130
    // ws frames carry no headers, so ws authorization is enforced here, not
131
    // by the channel (established by basic auth on the upgrade request).
132
    if (!authorized())
20 ✔
133
    {
134
        send_unauthorized(request);
×
135
        return;
6 ✔
136
    }
137

138
    // Admin websocket interface supports only string requests.
139
    if (!request.body().contains<http::string_value>())
20 ✔
140
    {
141
        send_not_acceptable(request);
×
142
        return;
×
143
    }
144

145
    // Target with query string is passed via websocket string body.
146
    const auto target = request.body().get<http::string_value>();
20 ✔
147

148
    rpc::request_t model{};
20 ✔
149
    if (const auto ec = admin_target(model, target))
20 ✔
150
    {
151
        // An unrecognized admin target may be a native interface request.
152
        if (ec == error::invalid_target)
4 ✔
153
            protocol_native::dispatch_websocket(request);
3 ✔
154
        else
155
            send_bad_target(ec, request);
1 ✔
156

157
        return;
4 ✔
158
    }
159

160
    // Default to json by simulating a json accept header (format overrides).
161
    if (!admin_query(model, target, { media_type::application_json }))
16 ✔
162
    {
163
        send_bad_request(request);
1 ✔
164
        return;
165
    }
166

167
    if (strip_media(model) != media_type::application_json)
15 ✔
168
    {
169
        send_not_acceptable(request);
1 ✔
170
        return;
171
    }
172

173
    // The credential may be restricted to a subset of interface methods.
174
    if (!permitted(model.method))
14 ✔
175
    {
176
        send_forbidden(request);
×
177
        return;
178
    }
179

180
    if (admin_dispatcher_.notify(model))
14 ✔
181
        send_bad_request(request);
×
182
}
20 ✔
183

184
// Interface handlers.
185
// ----------------------------------------------------------------------------
186

187
bool protocol_admin::handle_get_log_subscribe(const code& ec,
49 ✔
188
    admin_interface::log_subscribe, uint8_t /*version*/,
189
    uint64_t filter) NOEXCEPT
190
{
191
    BC_ASSERT(stranded());
49 ✔
192

193
    if (stopped(ec))
49 ✔
194
        return false;
195

196
    // log outlives node, so cannot hold shared_ptr to the protocol.
197
    const auto previous = log_interest_->set(filter);
15 ✔
198
    if (!is_zero(filter) && !log_subscribed_)
15 ✔
199
    {
200
        auto handler = BIND_WEAK(handle_log, _1, _2, _3, _4);
10 ✔
201
        log.subscribe_messages(log_interest_, std::move(handler));
10 ✔
202
        log_subscribed_ = true;
10 ✔
203
    }
10 ✔
204

205
    send_json({ { "previous", previous } });
15 ✔
206
    return true;
15 ✔
207
}
208

209
bool protocol_admin::handle_get_event_subscribe(const code& ec,
42 ✔
210
    admin_interface::event_subscribe, uint8_t /*version*/,
211
    uint64_t filter) NOEXCEPT
212
{
213
    BC_ASSERT(stranded());
42 ✔
214

215
    if (stopped(ec))
42 ✔
216
        return false;
217

218
    // log outlives node, so cannot hold shared_ptr to the protocol.
219
    uint64_t previous{};
8 ✔
220
    if (update_filter(previous, filter, event_state_))
8 ✔
221
        log.subscribe_events(BIND_WEAK(handle_event, _1, _2, _3, _4));
6 ✔
222

223
    send_json({ { "previous", previous } });
8 ✔
224
    return true;
8 ✔
225
}
226

227
// Event handlers.
228
// ----------------------------------------------------------------------------
229

230
bool protocol_admin::handle_log(const code& ec, uint8_t level, time_t zulu,
5 ✔
231
    const std::string& message) NOEXCEPT
232
{
233
    if (stopped(ec))
5 ✔
234
        return false;
235

236
    if (websocket())
5 ✔
237
    {
238
        POST(do_log, level, zulu * 1'000_u64, message);
4 ✔
239
    }
240

241
    return true;
242
}
243

244
bool protocol_admin::handle_event(const code& ec, uint8_t event_,
4 ✔
245
    uint64_t value, const logger::time& point) NOEXCEPT
246
{
247
    bool filtered{};
4 ✔
248
    if (stopped(ec) || !get_filter(filtered, event_, event_state_))
4 ✔
249
        return false;
1 ✔
250

251
    if (!filtered || !websocket())
3 ✔
252
        return true;
1 ✔
253

254
    POST(do_event, event_, to_zulu(point), value);
2 ✔
255
    return true;
2 ✔
256
}
257

258
// Notification event handlers.
259
// ----------------------------------------------------------------------------
260
// Posted to the protocol strand.
261

262
void protocol_admin::do_log(uint8_t level, uint64_t zulu,
4 ✔
263
    const std::string& message) NOEXCEPT
264
{
265
    BC_ASSERT(stranded() && websocket());
4 ✔
266

267
    if (stopped())
4 ✔
268
        return;
269

270
    notify_json(
4 ✔
271
    {
272
        { "level", level },
273
        { "time", zulu },
274
        { "message", message }
275
    });
276
}
277

278
void protocol_admin::do_event(uint8_t event_, uint64_t zulu,
2 ✔
279
    uint64_t value_) NOEXCEPT
280
{
281
    BC_ASSERT(stranded() && websocket());
2 ✔
282

283
    if (stopped())
2 ✔
284
        return;
285

286
    notify_json(
2 ✔
287
    {
288
        { "event", event_ },
289
        { "value", value_ },
290
        { "time", zulu }
291
    });
292
}
293

294
// Utils.
295
// ----------------------------------------------------------------------------
296
// static/private
297

298
// zulu time in milliseconds since epoch (native js time format).
299
uint64_t protocol_admin::to_zulu(const logger::time& point) NOEXCEPT
2 ✔
300
{
301
    using namespace std::chrono;
302
    const auto now = logger::now();
2 ✔
303
    const auto delta = duration_cast<system_clock::duration>(now - point);
2 ✔
304
    const auto time = (system_clock::now() - delta).time_since_epoch();
2 ✔
305
    return duration_cast<milliseconds>(time).count();
2 ✔
306
}
307

308
// Unsubscribe if false.
309
bool protocol_admin::get_filter(bool& filtered, size_t bit,
4 ✔
310
    filter_t& filter) NOEXCEPT
311
{
312
    auto state = filter.load(relaxed);
313
    const auto next = set_left(state, zero, false);
4 ✔
314

315
    // Empty filter unregisters, failure implies a new filter arrived.
316
    if (is_zero(next) && filter.compare_exchange_strong(state, next, relaxed))
5 ✔
317
        return false;
318

319
    filtered = get_right(state, bit);
3 ✔
320
    return true;
3 ✔
321
}
322

323
// Subscribe if true.
324
bool protocol_admin::update_filter(uint64_t& prior, uint64_t value,
8 ✔
325
    filter_t& filter) NOEXCEPT
326
{
327
    const auto next = set_left(value, zero, true);
8 ✔
328
    const auto state = is_zero(value) ? filter.fetch_and(next, relaxed) :
8 ✔
329
        filter.exchange(next, relaxed);
330

331
    prior = set_left(state, zero, false);
8 ✔
332
    return !is_zero(value) && !get_left(state);
8 ✔
333
}
334

335
} // namespace server
336
} // 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