• 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

96.17
/src/settings.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/settings.hpp>
20

21
#include <filesystem>
22
#include <bitcoin/server/define.hpp>
23
#include <bitcoin/server/utilities/utilities.hpp>
24

25
using namespace bc::system;
26
using namespace bc::network;
27

28
namespace libbitcoin {
29

30
// log
31
// ----------------------------------------------------------------------------
32

33
namespace log {
34

35
// Log states default to network compiled states or explicit false.
36
settings::settings() NOEXCEPT
2,121 ✔
37
  : application{ levels::application_defined },
2,121 ✔
38
    news{ levels::news_defined },
2,121 ✔
39
    session{ levels::session_defined },
2,121 ✔
40
    protocol{ false /*levels::protocol_defined*/ },
2,121 ✔
41
    proxy{ false /*levels::proxy_defined*/ },
2,121 ✔
42
    remote{ levels::remote_defined },
2,121 ✔
43
    fault{ levels::fault_defined },
2,121 ✔
44
    quitting{ false /*levels::quitting_defined*/ },
2,121 ✔
45
    objects{ false /*levels::objects_defined*/ },
2,121 ✔
46
    verbose{ false /*levels::verbose_defined*/ },
2,121 ✔
47
    console{ true },
2,121 ✔
48
    events{ true },
2,121 ✔
49
    thread_priority{ false },
2,121 ✔
50
    limit{ 1'000'000_u32 }
2,121 ✔
51
{
52
}
2,121 ✔
53

54
settings::settings(chain::selection) NOEXCEPT
2,120 ✔
55
  : settings()
2,120 ✔
56
{
57
}
2,120 ✔
58

59
processing_priority settings::thread_priority_() const NOEXCEPT
1 ✔
60
{
61
    // medium is "normal" (os default).
62
    return thread_priority ? processing_priority::medium :
1 ✔
63
        processing_priority::low;
1 ✔
64
}
65

66
std::filesystem::path settings::log_file1() const NOEXCEPT
2 ✔
67
{
68
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
69
    return console_path / "console_new.log";
2 ✔
70
    BC_POP_WARNING()
71
}
72

73
std::filesystem::path settings::log_file2() const NOEXCEPT
1 ✔
74
{
75
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
76
    return console_path / "console_old.log";
1 ✔
77
    BC_POP_WARNING()
78
}
79

80
std::filesystem::path settings::events_file() const NOEXCEPT
1 ✔
81
{
82
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
83
    return events_path / "events.log";
1 ✔
84
    BC_POP_WARNING()
85
}
86

87
} // namespace log
88

89
// server
90
// ----------------------------------------------------------------------------
91

92
namespace server {
93

94
using namespace system::wallet;
95

96
// settings::settings
97
settings::settings(system::chain::selection context,
2,150 ✔
98
    const embedded_pages& native_pages,
99
    const embedded_pages& admin_pages) NOEXCEPT
2,150 ✔
100
  : network(context),
2,150 ✔
101
    wallet(context),
2,150 ✔
102
    admin("admin", admin_pages),
2,150 ✔
103
    native("native", native_pages)
4,300 ✔
104
{
105
    // Admin is enabled by default, for the local machine only.
106
    admin.connections = 10;
2,150 ✔
107
    admin.binds.emplace_back(system::ip::ipv4::loopback(), 0_u16);
2,150 ✔
108

109
    // Admin may accumulate a large amount of log backlog.
110
    admin.maximum_backlog = 1024 * network::megabyte;
2,150 ✔
111

112
    // Admin sessions are held open for monitoring, so they do not expire.
113
    admin.inactivity_minutes = 0;
2,150 ✔
114
    admin.expiration_minutes = 0;
2,150 ✔
115

116
    // The largest btcd notification is a filtered block.
117
    btcd.maximum_backlog = 20 * network::megabyte;
2,150 ✔
118

119
    // btcd reports json-rpc errors in the response body only.
120
    btcd.error_status = false;
2,150 ✔
121

122
    // Raw block publications drop beyond the bound, eight blocks deep.
123
    zmq.maximum_backlog = 32 * network::megabyte;
2,150 ✔
124

125
    // Subscribers send nothing after subscribing, so publishers do not expire.
126
    zmq.inactivity_minutes = 0;
2,150 ✔
127
    zmq.expiration_minutes = 0;
2,150 ✔
128

129
    // Miners hold channels open and may go silent, so they do not expire.
130
    stratum_v1.inactivity_minutes = 0;
2,150 ✔
131
    stratum_v1.expiration_minutes = 0;
2,150 ✔
132
    stratum_v2.inactivity_minutes = 0;
2,150 ✔
133
    stratum_v2.expiration_minutes = 0;
2,150 ✔
134

135
    // A submitted block is base16 encoded within a json envelope.
136
    constexpr auto encoded = 2 * network::max_payload + 4 * network::kilobyte;
137
    bitcoind.maximum_request = encoded;
2,150 ✔
138
    btcd.maximum_request = encoded;
2,150 ✔
139

140
    // A broadcast transaction package is base16 encoded within a json envelope.
141
    electrum.maximum_request = encoded;
2,150 ✔
142
    sparrow.maximum_request = encoded;
2,150 ✔
143
    esplora.maximum_request = encoded;
2,150 ✔
144
}
2,150 ✔
145

146
// The service contexts are read from configured files, once, at startup.
147
code settings::initialize() NOEXCEPT
2,079 ✔
148
{
149
    // Service ports are offset by chain.
150
    const auto port = [this](uint16_t base) NOEXCEPT
41,580 ✔
151
    {
152
        switch (network)
41,580 ✔
153
        {
154
            case system::chain::selection::testnet3:
×
155
                return possible_narrow_cast<uint16_t>(base + 10000_u32);
×
156
            case system::chain::selection::regtest:
100 ✔
157
                return possible_narrow_cast<uint16_t>(base + 20000_u32);
100 ✔
158
            case system::chain::selection::signet:
×
159
                return possible_narrow_cast<uint16_t>(base + 30000_u32);
×
160
            case system::chain::selection::testnet4:
60 ✔
161
                return possible_narrow_cast<uint16_t>(base + 40000_u32);
60 ✔
162
            case system::chain::selection::testnet5:
20 ✔
163
                return possible_narrow_cast<uint16_t>(base + 50000_u32);
20 ✔
164
            default:
165
                return base;
166
        }
167
    };
2,079 ✔
168

169
    admin.bind_port = port(8080);
2,079 ✔
170
    admin.safe_port = port(8043);
2,079 ✔
171
    native.bind_port = port(8180);
2,079 ✔
172
    native.safe_port = port(8143);
2,079 ✔
173
    bitcoind.bind_port = port(8280);
2,079 ✔
174
    bitcoind.safe_port = port(8243);
2,079 ✔
175
    zmq.bind_port = port(8380);
2,079 ✔
176
    zmq.safe_port = port(8343);
2,079 ✔
177
    btcd.bind_port = port(8480);
2,079 ✔
178
    btcd.safe_port = port(8443);
2,079 ✔
179
    electrum.bind_port = port(8580);
2,079 ✔
180
    electrum.safe_port = port(8543);
2,079 ✔
181
    sparrow.bind_port = port(8680);
2,079 ✔
182
    sparrow.safe_port = port(8643);
2,079 ✔
183
    esplora.bind_port = port(8780);
2,079 ✔
184
    esplora.safe_port = port(8743);
2,079 ✔
185
    stratum_v1.bind_port = port(8880);
2,079 ✔
186
    stratum_v1.safe_port = port(8843);
2,079 ✔
187
    stratum_v2.bind_port = port(8980);
2,079 ✔
188
    stratum_v2.safe_port = port(8943);
2,079 ✔
189

190
    // Service bindings without a port take the service's default port.
191
    admin.default_ports();
2,079 ✔
192
    native.default_ports();
2,079 ✔
193
    bitcoind.default_ports();
2,079 ✔
194
    zmq.default_ports();
2,079 ✔
195
    btcd.default_ports();
2,079 ✔
196
    electrum.default_ports();
2,079 ✔
197
    sparrow.default_ports();
2,079 ✔
198
    esplora.default_ports();
2,079 ✔
199
    stratum_v1.default_ports();
2,079 ✔
200
    stratum_v2.default_ports();
2,079 ✔
201

202
    admin.default_buffers();
2,079 ✔
203
    native.default_buffers();
2,079 ✔
204
    bitcoind.default_buffers();
2,079 ✔
205
    zmq.default_buffers();
2,079 ✔
206
    btcd.default_buffers();
2,079 ✔
207
    electrum.default_buffers();
2,079 ✔
208
    sparrow.default_buffers();
2,079 ✔
209
    esplora.default_buffers();
2,079 ✔
210
    stratum_v1.default_buffers();
2,079 ✔
211
    stratum_v2.default_buffers();
2,079 ✔
212

213
    const auto initialize = [this](auto& service) NOEXCEPT
20,782 ✔
214
    {
215
        const auto ec = service.initialize_context();
20,782 ✔
216
        if (ec)
20,782 ✔
217
            failure = service.name;
1 ✔
218

219
        return ec;
20,782 ✔
220
    };
2,079 ✔
221

222
    if (const auto ec = initialize(admin)) return ec;
2,079 ✔
223
    if (const auto ec = initialize(native)) return ec;
2,079 ✔
224
    if (const auto ec = initialize(bitcoind)) return ec;
2,078 ✔
225
    if (const auto ec = initialize(btcd)) return ec;
2,078 ✔
226
    if (const auto ec = initialize(electrum)) return ec;
2,078 ✔
227
    if (const auto ec = initialize(sparrow)) return ec;
2,078 ✔
228
    if (const auto ec = initialize(esplora)) return ec;
2,078 ✔
229
    if (const auto ec = initialize(stratum_v1)) return ec;
2,078 ✔
230
    if (const auto ec = initialize(stratum_v2)) return ec;
2,078 ✔
231
    return initialize(zmq);
2,078 ✔
232
}
233

234
// settings::wallet_settings
235
settings::wallet_settings::wallet_settings() NOEXCEPT
1 ✔
236
  : wallet_settings(system::chain::selection::mainnet)
1 ✔
237
{
238
}
1 ✔
239

240
static const context& to_wallet_context(
4,392 ✔
241
    system::chain::selection context) NOEXCEPT
242
{
243
    switch (context)
4,392 ✔
244
    {
245
        case system::chain::selection::testnet3:
246
        case system::chain::selection::testnet4:
247
        case system::chain::selection::testnet5:
248
        case system::chain::selection::signet:
249
            return ctx::btc::test;
250
        case system::chain::selection::regtest:
21 ✔
251
            return ctx::btc::regtest;
21 ✔
252
        case system::chain::selection::mainnet:
4,349 ✔
253
        case system::chain::selection::none:
254
        default:
255
            return ctx::btc::main;
4,349 ✔
256
    }
257
}
258

259
settings::wallet_settings::wallet_settings(
4,392 ✔
260
    system::chain::selection context) NOEXCEPT
4,392 ✔
261
{
262
    const auto& value = to_wallet_context(context);
4,392 ✔
263
    p2kh_prefix = value.p2kh;
4,392 ✔
264
    p2sh_prefix = value.p2sh;
4,392 ✔
265
    wif_prefix = value.wif;
4,392 ✔
266
    witness_prefix = value.p2w;
4,392 ✔
267
    silent_prefix = value.sp;
4,392 ✔
268
    hd_private_prefix = value.hd.prv;
4,392 ✔
269
    hd_public_prefix = value.hd.pub;
4,392 ✔
270
}
4,392 ✔
271

272
context settings::wallet_settings::to_context() const NOEXCEPT
10,265 ✔
273
{
274
    return
275
    {
276
        { hd_private_prefix, hd_public_prefix },
10,265 ✔
277
        witness_prefix,
10,265 ✔
278
        p2kh_prefix,
10,265 ✔
279
        p2sh_prefix,
10,265 ✔
280
        wif_prefix,
10,265 ✔
281
        silent_prefix
10,265 ✔
282
    };
10,265 ✔
283
}
10,265 ✔
284

285
// settings::embedded_pages
286
span_value settings::embedded_pages::css()  const NOEXCEPT { return {}; }
4,137 ✔
287
span_value settings::embedded_pages::html() const NOEXCEPT { return {}; }
4,140 ✔
288
span_value settings::embedded_pages::ecma() const NOEXCEPT { return {}; }
4,137 ✔
289
span_value settings::embedded_pages::font() const NOEXCEPT { return {}; }
4,137 ✔
290
span_value settings::embedded_pages::icon() const NOEXCEPT { return {}; }
4,137 ✔
291
bool settings::embedded_pages::enabled() const NOEXCEPT
22 ✔
292
{
293
    return !html().empty();
22 ✔
294
}
295

296
// settings::html_server
297
settings::html_server::html_server(const std::string_view& logging_name,
4,301 ✔
298
    const embedded_pages& embedded) NOEXCEPT
4,301 ✔
299
  : network::settings::websocket_server(logging_name),
300
    pages(embedded)
4,301 ✔
301
{
302
}
4,301 ✔
303

304
// A weak tag, as each page may be sent in any content coding.
305
code settings::html_server::initialize_context() NOEXCEPT
4,158 ✔
306
{
307
    if (const auto ec = base::initialize_context())
4,158 ✔
308
        return ec;
1 ✔
309

310
    const auto tag = [](const span_value& page) NOEXCEPT -> std::string
20,785 ✔
311
    {
312
        if (page.empty())
20,785 ✔
313
            return {};
20,670 ✔
314

315
        const auto hash = accumulator<sha256>::hash(page.size(), page.data());
115 ✔
316
        return "W/\"" + encode_base16(hash) + "\"";
115 ✔
317
    };
318

319
    tags_ =
320
    {
321
        tag(pages.css()),
4,157 ✔
322
        tag(pages.html()),
4,157 ✔
323
        tag(pages.ecma()),
4,157 ✔
324
        tag(pages.font()),
4,157 ✔
325
        tag(pages.icon())
4,157 ✔
326
    };
4,157 ✔
327

328
    return network::error::success;
4,157 ✔
329
}
20,785 ✔
330

331
const settings::html_server::page_tags&
332
settings::html_server::tags() const NOEXCEPT
19 ✔
333
{
334
    return tags_;
19 ✔
335
}
336

337
// settings::bitcoind_server
338
code settings::bitcoind_server::initialize_context() NOEXCEPT
4,162 ✔
339
{
340
    using namespace bitcoind;
341
    if (const auto ec = base::initialize_context())
4,162 ✔
342
        return ec;
×
343

344
    // Range bounded by implemented versions,
345
    // maximum floors to implemented version within range.
346
    if (protocol_minimum < minimum_version ||
8,323 ✔
347
        protocol_maximum > maximum_version ||
4,162 ✔
348
        to_value(version_floor(protocol_maximum)) < protocol_minimum)
4,160 ✔
349
        return network::error::invalid_configuration;
3 ✔
350

351
    if (enable_utxo)
4,159 ✔
352
    {
353
        inactivity_minutes = 0;
670 ✔
354
        expiration_minutes = 0;
670 ✔
355
    }
356

357
    return network::error::success;
4,159 ✔
358
}
359

360
// Tag and extranonce are each a single push within the coinbase script.
361
code settings::mining_server::initialize_context() NOEXCEPT
4,193 ✔
362
{
363
    using namespace system::chain;
364
    if (const auto ec = base::initialize_context())
4,193 ✔
365
        return ec;
×
366

367
    const auto restricted = [](const auto& credential) NOEXCEPT
18 ✔
368
    {
369
        return !credential.methods().empty();
18 ✔
370
    };
371

372
    const auto extranonce = coinbase_extranonce_size();
4,193 ✔
373
    const auto script = coinbase_script_size(coinbase_tag.size(), extranonce);
4,193 ✔
374
    if (std::ranges::any_of(credentials, restricted) ||
4,193 ✔
375
        coinbase_tag.size() > max_direct_push ||
4,191 ✔
376
        extranonce > max_direct_push || script > max_coinbase_size ||
4,190 ✔
377
        !(minimum_difficulty > 0.0) || !(start_difficulty >= 0.0) ||
4,189 ✔
378
        std::isinf(minimum_difficulty) || std::isinf(start_difficulty))
8,379 ✔
379
        return network::error::invalid_configuration;
8 ✔
380

381
    return network::error::success;
4,185 ✔
382
}
383

384
double settings::mining_server::initial_difficulty(
129 ✔
385
    double hash_rate) const NOEXCEPT
386
{
387
    const auto start = start_difficulty > 0.0 ? start_difficulty :
129 ✔
388
        to_rate_difficulty(hash_rate, share_interval_seconds);
125 ✔
389

390
    return std::max(minimum_difficulty, start);
129 ✔
391
}
392

393
code settings::stratum_v1_server::initialize_context() NOEXCEPT
2,084 ✔
394
{
395
    if (const auto ec = base::initialize_context())
2,084 ✔
396
        return ec;
2 ✔
397

398
    if (extranonce1_size < sizeof(uint32_t) || is_zero(extranonce2_size))
2,082 ✔
399
        return network::error::invalid_configuration;
2 ✔
400

401
    return network::error::success;
2,080 ✔
402
}
403

404
size_t settings::stratum_v1_server::coinbase_extranonce_size() const NOEXCEPT
2,093 ✔
405
{
406
    return add<size_t>(extranonce1_size, extranonce2_size);
2,093 ✔
407
}
408

409
// The prefix of a standard channel is the whole extranonce, a B0_32 field.
410
code settings::stratum_v2_server::initialize_context() NOEXCEPT
2,109 ✔
411
{
412
    if (const auto ec = base::initialize_context())
2,109 ✔
413
        return ec;
6 ✔
414

415
    if (extranonce_prefix_size < sizeof(uint32_t) ||
4,205 ✔
416
        is_zero(extranonce_size) || is_zero(maximum_channels) ||
4,203 ✔
417
        coinbase_extranonce_size() > system::hash_size)
2,100 ✔
418
        return network::error::invalid_configuration;
4 ✔
419

420
    return initialize_noise();
2,099 ✔
421
}
422

423
static bool is_authorized(const network::noise::context& instance,
3 ✔
424
    const system::ec_xonly& authority) NOEXCEPT
425
{
426
    using namespace network::noise;
427
    const auto now = network::unix_time();
3 ✔
428
    system::ec_xonly key{};
3 ✔
429
    certificate::data proof{};
3 ✔
430
    return certificate::to_xonly(key, instance.static_secret()) &&
6 ✔
431
        instance.issue(proof, now) &&
6 ✔
432
        certificate::verify(proof, key, authority, now);
3 ✔
433
}
434

435
// Noise requires a static secret and exactly one of authority or certificate.
436
code settings::stratum_v2_server::initialize_noise() NOEXCEPT
2,099 ✔
437
{
438
    using namespace system;
439
    using namespace network;
440
    constexpr uint32_t seconds_per_day = 86400;
441
    const data_chunk& secret = noise_secret;
2,099 ✔
442
    const data_chunk& authority = noise_authority;
2,099 ✔
443
    const data_chunk& authority_key = noise_authority_key;
2,099 ✔
444
    const data_chunk& issued = noise_certificate;
2,099 ✔
445
    const auto authorized = !authority.empty();
2,099 ✔
446
    const auto certified = !issued.empty();
2,099 ✔
447
    const auto verified = !authority_key.empty();
2,099 ✔
448
    if (secret.empty())
2,099 ✔
449
        return authorized || certified || verified ||
4,165 ✔
450
            (!safes.empty() && !tls_secured()) ?
2,083 ✔
451
            network::error::invalid_configuration : network::error::success;
4,168 ✔
452

453
    if (noise)
14 ✔
UNCOV
454
        return network::error::operation_failed;
×
455

456
    ec_xonly verifier{};
14 ✔
457
    if (secret.size() != ec_secret_size || authorized == certified ||
13 ✔
458
        (authorized && authority.size() != ec_secret_size) ||
11 ✔
459
        (certified && issued.size() != noise::cipher::certificate_size) ||
25 ✔
460
        (verified && !noise::certificate::to_authority(verifier, authority_key)))
15 ✔
461
        return network::error::invalid_configuration;
5 ✔
462

463
    const auto key = data_slice{ secret }.to_array<ec_secret_size>();
9 ✔
464
    if (authorized)
9 ✔
465
        noise = to_unique<noise::context>(key,
12 ✔
466
            data_slice{ authority }.to_array<ec_secret_size>(),
12 ✔
467
            ceilinged_multiply<uint32_t>(noise_validity_days, seconds_per_day));
18 ✔
468
    else
469
        noise = to_unique<noise::context>(key,
6 ✔
470
            data_slice{ issued }.to_array<noise::cipher::certificate_size>());
9 ✔
471

472
    if (!noise->valid() || (verified && !is_authorized(*noise, verifier)))
9 ✔
473
    {
474
        noise.reset();
2 ✔
475
        return network::error::invalid_configuration;
2 ✔
476
    }
477

478
    return network::error::success;
7 ✔
479
}
480

481
bool settings::stratum_v2_server::tls_secured() const NOEXCEPT
4 ✔
482
{
483
    return !cert_path.empty() || !key_path.empty();
4 ✔
484
}
485

486
bool settings::stratum_v2_server::secure() const NOEXCEPT
83 ✔
487
{
488
    return network::settings::tls_server::secure() ||
166 ✔
489
        (!safes.empty() && noise);
84 ✔
490
}
491

492
network::settings::transport
493
settings::stratum_v2_server::secure_context() const NOEXCEPT
1 ✔
494
{
495
    const auto tls = network::settings::tls_server::secure();
1 ✔
496
    if (tls && noise)
1 ✔
NEW
497
        return network::settings::tls_noise{ std::cref(*context),
×
NEW
498
            std::cref(*noise) };
×
499

500
    if (tls)
1 ✔
NEW
501
        return network::settings::tls_server::secure_context();
×
502

503
    BC_ASSERT(noise);
1 ✔
504
    return std::cref(*noise);
1 ✔
505
}
506

507
size_t settings::stratum_v2_server::coinbase_extranonce_size() const NOEXCEPT
4,317 ✔
508
{
509
    return add<size_t>(extranonce_prefix_size, extranonce_size);
4,317 ✔
510
}
511

512
code settings::esplora_server::initialize_context() NOEXCEPT
2,083 ✔
513
{
514
    using namespace server::esplora;
515
    if (const auto ec = base::initialize_context())
2,083 ✔
UNCOV
516
        return ec;
×
517

518
    // The version floors to an implemented version.
519
    if (protocol_version < minimum_version ||
2,083 ✔
520
        protocol_version > maximum_version)
521
        return network::error::invalid_configuration;
2 ✔
522

523
    return network::error::success;
2,081 ✔
524
}
525

526
} // namespace server
527

528
// ----------------------------------------------------------------------------
529

530
} // 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