Coverage Report

Created: 2026-04-20 22:07

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
298
{
28
298
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
29
298
    const Consensus::Params& consensus_params = Params().GetConsensus();
30
298
    std::vector<std::unique_ptr<CBlockIndex>> blocks;
31
298
    const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
32
298
    const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
33
167k
    LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 10000) {
34
167k
        const std::optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider);
35
167k
        if (!block_header) {
36
66.0k
            continue;
37
66.0k
        }
38
101k
        CBlockIndex& current_block{
39
101k
            *blocks.emplace_back(std::make_unique<CBlockIndex>(*block_header))};
40
101k
        {
41
101k
            CBlockIndex* previous_block = blocks.empty() ? nullptr : PickValue(fuzzed_data_provider, blocks).get();
42
101k
            const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
43
101k
            if (fuzzed_data_provider.ConsumeBool()) {
44
52.6k
                current_block.pprev = previous_block;
45
52.6k
            }
46
101k
            if (fuzzed_data_provider.ConsumeBool()) {
47
52.9k
                current_block.nHeight = current_height;
48
52.9k
            }
49
101k
            if (fuzzed_data_provider.ConsumeBool()) {
50
53.0k
                const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing;
51
53.0k
                if (!AdditionOverflow(fixed_time, seconds)) {
52
51.1k
                    current_block.nTime = fixed_time + seconds;
53
51.1k
                }
54
53.0k
            }
55
101k
            if (fuzzed_data_provider.ConsumeBool()) {
56
53.0k
                current_block.nBits = fixed_bits;
57
53.0k
            }
58
101k
            if (fuzzed_data_provider.ConsumeBool()) {
59
53.4k
                current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0};
60
53.4k
            } else {
61
48.1k
                current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider);
62
48.1k
            }
63
101k
        }
64
101k
        {
65
101k
            (void)GetBlockProof(current_block);
66
101k
            (void)CalculateNextWorkRequired(&current_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
67
101k
            if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
68
0
                (void)GetNextWorkRequired(&current_block, &(*block_header), consensus_params);
69
0
            }
70
101k
        }
71
101k
        {
72
101k
            const auto& to = PickValue(fuzzed_data_provider, blocks);
73
101k
            const auto& from = PickValue(fuzzed_data_provider, blocks);
74
101k
            const auto& tip = PickValue(fuzzed_data_provider, blocks);
75
101k
            try {
76
101k
                (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
77
101k
            } catch (const uint_error&) {
78
55.5k
            }
79
101k
        }
80
101k
        {
81
101k
            const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
82
101k
            if (hash) {
83
48.4k
                (void)CheckProofOfWorkImpl(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params);
84
48.4k
            }
85
101k
        }
86
101k
    }
87
298
}
88
89
90
FUZZ_TARGET(pow_transition, .init = initialize_pow)
91
173
{
92
173
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
93
173
    const Consensus::Params& consensus_params{Params().GetConsensus()};
94
173
    std::vector<std::unique_ptr<CBlockIndex>> blocks;
95
96
173
    const uint32_t old_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
97
173
    const uint32_t new_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
98
173
    const int32_t version{fuzzed_data_provider.ConsumeIntegral<int32_t>()};
99
173
    uint32_t nbits{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
100
101
173
    const arith_uint256 pow_limit = UintToArith256(consensus_params.powLimit);
102
173
    arith_uint256 old_target;
103
173
    old_target.SetCompact(nbits);
104
173
    if (old_target > pow_limit) {
105
1
        nbits = pow_limit.GetCompact();
106
1
    }
107
    // Create one difficulty adjustment period worth of headers
108
348k
    for (int height = 0; height < consensus_params.DifficultyAdjustmentInterval(); ++height) {
109
348k
        CBlockHeader header;
110
348k
        header.nVersion = version;
111
348k
        header.nTime = old_time;
112
348k
        header.nBits = nbits;
113
348k
        if (height == consensus_params.DifficultyAdjustmentInterval() - 1) {
114
173
            header.nTime = new_time;
115
173
        }
116
348k
        auto current_block{std::make_unique<CBlockIndex>(header)};
117
348k
        current_block->pprev = blocks.empty() ? nullptr : blocks.back().get();
118
348k
        current_block->nHeight = height;
119
348k
        blocks.emplace_back(std::move(current_block));
120
348k
    }
121
173
    auto last_block{blocks.back().get()};
122
173
    unsigned int new_nbits{GetNextWorkRequired(last_block, nullptr, consensus_params)};
123
173
    Assert(PermittedDifficultyTransition(consensus_params, last_block->nHeight + 1, last_block->nBits, new_nbits));
124
173
}