Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/chain.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2022 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/time.h>
9
10
std::string CBlockFileInfo::ToString() const
11
11.0k
{
12
11.0k
    return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
13
11.0k
}
14
15
std::string CBlockIndex::ToString() const
16
0
{
17
0
    return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
18
0
                     pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
19
0
}
20
21
void CChain::SetTip(CBlockIndex& block)
22
8.96M
{
23
8.96M
    CBlockIndex* pindex = &block;
24
8.96M
    vChain.resize(pindex->nHeight + 1);
25
699M
    while (pindex && vChain[pindex->nHeight] != pindex) {
  Branch (25:12): [True: 692M, False: 6.73M]
  Branch (25:22): [True: 690M, False: 2.22M]
26
690M
        vChain[pindex->nHeight] = pindex;
27
690M
        pindex = pindex->pprev;
28
690M
    }
29
8.96M
}
30
31
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
32
167k
{
33
167k
    int step = 1;
34
167k
    std::vector<uint256> have;
35
167k
    if (index == nullptr) return have;
  Branch (35:9): [True: 0, False: 167k]
36
37
167k
    have.reserve(32);
38
1.39M
    while (index) {
  Branch (38:12): [True: 1.39M, False: 0]
39
1.39M
        have.emplace_back(index->GetBlockHash());
40
1.39M
        if (index->nHeight == 0) break;
  Branch (40:13): [True: 167k, False: 1.22M]
41
        // Exponentially larger steps back, plus the genesis block.
42
1.22M
        int height = std::max(index->nHeight - step, 0);
43
        // Use skiplist.
44
1.22M
        index = index->GetAncestor(height);
45
1.22M
        if (have.size() > 10) step *= 2;
  Branch (45:13): [True: 543k, False: 684k]
46
1.22M
    }
47
167k
    return have;
48
167k
}
49
50
CBlockLocator GetLocator(const CBlockIndex* index)
51
167k
{
52
167k
    return CBlockLocator{LocatorEntries(index)};
53
167k
}
54
55
CBlockLocator CChain::GetLocator() const
56
29.2k
{
57
29.2k
    return ::GetLocator(Tip());
58
29.2k
}
59
60
4.47M
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
61
4.47M
    if (pindex == nullptr) {
  Branch (61:9): [True: 11.0k, False: 4.45M]
62
11.0k
        return nullptr;
63
11.0k
    }
64
4.45M
    if (pindex->nHeight > Height())
  Branch (64:9): [True: 2.23M, False: 2.22M]
65
2.23M
        pindex = pindex->GetAncestor(Height());
66
4.46M
    while (pindex && !Contains(pindex))
  Branch (66:12): [True: 4.45M, False: 11.0k]
  Branch (66:22): [True: 5.56k, False: 4.44M]
67
5.56k
        pindex = pindex->pprev;
68
4.45M
    return pindex;
69
4.47M
}
70
71
CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const
72
0
{
73
0
    std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
74
0
    std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams,
75
0
        [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; });
  Branch (75:94): [True: 0, False: 0]
  Branch (75:143): [True: 0, False: 0]
76
0
    return (lower == vChain.end() ? nullptr : *lower);
  Branch (76:13): [True: 0, False: 0]
77
0
}
78
79
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
80
419M
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
280M
int static inline GetSkipHeight(int height) {
84
280M
    if (height < 2)
  Branch (84:9): [True: 396k, False: 280M]
85
396k
        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
280M
    return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
  Branch (90:12): [True: 139M, False: 140M]
91
280M
}
92
93
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
94
79.2M
{
95
79.2M
    if (height > nHeight || height < 0) {
  Branch (95:9): [True: 1.00M, False: 78.2M]
  Branch (95:29): [True: 46.0M, False: 32.2M]
96
47.0M
        return nullptr;
97
47.0M
    }
98
99
32.2M
    const CBlockIndex* pindexWalk = this;
100
32.2M
    int heightWalk = nHeight;
101
171M
    while (heightWalk > height) {
  Branch (101:12): [True: 139M, False: 32.2M]
102
139M
        int heightSkip = GetSkipHeight(heightWalk);
103
139M
        int heightSkipPrev = GetSkipHeight(heightWalk - 1);
104
139M
        if (pindexWalk->pskip != nullptr &&
  Branch (104:13): [True: 139M, False: 330]
105
139M
            (heightSkip == height ||
  Branch (105:14): [True: 3.81M, False: 135M]
106
139M
             (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
  Branch (106:15): [True: 63.8M, False: 71.3M]
  Branch (106:40): [True: 11.5M, False: 52.3M]
107
63.8M
                                       heightSkipPrev >= height)))) {
  Branch (107:40): [True: 4.05M, False: 7.45M]
108
            // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
109
63.6M
            pindexWalk = pindexWalk->pskip;
110
63.6M
            heightWalk = heightSkip;
111
75.4M
        } else {
112
75.4M
            assert(pindexWalk->pprev);
  Branch (112:13): [True: 75.4M, False: 0]
113
75.4M
            pindexWalk = pindexWalk->pprev;
114
75.4M
            heightWalk--;
115
75.4M
        }
116
139M
    }
117
32.2M
    return pindexWalk;
118
32.2M
}
119
120
CBlockIndex* CBlockIndex::GetAncestor(int height)
121
7.74M
{
122
7.74M
    return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
123
7.74M
}
124
125
void CBlockIndex::BuildSkip()
126
2.23M
{
127
2.23M
    if (pprev)
  Branch (127:9): [True: 2.23M, False: 0]
128
2.23M
        pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
129
2.23M
}
130
131
arith_uint256 GetBlockProof(const CBlockIndex& block)
132
6.78M
{
133
6.78M
    arith_uint256 bnTarget;
134
6.78M
    bool fNegative;
135
6.78M
    bool fOverflow;
136
6.78M
    bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
137
6.78M
    if (fNegative || fOverflow || bnTarget == 0)
  Branch (137:9): [True: 0, False: 6.78M]
  Branch (137:22): [True: 0, False: 6.78M]
  Branch (137:35): [True: 0, False: 6.78M]
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
6.78M
    return (~bnTarget / (bnTarget + 1)) + 1;
144
6.78M
}
145
146
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
147
589
{
148
589
    arith_uint256 r;
149
589
    int sign = 1;
150
589
    if (to.nChainWork > from.nChainWork) {
  Branch (150:9): [True: 502, False: 87]
151
502
        r = to.nChainWork - from.nChainWork;
152
502
    } else {
153
87
        r = from.nChainWork - to.nChainWork;
154
87
        sign = -1;
155
87
    }
156
589
    r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
157
589
    if (r.bits() > 63) {
  Branch (157:9): [True: 0, False: 589]
158
0
        return sign * std::numeric_limits<int64_t>::max();
159
0
    }
160
589
    return sign * int64_t(r.GetLow64());
161
589
}
162
163
/** Find the last common ancestor two blocks have.
164
 *  Both pa and pb must be non-nullptr. */
165
18.7M
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
166
18.7M
    if (pa->nHeight > pb->nHeight) {
  Branch (166:9): [True: 0, False: 18.7M]
167
0
        pa = pa->GetAncestor(pb->nHeight);
168
18.7M
    } else if (pb->nHeight > pa->nHeight) {
  Branch (168:16): [True: 380k, False: 18.3M]
169
380k
        pb = pb->GetAncestor(pa->nHeight);
170
380k
    }
171
172
18.7M
    while (pa != pb && pa && pb) {
  Branch (172:12): [True: 4.69k, False: 18.7M]
  Branch (172:24): [True: 4.69k, False: 0]
  Branch (172:30): [True: 4.69k, False: 0]
173
4.69k
        pa = pa->pprev;
174
4.69k
        pb = pb->pprev;
175
4.69k
    }
176
177
    // Eventually all chain branches meet at the genesis block.
178
18.7M
    assert(pa == pb);
  Branch (178:5): [True: 18.7M, False: 0]
179
18.7M
    return pa;
180
18.7M
}