• 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.78
/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,
379 ✔
36
    uint8_t, uint8_t media) NOEXCEPT
37
{
38
    if (stopped(ec))
379 ✔
39
        return;
40

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

54
    send_not_found();
×
55
}
56

57
void protocol_native::handle_get_top_subscribe(const code& ec,
359 ✔
58
    interface::top_subscribe, uint8_t version, uint8_t media,
59
    bool stop) NOEXCEPT
60
{
61
    if (stopped(ec))
359 ✔
62
        return;
63

64
    const auto value = stop ? media_type::unknown : (media_type)media;
11 ✔
65
    top_subscribe_.store(value, std::memory_order_relaxed);
11 ✔
66
    if (stop)
11 ✔
67
    {
68
        send_empty();
1 ✔
69
        return;
1 ✔
70
    }
71

72
    return handle_get_top(ec, {}, version, media);
10 ✔
73
}
74

75
void protocol_native::handle_get_top_chain(const code& ec,
353 ✔
76
    interface::top_chain, uint8_t, uint8_t media, bool candidate) NOEXCEPT
77
{
78
    if (stopped(ec))
353 ✔
79
        return;
80

81
    switch (media)
5 ✔
82
    {
83
        case data:
1 ✔
84
            send_chunk(to_top(candidate));
1 ✔
85
            return;
1 ✔
86
        case text:
1 ✔
87
            send_text(encode_base16(to_top(candidate)));
1 ✔
88
            return;
1 ✔
89
        case json:
3 ✔
90
            send_json(to_top_json(candidate));
3 ✔
91
            return;
3 ✔
92
    }
93

94
    send_not_found();
×
95
}
96

97
void protocol_native::handle_get_top_height(const code& ec,
353 ✔
98
    interface::top_height, uint8_t, uint8_t media, bool candidate) NOEXCEPT
99
{
100
    if (stopped(ec))
353 ✔
101
        return;
102

103
    const auto& query = archive();
5 ✔
104
    const auto height = possible_narrow_cast<uint32_t>(candidate ?
5 ✔
105
        query.get_top_candidate() : query.get_top_confirmed());
5 ✔
106

107
    switch (media)
5 ✔
108
    {
109
        case data:
1 ✔
110
            send_chunk(to_chunk(to_little_endian(height)));
1 ✔
111
            return;
1 ✔
112
        case text:
1 ✔
113
            send_text(encode_base16(to_little_endian(height)));
1 ✔
114
            return;
1 ✔
115
        case json:
3 ✔
116
            send_json(height);
3 ✔
117
            return;
3 ✔
118
    }
119

120
    send_not_found();
×
121
}
122

123
// notify
124
void protocol_native::do_top(media_type media) NOEXCEPT
6 ✔
125
{
126
    BC_ASSERT(stranded());
6 ✔
127

128
    switch (to_value(media))
6 ✔
129
    {
130
        case data:
4 ✔
131
            notify_chunk(to_tops());
4 ✔
132
            return;
4 ✔
133
        case text:
1 ✔
134
            notify_text(encode_base16(to_tops()));
1 ✔
135
            return;
1 ✔
136
        case json:
1 ✔
137
            notify_json(to_tops_json());
1 ✔
138
            return;
1 ✔
139
    }
140
}
141

142
void protocol_native::handle_get_block(const code& ec, interface::block,
368 ✔
143
    uint8_t, uint8_t media, std::optional<hash_cptr> hash,
144
    std::optional<uint32_t> height, bool witness, bool candidate) NOEXCEPT
145
{
146
    if (stopped(ec))
368 ✔
147
        return;
368 ✔
148

149
    const auto& query = archive();
20 ✔
150
    const auto link = to_header(height, hash, candidate);
20 ✔
151
    if (link.is_terminal() || query.is_pruned(link))
20 ✔
152
    {
153
        send_not_found();
8 ✔
154
        return;
155
    }
156

157
    size_t size{};
12 ✔
158
    if (!query.get_block_size(size, link, witness))
12 ✔
159
    {
160
        send_internal_server_error(database::error::integrity);
1 ✔
161
        return;
1 ✔
162
    }
163

164
    switch (media)
11 ✔
165
    {
166
        case data:
7 ✔
167
        {
168
            send_stream(size, [&query, link, witness, turbo = turbo_](
7 ✔
169
                const data_slab& out) NOEXCEPT
170
            {
171
                return query.get_wire_block(out, link, witness, turbo);
7 ✔
172
            }, media_type::application_octet_stream);
173
            return;
7 ✔
174
        }
175
        case text:
1 ✔
176
        {
177
            send_stream(two * size, [&query, link, witness, turbo = turbo_](
1 ✔
178
                const data_slab& out) NOEXCEPT
179
            {
180
                return query.get_wire_block<write::base16::fast>(out, link,
1 ✔
181
                    witness, turbo);
1 ✔
182
            }, media_type::text_plain);
183
            return;
1 ✔
184
        }
185
        case json:
3 ✔
186
        {
187
            const auto block = query.get_block(link, witness);
3 ✔
188
            if (!block)
3 ✔
189
            {
190
                send_internal_server_error(database::error::integrity);
×
NEW
191
                return;
×
192
            }
193

194
            auto model = value_from(block);
3 ✔
195
            inject(model.at("header"), height, link);
3 ✔
196
            send_json(std::move(model));
3 ✔
197
            return;
198
        }
3 ✔
199
    }
200

201
    send_not_found();
×
202
}
20 ✔
203

204
void protocol_native::handle_get_block_header(const code& ec,
357 ✔
205
    interface::block_header, uint8_t, uint8_t media,
206
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
207
    bool candidate) NOEXCEPT
208
{
209
    if (stopped(ec))
357 ✔
210
        return;
354 ✔
211

212
    const auto link = to_header(height, hash, candidate);
9 ✔
213
    if (const auto header = archive().get_header(link))
9 ✔
214
    {
215
        constexpr auto size = chain::header::serialized_size();
216
        switch (media)
6 ✔
217
        {
218
            case data:
2 ✔
219
                send_chunk(to_bin(*header, size));
2 ✔
220
                return;
2 ✔
221
            case text:
1 ✔
222
                send_text(to_hex(*header, size));
1 ✔
223
                return;
1 ✔
224
            case json:
3 ✔
225
                auto model = value_from(header);
3 ✔
226
                inject(model, height, link);
3 ✔
227
                send_json(std::move(model));
3 ✔
228
                return;
229
        }
3 ✔
230
    }
9 ✔
231

232
    send_not_found();
3 ✔
233
}
9 ✔
234

235
void protocol_native::handle_get_block_header_context(const code& ec,
353 ✔
236
    interface::block_header_context, uint8_t, uint8_t media,
237
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
238
    bool candidate) NOEXCEPT
239
{
240
    if (stopped(ec))
353 ✔
241
        return;
351 ✔
242

243
    if (media != json)
5 ✔
244
    {
245
        send_not_acceptable();
1 ✔
246
        return;
1 ✔
247
    }
248

249
    database::context context{};
4 ✔
250
    const auto& query = archive();
4 ✔
251
    const auto link = to_header(height, hash, candidate);
4 ✔
252
    if (!query.get_context(context, link))
4 ✔
253
    {
254
        send_not_found();
2 ✔
255
        return;
256
    }
257

258
    system::json::object object
259
    {
260
        { "hash", encode_hash(query.get_header_key(link)) },
4 ✔
261
        { "height", context.height },
262
        { "mtp", context.mtp },
263
    };
2 ✔
264

265
    // The "state" element implies transactions are associated.
266
    if (query.is_associated(link))
2 ✔
267
    {
268
        size_t nominal{}, maximal{};
2 ✔
269
        if (!query.get_block_sizes(nominal, maximal, link))
2 ✔
270
        {
271
            send_internal_server_error(database::error::integrity);
×
NEW
272
            return;
×
273
        }
274

275
        const auto bypass = query.is_bypass(link);
2 ✔
276

277
        object["state"] = system::json::object
6 ✔
278
        {
279
            { "size", nominal },
280
            { "weight", chain::weighted_size(nominal, maximal) },
2 ✔
281
            { "count", query.get_tx_count(link) },
2 ✔
282
            { "validated", bypass || query.is_validated(link) },
2 ✔
283
            { "confirmed", query.is_confirmed_block(link) },
2 ✔
284
            { "confirmable", bypass || query.is_confirmable(link) },
2 ✔
285
            { "unconfirmable", !bypass && query.is_unconfirmable(link) }
4 ✔
286
        };
2 ✔
287
    }
288

289
    // TODO: move to system serializer.
290
    // All modern configurable forks.
291
    object["forks"] = system::json::object
4 ✔
292
    {
293
        { "bip16",  context.is_enabled(chain::flags::bip16_rule) },
2 ✔
294
        { "bip30",  context.is_enabled(chain::flags::bip30_rule) },
2 ✔
295
        { "bip34",  context.is_enabled(chain::flags::bip34_rule) },
2 ✔
296
        { "bip66",  context.is_enabled(chain::flags::bip66_rule) },
2 ✔
297
        { "bip65",  context.is_enabled(chain::flags::bip65_rule) },
2 ✔
298
        { "bip68",  context.is_enabled(chain::flags::bip68_rule) },
2 ✔
299
        { "bip112", context.is_enabled(chain::flags::bip112_rule) },
2 ✔
300
        { "bip113", context.is_enabled(chain::flags::bip113_rule) },
2 ✔
301
        { "bip141", context.is_enabled(chain::flags::bip141_rule) },
2 ✔
302
        { "bip143", context.is_enabled(chain::flags::bip143_rule) },
2 ✔
303
        { "bip147", context.is_enabled(chain::flags::bip147_rule) },
2 ✔
304
        { "bip42",  context.is_enabled(chain::flags::bip42_rule) },
2 ✔
305
        { "bip341", context.is_enabled(chain::flags::bip341_rule) },
2 ✔
306
        { "bip342", context.is_enabled(chain::flags::bip342_rule) },
2 ✔
307
        { "bip54_timewarp", context.is_enabled(chain::flags::bip54_timewarp_rule) },
2 ✔
308
        { "bip54_timespan", context.is_enabled(chain::flags::bip54_timespan_rule) },
2 ✔
309
        { "bip54_sigops", context.is_enabled(chain::flags::bip54_sigops_rule) },
2 ✔
310
        { "bip54_txsize", context.is_enabled(chain::flags::bip54_txsize_rule) },
2 ✔
311
        { "bip54_coinbase", context.is_enabled(chain::flags::bip54_coinbase_rule) }
2 ✔
312
    };
2 ✔
313

314
    send_json(std::move(object));
2 ✔
315
}
4 ✔
316

317
void protocol_native::handle_get_block_details(const code& ec,
355 ✔
318
    interface::block_details, uint8_t, uint8_t media,
319
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
320
    bool candidate) NOEXCEPT
321
{
322
    if (stopped(ec))
355 ✔
323
        return;
352 ✔
324

325
    if (media != json)
7 ✔
326
    {
327
        send_not_acceptable();
1 ✔
328
        return;
1 ✔
329
    }
330

331
    const auto& query = archive();
6 ✔
332
    const auto link = to_header(height, hash, candidate);
6 ✔
333

334
    // Missing block.
335
    if (!query.is_associated(link))
6 ✔
336
    {
337
        send_not_found();
2 ✔
338
        return;
339
    }
340

341
    const auto count = query.get_tx_count(link);
4 ✔
342
    const auto key = hash.has_value() ? *hash.value() :
4 ✔
343
        query.get_header_key(link);
4 ✔
344

345
    database::context context{};
4 ✔
346
    size_t nominal{}, maximal{};
4 ✔
347
    uint64_t value{}, spend{}, claim{};
4 ✔
348
    if (is_zero(count) || key == system::null_hash ||
8 ✔
349
        !query.get_tx_spend(claim, query.to_coinbase(link)) ||
12 ✔
350
        !query.get_block_sizes(nominal, maximal, link) ||
8 ✔
351
        !query.get_block_value(value, link) ||
7 ✔
352
        !query.get_block_spend(spend, link) ||
6 ✔
353
        !query.get_context(context, link) ||
7 ✔
354
        is_subtract_overflow(value, spend))
3 ✔
355
    {
356
        send_internal_server_error(database::error::integrity);
1 ✔
357
        return;
1 ✔
358
    }
359

360
    const auto fees = floored_subtract(value, spend);
3 ✔
361
    const auto& settings = system_settings();
3 ✔
362
    const auto bip42 = context.is_enabled(chain::flags::bip42_rule);
3 ✔
363
    const auto subsidy = chain::block::subsidy(context.height,
6 ✔
364
        settings.subsidy_interval_blocks, settings.initial_subsidy(), bip42);
3 ✔
365

366
    // sigops is not cached, so removed for now.
367
    system::json::object object
368
    {
369
        { "hash", encode_hash(key) },
6 ✔
370
        { "height", context.height },
371
        { "count", count },
372
        { "segregated", maximal != nominal },
6 ✔
373
        { "nominal", nominal },
374
        { "maximal", maximal },
375
        { "weight", chain::weighted_size(nominal, maximal) },
6 ✔
376
        { "virtual", chain::virtual_size(nominal, maximal) },
6 ✔
377
        { "value", value },
378
        { "fees", fees },
379
        { "subsidy", subsidy },
380
        { "reward", ceilinged_add(fees, subsidy) },
3 ✔
381
        { "claim", claim }
382
    };
6 ✔
383

384
    send_json(std::move(object));
3 ✔
385
}
6 ✔
386

387
void protocol_native::handle_get_block_txs(const code& ec,
354 ✔
388
    interface::block_txs, uint8_t, uint8_t media,
389
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
390
    bool candidate) NOEXCEPT
391
{
392
    if (stopped(ec))
354 ✔
393
        return;
352 ✔
394

395
    const auto& query = archive();
6 ✔
396
    const auto link = to_header(height, hash, candidate);
6 ✔
397
    if (const auto hashes = query.get_tx_keys(link); !hashes.empty())
6 ✔
398
    {
399
        const auto size = hashes.size() * hash_size;
4 ✔
400
        switch (media)
4 ✔
401
        {
402
            case data:
2 ✔
403
            {
404
                const auto bytes = pointer_cast<const uint8_t>(hashes.data());
2 ✔
405
                send_chunk(to_chunk({ bytes, std::next(bytes, size) }));
2 ✔
406
                return;
2 ✔
407
            }
408
            case text:
1 ✔
409
            {
410
                const auto bytes = pointer_cast<const uint8_t>(hashes.data());
1 ✔
411
                send_text(encode_base16({ bytes, std::next(bytes, size) }));
1 ✔
412
                return;
1 ✔
413
            }
414
            case json:
1 ✔
415
            {
416
                system::json::array out(hashes.size());
1 ✔
417
                std::ranges::transform(hashes, out.begin(),
1 ✔
418
                    [](const auto& hash) { return encode_hash(hash); });
1 ✔
419
                send_json(out);
1 ✔
420
                return;
421
            }
1 ✔
422
        }
423
    }
6 ✔
424

425
    send_not_found();
2 ✔
426
}
6 ✔
427

428
void protocol_native::handle_get_block_txs_details(const code& ec,
357 ✔
429
    interface::block_txs_details, uint8_t, uint8_t media,
430
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
431
    bool candidate) NOEXCEPT
432
{
433
    if (stopped(ec))
357 ✔
434
        return;
352 ✔
435

436
    if (media != json)
9 ✔
437
    {
438
        send_not_acceptable();
1 ✔
439
        return;
1 ✔
440
    }
441

442
    const auto& query = archive();
8 ✔
443
    const auto link = to_header(height, hash, candidate);
8 ✔
444

445
    // Missing block.
446
    if (!query.is_associated(link))
8 ✔
447
    {
448
        send_not_found();
2 ✔
449
        return;
450
    }
451

452
    system::json::array out{};
6 ✔
453
    const auto txs = query.to_transactions(link);
6 ✔
454
    if (txs.empty() || !to_tx_details(out, txs, to_roots(txs)))
11 ✔
455
    {
456
        send_internal_server_error(database::error::integrity);
1 ✔
457
        return;
458
    }
459

460
    send_json(std::move(out));
5 ✔
461
}
8 ✔
462

463
void protocol_native::handle_get_block_filter(const code& ec,
360 ✔
464
    interface::block_filter, uint8_t, uint8_t media, uint8_t type,
465
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
466
    bool candidate) NOEXCEPT
467
{
468
    if (stopped(ec))
360 ✔
469
        return;
358 ✔
470

471
    const auto& query = archive();
12 ✔
472
    if (!query.filter_enabled() || type != client_filter::type_id::neutrino)
12 ✔
473
    {
474
        send_not_implemented();
1 ✔
475
        return;
1 ✔
476
    }
477

478
    data_chunk filter{};
11 ✔
479
    if (query.get_filter_body(filter, to_header(height, hash, candidate)))
11 ✔
480
    {
481
        switch (media)
9 ✔
482
        {
483
            case data:
7 ✔
484
                send_chunk(std::move(filter));
7 ✔
485
                return;
486
            case text:
1 ✔
487
                send_text(encode_base16(filter));
1 ✔
488
                return;
1 ✔
489
            case json:
1 ✔
490
                send_json(value_from(encode_base16(filter)));
1 ✔
491
                return;
1 ✔
492
        }
493
    }
494

495
    send_not_found();
2 ✔
496
}
11 ✔
497

498
void protocol_native::handle_get_block_filter_hash(const code& ec,
355 ✔
499
    interface::block_filter_hash, uint8_t, uint8_t media, uint8_t type,
500
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
501
    bool candidate) NOEXCEPT
502
{
503
    if (stopped(ec))
355 ✔
504
        return;
353 ✔
505

506
    const auto& query = archive();
7 ✔
507
    if (!query.filter_enabled() || type != client_filter::type_id::neutrino)
7 ✔
508
    {
509
        send_not_implemented();
1 ✔
510
        return;
1 ✔
511
    }
512

513
    hash_digest filter_hash{ hash_size };
6 ✔
514
    if (query.get_filter_hash(filter_hash, to_header(height, hash, candidate)))
6 ✔
515
    {
516
        switch (media)
4 ✔
517
        {
518
            case data:
2 ✔
519
                send_chunk(to_chunk(filter_hash));
2 ✔
520
                return;
2 ✔
521
            case text:
1 ✔
522
                send_text(encode_base16(filter_hash));
1 ✔
523
                return;
1 ✔
524
            case json:
1 ✔
525
                send_json(value_from(encode_hash(filter_hash)));
1 ✔
526
                return;
1 ✔
527
        }
528
    }
529

530
    send_not_found();
2 ✔
531
}
532

533
void protocol_native::handle_get_block_filter_header(const code& ec,
354 ✔
534
    interface::block_filter_header, uint8_t, uint8_t media, uint8_t type,
535
    std::optional<hash_cptr> hash, std::optional<uint32_t> height,
536
    bool candidate) NOEXCEPT
537
{
538
    if (stopped(ec))
354 ✔
539
        return;
352 ✔
540

541
    const auto& query = archive();
6 ✔
542
    if (!query.filter_enabled() || type != client_filter::type_id::neutrino)
6 ✔
543
    {
544
        send_not_implemented();
1 ✔
545
        return;
1 ✔
546
    }
547

548
    hash_digest filter_head{ hash_size };
5 ✔
549
    if (query.get_filter_head(filter_head, to_header(height, hash, candidate)))
5 ✔
550
    {
551
        switch (media)
3 ✔
552
        {
553
            case data:
1 ✔
554
                send_chunk(to_chunk(filter_head));
1 ✔
555
                return;
1 ✔
556
            case text:
1 ✔
557
                send_text(encode_base16(filter_head));
1 ✔
558
                return;
1 ✔
559
            case json:
1 ✔
560
                send_json(value_from(encode_hash(filter_head)));
1 ✔
561
                return;
1 ✔
562
        }
563
    }
564

565
    send_not_found();
2 ✔
566
}
567

568
void protocol_native::handle_get_block_tx(const code& ec, interface::block_tx,
356 ✔
569
    uint8_t, uint8_t media, uint32_t position, std::optional<hash_cptr> hash,
570
    std::optional<uint32_t> height, bool witness, bool candidate) NOEXCEPT
571
{
572
    if (stopped(ec))
356 ✔
573
        return;
354 ✔
574

575
    const auto& query = archive();
8 ✔
576
    const auto link = query.to_transaction(to_header(height, hash, candidate),
8 ✔
577
        position);
8 ✔
578
    if (query.is_pruned(link))
8 ✔
579
    {
580
        send_not_found();
1 ✔
581
        return;
582
    }
583

584
    if (const auto tx = query.get_transaction(link, witness))
7 ✔
585
    {
586
        const auto size = tx->serialized_size(witness);
5 ✔
587
        switch (media)
5 ✔
588
        {
589
            case data:
3 ✔
590
                send_chunk(to_bin(*tx, size, witness));
3 ✔
591
                return;
3 ✔
592
            case text:
1 ✔
593
                send_text(to_hex(*tx, size, witness));
1 ✔
594
                return;
1 ✔
595
            case json:
1 ✔
596
                send_json(value_from(tx));
1 ✔
597
                return;
1 ✔
598
        }
599
    }
7 ✔
600

601
    send_not_found();
2 ✔
602
}
8 ✔
603

604
void protocol_native::handle_get_block_tx_input_script(const code& ec,
356 ✔
605
    interface::block_tx_input_script, uint8_t, uint8_t media,
606
    uint32_t position, uint32_t index, std::optional<hash_cptr> hash,
607
    std::optional<uint32_t> height, bool candidate) NOEXCEPT
608
{
609
    if (stopped(ec))
356 ✔
610
        return;
353 ✔
611

612
    const auto& query = archive();
8 ✔
613
    const auto link = query.to_transaction(to_header(height, hash, candidate),
8 ✔
614
        position);
8 ✔
615
    if (query.is_pruned(link))
8 ✔
616
    {
617
        send_not_found();
1 ✔
618
        return;
619
    }
620

621
    if (const auto script = query.get_input_script(query.to_point(link, index)))
7 ✔
622
    {
623
        const auto size = script->serialized_size(false);
4 ✔
624
        switch (media)
4 ✔
625
        {
626
            case data:
2 ✔
627
                send_chunk(to_bin(*script, size, false));
2 ✔
628
                return;
2 ✔
629
            case text:
1 ✔
630
                send_text(to_hex(*script, size, false));
1 ✔
631
                return;
1 ✔
632
            case json:
1 ✔
633
                send_json(value_from(script));
1 ✔
634
                return;
1 ✔
635
        }
636
    }
7 ✔
637

638
    send_not_found();
3 ✔
639
}
8 ✔
640

641
void protocol_native::handle_get_block_tx_output_script(const code& ec,
353 ✔
642
    interface::block_tx_output_script, uint8_t, uint8_t media,
643
    uint32_t position, uint32_t index, std::optional<hash_cptr> hash,
644
    std::optional<uint32_t> height, bool candidate) NOEXCEPT
645
{
646
    if (stopped(ec))
353 ✔
647
        return;
351 ✔
648

649
    const auto& query = archive();
5 ✔
650
    const auto tx = query.to_transaction(to_header(height, hash, candidate),
5 ✔
651
        position);
5 ✔
652

653
    if (const auto script = query.get_output_script(query.to_output(tx, index)))
5 ✔
654
    {
655
        const auto size = script->serialized_size(false);
3 ✔
656
        switch (media)
3 ✔
657
        {
658
            case data:
1 ✔
659
                send_chunk(to_bin(*script, size, false));
1 ✔
660
                return;
1 ✔
661
            case text:
1 ✔
662
                send_text(to_hex(*script, size, false));
1 ✔
663
                return;
1 ✔
664
            case json:
1 ✔
665
                send_json(value_from(script));
1 ✔
666
                return;
1 ✔
667
        }
668
    }
5 ✔
669

670
    send_not_found();
2 ✔
671
}
5 ✔
672

673
void protocol_native::handle_get_block_subscribe(const code& ec,
355 ✔
674
    interface::block_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
675
{
676
    if (stopped(ec))
355 ✔
677
        return;
355 ✔
678

679
    const auto value = stop ? media_type::unknown : (media_type)media;
7 ✔
680
    block_subscribe_.store(value, std::memory_order_relaxed);
7 ✔
681
    if (stop)
7 ✔
682
    {
683
        send_empty();
1 ✔
684
        return;
1 ✔
685
    }
686

687
    // Return top block hash upon block subscription.
688
    const auto& query = archive();
6 ✔
689
    const auto link = query.to_top_confirmed();
6 ✔
690
    if (const auto hash = query.get_header_key(link); hash != null_hash)
6 ✔
691
    {
692
        switch (media)
6 ✔
693
        {
694
            case data:
2 ✔
695
                send_chunk(to_chunk(hash));
2 ✔
696
                return;
2 ✔
697
            case text:
2 ✔
698
                send_text(encode_base16(hash));
2 ✔
699
                return;
2 ✔
700
            case json:
2 ✔
701
                send_json(value_from(encode_hash(hash)));
2 ✔
702
                return;
2 ✔
703
        }
704
    }
705

706
    send_not_found();
×
707
}
6 ✔
708

709
// notify
710
void protocol_native::do_block(node::header_t link, media_type media) NOEXCEPT
3 ✔
711
{
712
    BC_ASSERT(stranded());
3 ✔
713

714
    const auto hash = archive().get_header_key(link);
3 ✔
715
    switch (to_value(media))
3 ✔
716
    {
717
        case data:
1 ✔
718
            notify_chunk(to_chunk(hash));
1 ✔
719
            return;
3 ✔
720
        case text:
1 ✔
721
            notify_text(encode_base16(hash));
1 ✔
722
            return;
1 ✔
723
        case json:
1 ✔
724
            notify_json(value_from(encode_hash(hash)));
1 ✔
725
            return;
1 ✔
726
    }
727
}
728

729
BC_POP_WARNING()
730
BC_POP_WARNING()
731

732
} // namespace server
733
} // 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