• 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

93.51
/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,
357 ✔
39
    uint8_t media, const hash_cptr& hash, bool witness) NOEXCEPT
40
{
41
    if (stopped(ec))
357 ✔
42
        return;
356 ✔
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,
354 ✔
73
    interface::tx_header, uint8_t, uint8_t media,
74
    const hash_cptr& hash) NOEXCEPT
75
{
76
    if (stopped(ec))
354 ✔
77
        return;
354 ✔
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);
×
NEW
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,
354 ✔
114
    interface::tx_details, uint8_t, uint8_t media,
115
    const hash_cptr& hash) NOEXCEPT
116
{
117
    if (stopped(ec))
354 ✔
118
        return;
352 ✔
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,
352 ✔
184
    interface::tx_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
185
{
186
    if (stopped(ec))
352 ✔
187
        return;
188

189
    const auto value = stop ? media_type::unknown : (media_type)media;
4 ✔
190
    tx_subscribe_.store(value, std::memory_order_relaxed);
4 ✔
191
    notify_empty();
4 ✔
192
}
193

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

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

215
void protocol_native::handle_get_template(const code& ec,
355 ✔
216
    interface::template_, uint8_t, uint8_t media, uint32_t height) NOEXCEPT
217
{
218
    if (stopped(ec))
355 ✔
219
        return;
220

221
    if (media != json)
7 ✔
222
    {
223
        send_not_acceptable();
1 ✔
224
        return;
1 ✔
225
    }
226

227
    get_template_view(height, template_view::summary, media);
6 ✔
228
}
229

230
void protocol_native::handle_get_template_txs(const code& ec,
353 ✔
231
    interface::template_txs, uint8_t, uint8_t media, uint32_t height) NOEXCEPT
232
{
233
    if (stopped(ec))
353 ✔
234
        return;
235

236
    get_template_view(height, template_view::txs, media);
5 ✔
237
}
238

239
void protocol_native::handle_get_template_txs_details(const code& ec,
352 ✔
240
    interface::template_txs_details, uint8_t, uint8_t media,
241
    uint32_t height) NOEXCEPT
242
{
243
    if (stopped(ec))
352 ✔
244
        return;
245

246
    if (media != json)
4 ✔
247
    {
248
        send_not_acceptable();
1 ✔
249
        return;
1 ✔
250
    }
251

252
    get_template_view(height, template_view::details, media);
3 ✔
253
}
254

255
// private
256
// Only the template of the next block is held.
257
void protocol_native::get_template_view(uint32_t height, template_view view,
14 ✔
258
    uint8_t media) NOEXCEPT
259
{
260
    if (add1(archive().get_top_confirmed()) != height)
14 ✔
261
    {
262
        send_not_found();
3 ✔
263
        return;
3 ✔
264
    }
265

266
    get_template(BIND(handle_template, _1, _2, height, view, media, gate()));
11 ✔
267
}
268

269
void protocol_native::handle_template(const code& ec,
11 ✔
270
    const node::block_template::cptr& held, uint32_t height,
271
    template_view view, uint8_t media, const gate_t::ptr& gate) NOEXCEPT
272
{
273
    POST(complete_get_template, ec, held, height, view, media, gate);
11 ✔
274
}
11 ✔
275

276
void protocol_native::complete_get_template(const code& ec,
11 ✔
277
    const node::block_template::cptr& held, uint32_t height,
278
    template_view view, uint8_t media, const gate_t::ptr& gate) NOEXCEPT
279
{
280
    BC_ASSERT(stranded());
11 ✔
281

282
    if (stopped())
11 ✔
283
        return;
284

285
    if (ec == node::error::pooling_disabled)
11 ✔
286
    {
287
        if (network_settings().enable_relay)
2 ✔
288
            send_service_unavailable();
1 ✔
289
        else
290
            send_not_implemented();
1 ✔
291

292
        return;
2 ✔
293
    }
294

295
    if (ec)
9 ✔
296
    {
NEW
297
        send_internal_server_error(ec);
×
NEW
298
        return;
×
299
    }
300

301
    // A template held for another height is of a chain that has since moved.
302
    if (held->context.height != height)
9 ✔
303
    {
NEW
304
        if (add1(archive().get_top_confirmed()) == height)
×
NEW
305
            get_template(BIND(handle_template, _1, _2, height, view, media,
×
306
                gate));
307
        else
NEW
308
            send_not_found();
×
309

NEW
310
        return;
×
311
    }
312

313
    switch (view)
9 ✔
314
    {
315
        case template_view::summary:
2 ✔
316
            send_template_summary(*held);
2 ✔
317
            return;
2 ✔
318
        case template_view::txs:
4 ✔
319
            send_template_txs(*held, media);
4 ✔
320
            return;
4 ✔
321
        case template_view::details:
3 ✔
322
            send_template_txs_details(*held);
3 ✔
323
            return;
3 ✔
324
    }
325
}
326

327
void protocol_native::send_template_summary(
2 ✔
328
    const node::block_template& held) NOEXCEPT
329
{
330
    BC_ASSERT(stranded());
2 ✔
331
    const auto& query = archive();
2 ✔
332
    size_t nominal{}, maximal{};
333
    for (const auto& link: held.links)
4 ✔
334
    {
335
        size_t light{}, heavy{};
2 ✔
336
        if (!query.get_tx_sizes(light, heavy, link))
2 ✔
337
        {
NEW
338
            send_internal_server_error(database::error::integrity);
×
NEW
339
            return;
×
340
        }
341

342
        nominal = ceilinged_add(nominal, light);
2 ✔
343
        maximal = ceilinged_add(maximal, heavy);
2 ✔
344
    }
345

346
    auto roots = to_roots(held.txs);
2 ✔
347
    std::vector<bool> labeled(roots.size(), false);
2 ✔
348
    size_t clusters{};
2 ✔
349
    for (size_t index{}; index < roots.size(); ++index)
4 ✔
350
    {
351
        const auto root = to_root(roots, index);
2 ✔
352
        if (!labeled.at(root))
2 ✔
353
        {
354
            labeled.at(root) = true;
1 ✔
355
            ++clusters;
1 ✔
356
        }
357
    }
358

359
    send_json(system::json::object
2 ✔
360
    {
361
        { "height", held.context.height },
2 ✔
362
        { "count", held.links.size() },
2 ✔
363
        { "virtual", chain::virtual_size(nominal, maximal) },
2 ✔
364
        { "weight", chain::weighted_size(nominal, maximal) },
2 ✔
365
        { "fees", held.fees },
2 ✔
366
        { "clusters", clusters }
367
    });
368
}
2 ✔
369

370
void protocol_native::send_template_txs(const node::block_template& held,
4 ✔
371
    uint8_t media) NOEXCEPT
372
{
373
    BC_ASSERT(stranded());
4 ✔
374
    hashes txids{};
4 ✔
375
    txids.reserve(held.txs.size());
4 ✔
376
    for (const auto& tx: held.txs)
8 ✔
377
        txids.push_back(tx.txid);
4 ✔
378

379
    const auto size = txids.size() * hash_size;
4 ✔
380
    const auto bytes = pointer_cast<const uint8_t>(txids.data());
4 ✔
381
    switch (media)
4 ✔
382
    {
383
        case data:
1 ✔
384
            send_chunk(to_chunk({ bytes, std::next(bytes, size) }));
1 ✔
385
            return;
1 ✔
386
        case text:
1 ✔
387
            send_text(encode_base16({ bytes, std::next(bytes, size) }));
1 ✔
388
            return;
1 ✔
389
        case json:
2 ✔
390
        {
391
            system::json::array out(txids.size());
2 ✔
392
            std::ranges::transform(txids, out.begin(),
2 ✔
393
                [](const auto& hash) { return encode_hash(hash); });
2 ✔
394
            send_json(std::move(out));
2 ✔
395
            return;
396
        }
2 ✔
397
    }
398

NEW
399
    send_not_acceptable();
×
400
}
4 ✔
401

402
void protocol_native::send_template_txs_details(
3 ✔
403
    const node::block_template& held) NOEXCEPT
404
{
405
    BC_ASSERT(stranded());
3 ✔
406
    system::json::array out{};
3 ✔
407
    if (!to_tx_details(out, held.links, to_roots(held.txs)))
3 ✔
408
    {
NEW
409
        send_internal_server_error(database::error::integrity);
×
410
        return;
411
    }
412

413
    send_json(std::move(out));
3 ✔
414
}
3 ✔
415

416
void protocol_native::handle_get_fees(const code& ec, interface::fees,
355 ✔
417
    uint8_t, uint8_t media) NOEXCEPT
418
{
419
    if (stopped(ec))
355 ✔
420
        return;
421

422
    if (media != json)
7 ✔
423
    {
424
        send_not_acceptable();
2 ✔
425
        return;
2 ✔
426
    }
427

428
    get_confirmable_fees(BIND(handle_fees, _1, _2, gate()));
5 ✔
429
}
430

431
void protocol_native::handle_fees(const code& ec,
5 ✔
432
    const database::fee_rates& rates, const gate_t::ptr& gate) NOEXCEPT
433
{
434
    POST(complete_get_fees, ec, rates, gate);
5 ✔
435
}
5 ✔
436

437
void protocol_native::complete_get_fees(const code& ec,
5 ✔
438
    const database::fee_rates& rates, const gate_t::ptr&) NOEXCEPT
439
{
440
    BC_ASSERT(stranded());
5 ✔
441

442
    if (stopped())
5 ✔
443
        return;
3 ✔
444

445
    // Closed pool is not implemented with relay disabled, and is otherwise
446
    // unavailable until it opens.
447
    if (ec == node::error::pooling_disabled)
5 ✔
448
    {
449
        if (network_settings().enable_relay)
3 ✔
450
            send_service_unavailable();
2 ✔
451
        else
452
            send_not_implemented();
1 ✔
453

454
        return;
3 ✔
455
    }
456

457
    if (ec)
2 ✔
458
    {
459
        send_internal_server_error(ec);
×
460
        return;
×
461
    }
462

463
    const auto summary = to_fee_summary(rates, histogram_bin);
2 ✔
464
    system::json::object object
465
    {
466
        { "count", summary.count },
467
        { "virtual", summary.vsize },
468
        { "fee", summary.fee },
469
        { "histogram", system::json::value_from(summary.histogram) }
4 ✔
470
    };
4 ✔
471

472
    send_json(std::move(object));
2 ✔
473
}
2 ✔
474

475
BC_POP_WARNING()
476
BC_POP_WARNING()
477

478
} // namespace server
479
} // 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