Skip to content

Commit 4839560

Browse files
author
MarcoFalke
committed
Merge #18407: tests: Add proof-of-work fuzzing harness
acf269e tests: Add proof-of-work fuzzing harness (practicalswift) Pull request description: Add proof-of-work fuzzing harness. Top commit has no ACKs. Tree-SHA512: dcdfa211cf1ec3018b61f378bb0f95793bbbe5d00e2f4d17f9db2c7263fe8ce919760c56cae7122c62c82e05c90e7056eb1778871674bdb3c42869e5fe4c2b60
2 parents c897154 + acf269e commit 4839560

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ FUZZ_TARGETS = \
6060
test/fuzz/parse_univalue \
6161
test/fuzz/partial_merkle_tree_deserialize \
6262
test/fuzz/partially_signed_transaction_deserialize \
63+
test/fuzz/pow \
6364
test/fuzz/prefilled_transaction_deserialize \
6465
test/fuzz/process_message \
6566
test/fuzz/process_message_addr \
@@ -634,6 +635,12 @@ test_fuzz_partially_signed_transaction_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMO
634635
test_fuzz_partially_signed_transaction_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
635636
test_fuzz_partially_signed_transaction_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
636637

638+
test_fuzz_pow_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
639+
test_fuzz_pow_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
640+
test_fuzz_pow_LDADD = $(FUZZ_SUITE_LD_COMMON)
641+
test_fuzz_pow_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
642+
test_fuzz_pow_SOURCES = $(FUZZ_SUITE) test/fuzz/pow.cpp
643+
637644
test_fuzz_prefilled_transaction_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DPREFILLED_TRANSACTION_DESERIALIZE=1
638645
test_fuzz_prefilled_transaction_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
639646
test_fuzz_prefilled_transaction_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/pow.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) 2020 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 <optional.h>
8+
#include <pow.h>
9+
#include <primitives/block.h>
10+
#include <test/fuzz/FuzzedDataProvider.h>
11+
#include <test/fuzz/fuzz.h>
12+
#include <test/fuzz/util.h>
13+
14+
#include <cstdint>
15+
#include <string>
16+
#include <vector>
17+
18+
void initialize()
19+
{
20+
SelectParams(CBaseChainParams::MAIN);
21+
}
22+
23+
void test_one_input(const std::vector<uint8_t>& buffer)
24+
{
25+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
26+
const Consensus::Params& consensus_params = Params().GetConsensus();
27+
std::vector<CBlockIndex> blocks;
28+
const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
29+
const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
30+
while (fuzzed_data_provider.remaining_bytes() > 0) {
31+
const Optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider);
32+
if (!block_header) {
33+
continue;
34+
}
35+
CBlockIndex current_block{*block_header};
36+
{
37+
CBlockIndex* previous_block = !blocks.empty() ? &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)] : nullptr;
38+
const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
39+
if (fuzzed_data_provider.ConsumeBool()) {
40+
current_block.pprev = previous_block;
41+
}
42+
if (fuzzed_data_provider.ConsumeBool()) {
43+
current_block.nHeight = current_height;
44+
}
45+
if (fuzzed_data_provider.ConsumeBool()) {
46+
current_block.nTime = fixed_time + current_height * consensus_params.nPowTargetSpacing;
47+
}
48+
if (fuzzed_data_provider.ConsumeBool()) {
49+
current_block.nBits = fixed_bits;
50+
}
51+
if (fuzzed_data_provider.ConsumeBool()) {
52+
current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0};
53+
} else {
54+
current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider);
55+
}
56+
blocks.push_back(current_block);
57+
}
58+
{
59+
(void)GetBlockProof(current_block);
60+
(void)CalculateNextWorkRequired(&current_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
61+
if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
62+
(void)GetNextWorkRequired(&current_block, &(*block_header), consensus_params);
63+
}
64+
}
65+
{
66+
const CBlockIndex* to = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)];
67+
const CBlockIndex* from = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)];
68+
const CBlockIndex* tip = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)];
69+
try {
70+
(void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
71+
} catch (const uint_error&) {
72+
}
73+
}
74+
{
75+
const Optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
76+
if (hash) {
77+
(void)CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params);
78+
}
79+
}
80+
}
81+
}

src/test/fuzz/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_TEST_FUZZ_UTIL_H
77

88
#include <amount.h>
9+
#include <arith_uint256.h>
910
#include <attributes.h>
1011
#include <optional.h>
1112
#include <script/script.h>
@@ -91,6 +92,11 @@ NODISCARD inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider
9192
return uint256{v256};
9293
}
9394

95+
NODISCARD inline arith_uint256 ConsumeArithUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
96+
{
97+
return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
98+
}
99+
94100
template <typename T>
95101
NODISCARD bool MultiplicationOverflow(const T i, const T j) noexcept
96102
{

0 commit comments

Comments
 (0)