• 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

96.96
/src/protocols/native/protocol_native_block.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 <ranges>
24
#include <bitcoin/server/define.hpp>
25

26
namespace libbitcoin {
27
namespace server {
28

29
using namespace system;
30
using namespace network::messages::peer;
31

32
BC_PUSH_WARNING(NO_INCOMPLETE_SWITCH)
33
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
34

35
void protocol_native::handle_get_top(const code& ec, interface::top,
407 ✔
36
    uint8_t, uint8_t media) NOEXCEPT
37
{
38
    if (stopped(ec))
407 ✔
39
        return;
40

41
    switch (media)
33 ✔
42
    {
43
        case data:
8 ✔
44
            send_chunk(to_tops());
8 ✔
45
            return;
8 ✔
46
        case text:
5 ✔
47
            send_text(encode_base16(to_tops()));
5 ✔
48
            return;
5 ✔
49
        case json:
20 ✔
50
            send_json(to_tops_json());
20 ✔
51
            return;
20 ✔
52
    }
53

54
    send_not_found();
×
55
}
56

57
void protocol_native::handle_get_headers(const code& ec, interface::headers,
375 ✔
58
    uint8_t, uint8_t) NOEXCEPT
59
{
60
    if (stopped(ec))
375 ✔
61
        return;
62

63
    send_not_implemented();
1 ✔
64
}
65

66
void protocol_native::handle_get_headers_confirmed(const code& ec,
375 ✔
67
    interface::headers_confirmed, uint8_t, uint8_t) NOEXCEPT
68
{
69
    if (stopped(ec))
375 ✔
70
        return;
71

72
    send_not_implemented();
1 ✔
73
}
74

75
void protocol_native::handle_get_headers_candidate(const code& ec,
375 ✔
76
    interface::headers_candidate, uint8_t, uint8_t) NOEXCEPT
77
{
78
    if (stopped(ec))
375 ✔
79
        return;
80

81
    send_not_implemented();
1 ✔
82
}
83

84
void protocol_native::handle_get_headers_top(const code& ec,
378 ✔
85
    interface::headers_top, uint8_t, uint8_t media) NOEXCEPT
86
{
87
    if (stopped(ec))
378 ✔
88
        return;
89

90
    switch (media)
4 ✔
91
    {
92
        case data:
2 ✔
93
            send_chunk(to_headers_top());
2 ✔
94
            return;
2 ✔
NEW
95
        case text:
×
NEW
96
            send_text(encode_base16(to_headers_top()));
×
NEW
97
            return;
×
98
        case json:
2 ✔
99
            send_json(to_headers_top_json());
2 ✔
100
            return;
2 ✔
101
    }
102

NEW
103
    send_not_found();
×
104
}
105

106
void protocol_native::handle_get_top_subscribe(const code& ec,
385 ✔
107
    interface::top_subscribe, uint8_t version, uint8_t media,
108
    bool stop) NOEXCEPT
109
{
110
    if (stopped(ec))
385 ✔
111
        return;
112

113
    top_subscribe_.store(to_media(stop, media), std::memory_order_relaxed);
11 ✔
114
    if (stop)
11 ✔
115
    {
116
        send_empty();
1 ✔
117
        return;
1 ✔
118
    }
119

120
    return handle_get_top(ec, {}, version, media);
10 ✔
121
}
122

123
void protocol_native::handle_get_top_chain(const code& ec,
381 ✔
124
    interface::top_chain, uint8_t, uint8_t media, bool candidate) NOEXCEPT
125
{
126
    if (stopped(ec))
381 ✔
127
        return;
128

129
    switch (media)
7 ✔
130
    {
131
        case data:
1 ✔
132
            send_chunk(to_top(candidate));
1 ✔
133
            return;
1 ✔
134
        case text:
1 ✔
135
            send_text(encode_base16(to_top(candidate)));
1 ✔
136
            return;
1 ✔
137
        case json:
5 ✔
138
            send_json(to_top_json(candidate));
5 ✔
139
            return;
5 ✔
140
    }
141

142
    send_not_found();
×
143
}
144

145
void protocol_native::handle_get_top_height(const code& ec,
379 ✔
146
    interface::top_height, uint8_t, uint8_t media, bool candidate) NOEXCEPT
147
{
148
    if (stopped(ec))
379 ✔
149
        return;
150

151
    const auto& query = archive();
5 ✔
152
    const auto height = possible_narrow_cast<uint32_t>(candidate ?
5 ✔
153
        query.get_top_candidate() : query.get_top_confirmed());
5 ✔
154

155
    switch (media)
5 ✔
156
    {
157
        case data:
1 ✔
158
            send_chunk(to_chunk(to_little_endian(height)));
1 ✔
159
            return;
1 ✔
160
        case text:
1 ✔
161
            send_text(encode_base16(to_little_endian(height)));
1 ✔
162
            return;
1 ✔
163
        case json:
3 ✔
164
            send_json(height);
3 ✔
165
            return;
3 ✔
166
    }
167

168
    send_not_found();
×
169
}
170

171
// notify
172
void protocol_native::do_top(media_type media) NOEXCEPT
6 ✔
173
{
174
    BC_ASSERT(stranded());
6 ✔
175

176
    switch (to_value(media))
6 ✔
177
    {
178
        case data:
4 ✔
179
            notify_chunk(to_tops());
4 ✔
180
            return;
4 ✔
181
        case text:
1 ✔
182
            notify_text(encode_base16(to_tops()));
1 ✔
183
            return;
1 ✔
184
        case json:
1 ✔
185
            notify_json(to_tops_json());
1 ✔
186
            return;
1 ✔
187
    }
188
}
189

190
void protocol_native::handle_get_block(const code& ec, interface::block,
394 ✔
191
    uint8_t, uint8_t media, std::optional<hash_cptr> hash,
192
    std::optional<uint32_t> height, bool witness, bool candidate) NOEXCEPT
193
{
194
    if (stopped(ec))
394 ✔
195
        return;
394 ✔
196

197
    const auto& query = archive();
20 ✔
198
    const auto link = to_header(height, hash, candidate);
20 ✔
199
    if (link.is_terminal() || query.is_pruned(link))
20 ✔
200
    {
201
        send_not_found();
8 ✔
202
        return;
203
    }
204

205
    size_t size{};
12 ✔
206
    if (!query.get_block_size(size, link, witness))
12 ✔
207
    {
208
        send_internal_server_error(database::error::integrity);
1 ✔
209
        return;
1 ✔
210
    }
211

212
    switch (media)
11 ✔
213
    {
214
        case data:
7 ✔
215
        {
216
            send_stream(size, [&query, link, witness, turbo = turbo_](
7 ✔
217
                const data_slab& out) NOEXCEPT
218
            {
219
                return query.get_wire_block(out, link, witness, turbo);
7 ✔
220
            }, media_type::application_octet_stream);
221
            return;
7 ✔
222
        }
223
        case text:
1 ✔
224
        {
225
            send_stream(two * size, [&query, link, witness, turbo = turbo_](
1 ✔
226
                const data_slab& out) NOEXCEPT
227
            {
228
                return query.get_wire_block<write::base16::fast>(out, link,
1 ✔
229
                    witness, turbo);
1 ✔
230
            }, media_type::text_plain);
231
            return;
1 ✔
232
        }
233
        case json:
3 ✔
234
        {
235
            const auto block = query.get_block(link, witness);
3 ✔
236
            if (!block)
3 ✔
237
            {
238
                send_internal_server_error(database::error::integrity);
×
239
                return;
×
240
            }
241

242
            auto model = value_from(block);
3 ✔
243
            inject(model.at("header"), height, link);
3 ✔
244
            send_json(std::move(model));
3 ✔
245
            return;
246
        }
3 ✔
247
    }
248

249
    send_not_found();
×
250
}
20 ✔
251

252
void protocol_native::handle_get_block_header(const code& ec,
383 ✔
253
    interface::block_header, uint8_t, uint8_t media,
254
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
255
    bool candidate) NOEXCEPT
256
{
257
    if (stopped(ec))
383 ✔
258
        return;
380 ✔
259

260
    const auto link = to_header(height, hash, candidate);
9 ✔
261
    if (const auto header = archive().get_header(link))
9 ✔
262
    {
263
        constexpr auto size = chain::header::serialized_size();
264
        switch (media)
6 ✔
265
        {
266
            case data:
2 ✔
267
                send_chunk(to_bin(*header, size));
2 ✔
268
                return;
2 ✔
269
            case text:
1 ✔
270
                send_text(to_hex(*header, size));
1 ✔
271
                return;
1 ✔
272
            case json:
3 ✔
273
                auto model = value_from(header);
3 ✔
274
                inject(model, height, link);
3 ✔
275
                send_json(std::move(model));
3 ✔
276
                return;
277
        }
3 ✔
278
    }
9 ✔
279

280
    send_not_found();
3 ✔
281
}
9 ✔
282

283
void protocol_native::handle_get_block_header_context(const code& ec,
379 ✔
284
    interface::block_header_context, uint8_t, uint8_t media,
285
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
286
    bool candidate) NOEXCEPT
287
{
288
    if (stopped(ec))
379 ✔
289
        return;
377 ✔
290

291
    if (media != json)
5 ✔
292
    {
293
        send_not_acceptable();
1 ✔
294
        return;
1 ✔
295
    }
296

297
    database::context context{};
4 ✔
298
    const auto& query = archive();
4 ✔
299
    const auto link = to_header(height, hash, candidate);
4 ✔
300
    if (!query.get_context(context, link))
4 ✔
301
    {
302
        send_not_found();
2 ✔
303
        return;
304
    }
305

306
    system::json::object object
307
    {
308
        { "hash", encode_hash(query.get_header_key(link)) },
4 ✔
309
        { "height", context.height },
310
        { "mtp", context.mtp },
311
    };
2 ✔
312

313
    // The "state" element implies transactions are associated.
314
    if (query.is_associated(link))
2 ✔
315
    {
316
        size_t nominal{}, maximal{};
2 ✔
317
        if (!query.get_block_sizes(nominal, maximal, link))
2 ✔
318
        {
319
            send_internal_server_error(database::error::integrity);
×
320
            return;
×
321
        }
322

323
        const auto bypass = query.is_bypass(link);
2 ✔
324

325
        object["state"] = system::json::object
6 ✔
326
        {
327
            { "size", nominal },
328
            { "weight", chain::weighted_size(nominal, maximal) },
2 ✔
329
            { "count", query.get_tx_count(link) },
2 ✔
330
            { "validated", bypass || query.is_validated(link) },
2 ✔
331
            { "confirmed", query.is_confirmed_block(link) },
2 ✔
332
            { "confirmable", bypass || query.is_confirmable(link) },
2 ✔
333
            { "unconfirmable", !bypass && query.is_unconfirmable(link) }
4 ✔
334
        };
2 ✔
335
    }
336

337
    // TODO: move to system serializer.
338
    // All modern configurable forks.
339
    object["forks"] = system::json::object
4 ✔
340
    {
341
        { "bip16",  context.is_enabled(chain::flags::bip16_rule) },
2 ✔
342
        { "bip30",  context.is_enabled(chain::flags::bip30_rule) },
2 ✔
343
        { "bip34",  context.is_enabled(chain::flags::bip34_rule) },
2 ✔
344
        { "bip66",  context.is_enabled(chain::flags::bip66_rule) },
2 ✔
345
        { "bip65",  context.is_enabled(chain::flags::bip65_rule) },
2 ✔
346
        { "bip68",  context.is_enabled(chain::flags::bip68_rule) },
2 ✔
347
        { "bip112", context.is_enabled(chain::flags::bip112_rule) },
2 ✔
348
        { "bip113", context.is_enabled(chain::flags::bip113_rule) },
2 ✔
349
        { "bip141", context.is_enabled(chain::flags::bip141_rule) },
2 ✔
350
        { "bip143", context.is_enabled(chain::flags::bip143_rule) },
2 ✔
351
        { "bip147", context.is_enabled(chain::flags::bip147_rule) },
2 ✔
352
        { "bip42",  context.is_enabled(chain::flags::bip42_rule) },
2 ✔
353
        { "bip341", context.is_enabled(chain::flags::bip341_rule) },
2 ✔
354
        { "bip342", context.is_enabled(chain::flags::bip342_rule) },
2 ✔
355
        { "bip54_timewarp", context.is_enabled(chain::flags::bip54_timewarp_rule) },
2 ✔
356
        { "bip54_timespan", context.is_enabled(chain::flags::bip54_timespan_rule) },
2 ✔
357
        { "bip54_sigops", context.is_enabled(chain::flags::bip54_sigops_rule) },
2 ✔
358
        { "bip54_txsize", context.is_enabled(chain::flags::bip54_txsize_rule) },
2 ✔
359
        { "bip54_coinbase", context.is_enabled(chain::flags::bip54_coinbase_rule) }
2 ✔
360
    };
2 ✔
361

362
    send_json(std::move(object));
2 ✔
363
}
4 ✔
364

365
void protocol_native::handle_get_block_details(const code& ec,
381 ✔
366
    interface::block_details, uint8_t, uint8_t media,
367
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
368
    bool candidate) NOEXCEPT
369
{
370
    if (stopped(ec))
381 ✔
371
        return;
378 ✔
372

373
    if (media != json)
7 ✔
374
    {
375
        send_not_acceptable();
1 ✔
376
        return;
1 ✔
377
    }
378

379
    const auto& query = archive();
6 ✔
380
    const auto link = to_header(height, hash, candidate);
6 ✔
381

382
    // Missing block.
383
    if (!query.is_associated(link))
6 ✔
384
    {
385
        send_not_found();
2 ✔
386
        return;
387
    }
388

389
    const auto count = query.get_tx_count(link);
4 ✔
390
    const auto key = hash.has_value() ? *hash.value() :
4 ✔
391
        query.get_header_key(link);
4 ✔
392

393
    database::context context{};
4 ✔
394
    size_t nominal{}, maximal{};
4 ✔
395
    uint64_t value{}, spend{}, claim{};
4 ✔
396
    if (is_zero(count) || key == system::null_hash ||
8 ✔
397
        !query.get_tx_spend(claim, query.to_coinbase(link)) ||
12 ✔
398
        !query.get_block_sizes(nominal, maximal, link) ||
8 ✔
399
        !query.get_block_value(value, link) ||
7 ✔
400
        !query.get_block_spend(spend, link) ||
6 ✔
401
        !query.get_context(context, link) ||
7 ✔
402
        is_subtract_overflow(value, spend))
3 ✔
403
    {
404
        send_internal_server_error(database::error::integrity);
1 ✔
405
        return;
1 ✔
406
    }
407

408
    const auto fees = floored_subtract(value, spend);
3 ✔
409
    const auto bip42 = context.is_enabled(chain::flags::bip42_rule);
3 ✔
410
    const auto subsidy = to_subsidy(context.height, bip42);
3 ✔
411

412
    // sigops is not cached, so removed for now.
413
    system::json::object object
414
    {
415
        { "hash", encode_hash(key) },
6 ✔
416
        { "height", context.height },
417
        { "count", count },
418
        { "segregated", maximal != nominal },
6 ✔
419
        { "nominal", nominal },
420
        { "maximal", maximal },
421
        { "weight", chain::weighted_size(nominal, maximal) },
6 ✔
422
        { "virtual", chain::virtual_size(nominal, maximal) },
6 ✔
423
        { "value", value },
424
        { "fees", fees },
425
        { "subsidy", subsidy },
426
        { "reward", ceilinged_add(fees, subsidy) },
3 ✔
427
        { "claim", claim }
428
    };
6 ✔
429

430
    send_json(std::move(object));
3 ✔
431
}
6 ✔
432

433
void protocol_native::handle_get_block_txs(const code& ec,
380 ✔
434
    interface::block_txs, uint8_t, uint8_t media,
435
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
436
    bool candidate) NOEXCEPT
437
{
438
    if (stopped(ec))
380 ✔
439
        return;
378 ✔
440

441
    const auto& query = archive();
6 ✔
442
    const auto link = to_header(height, hash, candidate);
6 ✔
443
    if (const auto hashes = query.get_tx_keys(link); !hashes.empty())
6 ✔
444
    {
445
        const auto size = hashes.size() * hash_size;
4 ✔
446
        switch (media)
4 ✔
447
        {
448
            case data:
2 ✔
449
            {
450
                const auto bytes = pointer_cast<const uint8_t>(hashes.data());
2 ✔
451
                send_chunk(to_chunk({ bytes, std::next(bytes, size) }));
2 ✔
452
                return;
2 ✔
453
            }
454
            case text:
1 ✔
455
            {
456
                const auto bytes = pointer_cast<const uint8_t>(hashes.data());
1 ✔
457
                send_text(encode_base16({ bytes, std::next(bytes, size) }));
1 ✔
458
                return;
1 ✔
459
            }
460
            case json:
1 ✔
461
            {
462
                system::json::array out(hashes.size());
1 ✔
463
                std::ranges::transform(hashes, out.begin(),
1 ✔
464
                    [](const auto& hash) { return encode_hash(hash); });
1 ✔
465
                send_json(out);
1 ✔
466
                return;
467
            }
1 ✔
468
        }
469
    }
6 ✔
470

471
    send_not_found();
2 ✔
472
}
6 ✔
473

474
void protocol_native::handle_get_block_txs_details(const code& ec,
384 ✔
475
    interface::block_txs_details, uint8_t, uint8_t media,
476
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
477
    bool candidate) NOEXCEPT
478
{
479
    if (stopped(ec))
384 ✔
480
        return;
377 ✔
481

482
    const auto& query = archive();
10 ✔
483
    const auto link = to_header(height, hash, candidate);
10 ✔
484

485
    // Missing block.
486
    if (!query.is_associated(link))
10 ✔
487
    {
488
        send_not_found();
2 ✔
489
        return;
490
    }
491

492
    tx_details out{};
8 ✔
493
    database::context context{};
8 ✔
494
    const auto txs = query.to_transactions(link);
8 ✔
495
    if (txs.empty() || !to_tx_details(out, txs, to_roots(txs)) ||
22 ✔
496
        !query.get_context(context, link))
7 ✔
497
    {
498
        send_internal_server_error(database::error::integrity);
1 ✔
499
        return;
500
    }
501

502
    uint64_t fees{};
503
    for (const auto& tx: out)
17 ✔
504
        fees = ceilinged_add(fees, tx.fee);
10 ✔
505

506
    const auto bip42 = context.is_enabled(chain::flags::bip42_rule);
7 ✔
507
    out.front().value = ceilinged_add(to_subsidy(context.height, bip42), fees);
7 ✔
508
    send_tx_details(out, media);
7 ✔
509
}
10 ✔
510

511
void protocol_native::handle_get_block_filter(const code& ec,
386 ✔
512
    interface::block_filter, uint8_t, uint8_t media, uint8_t type,
513
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
514
    bool candidate) NOEXCEPT
515
{
516
    if (stopped(ec))
386 ✔
517
        return;
384 ✔
518

519
    const auto& query = archive();
12 ✔
520
    if (!query.filter_enabled() || type != client_filter::type_id::neutrino)
12 ✔
521
    {
522
        send_not_implemented();
1 ✔
523
        return;
1 ✔
524
    }
525

526
    data_chunk filter{};
11 ✔
527
    if (query.get_filter_body(filter, to_header(height, hash, candidate)))
11 ✔
528
    {
529
        switch (media)
9 ✔
530
        {
531
            case data:
7 ✔
532
                send_chunk(std::move(filter));
7 ✔
533
                return;
534
            case text:
1 ✔
535
                send_text(encode_base16(filter));
1 ✔
536
                return;
1 ✔
537
            case json:
1 ✔
538
                send_json(value_from(encode_base16(filter)));
1 ✔
539
                return;
1 ✔
540
        }
541
    }
542

543
    send_not_found();
2 ✔
544
}
11 ✔
545

546
void protocol_native::handle_get_block_filter_hash(const code& ec,
381 ✔
547
    interface::block_filter_hash, uint8_t, uint8_t media, uint8_t type,
548
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
549
    bool candidate) NOEXCEPT
550
{
551
    if (stopped(ec))
381 ✔
552
        return;
379 ✔
553

554
    const auto& query = archive();
7 ✔
555
    if (!query.filter_enabled() || type != client_filter::type_id::neutrino)
7 ✔
556
    {
557
        send_not_implemented();
1 ✔
558
        return;
1 ✔
559
    }
560

561
    hash_digest filter_hash{ hash_size };
6 ✔
562
    if (query.get_filter_hash(filter_hash, to_header(height, hash, candidate)))
6 ✔
563
    {
564
        switch (media)
4 ✔
565
        {
566
            case data:
2 ✔
567
                send_chunk(to_chunk(filter_hash));
2 ✔
568
                return;
2 ✔
569
            case text:
1 ✔
570
                send_text(encode_base16(filter_hash));
1 ✔
571
                return;
1 ✔
572
            case json:
1 ✔
573
                send_json(value_from(encode_hash(filter_hash)));
1 ✔
574
                return;
1 ✔
575
        }
576
    }
577

578
    send_not_found();
2 ✔
579
}
580

581
void protocol_native::handle_get_block_filter_header(const code& ec,
380 ✔
582
    interface::block_filter_header, uint8_t, uint8_t media, uint8_t type,
583
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
584
    bool candidate) NOEXCEPT
585
{
586
    if (stopped(ec))
380 ✔
587
        return;
378 ✔
588

589
    const auto& query = archive();
6 ✔
590
    if (!query.filter_enabled() || type != client_filter::type_id::neutrino)
6 ✔
591
    {
592
        send_not_implemented();
1 ✔
593
        return;
1 ✔
594
    }
595

596
    hash_digest filter_head{ hash_size };
5 ✔
597
    if (query.get_filter_head(filter_head, to_header(height, hash, candidate)))
5 ✔
598
    {
599
        switch (media)
3 ✔
600
        {
601
            case data:
1 ✔
602
                send_chunk(to_chunk(filter_head));
1 ✔
603
                return;
1 ✔
604
            case text:
1 ✔
605
                send_text(encode_base16(filter_head));
1 ✔
606
                return;
1 ✔
607
            case json:
1 ✔
608
                send_json(value_from(encode_hash(filter_head)));
1 ✔
609
                return;
1 ✔
610
        }
611
    }
612

613
    send_not_found();
2 ✔
614
}
615

616
void protocol_native::handle_get_block_tx(const code& ec, interface::block_tx,
382 ✔
617
    uint8_t, uint8_t media, uint32_t position, std::optional<hash_cptr> hash,
618
    std::optional<uint32_t> height, bool witness, bool candidate) NOEXCEPT
619
{
620
    if (stopped(ec))
382 ✔
621
        return;
380 ✔
622

623
    const auto& query = archive();
8 ✔
624
    const auto link = query.to_transaction(to_header(height, hash, candidate),
8 ✔
625
        position);
8 ✔
626
    if (query.is_pruned(link))
8 ✔
627
    {
628
        send_not_found();
1 ✔
629
        return;
630
    }
631

632
    if (const auto tx = query.get_transaction(link, witness))
7 ✔
633
    {
634
        const auto size = tx->serialized_size(witness);
5 ✔
635
        switch (media)
5 ✔
636
        {
637
            case data:
3 ✔
638
                send_chunk(to_bin(*tx, size, witness));
3 ✔
639
                return;
3 ✔
640
            case text:
1 ✔
641
                send_text(to_hex(*tx, size, witness));
1 ✔
642
                return;
1 ✔
643
            case json:
1 ✔
644
                send_json(value_from(tx));
1 ✔
645
                return;
1 ✔
646
        }
647
    }
7 ✔
648

649
    send_not_found();
2 ✔
650
}
8 ✔
651

652
void protocol_native::handle_get_block_tx_input_script(const code& ec,
382 ✔
653
    interface::block_tx_input_script, uint8_t, uint8_t media,
654
    uint32_t position, uint32_t index, std::optional<hash_cptr> hash,
655
    std::optional<uint32_t> height, bool candidate) NOEXCEPT
656
{
657
    if (stopped(ec))
382 ✔
658
        return;
379 ✔
659

660
    const auto& query = archive();
8 ✔
661
    const auto link = query.to_transaction(to_header(height, hash, candidate),
8 ✔
662
        position);
8 ✔
663
    if (query.is_pruned(link))
8 ✔
664
    {
665
        send_not_found();
1 ✔
666
        return;
667
    }
668

669
    if (const auto script = query.get_input_script(query.to_point(link, index)))
7 ✔
670
    {
671
        const auto size = script->serialized_size(false);
4 ✔
672
        switch (media)
4 ✔
673
        {
674
            case data:
2 ✔
675
                send_chunk(to_bin(*script, size, false));
2 ✔
676
                return;
2 ✔
677
            case text:
1 ✔
678
                send_text(to_hex(*script, size, false));
1 ✔
679
                return;
1 ✔
680
            case json:
1 ✔
681
                send_json(value_from(script));
1 ✔
682
                return;
1 ✔
683
        }
684
    }
7 ✔
685

686
    send_not_found();
3 ✔
687
}
8 ✔
688

689
void protocol_native::handle_get_block_tx_output_script(const code& ec,
379 ✔
690
    interface::block_tx_output_script, uint8_t, uint8_t media,
691
    uint32_t position, uint32_t index, std::optional<hash_cptr> hash,
692
    std::optional<uint32_t> height, bool candidate) NOEXCEPT
693
{
694
    if (stopped(ec))
379 ✔
695
        return;
377 ✔
696

697
    const auto& query = archive();
5 ✔
698
    const auto tx = query.to_transaction(to_header(height, hash, candidate),
5 ✔
699
        position);
5 ✔
700

701
    if (const auto script = query.get_output_script(query.to_output(tx, index)))
5 ✔
702
    {
703
        const auto size = script->serialized_size(false);
3 ✔
704
        switch (media)
3 ✔
705
        {
706
            case data:
1 ✔
707
                send_chunk(to_bin(*script, size, false));
1 ✔
708
                return;
1 ✔
709
            case text:
1 ✔
710
                send_text(to_hex(*script, size, false));
1 ✔
711
                return;
1 ✔
712
            case json:
1 ✔
713
                send_json(value_from(script));
1 ✔
714
                return;
1 ✔
715
        }
716
    }
5 ✔
717

718
    send_not_found();
2 ✔
719
}
5 ✔
720

721
void protocol_native::handle_get_block_subscribe(const code& ec,
381 ✔
722
    interface::block_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
723
{
724
    if (stopped(ec))
381 ✔
725
        return;
381 ✔
726

727
    block_subscribe_.store(to_media(stop, media), std::memory_order_relaxed);
7 ✔
728
    if (stop)
7 ✔
729
    {
730
        send_empty();
1 ✔
731
        return;
1 ✔
732
    }
733

734
    // Return top block hash upon block subscription.
735
    const auto& query = archive();
6 ✔
736
    const auto link = query.to_top_confirmed();
6 ✔
737
    if (const auto hash = query.get_header_key(link); hash != null_hash)
6 ✔
738
    {
739
        switch (media)
6 ✔
740
        {
741
            case data:
2 ✔
742
                send_chunk(to_chunk(hash));
2 ✔
743
                return;
2 ✔
744
            case text:
2 ✔
745
                send_text(encode_base16(hash));
2 ✔
746
                return;
2 ✔
747
            case json:
2 ✔
748
                send_json(value_from(encode_hash(hash)));
2 ✔
749
                return;
2 ✔
750
        }
751
    }
752

753
    send_not_found();
×
754
}
6 ✔
755

756
// notify
757
void protocol_native::do_block(node::header_t link, media_type media) NOEXCEPT
3 ✔
758
{
759
    BC_ASSERT(stranded());
3 ✔
760

761
    const auto hash = archive().get_header_key(link);
3 ✔
762
    switch (to_value(media))
3 ✔
763
    {
764
        case data:
1 ✔
765
            notify_chunk(to_chunk(hash));
1 ✔
766
            return;
3 ✔
767
        case text:
1 ✔
768
            notify_text(encode_base16(hash));
1 ✔
769
            return;
1 ✔
770
        case json:
1 ✔
771
            notify_json(value_from(encode_hash(hash)));
1 ✔
772
            return;
1 ✔
773
    }
774
}
775

776
BC_POP_WARNING()
777
BC_POP_WARNING()
778

779
} // namespace server
780
} // 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