Coverage Report

Created: 2025-10-29 16:48

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