Coverage Report

Created: 2025-03-27 15:35

/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/time.h>
9
10
std::string CBlockFileInfo::ToString() const
11
2.82k
{
12
2.82k
    return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
13
2.82k
}
14
15
std::string CBlockIndex::ToString() const
16
277
{
17
277
    return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
18
277
                     pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
19
277
}
20
21
void CChain::SetTip(CBlockIndex& block)
22
878k
{
23
878k
    CBlockIndex* pindex = &block;
24
878k
    vChain.resize(pindex->nHeight + 1);
25
44.8M
    while (pindex && vChain[pindex->nHeight] != pindex) {
26
43.9M
        vChain[pindex->nHeight] = pindex;
27
43.9M
        pindex = pindex->pprev;
28
43.9M
    }
29
878k
}
30
31
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
32
562k
{
33
562k
    int step = 1;
34
562k
    std::vector<uint256> have;
35
562k
    if (index == nullptr) return have;
36
37
562k
    have.reserve(32);
38
3.50M
    while (index) {
39
3.50M
        have.emplace_back(index->GetBlockHash());
40
3.50M
        if (index->nHeight == 0) break;
41
        // Exponentially larger steps back, plus the genesis block.
42
2.94M
        int height = std::max(index->nHeight - step, 0);
43
        // Use skiplist.
44
2.94M
        index = index->GetAncestor(height);
45
2.94M
        if (have.size() > 10) step *= 2;
46
2.94M
    }
47
562k
    return have;
48
562k
}
49
50
CBlockLocator GetLocator(const CBlockIndex* index)
51
226k
{
52
226k
    return CBlockLocator{LocatorEntries(index)};
53
226k
}
54
55
CBlockLocator CChain::GetLocator() const
56
200k
{
57
200k
    return ::GetLocator(Tip());
58
200k
}
59
60
327k
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
61
327k
    if (pindex == nullptr) {
62
2.82k
        return nullptr;
63
2.82k
    }
64
325k
    if (pindex->nHeight > Height())
65
165k
        pindex = pindex->GetAncestor(Height());
66
325k
    while (pindex && !Contains(pindex))
67
0
        pindex = pindex->pprev;
68
325k
    return pindex;
69
327k
}
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; });
76
0
    return (lower == vChain.end() ? nullptr : *lower);
77
0
}
78
79
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
80
52.8M
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
35.7M
int static inline GetSkipHeight(int height) {
84
35.7M
    if (height < 2)
85
459k
        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
35.2M
    return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
91
35.7M
}
92
93
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
94
7.61M
{
95
7.61M
    if (height > nHeight || height < 0) {
96
2.16M
        return nullptr;
97
2.16M
    }
98
99
5.45M
    const CBlockIndex* pindexWalk = this;
100
5.45M
    int heightWalk = nHeight;
101
23.1M
    while (heightWalk > height) {
102
17.6M
        int heightSkip = GetSkipHeight(heightWalk);
103
17.6M
        int heightSkipPrev = GetSkipHeight(heightWalk - 1);
104
17.6M
        if (pindexWalk->pskip != nullptr &&
105
17.6M
            (heightSkip == height ||
106
16.9M
             (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
107
7.62M
                                       heightSkipPrev >= height)))) {
108
            // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
109
7.62M
            pindexWalk = pindexWalk->pskip;
110
7.62M
            heightWalk = heightSkip;
111
10.0M
        } else {
112
10.0M
            assert(pindexWalk->pprev);
113
10.0M
            pindexWalk = pindexWalk->pprev;
114
10.0M
            heightWalk--;
115
10.0M
        }
116
17.6M
    }
117
5.45M
    return pindexWalk;
118
5.45M
}
119
120
CBlockIndex* CBlockIndex::GetAncestor(int height)
121
3.69M
{
122
3.69M
    return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
123
3.69M
}
124
125
void CBlockIndex::BuildSkip()
126
436k
{
127
436k
    if (pprev)
128
435k
        pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
129
436k
}
130
131
arith_uint256 GetBlockProof(const CBlockIndex& block)
132
1.64M
{
133
1.64M
    arith_uint256 bnTarget;
134
1.64M
    bool fNegative;
135
1.64M
    bool fOverflow;
136
1.64M
    bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
137
1.64M
    if (fNegative || fOverflow || bnTarget == 0)
138
349k
        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
1.29M
    return (~bnTarget / (bnTarget + 1)) + 1;
144
1.64M
}
145
146
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
147
205k
{
148
205k
    arith_uint256 r;
149
205k
    int sign = 1;
150
205k
    if (to.nChainWork > from.nChainWork) {
151
52.0k
        r = to.nChainWork - from.nChainWork;
152
153k
    } else {
153
153k
        r = from.nChainWork - to.nChainWork;
154
153k
        sign = -1;
155
153k
    }
156
205k
    r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
157
205k
    if (r.bits() > 63) {
158
29.4k
        return sign * std::numeric_limits<int64_t>::max();
159
29.4k
    }
160
176k
    return sign * int64_t(r.GetLow64());
161
205k
}
162
163
/** Find the last common ancestor two blocks have.
164
 *  Both pa and pb must be non-nullptr. */
165
41.6k
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
166
41.6k
    if (pa->nHeight > pb->nHeight) {
167
0
        pa = pa->GetAncestor(pb->nHeight);
168
41.6k
    } else if (pb->nHeight > pa->nHeight) {
169
38.8k
        pb = pb->GetAncestor(pa->nHeight);
170
38.8k
    }
171
172
41.6k
    while (pa != pb && pa && pb) {
173
11
        pa = pa->pprev;
174
11
        pb = pb->pprev;
175
11
    }
176
177
    // Eventually all chain branches meet at the genesis block.
178
41.6k
    assert(pa == pb);
179
41.6k
    return pa;
180
41.6k
}