|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// dgb M3 §7b DigiShield Scrypt-only ancestor-walk regression guard. |
| 3 | +// |
| 4 | +// Pins the consensus quirk flagged at coin/header_chain.hpp's DIGISHIELD |
| 5 | +// INSERTION POINT: on a mixed-algo DGB chain the Scrypt difficulty window must |
| 6 | +// walk SCRYPT ancestors ONLY and SKIP continuity (non-Scrypt) headers. Folding |
| 7 | +// a continuity header into the window corrupts the Scrypt retarget and breaks |
| 8 | +// work-neutrality (THIRD INVARIANT). Also pins header_credits_work() == the |
| 9 | +// Scrypt predicate, so the validate() work-accounting and the retarget walk |
| 10 | +// share one SSOT and cannot drift apart. |
| 11 | +// |
| 12 | +// Links ONLY the header-only helpers + gtest -- no dgb OBJECT lib / transport. |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | + |
| 15 | +#include <cstdint> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <impl/dgb/coin/dgb_digishield.hpp> |
| 21 | + |
| 22 | +using namespace dgb::coin; |
| 23 | + |
| 24 | +static constexpr int32_t PRIMARY = 2; // BLOCK_VERSION_DEFAULT |
| 25 | +static constexpr int32_t SCRYPT = PRIMARY | DGB_BLOCK_VERSION_SCRYPT; |
| 26 | +static constexpr int32_t SHA256D = PRIMARY | DGB_BLOCK_VERSION_SHA256D; |
| 27 | +static constexpr int32_t SKEIN = PRIMARY | DGB_BLOCK_VERSION_SKEIN; |
| 28 | +static constexpr int32_t ODO = PRIMARY | DGB_BLOCK_VERSION_ODO; |
| 29 | + |
| 30 | +// Build a version_at() over a fixed nearest-first chain. |
| 31 | +static std::function<int32_t(std::size_t)> chain(const std::vector<int32_t>& c) |
| 32 | +{ |
| 33 | + return [c](std::size_t k) { return c.at(k); }; |
| 34 | +} |
| 35 | + |
| 36 | +TEST(DigishieldWalk, WorkCreditPredicateIsScryptOnly) |
| 37 | +{ |
| 38 | + EXPECT_TRUE (header_credits_work(SCRYPT)); |
| 39 | + EXPECT_TRUE (header_credits_work(PRIMARY)); // bare primary == Scrypt |
| 40 | + EXPECT_FALSE(header_credits_work(SHA256D)); |
| 41 | + EXPECT_FALSE(header_credits_work(SKEIN)); |
| 42 | + EXPECT_FALSE(header_credits_work(ODO)); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(DigishieldWalk, AllScryptChainTakesContiguousWindow) |
| 46 | +{ |
| 47 | + std::vector<int32_t> c(10, SCRYPT); |
| 48 | + auto w = scrypt_window_ancestors(chain(c), c.size(), 4); |
| 49 | + ASSERT_EQ(w.size(), 4u); |
| 50 | + EXPECT_EQ(w, (std::vector<std::size_t>{0, 1, 2, 3})); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(DigishieldWalk, SkipsInterleavedContinuityHeaders) |
| 54 | +{ |
| 55 | + // Mixed: S, sha, S, skein, S, odo, S -> Scrypt at k = 0,2,4,6. |
| 56 | + std::vector<int32_t> c{SCRYPT, SHA256D, SCRYPT, SKEIN, SCRYPT, ODO, SCRYPT}; |
| 57 | + auto w = scrypt_window_ancestors(chain(c), c.size(), 3); |
| 58 | + ASSERT_EQ(w.size(), 3u); |
| 59 | + EXPECT_EQ(w, (std::vector<std::size_t>{0, 2, 4})); // continuity skipped |
| 60 | +} |
| 61 | + |
| 62 | +TEST(DigishieldWalk, LeadingContinuityRunIsSkipped) |
| 63 | +{ |
| 64 | + // Nearest headers are all non-Scrypt; first Scrypt sample is deep. |
| 65 | + std::vector<int32_t> c{SHA256D, SKEIN, ODO, SCRYPT, SCRYPT}; |
| 66 | + auto w = scrypt_window_ancestors(chain(c), c.size(), 2); |
| 67 | + ASSERT_EQ(w.size(), 2u); |
| 68 | + EXPECT_EQ(w, (std::vector<std::size_t>{3, 4})); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(DigishieldWalk, ExhaustedChainReturnsFewerThanWindow) |
| 72 | +{ |
| 73 | + // Only 2 Scrypt headers exist but the window wants 4 (early-chain region). |
| 74 | + std::vector<int32_t> c{SCRYPT, SHA256D, SCRYPT, SHA256D}; |
| 75 | + auto w = scrypt_window_ancestors(chain(c), c.size(), 4); |
| 76 | + ASSERT_EQ(w.size(), 2u); |
| 77 | + EXPECT_EQ(w, (std::vector<std::size_t>{0, 2})); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(DigishieldWalk, ZeroWindowIsEmpty) |
| 81 | +{ |
| 82 | + std::vector<int32_t> c(4, SCRYPT); |
| 83 | + EXPECT_TRUE(scrypt_window_ancestors(chain(c), c.size(), 0).empty()); |
| 84 | +} |
0 commit comments