-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwork_source.cpp
More file actions
756 lines (671 loc) · 34.1 KB
/
Copy pathwork_source.cpp
File metadata and controls
756 lines (671 loc) · 34.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// bch::stratum::BCHWorkSource — slice-c implementation.
//
// Implements the read-only state accessors, per-connection worker
// bookkeeping, and the WORK-ASSEMBLY read path that bridges the
// coin-agnostic core::StratumServer to BCH work generation off
// bch::coin::TemplateBuilder::build_template:
//
// - cached_template() single-slot template memo (template cache)
// - get_current_work_template() GBT-shaped json for mining.notify
// - get_stratum_merkle_branches()coinbase-left merkle siblings (CTOR order)
// - get_coinbase_parts() trivial fallback split
// - get_current_gbt_prevhash() tip display-hex for clean_jobs dedup
// - worker registry + atomics
//
// DEFERRED to slice-d (share-WRITE / validation hot path, left as safe
// defaults here so the TU links and the read path is exercisable):
// - build_connection_coinbase() full PPLNS + ref_hash coinbase build
// - mining_submit() SHA256d classify + sharechain add + won-block
// - compute_share_difficulty() per-submission SHA256d difficulty
//
// BCH divergences from the BTC work source are confined here: no SegWit
// (non-witness coinbase + merkle throughout), CashTokens transparent-carry
// (build_template hands token-prefixed txs through unchanged), ABLA budget
// owned by TemplateBuilder. Per-coin isolation: bch tree only.
#include <impl/bch/stratum/work_source.hpp>
#include <impl/bch/stratum/coinbase_outputs.hpp> // assemble_v36_coinbase_outputs
#include <impl/bch/coin/header_chain.hpp>
#include <impl/bch/coin/mempool.hpp>
#include <impl/bch/coin/merkle.hpp> // merkle_hash_pair (CTOR SHA256d)
#include <impl/bch/coin/template_builder.hpp> // build_template + rpc::WorkData
#include <impl/bch/config_pool.hpp> // PoolConfig::max_target (G2 cold-start floor)
#include <core/log.hpp>
#include <btclibs/util/strencodings.h> // HexStr
#include <ctime>
#include <cstdlib> // std::getenv (BCH_DEMO_BLOCK_BITS isolated-net pin)
#include <span>
#include <utility>
#include <core/address_utils.hpp> // address_to_script (share write path)
#include <core/hash.hpp> // Hash (SHA256d)
#include <core/target_utils.hpp> // chain::target_to_difficulty
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <memory>
namespace {
// Little-endian Bitcoin-wire byte helpers (shared with BTC; non-witness here).
inline void push_u32_le(std::vector<uint8_t>& v, uint32_t x) {
v.push_back(static_cast<uint8_t>(x & 0xff));
v.push_back(static_cast<uint8_t>((x >> 8) & 0xff));
v.push_back(static_cast<uint8_t>((x >> 16) & 0xff));
v.push_back(static_cast<uint8_t>((x >> 24) & 0xff));
}
inline void push_u64_le(std::vector<uint8_t>& v, uint64_t x) {
for (int i = 0; i < 8; ++i)
v.push_back(static_cast<uint8_t>((x >> (i * 8)) & 0xff));
}
inline void push_varint(std::vector<uint8_t>& v, uint64_t n) {
if (n < 0xfd) { v.push_back(static_cast<uint8_t>(n)); }
else if (n <= 0xffff) {
v.push_back(0xfd);
v.push_back(static_cast<uint8_t>(n & 0xff));
v.push_back(static_cast<uint8_t>((n >> 8) & 0xff));
} else if (n <= 0xffffffff) {
v.push_back(0xfe); push_u32_le(v, static_cast<uint32_t>(n));
} else { v.push_back(0xff); push_u64_le(v, n); }
}
// BIP34 minimally-encoded height push for the coinbase scriptSig.
inline std::vector<uint8_t> bip34_height_push(uint32_t h) {
std::vector<uint8_t> enc; uint32_t tmp = h;
while (tmp) { enc.push_back(static_cast<uint8_t>(tmp & 0xff)); tmp >>= 8; }
if (enc.empty()) enc.push_back(0);
if (enc.back() & 0x80) enc.push_back(0);
std::vector<uint8_t> out;
out.push_back(static_cast<uint8_t>(enc.size())); // OP_PUSHBYTES_n
out.insert(out.end(), enc.begin(), enc.end());
return out;
}
inline uint32_t parse_be_hex_u32(const std::string& str) {
uint32_t v = 0; std::sscanf(str.c_str(), "%x", &v); return v;
}
} // anonymous namespace
namespace bch::stratum {
BCHWorkSource::BCHWorkSource(bch::coin::HeaderChain& chain,
bch::coin::Mempool& mempool,
bool is_testnet,
SubmitBlockFn submit_fn,
core::stratum::StratumConfig config)
: chain_(chain)
, mempool_(mempool)
, is_testnet_(is_testnet)
, submit_block_fn_(std::move(submit_fn))
, config_(std::move(config))
{
LOG_INFO << "[BCH-STRATUM] BCHWorkSource constructed (testnet="
<< (is_testnet_ ? "1" : "0") << ")";
}
BCHWorkSource::~BCHWorkSource() = default;
// -- IWorkSource: config + read-only state ------------------------------------
const core::stratum::StratumConfig& BCHWorkSource::get_stratum_config() const
{
return config_;
}
std::function<uint256()> BCHWorkSource::get_best_share_hash_fn() const
{
std::lock_guard<std::mutex> lk(best_share_mutex_);
return best_share_hash_fn_; // empty until set_best_share_hash_fn()
}
std::string BCHWorkSource::get_current_gbt_prevhash() const
{
// BE display-hex of the current BCH chain tip -- both the mining.notify
// prevhash field and the clean_jobs dedup key. Empty until the header
// chain has a tip (pre-IBD).
auto tip = chain_.tip();
if (!tip) return {};
return tip->block_hash.GetHex();
}
uint64_t BCHWorkSource::get_work_generation() const
{
return work_generation_.load(std::memory_order_relaxed);
}
bool BCHWorkSource::has_merged_chain(uint32_t /*chain_id*/) const
{
// BCH is a STANDALONE parent in V36 -- no merged-mining aux module.
return false;
}
// -- IWorkSource: per-connection bookkeeping ----------------------------------
void BCHWorkSource::register_stratum_worker(const std::string& session_id,
const core::stratum::WorkerInfo& info)
{
std::lock_guard<std::mutex> lk(workers_mutex_);
workers_[session_id] = info;
LOG_INFO << "[BCH-STRATUM] worker registered: session=" << session_id
<< " user=" << info.username
<< " worker=" << info.worker_name
<< " endpoint=" << info.remote_endpoint;
}
void BCHWorkSource::unregister_stratum_worker(const std::string& session_id)
{
std::lock_guard<std::mutex> lk(workers_mutex_);
auto it = workers_.find(session_id);
if (it != workers_.end()) {
LOG_INFO << "[BCH-STRATUM] worker unregistered: session=" << session_id
<< " user=" << it->second.username
<< " accepted=" << it->second.accepted
<< " rejected=" << it->second.rejected
<< " stale=" << it->second.stale;
workers_.erase(it);
}
}
void BCHWorkSource::update_stratum_worker(const std::string& session_id,
double hashrate, double dead_hashrate,
double difficulty,
uint64_t accepted, uint64_t rejected,
uint64_t stale)
{
std::lock_guard<std::mutex> lk(workers_mutex_);
auto it = workers_.find(session_id);
if (it == workers_.end()) return;
it->second.hashrate = hashrate;
it->second.dead_hashrate = dead_hashrate;
it->second.difficulty = difficulty;
it->second.accepted = accepted;
it->second.rejected = rejected;
it->second.stale = stale;
}
// -- IWorkSource: work generation (slice-c) -----------------------------------
std::shared_ptr<const bch::coin::rpc::WorkData>
BCHWorkSource::cached_template() const
{
const uint64_t gen = work_generation_.load(std::memory_order_relaxed);
uint256 tip_hash{};
if (auto tip = chain_.tip()) tip_hash = tip->block_hash;
{
std::lock_guard<std::mutex> lk(template_mutex_);
if (template_cache_ &&
template_cache_gen_ == gen &&
template_cache_tip_ == tip_hash)
return template_cache_;
}
// build_template is read-only over chain_ + mempool_ (their own internal
// locking). Done OUTSIDE template_mutex_ so a slow build never blocks a
// concurrent cache hit on another connection thread.
auto built = bch::coin::TemplateBuilder::build_template(chain_, mempool_, is_testnet_);
if (!built) return nullptr; // chain has no tip yet
// [ISOLATED-NET DEMO / G2] Env-gated block-target pin (single source).
// On a genesis-difficulty isolated net the substrate hands GBT bits =
// powLimit (regtest 0x207fffff), so every pseudoshare trivially clears
// block and the p2pool sharechain counter cannot move distinct from
// block-founds. When BCH_DEMO_BLOCK_BITS is set, pin the template block
// bits to a fixed harder compact target here -- on the mutable build
// result before it is frozen into the shared cache -- so the coinbase,
// the header nBits, and core stratum_server gbt_block_nbits all read one
// consistent value; the bulk of accepted shares then land as STORED
// shares and block-founds stay rare. OFF unless the env var is set;
// never active on normal or mainnet runs. BCH-local (no shared-core edit).
if (const char* e = std::getenv("BCH_DEMO_BLOCK_BITS"); e && *e)
built->m_data["bits"] = std::string(e);
// [G2 cold-start] Seed the pool share-target floor on the WORK-GEN path.
// build_connection_coinbase() also seeds share_bits_ from ref_hash_fn's
// genesis max_bits, but that path runs only AFTER a share is created --
// which core::StratumServer gates on pool_difficulty>0, itself derived
// from share_bits_. On an empty sharechain share_bits_==0 => pool
// difficulty==0 => the is_pool_share gate never fires => no share is
// created => share_bits_ can never advance: a cold-start deadlock the
// share-create seed cannot break because it sits behind the very gate it
// needs to open. cached_template() runs on every work poll, BEFORE and
// INDEPENDENT of share creation, so seeding the floor here bootstraps
// pool_difficulty and lets the first real submission cross the gate.
// Idempotent (fires only while unseeded); uses the exact floor
// compute_share_target's genesis branch emits for max_bits.
if (share_bits_.load(std::memory_order_relaxed) == 0) {
// [ISOLATED-NET DEMO / G2] Symmetric to BCH_DEMO_BLOCK_BITS above: on a
// genesis-difficulty isolated net PoolConfig::max_target() (the p2pool
// network share floor, ~diff-1 / 0x1d00ffff) is far too hard for a CPU
// grinder to clear at a useful rate, so the sharechain STORED counter
// never advances independently of block-founds even after the
// cold-start deadlock fix. BCH_DEMO_SHARE_BITS pins this cold-start
// share floor to a CPU-clearable compact target (e.g. regtest powLimit
// 0x207fffff) so a grinder promotes real STORED shares while
// BCH_DEMO_BLOCK_BITS keeps block-founds rare -- proving 0->N sharechain
// accumulation without ASIC hashrate. OFF unless set; never active on
// normal or mainnet runs. BCH-local (no shared-core edit).
uint32_t floor_bits;
if (const char* e = std::getenv("BCH_DEMO_SHARE_BITS"); e && *e)
floor_bits = static_cast<uint32_t>(std::strtoul(e, nullptr, 16));
else
floor_bits =
chain::target_to_bits_upper_bound(bch::PoolConfig::max_target());
share_bits_.store(floor_bits, std::memory_order_relaxed);
share_max_bits_.store(floor_bits, std::memory_order_relaxed);
}
auto sp = std::make_shared<const bch::coin::rpc::WorkData>(std::move(*built));
std::lock_guard<std::mutex> lk(template_mutex_);
template_cache_ = sp;
template_cache_gen_ = gen;
template_cache_tip_ = tip_hash;
return sp;
}
nlohmann::json BCHWorkSource::get_current_work_template() const
{
// TemplateBuilder::build_template already shapes WorkData::m_data as the
// GBT-style json stratum sessions consume (previousblockhash, bits,
// version, curtime, mintime, height, coinbasevalue, transactions[]).
// Empty object until the header chain is past genesis -- sessions skip
// work-push and retry. curtime is refreshed per-poll so mining.notify
// ntime tracks wall-clock even on a cache hit.
auto wd = cached_template();
if (!wd) return nlohmann::json::object();
nlohmann::json data = wd->m_data;
data["curtime"] = static_cast<int64_t>(std::time(nullptr));
return data;
}
std::vector<std::string> BCHWorkSource::get_stratum_merkle_branches() const
{
// Coinbase-left merkle siblings for mining.notify. The miner rebuilds
// the root via SHA256d(coinbase_txid || branch[0]), then folds in each
// subsequent branch. BCH uses CTOR (Nov 2018) for the block tx ORDER,
// but the merkle tree itself is the same SHA256d pairwise structure as
// BTC with NO witness commitment -- so the branch algorithm is identical;
// build_template has already emitted m_hashes in canonical (CTOR) order.
//
// Wire encoding: hex of the LE-internal 32 bytes (NOT GetHex() display
// order) -- the miner hex2bin-s this straight into SHA256d, so the bytes
// must match the leaves the pool hashed.
auto wd = cached_template();
if (!wd || wd->m_hashes.empty()) return {};
std::vector<uint256> level = wd->m_hashes; // [0] = coinbase placeholder
std::vector<std::string> branches;
while (level.size() > 1) {
branches.push_back(HexStr(std::span<const unsigned char>(level[1].data(), 32)));
std::vector<uint256> next;
next.reserve((level.size() + 2) / 2);
next.push_back(uint256::ZERO); // placeholder for (coinbase, level[1]) combo
for (size_t i = 2; i < level.size(); i += 2) {
const uint256& l = level[i];
const uint256& r = (i + 1 < level.size()) ? level[i + 1] : level[i];
next.push_back(bch::coin::merkle_hash_pair(l, r));
}
level = std::move(next);
}
return branches;
}
std::pair<std::string, std::string> BCHWorkSource::get_coinbase_parts() const
{
// Fallback split with no payout output -- the per-connection builder
// (build_connection_coinbase, slice-d) produces the real coinbase.
return { {}, {} };
}
// -- IWorkSource: setters (callback wiring from main_bch.cpp) ------------------
void BCHWorkSource::set_best_share_hash_fn(std::function<uint256()> fn)
{
std::lock_guard<std::mutex> lk(best_share_mutex_);
best_share_hash_fn_ = std::move(fn);
}
void BCHWorkSource::set_pplns_fn(PplnsFn fn)
{
std::lock_guard<std::mutex> lk(callback_mutex_);
pplns_fn_ = std::move(fn);
}
void BCHWorkSource::set_ref_hash_fn(RefHashFn fn)
{
std::lock_guard<std::mutex> lk(callback_mutex_);
ref_hash_fn_ = std::move(fn);
}
void BCHWorkSource::set_create_share_fn(CreateShareFn fn)
{
std::lock_guard<std::mutex> lk(callback_mutex_);
create_share_fn_ = std::move(fn);
}
void BCHWorkSource::set_donation_script(std::vector<unsigned char> script)
{
std::lock_guard<std::mutex> lk(callback_mutex_);
donation_script_ = std::move(script);
}
// -- slice-d: share-WRITE / validation hot path (SHA256d, non-SegWit) ---------
core::stratum::CoinbaseResult BCHWorkSource::build_connection_coinbase(
const uint256& prev_share_hash,
const std::string& /*extranonce1_hex*/,
const std::vector<unsigned char>& payout_script,
const std::vector<std::pair<uint32_t, std::vector<unsigned char>>>& /*merged_addrs*/) const
{
// c2pool BCH v36 coinbase. Ported from the BTC v35 work source with the
// BCH divergences applied:
// - NO SegWit: no witness-commitment output, no BIP141 reserved value;
// BCH never activated SegWit (M1 4) so the coinbase is non-witness.
// - pool tag is /c2pool-bch/.
// - CashTokens ride through build_template's mempool slice unchanged
// (transparent-carry; the coinbase author never inspects them).
// - CTOR governs block tx ORDER, not these coinbase bytes.
// Byte-shape (PPLNS split + sv>=36 donation P2SH + OP_RETURN ref_hash) is
// consensus-bearing -> pinned by the coinbase-KAT vs the p2pool-merged-v36
// BCH oracle; any divergence is a HARD STOP, not a silent ship.
auto wd = cached_template();
if (!wd) return {};
const uint32_t height = wd->m_data.value("height", 0u);
const uint64_t coinbasevalue = wd->m_data.value("coinbasevalue", uint64_t{0});
uint32_t block_bits = 0;
if (auto it = wd->m_data.find("bits"); it != wd->m_data.end() && it->is_string())
block_bits = parse_be_hex_u32(it->get<std::string>());
const uint32_t curtime = static_cast<uint32_t>(std::time(nullptr));
PplnsFn pplns_fn; RefHashFn ref_hash_fn;
std::vector<unsigned char> donation_script;
{
std::lock_guard<std::mutex> lk(callback_mutex_);
pplns_fn = pplns_fn_; ref_hash_fn = ref_hash_fn_; donation_script = donation_script_;
}
// scriptSig (deterministic): BIP34 height + pool tag.
auto bip34 = bip34_height_push(height);
static const std::string POOL_TAG = "/c2pool-bch/";
std::vector<uint8_t> scriptsig;
scriptsig.insert(scriptsig.end(), bip34.begin(), bip34.end());
if (!POOL_TAG.empty() && POOL_TAG.size() < 76) {
scriptsig.push_back(static_cast<uint8_t>(POOL_TAG.size()));
scriptsig.insert(scriptsig.end(), POOL_TAG.begin(), POOL_TAG.end());
}
// PPLNS payouts.
std::map<std::vector<unsigned char>, double> payouts;
if (pplns_fn) {
uint256 block_target;
if (block_bits != 0) block_target.SetCompact(block_bits);
try { payouts = pplns_fn(prev_share_hash, block_target, coinbasevalue, donation_script); }
catch (const std::exception& e) {
LOG_WARNING << "[BCH-STRATUM] pplns_fn threw: " << e.what()
<< " -- falling back to single-output coinbase";
payouts.clear();
}
}
// Drop empty-script entries; reabsorb their value into donation.
{
size_t dropped_n = 0; uint64_t dropped_value = 0;
for (auto it = payouts.begin(); it != payouts.end(); ) {
if (it->first.empty()) {
dropped_n++; dropped_value += static_cast<uint64_t>(it->second);
it = payouts.erase(it);
} else { ++it; }
}
if (dropped_n > 0 && !donation_script.empty())
payouts[donation_script] += static_cast<double>(dropped_value);
}
// V36 removes the finder fee -- pure PPLNS accounting. The oracle
// (p2pool-merged-v36 data.py ~945) fires the subsidy/200 finder fee ONLY in
// the `not v36_active` branch; the v36 gentx pays no finder fee. Byte-parity
// to the canonical merged-v36 gentx is the HARD invariant, so the sv>=36
// coinbase author deducts NOTHING here (integrator conform ruling 2026-06-27,
// same shape as the DASH dust call). The pre-v36 (v35) finder fee lives in
// the legacy work source's not-v36 path, not in this v36-only builder.
// Degraded fallback: full subsidy -> miner.
if (payouts.empty() && !payout_script.empty())
payouts[payout_script] = static_cast<double>(coinbasevalue);
// === Oracle-conforming output assembly (p2pool-merged-v36 data.py gentx) ===
// The donation/marker output is forced LAST (immediately before the
// OP_RETURN), EXCLUDED from the (amount asc, script asc) sort, and obeys the
// V36 >= 1-satoshi consensus marker rule. PPLNS dests sort ascending and are
// capped to the largest 4000 (oracle dests[-4000:]).
// The donation forced-LAST rule, the V36 >=1-sat marker rule, the
// (amount asc, script asc) sort and the dests[-4000:] cap all live in the
// pure, header-only assemble_v36_coinbase_outputs() so the byte-shape is
// pinned by coinbase_author_kat_test against the p2pool-merged-v36 oracle
// (data.py generate_transaction) rather than asserted against this builder.
auto outputs = assemble_v36_coinbase_outputs(std::move(payouts), donation_script, coinbasevalue);
// ref_hash + frozen chain-walk values.
core::stratum::RefHashResult rh_result;
if (ref_hash_fn) {
try {
rh_result = ref_hash_fn(prev_share_hash, scriptsig, payout_script,
coinbasevalue, block_bits, curtime);
if (rh_result.bits != 0) {
share_bits_.store(rh_result.bits, std::memory_order_relaxed);
share_max_bits_.store(rh_result.max_bits, std::memory_order_relaxed);
} else if (share_bits_.load(std::memory_order_relaxed) == 0 &&
rh_result.max_bits != 0) {
// Cold-start seed (empty sharechain): compute_share_target's
// genesis branch yields bits==0 while max_bits carries the
// MAX_TARGET floor (easiest share target). Without a nonzero
// share_bits_, StratumServer derives pool_difficulty==0, its
// is_pool_share gate never fires, no share is ever created, and
// share_bits_ can never advance past 0 -> cold-start deadlock.
// Seed both atomics to the max_bits floor so the first real
// submission clears the gate and bootstraps the sharechain.
share_bits_.store(rh_result.max_bits, std::memory_order_relaxed);
share_max_bits_.store(rh_result.max_bits, std::memory_order_relaxed);
}
} catch (const std::exception& e) {
LOG_WARNING << "[BCH-STRATUM] ref_hash_fn threw: " << e.what()
<< " -- coinbase will lack OP_RETURN commitment";
}
}
const uint256& ref_hash = rh_result.ref_hash;
const uint64_t ref_nonce = rh_result.last_txout_nonce;
const bool emit_op_return = ref_hash_fn && !ref_hash.IsNull();
// Assemble coinb1: full tx up to (and including) ref_hash. NO SegWit
// output -> output_count is PPLNS outputs + optional OP_RETURN only.
const size_t output_count = outputs.size() + (emit_op_return ? 1 : 0);
std::vector<uint8_t> coinb1;
push_u32_le(coinb1, 1); // tx version
coinb1.push_back(0x01); // vin_count = 1
coinb1.insert(coinb1.end(), 32, 0x00); // prev_hash = 32 zero bytes
push_u32_le(coinb1, 0xFFFFFFFFu); // prev_vout
push_varint(coinb1, scriptsig.size());
coinb1.insert(coinb1.end(), scriptsig.begin(), scriptsig.end());
push_u32_le(coinb1, 0xFFFFFFFFu); // sequence
push_varint(coinb1, output_count);
for (const auto& [script, amount] : outputs) {
push_u64_le(coinb1, amount);
push_varint(coinb1, script.size());
coinb1.insert(coinb1.end(), script.begin(), script.end());
}
if (emit_op_return) {
push_u64_le(coinb1, 0); // 0 sats
coinb1.push_back(0x2a); // script_len = 42
coinb1.push_back(0x6a); // OP_RETURN
coinb1.push_back(0x28); // PUSH_40
coinb1.insert(coinb1.end(), ref_hash.data(), ref_hash.data() + 32);
// [8B nonce slot -- coinb1 ends here; en1+en2 fills it]
}
std::vector<uint8_t> coinb2;
push_u32_le(coinb2, 0u); // locktime
core::stratum::CoinbaseResult result;
result.coinb1 = HexStr(std::span<const uint8_t>(coinb1.data(), coinb1.size()));
result.coinb2 = HexStr(std::span<const uint8_t>(coinb2.data(), coinb2.size()));
auto& snap = result.snapshot;
snap.subsidy = coinbasevalue;
snap.segwit_active = false; // BCH: never SegWit
snap.frozen_ref.share_version = rh_result.share_version;
snap.frozen_ref.desired_version = rh_result.desired_version;
snap.frozen_ref.bits = rh_result.bits ? rh_result.bits : share_bits_.load();
snap.frozen_ref.max_bits = rh_result.max_bits ? rh_result.max_bits : share_max_bits_.load();
snap.frozen_ref.timestamp = rh_result.timestamp ? rh_result.timestamp : curtime;
snap.frozen_ref.absheight = rh_result.absheight;
snap.frozen_ref.abswork = rh_result.abswork;
snap.frozen_ref.far_share_hash = rh_result.far_share_hash;
snap.frozen_ref.ref_hash = ref_hash;
snap.frozen_ref.last_txout_nonce = ref_nonce;
auto branches = get_stratum_merkle_branches();
for (const auto& h : branches) {
uint256 b; auto bb = ParseHex(h);
if (bb.size() == 32) std::memcpy(b.begin(), bb.data(), 32);
snap.frozen_ref.frozen_merkle_branches.push_back(b);
}
// tx_data: raw tx hex for full-block assembly at submit time. Built inline
// (no cross-tree btc memo include -- per-coin isolation); a memo seam is a
// follow-up only if a BCH heaptrack shows churn here.
if (auto txs_field = wd->m_data.find("transactions");
txs_field != wd->m_data.end() && txs_field->is_array())
{
auto txv = std::make_shared<std::vector<std::string>>();
txv->reserve(txs_field->size());
for (const auto& t : *txs_field) {
if (!t.is_object()) continue;
if (auto d = t.find("data"); d != t.end() && d->is_string())
txv->push_back(d->get<std::string>());
}
snap.tx_data = txv;
}
snap.merkle_branches = std::move(branches);
return result;
}
nlohmann::json BCHWorkSource::mining_submit(
const std::string& username, const std::string& job_id,
const std::string& extranonce1, const std::string& extranonce2,
const std::string& ntime, const std::string& nonce,
const std::string& /*request_id*/,
const std::map<uint32_t, std::vector<unsigned char>>& /*merged_addresses*/,
const core::stratum::JobSnapshot* job)
{
auto reject = [](int code, const char* msg) {
return nlohmann::json::array({ false, nlohmann::json::array({code, msg, nullptr}) });
};
if (!job) {
LOG_WARNING << "[BCH-STRATUM] submit reject (no JobSnapshot): user=" << username
<< " job=" << job_id;
return reject(21, "Job not found");
}
// Reconstruct full coinbase: coinb1 || en1 || en2 || coinb2 (non-witness).
auto coinb1_bytes = ParseHex(job->coinb1);
auto en1_bytes = ParseHex(extranonce1);
auto en2_bytes = ParseHex(extranonce2);
auto coinb2_bytes = ParseHex(job->coinb2);
std::vector<uint8_t> coinbase;
coinbase.reserve(coinb1_bytes.size() + en1_bytes.size() + en2_bytes.size() + coinb2_bytes.size());
coinbase.insert(coinbase.end(), coinb1_bytes.begin(), coinb1_bytes.end());
coinbase.insert(coinbase.end(), en1_bytes.begin(), en1_bytes.end());
coinbase.insert(coinbase.end(), en2_bytes.begin(), en2_bytes.end());
coinbase.insert(coinbase.end(), coinb2_bytes.begin(), coinb2_bytes.end());
uint256 coinbase_txid = Hash(std::span<const uint8_t>(coinbase.data(), coinbase.size()));
// Ascend stratum merkle branches (LE-internal byte order; ParseHex+memcpy).
uint256 merkle_root = coinbase_txid;
for (const auto& branch_hex : job->merkle_branches) {
uint256 b; auto bb = ParseHex(branch_hex);
if (bb.size() == 32) std::memcpy(b.begin(), bb.data(), 32);
merkle_root = bch::coin::merkle_hash_pair(merkle_root, b);
}
auto prevhash_be = ParseHex(job->gbt_prevhash);
std::vector<uint8_t> prevhash_le(prevhash_be.rbegin(), prevhash_be.rend());
std::vector<uint8_t> header; header.reserve(80);
push_u32_le(header, job->version);
header.insert(header.end(), prevhash_le.begin(), prevhash_le.end());
header.insert(header.end(), merkle_root.data(), merkle_root.data() + 32);
push_u32_le(header, parse_be_hex_u32(ntime));
push_u32_le(header, parse_be_hex_u32(job->nbits)); // share-target bits in header
push_u32_le(header, parse_be_hex_u32(nonce));
uint256 pow_hash = Hash(std::span<const uint8_t>(header.data(), header.size()));
uint256 share_target;
if (job->share_bits != 0) share_target.SetCompact(job->share_bits);
else share_target.SetCompact(/*diff 1*/ 0x1d00ffff);
uint256 block_target;
block_target.SetCompact(parse_be_hex_u32(
job->block_nbits.empty() ? job->nbits : job->block_nbits));
auto pow_hex_short = pow_hash.GetHex().substr(0, 16);
// -- Classify --
if (!(pow_hash > block_target)) {
// BLOCK FOUND.
uint32_t height = 0;
if (auto tip = chain_.tip(); tip) height = tip->height + 1;
LOG_WARNING << "[BCH-STRATUM-BLOCK] *** BLOCK FOUND *** user=" << username
<< " height~=" << height << " pow_hash=" << pow_hex_short
<< " job=" << job_id;
// Full block: header || tx_count || coinbase || other_txs. BCH has NO
// SegWit, so the coinbase is serialized as-is (no marker/flag/witness).
static const std::vector<std::string> kEmptyTxData;
const std::vector<std::string>& txs = job->tx_data ? *job->tx_data : kEmptyTxData;
std::vector<uint8_t> block_bytes;
block_bytes.reserve(80 + 9 + coinbase.size() + txs.size() * 256);
block_bytes.insert(block_bytes.end(), header.begin(), header.end());
push_varint(block_bytes, 1 + txs.size());
block_bytes.insert(block_bytes.end(), coinbase.begin(), coinbase.end());
for (const auto& tx_hex : txs) {
auto tx_bytes = ParseHex(tx_hex);
block_bytes.insert(block_bytes.end(), tx_bytes.begin(), tx_bytes.end());
}
// Won block: dual-path broadcast (embedded P2P relay + BCHN-RPC
// submitblock fallback). submit_block_fn_ returns true iff it reached
// at least one sink; NEITHER is a loud error (lost subsidy).
bool reached_network = false;
if (submit_block_fn_) {
try { reached_network = submit_block_fn_(block_bytes, height); }
catch (const std::exception& e) {
LOG_ERROR << "[BCH-STRATUM-BLOCK] submit_block_fn threw: " << e.what();
}
if (!reached_network)
LOG_ERROR << "[BCH-STRATUM-BLOCK] WON BLOCK height=" << height
<< " reached NEITHER P2P nor RPC -- lost subsidy!";
} else {
LOG_ERROR << "[BCH-STRATUM-BLOCK] no submit_block_fn wired -- WON BLOCK height="
<< height << " not broadcast -- lost subsidy!";
}
{
std::lock_guard<std::mutex> lk(workers_mutex_);
for (auto& [sid, w] : workers_) { (void)sid; if (w.username == username) { w.accepted++; break; } }
}
return nlohmann::json(true);
}
if (!(pow_hash > share_target)) {
// Share meets sharechain target -> create_share_fn_ (v36 share add).
CreateShareFn create_fn;
{ std::lock_guard<std::mutex> lk(callback_mutex_); create_fn = create_share_fn_; }
uint256 share_hash;
if (create_fn) {
auto payout_script = core::address_to_script(username);
try { share_hash = create_fn(coinbase, header, *job, payout_script); }
catch (const std::exception& e) {
LOG_WARNING << "[BCH-STRATUM-SHARE] create_share_fn threw: " << e.what()
<< " -- share not added";
}
}
if (!share_hash.IsNull())
LOG_INFO << "[BCH-STRATUM-SHARE] ACCEPTED + ADDED user=" << username
<< " share_hash=" << share_hash.GetHex().substr(0, 16)
<< " pow_hash=" << pow_hex_short << " job=" << job_id;
else if (create_fn)
LOG_INFO << "[BCH-STRATUM-SHARE] accepted (deferred) user=" << username
<< " pow_hash=" << pow_hex_short << " job=" << job_id;
else
LOG_INFO << "[BCH-STRATUM-SHARE] accepted (no-tracker) user=" << username
<< " pow_hash=" << pow_hex_short << " job=" << job_id;
{
std::lock_guard<std::mutex> lk(workers_mutex_);
for (auto& [sid, w] : workers_) { (void)sid; if (w.username == username) { w.accepted++; break; } }
}
return nlohmann::json(true);
}
// pow_hash > share_target -> low-difficulty rejection.
{
std::lock_guard<std::mutex> lk(workers_mutex_);
for (auto& [sid, w] : workers_) { (void)sid; if (w.username == username) { w.rejected++; break; } }
}
return reject(23, "Low difficulty share");
}
double BCHWorkSource::compute_share_difficulty(
const std::string& coinb1, const std::string& coinb2,
const std::string& extranonce1, const std::string& extranonce2,
const std::string& ntime, const std::string& nonce,
uint32_t version, const std::string& prevhash_hex,
const std::string& nbits_hex,
const std::vector<std::string>& merkle_branches) const
{
// SHA256d per-submission difficulty (BCH shares the BTC SHA256d PoW
// family). Reconstruct coinbase -> txid -> merkle ascent -> 80B header
// -> SHA256d -> target_to_difficulty. NO scrypt, NO SegWit.
auto coinb1_bytes = ParseHex(coinb1);
auto en1_bytes = ParseHex(extranonce1);
auto en2_bytes = ParseHex(extranonce2);
auto coinb2_bytes = ParseHex(coinb2);
std::vector<uint8_t> coinbase;
coinbase.reserve(coinb1_bytes.size() + en1_bytes.size() + en2_bytes.size() + coinb2_bytes.size());
coinbase.insert(coinbase.end(), coinb1_bytes.begin(), coinb1_bytes.end());
coinbase.insert(coinbase.end(), en1_bytes.begin(), en1_bytes.end());
coinbase.insert(coinbase.end(), en2_bytes.begin(), en2_bytes.end());
coinbase.insert(coinbase.end(), coinb2_bytes.begin(), coinb2_bytes.end());
uint256 coinbase_txid = Hash(std::span<const uint8_t>(coinbase.data(), coinbase.size()));
uint256 merkle_root = coinbase_txid;
for (const auto& bhex : merkle_branches) {
uint256 b; auto bb = ParseHex(bhex);
if (bb.size() == 32) std::memcpy(b.begin(), bb.data(), 32);
merkle_root = bch::coin::merkle_hash_pair(merkle_root, b);
}
auto prevhash_be = ParseHex(prevhash_hex);
std::vector<uint8_t> prevhash_le(prevhash_be.rbegin(), prevhash_be.rend());
std::vector<uint8_t> header; header.reserve(80);
push_u32_le(header, version);
header.insert(header.end(), prevhash_le.begin(), prevhash_le.end());
header.insert(header.end(), merkle_root.data(), merkle_root.data() + 32);
push_u32_le(header, parse_be_hex_u32(ntime));
push_u32_le(header, parse_be_hex_u32(nbits_hex));
push_u32_le(header, parse_be_hex_u32(nonce));
if (header.size() != 80) return 0.0;
uint256 pow_hash = Hash(std::span<const uint8_t>(header.data(), header.size()));
if (pow_hash.IsNull()) return 0.0;
return chain::target_to_difficulty(pow_hash);
}
} // namespace bch::stratum