|
| 1 | +// dgb::compute_stale_prop / compute_real_pool_hashrate — pool efficiency KAT. |
| 2 | +// |
| 3 | +// FENCED conformance test (no production code touched). Pins the pool |
| 4 | +// EFFICIENCY / REAL-HASHRATE diagnostics arithmetic lifted into |
| 5 | +// pool_efficiency.hpp against the p2pool-dgb-scrypt oracle main.py status loop: |
| 6 | +// real_att_s = get_pool_attempts_per_second(...) / (1 - stale_prop) |
| 7 | +// with stale_prop = (orphan + doa) / total_recent over the recent share window. |
| 8 | +// |
| 9 | +// The expected values here are HAND-DERIVED from the oracle formulas (not |
| 10 | +// produced by calling the helper under test), so the test is non-circular: it |
| 11 | +// independently recomputes the oracle expression and asserts the helper matches. |
| 12 | +// |
| 13 | +// MUST appear in BOTH the ctest registration (this dir CMakeLists.txt) AND the |
| 14 | +// build.yml --target allowlist, or it becomes a #143-style NOT_BUILT sentinel |
| 15 | +// that reds master. |
| 16 | + |
| 17 | +#include <impl/dgb/pool_efficiency.hpp> |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +// ---- compute_stale_prop ------------------------------------------------- |
| 24 | + |
| 25 | +TEST(DgbPoolEfficiency, StalePropEmptyWindowIsZero) { |
| 26 | + // total_recent == 0 -> 0.0, never NaN. |
| 27 | + EXPECT_DOUBLE_EQ(dgb::compute_stale_prop(0, 0, 0), 0.0); |
| 28 | + // Even with (impossibly) non-zero counts, an empty window short-circuits. |
| 29 | + EXPECT_DOUBLE_EQ(dgb::compute_stale_prop(7, 3, 0), 0.0); |
| 30 | +} |
| 31 | + |
| 32 | +TEST(DgbPoolEfficiency, StalePropNoStalesIsZero) { |
| 33 | + EXPECT_DOUBLE_EQ(dgb::compute_stale_prop(0, 0, 100), 0.0); |
| 34 | +} |
| 35 | + |
| 36 | +TEST(DgbPoolEfficiency, StalePropOrphanPlusDoaOverTotal) { |
| 37 | + // 3 orphan + 2 doa over 100 recent -> 5/100 = 0.05 (hand-derived). |
| 38 | + EXPECT_DOUBLE_EQ(dgb::compute_stale_prop(3, 2, 100), 0.05); |
| 39 | + // 1 orphan + 0 doa over 4 -> 0.25. |
| 40 | + EXPECT_DOUBLE_EQ(dgb::compute_stale_prop(1, 0, 4), 0.25); |
| 41 | + // All stale: 50 over 50 -> 1.0. |
| 42 | + EXPECT_DOUBLE_EQ(dgb::compute_stale_prop(30, 20, 50), 1.0); |
| 43 | +} |
| 44 | + |
| 45 | +// ---- compute_real_pool_hashrate ---------------------------------------- |
| 46 | + |
| 47 | +TEST(DgbPoolEfficiency, RealHashrateZeroStaleIsIdentity) { |
| 48 | + // stale_prop == 0 -> divisor is 1 -> unchanged. |
| 49 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(1000.0, 0.0), 1000.0); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(DgbPoolEfficiency, RealHashrateScalesByInverseEfficiency) { |
| 53 | + // Oracle: pool_hs / (1 - stale_prop). Recompute the oracle expression |
| 54 | + // independently (non-circular) and assert the helper equals it. |
| 55 | + const double pool_hs = 1000.0; |
| 56 | + const double stale_prop = 0.05; |
| 57 | + const double oracle = pool_hs / (1.0 - stale_prop); // ~1052.6315789... |
| 58 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(pool_hs, stale_prop), oracle); |
| 59 | + // Sanity anchor on the magnitude (hand: 1000/0.95 > 1052, < 1053). |
| 60 | + EXPECT_GT(dgb::compute_real_pool_hashrate(pool_hs, stale_prop), 1052.0); |
| 61 | + EXPECT_LT(dgb::compute_real_pool_hashrate(pool_hs, stale_prop), 1053.0); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(DgbPoolEfficiency, RealHashrateGuardSuppressesNearAllStale) { |
| 65 | + // stale_prop == 0.999 is NOT < 0.999 -> guard holds -> identity, no blow-up. |
| 66 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(1000.0, 0.999), 1000.0); |
| 67 | + // Above the threshold likewise suppressed. |
| 68 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(1000.0, 1.0), 1000.0); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(DgbPoolEfficiency, RealHashrateNoMeasuredWorkIsIdentity) { |
| 72 | + // pool_hs <= 0 -> nothing to scale. |
| 73 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(0.0, 0.05), 0.0); |
| 74 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(-5.0, 0.05), -5.0); |
| 75 | +} |
| 76 | + |
| 77 | +// ---- end-to-end chain mirroring node.cpp diagnostics -------------------- |
| 78 | + |
| 79 | +TEST(DgbPoolEfficiency, ChainStalePropThenRealHashrateMatchesOracle) { |
| 80 | + // node.cpp: stale_prop from counts, then real_pool_hs from raw aps. |
| 81 | + const double sp = dgb::compute_stale_prop(3, 2, 100); // 0.05 |
| 82 | + EXPECT_DOUBLE_EQ(sp, 0.05); |
| 83 | + const double pool_hs = 2000.0; |
| 84 | + const double oracle = pool_hs / (1.0 - sp); // 2000/0.95 |
| 85 | + EXPECT_DOUBLE_EQ(dgb::compute_real_pool_hashrate(pool_hs, sp), oracle); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace |
0 commit comments