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

libbitcoin / libbitcoin-server / 45530296317

25 Nov 2026 02:04AM UTC coverage: 93.825% (-0.07%) from 93.89%
45530296317

push

github

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

Add native headers and txs sets, strong and branch tops.

232 of 248 new or added lines in 5 files covered. (93.55%)

7 existing lines in 1 file now uncovered.

14464 of 15416 relevant lines covered (93.82%)

451.19 hits per line

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

95.13
/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
374 ✔
44
{
45
    BC_ASSERT(stranded());
374 ✔
46

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

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

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

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

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

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

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

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

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

104
    // Fee methods.
105
    SUBSCRIBE_NATIVE(handle_get_template, _1, _2, _3, _4, _5);
374 ✔
106
    SUBSCRIBE_NATIVE(handle_get_template_txs, _1, _2, _3, _4, _5);
374 ✔
107
    SUBSCRIBE_NATIVE(handle_get_template_txs_details, _1, _2, _3, _4, _5);
374 ✔
108
    SUBSCRIBE_NATIVE(handle_get_template_subscribe, _1, _2, _3, _4, _5);
374 ✔
109
    SUBSCRIBE_NATIVE(handle_get_fees, _1, _2, _3, _4);
374 ✔
110

111
    // Header methods.
112
    SUBSCRIBE_NATIVE(handle_get_headers, _1, _2, _3, _4);
374 ✔
113
    SUBSCRIBE_NATIVE(handle_get_headers_confirmed, _1, _2, _3, _4);
374 ✔
114
    SUBSCRIBE_NATIVE(handle_get_headers_candidate, _1, _2, _3, _4);
374 ✔
115
    SUBSCRIBE_NATIVE(handle_get_headers_top, _1, _2, _3, _4);
374 ✔
116

117
    // Transactions methods.
118
    SUBSCRIBE_NATIVE(handle_get_txs, _1, _2, _3, _4);
374 ✔
119
    SUBSCRIBE_NATIVE(handle_get_txs_details, _1, _2, _3, _4);
374 ✔
120
    SUBSCRIBE_NATIVE(handle_get_txs_confirmed, _1, _2, _3, _4);
374 ✔
121
    SUBSCRIBE_NATIVE(handle_get_txs_confirmed_details, _1, _2, _3, _4);
374 ✔
122
    SUBSCRIBE_NATIVE(handle_get_txs_unconfirmed, _1, _2, _3, _4);
374 ✔
123
    SUBSCRIBE_NATIVE(handle_get_txs_unconfirmed_details, _1, _2, _3, _4);
374 ✔
124
    protocol_html::start();
374 ✔
125
}
126

127
void protocol_native::stopping(const code& ec) NOEXCEPT
374 ✔
128
{
129
    BC_ASSERT(stranded());
374 ✔
130
    stopping_.store(true);
374 ✔
131
    if (template_subscribe_.exchange(media_type::unknown) != media_type::unknown)
374 ✔
132
        demand_template(false);
3 ✔
133

134
    dispatcher_.stop(ec);
374 ✔
135
    unsubscribe_chase();
374 ✔
136
    protocol_html::stopping(ec);
374 ✔
137
}
374 ✔
138

139
// Dispatch.
140
// ----------------------------------------------------------------------------
141

142
bool protocol_native::try_dispatch_object(const http::request& request) NOEXCEPT
313 ✔
143
{
144
    BC_ASSERT(stranded());
313 ✔
145

146
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
147
    const auto target = request.target();
313 ✔
148
    BC_POP_WARNING()
149

150
    rpc::request_t model{};
313 ✔
151
    if (const auto ec = native_target(model, target))
313 ✔
152
    {
153
        // A target without a version or requesting html is a page request.
154
        if (ec == error::missing_version || ec == error::empty_path ||
41 ✔
155
            is_page_request(request))
9 ✔
156
            return false;
32 ✔
157

158
        send_bad_target(ec, *reset_request());
6 ✔
159
        return true;
6 ✔
160
    }
161

162
    // No media defaults injected for an http request.
163
    if (!native_query(model, request))
281 ✔
164
    {
165
        send_bad_request();
4 ✔
166
        return true;
167
    }
168

169
    const auto media = get_media(model);
277 ✔
170
    if (media == media_type::unknown)
277 ✔
171
    {
172
        send_not_acceptable();
2 ✔
173
        return true;
174
    }
175

176
    // Falls through to html page dispatch.
177
    if (media == media_type::text_html)
275 ✔
178
        return false;
179

180
    // The credential may be restricted to a subset of interface methods.
181
    if (!permitted(model.method))
273 ✔
182
    {
183
        send_forbidden();
2 ✔
184
        return true;
185
    }
186

187
    if (dispatcher_.notify(model))
271 ✔
188
        send_bad_request();
1 ✔
189

190
    return true;
191
}
313 ✔
192

193
bool protocol_native::is_page_request(
10 ✔
194
    const http::request& request) const NOEXCEPT
195
{
196
    rpc::request_t model{ .params = rpc::object_t{} };
10 ✔
197
    return native_query(model, request) &&
20 ✔
198
        get_media(model) == media_type::text_html;
20 ✔
199
}
10 ✔
200

201
void protocol_native::dispatch_websocket(const http::request& request) NOEXCEPT
40 ✔
202
{
203
    BC_ASSERT(stranded());
40 ✔
204

205
    // ws frames carry no headers, so ws authorization is enforced here, not by
206
    // channel (established by basic auth on upgrade request).
207
    if (!authorized())
40 ✔
208
    {
209
        send_unauthorized(request);
×
210
        return;
4 ✔
211
    }
212

213
    // Native websocket interface supports only string requests.
214
    if (!request.body().contains<http::string_value>())
40 ✔
215
    {
216
        send_not_acceptable(request);
×
217
        return;
×
218
    }
219

220
    // Target with query string is passed via websocket string body.
221
    const auto target = request.body().get<http::string_value>();
40 ✔
222

223
    rpc::request_t model{};
40 ✔
224
    if (const auto ec = native_target(model, target))
40 ✔
225
    {
226
        send_bad_target(ec, request);
2 ✔
227
        return;
2 ✔
228
    }
229

230
    // Default to json by simulating a json accept header (format overrides).
231
    if (!native_query(model, target, { media_type::application_json }))
38 ✔
232
    {
233
        send_bad_request(request);
1 ✔
234
        return;
235
    }
236

237
    if (get_media(model) == media_type::text_html)
37 ✔
238
    {
239
        send_not_acceptable(request);
1 ✔
240
        return;
241
    }
242

243
    // The credential may be restricted to a subset of interface methods.
244
    if (!permitted(model.method))
36 ✔
245
    {
246
        send_forbidden(request);
×
247
        return;
248
    }
249

250
    if (dispatcher_.notify(model))
36 ✔
251
        send_bad_request(request);
1 ✔
252
}
40 ✔
253

254
// Senders.
255
// ----------------------------------------------------------------------------
256

257
void protocol_native::send_not_found(const http::request& request) NOEXCEPT
63 ✔
258
{
259
    BC_ASSERT(stranded());
63 ✔
260

261
    if (websocket())
63 ✔
262
    {
263
        send_empty();
2 ✔
264
        return;
2 ✔
265
    }
266

267
    protocol_html::send_not_found(request);
61 ✔
268
}
269

270
// Event handlers.
271
// ----------------------------------------------------------------------------
272

273
bool protocol_native::handle_chase(const code&,
415 ✔
274
    node::event_value value) NOEXCEPT
275
{
276
    // Do not pass ec to stopped as it is not a call status.
277
    if (stopped())
415 ✔
278
        return false;
279

280
    if (!websocket())
63 ✔
281
        return true;
282

283
    switch (node::to_chase(value))
18 ✔
284
    {
285
        case node::chase::block:
5 ✔
286
        {
287
            const auto block = node::to_payload<node::chase::block>(value);
5 ✔
288

289
            auto media = top_subscribe_.load(relaxed);
5 ✔
290
            if (media != media_type::unknown)
5 ✔
291
            {
292
                POST(do_top, media);
2 ✔
293
            }
294

295
            // No block emission for a fork point (start/reorg).
296
            media = block_subscribe_.load(relaxed);
5 ✔
297
            if (media != media_type::unknown)
5 ✔
298
            {
299
                POST(do_block, block.link, media);
3 ✔
300
            }
301

302
            break;
303
        }
304
        case node::chase::reorganized:
4 ✔
305
        case node::chase::headers:
306
        case node::chase::regressed:
307
        case node::chase::disorganized:
308
        {
309
            const auto media = top_subscribe_.load(relaxed);
4 ✔
310
            if (media != media_type::unknown)
4 ✔
311
            {
312
                POST(do_top, media);
4 ✔
313
            }
314

315
            break;
316
        }
317
        case node::chase::transaction:
3 ✔
318
        {
319
            const auto media = tx_subscribe_.load(relaxed);
3 ✔
320
            if (media != media_type::unknown)
3 ✔
321
            {
322
                POST(do_transaction,
3 ✔
323
                    node::to_payload<node::chase::transaction>(value).link,
324
                    media);
325
            }
326

327
            break;
328
        }
329
        case node::chase::template_:
3 ✔
330
        {
331
            const auto media = template_subscribe_.load(relaxed);
3 ✔
332
            if (media != media_type::unknown)
3 ✔
333
            {
334
                POST(do_template,
3 ✔
335
                    node::to_payload<node::chase::template_>(value).height,
336
                    media);
337
            }
338

339
            break;
340
        }
341
        default:
342
        {
343
            break;
344
        }
345
    }
346

347
    return true;
348
}
349

350
// Utilities.
351
// ----------------------------------------------------------------------------
352
// private
353

354
protocol_native::media_type protocol_native::to_media(bool stop,
26 ✔
355
    uint8_t media) NOEXCEPT
356
{
357
    return stop ? media_type::unknown : static_cast<media_type>(media);
26 ✔
358
}
359

360
uint64_t protocol_native::to_subsidy(size_t height, bool bip42) const NOEXCEPT
15 ✔
361
{
362
    const auto& settings = system_settings();
15 ✔
363
    return chain::block::subsidy(height, settings.subsidy_interval_blocks,
15 ✔
364
        settings.initial_subsidy(), bip42);
15 ✔
365
}
366

367
using namespace system::json;
368

369
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
370

371
void protocol_native::inject(value& out, std::optional<uint32_t> height,
7 ✔
372
    const database::header_link& link) const NOEXCEPT
373
{
374
    out.as_object().emplace("height", height.has_value() ? height.value() :
9 ✔
375
        archive().get_height(link).value);
9 ✔
376
}
7 ✔
377

378
size_t protocol_native::to_root(std::vector<size_t>& roots,
26 ✔
379
    size_t index) NOEXCEPT
380
{
381
    while (roots.at(index) != index)
30 ✔
382
        index = roots.at(index) = roots.at(roots.at(index));
4 ✔
383

384
    return index;
26 ✔
385
}
386

387
std::vector<size_t> protocol_native::to_roots(
7 ✔
388
    const database::template_txs& txs) NOEXCEPT
389
{
390
    std::vector<size_t> roots(txs.size());
7 ✔
391
    std::iota(roots.begin(), roots.end(), zero);
7 ✔
392
    for (size_t index{}; index < txs.size(); ++index)
12 ✔
393
    {
394
        for (const auto parent: txs.at(index).parents)
7 ✔
395
        {
396
            const auto child = to_root(roots, index);
2 ✔
397
            roots.at(child) = to_root(roots, parent);
2 ✔
398
        }
399
    }
400

401
    return roots;
7 ✔
402
}
×
403

404
std::vector<size_t> protocol_native::to_roots(
11 ✔
405
    const database::tx_links& txs) const NOEXCEPT
406
{
407
    const auto& query = archive();
11 ✔
408
    const auto count = txs.size();
11 ✔
409
    std::unordered_map<hash_digest, size_t> positions{};
11 ✔
410
    positions.reserve(count);
11 ✔
411
    for (size_t index{}; index < count; ++index)
26 ✔
412
        positions.emplace(query.get_tx_key(txs.at(index)), index);
15 ✔
413

414
    std::vector<size_t> roots(count);
11 ✔
415
    std::iota(roots.begin(), roots.end(), zero);
11 ✔
416
    for (size_t index{}; index < count; ++index)
26 ✔
417
    {
418
        for (const auto& point: query.to_points(txs.at(index)))
32 ✔
419
        {
420
            const auto parent = positions.find(query.get_point_hash(point));
17 ✔
421
            if (parent == positions.end())
17 ✔
422
                continue;
15 ✔
423

424
            const auto child = to_root(roots, index);
2 ✔
425
            roots.at(child) = to_root(roots, parent->second);
2 ✔
426
        }
15 ✔
427
    }
428

429
    return roots;
11 ✔
430
}
11 ✔
431

432
// The details of each tx, with its cluster: the label, in order of first
433
// appearance, of its component under spends of other txs of the set.
434
bool protocol_native::to_tx_details(tx_details& out,
16 ✔
435
    const database::tx_links& txs, std::vector<size_t>&& roots) const NOEXCEPT
436
{
437
    const auto& query = archive();
16 ✔
438
    const auto count = txs.size();
16 ✔
439
    out.clear();
16 ✔
440
    out.reserve(count);
16 ✔
441
    size_t clusters{};
442
    std::vector<size_t> labels(count, max_size_t);
16 ✔
443
    for (size_t index{}; index < count; ++index)
32 ✔
444
    {
445
        uint64_t value{}, spend{};
17 ✔
446
        size_t nominal{}, maximal{};
17 ✔
447
        const auto& link = txs.at(index);
17 ✔
448
        const auto coinbase = query.is_coinbase(link);
17 ✔
449
        if (!query.get_tx_sizes(nominal, maximal, link) ||
51 ✔
450
            !query.get_tx_value(value, link) ||
51 ✔
451
            !query.get_tx_spend(spend, link) ||
61 ✔
452
            (!coinbase && is_subtract_overflow(value, spend)))
10 ✔
453
            return false;
1 ✔
454

455
        auto& label = labels.at(to_root(roots, index));
16 ✔
456
        if (label == max_size_t)
16 ✔
457
            label = clusters++;
13 ✔
458

459
        out.push_back(tx_detail
16 ✔
460
        {
461
            possible_narrow_cast<uint32_t>(nominal),
16 ✔
462
            possible_narrow_cast<uint32_t>(maximal),
16 ✔
463
            value,
464
            spend,
465
            floored_subtract(value, spend),
16 ✔
466
            possible_narrow_cast<uint32_t>(label),
16 ✔
467
            0u
468
        });
469
    }
470

471
    return true;
472
}
16 ✔
473

474
// Data is the fixed width fields of each tx, text is that in base16, and json
475
// adds what follows from the sizes. The time is included where timed.
476
void protocol_native::send_tx_details(const tx_details& details,
15 ✔
477
    uint8_t media, bool timed) NOEXCEPT
478
{
479
    if (media == json)
15 ✔
480
    {
481
        system::json::array out{};
10 ✔
482
        out.reserve(details.size());
10 ✔
483
        for (const auto& tx: details)
26 ✔
484
        {
485
            const auto nominal = size_t{ tx.nominal };
16 ✔
486
            const auto maximal = size_t{ tx.maximal };
16 ✔
487
            system::json::object object
488
            {
489
                { "segregated", maximal != nominal },
32 ✔
490
                { "nominal", nominal },
491
                { "maximal", maximal },
492
                { "weight", chain::weighted_size(nominal, maximal) },
32 ✔
493
                { "virtual", chain::virtual_size(nominal, maximal) },
32 ✔
494
                { "value", tx.value },
16 ✔
495
                { "spend", tx.spend },
16 ✔
496
                { "fee", tx.fee },
16 ✔
497
                { "cluster", tx.cluster }
16 ✔
498
            };
16 ✔
499

500
            if (timed)
16 ✔
501
                object.emplace("time", tx.time);
2 ✔
502

503
            out.emplace_back(std::move(object));
16 ✔
504
        }
16 ✔
505

506
        send_json(std::move(out));
10 ✔
507
        return;
508
    }
10 ✔
509

510
    if (media != data && media != text)
5 ✔
511
    {
NEW
512
        send_not_acceptable();
×
NEW
513
        return;
×
514
    }
515

516
    data_chunk out{};
5 ✔
517
    write::bytes::data sink{ out };
5 ✔
518
    for (const auto& tx: details)
10 ✔
519
    {
520
        sink.write_4_bytes_little_endian(tx.nominal);
5 ✔
521
        sink.write_4_bytes_little_endian(tx.maximal);
5 ✔
522
        sink.write_8_bytes_little_endian(tx.value);
5 ✔
523
        sink.write_8_bytes_little_endian(tx.spend);
5 ✔
524
        sink.write_8_bytes_little_endian(tx.fee);
5 ✔
525
        sink.write_4_bytes_little_endian(tx.cluster);
5 ✔
526
        if (timed)
5 ✔
527
            sink.write_4_bytes_little_endian(tx.time);
1 ✔
528
    }
529

530
    sink.flush();
5 ✔
531
    if (media == data)
5 ✔
532
        send_chunk(std::move(out));
3 ✔
533
    else
534
        send_text(encode_base16(out));
2 ✔
535
}
5 ✔
536

537
bool protocol_native::to_pruned_input(system::json::value& out,
2 ✔
538
    const database::ins_link& link) const NOEXCEPT
539
{
540
    const auto& query = archive();
2 ✔
541
    uint32_t sequence{};
2 ✔
542
    if (!query.get_input_sequence(sequence, link))
2 ✔
543
        return false;
544

545
    out = system::json::object
4 ✔
546
    {
547
        { "point", value_from(query.get_point(link)) },
4 ✔
548
        { "sequence", sequence }
549
    };
2 ✔
550

551
    return true;
2 ✔
552
}
553

554
bool protocol_native::to_pruned_inputs(system::json::array& out,
1 ✔
555
    const database::tx_link& link) const NOEXCEPT
556
{
557
    const auto points = archive().to_points(link);
1 ✔
558
    if (points.empty())
1 ✔
559
        return false;
560

561
    out.clear();
1 ✔
562
    out.reserve(points.size());
1 ✔
563
    for (const auto& point: points)
2 ✔
564
        if (!to_pruned_input(out.emplace_back(), point))
1 ✔
565
            return false;
×
566

567
    return true;
1 ✔
568
}
1 ✔
569

UNCOV
570
chain::header::cptr protocol_native::get_top(bool candidate,
×
571
    size_t& height) const NOEXCEPT
572
{
UNCOV
573
    const auto& query = archive();
×
UNCOV
574
    height = candidate ? query.get_top_candidate() : query.get_top_confirmed();
×
UNCOV
575
    return query.get_header(candidate ? query.to_candidate(height) :
×
UNCOV
576
        query.to_confirmed(height));
×
577
}
578

579
// The strong tops, confirmed then candidate, the candidate where distinct.
580
database::header_links protocol_native::to_strong_tops() const NOEXCEPT
43 ✔
581
{
582
    const auto& query = archive();
43 ✔
583
    const auto confirmed = query.to_confirmed(query.get_top_confirmed());
43 ✔
584
    const auto candidate = query.to_candidate(query.get_top_candidate());
43 ✔
585
    if (candidate == confirmed)
43 ✔
586
        return { confirmed.value };
2 ✔
587

588
    return { confirmed.value, candidate.value };
41 ✔
589
}
43 ✔
590

591
// A top is its checkpoint followed by its header.
592
data_chunk protocol_native::to_header_top(
42 ✔
593
    const database::header_link& link) const NOEXCEPT
594
{
595
    const auto& query = archive();
42 ✔
596
    size_t height{};
42 ✔
597
    const auto header = query.get_header(link);
42 ✔
598
    query.get_height(height, link);
42 ✔
599
    return splice(chain::checkpoint{ header->hash(), height }.to_data(),
84 ✔
600
        header->to_data());
168 ✔
601
}
42 ✔
602

603
value protocol_native::to_header_top_json(const database::header_link& link,
51 ✔
604
    const std::string_view& role) const NOEXCEPT
605
{
606
    const auto& query = archive();
51 ✔
607
    size_t height{};
51 ✔
608
    const auto header = query.get_header(link);
51 ✔
609
    query.get_height(height, link);
51 ✔
610
    auto model = value_from(chain::checkpoint{ header->hash(), height });
51 ✔
611
    auto& object = model.as_object();
51 ✔
612
    object.emplace("header", value_from(header));
51 ✔
613
    if (!role.empty())
51 ✔
614
        object.emplace("chain", std::string{ role });
50 ✔
615

616
    return model;
51 ✔
617
}
51 ✔
618

619
data_chunk protocol_native::to_top(bool candidate) const NOEXCEPT
2 ✔
620
{
621
    const auto& query = archive();
2 ✔
622
    return to_header_top(candidate ?
4 ✔
623
        query.to_candidate(query.get_top_candidate()) :
624
        query.to_confirmed(query.get_top_confirmed()));
4 ✔
625
}
626

627
value protocol_native::to_top_json(bool candidate) const NOEXCEPT
5 ✔
628
{
629
    const auto& query = archive();
5 ✔
630
    return candidate ?
5 ✔
631
        to_header_top_json(query.to_candidate(query.get_top_candidate()),
6 ✔
632
            "candidate") :
633
        to_header_top_json(query.to_confirmed(query.get_top_confirmed()),
4 ✔
634
            "confirmed");
10 ✔
635
}
636

637
data_chunk protocol_native::to_tops() const NOEXCEPT
20 ✔
638
{
639
    data_chunk out{};
20 ✔
640
    for (const auto link: to_strong_tops())
59 ✔
641
        extend(out, to_header_top(link));
59 ✔
642

643
    return out;
20 ✔
UNCOV
644
}
×
645

646
value protocol_native::to_tops_json() const NOEXCEPT
23 ✔
647
{
648
    const auto tops = to_strong_tops();
23 ✔
649
    system::json::array out{};
23 ✔
650
    out.reserve(tops.size());
23 ✔
651
    out.push_back(to_header_top_json(tops.front(), "confirmed"));
23 ✔
652
    if (is_one(tops.size()))
23 ✔
653
        return out;
1 ✔
654

655
    out.push_back(to_header_top_json(tops.back(), "candidate"));
22 ✔
656
    return out;
22 ✔
657
}
23 ✔
658

659
// The strong tops, then the tops of weak branches.
660
data_chunk protocol_native::to_headers_top() const NOEXCEPT
2 ✔
661
{
662
    auto out = to_tops();
2 ✔
663
    for (const auto link: archive().get_branch_tops())
3 ✔
664
        extend(out, to_header_top(link));
3 ✔
665

666
    return out;
2 ✔
NEW
667
}
×
668

669
value protocol_native::to_headers_top_json() const NOEXCEPT
2 ✔
670
{
671
    auto out = to_tops_json();
2 ✔
672
    auto& tops = out.as_array();
2 ✔
673
    for (const auto link: archive().get_branch_tops())
3 ✔
674
        tops.push_back(to_header_top_json(link, {}));
3 ✔
675

676
    return out;
2 ✔
UNCOV
677
}
×
678

679
BC_POP_WARNING()
680

681
database::header_link protocol_native::to_header(
98 ✔
682
    const std::optional<uint32_t>& height,
683
    const std::optional<hash_cptr>& hash, bool candidate) NOEXCEPT
684
{
685
    const auto& query = archive();
98 ✔
686

687
    if (hash.has_value())
98 ✔
688
        return query.to_header(*(hash.value()));
9 ✔
689

690
    if (!height.has_value())
89 ✔
691
        return {};
×
692

693
    return candidate ? query.to_candidate(height.value()) :
89 ✔
694
        query.to_confirmed(height.value());
89 ✔
695
}
696

697
// Use if deserialization is required.
698
#if defined(UNDEFINED)
699
using inpoints = database::inpoints;
700
using outpoints = database::outpoints;
701

702
DECLARE_JSON_TAG_INVOKE(inpoints);
703
DECLARE_JSON_TAG_INVOKE(outpoints);
704

705
DEFINE_JSON_TO_TAG(inpoints)
706
{
707
    inpoints out{};
708
    for (const auto& point: value.as_array())
709
        out.insert(value_to<inpoint>(point));
710

711
    return out;
712
}
713

714
DEFINE_JSON_TO_TAG(outpoints)
715
{
716
    outpoints out{};
717
    for (const auto& point: value.as_array())
718
        out.insert(value_to<outpoint>(point));
719

720
    return out;
721
}
722
#endif
723

724
} // namespace server
725
} // 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