1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 6 #ifndef BITCOIN_POW_H
7 #define BITCOIN_POW_H
8 9 #include <consensus/params.h>
10 11 #include <cstdint>
12 13 class CBlockHeader;
14 class CBlockIndex;
15 class uint256;
16 class arith_uint256;
17 18 /**
19 * Convert nBits value to target.
20 *
21 * @param[in] nBits compact representation of the target
22 * @param[in] pow_limit PoW limit (consensus parameter)
23 *
24 * @return the proof-of-work target or nullopt if the nBits value
25 * is invalid (due to overflow or exceeding pow_limit)
26 */
27 std::optional<arith_uint256> DeriveTarget(unsigned int nBits, uint256 pow_limit);
28 29 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&);
30 unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);
31 32 /** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
33 bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&);
34 bool CheckProofOfWorkImpl(uint256 hash, unsigned int nBits, const Consensus::Params&);
35 36 /**
37 * Return false if the proof-of-work requirement specified by new_nbits at a
38 * given height is not possible, given the proof-of-work on the prior block as
39 * specified by old_nbits.
40 *
41 * This function only checks that the new value is within a factor of 4 of the
42 * old value for blocks at the difficulty adjustment interval, and otherwise
43 * requires the values to be the same.
44 *
45 * Always returns true on networks where min difficulty blocks are allowed,
46 * such as regtest/testnet.
47 */
48 bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t height, uint32_t old_nbits, uint32_t new_nbits);
49 50 #endif // BITCOIN_POW_H
51