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:
Suhas Daftuar
2022-05-25 10:16:56 -04:00
parent 2bd9aa5a44
commit 1d4cfa4272
4 changed files with 125 additions and 4 deletions

View File

@@ -20,4 +20,18 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&);
/**
* Return false if the proof-of-work requirement specified by new_nbits at a
* given height is not possible, given the proof-of-work on the prior block as
* specified by old_nbits.
*
* This function only checks that the new value is within a factor of 4 of the
* old value for blocks at the difficulty adjustment interval, and otherwise
* requires the values to be the same.
*
* Always returns true on networks where min difficulty blocks are allowed,
* such as regtest/testnet.
*/
bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t height, uint32_t old_nbits, uint32_t new_nbits);
#endif // BITCOIN_POW_H