Coverage Report

Created: 2025-09-08 17:06

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-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
1.37k
{
12
1.37k
    return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
13
1.37k
}
14
15
std::string CBlockIndex::ToString() const
16
196
{
17
196
    return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
18
196
                     pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
19
196
}
20
21
void CChain::SetTip(CBlockIndex& block)
22
558k
{
23
558k
    CBlockIndex* pindex = &block;
24
558k
    vChain.resize(pindex->nHeight + 1);
25
34.6M
    while (pindex && vChain[pindex->nHeight] != pindex) {
26
34.0M
        vChain[pindex->nHeight] = pindex;
27
34.0M
        pindex = pindex->pprev;
28
34.0M
    }
29
558k
}
30
31
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
32
258k
{
33
258k
    int step = 1;
34
258k
    std::vector<uint256> have;
35
258k
    if (index == nullptr) return have;
36
37
258k
    have.reserve(32);
38
1.21M
    while (index) {
39
1.21M
        have.emplace_back(index->GetBlockHash());
40
1.21M
        if (index->nHeight == 0) break;
41
        // Exponentially larger steps back, plus the genesis block.
42
959k
        int height = std::max(index->nHeight - step, 0);
43
        // Use skiplist.
44
959k
        index = index->GetAncestor(height);
45
959k
        if (have.size() > 10) step *= 2;
46
959k
    }
47
258k
    return have;
48
258k
}
49
50
CBlockLocator GetLocator(const CBlockIndex* index)
51
81.6k
{
52
81.6k
    return CBlockLocator{LocatorEntries(index)};
53
81.6k
}
54
55
227k
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
56
227k
    if (pindex == nullptr) {
57
1.37k
        return nullptr;
58
1.37k
    }
59
225k
    if (pindex->nHeight > Height())
60
113k
        pindex = pindex->GetAncestor(Height());
61
225k
    while (pindex && !Contains(pindex))
62
0
        pindex = pindex->pprev;
63
225k
    return pindex;
64
227k
}
65
66
CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const
67
0
{
68
0
    std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
69
0
    std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams,
70
0
        [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; });
71
0
    return (lower == vChain.end() ? nullptr : *lower);
72
0
}
73
74
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
75
31.7M
int static inline InvertLowestOne(int n) { return n & (n - 1); }
76
77
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
78
21.3M
int static inline GetSkipHeight(int height) {
79
21.3M
    if (height < 2)
80
162k
        return 0;
81
82
    // Determine which height to jump back to. Any number strictly lower than height is acceptable,
83
    // but the following expression seems to perform well in simulations (max 110 steps to go back
84
    // up to 2**18 blocks).
85
21.1M
    return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
86
21.3M
}
87
88
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
89
4.89M
{
90
4.89M
    if (height > nHeight || height < 0) {
91
2.06M
        return nullptr;
92
2.06M
    }
93
94
2.83M
    const CBlockIndex* pindexWalk = this;
95
2.83M
    int heightWalk = nHeight;
96
13.3M
    while (heightWalk > height) {
97
10.5M
        int heightSkip = GetSkipHeight(heightWalk);
98
10.5M
        int heightSkipPrev = GetSkipHeight(heightWalk - 1);
99
10.5M
        if (pindexWalk->pskip != nullptr &&
100
10.5M
            (heightSkip == height ||
101
10.2M
             (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
102
5.06M
                                       heightSkipPrev >= height)))) {
103
            // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
104
5.06M
            pindexWalk = pindexWalk->pskip;
105
5.06M
            heightWalk = heightSkip;
106
5.48M
        } else {
107
5.48M
            assert(pindexWalk->pprev);
108
5.48M
            pindexWalk = pindexWalk->pprev;
109
5.48M
            heightWalk--;
110
5.48M
        }
111
10.5M
    }
112
2.83M
    return pindexWalk;
113
2.83M
}
114
115
CBlockIndex* CBlockIndex::GetAncestor(int height)
116
1.81M
{
117
1.81M
    return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
118
1.81M
}
119
120
void CBlockIndex::BuildSkip()
121
220k
{
122
220k
    if (pprev)
123
220k
        pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
124
220k
}
125
126
arith_uint256 GetBlockProof(const CBlockIndex& block)
127
725k
{
128
725k
    arith_uint256 bnTarget;
129
725k
    bool fNegative;
130
725k
    bool fOverflow;
131
725k
    bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
132
725k
    if (fNegative || fOverflow || bnTarget == 0)
133
87.9k
        return 0;
134
    // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
135
    // as it's too large for an arith_uint256. However, as 2**256 is at least as large
136
    // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
137
    // or ~bnTarget / (bnTarget+1) + 1.
138
637k
    return (~bnTarget / (bnTarget + 1)) + 1;
139
725k
}
140
141
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
142
57.7k
{
143
57.7k
    arith_uint256 r;
144
57.7k
    int sign = 1;
145
57.7k
    if (to.nChainWork > from.nChainWork) {
146
14.4k
        r = to.nChainWork - from.nChainWork;
147
43.2k
    } else {
148
43.2k
        r = from.nChainWork - to.nChainWork;
149
43.2k
        sign = -1;
150
43.2k
    }
151
57.7k
    r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
152
57.7k
    if (r.bits() > 63) {
153
10.2k
        return sign * std::numeric_limits<int64_t>::max();
154
10.2k
    }
155
47.5k
    return sign * int64_t(r.GetLow64());
156
57.7k
}
157
158
/** Find the last common ancestor two blocks have.
159
 *  Both pa and pb must be non-nullptr. */
160
0
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
161
0
    if (pa->nHeight > pb->nHeight) {
162
0
        pa = pa->GetAncestor(pb->nHeight);
163
0
    } else if (pb->nHeight > pa->nHeight) {
164
0
        pb = pb->GetAncestor(pa->nHeight);
165
0
    }
166
167
0
    while (pa != pb && pa && pb) {
168
0
        pa = pa->pprev;
169
0
        pb = pb->pprev;
170
0
    }
171
172
    // Eventually all chain branches meet at the genesis block.
173
0
    assert(pa == pb);
174
0
    return pa;
175
0
}