LCOV - code coverage report
Current view: top level - src - pow.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 53 53 100.0 %
Date: 2021-06-29 14:35:33 Functions: 3 3 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 30 32 93.8 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2018 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                 :            : #include <pow.h>
#       7                 :            : 
#       8                 :            : #include <arith_uint256.h>
#       9                 :            : #include <chain.h>
#      10                 :            : #include <primitives/block.h>
#      11                 :            : #include <uint256.h>
#      12                 :            : 
#      13                 :            : unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
#      14                 :     161836 : {
#      15                 :     161836 :     assert(pindexLast != nullptr);
#      16                 :     161836 :     unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
#      17                 :            : 
#      18                 :            :     // Only change once per difficulty adjustment interval
#      19         [ +  + ]:     161836 :     if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
#      20                 :     161824 :     {
#      21         [ +  + ]:     161824 :         if (params.fPowAllowMinDifficultyBlocks)
#      22                 :     161514 :         {
#      23                 :            :             // Special difficulty rule for testnet:
#      24                 :            :             // If the new block's timestamp is more than 2* 10 minutes
#      25                 :            :             // then allow mining of a min-difficulty block.
#      26         [ +  + ]:     161514 :             if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
#      27                 :       3524 :                 return nProofOfWorkLimit;
#      28                 :     157990 :             else
#      29                 :     157990 :             {
#      30                 :            :                 // Return the last non-special-min-difficulty-rules-block
#      31                 :     157990 :                 const CBlockIndex* pindex = pindexLast;
#      32 [ +  + ][ +  + ]:   66307404 :                 while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
#                 [ +  - ]
#      33                 :   66149414 :                     pindex = pindex->pprev;
#      34                 :     157990 :                 return pindex->nBits;
#      35                 :     157990 :             }
#      36                 :        310 :         }
#      37                 :        310 :         return pindexLast->nBits;
#      38                 :        310 :     }
#      39                 :            : 
#      40                 :            :     // Go back by what we want to be 14 days worth of blocks
#      41                 :         12 :     int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
#      42                 :         12 :     assert(nHeightFirst >= 0);
#      43                 :         12 :     const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
#      44                 :         12 :     assert(pindexFirst);
#      45                 :            : 
#      46                 :         12 :     return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
#      47                 :         12 : }
#      48                 :            : 
#      49                 :            : unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
#      50                 :         20 : {
#      51         [ +  + ]:         20 :     if (params.fPowNoRetargeting)
#      52                 :         12 :         return pindexLast->nBits;
#      53                 :            : 
#      54                 :            :     // Limit adjustment step
#      55                 :          8 :     int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
#      56         [ +  + ]:          8 :     if (nActualTimespan < params.nPowTargetTimespan/4)
#      57                 :          2 :         nActualTimespan = params.nPowTargetTimespan/4;
#      58         [ +  + ]:          8 :     if (nActualTimespan > params.nPowTargetTimespan*4)
#      59                 :          2 :         nActualTimespan = params.nPowTargetTimespan*4;
#      60                 :            : 
#      61                 :            :     // Retarget
#      62                 :          8 :     const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
#      63                 :          8 :     arith_uint256 bnNew;
#      64                 :          8 :     bnNew.SetCompact(pindexLast->nBits);
#      65                 :          8 :     bnNew *= nActualTimespan;
#      66                 :          8 :     bnNew /= params.nPowTargetTimespan;
#      67                 :            : 
#      68         [ +  + ]:          8 :     if (bnNew > bnPowLimit)
#      69                 :          2 :         bnNew = bnPowLimit;
#      70                 :            : 
#      71                 :          8 :     return bnNew.GetCompact();
#      72                 :          8 : }
#      73                 :            : 
#      74                 :            : bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
#      75                 :     791976 : {
#      76                 :     791976 :     bool fNegative;
#      77                 :     791976 :     bool fOverflow;
#      78                 :     791976 :     arith_uint256 bnTarget;
#      79                 :            : 
#      80                 :     791976 :     bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
#      81                 :            : 
#      82                 :            :     // Check range
#      83 [ +  + ][ +  + ]:     791976 :     if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
#         [ +  + ][ -  + ]
#                 [ +  + ]
#      84                 :          8 :         return false;
#      85                 :            : 
#      86                 :            :     // Check proof of work matches claimed amount
#      87         [ +  + ]:     791968 :     if (UintToArith256(hash) > bnTarget)
#      88                 :     440134 :         return false;
#      89                 :            : 
#      90                 :     351834 :     return true;
#      91                 :     351834 : }

Generated by: LCOV version 1.14