-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathwork_source_test.cpp
More file actions
285 lines (255 loc) · 10.9 KB
/
Copy pathwork_source_test.cpp
File metadata and controls
285 lines (255 loc) · 10.9 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
// dgb::stratum::DGBWorkSource — Stage 4a skeleton construction + contract test.
//
// Proves the work source instantiates against the live coin types
// (c2pool::dgb::HeaderChain + dgb::coin::Mempool), satisfies the full
// core::stratum::IWorkSource pure-virtual contract (so core::StratumServer
// can hold it via shared_ptr<IWorkSource> in the next slice), and that its
// real-now surface (config defaults, atomic work-generation, share-target
// atomics, worker registry, best-share callback) behaves. The stubbed
// work-generation / submit methods are asserted to return their documented
// safe defaults so a regression that accidentally "implements" them with
// garbage is caught.
//
// MUST appear in BOTH this ctest registration AND the build.yml --target
// allowlist, or it becomes a #143-style NOT_BUILT sentinel that reds master.
#include <impl/dgb/stratum/work_source.hpp>
#include <impl/dgb/coin/header_chain.hpp>
#include <impl/dgb/coin/mempool.hpp>
#include <impl/dgb/config_coin.hpp> // dgb::CoinParams::subsidy (oracle SSOT)
#include <core/pow.hpp> // core::SubsidyFunc
#include <core/stratum_work_source.hpp>
#include <gtest/gtest.h>
#include <memory>
#include <optional>
namespace {
// IDENTICAL to params.hpp `p.subsidy_func` — the live CoinParams indirection.
const core::SubsidyFunc kSubsidyFunc =
[](uint32_t height) -> uint64_t { return dgb::CoinParams::subsidy(height); };
// Construct a DGBWorkSource over default-constructed coin deps. The submit
// callback records whether it was invoked (it must NOT be in the 4a skeleton).
struct Fixture {
c2pool::dgb::HeaderChain chain;
dgb::coin::Mempool mempool;
bool submit_called = false;
std::unique_ptr<dgb::stratum::DGBWorkSource> make()
{
auto fn = [this](const std::vector<unsigned char>&, uint32_t) -> bool {
submit_called = true;
return false;
};
return std::make_unique<dgb::stratum::DGBWorkSource>(
chain, mempool, /*is_testnet=*/false, fn, kSubsidyFunc);
}
};
TEST(DgbWorkSource, ConstructsAndSatisfiesIWorkSourceContract)
{
Fixture fx;
auto ws = fx.make();
// Usable through the abstract interface core::StratumServer holds.
core::stratum::IWorkSource* iface = ws.get();
ASSERT_NE(iface, nullptr);
}
TEST(DgbWorkSource, ConfigDefaultsMatchStratumConfig)
{
Fixture fx;
auto ws = fx.make();
const auto& cfg = ws->get_stratum_config();
EXPECT_DOUBLE_EQ(cfg.min_difficulty, 0.0005);
EXPECT_DOUBLE_EQ(cfg.max_difficulty, 65536.0);
EXPECT_DOUBLE_EQ(cfg.target_time, 3.0);
EXPECT_TRUE(cfg.vardiff_enabled);
}
TEST(DgbWorkSource, WorkGenerationStartsZeroAndBumps)
{
Fixture fx;
auto ws = fx.make();
EXPECT_EQ(ws->get_work_generation(), 0u);
ws->bump_work_generation();
ws->bump_work_generation();
EXPECT_EQ(ws->get_work_generation(), 2u);
}
TEST(DgbWorkSource, ShareTargetAtomicsRoundTrip)
{
Fixture fx;
auto ws = fx.make();
EXPECT_EQ(ws->get_share_bits(), 0u);
EXPECT_EQ(ws->get_share_max_bits(), 0u);
ws->set_share_target(0x1d00ffff, 0x1e0fffff);
EXPECT_EQ(ws->get_share_bits(), 0x1d00ffffu);
EXPECT_EQ(ws->get_share_max_bits(), 0x1e0fffffu);
}
TEST(DgbWorkSource, NoMergedChainInDefaultBuild)
{
Fixture fx;
auto ws = fx.make();
// DGB V36 default build is a standalone Scrypt parent (no merged mining;
// -DAUX_DOGE dual-parent is a parked STRETCH).
EXPECT_FALSE(ws->has_merged_chain(0x0001));
}
TEST(DgbWorkSource, BestShareHashFnEmptyUntilWired)
{
Fixture fx;
auto ws = fx.make();
EXPECT_FALSE(static_cast<bool>(ws->get_best_share_hash_fn()));
ws->set_best_share_hash_fn([]() { return uint256::ZERO; });
auto fn = ws->get_best_share_hash_fn();
ASSERT_TRUE(static_cast<bool>(fn));
EXPECT_EQ(fn(), uint256::ZERO);
}
TEST(DgbWorkSource, WorkerRegistryRoundTrip)
{
Fixture fx;
auto ws = fx.make();
core::stratum::WorkerInfo info;
info.username = "DGBaddr.worker1";
info.worker_name = "worker1";
ws->register_stratum_worker("sess-1", info);
ws->update_stratum_worker("sess-1", /*hashrate=*/1.0e9, /*dead=*/0.0,
/*difficulty=*/16.0, /*accepted=*/3, /*rejected=*/0, /*stale=*/0);
// No crash + idempotent unregister of a known + unknown session.
ws->unregister_stratum_worker("sess-1");
ws->unregister_stratum_worker("sess-unknown");
SUCCEED();
}
TEST(DgbWorkSource, WorkGenStubsReturnSafeDefaults)
{
Fixture fx;
auto ws = fx.make();
// 4a skeleton: every work-generation getter returns its documented
// empty/default form (4c fills them in).
EXPECT_TRUE(ws->get_current_gbt_prevhash().empty());
// get_current_work_template() now emits height + coinbasevalue (Stage 4c
// coinbasevalue wire); its dedicated assertions live in
// WorkTemplateEmitsHeightAndCoinbaseValueViaSsot below.
EXPECT_TRUE(ws->get_current_work_template().is_object());
EXPECT_TRUE(ws->get_stratum_merkle_branches().empty());
auto parts = ws->get_coinbase_parts();
EXPECT_TRUE(parts.first.empty());
EXPECT_TRUE(parts.second.empty());
}
TEST(DgbWorkSource, MiningSubmitStubRejectsWithoutCallingBroadcaster)
{
Fixture fx;
auto ws = fx.make();
auto result = ws->mining_submit(
"DGBaddr.worker1", "job-0", "en1", "en2", "ntime", "nonce", "rid-0",
/*merged_addresses=*/{}, /*job=*/nullptr);
// Stratum mining.submit response = [false, [code, msg, null]] reject form.
ASSERT_TRUE(result.is_array());
ASSERT_GE(result.size(), 1u);
EXPECT_FALSE(result[0].get<bool>());
// The 4a stub must NOT have reached the won-block broadcaster.
EXPECT_FALSE(fx.submit_called);
}
TEST(DgbWorkSource, ComputeShareDifficultyReturnsNotYetSentinel)
{
Fixture fx;
auto ws = fx.make();
// 4a skeleton: the per-coin (Scrypt) PoW-difficulty hook returns the
// documented 0.0 parse-error/not-yet sentinel. The coin-agnostic
// StratumServer's vardiff gate treats 0.0 as a hard reject, so no
// garbage difficulty leaks into the rate monitor before 4b/4c land
// the real scrypt_1024_1_1_256 assembly.
double diff = ws->compute_share_difficulty(
"coinb1", "coinb2", "en1", "en2", "ntime", "nonce",
/*version=*/0x20000000u, "prevhash", "1e0ffff0",
/*merkle_branches=*/{});
EXPECT_DOUBLE_EQ(diff, 0.0);
}
// Stage 4c coinbasevalue wire: the work template surfaces the NEXT-block
// height and its coinbasevalue, the latter derived THROUGH the #207 SSOT
// (subsidy_func) keyed on next_block_height() == tip.height + 1 (#209). An
// empty chain makes next_block_height() == base_height, so seeding an oracle
// era boundary pins the value unambiguously to the p2pool-dgb-scrypt subsidy.
TEST(DgbWorkSource, WorkTemplateEmitsHeightAndCoinbaseValueViaSsot)
{
Fixture fx;
fx.chain.set_base_height(400000); // phase3 first block (oracle boundary)
auto ws = fx.make();
auto tmpl = ws->get_current_work_template();
ASSERT_TRUE(tmpl.is_object());
ASSERT_TRUE(tmpl.contains("height"));
ASSERT_TRUE(tmpl.contains("coinbasevalue"));
// next_h = next_block_height() = base_height (empty chain) = 400000.
EXPECT_EQ(tmpl["height"].get<uint32_t>(), 400000u);
// Zero embedded fees, no external GBT -> oracle subsidy at the boundary.
EXPECT_EQ(tmpl["coinbasevalue"].get<uint64_t>(), 2434410000ULL);
}
// Stage 4c GBT scaffold: alongside height + coinbasevalue, the work template
// now surfaces the GBT fields the embedded path can derive truthfully without
// a TemplateBuilder port -- version (Scrypt algo lane), curtime, mintime, and
// an (empty) transactions[]. previousblockhash + bits intentionally stay absent
// until HeaderSample carries the tip hash / next-target compact (later slices).
TEST(DgbWorkSource, WorkTemplateEmitsGbtScaffoldFields)
{
Fixture fx;
fx.chain.set_base_height(400000);
auto ws = fx.make();
auto tmpl = ws->get_current_work_template();
ASSERT_TRUE(tmpl.is_object());
// version pins the DGB Scrypt lane: BIP9 base | algo nibble 0x0000.
ASSERT_TRUE(tmpl.contains("version"));
EXPECT_EQ(tmpl["version"].get<uint32_t>(), 0x20000000u);
// Empty chain -> median_time_past() == INT64_MIN -> mintime emitted as 0
// (unconstrained), and curtime is a real wall-clock stamp (>= 0).
ASSERT_TRUE(tmpl.contains("mintime"));
EXPECT_EQ(tmpl["mintime"].get<int64_t>(), 0);
ASSERT_TRUE(tmpl.contains("curtime"));
EXPECT_GE(tmpl["curtime"].get<int64_t>(), 0);
// No embedded tx selection yet -> transactions[] present but empty (no
// fabricated entries; consistent with the total_fees=0 coinbasevalue).
ASSERT_TRUE(tmpl.contains("transactions"));
EXPECT_TRUE(tmpl["transactions"].is_array());
EXPECT_TRUE(tmpl["transactions"].empty());
// The two hash/difficulty fields are deliberately NOT emitted yet.
EXPECT_FALSE(tmpl.contains("previousblockhash"));
EXPECT_FALSE(tmpl.contains("bits"));
}
// ── Embedded coinbasevalue: first production caller of subsidy_func ──────────
// One pin on each side of every DGB reward-era boundary (p2pool-dgb-scrypt
// oracle vectors, test_dgb_subsidy.cpp).
namespace {
struct EraVec { uint32_t height; uint64_t subsidy; const char* era; };
constexpr EraVec kEraBoundaries[] = {
{67199, 8000000000ULL, "phase1-fixed last"},
{67200, 7960000000ULL, "phase2 -0.5%/wk first"},
{399999, 6746441103ULL, "phase2 last"},
{400000, 2434410000ULL, "phase3 -1%/wk first"},
{1429999, 2157824200ULL, "phase3 last"},
{1430000, 1078500000ULL, "phase4 monthly-decay first"},
};
} // namespace
// No external GBT (embedded path): coinbasevalue is derived THROUGH the work
// source's subsidy_func at every era boundary, zero fees -> oracle subsidy.
TEST(DgbWorkSource, CoinbaseValueDerivesViaSubsidyFuncWhenNoGbt)
{
Fixture fx;
auto ws = fx.make();
for (const auto& v : kEraBoundaries) {
EXPECT_EQ(ws->coinbase_value(v.height, /*fees=*/0, std::nullopt), v.subsidy)
<< "embedded coinbasevalue diverged from oracle subsidy at " << v.era;
}
}
// Fees compose additively on the embedded path: subsidy + total_fees.
TEST(DgbWorkSource, CoinbaseValueAddsFeesOnEmbeddedPath)
{
Fixture fx;
auto ws = fx.make();
constexpr uint64_t kFees = 1234567ULL;
for (const auto& v : kEraBoundaries) {
EXPECT_EQ(ws->coinbase_value(v.height, kFees, std::nullopt), v.subsidy + kFees)
<< "fee addition wrong at " << v.era;
}
}
// External-daemon fallback PERSISTS: a present GBT coinbasevalue is authoritative
// and returned verbatim through the work source, bypassing local derivation.
TEST(DgbWorkSource, CoinbaseValueHonorsGbtVerbatim)
{
Fixture fx;
auto ws = fx.make();
constexpr uint64_t kGbt = 99999999999ULL; // deliberately != subsidy+fees
EXPECT_EQ(ws->coinbase_value(/*height=*/400000, /*fees=*/500,
std::optional<uint64_t>{kGbt}),
kGbt);
}
} // namespace