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

libbitcoin / libbitcoin-server / 45422578410

24 Nov 2026 03:11PM UTC coverage: 93.89% (+0.02%) from 93.872%
45422578410

push

github

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

Add block/template views, tables/updates, notify templates,.

75 of 76 new or added lines in 5 files covered. (98.68%)

14276 of 15205 relevant lines covered (93.89%)

453.68 hits per line

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

94.1
/src/protocols/native/protocol_native_tx.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 <bitcoin/server/define.hpp>
23

24
namespace libbitcoin {
25
namespace server {
26

27
using namespace system;
28
using namespace std::placeholders;
29

30
#define CLASS protocol_native
31

32
// The vsize of a fee histogram bin (electrs).
33
constexpr size_t histogram_bin = 50'000;
34

35
BC_PUSH_WARNING(NO_INCOMPLETE_SWITCH)
36
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
37

38
void protocol_native::handle_get_tx(const code& ec, interface::tx, uint8_t,
361 ✔
39
    uint8_t media, const hash_cptr& hash, bool witness) NOEXCEPT
40
{
41
    if (stopped(ec))
361 ✔
42
        return;
360 ✔
43

44
    const auto& query = archive();
9 ✔
45
    const auto link = query.to_tx(*hash);
9 ✔
46
    if (query.is_pruned(link))
9 ✔
47
    {
48
        send_not_found();
2 ✔
49
        return;
50
    }
51

52
    if (const auto tx = query.get_transaction(link, witness))
7 ✔
53
    {
54
        const auto size = tx->serialized_size(witness);
6 ✔
55
        switch (media)
6 ✔
56
        {
57
            case data:
4 ✔
58
                send_chunk(to_bin(*tx, size, witness));
4 ✔
59
                return;
4 ✔
60
            case text:
1 ✔
61
                send_text(to_hex(*tx, size, witness));
1 ✔
62
                return;
1 ✔
63
            case json:
1 ✔
64
                send_json(value_from(tx));
1 ✔
65
                return;
1 ✔
66
        }
67
    }
7 ✔
68

69
    send_not_found();
1 ✔
70
}
9 ✔
71

72
void protocol_native::handle_get_tx_header(const code& ec,
358 ✔
73
    interface::tx_header, uint8_t, uint8_t media,
74
    const hash_cptr& hash) NOEXCEPT
75
{
76
    if (stopped(ec))
358 ✔
77
        return;
358 ✔
78

79
    const auto& query = archive();
6 ✔
80
    const auto link = query.find_confirmed_block(*hash);
6 ✔
81
    if (link.is_terminal())
6 ✔
82
    {
83
        send_not_found();
2 ✔
84
        return;
85
    }
86

87
    const auto header = query.get_header(link);
4 ✔
88
    if (!header)
4 ✔
89
    {
90
        send_internal_server_error(database::error::integrity);
×
91
        return;
×
92
    }
93

94
    constexpr auto size = chain::header::serialized_size();
95
    switch (media)
4 ✔
96
    {
97
        case data:
2 ✔
98
            send_chunk(to_bin(*header, size));
2 ✔
99
            return;
2 ✔
100
        case text:
1 ✔
101
            send_text(to_hex(*header, size));
1 ✔
102
            return;
1 ✔
103
        case json:
1 ✔
104
            auto model = value_from(header);
1 ✔
105
            inject(model, {}, link);
1 ✔
106
            send_json(std::move(model));
1 ✔
107
            return;
108
    }
1 ✔
109

110
    send_not_found();
×
111
}
6 ✔
112

113
void protocol_native::handle_get_tx_details(const code& ec,
358 ✔
114
    interface::tx_details, uint8_t, uint8_t media,
115
    const hash_cptr& hash) NOEXCEPT
116
{
117
    if (stopped(ec))
358 ✔
118
        return;
356 ✔
119

120
    if (media != json)
6 ✔
121
    {
122
        send_not_acceptable();
1 ✔
123
        return;
1 ✔
124
    }
125

126
    const auto& query = archive();
5 ✔
127
    const auto link = query.to_tx(*hash);
5 ✔
128

129
    // Missing tx.
130
    if (link.is_terminal())
5 ✔
131
    {
132
        send_not_found();
1 ✔
133
        return;
134
    }
135

136
    uint64_t value{}, spend{};
4 ✔
137
    size_t nominal{}, maximal{};
4 ✔
138
    const auto coinbase = query.is_coinbase(link);
4 ✔
139
    if (!query.get_tx_sizes(nominal, maximal, link) ||
8 ✔
140
        !query.get_tx_value(value, link) ||
8 ✔
141
        !query.get_tx_spend(spend, link) ||
8 ✔
142
        (!coinbase && is_subtract_overflow(value, spend)))
4 ✔
143
    {
144
        send_internal_server_error(database::error::integrity);
1 ✔
145
        return;
1 ✔
146
    }
147

148
    // sigops and wtxid are not cached, so removed for now.
149
    system::json::object object
150
    {
151
        { "segregated", maximal != nominal },
6 ✔
152
        { "coinbase", coinbase },
153
        { "nominal", nominal },
154
        { "maximal", maximal },
155
        { "weight", chain::weighted_size(nominal, maximal) },
6 ✔
156
        { "virtual", chain::virtual_size(nominal, maximal) },
6 ✔
157
        { "value", value },
158
        { "spend", spend },
159
        { "fee", floored_subtract(value, spend) }
6 ✔
160
    };
3 ✔
161

162
    if (const auto block = query.find_strong(link); !block.is_terminal())
3 ✔
163
    {
164
        size_t position{};
2 ✔
165
        database::context context{};
2 ✔
166
        if (!query.get_context(context, block) ||
4 ✔
167
            !query.get_tx_position(position, link, block))
2 ✔
168
        {
169
            send_internal_server_error(database::error::integrity);
1 ✔
170
            return;
1 ✔
171
        }
172

173
        object["confirmed"] = system::json::object
2 ✔
174
        {
175
            { "height", context.height },
176
            { "position", position }
177
        };
1 ✔
178
    }
3 ✔
179

180
    send_json(std::move(object));
2 ✔
181
}
5 ✔
182

183
void protocol_native::handle_get_tx_subscribe(const code& ec,
356 ✔
184
    interface::tx_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
185
{
186
    if (stopped(ec))
356 ✔
187
        return;
188

189
    tx_subscribe_.store(to_media(stop, media), std::memory_order_relaxed);
4 ✔
190
    notify_empty();
4 ✔
191
}
192

193
// notify
194
void protocol_native::do_transaction(node::transaction_t link,
3 ✔
195
    media_type media) NOEXCEPT
196
{
197
    BC_ASSERT(stranded());
3 ✔
198

199
    const auto hash = archive().get_tx_key(link);
3 ✔
200
    switch (to_value(media))
3 ✔
201
    {
202
        case data:
1 ✔
203
            notify_chunk(to_chunk(hash));
1 ✔
204
            return;
3 ✔
205
        case text:
1 ✔
206
            notify_text(encode_base16(hash));
1 ✔
207
            return;
1 ✔
208
        case json:
1 ✔
209
            notify_json(value_from(encode_hash(hash)));
1 ✔
210
            return;
1 ✔
211
    }
212
}
213

214
void protocol_native::handle_get_template_subscribe(const code& ec,
356 ✔
215
    interface::template_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
216
{
217
    if (stopped(ec))
356 ✔
218
        return;
219

220
    // Templates are built for notification only while demanded.
221
    const auto prior = template_subscribe_.exchange(to_media(stop, media));
4 ✔
222
    if (prior == media_type::unknown && !stop)
4 ✔
223
        demand_template(true);
3 ✔
224
    else if (prior != media_type::unknown && stop)
1 ✔
NEW
225
        demand_template(false);
×
226

227
    notify_empty();
4 ✔
228
}
229

230
// notify
231
void protocol_native::do_template(node::height_t height,
3 ✔
232
    media_type media) NOEXCEPT
233
{
234
    BC_ASSERT(stranded());
3 ✔
235

236
    const auto value = possible_narrow_cast<uint32_t>(height);
3 ✔
237
    switch (to_value(media))
3 ✔
238
    {
239
        case data:
1 ✔
240
            notify_chunk(to_chunk(to_little_endian(value)));
1 ✔
241
            return;
1 ✔
242
        case text:
1 ✔
243
            notify_text(encode_base16(to_little_endian(value)));
1 ✔
244
            return;
1 ✔
245
        case json:
1 ✔
246
            notify_json(value);
1 ✔
247
            return;
1 ✔
248
    }
249
}
250

251
void protocol_native::handle_get_template(const code& ec,
359 ✔
252
    interface::template_, uint8_t, uint8_t media, uint32_t height) NOEXCEPT
253
{
254
    if (stopped(ec))
359 ✔
255
        return;
256

257
    if (media != json)
7 ✔
258
    {
259
        send_not_acceptable();
1 ✔
260
        return;
1 ✔
261
    }
262

263
    get_template_view(height, template_view::summary, media);
6 ✔
264
}
265

266
void protocol_native::handle_get_template_txs(const code& ec,
357 ✔
267
    interface::template_txs, uint8_t, uint8_t media, uint32_t height) NOEXCEPT
268
{
269
    if (stopped(ec))
357 ✔
270
        return;
271

272
    get_template_view(height, template_view::txs, media);
5 ✔
273
}
274

275
void protocol_native::handle_get_template_txs_details(const code& ec,
356 ✔
276
    interface::template_txs_details, uint8_t, uint8_t media,
277
    uint32_t height) NOEXCEPT
278
{
279
    if (stopped(ec))
356 ✔
280
        return;
281

282
    if (media != json)
4 ✔
283
    {
284
        send_not_acceptable();
1 ✔
285
        return;
1 ✔
286
    }
287

288
    get_template_view(height, template_view::details, media);
3 ✔
289
}
290

291
// private
292
// Only the template of the next block is held.
293
void protocol_native::get_template_view(uint32_t height, template_view view,
14 ✔
294
    uint8_t media) NOEXCEPT
295
{
296
    if (add1(archive().get_top_confirmed()) != height)
14 ✔
297
    {
298
        send_not_found();
3 ✔
299
        return;
3 ✔
300
    }
301

302
    get_template(BIND(handle_template, _1, _2, height, view, media, gate()));
11 ✔
303
}
304

305
void protocol_native::handle_template(const code& ec,
11 ✔
306
    const node::block_template::cptr& held, uint32_t height,
307
    template_view view, uint8_t media, const gate_t::ptr& gate) NOEXCEPT
308
{
309
    POST(complete_get_template, ec, held, height, view, media, gate);
11 ✔
310
}
11 ✔
311

312
void protocol_native::complete_get_template(const code& ec,
11 ✔
313
    const node::block_template::cptr& held, uint32_t height,
314
    template_view view, uint8_t media, const gate_t::ptr& gate) NOEXCEPT
315
{
316
    BC_ASSERT(stranded());
11 ✔
317

318
    if (stopped())
11 ✔
319
        return;
320

321
    if (ec == node::error::pooling_disabled)
11 ✔
322
    {
323
        if (network_settings().enable_relay)
2 ✔
324
            send_service_unavailable();
1 ✔
325
        else
326
            send_not_implemented();
1 ✔
327

328
        return;
2 ✔
329
    }
330

331
    if (ec)
9 ✔
332
    {
333
        send_internal_server_error(ec);
×
334
        return;
×
335
    }
336

337
    // A template held for another height is of a chain that has since moved.
338
    if (held->context.height != height)
9 ✔
339
    {
340
        if (add1(archive().get_top_confirmed()) == height)
×
341
            get_template(BIND(handle_template, _1, _2, height, view, media,
×
342
                gate));
343
        else
344
            send_not_found();
×
345

346
        return;
×
347
    }
348

349
    switch (view)
9 ✔
350
    {
351
        case template_view::summary:
2 ✔
352
            send_template_summary(*held);
2 ✔
353
            return;
2 ✔
354
        case template_view::txs:
4 ✔
355
            send_template_txs(*held, media);
4 ✔
356
            return;
4 ✔
357
        case template_view::details:
3 ✔
358
            send_template_txs_details(*held);
3 ✔
359
            return;
3 ✔
360
    }
361
}
362

363
void protocol_native::send_template_summary(
2 ✔
364
    const node::block_template& held) NOEXCEPT
365
{
366
    BC_ASSERT(stranded());
2 ✔
367
    const auto& query = archive();
2 ✔
368
    size_t nominal{}, maximal{};
369
    for (const auto& link: held.links)
4 ✔
370
    {
371
        size_t light{}, heavy{};
2 ✔
372
        if (!query.get_tx_sizes(light, heavy, link))
2 ✔
373
        {
374
            send_internal_server_error(database::error::integrity);
×
375
            return;
×
376
        }
377

378
        nominal = ceilinged_add(nominal, light);
2 ✔
379
        maximal = ceilinged_add(maximal, heavy);
2 ✔
380
    }
381

382
    auto roots = to_roots(held.txs);
2 ✔
383
    std::vector<bool> labeled(roots.size(), false);
2 ✔
384
    size_t clusters{};
2 ✔
385
    for (size_t index{}; index < roots.size(); ++index)
4 ✔
386
    {
387
        const auto root = to_root(roots, index);
2 ✔
388
        if (!labeled.at(root))
2 ✔
389
        {
390
            labeled.at(root) = true;
1 ✔
391
            ++clusters;
1 ✔
392
        }
393
    }
394

395
    send_json(system::json::object
2 ✔
396
    {
397
        { "height", held.context.height },
2 ✔
398
        { "count", held.links.size() },
2 ✔
399
        { "virtual", chain::virtual_size(nominal, maximal) },
2 ✔
400
        { "weight", chain::weighted_size(nominal, maximal) },
2 ✔
401
        { "fees", held.fees },
2 ✔
402
        { "clusters", clusters }
403
    });
404
}
2 ✔
405

406
void protocol_native::send_template_txs(const node::block_template& held,
4 ✔
407
    uint8_t media) NOEXCEPT
408
{
409
    BC_ASSERT(stranded());
4 ✔
410

411
    // Position zero is the coinbase, which is not of the template.
412
    hashes txids{ null_hash };
4 ✔
413
    txids.reserve(add1(held.txs.size()));
4 ✔
414
    for (const auto& tx: held.txs)
8 ✔
415
        txids.push_back(tx.txid);
4 ✔
416

417
    const auto size = txids.size() * hash_size;
4 ✔
418
    const auto bytes = pointer_cast<const uint8_t>(txids.data());
4 ✔
419
    switch (media)
4 ✔
420
    {
421
        case data:
1 ✔
422
            send_chunk(to_chunk({ bytes, std::next(bytes, size) }));
1 ✔
423
            return;
1 ✔
424
        case text:
1 ✔
425
            send_text(encode_base16({ bytes, std::next(bytes, size) }));
1 ✔
426
            return;
1 ✔
427
        case json:
2 ✔
428
        {
429
            system::json::array out(txids.size());
2 ✔
430
            std::ranges::transform(txids, out.begin(),
2 ✔
431
                [](const auto& hash) { return encode_hash(hash); });
4 ✔
432
            send_json(std::move(out));
2 ✔
433
            return;
434
        }
2 ✔
435
    }
436

437
    send_not_acceptable();
×
438
}
4 ✔
439

440
void protocol_native::send_template_txs_details(
3 ✔
441
    const node::block_template& held) NOEXCEPT
442
{
443
    BC_ASSERT(stranded());
3 ✔
444
    system::json::array txs{};
3 ✔
445
    if (!to_tx_details(txs, held.links, to_roots(held.txs)))
3 ✔
446
    {
447
        send_internal_server_error(database::error::integrity);
×
448
        return;
449
    }
450

451
    const auto weight = size_t{ node_settings().pool.coinbase_weight };
3 ✔
452
    const auto base = floored_subtract(weight, coinbase_witness_size) /
3 ✔
453
        chain::light_weight_factor;
3 ✔
454
    const auto maximal = base + coinbase_witness_size;
3 ✔
455
    const auto nominal = floored_subtract(maximal,
3 ✔
456
        coinbase_witness_size + coinbase_commitment_size);
3 ✔
457
    const auto bip42 = held.context.is_enabled(chain::flags::bip42_rule);
3 ✔
458
    const auto reward = ceilinged_add(to_subsidy(held.context.height, bip42),
6 ✔
459
        held.fees);
3 ✔
460

461
    system::json::array out{};
3 ✔
462
    out.reserve(add1(txs.size()));
3 ✔
463
    out.emplace_back(system::json::object
9 ✔
464
    {
465
        { "segregated", maximal != nominal },
3 ✔
466
        { "nominal", nominal },
467
        { "maximal", maximal },
468
        { "weight", weight },
469
        { "virtual", ceilinged_divide(weight, chain::light_weight_factor) },
3 ✔
470
        { "value", reward },
471
        { "spend", reward },
472
        { "fee", 0 },
3 ✔
473
        { "cluster", 0 }
3 ✔
474
    });
475

476
    for (auto& tx: txs)
6 ✔
477
    {
478
        auto& object = tx.as_object();
3 ✔
479
        object["cluster"] = add1(object.at("cluster").to_number<size_t>());
3 ✔
480
        out.push_back(std::move(tx));
3 ✔
481
    }
482

483
    send_json(std::move(out));
3 ✔
484
}
3 ✔
485

486
void protocol_native::handle_get_fees(const code& ec, interface::fees,
359 ✔
487
    uint8_t, uint8_t media) NOEXCEPT
488
{
489
    if (stopped(ec))
359 ✔
490
        return;
491

492
    if (media != json)
7 ✔
493
    {
494
        send_not_acceptable();
2 ✔
495
        return;
2 ✔
496
    }
497

498
    get_confirmable_fees(BIND(handle_fees, _1, _2, gate()));
5 ✔
499
}
500

501
void protocol_native::handle_fees(const code& ec,
5 ✔
502
    const database::fee_rates& rates, const gate_t::ptr& gate) NOEXCEPT
503
{
504
    POST(complete_get_fees, ec, rates, gate);
5 ✔
505
}
5 ✔
506

507
void protocol_native::complete_get_fees(const code& ec,
5 ✔
508
    const database::fee_rates& rates, const gate_t::ptr&) NOEXCEPT
509
{
510
    BC_ASSERT(stranded());
5 ✔
511

512
    if (stopped())
5 ✔
513
        return;
3 ✔
514

515
    // Closed pool is not implemented with relay disabled, and is otherwise
516
    // unavailable until it opens.
517
    if (ec == node::error::pooling_disabled)
5 ✔
518
    {
519
        if (network_settings().enable_relay)
3 ✔
520
            send_service_unavailable();
2 ✔
521
        else
522
            send_not_implemented();
1 ✔
523

524
        return;
3 ✔
525
    }
526

527
    if (ec)
2 ✔
528
    {
529
        send_internal_server_error(ec);
×
530
        return;
×
531
    }
532

533
    const auto summary = to_fee_summary(rates, histogram_bin);
2 ✔
534
    system::json::object object
535
    {
536
        { "count", summary.count },
537
        { "virtual", summary.vsize },
538
        { "fee", summary.fee },
539
        { "histogram", system::json::value_from(summary.histogram) }
4 ✔
540
    };
4 ✔
541

542
    send_json(std::move(object));
2 ✔
543
}
2 ✔
544

545
BC_POP_WARNING()
546
BC_POP_WARNING()
547

548
} // namespace server
549
} // 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