fuzz coverage

Coverage Report

Created: 2025-08-28 15:26

/Users/eugenesiegel/btc/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
7.28k
{
12
7.28k
    return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
Line
Count
Source
1172
7.28k
#define strprintf tfm::format
13
7.28k
}
14
15
std::string CBlockIndex::ToString() const
16
0
{
17
0
    return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
Line
Count
Source
1172
0
#define strprintf tfm::format
18
0
                     pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
19
0
}
20
21
void CChain::SetTip(CBlockIndex& block)
22
154k
{
23
154k
    CBlockIndex* pindex = &block;
24
154k
    vChain.resize(pindex->nHeight + 1);
25
31.2M
    while (pindex && 
vChain[pindex->nHeight] != pindex31.0M
) {
26
31.0M
        vChain[pindex->nHeight] = pindex;
27
31.0M
        pindex = pindex->pprev;
28
31.0M
    }
29
154k
}
30
31
std::vector<uint256> LocatorEntries(const CBlockIndex* index)
32
21.4k
{
33
21.4k
    int step = 1;
34
21.4k
    std::vector<uint256> have;
35
21.4k
    if (index == nullptr) 
return have0
;
36
37
21.4k
    have.reserve(32);
38
407k
    while (index) {
39
407k
        have.emplace_back(index->GetBlockHash());
40
407k
        if (index->nHeight == 0) 
break21.4k
;
41
        // Exponentially larger steps back, plus the genesis block.
42
386k
        int height = std::max(index->nHeight - step, 0);
43
        // Use skiplist.
44
386k
        index = index->GetAncestor(height);
45
386k
        if (have.size() > 10) 
step *= 2171k
;
46
386k
    }
47
21.4k
    return have;
48
21.4k
}
49
50
CBlockLocator GetLocator(const CBlockIndex* index)
51
21.4k
{
52
21.4k
    return CBlockLocator{LocatorEntries(index)};
53
21.4k
}
54
55
CBlockLocator CChain::GetLocator() const
56
670
{
57
670
    return ::GetLocator(Tip());
58
670
}
59
60
2.14k
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
61
2.14k
    if (pindex == nullptr) {
62
0
        return nullptr;
63
0
    }
64
2.14k
    if (pindex->nHeight > Height())
65
1.07k
        pindex = pindex->GetAncestor(Height());
66
2.14k
    while (pindex && !Contains(pindex))
67
0
        pindex = pindex->pprev;
68
2.14k
    return pindex;
69
2.14k
}
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
16.6M
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
11.2M
int static inline GetSkipHeight(int height) {
84
11.2M
    if (height < 2)
85
159k
        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
11.1M
    return (height & 1) ? 
InvertLowestOne(InvertLowestOne(height - 1)) + 15.56M
:
InvertLowestOne(height)5.57M
;
91
11.2M
}
92
93
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
94
2.32M
{
95
2.32M
    if (height > nHeight || 
height < 02.20M
) {
96
131k
        return nullptr;
97
131k
    }
98
99
2.19M
    const CBlockIndex* pindexWalk = this;
100
2.19M
    int heightWalk = nHeight;
101
7.10M
    while (heightWalk > height) {
102
4.91M
        int heightSkip = GetSkipHeight(heightWalk);
103
4.91M
        int heightSkipPrev = GetSkipHeight(heightWalk - 1);
104
4.91M
        if (pindexWalk->pskip != nullptr &&
105
4.91M
            
(4.91M
heightSkip == height4.91M
||
106
4.91M
             
(4.04M
heightSkip > height4.04M
&&
!(1.62M
heightSkipPrev < heightSkip - 21.62M
&&
107
2.25M
                                       
heightSkipPrev >= height585k
)))) {
108
            // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
109
2.25M
            pindexWalk = pindexWalk->pskip;
110
2.25M
            heightWalk = heightSkip;
111
2.66M
        } else {
112
2.66M
            assert(pindexWalk->pprev);
113
2.66M
            pindexWalk = pindexWalk->pprev;
114
2.66M
            heightWalk--;
115
2.66M
        }
116
4.91M
    }
117
2.19M
    return pindexWalk;
118
2.19M
}
119
120
CBlockIndex* CBlockIndex::GetAncestor(int height)
121
1.65M
{
122
1.65M
    return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
123
1.65M
}
124
125
void CBlockIndex::BuildSkip()
126
1.46M
{
127
1.46M
    if (pprev)
128
1.46M
        pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
129
1.46M
}
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
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
1.64M
    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
0
{
148
0
    arith_uint256 r;
149
0
    int sign = 1;
150
0
    if (to.nChainWork > from.nChainWork) {
151
0
        r = to.nChainWork - from.nChainWork;
152
0
    } else {
153
0
        r = from.nChainWork - to.nChainWork;
154
0
        sign = -1;
155
0
    }
156
0
    r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
157
0
    if (r.bits() > 63) {
158
0
        return sign * std::numeric_limits<int64_t>::max();
159
0
    }
160
0
    return sign * int64_t(r.GetLow64());
161
0
}
162
163
/** Find the last common ancestor two blocks have.
164
 *  Both pa and pb must be non-nullptr. */
165
130k
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
166
130k
    if (pa->nHeight > pb->nHeight) {
167
0
        pa = pa->GetAncestor(pb->nHeight);
168
130k
    } else if (pb->nHeight > pa->nHeight) {
169
124k
        pb = pb->GetAncestor(pa->nHeight);
170
124k
    }
171
172
131k
    while (pa != pb && 
pa164
&&
pb164
) {
173
164
        pa = pa->pprev;
174
164
        pb = pb->pprev;
175
164
    }
176
177
    // Eventually all chain branches meet at the genesis block.
178
130k
    assert(pa == pb);
179
130k
    return pa;
180
130k
}