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

libbitcoin / libbitcoin-server / 44232621448

18 Nov 2026 03:45AM UTC coverage: 95.091% (-0.1%) from 95.201%
44232621448

push

github

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

Add tidal pool.

148 of 170 new or added lines in 4 files covered. (87.06%)

12592 of 13242 relevant lines covered (95.09%)

437.62 hits per line

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

96.32
/src/protocols/native/protocol_native.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 <bitcoin/server/define.hpp>
24
#include <bitcoin/server/parsers/parsers.hpp>
25

26
namespace libbitcoin {
27
namespace server {
28

29
using namespace system;
30
using namespace network;
31
using namespace std::placeholders;
32
constexpr auto relaxed = std::memory_order_relaxed;
33

34
#define CLASS protocol_native
35
#define SUBSCRIBE_NATIVE(method, ...) \
36
    subscribe<CLASS>(&CLASS::method, __VA_ARGS__)
37

38
// Start.
39
// ----------------------------------------------------------------------------
40

41
void protocol_native::start() NOEXCEPT
266 ✔
42
{
43
    BC_ASSERT(stranded());
266 ✔
44

45
    if (started())
266 ✔
46
        return;
47

48
    // Chaser subscription is asynchronous, events may be missed.
49
    subscribe_chase(BIND(handle_chase, _1, _2));
266 ✔
50

51
    // Configuration methods.
52
    SUBSCRIBE_NATIVE(handle_get_configuration, _1, _2, _3, _4);
266 ✔
53

54
    // Top methods.
55
    SUBSCRIBE_NATIVE(handle_get_top, _1, _2, _3, _4);
266 ✔
56
    SUBSCRIBE_NATIVE(handle_get_top_subscribe, _1, _2, _3, _4, _5);
266 ✔
57

58
    // Block methods.
59
    SUBSCRIBE_NATIVE(handle_get_block, _1, _2, _3, _4, _5, _6, _7, _8);
266 ✔
60
    SUBSCRIBE_NATIVE(handle_get_block_header, _1, _2, _3, _4, _5, _6, _7);
266 ✔
61
    SUBSCRIBE_NATIVE(handle_get_block_header_context, _1, _2, _3, _4, _5, _6, _7);
266 ✔
62
    SUBSCRIBE_NATIVE(handle_get_block_details, _1, _2, _3, _4, _5, _6, _7);
266 ✔
63
    SUBSCRIBE_NATIVE(handle_get_block_txs, _1, _2, _3, _4, _5, _6, _7);
266 ✔
64
    SUBSCRIBE_NATIVE(handle_get_block_filter, _1, _2, _3, _4, _5, _6, _7, _8);
266 ✔
65
    SUBSCRIBE_NATIVE(handle_get_block_filter_hash, _1, _2, _3, _4, _5, _6, _7, _8);
266 ✔
66
    SUBSCRIBE_NATIVE(handle_get_block_filter_header, _1, _2, _3, _4, _5, _6, _7, _8);
266 ✔
67
    SUBSCRIBE_NATIVE(handle_get_block_tx, _1, _2, _3, _4, _5, _6, _7, _8, _9);
266 ✔
68
    SUBSCRIBE_NATIVE(handle_get_block_subscribe, _1, _2, _3, _4, _5);
266 ✔
69

70
    // Transaction methods.
71
    SUBSCRIBE_NATIVE(handle_get_tx, _1, _2, _3, _4, _5, _6);
266 ✔
72
    SUBSCRIBE_NATIVE(handle_get_tx_header, _1, _2, _3, _4, _5);
266 ✔
73
    SUBSCRIBE_NATIVE(handle_get_tx_details, _1, _2, _3, _4, _5);
266 ✔
74
    SUBSCRIBE_NATIVE(handle_get_tx_subscribe, _1, _2, _3, _4, _5);
266 ✔
75

76
    // Input methods.
77
    SUBSCRIBE_NATIVE(handle_get_inputs, _1, _2, _3, _4, _5, _6);
266 ✔
78
    SUBSCRIBE_NATIVE(handle_get_input, _1, _2, _3, _4, _5, _6, _7);
266 ✔
79
    SUBSCRIBE_NATIVE(handle_get_input_script, _1, _2, _3, _4, _5, _6);
266 ✔
80
    SUBSCRIBE_NATIVE(handle_get_input_witness, _1, _2, _3, _4, _5, _6);
266 ✔
81

82
    // Output methods.
83
    SUBSCRIBE_NATIVE(handle_get_outputs, _1, _2, _3, _4, _5);
266 ✔
84
    SUBSCRIBE_NATIVE(handle_get_output, _1, _2, _3, _4, _5, _6);
266 ✔
85
    SUBSCRIBE_NATIVE(handle_get_output_script, _1, _2, _3, _4, _5, _6);
266 ✔
86
    SUBSCRIBE_NATIVE(handle_get_output_spender, _1, _2, _3, _4, _5, _6);
266 ✔
87
    SUBSCRIBE_NATIVE(handle_get_output_spenders, _1, _2, _3, _4, _5, _6);
266 ✔
88
    SUBSCRIBE_NATIVE(handle_get_output_subscribe, _1, _2, _3, _4, _5, _6, _7);
266 ✔
89

90
    // Address methods.
91
    SUBSCRIBE_NATIVE(handle_get_address, _1, _2, _3, _4, _5, _6);
266 ✔
92
    SUBSCRIBE_NATIVE(handle_get_address_confirmed, _1, _2, _3, _4, _5, _6);
266 ✔
93
    SUBSCRIBE_NATIVE(handle_get_address_unconfirmed, _1, _2, _3, _4, _5, _6);
266 ✔
94
    SUBSCRIBE_NATIVE(handle_get_address_balance, _1, _2, _3, _4, _5, _6);
266 ✔
95
    SUBSCRIBE_NATIVE(handle_get_address_subscribe, _1, _2, _3, _4, _5, _6, _7);
266 ✔
96

97
    // Fee methods.
98
    SUBSCRIBE_NATIVE(handle_get_fees, _1, _2, _3, _4);
266 ✔
99
    protocol_html::start();
266 ✔
100
}
101

102
void protocol_native::stopping(const code& ec) NOEXCEPT
266 ✔
103
{
104
    BC_ASSERT(stranded());
266 ✔
105
    stopping_.store(true);
266 ✔
106
    dispatcher_.stop(ec);
266 ✔
107
    unsubscribe_chase();
266 ✔
108
    protocol_html::stopping(ec);
266 ✔
109
}
266 ✔
110

111
// Dispatch.
112
// ----------------------------------------------------------------------------
113

114
bool protocol_native::try_dispatch_object(const http::request& request) NOEXCEPT
207 ✔
115
{
116
    BC_ASSERT(stranded());
207 ✔
117

118
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
119
    const auto target = request.target();
207 ✔
120
    BC_POP_WARNING()
121

122
    rpc::request_t model{};
207 ✔
123
    if (const auto ec = native_target(model, target))
207 ✔
124
    {
125
        // Allow invalid interface target to be retried as a page request.
126
        return !ec;
22 ✔
127
    }
128

129
    // No media defaults injected for an http request.
130
    if (!native_query(model, request))
185 ✔
131
    {
132
        send_bad_request();
5 ✔
133
        return true;
134
    }
135

136
    const auto media = get_media(model);
180 ✔
137
    if (media == media_type::unknown)
180 ✔
138
    {
139
        send_not_acceptable();
2 ✔
140
        return true;
141
    }
142

143
    // Falls through to html page dispatch.
144
    if (media == media_type::text_html)
178 ✔
145
        return false;
146

147
    // The credential may be restricted to a subset of interface methods.
148
    if (!permitted(model.method))
177 ✔
149
    {
150
        send_forbidden();
2 ✔
151
        return true;
152
    }
153

154
    if (dispatcher_.notify(model))
175 ✔
155
        send_bad_request();
2 ✔
156

157
    return true;
158
}
207 ✔
159

160
void protocol_native::dispatch_websocket(const http::request& request) NOEXCEPT
35 ✔
161
{
162
    BC_ASSERT(stranded());
35 ✔
163

164
    // ws frames carry no headers, so ws authorization is enforced here, not
165
    // by the channel (established by basic auth on the upgrade request).
166
    if (!authorized())
35 ✔
167
    {
168
        send_unauthorized(request);
×
169
        return;
4 ✔
170
    }
171

172
    // Native websocket interface supports only string requests.
173
    if (!request.body().contains<http::string_value>())
35 ✔
174
    {
175
        send_not_acceptable(request);
×
176
        return;
×
177
    }
178

179
    // Target with query string is passed via websocket string body.
180
    const auto target = request.body().get<http::string_value>();
35 ✔
181

182
    rpc::request_t model{};
35 ✔
183
    if (const auto ec = native_target(model, target))
35 ✔
184
    {
185
        send_bad_target(ec, request);
2 ✔
186
        return;
2 ✔
187
    }
188

189
    // Default to json by simulating a json accept header (format overrides).
190
    if (!native_query(model, target, { media_type::application_json }))
33 ✔
191
    {
192
        send_bad_request(request);
1 ✔
193
        return;
194
    }
195

196
    if (get_media(model) == media_type::text_html)
32 ✔
197
    {
198
        send_not_acceptable(request);
1 ✔
199
        return;
200
    }
201

202
    // The credential may be restricted to a subset of interface methods.
203
    if (!permitted(model.method))
31 ✔
204
    {
205
        send_forbidden(request);
×
206
        return;
207
    }
208

209
    if (dispatcher_.notify(model))
31 ✔
210
        send_bad_request(request);
1 ✔
211
}
35 ✔
212

213
// Senders.
214
// ----------------------------------------------------------------------------
215

216
void protocol_native::send_not_found(const http::request& request) NOEXCEPT
40 ✔
217
{
218
    BC_ASSERT(stranded());
40 ✔
219

220
    if (websocket())
40 ✔
221
    {
222
        send_empty();
2 ✔
223
        return;
2 ✔
224
    }
225

226
    protocol_html::send_not_found(request);
38 ✔
227
}
228

229
// Event handlers.
230
// ----------------------------------------------------------------------------
231

232
bool protocol_native::handle_chase(const code&,
278 ✔
233
    node::event_value value) NOEXCEPT
234
{
235
    // Do not pass ec to stopped as it is not a call status.
236
    if (stopped())
278 ✔
237
        return false;
238

239
    if (!websocket())
54 ✔
240
        return true;
241

242
    switch (node::to_chase(value))
21 ✔
243
    {
244
        case node::chase::block:
5 ✔
245
        {
246
            const auto block = node::to_payload<node::chase::block>(value);
5 ✔
247

248
            auto media = top_subscribe_.load(relaxed);
5 ✔
249
            if (media != media_type::unknown)
5 ✔
250
            {
251
                POST(do_top, media);
2 ✔
252
            }
253

254
            // No block emission for a fork point (start/reorg).
255
            media = block_subscribe_.load(relaxed);
5 ✔
256
            if (media != media_type::unknown)
5 ✔
257
            {
258
                POST(do_block, block.link, media);
3 ✔
259
            }
260

261
            break;
262
        }
263
        case node::chase::reorganized:
4 ✔
264
        case node::chase::headers:
265
        case node::chase::regressed:
266
        case node::chase::disorganized:
267
        {
268
            const auto media = top_subscribe_.load(relaxed);
4 ✔
269
            if (media != media_type::unknown)
4 ✔
270
            {
271
                POST(do_top, media);
4 ✔
272
            }
273

274
            break;
275
        }
276
        case node::chase::transaction:
3 ✔
277
        {
278
            const auto media = tx_subscribe_.load(relaxed);
3 ✔
279
            if (media != media_type::unknown)
3 ✔
280
            {
281
                POST(do_transaction,
3 ✔
282
                    node::to_payload<node::chase::transaction>(value).link,
283
                    media);
284
            }
285

286
            break;
287
        }
288
        default:
289
        {
290
            break;
291
        }
292
    }
293

294
    return true;
295
}
296

297
// Utilities.
298
// ----------------------------------------------------------------------------
299
// private
300

301
using namespace system::json;
302

303
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
304

305
void protocol_native::inject(value& out, std::optional<uint32_t> height,
7 ✔
306
    const database::header_link& link) const NOEXCEPT
307
{
308
    out.as_object().emplace("height", height.has_value() ? height.value() :
9 ✔
309
        archive().get_height(link).value);
9 ✔
310
}
7 ✔
311

312
chain::checkpoint protocol_native::get_top(bool candidate) const NOEXCEPT
74 ✔
313
{
314
    const auto& query = archive();
74 ✔
315
    const auto height = candidate ? query.get_top_candidate() :
74 ✔
316
        query.get_top_confirmed();
37 ✔
317
    const auto link = candidate ? query.to_candidate(height) :
74 ✔
318
        query.to_confirmed(height);
74 ✔
319
    return { query.get_header_key(link), height };
148 ✔
320
}
74 ✔
321

322
data_chunk protocol_native::to_tops() const NOEXCEPT
17 ✔
323
{
324
    return splice(get_top(false).to_data(), get_top(true).to_data());
17 ✔
325
}
326

327
value protocol_native::to_tops_json() const NOEXCEPT
20 ✔
328
{
329
    return
330
    {
331
        { "confirmed", value_from(get_top(false)) },
40 ✔
332
        { "candidate", value_from(get_top(true)) }
40 ✔
333
    };
100 ✔
334
}
335

336
BC_POP_WARNING()
337

338
database::header_link protocol_native::to_header(
67 ✔
339
    const std::optional<uint32_t>& height,
340
    const std::optional<hash_cptr>& hash, bool candidate) NOEXCEPT
341
{
342
    const auto& query = archive();
67 ✔
343

344
    if (hash.has_value())
67 ✔
345
        return query.to_header(*(hash.value()));
6 ✔
346

347
    if (!height.has_value())
61 ✔
NEW
348
        return {};
×
349

350
    return candidate ? query.to_candidate(height.value()) :
61 ✔
351
        query.to_confirmed(height.value());
61 ✔
352
}
353

354
// Use if deserialization is required.
355
#if defined(UNDEFINED)
356
using inpoints = database::inpoints;
357
using outpoints = database::outpoints;
358

359
DECLARE_JSON_TAG_INVOKE(inpoints);
360
DECLARE_JSON_TAG_INVOKE(outpoints);
361

362
DEFINE_JSON_TO_TAG(inpoints)
363
{
364
    inpoints out{};
365
    for (const auto& point: value.as_array())
366
        out.insert(value_to<inpoint>(point));
367

368
    return out;
369
}
370

371
DEFINE_JSON_TO_TAG(outpoints)
372
{
373
    outpoints out{};
374
    for (const auto& point: value.as_array())
375
        out.insert(value_to<outpoint>(point));
376

377
    return out;
378
}
379
#endif
380

381
} // namespace server
382
} // 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