test: GetWeight V5 soft-cap coverage across all three regimes
The 2026-04-20 deploy added a 7-day soft cap to GetWeight that activates ONLY when BOTH height >= FORK_HEIGHT_V5 (17651) AND nIntervalEnd >= STAKE_AGE_SOFT_CAP_ACTIVATION (1776000000 = 2026-04-12 ~13:20 UTC). This is the production code path for every stake on the live chain since the deploy. The existing staking_tests only covered the pre-V5 (nStakeMaxAge hard cap) path, plus one negative test that confirmed the soft cap does NOT apply pre-V5. The two production regimes -- V5+post-activation and V5+pre-activation -- had no direct test coverage. Adds 8 cases: - V5+post-activation: cap at 7 days for stakes past the cap - V5+post-activation: linear below the cap - V5+post-activation: exactly at the cap (boundary) - V5+post-activation: 1 second past the cap (boundary) - V5+pre-activation: UNcapped (historical stakes preserve original rules) - V5+activation-exact: >= semantics include the activation timestamp - V5+high height (2.5M, like DNS2 live): cap unchanged by distance from fork - V5+min-age floor: nStakeMinAge still returns 0 below floor Uses RAII (BestChainGuard) to scope pindexBest swaps so a failed assertion can't leave a stack pointer dangling in the global -- an improvement over the manual save/restore pattern used in consensus_safety_tests, which is also prone to leaving stale pointers if a CHECK throws. Test surface: 8 new test cases, 8 new assertions. Full suite: 235/235 cases, 21617/21617 assertions. ctest: 4/4 green.
This commit is contained in:
@@ -153,4 +153,167 @@ BOOST_AUTO_TEST_CASE(pos_reward_large_coinage)
|
||||
BOOST_CHECK(reward > 0);
|
||||
}
|
||||
|
||||
// --- GetWeight: V5 soft-cap behavior (post-2026-04-12 fork fix) ---
|
||||
//
|
||||
// The 2026-04-20 deploy changed GetWeight to apply a 7-day soft cap on
|
||||
// stake weight instead of the hard nStakeMaxAge (= 12 hours) cap, but only
|
||||
// after a height AND a timestamp gate:
|
||||
// - height must be >= FORK_HEIGHT_V5 (= 17651), AND
|
||||
// - nIntervalEnd must be >= STAKE_AGE_SOFT_CAP_ACTIVATION (= 1776000000,
|
||||
// 2026-04-12 ~13:20 UTC).
|
||||
//
|
||||
// Pre-V5 path stays at hard nStakeMaxAge cap (regression-tested above).
|
||||
// V5 + pre-activation path is INTENTIONALLY uncapped (historical stakes
|
||||
// validate under the rules they were staked with).
|
||||
// V5 + post-activation path applies the 7-day soft cap.
|
||||
//
|
||||
// These tests use RAII to scope pindexBest swaps so a failed assertion
|
||||
// can't leave a stack pointer dangling in the global. The mock CBlockIndex
|
||||
// only needs nHeight populated; GetWeight reads nothing else from it.
|
||||
|
||||
// RAII guard: install a synthetic pindexBest on construction, restore the
|
||||
// prior value on destruction. Mandatory because boost CHECK failures
|
||||
// throw, and a manual pindexBest restore in the catch-less path leaks the
|
||||
// stack pointer into the global -- corrupting every subsequent test in
|
||||
// the suite.
|
||||
struct BestChainGuard
|
||||
{
|
||||
CBlockIndex* prev;
|
||||
explicit BestChainGuard(CBlockIndex* mock) : prev(pindexBest) { pindexBest = mock; }
|
||||
~BestChainGuard() { pindexBest = prev; }
|
||||
};
|
||||
|
||||
static const int64_t STAKE_AGE_SOFT_CAP_DAYS = 7;
|
||||
static const int64_t STAKE_AGE_SOFT_CAP_TEST_SECS = STAKE_AGE_SOFT_CAP_DAYS * 24 * 60 * 60;
|
||||
static const int64_t STAKE_AGE_SOFT_CAP_ACTIVATION_TEST = 1776000000;
|
||||
static const int64_t STAKE_AGE_MAX_TEST = 10 * 24 * 60 * 60; // 10 days -- past the 7-day cap
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_post_activation_capped_at_7_days)
|
||||
{
|
||||
// V5 + post-activation: a 10-day-old stake should be capped at 7 days.
|
||||
// This is the production code path for every stake on the live chain
|
||||
// since 2026-04-20 -- the highest-value missing test.
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5; // 17651, just at the fork
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST + (30 * 24 * 60 * 60); // 30 days post-activation
|
||||
int64_t tenDaysOld = now - nStakeMinAge - STAKE_AGE_MAX_TEST;
|
||||
|
||||
int64_t weight = GetWeight(tenDaysOld, now);
|
||||
BOOST_CHECK_EQUAL(weight, STAKE_AGE_SOFT_CAP_TEST_SECS);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_post_activation_below_cap_is_linear)
|
||||
{
|
||||
// V5 + post-activation: a stake younger than the 7-day cap should
|
||||
// return the raw nAge (capping only applies past the limit).
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5;
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST + (30 * 24 * 60 * 60);
|
||||
int64_t threeDaysOld = now - nStakeMinAge - (3 * 24 * 60 * 60);
|
||||
|
||||
int64_t weight = GetWeight(threeDaysOld, now);
|
||||
BOOST_CHECK_EQUAL(weight, 3 * 24 * 60 * 60);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_post_activation_exactly_7_days)
|
||||
{
|
||||
// V5 + post-activation: exactly at the cap should return cap value.
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5;
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST + (30 * 24 * 60 * 60);
|
||||
int64_t exactlySevenDays = now - nStakeMinAge - STAKE_AGE_SOFT_CAP_TEST_SECS;
|
||||
|
||||
int64_t weight = GetWeight(exactlySevenDays, now);
|
||||
BOOST_CHECK_EQUAL(weight, STAKE_AGE_SOFT_CAP_TEST_SECS);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_post_activation_one_second_past_cap)
|
||||
{
|
||||
// V5 + post-activation: 1 second past the cap should still be capped
|
||||
// (min() boundary semantics).
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5;
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST + (30 * 24 * 60 * 60);
|
||||
int64_t justPastCap = now - nStakeMinAge - STAKE_AGE_SOFT_CAP_TEST_SECS - 1;
|
||||
|
||||
int64_t weight = GetWeight(justPastCap, now);
|
||||
BOOST_CHECK_EQUAL(weight, STAKE_AGE_SOFT_CAP_TEST_SECS);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_pre_activation_is_uncapped)
|
||||
{
|
||||
// V5 active (height >= 17651) but stake timestamp is BEFORE the
|
||||
// activation gate. This is the "historical stakes validate under the
|
||||
// rules they were created with" path. A 30-day-old stake with
|
||||
// nIntervalEnd pre-activation should NOT be capped at 7 days or at
|
||||
// nStakeMaxAge -- it returns the raw nAge. This is intentional:
|
||||
// changing the cap retroactively would hard-fork historical blocks.
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5;
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST - 1; // 1 second before activation
|
||||
int64_t thirtyDaysOld = now - nStakeMinAge - (30 * 24 * 60 * 60);
|
||||
|
||||
int64_t weight = GetWeight(thirtyDaysOld, now);
|
||||
BOOST_CHECK_EQUAL(weight, 30 * 24 * 60 * 60); // raw nAge, no cap
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_exactly_at_activation_is_capped)
|
||||
{
|
||||
// V5 + nIntervalEnd exactly equal to the activation timestamp.
|
||||
// Boundary semantics: `>=` means AT the timestamp counts as activated,
|
||||
// so the 7-day cap applies. (Confirmed against the source: line 47
|
||||
// is `if (nIntervalEnd >= STAKE_AGE_SOFT_CAP_ACTIVATION) return min(...)`)
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5;
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST; // exactly at activation
|
||||
int64_t tenDaysOld = now - nStakeMinAge - STAKE_AGE_MAX_TEST;
|
||||
|
||||
int64_t weight = GetWeight(tenDaysOld, now);
|
||||
BOOST_CHECK_EQUAL(weight, STAKE_AGE_SOFT_CAP_TEST_SECS); // capped at 7 days
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_high_height_same_as_fork_height)
|
||||
{
|
||||
// V5 + post-activation at a height FAR past the fork (e.g. the live
|
||||
// DNS2 chain at height ~2.2M). Cap should still apply identically --
|
||||
// the soft cap doesn't weaken or strengthen with distance from fork.
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = 2500000; // well past FORK_HEIGHT_V5 and FORK_HEIGHT_V5_4
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST + (60 * 24 * 60 * 60);
|
||||
int64_t hundredDaysOld = now - nStakeMinAge - (100 * 24 * 60 * 60);
|
||||
|
||||
int64_t weight = GetWeight(hundredDaysOld, now);
|
||||
BOOST_CHECK_EQUAL(weight, STAKE_AGE_SOFT_CAP_TEST_SECS); // still 7 days, not 100
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(weight_v5_min_age_floor_still_applies)
|
||||
{
|
||||
// V5 + post-activation: nStakeMinAge floor still applies (a coin
|
||||
// younger than min_age returns 0 even if all gates pass). Confirms
|
||||
// the fork change didn't accidentally remove the floor.
|
||||
CBlockIndex mockBest;
|
||||
mockBest.nHeight = FORK_HEIGHT_V5;
|
||||
BestChainGuard guard(&mockBest);
|
||||
|
||||
int64_t now = STAKE_AGE_SOFT_CAP_ACTIVATION_TEST + (30 * 24 * 60 * 60);
|
||||
int64_t tooYoung = now - nStakeMinAge + 1; // 1 second short of min age
|
||||
|
||||
int64_t weight = GetWeight(tooYoung, now);
|
||||
BOOST_CHECK_EQUAL(weight, 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user