LCOV - code coverage report
Current view: top level - src - chain.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 102 110 92.7 %
Date: 2026-06-25 07:23:51 Functions: 14 15 93.3 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2019 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 <chain.h>
       7             : #include <util/time.h>
       8             : 
       9             : #include <tinyformat.h>
      10             : 
      11         182 : std::string CBlockFileInfo::ToString() const
      12             : {
      13         182 :     return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
      14           0 : }
      15             : 
      16           0 : std::string CBlockIndex::ToString() const
      17             : {
      18           0 :     return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
      19           0 :                      pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
      20           0 : }
      21             : 
      22       25065 : void CChain::SetTip(CBlockIndex& block)
      23             : {
      24       25065 :     CBlockIndex* pindex = &block;
      25       25065 :     vChain.resize(pindex->nHeight + 1);
      26      249969 :     while (pindex && vChain[pindex->nHeight] != pindex) {
      27      224904 :         vChain[pindex->nHeight] = pindex;
      28      224904 :         pindex = pindex->pprev;
      29             :     }
      30       25065 : }
      31             : 
      32         314 : CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
      33         314 :     int nStep = 1;
      34         314 :     std::vector<uint256> vHave;
      35         314 :     vHave.reserve(32);
      36             : 
      37         314 :     if (!pindex)
      38          27 :         pindex = Tip();
      39        5240 :     while (pindex) {
      40        5236 :         vHave.push_back(pindex->GetBlockHash());
      41             :         // Stop when we have added the genesis block.
      42        5236 :         if (pindex->nHeight == 0)
      43         310 :             break;
      44             :         // Exponentially larger steps back, plus the genesis block.
      45        4926 :         int nHeight = std::max(pindex->nHeight - nStep, 0);
      46        4926 :         if (Contains(pindex)) {
      47             :             // Use O(1) CChain index if possible.
      48        4070 :             pindex = (*this)[nHeight];
      49        4070 :         } else {
      50             :             // Otherwise, use O(log n) skiplist.
      51         856 :             pindex = pindex->GetAncestor(nHeight);
      52             :         }
      53        4926 :         if (vHave.size() > 10)
      54        2576 :             nStep *= 2;
      55             :     }
      56             : 
      57         314 :     return CBlockLocator(vHave);
      58         314 : }
      59             : 
      60       48672 : const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
      61       48672 :     if (pindex == nullptr) {
      62         179 :         return nullptr;
      63             :     }
      64       48493 :     if (pindex->nHeight > Height())
      65       24300 :         pindex = pindex->GetAncestor(Height());
      66       49245 :     while (pindex && !Contains(pindex))
      67         752 :         pindex = pindex->pprev;
      68       48493 :     return pindex;
      69       48672 : }
      70             : 
      71       10027 : CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const
      72             : {
      73       10027 :     std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
      74       10027 :     std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams,
      75      167820 :         [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; });
      76       10027 :     return (lower == vChain.end() ? nullptr : *lower);
      77             : }
      78             : 
      79             : /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
      80    86413533 : int static inline InvertLowestOne(int n) { return n & (n - 1); }
      81             : 
      82             : /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
      83    57621878 : int static inline GetSkipHeight(int height) {
      84    57621878 :     if (height < 2)
      85       12639 :         return 0;
      86             : 
      87             :     // Determine which height to jump back to. Any number strictly lower than height is acceptable,
      88             :     // but the following expression seems to perform well in simulations (max 110 steps to go back
      89             :     // up to 2**18 blocks).
      90    57609239 :     return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
      91    57621878 : }
      92             : 
      93     7350789 : const CBlockIndex* CBlockIndex::GetAncestor(int height) const
      94             : {
      95     7350789 :     if (height > nHeight || height < 0) {
      96      261560 :         return nullptr;
      97             :     }
      98             : 
      99     7089229 :     const CBlockIndex* pindexWalk = this;
     100     7089229 :     int heightWalk = nHeight;
     101    32849706 :     while (heightWalk > height) {
     102    25760477 :         int heightSkip = GetSkipHeight(heightWalk);
     103    25760477 :         int heightSkipPrev = GetSkipHeight(heightWalk - 1);
     104    28452802 :         if (pindexWalk->pskip != nullptr &&
     105    25733582 :             (heightSkip == height ||
     106    22337463 :              (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
     107     2692325 :                                        heightSkipPrev >= height)))) {
     108             :             // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
     109    12877848 :             pindexWalk = pindexWalk->pskip;
     110    12877848 :             heightWalk = heightSkip;
     111    12877848 :         } else {
     112    12882629 :             assert(pindexWalk->pprev);
     113    12882629 :             pindexWalk = pindexWalk->pprev;
     114    12882629 :             heightWalk--;
     115             :         }
     116             :     }
     117     7089229 :     return pindexWalk;
     118     7350789 : }
     119             : 
     120     6187398 : CBlockIndex* CBlockIndex::GetAncestor(int height)
     121             : {
     122     6187398 :     return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
     123             : }
     124             : 
     125     6101254 : void CBlockIndex::BuildSkip()
     126             : {
     127     6101254 :     if (pprev)
     128     6100924 :         pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
     129     6101254 : }
     130             : 
     131       35954 : arith_uint256 GetBlockProof(const CBlockIndex& block)
     132             : {
     133       35954 :     arith_uint256 bnTarget;
     134             :     bool fNegative;
     135             :     bool fOverflow;
     136       35954 :     bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
     137       35954 :     if (fNegative || fOverflow || bnTarget == 0)
     138           0 :         return 0;
     139             :     // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
     140             :     // as it's too large for an arith_uint256. However, as 2**256 is at least as large
     141             :     // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
     142             :     // or ~bnTarget / (bnTarget+1) + 1.
     143       35954 :     return (~bnTarget / (bnTarget + 1)) + 1;
     144       35954 : }
     145             : 
     146        1000 : int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
     147             : {
     148        1000 :     arith_uint256 r;
     149        1000 :     int sign = 1;
     150        1000 :     if (to.nChainWork > from.nChainWork) {
     151         495 :         r = to.nChainWork - from.nChainWork;
     152         495 :     } else {
     153         505 :         r = from.nChainWork - to.nChainWork;
     154         505 :         sign = -1;
     155             :     }
     156        1000 :     r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
     157        1000 :     if (r.bits() > 63) {
     158           0 :         return sign * std::numeric_limits<int64_t>::max();
     159             :     }
     160        1000 :     return sign * int64_t(r.GetLow64());
     161        1000 : }
     162             : 
     163             : /** Find the last common ancestor two blocks have.
     164             :  *  Both pa and pb must be non-nullptr. */
     165           1 : const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
     166           1 :     if (pa->nHeight > pb->nHeight) {
     167           0 :         pa = pa->GetAncestor(pb->nHeight);
     168           1 :     } else if (pb->nHeight > pa->nHeight) {
     169           1 :         pb = pb->GetAncestor(pa->nHeight);
     170           1 :     }
     171             : 
     172          11 :     while (pa != pb && pa && pb) {
     173          10 :         pa = pa->pprev;
     174          10 :         pb = pb->pprev;
     175             :     }
     176             : 
     177             :     // Eventually all chain branches meet at the genesis block.
     178           1 :     assert(pa == pb);
     179           1 :     return pa;
     180             : }

Generated by: LCOV version 1.16