Merge bitcoin/bitcoin#36231: test: add pow tests covering live mutants
What changed, and why it matters
This commit only adds new test cases to Bitcoin Core's proof-of-work test file. It does not change any production code. The tests are designed to detect if future code changes accidentally break subtle consensus rules around difficulty retargeting. There is no vulnerability being fixed here.
No action required. This is a defensive test-only change. Reviewers may optionally verify the new test vectors match the intended consensus semantics described in the PR.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The merge commit 368c138231526a9437ae5166f8231d8602bc0908 adds four new BOOST_AUTO_TEST_CASE unit tests in src/test/pow_tests.cpp. These tests cover edge cases in pow.cpp’s difficulty calculation and validation logic: (1) compact-form rounding of the lower difficulty bound, (2) negative actual timespans in CalculateNextWorkRequired, (3) use of first-block timestamp vs. median-time-past in GetNextWorkRequired, and (4) rejection of nBits changes at non-retarget heights. The commit message includes hypothetical mutants that the tests would catch, but the actual diff is test-only.
Changed components
src/test/pow_tests.cppInspect captured patch +67 / −0
### src/test/pow_tests.cpp
@@ -64,6 +64,36 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
}
+/* Test that the largest permitted difficulty increase is not itself rejected
+ * when the new target is not exactly representable in compact form. */
+BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_rounding)
+{
+ const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
+ CBlockIndex pindexLast;
+ pindexLast.nHeight = 68543;
+ pindexLast.nTime = 1279297671;
+ pindexLast.nBits = 0x1c05a3f5; // Mantissa is not a multiple of four
+ int64_t nLastRetargetTime = pindexLast.nTime - consensus.nPowTargetTimespan/4;
+ unsigned int expected_nbits = 0x1c0168fdU;
+ BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, consensus), expected_nbits);
+ BOOST_CHECK(PermittedDifficultyTransition(consensus, pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
+}
+
+/* Test the lower bound for a negative actual time taken, which is possible
+ * because timestamps only have to exceed the median of the previous eleven. */
+BOOST_AUTO_TEST_CASE(get_next_work_negative_actual)
+{
+ const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
+ int64_t nLastRetargetTime = 1279297671; // Later than pindexLast.nTime
+ CBlockIndex pindexLast;
+ pindexLast.nHeight = 68543;
+ pindexLast.nTime = 1279008237;
+ pindexLast.nBits = 0x1c05a3f4;
+ unsigned int expected_nbits = 0x1c0168fdU;
+ BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, consensus), expected_nbits);
+ BOOST_CHECK(PermittedDifficultyTransition(consensus, pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
+}
+
/* Test the constraint on the upper bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
{
@@ -81,6 +111,43 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
}
+/* Test that the retarget window starts at the timestamp of the first block
+ * of the window, not its median time past. */
+BOOST_AUTO_TEST_CASE(get_next_work_first_block_time)
+{
+ const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
+ const int64_t interval{consensus.DifficultyAdjustmentInterval()};
+
+ // Two retarget windows, so that the first block of the second one has
+ // ancestors and hence a median time past earlier than its own time.
+ std::vector<CBlockIndex> blocks(2 * interval);
+ for (int64_t i = 0; i < 2 * interval; i++) {
+ blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
+ blocks[i].nHeight = i;
+ blocks[i].nTime = 1269211443 + i * consensus.nPowTargetSpacing;
+ blocks[i].nBits = 0x1c05a3f4;
+ }
+
+ const CBlockIndex& first{blocks[interval]};
+ const CBlockIndex& last{blocks[2 * interval - 1]};
+ BOOST_CHECK_NE(first.GetBlockTime(), first.GetMedianTimePast());
+ unsigned int expected_nbits = 0x1c05a33c;
+ BOOST_CHECK_EQUAL(GetNextWorkRequired(&last, nullptr, consensus), expected_nbits);
+ BOOST_CHECK(PermittedDifficultyTransition(consensus, last.nHeight+1, last.nBits, expected_nbits));
+}
+
+/* Test that nbits is only permitted to change at a difficulty adjustment. */
+BOOST_AUTO_TEST_CASE(permitted_difficulty_transition_no_retarget)
+{
+ const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
+ const int64_t height{consensus.DifficultyAdjustmentInterval() + 1}; // Not a retarget height
+
+ const unsigned int nbits{0x1c05a3f4};
+ BOOST_CHECK(PermittedDifficultyTransition(consensus, height, nbits, nbits));
+ BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits - 1));
+ BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits + 1));
+}
+
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.