From 1d4cfa4272cf2c8b980cc8762c1ff2220d3e8d51 Mon Sep 17 00:00:00 2001 From: Suhas Daftuar Date: Wed, 25 May 2022 10:16:56 -0400 Subject: 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. --- src/pow.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/pow.h') diff --git a/src/pow.h b/src/pow.h index 1d802cd01e..44b9d673ef 100644 --- a/src/pow.h +++ b/src/pow.h @@ -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 -- cgit v1.2.3