Branch data Line data Source code
1 : : // Copyright (c) 2023-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 <node/blockstorage.h>
8 : : #include <test/fuzz/FuzzedDataProvider.h>
9 : : #include <test/fuzz/fuzz.h>
10 : : #include <test/fuzz/util.h>
11 : : #include <test/util/setup_common.h>
12 : : #include <txdb.h>
13 : : #include <util/byte_units.h>
14 : : #include <util/check.h>
15 : : #include <validation.h>
16 : :
17 : : using kernel::CBlockFileInfo;
18 : :
19 : : namespace {
20 : :
21 : : const BasicTestingSetup* g_setup;
22 : :
23 : : // Hardcoded block hash and nBits to make sure the blocks we store pass the pow check.
24 : : uint256 g_block_hash;
25 : :
26 : 21671 : bool operator==(const CBlockFileInfo& a, const CBlockFileInfo& b)
27 : : {
28 : 43342 : return a.nBlocks == b.nBlocks &&
29 [ + - ]: 21671 : a.nSize == b.nSize &&
30 [ + - ]: 21671 : a.nUndoSize == b.nUndoSize &&
31 [ + - ]: 21671 : a.nHeightFirst == b.nHeightFirst &&
32 [ + - ]: 21671 : a.nHeightLast == b.nHeightLast &&
33 [ + - + - ]: 43342 : a.nTimeFirst == b.nTimeFirst &&
34 [ - + ]: 21671 : a.nTimeLast == b.nTimeLast;
35 : : }
36 : :
37 : 1807037 : CBlockHeader ConsumeBlockHeader(FuzzedDataProvider& provider)
38 : : {
39 : 1807037 : CBlockHeader header;
40 : 1807037 : header.nVersion = provider.ConsumeIntegral<decltype(header.nVersion)>();
41 : 1807037 : header.hashPrevBlock = g_block_hash;
42 : 1807037 : header.hashMerkleRoot = g_block_hash;
43 : 1807037 : header.nTime = provider.ConsumeIntegral<decltype(header.nTime)>();
44 : 1807037 : header.nBits = Params().GenesisBlock().nBits;
45 : 1807037 : header.nNonce = provider.ConsumeIntegral<decltype(header.nNonce)>();
46 : 1807037 : return header;
47 : : }
48 : :
49 : : } // namespace
50 : :
51 : 1 : void init_block_index()
52 : : {
53 [ + - + - : 1 : static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
+ - ]
54 : 1 : g_setup = testing_setup.get();
55 : 1 : g_block_hash = Params().GenesisBlock().GetHash();
56 : 1 : }
57 : :
58 [ + - ]: 1037 : FUZZ_TARGET(block_index, .init = init_block_index)
59 : : {
60 : 561 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
61 [ + - ]: 561 : auto block_index = kernel::BlockTreeDB(DBParams{
62 : : .path = "", // Memory only.
63 : : .cache_bytes = 1_MiB,
64 : : .memory_only = true,
65 : 561 : });
66 : :
67 : : // Generate a number of block files to be stored in the index.
68 : 561 : int files_count = fuzzed_data_provider.ConsumeIntegralInRange(1, 100);
69 : 561 : std::vector<std::unique_ptr<CBlockFileInfo>> files;
70 [ + - ]: 561 : files.reserve(files_count);
71 : 561 : std::vector<std::pair<int, const CBlockFileInfo*>> files_info;
72 [ + - ]: 561 : files_info.reserve(files_count);
73 [ + + ]: 22256 : for (int i = 0; i < files_count; i++) {
74 [ + + ]: 21724 : if (auto file_info = ConsumeDeserializable<CBlockFileInfo>(fuzzed_data_provider)) {
75 [ + - ]: 43390 : files.push_back(std::make_unique<CBlockFileInfo>(std::move(*file_info)));
76 [ + - ]: 21695 : files_info.emplace_back(i, files.back().get());
77 : : } else {
78 : 29 : return;
79 : : }
80 : : }
81 : :
82 : : // Generate a number of block headers to be stored in the index.
83 : 532 : int blocks_count = fuzzed_data_provider.ConsumeIntegralInRange(files_count * 10, files_count * 100);
84 : 532 : std::vector<std::unique_ptr<CBlockIndex>> blocks;
85 [ + - ]: 532 : blocks.reserve(blocks_count);
86 : 532 : std::vector<const CBlockIndex*> blocks_info;
87 [ + - ]: 532 : blocks_info.reserve(blocks_count);
88 [ + + ]: 1807569 : for (int i = 0; i < blocks_count; i++) {
89 [ + - ]: 1807037 : CBlockHeader header{ConsumeBlockHeader(fuzzed_data_provider)};
90 [ + - ]: 3614074 : blocks.push_back(std::make_unique<CBlockIndex>(std::move(header)));
91 [ + - ]: 1807037 : blocks.back()->phashBlock = &g_block_hash;
92 [ + - ]: 1807037 : blocks_info.push_back(blocks.back().get());
93 : : }
94 : :
95 : : // Store these files and blocks in the block index. It should not fail.
96 [ + - ]: 532 : block_index.WriteBatchSync(files_info, files_count - 1, blocks_info);
97 : :
98 : : // We should be able to read every block file info we stored. Its value should correspond to
99 : : // what we stored above.
100 : 532 : CBlockFileInfo info;
101 [ + - + + ]: 22203 : for (const auto& [n, file_info]: files_info) {
102 [ + - - + ]: 21671 : assert(block_index.ReadBlockFileInfo(n, info));
103 [ - + ]: 21671 : assert(info == *file_info);
104 : : }
105 : :
106 : : // We should be able to read the last block file number. Its value should be consistent.
107 : 532 : int last_block_file;
108 [ + - - + ]: 532 : assert(block_index.ReadLastBlockFile(last_block_file));
109 [ - + ]: 532 : assert(last_block_file == files_count - 1);
110 : :
111 : : // We should be able to flip and read the reindexing flag.
112 : 532 : bool reindexing;
113 [ + - ]: 532 : block_index.WriteReindexing(true);
114 [ + - ]: 532 : block_index.ReadReindexing(reindexing);
115 [ - + ]: 532 : assert(reindexing);
116 [ + - ]: 532 : block_index.WriteReindexing(false);
117 [ + - ]: 532 : block_index.ReadReindexing(reindexing);
118 [ - + ]: 532 : assert(!reindexing);
119 : :
120 : : // We should be able to set and read the value of any random flag.
121 [ + - ]: 532 : const std::string flag_name = fuzzed_data_provider.ConsumeRandomLengthString(100);
122 : 532 : bool flag_value;
123 [ + - ]: 532 : block_index.WriteFlag(flag_name, true);
124 [ + - ]: 532 : block_index.ReadFlag(flag_name, flag_value);
125 [ - + ]: 532 : assert(flag_value);
126 [ + - ]: 532 : block_index.WriteFlag(flag_name, false);
127 [ + - ]: 532 : block_index.ReadFlag(flag_name, flag_value);
128 [ - + ]: 532 : assert(!flag_value);
129 : :
130 : : // We should be able to load everything we've previously stored. Note to assert on the
131 : : // return value we need to make sure all blocks pass the pow check.
132 [ + - + - ]: 532 : const auto params{Params().GetConsensus()};
133 : 1596 : const auto inserter = [&](const uint256&) {
134 : 1064 : return blocks.back().get();
135 : 532 : };
136 [ + - ][ + - : 2128 : WITH_LOCK(::cs_main, assert(block_index.LoadBlockIndexGuts(params, inserter, g_setup->m_interrupt)));
- + + - ]
137 : 561 : }
|