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

libbitcoin / libbitcoin-server / 46042397839

27 Nov 2026 05:30PM UTC coverage: 93.797% (-0.2%) from 93.971%
46042397839

push

github

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

Mine stratum v2 standard channels in the web miner.

41 of 44 new or added lines in 2 files covered. (93.18%)

134 existing lines in 12 files now uncovered.

15394 of 16412 relevant lines covered (93.8%)

453.14 hits per line

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

91.37
/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
// Fee estimates, by mode and confirmation target in blocks.
36
using mode_t = node::estimate_mode;
37
constexpr std::array<std::pair<mode_t, std::string_view>, 4> estimate_modes
38
{
39
    std::pair{ mode_t::economical, "economical" },
40
    std::pair{ mode_t::conservative, "conservative" },
41
    std::pair{ mode_t::geometric, "geometric" },
42
    std::pair{ mode_t::basic, "basic" }
43
};
44
constexpr std::array<size_t, 5> estimate_targets{ 1, 3, 6, 12, 144 };
45
constexpr auto estimate_count = estimate_modes.size() * estimate_targets.size();
46

47
BC_PUSH_WARNING(NO_INCOMPLETE_SWITCH)
48
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
49

50
void protocol_native::handle_get_tx(const code& ec, interface::tx, uint8_t,
394 ✔
51
    uint8_t media, const hash_cptr& hash, bool witness) NOEXCEPT
52
{
53
    if (stopped(ec))
394 ✔
54
        return;
393 ✔
55

56
    const auto& query = archive();
9 ✔
57
    const auto link = query.to_tx(*hash);
9 ✔
58
    if (query.is_pruned(link))
9 ✔
59
    {
60
        send_not_found();
2 ✔
61
        return;
62
    }
63

64
    if (const auto tx = query.get_transaction(link, witness))
7 ✔
65
    {
66
        const auto size = tx->serialized_size(witness);
6 ✔
67
        switch (media)
6 ✔
68
        {
69
            case data:
4 ✔
70
                send_chunk(to_bin(*tx, size, witness));
4 ✔
71
                return;
4 ✔
72
            case text:
1 ✔
73
                send_text(to_hex(*tx, size, witness));
1 ✔
74
                return;
1 ✔
75
            case json:
1 ✔
76
                send_json(value_from(tx));
1 ✔
77
                return;
1 ✔
78
        }
79
    }
7 ✔
80

81
    send_not_found();
1 ✔
82
}
9 ✔
83

84
void protocol_native::handle_get_tx_header(const code& ec,
391 ✔
85
    interface::tx_header, uint8_t, uint8_t media,
86
    const hash_cptr& hash) NOEXCEPT
87
{
88
    if (stopped(ec))
391 ✔
89
        return;
391 ✔
90

91
    const auto& query = archive();
6 ✔
92
    const auto link = query.find_confirmed_block(*hash);
6 ✔
93
    if (link.is_terminal())
6 ✔
94
    {
95
        send_not_found();
2 ✔
96
        return;
97
    }
98

99
    const auto header = query.get_header(link);
4 ✔
100
    if (!header)
4 ✔
101
    {
UNCOV
102
        send_internal_server_error(database::error::integrity);
×
UNCOV
103
        return;
×
104
    }
105

106
    constexpr auto size = chain::header::serialized_size();
107
    switch (media)
4 ✔
108
    {
109
        case data:
2 ✔
110
            send_chunk(to_bin(*header, size));
2 ✔
111
            return;
2 ✔
112
        case text:
1 ✔
113
            send_text(to_hex(*header, size));
1 ✔
114
            return;
1 ✔
115
        case json:
1 ✔
116
            auto model = value_from(header);
1 ✔
117
            inject(model, {}, link);
1 ✔
118
            send_json(std::move(model));
1 ✔
119
            return;
120
    }
1 ✔
121

UNCOV
122
    send_not_found();
×
123
}
6 ✔
124

125
void protocol_native::handle_get_tx_details(const code& ec,
395 ✔
126
    interface::tx_details, uint8_t, uint8_t media,
127
    const hash_cptr& hash) NOEXCEPT
128
{
129
    if (stopped(ec))
395 ✔
130
        return;
389 ✔
131

132
    if (media != json)
10 ✔
133
    {
134
        send_not_acceptable();
1 ✔
135
        return;
1 ✔
136
    }
137

138
    const auto& query = archive();
9 ✔
139
    const auto link = query.to_tx(*hash);
9 ✔
140

141
    // Missing tx.
142
    if (link.is_terminal())
9 ✔
143
    {
144
        send_not_found();
1 ✔
145
        return;
146
    }
147

148
    uint64_t value{}, spend{};
8 ✔
149
    size_t nominal{}, maximal{};
8 ✔
150
    const auto coinbase = query.is_coinbase(link);
8 ✔
151
    if (!query.get_tx_sizes(nominal, maximal, link) ||
16 ✔
152
        !query.get_tx_value(value, link) ||
16 ✔
153
        !query.get_tx_spend(spend, link) ||
16 ✔
154
        (!coinbase && is_subtract_overflow(value, spend)))
8 ✔
155
    {
156
        send_internal_server_error(database::error::integrity);
1 ✔
157
        return;
1 ✔
158
    }
159

160
    // sigops and wtxid are not cached, so removed for now.
161
    system::json::object object
162
    {
163
        { "segregated", maximal != nominal },
14 ✔
164
        { "coinbase", coinbase },
165
        { "nominal", nominal },
166
        { "maximal", maximal },
167
        { "weight", chain::weighted_size(nominal, maximal) },
14 ✔
168
        { "virtual", chain::virtual_size(nominal, maximal) },
14 ✔
169
        { "value", value },
170
        { "spend", spend },
171
        { "fee", floored_subtract(value, spend) }
14 ✔
172
    };
7 ✔
173

174
    const auto block = query.find_strong(link);
7 ✔
175
    if (!block.is_terminal() && query.is_confirmed_block(block))
7 ✔
176
    {
177
        size_t position{};
3 ✔
178
        database::context context{};
3 ✔
179
        if (!query.get_context(context, block) ||
6 ✔
180
            !query.get_tx_position(position, link, block))
3 ✔
181
        {
182
            send_internal_server_error(database::error::integrity);
1 ✔
183
            return;
1 ✔
184
        }
185

186
        object["confirmed"] = system::json::object
4 ✔
187
        {
188
            { "height", context.height },
189
            { "position", position }
190
        };
2 ✔
191
    }
192
    else
193
    {
194
        if (!coinbase && query.is_confirmable_tx(link))
4 ✔
195
            object["confirmable"] = true;
1 ✔
196

197
        if (uint32_t time{}; query.get_pooled_time(time, link))
4 ✔
198
            object["time"] = time;
1 ✔
199
    }
200

201
    send_json(std::move(object));
6 ✔
202
}
9 ✔
203

204
void protocol_native::handle_get_tx_subscribe(const code& ec,
389 ✔
205
    interface::tx_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
206
{
207
    if (stopped(ec))
389 ✔
208
        return;
209

210
    tx_subscribe_.store(to_media(stop, media), std::memory_order_relaxed);
4 ✔
211
    notify_empty();
4 ✔
212
}
213

214
// notify
215
void protocol_native::do_transaction(node::transaction_t link,
3 ✔
216
    media_type media) NOEXCEPT
217
{
218
    BC_ASSERT(stranded());
3 ✔
219

220
    const auto hash = archive().get_tx_key(link);
3 ✔
221
    switch (to_value(media))
3 ✔
222
    {
223
        case data:
1 ✔
224
            notify_chunk(to_chunk(hash));
1 ✔
225
            return;
3 ✔
226
        case text:
1 ✔
227
            notify_text(encode_base16(hash));
1 ✔
228
            return;
1 ✔
229
        case json:
1 ✔
230
            notify_json(value_from(encode_hash(hash)));
1 ✔
231
            return;
1 ✔
232
    }
233
}
234

235
void protocol_native::handle_get_template_subscribe(const code& ec,
389 ✔
236
    interface::template_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
237
{
238
    if (stopped(ec))
389 ✔
239
        return;
240

241
    // Templates are built for notification only while demanded.
242
    const auto prior = template_subscribe_.exchange(to_media(stop, media));
4 ✔
243
    if (prior == media_type::unknown && !stop)
4 ✔
244
        demand_template(true);
3 ✔
245
    else if (prior != media_type::unknown && stop)
1 ✔
UNCOV
246
        demand_template(false);
×
247

248
    notify_empty();
4 ✔
249
}
250

251
// notify
252
void protocol_native::do_template(node::height_t height,
3 ✔
253
    media_type media) NOEXCEPT
254
{
255
    BC_ASSERT(stranded());
3 ✔
256

257
    const auto value = possible_narrow_cast<uint32_t>(height);
3 ✔
258
    switch (to_value(media))
3 ✔
259
    {
260
        case data:
1 ✔
261
            notify_chunk(to_chunk(to_little_endian(value)));
1 ✔
262
            return;
1 ✔
263
        case text:
1 ✔
264
            notify_text(encode_base16(to_little_endian(value)));
1 ✔
265
            return;
1 ✔
266
        case json:
1 ✔
267
            notify_json(value);
1 ✔
268
            return;
1 ✔
269
    }
270
}
271

272
void protocol_native::handle_get_template(const code& ec,
392 ✔
273
    interface::template_, uint8_t, uint8_t media, uint32_t height) NOEXCEPT
274
{
275
    if (stopped(ec))
392 ✔
276
        return;
277

278
    if (media != json)
7 ✔
279
    {
280
        send_not_acceptable();
1 ✔
281
        return;
1 ✔
282
    }
283

284
    get_template_view(height, template_view::summary, media);
6 ✔
285
}
286

287
void protocol_native::handle_get_template_txs(const code& ec,
390 ✔
288
    interface::template_txs, uint8_t, uint8_t media, uint32_t height) NOEXCEPT
289
{
290
    if (stopped(ec))
390 ✔
291
        return;
292

293
    get_template_view(height, template_view::txs, media);
5 ✔
294
}
295

296
void protocol_native::handle_get_template_txs_details(const code& ec,
390 ✔
297
    interface::template_txs_details, uint8_t, uint8_t media,
298
    uint32_t height) NOEXCEPT
299
{
300
    if (stopped(ec))
390 ✔
301
        return;
302

303
    get_template_view(height, template_view::details, media);
5 ✔
304
}
305

306
// private
307
// Only the template of the next block is held.
308
void protocol_native::get_template_view(uint32_t height, template_view view,
16 ✔
309
    uint8_t media) NOEXCEPT
310
{
311
    if (add1(archive().get_top_confirmed()) != height)
16 ✔
312
    {
313
        send_not_found();
3 ✔
314
        return;
3 ✔
315
    }
316

317
    get_template(BIND(handle_template, _1, _2, height, view, media, gate()));
13 ✔
318
}
319

320
void protocol_native::handle_template(const code& ec,
13 ✔
321
    const node::block_template::cptr& held, uint32_t height,
322
    template_view view, uint8_t media, const gate_t::ptr& gate) NOEXCEPT
323
{
324
    POST(complete_get_template, ec, held, height, view, media, gate);
13 ✔
325
}
13 ✔
326

327
void protocol_native::complete_get_template(const code& ec,
13 ✔
328
    const node::block_template::cptr& held, uint32_t height,
329
    template_view view, uint8_t media, const gate_t::ptr& gate) NOEXCEPT
330
{
331
    BC_ASSERT(stranded());
13 ✔
332

333
    if (stopped())
13 ✔
334
        return;
335

336
    if (ec == node::error::pooling_disabled)
13 ✔
337
    {
338
        if (network_settings().enable_relay)
2 ✔
339
            send_service_unavailable();
1 ✔
340
        else
341
            send_not_implemented();
1 ✔
342

343
        return;
2 ✔
344
    }
345

346
    if (ec)
11 ✔
347
    {
UNCOV
348
        send_internal_server_error(ec);
×
UNCOV
349
        return;
×
350
    }
351

352
    // A template held for another height is of a chain that has since moved.
353
    if (held->context.height != height)
11 ✔
354
    {
UNCOV
355
        if (add1(archive().get_top_confirmed()) == height)
×
UNCOV
356
            get_template(BIND(handle_template, _1, _2, height, view, media,
×
357
                gate));
358
        else
UNCOV
359
            send_not_found();
×
360

UNCOV
361
        return;
×
362
    }
363

364
    switch (view)
11 ✔
365
    {
366
        case template_view::summary:
2 ✔
367
            send_template_summary(*held);
2 ✔
368
            return;
2 ✔
369
        case template_view::txs:
4 ✔
370
            send_template_txs(*held, media);
4 ✔
371
            return;
4 ✔
372
        case template_view::details:
5 ✔
373
            send_template_txs_details(*held, media);
5 ✔
374
            return;
5 ✔
375
    }
376
}
377

378
void protocol_native::send_template_summary(
2 ✔
379
    const node::block_template& held) NOEXCEPT
380
{
381
    BC_ASSERT(stranded());
2 ✔
382
    const auto& query = archive();
2 ✔
383
    size_t nominal{}, maximal{};
384
    for (const auto& link: held.links)
4 ✔
385
    {
386
        size_t light{}, heavy{};
2 ✔
387
        if (!query.get_tx_sizes(light, heavy, link))
2 ✔
388
        {
UNCOV
389
            send_internal_server_error(database::error::integrity);
×
UNCOV
390
            return;
×
391
        }
392

393
        nominal = ceilinged_add(nominal, light);
2 ✔
394
        maximal = ceilinged_add(maximal, heavy);
2 ✔
395
    }
396

397
    auto roots = to_roots(held.txs);
2 ✔
398
    std::vector<bool> labeled(roots.size(), false);
2 ✔
399
    size_t clusters{};
2 ✔
400
    for (size_t index{}; index < roots.size(); ++index)
4 ✔
401
    {
402
        const auto root = to_root(roots, index);
2 ✔
403
        if (!labeled.at(root))
2 ✔
404
        {
405
            labeled.at(root) = true;
1 ✔
406
            ++clusters;
1 ✔
407
        }
408
    }
409

410
    send_json(system::json::object
2 ✔
411
    {
412
        { "height", held.context.height },
2 ✔
413
        { "count", held.links.size() },
2 ✔
414
        { "virtual", chain::virtual_size(nominal, maximal) },
2 ✔
415
        { "weight", chain::weighted_size(nominal, maximal) },
2 ✔
416
        { "fees", held.fees },
2 ✔
417
        { "clusters", clusters }
418
    });
419
}
2 ✔
420

421
void protocol_native::send_template_txs(const node::block_template& held,
4 ✔
422
    uint8_t media) NOEXCEPT
423
{
424
    BC_ASSERT(stranded());
4 ✔
425

426
    // Position zero is the coinbase, which is not of the template.
427
    hashes txids{ null_hash };
4 ✔
428
    txids.reserve(add1(held.txs.size()));
4 ✔
429
    for (const auto& tx: held.txs)
8 ✔
430
        txids.push_back(tx.txid);
4 ✔
431

432
    const auto size = txids.size() * hash_size;
4 ✔
433
    const auto bytes = pointer_cast<const uint8_t>(txids.data());
4 ✔
434
    switch (media)
4 ✔
435
    {
436
        case data:
1 ✔
437
            send_chunk(to_chunk({ bytes, std::next(bytes, size) }));
1 ✔
438
            return;
1 ✔
439
        case text:
1 ✔
440
            send_text(encode_base16({ bytes, std::next(bytes, size) }));
1 ✔
441
            return;
1 ✔
442
        case json:
2 ✔
443
        {
444
            system::json::array out(txids.size());
2 ✔
445
            std::ranges::transform(txids, out.begin(),
2 ✔
446
                [](const auto& hash) { return encode_hash(hash); });
4 ✔
447
            send_json(std::move(out));
2 ✔
448
            return;
449
        }
2 ✔
450
    }
451

UNCOV
452
    send_not_acceptable();
×
453
}
4 ✔
454

455
void protocol_native::send_template_txs_details(
5 ✔
456
    const node::block_template& held, uint8_t media) NOEXCEPT
457
{
458
    BC_ASSERT(stranded());
5 ✔
459
    tx_details txs{};
5 ✔
460
    if (!to_tx_details(txs, held.links, to_roots(held.txs)))
5 ✔
461
    {
UNCOV
462
        send_internal_server_error(database::error::integrity);
×
463
        return;
464
    }
465

466
    const auto weight = size_t{ node_settings().pool.coinbase_weight };
5 ✔
467
    const auto base = floored_subtract(weight, coinbase_witness_size) /
5 ✔
468
        chain::light_weight_factor;
5 ✔
469
    const auto maximal = base + coinbase_witness_size;
5 ✔
470
    const auto nominal = floored_subtract(maximal,
5 ✔
471
        coinbase_witness_size + coinbase_commitment_size);
472
    const auto bip42 = held.context.is_enabled(chain::flags::bip42_rule);
5 ✔
473
    const auto reward = ceilinged_add(to_subsidy(held.context.height, bip42),
10 ✔
474
        held.fees);
5 ✔
475

476
    tx_details out{};
5 ✔
477
    out.reserve(add1(txs.size()));
5 ✔
478
    out.push_back(tx_detail
10 ✔
479
    {
480
        possible_narrow_cast<uint32_t>(nominal),
5 ✔
481
        possible_narrow_cast<uint32_t>(maximal),
5 ✔
482
        reward,
483
        reward,
484
        0u,
485
        0u,
486
        0u
487
    });
488

489
    for (auto& tx: txs)
8 ✔
490
    {
491
        ++tx.cluster;
3 ✔
492
        out.push_back(tx);
3 ✔
493
    }
494

495
    send_tx_details(out, media);
5 ✔
496
}
5 ✔
497

498
void protocol_native::handle_get_txs(const code& ec, interface::txs,
386 ✔
499
    uint8_t, uint8_t) NOEXCEPT
500
{
501
    if (stopped(ec))
386 ✔
502
        return;
503

504
    send_not_implemented();
1 ✔
505
}
506

507
void protocol_native::handle_get_txs_details(const code& ec,
386 ✔
508
    interface::txs_details, uint8_t, uint8_t) NOEXCEPT
509
{
510
    if (stopped(ec))
386 ✔
511
        return;
512

513
    send_not_implemented();
1 ✔
514
}
515

516
void protocol_native::handle_get_txs_confirmed(const code& ec,
386 ✔
517
    interface::txs_confirmed, uint8_t, uint8_t) NOEXCEPT
518
{
519
    if (stopped(ec))
386 ✔
520
        return;
521

522
    send_not_implemented();
1 ✔
523
}
524

525
void protocol_native::handle_get_txs_confirmed_details(const code& ec,
386 ✔
526
    interface::txs_confirmed_details, uint8_t, uint8_t) NOEXCEPT
527
{
528
    if (stopped(ec))
386 ✔
529
        return;
530

531
    send_not_implemented();
1 ✔
532
}
533

534
void protocol_native::handle_get_txs_unconfirmed(const code& ec,
389 ✔
535
    interface::txs_unconfirmed, uint8_t, uint8_t media) NOEXCEPT
536
{
537
    if (stopped(ec))
389 ✔
538
        return;
539

540
    get_txs_unconfirmed(false, media);
4 ✔
541
}
542

543
void protocol_native::handle_get_txs_unconfirmed_details(const code& ec,
389 ✔
544
    interface::txs_unconfirmed_details, uint8_t, uint8_t media) NOEXCEPT
545
{
546
    if (stopped(ec))
389 ✔
547
        return;
548

549
    get_txs_unconfirmed(true, media);
4 ✔
550
}
551

552
void protocol_native::get_txs_unconfirmed(bool details,
8 ✔
553
    uint8_t media) NOEXCEPT
554
{
555
    get_confirmable_txs(BIND(handle_txs_unconfirmed, _1, _2, details, media,
8 ✔
556
        gate()));
557
}
8 ✔
558

559
void protocol_native::handle_txs_unconfirmed(const code& ec,
8 ✔
560
    const database::tx_links& links, bool details, uint8_t media,
561
    const gate_t::ptr& gate) NOEXCEPT
562
{
563
    POST(complete_get_txs_unconfirmed, ec, links, details, media, gate);
8 ✔
564
}
8 ✔
565

566
void protocol_native::complete_get_txs_unconfirmed(const code& ec,
8 ✔
567
    const database::tx_links& links, bool details, uint8_t media,
568
    const gate_t::ptr&) NOEXCEPT
569
{
570
    BC_ASSERT(stranded());
8 ✔
571

572
    if (stopped())
8 ✔
573
        return;
8 ✔
574

575
    if (ec == node::error::pooling_disabled)
8 ✔
576
    {
577
        if (network_settings().enable_relay)
2 ✔
UNCOV
578
            send_service_unavailable();
×
579
        else
580
            send_not_implemented();
2 ✔
581

582
        return;
2 ✔
583
    }
584

585
    if (ec)
6 ✔
586
    {
UNCOV
587
        send_internal_server_error(ec);
×
UNCOV
588
        return;
×
589
    }
590

591
    if (details)
6 ✔
592
    {
593
        tx_details out{};
3 ✔
594
        if (!to_tx_details(out, links, to_roots(links)))
3 ✔
595
        {
UNCOV
596
            send_internal_server_error(database::error::integrity);
×
UNCOV
597
            return;
×
598
        }
599

600
        const auto& query = archive();
3 ✔
601
        for (size_t index{}; index < links.size(); ++index)
6 ✔
602
            query.get_pooled_time(out.at(index).time, links.at(index));
3 ✔
603

604
        send_tx_details(out, media, true);
3 ✔
605
        return;
606
    }
3 ✔
607

608
    const auto& query = archive();
3 ✔
609
    hashes txids(links.size());
3 ✔
610
    std::ranges::transform(links, txids.begin(),
3 ✔
611
        [&](const auto& link) NOEXCEPT { return query.get_tx_key(link); });
3 ✔
612

613
    const auto size = txids.size() * hash_size;
3 ✔
614
    const auto bytes = pointer_cast<const uint8_t>(txids.data());
3 ✔
615
    switch (media)
3 ✔
616
    {
617
        case data:
1 ✔
618
            send_chunk(to_chunk({ bytes, std::next(bytes, size) }));
1 ✔
619
            return;
1 ✔
UNCOV
620
        case text:
×
UNCOV
621
            send_text(encode_base16({ bytes, std::next(bytes, size) }));
×
UNCOV
622
            return;
×
623
        case json:
2 ✔
624
        {
625
            system::json::array out(txids.size());
2 ✔
626
            std::ranges::transform(txids, out.begin(),
2 ✔
627
                [](const auto& hash) { return encode_hash(hash); });
2 ✔
628
            send_json(std::move(out));
2 ✔
629
            return;
630
        }
2 ✔
631
    }
632

UNCOV
633
    send_not_acceptable();
×
634
}
3 ✔
635

636
void protocol_native::handle_get_fees(const code& ec, interface::fees,
392 ✔
637
    uint8_t, uint8_t media) NOEXCEPT
638
{
639
    if (stopped(ec))
392 ✔
640
        return;
641

642
    if (media != json)
7 ✔
643
    {
644
        send_not_acceptable();
2 ✔
645
        return;
2 ✔
646
    }
647

648
    get_confirmable_fees(BIND(handle_fees, _1, _2, gate()));
5 ✔
649
}
650

651
void protocol_native::handle_fees(const code& ec,
5 ✔
652
    const database::fee_rates& rates, const gate_t::ptr& gate) NOEXCEPT
653
{
654
    POST(complete_get_fees, ec, rates, gate);
5 ✔
655
}
5 ✔
656

657
void protocol_native::complete_get_fees(const code& ec,
5 ✔
658
    const database::fee_rates& rates, const gate_t::ptr& gate) NOEXCEPT
659
{
660
    BC_ASSERT(stranded());
5 ✔
661

662
    if (stopped())
5 ✔
663
        return;
3 ✔
664

665
    // Closed pool is not implemented with relay disabled, and is otherwise
666
    // unavailable until it opens.
667
    if (ec == node::error::pooling_disabled)
5 ✔
668
    {
669
        if (network_settings().enable_relay)
3 ✔
670
            send_service_unavailable();
2 ✔
671
        else
672
            send_not_implemented();
1 ✔
673

674
        return;
3 ✔
675
    }
676

677
    if (ec)
2 ✔
678
    {
UNCOV
679
        send_internal_server_error(ec);
×
UNCOV
680
        return;
×
681
    }
682

683
    system::json::object estimates{};
2 ✔
684
    for (const auto& mode: estimate_modes)
10 ✔
685
        estimates[mode.second] = system::json::object{};
8 ✔
686

687
    const auto summary = to_fee_summary(rates, histogram_bin);
2 ✔
688
    const auto fees = emplace_shared<system::json::object>(system::json::object
8 ✔
689
    {
690
        { "count", summary.count },
691
        { "virtual", summary.vsize },
692
        { "fee", summary.fee },
693
        { "histogram", system::json::value_from(summary.histogram) },
4 ✔
694
        { "estimates", std::move(estimates) }
2 ✔
695
    });
2 ✔
696

697
    estimate_fees(zero, fees, gate);
2 ✔
698
}
2 ✔
699

700
// Estimates are made in turn, so that each continues one pool projection.
701
void protocol_native::estimate_fees(size_t index, const fees_ptr& fees,
42 ✔
702
    const gate_t::ptr& gate) NOEXCEPT
703
{
704
    BC_ASSERT(stranded());
42 ✔
705

706
    if (index == estimate_count)
42 ✔
707
    {
708
        send_json(std::move(*fees));
2 ✔
709
        return;
2 ✔
710
    }
711

712
    // The estimator targets the next block as zero.
713
    const auto mode = estimate_modes.at(index / estimate_targets.size()).first;
40 ✔
714
    const auto target = estimate_targets.at(index % estimate_targets.size());
40 ✔
715
    estimate(sub1(target), mode,
40 ✔
716
        BIND(handle_estimate_fees, _1, _2, index, fees, gate));
80 ✔
717
}
718

719
void protocol_native::handle_estimate_fees(const code& ec, uint64_t rate,
40 ✔
720
    size_t index, const fees_ptr& fees, const gate_t::ptr& gate) NOEXCEPT
721
{
722
    POST(complete_estimate_fees, ec, rate, index, fees, gate);
40 ✔
723
}
40 ✔
724

725
// Rates are sat per virtual kilobyte, published as sat per virtual byte.
726
void protocol_native::complete_estimate_fees(const code& ec, uint64_t rate,
40 ✔
727
    size_t index, const fees_ptr& fees, const gate_t::ptr& gate) NOEXCEPT
728
{
729
    BC_ASSERT(stranded());
40 ✔
730

731
    if (stopped())
40 ✔
UNCOV
732
        return;
×
733

734
    if (ec == node::error::estimate_disabled ||
40 ✔
735
        ec == node::error::estimate_premature)
40 ✔
736
    {
UNCOV
737
        (*fees)["estimates"] = nullptr;
×
UNCOV
738
        send_json(std::move(*fees));
×
UNCOV
739
        return;
×
740
    }
741

742
    if (ec && ec != node::error::estimate_false)
40 ✔
743
    {
UNCOV
744
        send_internal_server_error(ec);
×
UNCOV
745
        return;
×
746
    }
747

748
    const auto name = estimate_modes.at(index / estimate_targets.size()).second;
40 ✔
749
    const auto target = estimate_targets.at(index % estimate_targets.size());
40 ✔
750
    auto& estimates = (*fees)["estimates"].as_object()[name].as_object();
40 ✔
751
    if (ec)
40 ✔
UNCOV
752
        estimates[std::to_string(target)] = nullptr;
×
753
    else
754
        estimates[std::to_string(target)] = to_floating(rate) / 1'000.0;
40 ✔
755

756
    estimate_fees(add1(index), fees, gate);
40 ✔
757
}
758

759
BC_POP_WARNING()
760
BC_POP_WARNING()
761

762
} // namespace server
763
} // 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