Skip to content

Commit 5239e93

Browse files
committed
Merge bitcoin/bitcoin#30329: fuzz: improve utxo_snapshot target
de71d4d fuzz: improve utxo_snapshot target (Martin Zumsande) Pull request description: Add the possibility of giving more guidance to the creation of the metadata and/or coins, so that the fuzzer gets the chance to reach more error conditions in ActivateSnapshot and sometimes successfully creates a valid snapshot. This also changes the asserts for the success case that were outdated (after #29370) and only didn't result in a crash because the fuzzer wasn't able to reach this code before. ACKs for top commit: maflcko: re-ACK de71d4d 🎆 fjahr: utACK de71d4d TheCharlatan: ACK de71d4d Tree-SHA512: 346974d594164544d8cd3df7d8362c905fd93116215e9f5df308dfdac55bab04d727bfd7fd001cf11318682d11ee329b4b4a43308124c04d64b67840ab8a58a0
2 parents c06b376 + de71d4d commit 5239e93

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/kernel/chainparams.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,19 @@ class CRegTestParams : public CChainParams
495495
};
496496

497497
m_assumeutxo_data = {
498-
{
498+
{ // For use by unit tests
499499
.height = 110,
500500
.hash_serialized = AssumeutxoHash{uint256S("0x6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1")},
501501
.nChainTx = 111,
502502
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c")
503503
},
504+
{
505+
// For use by fuzz target src/test/fuzz/utxo_snapshot.cpp
506+
.height = 200,
507+
.hash_serialized = AssumeutxoHash{uint256S("0x4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2")},
508+
.nChainTx = 201,
509+
.blockhash = uint256S("0x5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"),
510+
},
504511
{
505512
// For use by test/functional/feature_assumeutxo.py
506513
.height = 299,

src/test/fuzz/utxo_snapshot.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,39 @@ FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)
4141

4242
{
4343
AutoFile outfile{fsbridge::fopen(snapshot_path, "wb")};
44-
const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
45-
outfile << Span{file_data};
44+
// Metadata
45+
if (fuzzed_data_provider.ConsumeBool()) {
46+
std::vector<uint8_t> metadata{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
47+
outfile << Span{metadata};
48+
} else {
49+
DataStream data_stream{};
50+
auto msg_start = chainman.GetParams().MessageStart();
51+
int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 2 * COINBASE_MATURITY)};
52+
uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
53+
uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 * COINBASE_MATURITY)};
54+
SnapshotMetadata metadata{msg_start, base_blockhash, base_blockheight, m_coins_count};
55+
outfile << metadata;
56+
}
57+
// Coins
58+
if (fuzzed_data_provider.ConsumeBool()) {
59+
std::vector<uint8_t> file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
60+
outfile << Span{file_data};
61+
} else {
62+
int height{0};
63+
for (const auto& block : *g_chain) {
64+
auto coinbase{block->vtx.at(0)};
65+
outfile << coinbase->GetHash();
66+
WriteCompactSize(outfile, 1); // number of coins for the hash
67+
WriteCompactSize(outfile, 0); // index of coin
68+
outfile << Coin(coinbase->vout[0], height, /*fCoinBaseIn=*/1);
69+
height++;
70+
}
71+
}
4672
}
4773

4874
const auto ActivateFuzzedSnapshot{[&] {
4975
AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
50-
auto msg_start = Params().MessageStart();
76+
auto msg_start = chainman.GetParams().MessageStart();
5177
SnapshotMetadata metadata{msg_start};
5278
try {
5379
infile >> metadata;
@@ -73,16 +99,20 @@ FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)
7399
Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
74100
*chainman.SnapshotBlockhash());
75101
const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
76-
int64_t chain_tx{};
77102
for (const auto& block : *g_chain) {
78103
Assert(coinscache.HaveCoin(COutPoint{block->vtx.at(0)->GetHash(), 0}));
79104
const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
80-
const auto num_tx{Assert(index)->nTx};
81-
Assert(num_tx == 1);
82-
chain_tx += num_tx;
105+
Assert(index);
106+
Assert(index->nTx == 0);
107+
if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
108+
auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
109+
Assert(params.has_value());
110+
Assert(params.value().nChainTx == index->nChainTx);
111+
} else {
112+
Assert(index->nChainTx == 0);
113+
}
83114
}
84115
Assert(g_chain->size() == coinscache.GetCacheSize());
85-
Assert(chain_tx == chainman.ActiveTip()->nChainTx);
86116
} else {
87117
Assert(!chainman.SnapshotBlockhash());
88118
Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);

test/functional/feature_assumeutxo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def expected_error(log_msg="", rpc_details=""):
117117
with open(bad_snapshot_path, 'wb') as f:
118118
f.write(valid_snapshot_contents[:11] + bogus_height.to_bytes(4, "little") + bytes.fromhex(bad_block_hash)[::-1] + valid_snapshot_contents[47:])
119119

120-
msg = f"Unable to load UTXO snapshot: assumeutxo block hash in snapshot metadata not recognized (hash: {bad_block_hash}, height: {bogus_height}). The following snapshot heights are available: 110, 299."
120+
msg = f"Unable to load UTXO snapshot: assumeutxo block hash in snapshot metadata not recognized (hash: {bad_block_hash}, height: {bogus_height}). The following snapshot heights are available: 110, 200, 299."
121121
assert_raises_rpc_error(-32603, msg, node.loadtxoutset, bad_snapshot_path)
122122

123123
self.log.info(" - snapshot file with wrong number of coins")

0 commit comments

Comments
 (0)