Skip to content

Commit 2db0027

Browse files
committed
Merge bitcoin/bitcoin#31910: qa: fix an off-by-one in utxo snapshot fuzz target and sanity check its snapshot data
63b534f fuzz: sanity check hardcoded snapshot in utxo_snapshot target (Antoine Poinsot) 3b85eba test util: split up ConnectBlock from MineBlock (Antoine Poinsot) d1527f6 qa: correct off-by-one in utxo snapshot fuzz target (Antoine Poinsot) Pull request description: The assumeutxo data for the fuzz target could change and invalidate the hash silently, preventing the fuzz target from reaching some code paths. Fix this by introducing a unit test which would break if the snapshot data the fuzz target relies on were to change. In implementing this i noticed the height used for coins in the fuzz target is actually off-by-one (as if the first block in the created chain was the genesis but it's block `1`), so fix that too. ACKs for top commit: mzumsande: Code Review ACK 63b534f fjahr: tACK 63b534f Tree-SHA512: 2399b6e74db9b78aab8efba67c57a405d2d7d880ae3b7d8518a1c96cc6266f61f5e77722cd999adeac5d3e03e73d84cf9ae7bdbcc0afae198cc87049dde4012f
2 parents c9a6150 + 63b534f commit 2db0027

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/kernel/chainparams.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ class CRegTestParams : public CChainParams
589589
{
590590
// For use by fuzz target src/test/fuzz/utxo_snapshot.cpp
591591
.height = 200,
592-
.hash_serialized = AssumeutxoHash{uint256{"4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2"}},
592+
.hash_serialized = AssumeutxoHash{uint256{"7e3b7780fbd2fa479a01f66950dc8f728dc1b11f03d06d5bf223168520df3a48"}},
593593
.m_chain_tx_count = 201,
594594
.blockhash = consteval_ctor(uint256{"5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"}),
595595
},

src/test/fuzz/utxo_snapshot.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <coins.h>
88
#include <consensus/consensus.h>
99
#include <consensus/validation.h>
10+
#include <kernel/coinstats.h>
1011
#include <node/blockstorage.h>
1112
#include <node/utxo_snapshot.h>
1213
#include <primitives/block.h>
@@ -39,14 +40,42 @@ using node::SnapshotMetadata;
3940
namespace {
4041

4142
const std::vector<std::shared_ptr<CBlock>>* g_chain;
42-
TestingSetup* g_setup;
43+
TestingSetup* g_setup{nullptr};
44+
45+
/** Sanity check the assumeutxo values hardcoded in chainparams for the fuzz target. */
46+
void sanity_check_snapshot()
47+
{
48+
Assert(g_chain && g_setup == nullptr);
49+
50+
// Create a temporary chainstate manager to connect the chain to.
51+
const auto tmp_setup{MakeNoLogFileContext<TestingSetup>(ChainType::REGTEST, TestOpts{.setup_net = false})};
52+
const auto& node{tmp_setup->m_node};
53+
for (auto& block: *g_chain) {
54+
ProcessBlock(node, block);
55+
}
56+
57+
// Connect the chain to the tmp chainman and sanity check the chainparams snapshot values.
58+
LOCK(cs_main);
59+
auto& cs{node.chainman->ActiveChainstate()};
60+
cs.ForceFlushStateToDisk();
61+
const auto stats{*Assert(kernel::ComputeUTXOStats(kernel::CoinStatsHashType::HASH_SERIALIZED, &cs.CoinsDB(), node.chainman->m_blockman))};
62+
const auto cp_au_data{*Assert(node.chainman->GetParams().AssumeutxoForHeight(2 * COINBASE_MATURITY))};
63+
Assert(stats.nHeight == cp_au_data.height);
64+
Assert(stats.nTransactions + 1 == cp_au_data.m_chain_tx_count); // +1 for the genesis tx.
65+
Assert(stats.hashBlock == cp_au_data.blockhash);
66+
Assert(AssumeutxoHash{stats.hashSerialized} == cp_au_data.hash_serialized);
67+
}
4368

4469
template <bool INVALID>
4570
void initialize_chain()
4671
{
4772
const auto params{CreateChainParams(ArgsManager{}, ChainType::REGTEST)};
4873
static const auto chain{CreateBlockChain(2 * COINBASE_MATURITY, *params)};
4974
g_chain = &chain;
75+
76+
// Make sure we can generate a valid snapshot.
77+
sanity_check_snapshot();
78+
5079
static const auto setup{
5180
MakeNoLogFileContext<TestingSetup>(ChainType::REGTEST,
5281
TestOpts{
@@ -101,7 +130,7 @@ void utxo_snapshot_fuzz(FuzzBufferType buffer)
101130
std::vector<uint8_t> file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
102131
outfile << std::span{file_data};
103132
} else {
104-
int height{0};
133+
int height{1};
105134
for (const auto& block : *g_chain) {
106135
auto coinbase{block->vtx.at(0)};
107136
outfile << coinbase->GetHash();

src/test/util/mining.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ COutPoint MineBlock(const NodeContext& node, std::shared_ptr<CBlock>& block)
9292
assert(block->nNonce);
9393
}
9494

95+
return ProcessBlock(node, block);
96+
}
97+
98+
COutPoint ProcessBlock(const NodeContext& node, const std::shared_ptr<CBlock>& block)
99+
{
95100
auto& chainman{*Assert(node.chainman)};
96101
const auto old_height = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight());
97102
bool new_block;

src/test/util/mining.h

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ COutPoint MineBlock(const node::NodeContext&,
3232
**/
3333
COutPoint MineBlock(const node::NodeContext&, std::shared_ptr<CBlock>& block);
3434

35+
/**
36+
* Returns the generated coin (or Null if the block was invalid).
37+
*/
38+
COutPoint ProcessBlock(const node::NodeContext&, const std::shared_ptr<CBlock>& block);
39+
3540
/** Prepare a block to be mined */
3641
std::shared_ptr<CBlock> PrepareBlock(const node::NodeContext&);
3742
std::shared_ptr<CBlock> PrepareBlock(const node::NodeContext& node,

0 commit comments

Comments
 (0)