Repository: bitcoin/bitcoin
File: src/test/fuzz/block_index.cpp (C++)

Code before:
            files.push_back(std::make_unique<CBlockFileInfo>(std::move(*file_info)));
            files_info.emplace_back(i, files.back().get());
        } else {
            return;
        }
    }

    // Generate a number of block headers to be stored in the index.
    int blocks_count = fuzzed_data_provider.ConsumeIntegralInRange(files_count * 10, files_count * 100);
    std::vector<std::unique_ptr<CBlockIndex>> blocks;
    blocks.reserve(blocks_count);
    std::vector<const CBlockIndex*> blocks_info;
    blocks_info.reserve(blocks_count);
    for (int i = 0; i < blocks_count; i++) {
        CBlockHeader header{ConsumeBlockHeader(fuzzed_data_provider)};
        blocks.push_back(std::make_unique<CBlockIndex>(std::move(header)));
        blocks.back()->phashBlock = &g_block_hash;
        blocks_info.push_back(blocks.back().get());
    }

    // Store these files and blocks in the block index. It should not fail.
    assert(block_index.WriteBatchSync(files_info, files_count - 1, blocks_info));

    // We should be able to read every block file info we stored. Its value should correspond to
    // what we stored above.
    CBlockFileInfo info;
    for (const auto& [n, file_info]: files_info) {
        assert(block_index.ReadBlockFileInfo(n, info));
        assert(info == *file_info);
    }

    // We should be able to read the last block file number. Its value should be consistent.
    int last_block_file;
    assert(block_index.ReadLastBlockFile(last_block_file));
    assert(last_block_file == files_count - 1);

    // We should be able to flip and read the reindexing flag.
    bool reindexing;
    block_index.WriteReindexing(true);
    block_index.ReadReindexing(reindexing);
    assert(reindexing);
    block_index.WriteReindexing(false);
    block_index.ReadReindexing(reindexing);
    assert(!reindexing);

    // We should be able to set and read the value of any random flag.
    int flag_size = fuzzed_data_provider.ConsumeIntegralInRange(0, 100);
    const std::string flag_name = fuzzed_data_provider.ConsumeBytesAsString(flag_size);
    bool flag_value;
    block_index.WriteFlag(flag_name, true);
    block_index.ReadFlag(flag_name, flag_value);

Review comment (performance):
nit (feel free to ignore):

```diff
diff --git a/src/test/fuzz/block_index.cpp b/src/test/fuzz/block_index.cpp
index 7885eda747..bc073e4930 100644
--- a/src/test/fuzz/block_index.cpp
+++ b/src/test/fuzz/block_index.cpp
@@ -93,8 +93,8 @@ FUZZ_TARGET(block_index, .init = init_block_index)
 
     // We should be able to read every block file info we stored. Its value should correspond to
     // what we stored above.
-    CBlockFileInfo info;
     for (const auto& [n, file_info]: files_info) {
+        CBlockFileInfo info;
         assert(block_index.ReadBlockFileInfo(n, info));
         assert(info == *file_info);
     }
```

Code after:
            files.push_back(std::make_unique<CBlockFileInfo>(std::move(*file_info)));
            files_info.emplace_back(i, files.back().get());
        } else {
            return;
        }
    }

    // Generate a number of block headers to be stored in the index.
    int blocks_count = fuzzed_data_provider.ConsumeIntegralInRange(files_count * 10, files_count * 100);
    std::vector<std::unique_ptr<CBlockIndex>> blocks;
    blocks.reserve(blocks_count);
    std::vector<const CBlockIndex*> blocks_info;
    blocks_info.reserve(blocks_count);
    for (int i = 0; i < blocks_count; i++) {
        CBlockHeader header{ConsumeBlockHeader(fuzzed_data_provider)};
        blocks.push_back(std::make_unique<CBlockIndex>(std::move(header)));
        blocks.back()->phashBlock = &g_block_hash;
        blocks_info.push_back(blocks.back().get());
    }

    // Store these files and blocks in the block index. It should not fail.
    assert(block_index.WriteBatchSync(files_info, files_count - 1, blocks_info));

    // We should be able to read every block file info we stored. Its value should correspond to
    // what we stored above.
    CBlockFileInfo info;
    for (const auto& [n, file_info]: files_info) {
        assert(block_index.ReadBlockFileInfo(n, info));
        assert(info == *file_info);
    }

    // We should be able to read the last block file number. Its value should be consistent.
    int last_block_file;
    assert(block_index.ReadLastBlockFile(last_block_file));
    assert(last_block_file == files_count - 1);

    // We should be able to flip and read the reindexing flag.
    bool reindexing;
    block_index.WriteReindexing(true);
    block_index.ReadReindexing(reindexing);
    assert(reindexing);
    block_index.WriteReindexing(false);
    block_index.ReadReindexing(reindexing);
    assert(!reindexing);

    // We should be able to set and read the value of any random flag.
    const std::string flag_name = fuzzed_data_provider.ConsumeRandomLengthString(100);
    bool flag_value;
    block_index.WriteFlag(flag_name, true);
    block_index.ReadFlag(flag_name, flag_value);
    assert(flag_value);