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

libbitcoin / libbitcoin-server / 45308589906

24 Nov 2026 01:55AM UTC coverage: 93.872% (+2.7%) from 91.147%
45308589906

push

github

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

Stratum_v1, native block viewer, pruning fixes to native embed.

1246 of 1333 new or added lines in 43 files covered. (93.47%)

1 existing line in 1 file now uncovered.

14216 of 15144 relevant lines covered (93.87%)

454.75 hits per line

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

97.06
/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 <numeric>
23
#include <optional>
24
#include <unordered_map>
25
#include <bitcoin/server/define.hpp>
26
#include <bitcoin/server/parsers/parsers.hpp>
27

28
namespace libbitcoin {
29
namespace server {
30

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

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

40
// Start.
41
// ----------------------------------------------------------------------------
42

43
void protocol_native::start() NOEXCEPT
348 ✔
44
{
45
    BC_ASSERT(stranded());
348 ✔
46

47
    if (started())
348 ✔
48
        return;
49

50
    // Chaser subscription is asynchronous, events may be missed.
51
    subscribe_chase(BIND(handle_chase, _1, _2));
348 ✔
52

53
    // Configuration methods.
54
    SUBSCRIBE_NATIVE(handle_get_configuration, _1, _2, _3, _4);
348 ✔
55

56
    // Top methods.
57
    SUBSCRIBE_NATIVE(handle_get_top, _1, _2, _3, _4);
348 ✔
58
    SUBSCRIBE_NATIVE(handle_get_top_subscribe, _1, _2, _3, _4, _5);
348 ✔
59
    SUBSCRIBE_NATIVE(handle_get_top_chain, _1, _2, _3, _4, _5);
348 ✔
60
    SUBSCRIBE_NATIVE(handle_get_top_height, _1, _2, _3, _4, _5);
348 ✔
61

62
    // Block methods.
63
    SUBSCRIBE_NATIVE(handle_get_block, _1, _2, _3, _4, _5, _6, _7, _8);
348 ✔
64
    SUBSCRIBE_NATIVE(handle_get_block_header, _1, _2, _3, _4, _5, _6, _7);
348 ✔
65
    SUBSCRIBE_NATIVE(handle_get_block_header_context, _1, _2, _3, _4, _5, _6, _7);
348 ✔
66
    SUBSCRIBE_NATIVE(handle_get_block_details, _1, _2, _3, _4, _5, _6, _7);
348 ✔
67
    SUBSCRIBE_NATIVE(handle_get_block_txs, _1, _2, _3, _4, _5, _6, _7);
348 ✔
68
    SUBSCRIBE_NATIVE(handle_get_block_txs_details, _1, _2, _3, _4, _5, _6, _7);
348 ✔
69
    SUBSCRIBE_NATIVE(handle_get_block_filter, _1, _2, _3, _4, _5, _6, _7, _8);
348 ✔
70
    SUBSCRIBE_NATIVE(handle_get_block_filter_hash, _1, _2, _3, _4, _5, _6, _7, _8);
348 ✔
71
    SUBSCRIBE_NATIVE(handle_get_block_filter_header, _1, _2, _3, _4, _5, _6, _7, _8);
348 ✔
72
    SUBSCRIBE_NATIVE(handle_get_block_tx, _1, _2, _3, _4, _5, _6, _7, _8, _9);
348 ✔
73
    SUBSCRIBE_NATIVE(handle_get_block_tx_input_script, _1, _2, _3, _4, _5, _6, _7, _8, _9);
348 ✔
74
    SUBSCRIBE_NATIVE(handle_get_block_tx_output_script, _1, _2, _3, _4, _5, _6, _7, _8, _9);
348 ✔
75
    SUBSCRIBE_NATIVE(handle_get_block_subscribe, _1, _2, _3, _4, _5);
348 ✔
76

77
    // Transaction methods.
78
    SUBSCRIBE_NATIVE(handle_get_tx, _1, _2, _3, _4, _5, _6);
348 ✔
79
    SUBSCRIBE_NATIVE(handle_get_tx_header, _1, _2, _3, _4, _5);
348 ✔
80
    SUBSCRIBE_NATIVE(handle_get_tx_details, _1, _2, _3, _4, _5);
348 ✔
81
    SUBSCRIBE_NATIVE(handle_get_tx_subscribe, _1, _2, _3, _4, _5);
348 ✔
82

83
    // Input methods.
84
    SUBSCRIBE_NATIVE(handle_get_inputs, _1, _2, _3, _4, _5, _6);
348 ✔
85
    SUBSCRIBE_NATIVE(handle_get_input, _1, _2, _3, _4, _5, _6, _7);
348 ✔
86
    SUBSCRIBE_NATIVE(handle_get_input_script, _1, _2, _3, _4, _5, _6);
348 ✔
87
    SUBSCRIBE_NATIVE(handle_get_input_witness, _1, _2, _3, _4, _5, _6);
348 ✔
88

89
    // Output methods.
90
    SUBSCRIBE_NATIVE(handle_get_outputs, _1, _2, _3, _4, _5);
348 ✔
91
    SUBSCRIBE_NATIVE(handle_get_output, _1, _2, _3, _4, _5, _6);
348 ✔
92
    SUBSCRIBE_NATIVE(handle_get_output_script, _1, _2, _3, _4, _5, _6);
348 ✔
93
    SUBSCRIBE_NATIVE(handle_get_output_spender, _1, _2, _3, _4, _5, _6);
348 ✔
94
    SUBSCRIBE_NATIVE(handle_get_output_spenders, _1, _2, _3, _4, _5, _6);
348 ✔
95
    SUBSCRIBE_NATIVE(handle_get_output_subscribe, _1, _2, _3, _4, _5, _6, _7);
348 ✔
96

97
    // Address methods.
98
    SUBSCRIBE_NATIVE(handle_get_address, _1, _2, _3, _4, _5, _6);
348 ✔
99
    SUBSCRIBE_NATIVE(handle_get_address_confirmed, _1, _2, _3, _4, _5, _6);
348 ✔
100
    SUBSCRIBE_NATIVE(handle_get_address_unconfirmed, _1, _2, _3, _4, _5, _6);
348 ✔
101
    SUBSCRIBE_NATIVE(handle_get_address_balance, _1, _2, _3, _4, _5, _6);
348 ✔
102
    SUBSCRIBE_NATIVE(handle_get_address_subscribe, _1, _2, _3, _4, _5, _6, _7);
348 ✔
103

104
    // Fee methods.
105
    SUBSCRIBE_NATIVE(handle_get_template, _1, _2, _3, _4, _5);
348 ✔
106
    SUBSCRIBE_NATIVE(handle_get_template_txs, _1, _2, _3, _4, _5);
348 ✔
107
    SUBSCRIBE_NATIVE(handle_get_template_txs_details, _1, _2, _3, _4, _5);
348 ✔
108
    SUBSCRIBE_NATIVE(handle_get_fees, _1, _2, _3, _4);
348 ✔
109
    protocol_html::start();
348 ✔
110
}
111

112
void protocol_native::stopping(const code& ec) NOEXCEPT
348 ✔
113
{
114
    BC_ASSERT(stranded());
348 ✔
115
    stopping_.store(true);
348 ✔
116
    dispatcher_.stop(ec);
348 ✔
117
    unsubscribe_chase();
348 ✔
118
    protocol_html::stopping(ec);
348 ✔
119
}
348 ✔
120

121
// Dispatch.
122
// ----------------------------------------------------------------------------
123

124
bool protocol_native::try_dispatch_object(const http::request& request) NOEXCEPT
288 ✔
125
{
126
    BC_ASSERT(stranded());
288 ✔
127

128
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
129
    const auto target = request.target();
288 ✔
130
    BC_POP_WARNING()
131

132
    rpc::request_t model{};
288 ✔
133
    if (const auto ec = native_target(model, target))
288 ✔
134
    {
135
        // A target without a version or requesting html is a page request.
136
        if (ec == error::missing_version || ec == error::empty_path ||
41 ✔
137
            is_page_request(request))
9 ✔
138
            return false;
32 ✔
139

140
        send_bad_target(ec, *reset_request());
6 ✔
141
        return true;
6 ✔
142
    }
143

144
    // No media defaults injected for an http request.
145
    if (!native_query(model, request))
256 ✔
146
    {
147
        send_bad_request();
4 ✔
148
        return true;
149
    }
150

151
    const auto media = get_media(model);
252 ✔
152
    if (media == media_type::unknown)
252 ✔
153
    {
154
        send_not_acceptable();
2 ✔
155
        return true;
156
    }
157

158
    // Falls through to html page dispatch.
159
    if (media == media_type::text_html)
250 ✔
160
        return false;
161

162
    // The credential may be restricted to a subset of interface methods.
163
    if (!permitted(model.method))
248 ✔
164
    {
165
        send_forbidden();
2 ✔
166
        return true;
167
    }
168

169
    if (dispatcher_.notify(model))
246 ✔
170
        send_bad_request();
1 ✔
171

172
    return true;
173
}
288 ✔
174

175
bool protocol_native::is_page_request(
10 ✔
176
    const http::request& request) const NOEXCEPT
177
{
178
    rpc::request_t model{ .params = rpc::object_t{} };
10 ✔
179
    return native_query(model, request) &&
20 ✔
180
        get_media(model) == media_type::text_html;
20 ✔
181
}
10 ✔
182

183
void protocol_native::dispatch_websocket(const http::request& request) NOEXCEPT
36 ✔
184
{
185
    BC_ASSERT(stranded());
36 ✔
186

187
    // ws frames carry no headers, so ws authorization is enforced here, not by
188
    // channel (established by basic auth on upgrade request).
189
    if (!authorized())
36 ✔
190
    {
191
        send_unauthorized(request);
×
192
        return;
4 ✔
193
    }
194

195
    // Native websocket interface supports only string requests.
196
    if (!request.body().contains<http::string_value>())
36 ✔
197
    {
198
        send_not_acceptable(request);
×
199
        return;
×
200
    }
201

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

205
    rpc::request_t model{};
36 ✔
206
    if (const auto ec = native_target(model, target))
36 ✔
207
    {
208
        send_bad_target(ec, request);
2 ✔
209
        return;
2 ✔
210
    }
211

212
    // Default to json by simulating a json accept header (format overrides).
213
    if (!native_query(model, target, { media_type::application_json }))
34 ✔
214
    {
215
        send_bad_request(request);
1 ✔
216
        return;
217
    }
218

219
    if (get_media(model) == media_type::text_html)
33 ✔
220
    {
221
        send_not_acceptable(request);
1 ✔
222
        return;
223
    }
224

225
    // The credential may be restricted to a subset of interface methods.
226
    if (!permitted(model.method))
32 ✔
227
    {
228
        send_forbidden(request);
×
229
        return;
230
    }
231

232
    if (dispatcher_.notify(model))
32 ✔
233
        send_bad_request(request);
1 ✔
234
}
36 ✔
235

236
// Senders.
237
// ----------------------------------------------------------------------------
238

239
void protocol_native::send_not_found(const http::request& request) NOEXCEPT
63 ✔
240
{
241
    BC_ASSERT(stranded());
63 ✔
242

243
    if (websocket())
63 ✔
244
    {
245
        send_empty();
2 ✔
246
        return;
2 ✔
247
    }
248

249
    protocol_html::send_not_found(request);
61 ✔
250
}
251

252
// Event handlers.
253
// ----------------------------------------------------------------------------
254

255
bool protocol_native::handle_chase(const code&,
378 ✔
256
    node::event_value value) NOEXCEPT
257
{
258
    // Do not pass ec to stopped as it is not a call status.
259
    if (stopped())
378 ✔
260
        return false;
261

262
    if (!websocket())
162 ✔
263
        return true;
264

265
    switch (node::to_chase(value))
16 ✔
266
    {
267
        case node::chase::block:
5 ✔
268
        {
269
            const auto block = node::to_payload<node::chase::block>(value);
5 ✔
270

271
            auto media = top_subscribe_.load(relaxed);
5 ✔
272
            if (media != media_type::unknown)
5 ✔
273
            {
274
                POST(do_top, media);
2 ✔
275
            }
276

277
            // No block emission for a fork point (start/reorg).
278
            media = block_subscribe_.load(relaxed);
5 ✔
279
            if (media != media_type::unknown)
5 ✔
280
            {
281
                POST(do_block, block.link, media);
3 ✔
282
            }
283

284
            break;
285
        }
286
        case node::chase::reorganized:
4 ✔
287
        case node::chase::headers:
288
        case node::chase::regressed:
289
        case node::chase::disorganized:
290
        {
291
            const auto media = top_subscribe_.load(relaxed);
4 ✔
292
            if (media != media_type::unknown)
4 ✔
293
            {
294
                POST(do_top, media);
4 ✔
295
            }
296

297
            break;
298
        }
299
        case node::chase::transaction:
3 ✔
300
        {
301
            const auto media = tx_subscribe_.load(relaxed);
3 ✔
302
            if (media != media_type::unknown)
3 ✔
303
            {
304
                POST(do_transaction,
3 ✔
305
                    node::to_payload<node::chase::transaction>(value).link,
306
                    media);
307
            }
308

309
            break;
310
        }
311
        default:
312
        {
313
            break;
314
        }
315
    }
316

317
    return true;
318
}
319

320
// Utilities.
321
// ----------------------------------------------------------------------------
322
// private
323

324
using namespace system::json;
325

326
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
327

328
void protocol_native::inject(value& out, std::optional<uint32_t> height,
7 ✔
329
    const database::header_link& link) const NOEXCEPT
330
{
331
    out.as_object().emplace("height", height.has_value() ? height.value() :
9 ✔
332
        archive().get_height(link).value);
9 ✔
333
}
7 ✔
334

335
size_t protocol_native::to_root(std::vector<size_t>& roots,
19 ✔
336
    size_t index) NOEXCEPT
337
{
338
    while (roots.at(index) != index)
22 ✔
339
        index = roots.at(index) = roots.at(roots.at(index));
3 ✔
340

341
    return index;
19 ✔
342
}
343

344
std::vector<size_t> protocol_native::to_roots(
5 ✔
345
    const database::template_txs& txs) NOEXCEPT
346
{
347
    std::vector<size_t> roots(txs.size());
5 ✔
348
    std::iota(roots.begin(), roots.end(), zero);
5 ✔
349
    for (size_t index{}; index < txs.size(); ++index)
10 ✔
350
    {
351
        for (const auto parent: txs.at(index).parents)
7 ✔
352
        {
353
            const auto child = to_root(roots, index);
2 ✔
354
            roots.at(child) = to_root(roots, parent);
2 ✔
355
        }
356
    }
357

358
    return roots;
5 ✔
NEW
359
}
×
360

361
std::vector<size_t> protocol_native::to_roots(
6 ✔
362
    const database::tx_links& txs) const NOEXCEPT
363
{
364
    const auto& query = archive();
6 ✔
365
    const auto count = txs.size();
6 ✔
366
    std::unordered_map<hash_digest, size_t> positions{};
6 ✔
367
    positions.reserve(count);
6 ✔
368
    for (size_t index{}; index < count; ++index)
16 ✔
369
        positions.emplace(query.get_tx_key(txs.at(index)), index);
10 ✔
370

371
    std::vector<size_t> roots(count);
6 ✔
372
    std::iota(roots.begin(), roots.end(), zero);
6 ✔
373
    for (size_t index{}; index < count; ++index)
16 ✔
374
    {
375
        for (const auto& point: query.to_points(txs.at(index)))
22 ✔
376
        {
377
            const auto parent = positions.find(query.get_point_hash(point));
12 ✔
378
            if (parent == positions.end())
12 ✔
379
                continue;
11 ✔
380

381
            const auto child = to_root(roots, index);
1 ✔
382
            roots.at(child) = to_root(roots, parent->second);
1 ✔
383
        }
10 ✔
384
    }
385

386
    return roots;
6 ✔
387
}
6 ✔
388

389
// The details of each tx, with its cluster: the label, in order of first
390
// appearance, of its component under spends of other txs of the set.
391
bool protocol_native::to_tx_details(system::json::array& out,
9 ✔
392
    const database::tx_links& txs, std::vector<size_t>&& roots) const NOEXCEPT
393
{
394
    const auto& query = archive();
9 ✔
395
    const auto count = txs.size();
9 ✔
396
    out.clear();
9 ✔
397
    out.reserve(count);
9 ✔
398
    size_t clusters{};
399
    std::vector<size_t> labels(count, max_size_t);
9 ✔
400
    for (size_t index{}; index < count; ++index)
20 ✔
401
    {
402
        uint64_t value{}, spend{};
12 ✔
403
        size_t nominal{}, maximal{};
12 ✔
404
        const auto& link = txs.at(index);
12 ✔
405
        const auto coinbase = query.is_coinbase(link);
12 ✔
406
        if (!query.get_tx_sizes(nominal, maximal, link) ||
36 ✔
407
            !query.get_tx_value(value, link) ||
36 ✔
408
            !query.get_tx_spend(spend, link) ||
43 ✔
409
            (!coinbase && is_subtract_overflow(value, spend)))
7 ✔
410
            return false;
1 ✔
411

412
        auto& label = labels.at(to_root(roots, index));
11 ✔
413
        if (label == max_size_t)
11 ✔
414
            label = clusters++;
9 ✔
415

416
        out.emplace_back(system::json::object
33 ✔
417
        {
418
            { "segregated", maximal != nominal },
11 ✔
419
            { "nominal", nominal },
420
            { "maximal", maximal },
421
            { "weight", chain::weighted_size(nominal, maximal) },
11 ✔
422
            { "virtual", chain::virtual_size(nominal, maximal) },
11 ✔
423
            { "value", value },
424
            { "spend", spend },
425
            { "fee", floored_subtract(value, spend) },
22 ✔
426
            { "cluster", label }
427
        });
428
    }
429

430
    return true;
431
}
9 ✔
432

433
bool protocol_native::to_pruned_input(system::json::value& out,
2 ✔
434
    const database::ins_link& link) const NOEXCEPT
435
{
436
    const auto& query = archive();
2 ✔
437
    uint32_t sequence{};
2 ✔
438
    if (!query.get_input_sequence(sequence, link))
2 ✔
439
        return false;
440

441
    out = system::json::object
4 ✔
442
    {
443
        { "point", value_from(query.get_point(link)) },
4 ✔
444
        { "sequence", sequence }
445
    };
2 ✔
446

447
    return true;
2 ✔
448
}
449

450
bool protocol_native::to_pruned_inputs(system::json::array& out,
1 ✔
451
    const database::tx_link& link) const NOEXCEPT
452
{
453
    const auto points = archive().to_points(link);
1 ✔
454
    if (points.empty())
1 ✔
455
        return false;
456

457
    out.clear();
1 ✔
458
    out.reserve(points.size());
1 ✔
459
    for (const auto& point: points)
2 ✔
460
        if (!to_pruned_input(out.emplace_back(), point))
1 ✔
NEW
461
            return false;
×
462

463
    return true;
1 ✔
464
}
1 ✔
465

466
chain::header::cptr protocol_native::get_top(bool candidate,
79 ✔
467
    size_t& height) const NOEXCEPT
468
{
469
    const auto& query = archive();
79 ✔
470
    height = candidate ? query.get_top_candidate() : query.get_top_confirmed();
79 ✔
471
    return query.get_header(candidate ? query.to_candidate(height) :
158 ✔
472
        query.to_confirmed(height));
158 ✔
473
}
474

475
// A top is its checkpoint followed by its header.
476
data_chunk protocol_native::to_top(bool candidate) const NOEXCEPT
36 ✔
477
{
478
    size_t height{};
36 ✔
479
    const auto header = get_top(candidate, height);
36 ✔
480
    return splice(chain::checkpoint{ header->hash(), height }.to_data(),
72 ✔
481
        header->to_data());
144 ✔
482
}
36 ✔
483

484
value protocol_native::to_top_json(bool candidate) const NOEXCEPT
43 ✔
485
{
486
    size_t height{};
43 ✔
487
    const auto header = get_top(candidate, height);
43 ✔
488
    auto model = value_from(chain::checkpoint{ header->hash(), height });
43 ✔
489
    model.as_object().emplace("header", value_from(header));
43 ✔
490
    return model;
43 ✔
491
}
43 ✔
492

493
data_chunk protocol_native::to_tops() const NOEXCEPT
17 ✔
494
{
495
    return splice(to_top(false), to_top(true));
17 ✔
496
}
497

498
value protocol_native::to_tops_json() const NOEXCEPT
20 ✔
499
{
500
    return
501
    {
502
        { "confirmed", to_top_json(false) },
20 ✔
503
        { "candidate", to_top_json(true) }
40 ✔
504
    };
80 ✔
505
}
506

507
BC_POP_WARNING()
508

509
database::header_link protocol_native::to_header(
96 ✔
510
    const std::optional<uint32_t>& height,
511
    const std::optional<hash_cptr>& hash, bool candidate) NOEXCEPT
512
{
513
    const auto& query = archive();
96 ✔
514

515
    if (hash.has_value())
96 ✔
516
        return query.to_header(*(hash.value()));
9 ✔
517

518
    if (!height.has_value())
87 ✔
519
        return {};
×
520

521
    return candidate ? query.to_candidate(height.value()) :
87 ✔
522
        query.to_confirmed(height.value());
87 ✔
523
}
524

525
// Use if deserialization is required.
526
#if defined(UNDEFINED)
527
using inpoints = database::inpoints;
528
using outpoints = database::outpoints;
529

530
DECLARE_JSON_TAG_INVOKE(inpoints);
531
DECLARE_JSON_TAG_INVOKE(outpoints);
532

533
DEFINE_JSON_TO_TAG(inpoints)
534
{
535
    inpoints out{};
536
    for (const auto& point: value.as_array())
537
        out.insert(value_to<inpoint>(point));
538

539
    return out;
540
}
541

542
DEFINE_JSON_TO_TAG(outpoints)
543
{
544
    outpoints out{};
545
    for (const auto& point: value.as_array())
546
        out.insert(value_to<outpoint>(point));
547

548
    return out;
549
}
550
#endif
551

552
} // namespace server
553
} // 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