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

libbitcoin / libbitcoin-server / 44691724904

20 Nov 2026 06:59AM UTC coverage: 95.15% (+0.05%) from 95.102%
44691724904

push

github

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

Fix test, disable admin channel inactivity and expiration by default.

2 of 2 new or added lines in 1 file covered. (100.0%)

122 existing lines in 14 files now uncovered.

12792 of 13444 relevant lines covered (95.15%)

445.71 hits per line

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

96.62
/src/protocols/native/protocol_native.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_native.hpp>
20

21
#include <atomic>
22
#include <optional>
23
#include <bitcoin/server/define.hpp>
24
#include <bitcoin/server/parsers/parsers.hpp>
25

26
namespace libbitcoin {
27
namespace server {
28

29
using namespace system;
30
using namespace network;
31
using namespace std::placeholders;
32
constexpr auto relaxed = std::memory_order_relaxed;
33

34
#define CLASS protocol_native
35
#define SUBSCRIBE_NATIVE(method, ...) \
36
    subscribe<CLASS>(&CLASS::method, __VA_ARGS__)
37

38
// Start.
39
// ----------------------------------------------------------------------------
40

41
void protocol_native::start() NOEXCEPT
277 ✔
42
{
43
    BC_ASSERT(stranded());
277 ✔
44

45
    if (started())
277 ✔
46
        return;
47

48
    // Chaser subscription is asynchronous, events may be missed.
49
    subscribe_chase(BIND(handle_chase, _1, _2));
277 ✔
50

51
    // Configuration methods.
52
    SUBSCRIBE_NATIVE(handle_get_configuration, _1, _2, _3, _4);
277 ✔
53

54
    // Top methods.
55
    SUBSCRIBE_NATIVE(handle_get_top, _1, _2, _3, _4);
277 ✔
56
    SUBSCRIBE_NATIVE(handle_get_top_subscribe, _1, _2, _3, _4, _5);
277 ✔
57
    SUBSCRIBE_NATIVE(handle_get_top_chain, _1, _2, _3, _4, _5);
277 ✔
58
    SUBSCRIBE_NATIVE(handle_get_top_height, _1, _2, _3, _4, _5);
277 ✔
59

60
    // Block methods.
61
    SUBSCRIBE_NATIVE(handle_get_block, _1, _2, _3, _4, _5, _6, _7, _8);
277 ✔
62
    SUBSCRIBE_NATIVE(handle_get_block_header, _1, _2, _3, _4, _5, _6, _7);
277 ✔
63
    SUBSCRIBE_NATIVE(handle_get_block_header_context, _1, _2, _3, _4, _5, _6, _7);
277 ✔
64
    SUBSCRIBE_NATIVE(handle_get_block_details, _1, _2, _3, _4, _5, _6, _7);
277 ✔
65
    SUBSCRIBE_NATIVE(handle_get_block_txs, _1, _2, _3, _4, _5, _6, _7);
277 ✔
66
    SUBSCRIBE_NATIVE(handle_get_block_filter, _1, _2, _3, _4, _5, _6, _7, _8);
277 ✔
67
    SUBSCRIBE_NATIVE(handle_get_block_filter_hash, _1, _2, _3, _4, _5, _6, _7, _8);
277 ✔
68
    SUBSCRIBE_NATIVE(handle_get_block_filter_header, _1, _2, _3, _4, _5, _6, _7, _8);
277 ✔
69
    SUBSCRIBE_NATIVE(handle_get_block_tx, _1, _2, _3, _4, _5, _6, _7, _8, _9);
277 ✔
70
    SUBSCRIBE_NATIVE(handle_get_block_subscribe, _1, _2, _3, _4, _5);
277 ✔
71

72
    // Transaction methods.
73
    SUBSCRIBE_NATIVE(handle_get_tx, _1, _2, _3, _4, _5, _6);
277 ✔
74
    SUBSCRIBE_NATIVE(handle_get_tx_header, _1, _2, _3, _4, _5);
277 ✔
75
    SUBSCRIBE_NATIVE(handle_get_tx_details, _1, _2, _3, _4, _5);
277 ✔
76
    SUBSCRIBE_NATIVE(handle_get_tx_subscribe, _1, _2, _3, _4, _5);
277 ✔
77

78
    // Input methods.
79
    SUBSCRIBE_NATIVE(handle_get_inputs, _1, _2, _3, _4, _5, _6);
277 ✔
80
    SUBSCRIBE_NATIVE(handle_get_input, _1, _2, _3, _4, _5, _6, _7);
277 ✔
81
    SUBSCRIBE_NATIVE(handle_get_input_script, _1, _2, _3, _4, _5, _6);
277 ✔
82
    SUBSCRIBE_NATIVE(handle_get_input_witness, _1, _2, _3, _4, _5, _6);
277 ✔
83

84
    // Output methods.
85
    SUBSCRIBE_NATIVE(handle_get_outputs, _1, _2, _3, _4, _5);
277 ✔
86
    SUBSCRIBE_NATIVE(handle_get_output, _1, _2, _3, _4, _5, _6);
277 ✔
87
    SUBSCRIBE_NATIVE(handle_get_output_script, _1, _2, _3, _4, _5, _6);
277 ✔
88
    SUBSCRIBE_NATIVE(handle_get_output_spender, _1, _2, _3, _4, _5, _6);
277 ✔
89
    SUBSCRIBE_NATIVE(handle_get_output_spenders, _1, _2, _3, _4, _5, _6);
277 ✔
90
    SUBSCRIBE_NATIVE(handle_get_output_subscribe, _1, _2, _3, _4, _5, _6, _7);
277 ✔
91

92
    // Address methods.
93
    SUBSCRIBE_NATIVE(handle_get_address, _1, _2, _3, _4, _5, _6);
277 ✔
94
    SUBSCRIBE_NATIVE(handle_get_address_confirmed, _1, _2, _3, _4, _5, _6);
277 ✔
95
    SUBSCRIBE_NATIVE(handle_get_address_unconfirmed, _1, _2, _3, _4, _5, _6);
277 ✔
96
    SUBSCRIBE_NATIVE(handle_get_address_balance, _1, _2, _3, _4, _5, _6);
277 ✔
97
    SUBSCRIBE_NATIVE(handle_get_address_subscribe, _1, _2, _3, _4, _5, _6, _7);
277 ✔
98

99
    // Fee methods.
100
    SUBSCRIBE_NATIVE(handle_get_fees, _1, _2, _3, _4);
277 ✔
101
    protocol_html::start();
277 ✔
102
}
103

104
void protocol_native::stopping(const code& ec) NOEXCEPT
277 ✔
105
{
106
    BC_ASSERT(stranded());
277 ✔
107
    stopping_.store(true);
277 ✔
108
    dispatcher_.stop(ec);
277 ✔
109
    unsubscribe_chase();
277 ✔
110
    protocol_html::stopping(ec);
277 ✔
111
}
277 ✔
112

113
// Dispatch.
114
// ----------------------------------------------------------------------------
115

116
bool protocol_native::try_dispatch_object(const http::request& request) NOEXCEPT
217 ✔
117
{
118
    BC_ASSERT(stranded());
217 ✔
119

120
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
121
    const auto target = request.target();
217 ✔
122
    BC_POP_WARNING()
123

124
    rpc::request_t model{};
217 ✔
125
    if (const auto ec = native_target(model, target))
217 ✔
126
    {
127
        // Allow invalid interface target to be retried as a page request.
128
        return !ec;
22 ✔
129
    }
130

131
    // No media defaults injected for an http request.
132
    if (!native_query(model, request))
195 ✔
133
    {
134
        send_bad_request();
4 ✔
135
        return true;
136
    }
137

138
    const auto media = get_media(model);
191 ✔
139
    if (media == media_type::unknown)
191 ✔
140
    {
141
        send_not_acceptable();
2 ✔
142
        return true;
143
    }
144

145
    // Falls through to html page dispatch.
146
    if (media == media_type::text_html)
189 ✔
147
        return false;
148

149
    // The credential may be restricted to a subset of interface methods.
150
    if (!permitted(model.method))
188 ✔
151
    {
152
        send_forbidden();
2 ✔
153
        return true;
154
    }
155

156
    if (dispatcher_.notify(model))
186 ✔
157
        send_bad_request();
1 ✔
158

159
    return true;
160
}
217 ✔
161

162
void protocol_native::dispatch_websocket(const http::request& request) NOEXCEPT
36 ✔
163
{
164
    BC_ASSERT(stranded());
36 ✔
165

166
    // ws frames carry no headers, so ws authorization is enforced here, not
167
    // by the channel (established by basic auth on the upgrade request).
168
    if (!authorized())
36 ✔
169
    {
UNCOV
170
        send_unauthorized(request);
×
171
        return;
4 ✔
172
    }
173

174
    // Native websocket interface supports only string requests.
175
    if (!request.body().contains<http::string_value>())
36 ✔
176
    {
UNCOV
177
        send_not_acceptable(request);
×
UNCOV
178
        return;
×
179
    }
180

181
    // Target with query string is passed via websocket string body.
182
    const auto target = request.body().get<http::string_value>();
36 ✔
183

184
    rpc::request_t model{};
36 ✔
185
    if (const auto ec = native_target(model, target))
36 ✔
186
    {
187
        send_bad_target(ec, request);
2 ✔
188
        return;
2 ✔
189
    }
190

191
    // Default to json by simulating a json accept header (format overrides).
192
    if (!native_query(model, target, { media_type::application_json }))
34 ✔
193
    {
194
        send_bad_request(request);
1 ✔
195
        return;
196
    }
197

198
    if (get_media(model) == media_type::text_html)
33 ✔
199
    {
200
        send_not_acceptable(request);
1 ✔
201
        return;
202
    }
203

204
    // The credential may be restricted to a subset of interface methods.
205
    if (!permitted(model.method))
32 ✔
206
    {
UNCOV
207
        send_forbidden(request);
×
208
        return;
209
    }
210

211
    if (dispatcher_.notify(model))
32 ✔
212
        send_bad_request(request);
1 ✔
213
}
36 ✔
214

215
// Senders.
216
// ----------------------------------------------------------------------------
217

218
void protocol_native::send_not_found(const http::request& request) NOEXCEPT
40 ✔
219
{
220
    BC_ASSERT(stranded());
40 ✔
221

222
    if (websocket())
40 ✔
223
    {
224
        send_empty();
2 ✔
225
        return;
2 ✔
226
    }
227

228
    protocol_html::send_not_found(request);
38 ✔
229
}
230

231
// Event handlers.
232
// ----------------------------------------------------------------------------
233

234
bool protocol_native::handle_chase(const code&,
289 ✔
235
    node::event_value value) NOEXCEPT
236
{
237
    // Do not pass ec to stopped as it is not a call status.
238
    if (stopped())
289 ✔
239
        return false;
240

241
    if (!websocket())
59 ✔
242
        return true;
243

244
    switch (node::to_chase(value))
17 ✔
245
    {
246
        case node::chase::block:
5 ✔
247
        {
248
            const auto block = node::to_payload<node::chase::block>(value);
5 ✔
249

250
            auto media = top_subscribe_.load(relaxed);
5 ✔
251
            if (media != media_type::unknown)
5 ✔
252
            {
253
                POST(do_top, media);
2 ✔
254
            }
255

256
            // No block emission for a fork point (start/reorg).
257
            media = block_subscribe_.load(relaxed);
5 ✔
258
            if (media != media_type::unknown)
5 ✔
259
            {
260
                POST(do_block, block.link, media);
3 ✔
261
            }
262

263
            break;
264
        }
265
        case node::chase::reorganized:
4 ✔
266
        case node::chase::headers:
267
        case node::chase::regressed:
268
        case node::chase::disorganized:
269
        {
270
            const auto media = top_subscribe_.load(relaxed);
4 ✔
271
            if (media != media_type::unknown)
4 ✔
272
            {
273
                POST(do_top, media);
4 ✔
274
            }
275

276
            break;
277
        }
278
        case node::chase::transaction:
3 ✔
279
        {
280
            const auto media = tx_subscribe_.load(relaxed);
3 ✔
281
            if (media != media_type::unknown)
3 ✔
282
            {
283
                POST(do_transaction,
3 ✔
284
                    node::to_payload<node::chase::transaction>(value).link,
285
                    media);
286
            }
287

288
            break;
289
        }
290
        default:
291
        {
292
            break;
293
        }
294
    }
295

296
    return true;
297
}
298

299
// Utilities.
300
// ----------------------------------------------------------------------------
301
// private
302

303
using namespace system::json;
304

305
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
306

307
void protocol_native::inject(value& out, std::optional<uint32_t> height,
7 ✔
308
    const database::header_link& link) const NOEXCEPT
309
{
310
    out.as_object().emplace("height", height.has_value() ? height.value() :
9 ✔
311
        archive().get_height(link).value);
9 ✔
312
}
7 ✔
313

314
chain::header::cptr protocol_native::get_top(bool candidate,
79 ✔
315
    size_t& height) const NOEXCEPT
316
{
317
    const auto& query = archive();
79 ✔
318
    height = candidate ? query.get_top_candidate() : query.get_top_confirmed();
79 ✔
319
    return query.get_header(candidate ? query.to_candidate(height) :
158 ✔
320
        query.to_confirmed(height));
158 ✔
321
}
322

323
// A top is its checkpoint followed by its header.
324
data_chunk protocol_native::to_top(bool candidate) const NOEXCEPT
36 ✔
325
{
326
    size_t height{};
36 ✔
327
    const auto header = get_top(candidate, height);
36 ✔
328
    return splice(chain::checkpoint{ header->hash(), height }.to_data(),
72 ✔
329
        header->to_data());
144 ✔
330
}
36 ✔
331

332
value protocol_native::to_top_json(bool candidate) const NOEXCEPT
43 ✔
333
{
334
    size_t height{};
43 ✔
335
    const auto header = get_top(candidate, height);
43 ✔
336
    auto model = value_from(chain::checkpoint{ header->hash(), height });
43 ✔
337
    model.as_object().emplace("header", value_from(header));
43 ✔
338
    return model;
43 ✔
339
}
43 ✔
340

341
data_chunk protocol_native::to_tops() const NOEXCEPT
17 ✔
342
{
343
    return splice(to_top(false), to_top(true));
17 ✔
344
}
345

346
value protocol_native::to_tops_json() const NOEXCEPT
20 ✔
347
{
348
    return
349
    {
350
        { "confirmed", to_top_json(false) },
20 ✔
351
        { "candidate", to_top_json(true) }
40 ✔
352
    };
80 ✔
353
}
354

355
BC_POP_WARNING()
356

357
database::header_link protocol_native::to_header(
67 ✔
358
    const std::optional<uint32_t>& height,
359
    const std::optional<hash_cptr>& hash, bool candidate) NOEXCEPT
360
{
361
    const auto& query = archive();
67 ✔
362

363
    if (hash.has_value())
67 ✔
364
        return query.to_header(*(hash.value()));
6 ✔
365

366
    if (!height.has_value())
61 ✔
UNCOV
367
        return {};
×
368

369
    return candidate ? query.to_candidate(height.value()) :
61 ✔
370
        query.to_confirmed(height.value());
61 ✔
371
}
372

373
// Use if deserialization is required.
374
#if defined(UNDEFINED)
375
using inpoints = database::inpoints;
376
using outpoints = database::outpoints;
377

378
DECLARE_JSON_TAG_INVOKE(inpoints);
379
DECLARE_JSON_TAG_INVOKE(outpoints);
380

381
DEFINE_JSON_TO_TAG(inpoints)
382
{
383
    inpoints out{};
384
    for (const auto& point: value.as_array())
385
        out.insert(value_to<inpoint>(point));
386

387
    return out;
388
}
389

390
DEFINE_JSON_TO_TAG(outpoints)
391
{
392
    outpoints out{};
393
    for (const auto& point: value.as_array())
394
        out.insert(value_to<outpoint>(point));
395

396
    return out;
397
}
398
#endif
399

400
} // namespace server
401
} // 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