Coverage Report

Created: 2026-06-09 19:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/chain.cpp
Line
Count
Source
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
#include <chain.h>
7
#include <tinyformat.h>
8
#include <util/check.h>
9
10
std::string CBlockIndex::ToString() const
11
0
{
12
0
    return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
13
0
                     pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
14
0
}
15
16
void CChain::SetTip(CBlockIndex& block)
17
0
{
18
0
    CBlockIndex* pindex = &block;
19
0
    vChain.resize(pindex->nHeight + 1);
20
0
    while (pindex && vChain[pindex->nHeight] != pindex) {
  Branch (20:12): [True: 0, False: 0]
  Branch (20:22): [True: 0, False: 0]
21
0
        vChain[pindex->nHeight] = pindex;
22
0
        pindex = pindex->pprev;
23
0
    }
24
0
}
25
26
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
27
0
{
28
0
    int step = 1;
29
0
    std::vector<uint256> have;
30
0
    if (index == nullptr) return have;
  Branch (30:9): [True: 0, False: 0]
31
32
0
    have.reserve(32);
33
0
    while (index) {
  Branch (33:12): [True: 0, False: 0]
34
0
        have.emplace_back(index->GetBlockHash());
35
0
        if (index->nHeight == 0) break;
  Branch (35:13): [True: 0, False: 0]
36
        // Exponentially larger steps back, plus the genesis block.
37
0
        int height = std::max(index->nHeight - step, 0);
38
        // Use skiplist.
39
0
        index = index->GetAncestor(height);
40
0
        if (have.size() > 10) step *= 2;
  Branch (40:13): [True: 0, False: 0]
41
0
    }
42
0
    return have;
43
0
}
44
45
CBlockLocator GetLocator(const CBlockIndex* index)
46
0
{
47
0
    return CBlockLocator{LocatorEntries(index)};
48
0
}
49
50
const CBlockIndex* CChain::FindFork(const CBlockIndex& index) const
51
0
{
52
0
    const auto* pindex{&index};
53
0
    if (pindex->nHeight > Height())
  Branch (53:9): [True: 0, False: 0]
54
0
        pindex = pindex->GetAncestor(Height());
55
0
    while (pindex && !Contains(*pindex))
  Branch (55:12): [True: 0, False: 0]
  Branch (55:22): [True: 0, False: 0]
56
0
        pindex = pindex->pprev;
57
0
    return pindex;
58
0
}
59
60
CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const
61
0
{
62
0
    std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
63
0
    std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams,
64
0
        [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; });
  Branch (64:94): [True: 0, False: 0]
  Branch (64:143): [True: 0, False: 0]
65
0
    return (lower == vChain.end() ? nullptr : *lower);
  Branch (65:13): [True: 0, False: 0]
66
0
}
67
68
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
69
0
int static inline InvertLowestOne(int n) { return n & (n - 1); }
70
71
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
72
0
int static inline GetSkipHeight(int height) {
73
0
    if (height < 2)
  Branch (73:9): [True: 0, False: 0]
74
0
        return 0;
75
76
    // Determine which height to jump back to. Any number strictly lower than height is acceptable,
77
    // but the following expression seems to perform well in simulations (max 110 steps to go back
78
    // up to 2**18 blocks).
79
0
    return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
  Branch (79:12): [True: 0, False: 0]
80
0
}
81
82
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
83
0
{
84
0
    if (height > nHeight || height < 0) {
  Branch (84:9): [True: 0, False: 0]
  Branch (84:29): [True: 0, False: 0]
85
0
        return nullptr;
86
0
    }
87
88
0
    const CBlockIndex* pindexWalk = this;
89
0
    int heightWalk = nHeight;
90
0
    while (heightWalk > height) {
  Branch (90:12): [True: 0, False: 0]
91
0
        int heightSkip = GetSkipHeight(heightWalk);
92
0
        int heightSkipPrev = GetSkipHeight(heightWalk - 1);
93
0
        if (pindexWalk->pskip != nullptr &&
  Branch (93:13): [True: 0, False: 0]
94
0
            (heightSkip == height ||
  Branch (94:14): [True: 0, False: 0]
95
0
             (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
  Branch (95:15): [True: 0, False: 0]
  Branch (95:40): [True: 0, False: 0]
96
0
                                       heightSkipPrev >= height)))) {
  Branch (96:40): [True: 0, False: 0]
97
            // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
98
0
            pindexWalk = pindexWalk->pskip;
99
0
            heightWalk = heightSkip;
100
0
        } else {
101
0
            assert(pindexWalk->pprev);
  Branch (101:13): [True: 0, False: 0]
102
0
            pindexWalk = pindexWalk->pprev;
103
0
            heightWalk--;
104
0
        }
105
0
    }
106
0
    return pindexWalk;
107
0
}
108
109
CBlockIndex* CBlockIndex::GetAncestor(int height)
110
0
{
111
0
    return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
112
0
}
113
114
void CBlockIndex::BuildSkip()
115
0
{
116
0
    if (pprev)
  Branch (116:9): [True: 0, False: 0]
117
0
        pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
118
0
}
119
120
arith_uint256 GetBitsProof(uint32_t bits)
121
0
{
122
0
    arith_uint256 bnTarget;
123
0
    bool fNegative;
124
0
    bool fOverflow;
125
0
    bnTarget.SetCompact(bits, &fNegative, &fOverflow);
126
0
    if (fNegative || fOverflow || bnTarget == 0)
  Branch (126:9): [True: 0, False: 0]
  Branch (126:22): [True: 0, False: 0]
  Branch (126:35): [True: 0, False: 0]
127
0
        return 0;
128
    // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
129
    // as it's too large for an arith_uint256. However, as 2**256 is at least as large
130
    // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
131
    // or ~bnTarget / (bnTarget+1) + 1.
132
0
    return (~bnTarget / (bnTarget + 1)) + 1;
133
0
}
134
135
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
136
0
{
137
0
    arith_uint256 r;
138
0
    int sign = 1;
139
0
    if (to.nChainWork > from.nChainWork) {
  Branch (139:9): [True: 0, False: 0]
140
0
        r = to.nChainWork - from.nChainWork;
141
0
    } else {
142
0
        r = from.nChainWork - to.nChainWork;
143
0
        sign = -1;
144
0
    }
145
0
    r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
146
0
    if (r.bits() > 63) {
  Branch (146:9): [True: 0, False: 0]
147
0
        return sign * std::numeric_limits<int64_t>::max();
148
0
    }
149
0
    return sign * int64_t(r.GetLow64());
150
0
}
151
152
/** Find the last common ancestor two blocks have.
153
 *  Both pa and pb must be non-nullptr. */
154
0
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
155
    // First rewind to the last common height (the forking point cannot be past one of the two).
156
0
    if (pa->nHeight > pb->nHeight) {
  Branch (156:9): [True: 0, False: 0]
157
0
        pa = pa->GetAncestor(pb->nHeight);
158
0
    } else if (pb->nHeight > pa->nHeight) {
  Branch (158:16): [True: 0, False: 0]
159
0
        pb = pb->GetAncestor(pa->nHeight);
160
0
    }
161
0
    while (pa != pb) {
  Branch (161:12): [True: 0, False: 0]
162
        // Jump back until pa and pb have a common "skip" ancestor.
163
0
        while (pa->pskip != pb->pskip) {
  Branch (163:16): [True: 0, False: 0]
164
            // This logic relies on the property that equal-height blocks have equal-height skip
165
            // pointers.
166
0
            Assume(pa->nHeight == pb->nHeight);
167
0
            Assume(pa->pskip->nHeight == pb->pskip->nHeight);
168
0
            pa = pa->pskip;
169
0
            pb = pb->pskip;
170
0
        }
171
        // At this point, pa and pb are different, but have equal pskip. The forking point lies in
172
        // between pa/pb on the one end, and pa->pskip/pb->pskip on the other end.
173
0
        pa = pa->pprev;
174
0
        pb = pb->pprev;
175
0
    }
176
0
    return pa;
177
0
}