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

libbitcoin / libbitcoin-server / 44691724904

20 Nov 2026 06:59AM UTC coverage: 95.15% (+0.05%) from 95.102%
44691724904

push

github

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

Fix test, disable admin channel inactivity and expiration by default.

2 of 2 new or added lines in 1 file covered. (100.0%)

122 existing lines in 14 files now uncovered.

12792 of 13444 relevant lines covered (95.15%)

445.71 hits per line

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

95.97
/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
bool protocol_native::handle_get_tx(const code& ec, interface::tx, uint8_t,
283 ✔
39
    uint8_t media, const hash_cptr& hash, bool witness) NOEXCEPT
40
{
41
    if (stopped(ec))
283 ✔
42
        return false;
43

44
    const auto& query = archive();
6 ✔
45
    if (const auto tx = query.get_transaction(query.to_tx(*hash), witness))
6 ✔
46
    {
47
        const auto size = tx->serialized_size(witness);
5 ✔
48
        switch (media)
5 ✔
49
        {
50
            case data:
3 ✔
51
                send_chunk(to_bin(*tx, size, witness));
3 ✔
52
                return true;
3 ✔
53
            case text:
1 ✔
54
                send_text(to_hex(*tx, size, witness));
1 ✔
55
                return true;
1 ✔
56
            case json:
1 ✔
57
                send_json(value_from(tx));
1 ✔
58
                return true;
1 ✔
59
        }
60
    }
6 ✔
61

62
    send_not_found();
1 ✔
63
    return true;
1 ✔
64
}
65

66
bool protocol_native::handle_get_tx_header(const code& ec,
282 ✔
67
    interface::tx_header, uint8_t, uint8_t media,
68
    const hash_cptr& hash) NOEXCEPT
69
{
70
    if (stopped(ec))
282 ✔
71
        return false;
72

73
    const auto& query = archive();
5 ✔
74
    const auto link = query.find_confirmed_block(*hash);
5 ✔
75
    if (link.is_terminal())
5 ✔
76
    {
77
        send_not_found();
2 ✔
78
        return true;
79
    }
80

81
    const auto header = query.get_header(link);
3 ✔
82
    if (!header)
3 ✔
83
    {
84
        send_internal_server_error(database::error::integrity);
×
85
        return true;
×
86
    }
87

88
    constexpr auto size = chain::header::serialized_size();
89
    switch (media)
3 ✔
90
    {
91
        case data:
1 ✔
92
            send_chunk(to_bin(*header, size));
1 ✔
93
            return true;
1 ✔
94
        case text:
1 ✔
95
            send_text(to_hex(*header, size));
1 ✔
96
            return true;
1 ✔
97
        case json:
1 ✔
98
            auto model = value_from(header);
1 ✔
99
            inject(model, {}, link);
1 ✔
100
            send_json(std::move(model));
1 ✔
101
            return true;
102
    }
1 ✔
103

104
    send_not_found();
×
105
    return true;
106
}
5 ✔
107

108
bool protocol_native::handle_get_tx_details(const code& ec,
283 ✔
109
    interface::tx_details, uint8_t, uint8_t media,
110
    const hash_cptr& hash) NOEXCEPT
111
{
112
    if (stopped(ec))
283 ✔
113
        return false;
114

115
    if (media != json)
6 ✔
116
    {
117
        send_not_acceptable();
1 ✔
118
        return true;
1 ✔
119
    }
120

121
    const auto& query = archive();
5 ✔
122
    const auto link = query.to_tx(*hash);
5 ✔
123

124
    // Missing tx.
125
    if (link.is_terminal())
5 ✔
126
    {
127
        send_not_found();
1 ✔
128
        return true;
129
    }
130

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

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

157
    if (const auto block = query.find_strong(link); !block.is_terminal())
3 ✔
158
    {
159
        size_t position{};
2 ✔
160
        database::context context{};
2 ✔
161
        if (!query.get_context(context, block) ||
4 ✔
162
            !query.get_tx_position(position, link, block))
2 ✔
163
        {
164
            send_internal_server_error(database::error::integrity);
1 ✔
165
            return true;
1 ✔
166
        }
167

168
        object["confirmed"] = system::json::object
2 ✔
169
        {
170
            { "height", context.height },
171
            { "position", position }
172
        };
1 ✔
173
    }
3 ✔
174

175
    send_json(std::move(object));
2 ✔
176
    return true;
2 ✔
177
}
5 ✔
178

179
bool protocol_native::handle_get_tx_subscribe(const code& ec,
281 ✔
180
    interface::tx_subscribe, uint8_t, uint8_t media, bool stop) NOEXCEPT
181
{
182
    if (stopped(ec))
281 ✔
183
        return false;
184

185
    const auto value = stop ? media_type::unknown : (media_type)media;
4 ✔
186
    tx_subscribe_.store(value, std::memory_order_relaxed);
4 ✔
187
    notify_empty();
4 ✔
188
    return true;
4 ✔
189
}
190

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

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

212
bool protocol_native::handle_get_fees(const code& ec, interface::fees,
284 ✔
213
    uint8_t, uint8_t media) NOEXCEPT
214
{
215
    if (stopped(ec))
284 ✔
216
        return false;
217

218
    if (media != json)
7 ✔
219
    {
220
        send_not_acceptable();
2 ✔
221
        return true;
2 ✔
222
    }
223

224
    get_confirmable_fees(BIND(handle_fees, _1, _2, gate()));
5 ✔
225
    return true;
5 ✔
226
}
227

228
void protocol_native::handle_fees(const code& ec,
5 ✔
229
    const database::fee_rates& rates, const gate_t::ptr& gate) NOEXCEPT
230
{
231
    POST(complete_get_fees, ec, rates, gate);
5 ✔
232
}
5 ✔
233

234
void protocol_native::complete_get_fees(const code& ec,
5 ✔
235
    const database::fee_rates& rates, const gate_t::ptr&) NOEXCEPT
236
{
237
    BC_ASSERT(stranded());
5 ✔
238

239
    if (stopped())
5 ✔
240
        return;
3 ✔
241

242
    // A closed pool is not implemented with relay disabled, and is
243
    // otherwise unavailable until it opens.
244
    if (ec == node::error::pooling_disabled)
5 ✔
245
    {
246
        if (network_settings().enable_relay)
3 ✔
247
            send_service_unavailable();
2 ✔
248
        else
249
            send_not_implemented();
1 ✔
250

251
        return;
3 ✔
252
    }
253

254
    if (ec)
2 ✔
255
    {
UNCOV
256
        send_internal_server_error(ec);
×
UNCOV
257
        return;
×
258
    }
259

260
    const auto summary = to_fee_summary(rates, histogram_bin);
2 ✔
261
    system::json::object object
262
    {
263
        { "count", summary.count },
264
        { "virtual", summary.vsize },
265
        { "fee", summary.fee },
266
        { "histogram", system::json::value_from(summary.histogram) }
4 ✔
267
    };
4 ✔
268

269
    send_json(std::move(object));
2 ✔
270
}
2 ✔
271

272
BC_POP_WARNING()
273
BC_POP_WARNING()
274

275
} // namespace server
276
} // 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