mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
Add function to validate difficulty changes
The rule against difficulty adjustments changing by more than a factor of 4 can be helpful for anti-DoS measures in contexts where we lack a full headers chain, so expose this functionality separately and in the narrow case where we only know the height, new value, and old value. Includes fuzz test by Martin Zumsande.
This commit is contained in:
@@ -20,7 +20,14 @@ BOOST_AUTO_TEST_CASE(get_next_work)
|
||||
pindexLast.nHeight = 32255;
|
||||
pindexLast.nTime = 1262152739; // Block #32255
|
||||
pindexLast.nBits = 0x1d00ffff;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00d86aU);
|
||||
|
||||
// Here (and below): expected_nbits is calculated in
|
||||
// CalculateNextWorkRequired(); redoing the calculation here would be just
|
||||
// reimplementing the same code that is written in pow.cpp. Rather than
|
||||
// copy that code, we just hardcode the expected result.
|
||||
unsigned int expected_nbits = 0x1d00d86aU;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
|
||||
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
|
||||
}
|
||||
|
||||
/* Test the constraint on the upper bound for next work */
|
||||
@@ -32,7 +39,9 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
|
||||
pindexLast.nHeight = 2015;
|
||||
pindexLast.nTime = 1233061996; // Block #2015
|
||||
pindexLast.nBits = 0x1d00ffff;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00ffffU);
|
||||
unsigned int expected_nbits = 0x1d00ffffU;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
|
||||
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
|
||||
}
|
||||
|
||||
/* Test the constraint on the lower bound for actual time taken */
|
||||
@@ -44,7 +53,12 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
|
||||
pindexLast.nHeight = 68543;
|
||||
pindexLast.nTime = 1279297671; // Block #68543
|
||||
pindexLast.nBits = 0x1c05a3f4;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1c0168fdU);
|
||||
unsigned int expected_nbits = 0x1c0168fdU;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
|
||||
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
|
||||
// Test that reducing nbits further would not be a PermittedDifficultyTransition.
|
||||
unsigned int invalid_nbits = expected_nbits-1;
|
||||
BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
|
||||
}
|
||||
|
||||
/* Test the constraint on the upper bound for actual time taken */
|
||||
@@ -56,7 +70,12 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
|
||||
pindexLast.nHeight = 46367;
|
||||
pindexLast.nTime = 1269211443; // Block #46367
|
||||
pindexLast.nBits = 0x1c387f6f;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fdU);
|
||||
unsigned int expected_nbits = 0x1d00e1fdU;
|
||||
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
|
||||
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
|
||||
// Test that increasing nbits further would not be a PermittedDifficultyTransition.
|
||||
unsigned int invalid_nbits = expected_nbits+1;
|
||||
BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
|
||||
|
||||
Reference in New Issue
Block a user