Coverage Report

Created: 2026-08-25 19:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/test/fuzz/pow.cpp
Line
Count
Source
1
// Copyright (c) 2020-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <chain.h>
6
#include <chainparams.h>
7
#include <pow.h>
8
#include <primitives/block.h>
9
#include <test/fuzz/FuzzedDataProvider.h>
10
#include <test/fuzz/fuzz.h>
11
#include <test/fuzz/util.h>
12
#include <util/chaintype.h>
13
#include <util/check.h>
14
#include <util/overflow.h>
15
16
#include <cstdint>
17
#include <optional>
18
#include <string>
19
#include <vector>
20
21
void initialize_pow()
22
0
{
23
0
    SelectParams(ChainType::MAIN);
24
0
}
25
26
FUZZ_TARGET(pow, .init = initialize_pow)
27
415
{
28
415
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
29
415
    const Consensus::Params& consensus_params = Params().GetConsensus();
30
415
    std::vector<std::unique_ptr<CBlockIndex>> blocks;
31
415
    const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
32
415
    const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
33
263k
    LIMITED_WHILE (fuzzed_data_provider.remaining_bytes() > 0, 10000) {
34
263k
        const std::optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider);
35
263k
        if (!block_header) {
  Branch (35:13): [True: 105k, False: 158k]
36
105k
            continue;
37
105k
        }
38
158k
        CBlockIndex& current_block{
39
158k
            *blocks.emplace_back(std::make_unique<CBlockIndex>(*block_header))};
40
158k
        {
41
158k
            CBlockIndex* previous_block = blocks.empty() ? nullptr : PickValue(fuzzed_data_provider, blocks).get();
  Branch (41:43): [True: 0, False: 158k]
42
158k
            const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
  Branch (42:41): [True: 158k, False: 0]
  Branch (42:70): [True: 158k, False: 0]
43
158k
            if (fuzzed_data_provider.ConsumeBool()) {
  Branch (43:17): [True: 79.4k, False: 78.7k]
44
79.4k
                current_block.pprev = previous_block;
45
79.4k
            }
46
158k
            if (fuzzed_data_provider.ConsumeBool()) {
  Branch (46:17): [True: 79.9k, False: 78.2k]
47
79.9k
                current_block.nHeight = current_height;
48
79.9k
            }
49
158k
            if (fuzzed_data_provider.ConsumeBool()) {
  Branch (49:17): [True: 79.8k, False: 78.3k]
50
79.8k
                const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing;
51
79.8k
                if (!AdditionOverflow(fixed_time, seconds)) {
  Branch (51:21): [True: 76.9k, False: 2.86k]
52
76.9k
                    current_block.nTime = fixed_time + seconds;
53
76.9k
                }
54
79.8k
            }
55
158k
            if (fuzzed_data_provider.ConsumeBool()) {
  Branch (55:17): [True: 80.0k, False: 78.1k]
56
80.0k
                current_block.nBits = fixed_bits;
57
80.0k
            }
58
158k
            if (fuzzed_data_provider.ConsumeBool()) {
  Branch (58:17): [True: 80.5k, False: 77.6k]
59
80.5k
                current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0};
  Branch (59:44): [True: 80.5k, False: 0]
60
80.5k
            } else {
61
77.6k
                current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider);
62
77.6k
            }
63
158k
        }
64
158k
        {
65
158k
            (void)GetBlockProof(current_block);
66
158k
            (void)CalculateNextWorkRequired(&current_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
67
158k
            if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
  Branch (67:17): [True: 158k, False: 0]
  Branch (67:77): [True: 0, False: 158k]
68
0
                (void)GetNextWorkRequired(&current_block, &(*block_header), consensus_params);
69
0
            }
70
158k
        }
71
158k
        {
72
158k
            const auto& to = PickValue(fuzzed_data_provider, blocks);
73
158k
            const auto& from = PickValue(fuzzed_data_provider, blocks);
74
158k
            const auto& tip = PickValue(fuzzed_data_provider, blocks);
75
158k
            try {
76
158k
                (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
77
158k
            } catch (const uint_error&) {
78
88.3k
            }
79
158k
        }
80
158k
        {
81
158k
            const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
82
158k
            if (hash) {
  Branch (82:17): [True: 78.2k, False: 79.9k]
83
78.2k
                (void)CheckProofOfWorkImpl(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params);
84
78.2k
            }
85
158k
        }
86
158k
    }
87
415
}
88
89
90
FUZZ_TARGET(pow_transition, .init = initialize_pow)
91
308
{
92
308
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
93
308
    const Consensus::Params& consensus_params{Params().GetConsensus()};
94
308
    std::vector<std::unique_ptr<CBlockIndex>> blocks;
95
96
308
    const uint32_t old_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
97
308
    const uint32_t new_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
98
308
    const int32_t version{fuzzed_data_provider.ConsumeIntegral<int32_t>()};
99
308
    uint32_t nbits{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
100
101
308
    const arith_uint256 pow_limit = UintToArith256(consensus_params.powLimit);
102
308
    arith_uint256 old_target;
103
308
    old_target.SetCompact(nbits);
104
308
    if (old_target > pow_limit) {
  Branch (104:9): [True: 5, False: 303]
105
5
        nbits = pow_limit.GetCompact();
106
5
    }
107
    // Create one difficulty adjustment period worth of headers
108
621k
    for (int height = 0; height < consensus_params.DifficultyAdjustmentInterval(); ++height) {
  Branch (108:26): [True: 620k, False: 308]
109
620k
        CBlockHeader header;
110
620k
        header.nVersion = version;
111
620k
        header.nTime = old_time;
112
620k
        header.nBits = nbits;
113
620k
        if (height == consensus_params.DifficultyAdjustmentInterval() - 1) {
  Branch (113:13): [True: 308, False: 620k]
114
308
            header.nTime = new_time;
115
308
        }
116
620k
        auto current_block{std::make_unique<CBlockIndex>(header)};
117
620k
        current_block->pprev = blocks.empty() ? nullptr : blocks.back().get();
  Branch (117:32): [True: 308, False: 620k]
118
620k
        current_block->nHeight = height;
119
620k
        blocks.emplace_back(std::move(current_block));
120
620k
    }
121
308
    auto last_block{blocks.back().get()};
122
308
    unsigned int new_nbits{GetNextWorkRequired(last_block, nullptr, consensus_params)};
123
308
    Assert(PermittedDifficultyTransition(consensus_params, last_block->nHeight + 1, last_block->nBits, new_nbits));
124
308
}